OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/extensions/api/system_cpu/cpu_info_provider.h" | 5 #include "chrome/browser/extensions/api/system_cpu/cpu_info_provider.h" |
6 | 6 |
7 #include <cstdio> | 7 #include <cstdio> |
8 #include <sstream> | 8 #include <sstream> |
9 | 9 |
10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
(...skipping 19 matching lines...) Expand all Loading... |
30 std::string line; | 30 std::string line; |
31 | 31 |
32 // Skip the first line because it is just an aggregated number of | 32 // Skip the first line because it is just an aggregated number of |
33 // all cpuN lines. | 33 // all cpuN lines. |
34 std::getline(iss, line); | 34 std::getline(iss, line); |
35 size_t i = 0; | 35 size_t i = 0; |
36 while (std::getline(iss, line)) { | 36 while (std::getline(iss, line)) { |
37 if (line.compare(0, 3, "cpu") != 0) | 37 if (line.compare(0, 3, "cpu") != 0) |
38 continue; | 38 continue; |
39 | 39 |
| 40 // The number of entries in /proc/stat may mismatch the size of infos |
| 41 // because the number of online processors may change after the value has |
| 42 // been decided in CpuInfoProvider::QueryInfo(). |
| 43 // |
| 44 // TODO(jchuang): fix the fail case by using the number of configured |
| 45 // processors instead of online processors. |
| 46 if (i == infos->size()) { |
| 47 LOG(ERROR) << "Got more entries in /proc/stat than online CPUs"; |
| 48 return false; |
| 49 } |
| 50 |
40 uint64 user = 0, nice = 0, sys = 0, idle = 0; | 51 uint64 user = 0, nice = 0, sys = 0, idle = 0; |
41 int vals = sscanf(line.c_str(), | 52 int vals = sscanf(line.c_str(), |
42 "%*s %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64, | 53 "%*s %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64, |
43 &user, &nice, &sys, &idle); | 54 &user, &nice, &sys, &idle); |
44 DCHECK_EQ(4, vals); | 55 DCHECK_EQ(4, vals); |
45 | 56 |
46 DCHECK(i < infos->size()); | |
47 infos->at(i)->usage.kernel = static_cast<double>(sys); | 57 infos->at(i)->usage.kernel = static_cast<double>(sys); |
48 infos->at(i)->usage.user = static_cast<double>(user + nice); | 58 infos->at(i)->usage.user = static_cast<double>(user + nice); |
49 infos->at(i)->usage.idle = static_cast<double>(idle); | 59 infos->at(i)->usage.idle = static_cast<double>(idle); |
50 infos->at(i)->usage.total = static_cast<double>(sys + user + nice + idle); | 60 infos->at(i)->usage.total = static_cast<double>(sys + user + nice + idle); |
51 ++i; | 61 ++i; |
52 } | 62 } |
53 DCHECK_EQ(infos->size(), i); | 63 if (i < infos->size()) { |
| 64 LOG(ERROR) << "Got fewer entries in /proc/stat than online CPUs"; |
| 65 return false; |
| 66 } |
| 67 |
54 return true; | 68 return true; |
55 } | 69 } |
56 | 70 |
57 } // namespace extensions | 71 } // namespace extensions |
OLD | NEW |