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

Unified Diff: base/process_util_posix.cc

Issue 12869: Implement GetCPUUsage; it was causing link errors because of its absence on t... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 12 years 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « base/process_util_linux.cc ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/process_util_posix.cc
===================================================================
--- base/process_util_posix.cc (revision 6214)
+++ base/process_util_posix.cc (working copy)
@@ -4,12 +4,17 @@
#include "base/process_util.h"
+#include <sys/resource.h>
+#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include "base/basictypes.h"
+#include "base/logging.h"
#include "base/sys_info.h"
+const int kMicrosecondsPerSecond = 1000000;
+
namespace base {
int GetCurrentProcId() {
@@ -46,4 +51,52 @@
// setpriority() or sched_getscheduler, but these all require extra rights.
}
+namespace {
+
+int64 TimeValToMicroseconds(const struct timeval& tv) {
+ return tv.tv_sec * kMicrosecondsPerSecond + tv.tv_usec;
+}
+
+}
+
+int ProcessMetrics::GetCPUUsage() {
+ int retval;
pink (ping after 24hrs) 2008/12/02 19:36:11 declare retval where it's initialized or give it a
+ struct timeval now;
+ struct rusage usage;
+
+ retval = gettimeofday(&now, NULL);
+ if (retval)
+ return 0;
+ retval = getrusage(RUSAGE_SELF, &usage);
+ if (retval)
+ return 0;
+
+ int64 system_time = (TimeValToMicroseconds(usage.ru_stime) +
+ TimeValToMicroseconds(usage.ru_utime)) /
+ processor_count_;
pink (ping after 24hrs) 2008/12/02 19:36:11 does |processor_count_| get scaled to an int64 or
Avi (use Gerrit) 2008/12/02 19:40:31 I'm pretty sure int64/int32 causes the int32 to be
+ int64 time = TimeValToMicroseconds(now);
+
+ if ((last_system_time_ == 0) || (last_time_ == 0)) {
pink (ping after 24hrs) 2008/12/02 19:36:11 don't need the extra parens, but not a biggie.
Avi (use Gerrit) 2008/12/02 19:40:31 Straight from the Windows code.
+ // First call, just set the last values.
+ last_system_time_ = system_time;
+ last_time_ = time;
+ return 0;
+ }
+
+ int64 system_time_delta = system_time - last_system_time_;
+ int64 time_delta = time - last_time_;
+ DCHECK(time_delta != 0);
+ if (time_delta == 0)
+ return 0;
+
+ // We add time_delta / 2 so the result is rounded.
+ int cpu = static_cast<int>((system_time_delta * 100 + time_delta / 2) /
+ time_delta);
+
+ last_system_time_ = system_time;
+ last_time_ = time;
+
+ return cpu;
+}
+
} // namespace base
« no previous file with comments | « base/process_util_linux.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698