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() { |
| 24 #if defined(HOST_ARCH_IA32) || defined(HOST_ARCH_X64) |
| 25 fields_[kCpuInfoProcessor] = "vendor_id"; |
| 26 fields_[kCpuInfoModel] = "model name"; |
| 27 fields_[kCpuInfoHardware] = "model name"; |
| 28 fields_[kCpuInfoFeatures] = "flags"; |
| 29 method_ = kCpuInfoCpuId; |
| 30 CpuId::InitOnce(); |
| 31 #elif defined(HOST_ARCH_ARM) |
| 32 fields_[kCpuInfoProcessor] = "Processor"; |
| 33 fields_[kCpuInfoModel] = "model name"; |
| 34 fields_[kCpuInfoHardware] = "Hardware"; |
| 35 fields_[kCpuInfoFeatures] = "Features"; |
| 36 method_ = kCpuInfoSystem; |
| 37 ProcCpuInfo::InitOnce(); |
| 38 #elif defined(HOST_ARCH_MIPS) |
| 39 fields_[kCpuInfoProcessor] = "system type"; |
| 40 fields_[kCpuInfoModel] = "cpu model"; |
| 41 fields_[kCpuInfoHardware] = "cpu model"; |
| 42 fields_[kCpuInfoFeatures] = "ASEs implemented"; |
| 43 method_ = kCpuInfoSystem; |
| 44 ProcCpuInfo::InitOnce(); |
| 45 #else |
| 46 #error Unrecognized target architecture |
| 47 #endif |
| 48 } |
| 49 |
| 50 |
| 51 void CpuInfo::Cleanup() { |
| 52 if (method_ == kCpuInfoCpuId) { |
| 53 CpuId::Cleanup(); |
| 54 } else { |
| 55 ASSERT(method_ == kCpuInfoSystem); |
| 56 ProcCpuInfo::Cleanup(); |
| 57 } |
| 58 } |
| 59 |
| 60 |
| 61 bool CpuInfo::FieldContains(CpuInfoIndices idx, const char* search_string) { |
| 62 if (method_ == kCpuInfoCpuId) { |
| 63 return strstr(CpuId::field(idx), search_string); |
| 64 } else { |
| 65 ASSERT(method_ == kCpuInfoSystem); |
| 66 return ProcCpuInfo::FieldContains(FieldName(idx), search_string); |
| 67 } |
| 68 } |
| 69 |
| 70 |
| 71 bool CpuInfo::FieldContainsByString(const char* field, |
| 72 const char* search_string) { |
| 73 if (method_ == kCpuInfoCpuId) { |
| 74 for (int i = 0; i < kCpuInfoMax; i++) { |
| 75 if (strcmp(field, fields_[i]) == 0) { |
| 76 return FieldContains(static_cast<CpuInfoIndices>(i), search_string); |
| 77 } |
| 78 } |
| 79 UNIMPLEMENTED(); |
| 80 return false; |
| 81 } else { |
| 82 ASSERT(method_ == kCpuInfoSystem); |
| 83 return ProcCpuInfo::FieldContains(field, search_string); |
| 84 } |
| 85 } |
| 86 |
| 87 |
| 88 const char* CpuInfo::ExtractField(CpuInfoIndices idx) { |
| 89 if (method_ == kCpuInfoCpuId) { |
| 90 return CpuId::field(idx); |
| 91 } else { |
| 92 ASSERT(method_ == kCpuInfoSystem); |
| 93 return ProcCpuInfo::ExtractField(FieldName(idx)); |
| 94 } |
| 95 } |
| 96 |
| 97 |
| 98 const char* CpuInfo::ExtractFieldByString(const char* field) { |
| 99 if (method_ == kCpuInfoCpuId) { |
| 100 for (int i = 0; i < kCpuInfoMax; i++) { |
| 101 if (strcmp(field, fields_[i]) == 0) { |
| 102 return ExtractField(static_cast<CpuInfoIndices>(i)); |
| 103 } |
| 104 } |
| 105 UNIMPLEMENTED(); |
| 106 return NULL; |
| 107 } else { |
| 108 ASSERT(method_ == kCpuInfoSystem); |
| 109 return ProcCpuInfo::ExtractField(field); |
| 110 } |
| 111 } |
| 112 |
| 113 |
| 114 bool CpuInfo::HasField(const char* field) { |
| 115 if (method_ == kCpuInfoCpuId) { |
| 116 return (strcmp(field, fields_[kCpuInfoProcessor]) == 0) || |
| 117 (strcmp(field, fields_[kCpuInfoModel]) == 0) || |
| 118 (strcmp(field, fields_[kCpuInfoHardware]) == 0) || |
| 119 (strcmp(field, fields_[kCpuInfoFeatures]) == 0); |
| 120 } else { |
| 121 ASSERT(method_ == kCpuInfoSystem); |
| 122 return ProcCpuInfo::HasField(field); |
| 123 } |
| 124 } |
| 125 |
| 126 } // namespace dart |
| 127 |
| 128 #endif // defined(TARGET_OS_LINUX) |
OLD | NEW |