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

Unified Diff: base/process_util_posix.cc

Issue 504068: Revert 34994, maybe it regressed startup perf - Fix cpu/memory measurements o... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 11 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_mac.mm ('k') | base/trace_event.cc » ('j') | 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 35019)
+++ base/process_util_posix.cc (working copy)
@@ -360,36 +360,20 @@
return LaunchApp(cl.argv(), no_files, wait, process_handle);
}
-#if !defined(OS_MACOSX)
-ProcessMetrics::ProcessMetrics(ProcessHandle process)
-#else
-ProcessMetrics::ProcessMetrics(ProcessHandle process,
- ProcessMetrics::PortProvider* port_provider)
-#endif
- : process_(process),
- last_time_(0),
- last_system_time_(0)
+ProcessMetrics::ProcessMetrics(ProcessHandle process) : process_(process),
+ last_time_(0),
+ last_system_time_(0)
#if defined(OS_LINUX)
- , last_cpu_(0)
-#elif defined (OS_MACOSX)
- , port_provider_(port_provider)
+ , last_cpu_(0)
#endif
{
processor_count_ = base::SysInfo::NumberOfProcessors();
}
// static
-#if !defined(OS_MACOSX)
ProcessMetrics* ProcessMetrics::CreateProcessMetrics(ProcessHandle process) {
return new ProcessMetrics(process);
}
-#else
-ProcessMetrics* ProcessMetrics::CreateProcessMetrics(
- ProcessHandle process,
- ProcessMetrics::PortProvider* port_provider) {
- return new ProcessMetrics(process, port_provider);
-}
-#endif
ProcessMetrics::~ProcessMetrics() { }
@@ -510,6 +494,49 @@
return tv.tv_sec * kMicrosecondsPerSecond + tv.tv_usec;
}
+#if defined(OS_MACOSX)
+// TODO(port): this function only returns the *current* CPU's usage;
+// we want to return this->process_'s CPU usage.
+int ProcessMetrics::GetCPUUsage() {
+ struct timeval now;
+ struct rusage usage;
+
+ int 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_;
+ int64 time = TimeValToMicroseconds(now);
+
+ if ((last_system_time_ == 0) || (last_time_ == 0)) {
+ // 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;
+}
+#endif
+
// Executes the application specified by |cl| and wait for it to exit. Stores
// the output (stdout) in |output|. If |do_search_path| is set, it searches the
// path for the application; in that case, |envp| must be null, and it will use
« no previous file with comments | « base/process_util_mac.mm ('k') | base/trace_event.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698