OLD | NEW |
(Empty) | |
| 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(TARGET_OS_LINUX) || defined(TARGET_OS_ANDROID) |
| 7 |
| 8 #include "vm/proccpuinfo.h" |
| 9 |
| 10 #include <ctype.h> // NOLINT |
| 11 #include <string.h> // NOLINT |
| 12 |
| 13 #include "platform/assert.h" |
| 14 |
| 15 namespace dart { |
| 16 |
| 17 |
| 18 char* ProcCpuInfo::data_ = NULL; |
| 19 intptr_t ProcCpuInfo::datalen_ = 0; |
| 20 |
| 21 |
| 22 void ProcCpuInfo::InitOnce() { |
| 23 // 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 |
| 25 // when using fseek(0, SEEK_END) + ftell(). Nor can they be mmap()-ed. |
| 26 static const char PATHNAME[] = "/proc/cpuinfo"; |
| 27 FILE* fp = fopen(PATHNAME, "r"); |
| 28 if (fp != NULL) { |
| 29 for (;;) { |
| 30 char buffer[256]; |
| 31 size_t n = fread(buffer, 1, sizeof(buffer), fp); |
| 32 if (n == 0) { |
| 33 break; |
| 34 } |
| 35 datalen_ += n; |
| 36 } |
| 37 fclose(fp); |
| 38 } |
| 39 |
| 40 // Read the contents of the cpuinfo file. |
| 41 data_ = new char[datalen_ + 1]; |
| 42 fp = fopen(PATHNAME, "r"); |
| 43 if (fp != NULL) { |
| 44 for (intptr_t offset = 0; offset < datalen_; ) { |
| 45 size_t n = fread(data_ + offset, 1, datalen_ - offset, fp); |
| 46 if (n == 0) { |
| 47 break; |
| 48 } |
| 49 offset += n; |
| 50 } |
| 51 fclose(fp); |
| 52 } |
| 53 |
| 54 // Zero-terminate the data. |
| 55 data_[datalen_] = '\0'; |
| 56 } |
| 57 |
| 58 |
| 59 void ProcCpuInfo::Cleanup() { |
| 60 ASSERT(data_); |
| 61 delete[] data_; |
| 62 data_ = NULL; |
| 63 } |
| 64 |
| 65 |
| 66 char* ProcCpuInfo::FieldStart(const char* field) { |
| 67 // Look for first field occurrence, and ensure it starts the line. |
| 68 size_t fieldlen = strlen(field); |
| 69 char* p = data_; |
| 70 for (;;) { |
| 71 p = strstr(p, field); |
| 72 if (p == NULL) { |
| 73 return NULL; |
| 74 } |
| 75 if (p == data_ || p[-1] == '\n') { |
| 76 break; |
| 77 } |
| 78 p += fieldlen; |
| 79 } |
| 80 |
| 81 // Skip to the first colon followed by a space. |
| 82 p = strchr(p + fieldlen, ':'); |
| 83 if (p == NULL || !isspace(p[1])) { |
| 84 return NULL; |
| 85 } |
| 86 p += 2; |
| 87 |
| 88 return p; |
| 89 } |
| 90 |
| 91 |
| 92 bool ProcCpuInfo::FieldContains(const char* field, const char* search_string) { |
| 93 ASSERT(data_ != NULL); |
| 94 ASSERT(search_string != NULL); |
| 95 |
| 96 char* p = FieldStart(field); |
| 97 if (p == NULL) { |
| 98 return false; |
| 99 } |
| 100 |
| 101 // Find the end of the line. |
| 102 char* q = strchr(p, '\n'); |
| 103 if (q == NULL) { |
| 104 q = data_ + datalen_; |
| 105 } |
| 106 |
| 107 char saved_end = *q; |
| 108 *q = '\0'; |
| 109 bool ret = (strcasestr(p, search_string) != NULL); |
| 110 *q = saved_end; |
| 111 |
| 112 return ret; |
| 113 } |
| 114 |
| 115 |
| 116 // 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 |
| 118 // string that must be freed by the caller using delete[]. |
| 119 // Return NULL if not found. |
| 120 const char* ProcCpuInfo::ExtractField(const char* field) { |
| 121 ASSERT(field != NULL); |
| 122 ASSERT(data_ != NULL); |
| 123 |
| 124 char* p = FieldStart(field); |
| 125 if (p == NULL) { |
| 126 return NULL; |
| 127 } |
| 128 |
| 129 // Find the end of the line. |
| 130 char* q = strchr(p, '\n'); |
| 131 if (q == NULL) { |
| 132 q = data_ + datalen_; |
| 133 } |
| 134 |
| 135 intptr_t len = q - p; |
| 136 char* result = new char[len + 1]; // plus one for null-terminator. |
| 137 // Copy the line into result, leaving enough room for a null-terminator. |
| 138 char saved_end = *q; |
| 139 *q = '\0'; |
| 140 strncpy(result, p, len); |
| 141 result[len] = '\0'; |
| 142 *q = saved_end; |
| 143 |
| 144 return result; |
| 145 } |
| 146 |
| 147 |
| 148 bool ProcCpuInfo::HasField(const char* field) { |
| 149 ASSERT(field != NULL); |
| 150 ASSERT(data_ != NULL); |
| 151 return (FieldStart(field) != NULL); |
| 152 } |
| 153 |
| 154 } // namespace dart |
| 155 |
| 156 #endif // defined(TARGET_OS_LINUX) || defined(TARGET_OS_ANDROID) |
OLD | NEW |