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 // Cope with kernel race condition when /proc/stat does not reflect to the | |
Pawel Osciak
2014/08/04 00:55:21
s/to//
Justin Chuang
2014/08/04 08:12:03
Acknowledged.
| |
41 // current number of online processors in time. Also handle a less-likely | |
Pawel Osciak
2014/08/04 00:55:21
s/in time//
Justin Chuang
2014/08/04 08:12:03
Acknowledged.
| |
42 // race condition when the number of online processors is changed after it | |
43 // has been decided in CpuInfoProvider::QueryInfo(). | |
44 if (i == infos->size()) | |
Pawel Osciak
2014/08/04 00:55:22
Please explain how this actually handles the issue
Justin Chuang
2014/08/04 08:12:04
Thanks for the comments. It's actually a race cond
| |
45 return false; | |
46 | |
40 uint64 user = 0, nice = 0, sys = 0, idle = 0; | 47 uint64 user = 0, nice = 0, sys = 0, idle = 0; |
41 int vals = sscanf(line.c_str(), "%*s %"PRIu64" %"PRIu64" %"PRIu64" %"PRIu64, | 48 int vals = sscanf(line.c_str(), "%*s %"PRIu64" %"PRIu64" %"PRIu64" %"PRIu64, |
42 &user, &nice, &sys, &idle); | 49 &user, &nice, &sys, &idle); |
43 DCHECK_EQ(4, vals); | 50 DCHECK_EQ(4, vals); |
44 | 51 |
45 DCHECK(i < infos->size()); | |
46 infos->at(i)->usage.kernel = static_cast<double>(sys); | 52 infos->at(i)->usage.kernel = static_cast<double>(sys); |
47 infos->at(i)->usage.user = static_cast<double>(user + nice); | 53 infos->at(i)->usage.user = static_cast<double>(user + nice); |
48 infos->at(i)->usage.idle = static_cast<double>(idle); | 54 infos->at(i)->usage.idle = static_cast<double>(idle); |
49 infos->at(i)->usage.total = static_cast<double>(sys + user + nice + idle); | 55 infos->at(i)->usage.total = static_cast<double>(sys + user + nice + idle); |
50 ++i; | 56 ++i; |
51 } | 57 } |
52 DCHECK_EQ(infos->size(), i); | 58 DCHECK_EQ(infos->size(), i); |
53 return true; | 59 return true; |
54 } | 60 } |
55 | 61 |
56 } // namespace extensions | 62 } // namespace extensions |
OLD | NEW |