| 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(HOST_OS_LINUX) |
| 7 | |
| 8 | #include "vm/cpuid.h" |
| 9 | #include "vm/cpuinfo.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, 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::Init() { |
| 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 | fields_[kCpuInfoArchitecture] = "CPU architecture" ; |
| 30 | method_ = kCpuInfoCpuId; |
| 31 | CpuId::Init(); |
| 32 | #elif defined(HOST_ARCH_ARM) |
| 33 | fields_[kCpuInfoProcessor] = "Processor" ; |
| 34 | fields_[kCpuInfoModel] = "model name" ; |
| 35 | fields_[kCpuInfoHardware] = "Hardware" ; |
| 36 | fields_[kCpuInfoFeatures] = "Features" ; |
| 37 | fields_[kCpuInfoArchitecture] = "CPU architecture" ; |
| 38 | method_ = kCpuInfoSystem; |
| 39 | ProcCpuInfo::Init(); |
| 40 | #elif defined(HOST_ARCH_ARM64) |
| 41 | fields_[kCpuInfoProcessor] = "Processor" ; |
| 42 | fields_[kCpuInfoModel] = "CPU implementer" ; |
| 43 | fields_[kCpuInfoHardware] = "CPU implementer" ; |
| 44 | fields_[kCpuInfoFeatures] = "Features" ; |
| 45 | fields_[kCpuInfoArchitecture] = "CPU architecture" ; |
| 46 | method_ = kCpuInfoSystem; |
| 47 | ProcCpuInfo::Init(); |
| 48 | #else |
| 49 | #error Unrecognized target architecture |
| 50 | #endif |
| 51 | } |
| 52 | |
| 53 | void CpuInfo::Cleanup() { |
| 54 | if (method_ == kCpuInfoCpuId) { |
| 55 | CpuId::Cleanup(); |
| 56 | } else { |
| 57 | ASSERT(method_ == kCpuInfoSystem); |
| 58 | ProcCpuInfo::Cleanup(); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | bool CpuInfo::FieldContains(CpuInfoIndices idx, const char* search_string) { |
| 63 | if (method_ == kCpuInfoCpuId) { |
| 64 | const char* field = CpuId::field(idx); |
| 65 | bool contains = (strstr(field, search_string) != NULL); |
| 66 | free(const_cast<char*>(field)); |
| 67 | return contains; |
| 68 | } else { |
| 69 | ASSERT(method_ == kCpuInfoSystem); |
| 70 | return ProcCpuInfo::FieldContains(FieldName(idx), search_string); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | const char* CpuInfo::(CpuInfoIndices idx) { |
| 75 | if (method_ == kCpuInfoCpuId) { |
| 76 | return CpuId::field(idx); |
| 77 | } else { |
| 78 | ASSERT(method_ == kCpuInfoSystem); |
| 79 | return ProcCpuInfo::ExtractField(FieldName(idx)); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | bool CpuInfo::HasField(const char* field) { |
| 84 | if (method_ == kCpuInfoCpuId) { |
| 85 | return (strcmp(field, fields_[kCpuInfoProcessor]) == 0) || |
| 86 | (strcmp(field, fields_[kCpuInfoModel]) == 0) || |
| 87 | (strcmp(field, fields_[kCpuInfoHardware]) == 0) || |
| 88 | (strcmp(field, fields_[kCpuInfoFeatures]) == 0); |
| 89 | } else { |
| 90 | ASSERT(method_ == kCpuInfoSystem); |
| 91 | return ProcCpuInfo::HasField(field); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | } // namespace dart |
| 96 | |
| 97 | #endif // defined(HOST_OS_LINUX) |
| 98 | |