OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #include "vm/globals.h" | 5 #include "vm/globals.h" |
6 #if defined(HOST_OS_LINUX) || defined(HOST_OS_ANDROID) | 6 #if defined(HOST_OS_LINUX) || defined(HOST_OS_ANDROID) |
7 | 7 |
8 #include "vm/proccpuinfo.h" | 8 #include "vm/proccpuinfo.h" |
9 | 9 |
10 #include <ctype.h> // NOLINT | 10 #include <ctype.h> // NOLINT |
11 #include <string.h> // NOLINT | 11 #include <string.h> // NOLINT |
12 | 12 |
13 #include "platform/assert.h" | 13 #include "platform/assert.h" |
14 | 14 |
15 namespace dart { | 15 namespace dart { |
16 | 16 |
17 | |
18 char* ProcCpuInfo::data_ = NULL; | 17 char* ProcCpuInfo::data_ = NULL; |
19 intptr_t ProcCpuInfo::datalen_ = 0; | 18 intptr_t ProcCpuInfo::datalen_ = 0; |
20 | 19 |
21 | |
22 void ProcCpuInfo::InitOnce() { | 20 void ProcCpuInfo::InitOnce() { |
23 // Get the size of the cpuinfo file by reading it until the end. This is | 21 // Get the size of the cpuinfo file by reading it until the end. This is |
24 // required because files under /proc do not always return a valid size | 22 // required because files under /proc do not always return a valid size |
25 // when using fseek(0, SEEK_END) + ftell(). Nor can they be mmap()-ed. | 23 // when using fseek(0, SEEK_END) + ftell(). Nor can they be mmap()-ed. |
26 static const char PATHNAME[] = "/proc/cpuinfo"; | 24 static const char PATHNAME[] = "/proc/cpuinfo"; |
27 FILE* fp = fopen(PATHNAME, "r"); | 25 FILE* fp = fopen(PATHNAME, "r"); |
28 if (fp != NULL) { | 26 if (fp != NULL) { |
29 for (;;) { | 27 for (;;) { |
30 char buffer[256]; | 28 char buffer[256]; |
31 size_t n = fread(buffer, 1, sizeof(buffer), fp); | 29 size_t n = fread(buffer, 1, sizeof(buffer), fp); |
(...skipping 16 matching lines...) Expand all Loading... |
48 } | 46 } |
49 offset += n; | 47 offset += n; |
50 } | 48 } |
51 fclose(fp); | 49 fclose(fp); |
52 } | 50 } |
53 | 51 |
54 // Zero-terminate the data. | 52 // Zero-terminate the data. |
55 data_[datalen_] = '\0'; | 53 data_[datalen_] = '\0'; |
56 } | 54 } |
57 | 55 |
58 | |
59 void ProcCpuInfo::Cleanup() { | 56 void ProcCpuInfo::Cleanup() { |
60 ASSERT(data_); | 57 ASSERT(data_); |
61 free(data_); | 58 free(data_); |
62 data_ = NULL; | 59 data_ = NULL; |
63 } | 60 } |
64 | 61 |
65 | |
66 char* ProcCpuInfo::FieldStart(const char* field) { | 62 char* ProcCpuInfo::FieldStart(const char* field) { |
67 // Look for first field occurrence, and ensure it starts the line. | 63 // Look for first field occurrence, and ensure it starts the line. |
68 size_t fieldlen = strlen(field); | 64 size_t fieldlen = strlen(field); |
69 char* p = data_; | 65 char* p = data_; |
70 for (;;) { | 66 for (;;) { |
71 p = strstr(p, field); | 67 p = strstr(p, field); |
72 if (p == NULL) { | 68 if (p == NULL) { |
73 return NULL; | 69 return NULL; |
74 } | 70 } |
75 if (p == data_ || p[-1] == '\n') { | 71 if (p == data_ || p[-1] == '\n') { |
76 break; | 72 break; |
77 } | 73 } |
78 p += fieldlen; | 74 p += fieldlen; |
79 } | 75 } |
80 | 76 |
81 // Skip to the first colon followed by a space. | 77 // Skip to the first colon followed by a space. |
82 p = strchr(p + fieldlen, ':'); | 78 p = strchr(p + fieldlen, ':'); |
83 if (p == NULL || !isspace(p[1])) { | 79 if (p == NULL || !isspace(p[1])) { |
84 return NULL; | 80 return NULL; |
85 } | 81 } |
86 p += 2; | 82 p += 2; |
87 | 83 |
88 return p; | 84 return p; |
89 } | 85 } |
90 | 86 |
91 | |
92 bool ProcCpuInfo::FieldContains(const char* field, const char* search_string) { | 87 bool ProcCpuInfo::FieldContains(const char* field, const char* search_string) { |
93 ASSERT(data_ != NULL); | 88 ASSERT(data_ != NULL); |
94 ASSERT(search_string != NULL); | 89 ASSERT(search_string != NULL); |
95 | 90 |
96 char* p = FieldStart(field); | 91 char* p = FieldStart(field); |
97 if (p == NULL) { | 92 if (p == NULL) { |
98 return false; | 93 return false; |
99 } | 94 } |
100 | 95 |
101 // Find the end of the line. | 96 // Find the end of the line. |
102 char* q = strchr(p, '\n'); | 97 char* q = strchr(p, '\n'); |
103 if (q == NULL) { | 98 if (q == NULL) { |
104 q = data_ + datalen_; | 99 q = data_ + datalen_; |
105 } | 100 } |
106 | 101 |
107 char saved_end = *q; | 102 char saved_end = *q; |
108 *q = '\0'; | 103 *q = '\0'; |
109 bool ret = (strcasestr(p, search_string) != NULL); | 104 bool ret = (strcasestr(p, search_string) != NULL); |
110 *q = saved_end; | 105 *q = saved_end; |
111 | 106 |
112 return ret; | 107 return ret; |
113 } | 108 } |
114 | 109 |
115 | |
116 // Extract the content of a the first occurrence of a given field in | 110 // Extract the content of a the first occurrence of a given field in |
117 // the content of the cpuinfo file and return it as a heap-allocated | 111 // the content of the cpuinfo file and return it as a heap-allocated |
118 // string that must be freed by the caller using free. | 112 // string that must be freed by the caller using free. |
119 // Return NULL if not found. | 113 // Return NULL if not found. |
120 const char* ProcCpuInfo::ExtractField(const char* field) { | 114 const char* ProcCpuInfo::ExtractField(const char* field) { |
121 ASSERT(field != NULL); | 115 ASSERT(field != NULL); |
122 ASSERT(data_ != NULL); | 116 ASSERT(data_ != NULL); |
123 | 117 |
124 char* p = FieldStart(field); | 118 char* p = FieldStart(field); |
125 if (p == NULL) { | 119 if (p == NULL) { |
(...skipping 11 matching lines...) Expand all Loading... |
137 // Copy the line into result, leaving enough room for a null-terminator. | 131 // Copy the line into result, leaving enough room for a null-terminator. |
138 char saved_end = *q; | 132 char saved_end = *q; |
139 *q = '\0'; | 133 *q = '\0'; |
140 strncpy(result, p, len); | 134 strncpy(result, p, len); |
141 result[len] = '\0'; | 135 result[len] = '\0'; |
142 *q = saved_end; | 136 *q = saved_end; |
143 | 137 |
144 return result; | 138 return result; |
145 } | 139 } |
146 | 140 |
147 | |
148 bool ProcCpuInfo::HasField(const char* field) { | 141 bool ProcCpuInfo::HasField(const char* field) { |
149 ASSERT(field != NULL); | 142 ASSERT(field != NULL); |
150 ASSERT(data_ != NULL); | 143 ASSERT(data_ != NULL); |
151 return (FieldStart(field) != NULL); | 144 return (FieldStart(field) != NULL); |
152 } | 145 } |
153 | 146 |
154 } // namespace dart | 147 } // namespace dart |
155 | 148 |
156 #endif // defined(HOST_OS_LINUX) || defined(HOST_OS_ANDROID) | 149 #endif // defined(HOST_OS_LINUX) || defined(HOST_OS_ANDROID) |
OLD | NEW |