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

Side by Side Diff: base/process/process_metrics_linux.cc

Issue 888193002: ProcessMetrics::GetCPUUsage() is Slow (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 10 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
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 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 "base/process/process_metrics.h" 5 #include "base/process/process_metrics.h"
6 6
7 #include <dirent.h> 7 #include <dirent.h>
8 #include <fcntl.h> 8 #include <fcntl.h>
9 #include <sys/stat.h> 9 #include <sys/stat.h>
10 #include <sys/time.h> 10 #include <sys/time.h>
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after
137 if (!tid) 137 if (!tid)
138 continue; 138 continue;
139 139
140 // Synchronously reading files in /proc does not hit the disk. 140 // Synchronously reading files in /proc does not hit the disk.
141 ThreadRestrictions::ScopedAllowIO allow_io; 141 ThreadRestrictions::ScopedAllowIO allow_io;
142 142
143 std::string stat; 143 std::string stat;
144 FilePath stat_path = 144 FilePath stat_path =
145 task_path.Append(ent->d_name).Append(internal::kStatFile); 145 task_path.Append(ent->d_name).Append(internal::kStatFile);
146 if (ReadFileToString(stat_path, &stat)) { 146 if (ReadFileToString(stat_path, &stat)) {
147 int cpu = ParseProcStatCPU(stat); 147 int cpu = GetCpuFromProcStats(stat);
148 if (cpu > 0) 148 if (cpu > 0)
149 total_cpu += cpu; 149 total_cpu += cpu;
150 } 150 }
151 } 151 }
152 closedir(dir); 152 closedir(dir);
153 153
154 return total_cpu; 154 return total_cpu;
155 } 155 }
156 156
157 } // namespace 157 } // namespace
(...skipping 247 matching lines...) Expand 10 before | Expand all | Expand 10 after
405 if (!internal::ParseProcStats(input, &proc_stats)) 405 if (!internal::ParseProcStats(input, &proc_stats))
406 return -1; 406 return -1;
407 407
408 if (proc_stats.size() <= internal::VM_STIME) 408 if (proc_stats.size() <= internal::VM_STIME)
409 return -1; 409 return -1;
410 int utime = GetProcStatsFieldAsInt64(proc_stats, internal::VM_UTIME); 410 int utime = GetProcStatsFieldAsInt64(proc_stats, internal::VM_UTIME);
411 int stime = GetProcStatsFieldAsInt64(proc_stats, internal::VM_STIME); 411 int stime = GetProcStatsFieldAsInt64(proc_stats, internal::VM_STIME);
412 return utime + stime; 412 return utime + stime;
413 } 413 }
414 414
415 // Exposed for testing.
danakj 2015/02/03 18:51:09 this comment is in a weird place, itd expect it in
afakhry 2015/02/03 20:23:54 I was following the same style of the original Par
416 int GetCpuFromProcStats(const std::string& input) {
417 if (input.empty())
418 return -1;
419
420 int j = 0;
421 int last_space_index = 0;
422 int utime_start_index = 0;
423 bool prevWasSpace = false;
danakj 2015/02/03 18:51:09 fix style
afakhry 2015/02/03 20:23:54 Done.
424 for (auto& ch : input) {
425 if (ch == ' ') {
426 if (!prevWasSpace)
427 ++last_space_index;
428
429 prevWasSpace = true;
430 } else {
431 if (ch == ')') {
432 // Restart the counter, we're looking to count from the last ')' we
433 // encounter.
434 last_space_index = 0;
435 } else if (prevWasSpace && last_space_index == internal::VM_UTIME - 2) {
436 utime_start_index = j + 1;
437 break;
438 }
439 prevWasSpace = false;
440 }
441 ++j;
442 }
443
444 if (last_space_index < internal::VM_UTIME - 2)
445 return -1;
446
447 int utime = 0;
448 int stime = 0;
449 if (sscanf(&input.data()[utime_start_index], "%d %d", &utime, &stime) != 2)
450 return -1;
451
452 return utime + stime;
453 }
454
415 const char kProcSelfExe[] = "/proc/self/exe"; 455 const char kProcSelfExe[] = "/proc/self/exe";
416 456
417 int GetNumberOfThreads(ProcessHandle process) { 457 int GetNumberOfThreads(ProcessHandle process) {
418 return internal::ReadProcStatsAndGetFieldAsInt64(process, 458 return internal::ReadProcStatsAndGetFieldAsInt64(process,
419 internal::VM_NUMTHREADS); 459 internal::VM_NUMTHREADS);
420 } 460 }
421 461
422 namespace { 462 namespace {
423 463
424 // The format of /proc/diskstats is: 464 // The format of /proc/diskstats is:
(...skipping 463 matching lines...) Expand 10 before | Expand all | Expand 10 after
888 #if defined(OS_LINUX) 928 #if defined(OS_LINUX)
889 int ProcessMetrics::GetIdleWakeupsPerSecond() { 929 int ProcessMetrics::GetIdleWakeupsPerSecond() {
890 uint64 wake_ups; 930 uint64 wake_ups;
891 const char kWakeupStat[] = "se.statistics.nr_wakeups"; 931 const char kWakeupStat[] = "se.statistics.nr_wakeups";
892 return ReadProcSchedAndGetFieldAsUint64(process_, kWakeupStat, &wake_ups) ? 932 return ReadProcSchedAndGetFieldAsUint64(process_, kWakeupStat, &wake_ups) ?
893 CalculateIdleWakeupsPerSecond(wake_ups) : 0; 933 CalculateIdleWakeupsPerSecond(wake_ups) : 0;
894 } 934 }
895 #endif // defined(OS_LINUX) 935 #endif // defined(OS_LINUX)
896 936
897 } // namespace base 937 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698