Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(303)

Side by Side Diff: chrome/browser/extensions/api/system_cpu/cpu_info_provider_linux.cc

Issue 426303002: Fix race condition in CpuInfoProvider::QueryCpuTimePerProcessor (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add comments and logging Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 // Due to a race condition in the kernel, we cannot trust the kernel to
Lei Zhang 2014/08/07 06:19:37 Can you mention the number of CPUs might have chan
41 // always return the same number of entries in /proc/stat as the number of
42 // online processors.
43 //
44 // If the number of /proc/stat is different than the number of online
45 // processors, it is treated as an intermittent problem in the kernel, and
Pawel Osciak 2014/08/04 09:22:54 This is not really an intermittent problem on some
Justin Chuang 2014/08/04 11:55:02 Interesting. Luckily it's not the case. On Nyan, t
46 // this function will return false.
47 //
48 // In addition to the kernel issue, there is another less-likely race
49 // condition that the number of online processors is changed after the value
50 // has been decided in CpuInfoProvider::QueryInfo().
51 if (i == infos->size()) {
Pawel Osciak 2014/08/04 09:22:54 s/==/>=/
Justin Chuang 2014/08/04 09:37:23 'i' won't be greater infos->size() unless std::vec
Pawel Osciak 2014/08/05 07:48:16 Of course, sorry, I misread the code. No need to c
52 LOG(ERROR) << "Got more entries in /proc/stat than online CPUs";
53 return false;
54 }
55
40 uint64 user = 0, nice = 0, sys = 0, idle = 0; 56 uint64 user = 0, nice = 0, sys = 0, idle = 0;
41 int vals = sscanf(line.c_str(), "%*s %"PRIu64" %"PRIu64" %"PRIu64" %"PRIu64, 57 int vals = sscanf(line.c_str(), "%*s %"PRIu64" %"PRIu64" %"PRIu64" %"PRIu64,
42 &user, &nice, &sys, &idle); 58 &user, &nice, &sys, &idle);
43 DCHECK_EQ(4, vals); 59 DCHECK_EQ(4, vals);
44 60
45 DCHECK(i < infos->size());
46 infos->at(i)->usage.kernel = static_cast<double>(sys); 61 infos->at(i)->usage.kernel = static_cast<double>(sys);
47 infos->at(i)->usage.user = static_cast<double>(user + nice); 62 infos->at(i)->usage.user = static_cast<double>(user + nice);
48 infos->at(i)->usage.idle = static_cast<double>(idle); 63 infos->at(i)->usage.idle = static_cast<double>(idle);
49 infos->at(i)->usage.total = static_cast<double>(sys + user + nice + idle); 64 infos->at(i)->usage.total = static_cast<double>(sys + user + nice + idle);
50 ++i; 65 ++i;
51 } 66 }
52 DCHECK_EQ(infos->size(), i); 67 if (i < infos->size()) {
Pawel Osciak 2014/08/04 09:22:54 Is this also an actual issue? Having less CPUs pre
Justin Chuang 2014/08/04 09:37:23 Yes, I have seen both cases on actual nyan device.
68 LOG(ERROR) << "Got fewer entries in /proc/stat than online CPUs";
69 return false;
70 }
71
53 return true; 72 return true;
54 } 73 }
55 74
56 } // namespace extensions 75 } // namespace extensions
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698