1// Copyright (c) 2014, 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#ifndef RUNTIME_VM_CPUINFO_H_
6#define RUNTIME_VM_CPUINFO_H_
7
8#include "platform/assert.h"
9#include "platform/utils.h"
10#include "vm/allocation.h"
11
12namespace dart {
13
14// Indices into cpuinfo field name arrays.
15enum CpuInfoIndices {
16 kCpuInfoProcessor = 0,
17 kCpuInfoModel = 1,
18 kCpuInfoHardware = 2,
19 kCpuInfoFeatures = 3,
20 kCpuInfoArchitecture = 4,
21 kCpuInfoMax = 5,
22};
23
24// For Intel architectures, the method to use to get CPU information.
25enum CpuInfoMethod {
26 // Use the cpuid instruction.
27 kCpuInfoCpuId,
28
29 // Use system calls.
30 kCpuInfoSystem,
31
32 // Use whatever the default is for a particular OS:
33 // Linux, Windows -> CpuId,
34 // Android, MacOS -> System.
35 kCpuInfoDefault,
36};
37
38class CpuInfo : public AllStatic {
39 public:
40 static void Init();
41 static void Cleanup();
42
43 static const char* FieldName(CpuInfoIndices idx) {
44 ASSERT((idx >= 0) && (idx < kCpuInfoMax));
45 return fields_[idx];
46 }
47
48 // Returns true if the cpuinfo field contains the string.
49 static bool FieldContains(CpuInfoIndices idx, const char* search_string);
50
51 // Returns true if the cpuinfo field [field] exists and is non-empty.
52 static bool HasField(const char* field);
53
54 // Returns the field describing the CPU model. Caller is responsible for
55 // freeing the result.
56 static const char* GetCpuModel() {
57 if (HasField(FieldName(kCpuInfoHardware))) {
58 return ExtractField(kCpuInfoHardware);
59 } else {
60 return Utils::StrDup("Unknown");
61 }
62 }
63
64 private:
65 // Returns the field. Caller is responsible for freeing the result.
66 static const char* ExtractField(CpuInfoIndices idx);
67
68 // The method to use to acquire info about the CPU.
69 static CpuInfoMethod method_;
70
71 // Cpuinfo field names.
72 static const char* fields_[kCpuInfoMax];
73};
74
75} // namespace dart
76
77#endif // RUNTIME_VM_CPUINFO_H_
78