Flutter Engine
The Flutter Engine
cpuinfo_macos.cc
Go to the documentation of this file.
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(DART_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] = {};
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] = nullptr;
29}
30
31void CpuInfo::Cleanup() {}
32
33bool CpuInfo::FieldContains(CpuInfoIndices idx, const char* search_string) {
34 ASSERT(method_ != kCpuInfoDefault);
35 ASSERT(search_string != nullptr);
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, nullptr, 0) != 0) {
43 return false;
44 }
45
46 return (strcasestr(dest, search_string) != nullptr);
47}
48
49const char* CpuInfo::ExtractField(CpuInfoIndices idx) {
50 ASSERT(method_ != kCpuInfoDefault);
51 const char* field = FieldName(idx);
52 ASSERT(field != nullptr);
53 size_t result_len;
54
55 ASSERT(HasField(field));
56 if (sysctlbyname(field, nullptr, &result_len, nullptr, 0) != 0) {
58 return 0;
59 }
60
61 char* result = reinterpret_cast<char*>(malloc(result_len));
62 if (sysctlbyname(field, result, &result_len, nullptr, 0) != 0) {
64 return 0;
65 }
66
67 return result;
68}
69
70bool CpuInfo::HasField(const char* field) {
71 ASSERT(method_ != kCpuInfoDefault);
72 ASSERT(field != nullptr);
73 int ret = sysctlbyname(field, nullptr, nullptr, nullptr, 0);
74 return (ret == 0);
75}
76
77} // namespace dart
78
79#endif // defined(DART_HOST_OS_MACOS)
#define UNREACHABLE()
Definition: assert.h:248
static const char * FieldName(CpuInfoIndices idx)
Definition: cpuinfo.h:46
static void Init()
static void Cleanup()
static bool FieldContains(CpuInfoIndices idx, const char *search_string)
static bool HasField(const char *field)
#define ASSERT(E)
GAsyncResult * result
Definition: dart_vm.cc:33
CpuInfoMethod
Definition: cpuinfo.h:25
@ kCpuInfoDefault
Definition: cpuinfo.h:38
@ kCpuInfoSystem
Definition: cpuinfo.h:30
void * malloc(size_t size)
Definition: allocation.cc:19
CpuInfoIndices
Definition: cpuinfo.h:15
@ kCpuInfoFeatures
Definition: cpuinfo.h:19
@ kCpuInfoArchitecture
Definition: cpuinfo.h:20
@ kCpuInfoProcessor
Definition: cpuinfo.h:16
@ kCpuInfoHardware
Definition: cpuinfo.h:18
@ kCpuInfoMax
Definition: cpuinfo.h:21
@ kCpuInfoModel
Definition: cpuinfo.h:17
dest
Definition: zip.py:79