Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | |
| 2 // for details. All rights reserved. Use of this source code is governed by a | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 #include "vm/globals.h" | |
| 6 #if defined(TARGET_OS_LINUX) | |
| 7 | |
| 8 #include "vm/cpuinfo.h" | |
| 9 #include "vm/cpuid.h" | |
| 10 #include "vm/proccpuinfo.h" | |
| 11 | |
| 12 #include "platform/assert.h" | |
| 13 | |
| 14 // As with Windows, on IA32 and X64, we use the cpuid instruction. | |
| 15 // The analogous instruction is privileged on ARM and MIPS, so we resort to | |
| 16 // reading from /proc/cpuinfo. | |
| 17 | |
| 18 namespace dart { | |
| 19 | |
| 20 CpuInfoMethod CpuInfo::method_ = kCpuInfoDefault; | |
| 21 const char* CpuInfo::fields_[kCpuInfoMax] = {0}; | |
| 22 | |
| 23 void CpuInfo::InitOnce(CpuInfoMethod method) { | |
| 24 // The default on Linux is kCpuInfoCpuId. | |
| 25 method_ = | |
| 26 ((method == kCpuInfoDefault) || (method == kCpuInfoCpuId)) ? | |
| 27 kCpuInfoCpuId : kCpuInfoSystem; | |
| 28 | |
| 29 #if defined(HOST_ARCH_IA32) || defined(HOST_ARCH_X64) | |
| 30 fields_[kCpuInfoProcessor] = "vendor_id"; | |
| 31 fields_[kCpuInfoModel] = "model name"; | |
| 32 fields_[kCpuInfoHardware] = "model name"; | |
| 33 fields_[kCpuInfoFeatures] = "flags"; | |
| 34 #elif defined(HOST_ARCH_ARM) | |
| 35 ASSERT(method_ != kCpuInfoCpuId); // Method does not exist. | |
| 36 fields_[kCpuInfoProcessor] = "Processor"; | |
| 37 fields_[kCpuInfoModel] = "model name"; | |
| 38 fields_[kCpuInfoHardware] = "Hardware"; | |
| 39 fields_[kCpuInfoFeatures] = "Features"; | |
| 40 #elif defined(HOST_ARCH_MIPS) | |
| 41 ASSERT(method_ != kCpuInfoCpuId); // Method does not exist. | |
| 42 fields_[kCpuInfoProcessor] = "system type"; | |
| 43 fields_[kCpuInfoModel] = "cpu model"; | |
| 44 fields_[kCpuInfoHardware] = "cpu model"; | |
| 45 fields_[kCpuInfoFeatures] = "ASEs implemented"; | |
| 46 #else | |
| 47 #error Unrecognized target architecture | |
| 48 #endif | |
| 49 | |
| 50 if (method_ == kCpuInfoCpuId) { | |
| 51 CpuId::InitOnce(); | |
| 52 } else { | |
| 53 ASSERT(method_ == kCpuInfoSystem); | |
| 54 ProcCpuInfo::InitOnce(); | |
| 55 } | |
|
siva
2014/02/21 19:28:55
As discussed offline we could enforce either CpuId
zra
2014/02/21 21:51:55
Done.
| |
| 56 } | |
| 57 | |
| 58 | |
| 59 bool CpuInfo::FieldContains(CpuInfoIndices idx, const char* search_string) { | |
| 60 if (method_ == kCpuInfoCpuId) { | |
| 61 return strstr(CpuId::field(idx), search_string); | |
| 62 } else { | |
| 63 ASSERT(method_ == kCpuInfoSystem); | |
| 64 return ProcCpuInfo::FieldContains(FieldName(idx), search_string); | |
| 65 } | |
| 66 } | |
| 67 | |
| 68 | |
| 69 bool CpuInfo::FieldContainsByString(const char* field, | |
| 70 const char* search_string) { | |
| 71 if (method_ == kCpuInfoCpuId) { | |
| 72 for (int i = 0; i < kCpuInfoMax; i++) { | |
| 73 if (strcmp(field, fields_[i]) == 0) { | |
| 74 return FieldContains(static_cast<CpuInfoIndices>(i), search_string); | |
| 75 } | |
| 76 } | |
| 77 UNIMPLEMENTED(); | |
| 78 return false; | |
| 79 } else { | |
| 80 ASSERT(method_ == kCpuInfoSystem); | |
| 81 return ProcCpuInfo::FieldContains(field, search_string); | |
| 82 } | |
| 83 } | |
| 84 | |
| 85 | |
| 86 const char* CpuInfo::ExtractField(CpuInfoIndices idx) { | |
| 87 if (method_ == kCpuInfoCpuId) { | |
| 88 return CpuId::field(idx); | |
| 89 } else { | |
| 90 ASSERT(method_ == kCpuInfoSystem); | |
| 91 return ProcCpuInfo::ExtractField(FieldName(idx)); | |
| 92 } | |
| 93 } | |
| 94 | |
| 95 | |
| 96 const char* CpuInfo::ExtractFieldByString(const char* field) { | |
| 97 if (method_ == kCpuInfoCpuId) { | |
| 98 for (int i = 0; i < kCpuInfoMax; i++) { | |
| 99 if (strcmp(field, fields_[i]) == 0) { | |
| 100 return ExtractField(static_cast<CpuInfoIndices>(i)); | |
| 101 } | |
| 102 } | |
| 103 UNIMPLEMENTED(); | |
| 104 return NULL; | |
| 105 } else { | |
| 106 ASSERT(method_ == kCpuInfoSystem); | |
| 107 return ProcCpuInfo::ExtractField(field); | |
| 108 } | |
| 109 } | |
| 110 | |
| 111 | |
| 112 bool CpuInfo::HasField(const char* field) { | |
| 113 if (method_ == kCpuInfoCpuId) { | |
| 114 return (strcmp(field, fields_[kCpuInfoProcessor]) == 0) || | |
| 115 (strcmp(field, fields_[kCpuInfoModel]) == 0) || | |
| 116 (strcmp(field, fields_[kCpuInfoHardware]) == 0) || | |
| 117 (strcmp(field, fields_[kCpuInfoFeatures]) == 0); | |
| 118 } else { | |
| 119 ASSERT(method_ == kCpuInfoSystem); | |
| 120 return ProcCpuInfo::HasField(field); | |
| 121 } | |
| 122 } | |
| 123 | |
| 124 } // namespace dart | |
| 125 | |
| 126 #endif // defined(TARGET_OS_LINUX) | |
| OLD | NEW |