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_MACOS)
7
8#include "vm/cpuinfo.h"
9
10#include <errno.h> // NOLINT
11#include <sys/sysctl.h> // NOLINT
12#include <sys/types.h> // NOLINT
13
14#include "platform/assert.h"
15
16namespace dart {
17
18CpuInfoMethod CpuInfo::method_ = kCpuInfoDefault;
19const char* CpuInfo::fields_[kCpuInfoMax] = {0};
20
21void CpuInfo::Init() {
22 method_ = kCpuInfoSystem;
23
24 fields_[kCpuInfoProcessor] = "machdep.cpu.vendor";
25 fields_[kCpuInfoModel] = "machdep.cpu.brand_string";
26 fields_[kCpuInfoHardware] = "machdep.cpu.brand_string";
27 fields_[kCpuInfoFeatures] = "machdep.cpu.features";
28 fields_[kCpuInfoArchitecture] = NULL;
29}
30
31void CpuInfo::Cleanup() {}
32
33bool CpuInfo::FieldContains(CpuInfoIndices idx, const char* search_string) {
34 ASSERT(method_ != kCpuInfoDefault);
35 ASSERT(search_string != NULL);
36 const char* field = FieldName(idx);
37 char dest[1024];
38 size_t dest_len = 1024;
39
40 ASSERT(HasField(field));
41 if (sysctlbyname(field, dest, &dest_len, NULL, 0) != 0) {
42 UNREACHABLE();
43 return false;
44 }
45
46 return (strcasestr(dest, search_string) != NULL);
47}
48
49const char* CpuInfo::ExtractField(CpuInfoIndices idx) {
50 ASSERT(method_ != kCpuInfoDefault);
51 const char* field = FieldName(idx);
52 ASSERT(field != NULL);
53 size_t result_len;
54
55 ASSERT(HasField(field));
56 if (sysctlbyname(field, NULL, &result_len, NULL, 0) != 0) {
57 UNREACHABLE();
58 return 0;
59 }
60
61 char* result = reinterpret_cast<char*>(malloc(result_len));
62 if (sysctlbyname(field, result, &result_len, NULL, 0) != 0) {
63 UNREACHABLE();
64 return 0;
65 }
66
67 return result;
68}
69
70bool CpuInfo::HasField(const char* field) {
71 ASSERT(method_ != kCpuInfoDefault);
72 ASSERT(field != NULL);
73 int ret = sysctlbyname(field, NULL, NULL, NULL, 0);
74 return (ret == 0);
75}
76
77} // namespace dart
78
79#endif // defined(HOST_OS_MACOS)
80