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

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

Issue 139103007: Use TimeTicks instead of gettimeofday in ProcessMetrics. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: reverty Created 6 years, 8 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 | Annotate | Revision Log
« no previous file with comments | « base/process/process_metrics_linux.cc ('k') | base/process/process_metrics_openbsd.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 <mach/mach.h> 7 #include <mach/mach.h>
8 #include <mach/mach_vm.h> 8 #include <mach/mach_vm.h>
9 #include <mach/shared_region.h> 9 #include <mach/shared_region.h>
10 #include <sys/sysctl.h> 10 #include <sys/sysctl.h>
(...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after
260 TIME_VALUE_TO_TIMEVAL(&thread_info_data.user_time, &user_timeval); 260 TIME_VALUE_TO_TIMEVAL(&thread_info_data.user_time, &user_timeval);
261 TIME_VALUE_TO_TIMEVAL(&thread_info_data.system_time, &system_timeval); 261 TIME_VALUE_TO_TIMEVAL(&thread_info_data.system_time, &system_timeval);
262 timeradd(&user_timeval, &system_timeval, &task_timeval); 262 timeradd(&user_timeval, &system_timeval, &task_timeval);
263 263
264 // ... task info contains terminated time. 264 // ... task info contains terminated time.
265 TIME_VALUE_TO_TIMEVAL(&task_info_data.user_time, &user_timeval); 265 TIME_VALUE_TO_TIMEVAL(&task_info_data.user_time, &user_timeval);
266 TIME_VALUE_TO_TIMEVAL(&task_info_data.system_time, &system_timeval); 266 TIME_VALUE_TO_TIMEVAL(&task_info_data.system_time, &system_timeval);
267 timeradd(&user_timeval, &task_timeval, &task_timeval); 267 timeradd(&user_timeval, &task_timeval, &task_timeval);
268 timeradd(&system_timeval, &task_timeval, &task_timeval); 268 timeradd(&system_timeval, &task_timeval, &task_timeval);
269 269
270 struct timeval now; 270 TimeTicks time = TimeTicks::Now();
271 int retval = gettimeofday(&now, NULL);
272 if (retval)
273 return 0;
274
275 int64 time = TimeValToMicroseconds(now);
276 int64 task_time = TimeValToMicroseconds(task_timeval); 271 int64 task_time = TimeValToMicroseconds(task_timeval);
277 272
278 if (last_cpu_time_ == 0) { 273 if (last_system_time_ == 0) {
279 // First call, just set the last values. 274 // First call, just set the last values.
280 last_cpu_time_ = time; 275 last_cpu_time_ = time;
281 last_system_time_ = task_time; 276 last_system_time_ = task_time;
282 return 0; 277 return 0;
283 } 278 }
284 279
285 int64 system_time_delta = task_time - last_system_time_; 280 int64 system_time_delta = task_time - last_system_time_;
286 int64 time_delta = time - last_cpu_time_; 281 int64 time_delta = (time - last_cpu_time_).InMicroseconds();
287 DCHECK_NE(0U, time_delta); 282 DCHECK_NE(0U, time_delta);
288 if (time_delta == 0) 283 if (time_delta == 0)
289 return 0; 284 return 0;
290 285
291 last_cpu_time_ = time; 286 last_cpu_time_ = time;
292 last_system_time_ = task_time; 287 last_system_time_ = task_time;
293 288
294 return static_cast<double>(system_time_delta * 100.0) / time_delta; 289 return static_cast<double>(system_time_delta * 100.0) / time_delta;
295 } 290 }
296 291
(...skipping 10 matching lines...) Expand all
307 TASK_POWER_INFO, 302 TASK_POWER_INFO,
308 reinterpret_cast<task_info_t>(&power_info_data), 303 reinterpret_cast<task_info_t>(&power_info_data),
309 &power_info_count); 304 &power_info_count);
310 if (kr != KERN_SUCCESS) { 305 if (kr != KERN_SUCCESS) {
311 // Most likely cause: |task| is a zombie, or this is on a pre-10.8.4 system 306 // Most likely cause: |task| is a zombie, or this is on a pre-10.8.4 system
312 // where TASK_POWER_INFO isn't supported yet. 307 // where TASK_POWER_INFO isn't supported yet.
313 return 0; 308 return 0;
314 } 309 }
315 uint64_t absolute_idle_wakeups = power_info_data.task_platform_idle_wakeups; 310 uint64_t absolute_idle_wakeups = power_info_data.task_platform_idle_wakeups;
316 311
317 struct timeval now; 312 TimeTicks time = TimeTicks::Now();
318 int retval = gettimeofday(&now, NULL);
319 if (retval)
320 return 0;
321 313
322 int64 time = TimeValToMicroseconds(now); 314 if (last_absolute_idle_wakeups_ == 0) {
323
324 if (last_idle_wakeups_time_ == 0) {
325 // First call, just set the last values. 315 // First call, just set the last values.
326 last_idle_wakeups_time_ = time; 316 last_idle_wakeups_time_ = time;
327 last_absolute_idle_wakeups_ = absolute_idle_wakeups; 317 last_absolute_idle_wakeups_ = absolute_idle_wakeups;
328 return 0; 318 return 0;
329 } 319 }
330 320
331 int64 wakeups_delta = absolute_idle_wakeups - last_absolute_idle_wakeups_; 321 int64 wakeups_delta = absolute_idle_wakeups - last_absolute_idle_wakeups_;
332 int64 time_delta = time - last_idle_wakeups_time_; 322 int64 time_delta = (time - last_idle_wakeups_time_).InMicroseconds();
333 DCHECK_NE(0U, time_delta); 323 DCHECK_NE(0U, time_delta);
334 if (time_delta == 0) 324 if (time_delta == 0)
335 return 0; 325 return 0;
336 326
337 last_idle_wakeups_time_ = time; 327 last_idle_wakeups_time_ = time;
338 last_absolute_idle_wakeups_ = absolute_idle_wakeups; 328 last_absolute_idle_wakeups_ = absolute_idle_wakeups;
339 329
340 // Round to average wakeups per second. 330 // Round to average wakeups per second.
341 const int kMicrosecondsPerSecond = 1000 * 1000; 331 const int kMicrosecondsPerSecond = 1000 * 1000;
342 return (wakeups_delta * kMicrosecondsPerSecond + time_delta/2) / time_delta; 332 return (wakeups_delta * kMicrosecondsPerSecond + time_delta/2) / time_delta;
343 } 333 }
344 334
345 bool ProcessMetrics::GetIOCounters(IoCounters* io_counters) const { 335 bool ProcessMetrics::GetIOCounters(IoCounters* io_counters) const {
346 return false; 336 return false;
347 } 337 }
348 338
349 ProcessMetrics::ProcessMetrics(ProcessHandle process, 339 ProcessMetrics::ProcessMetrics(ProcessHandle process,
350 ProcessMetrics::PortProvider* port_provider) 340 ProcessMetrics::PortProvider* port_provider)
351 : process_(process), 341 : process_(process),
352 last_cpu_time_(0),
353 last_system_time_(0), 342 last_system_time_(0),
354 last_idle_wakeups_time_(0),
355 last_absolute_idle_wakeups_(0), 343 last_absolute_idle_wakeups_(0),
356 port_provider_(port_provider) { 344 port_provider_(port_provider) {
357 processor_count_ = SysInfo::NumberOfProcessors(); 345 processor_count_ = SysInfo::NumberOfProcessors();
358 } 346 }
359 347
360 mach_port_t ProcessMetrics::TaskForPid(ProcessHandle process) const { 348 mach_port_t ProcessMetrics::TaskForPid(ProcessHandle process) const {
361 mach_port_t task = MACH_PORT_NULL; 349 mach_port_t task = MACH_PORT_NULL;
362 if (port_provider_) 350 if (port_provider_)
363 task = port_provider_->TaskForPid(process_); 351 task = port_provider_->TaskForPid(process_);
364 if (task == MACH_PORT_NULL && process_ == getpid()) 352 if (task == MACH_PORT_NULL && process_ == getpid())
(...skipping 18 matching lines...) Expand all
383 kr = host_page_size(host, &page_size); 371 kr = host_page_size(host, &page_size);
384 if (kr) { 372 if (kr) {
385 DLOG(ERROR) << "Failed to fetch host page size."; 373 DLOG(ERROR) << "Failed to fetch host page size.";
386 return 0; 374 return 0;
387 } 375 }
388 376
389 return (data.active_count * page_size) / 1024; 377 return (data.active_count * page_size) / 1024;
390 } 378 }
391 379
392 } // namespace base 380 } // namespace base
OLDNEW
« no previous file with comments | « base/process/process_metrics_linux.cc ('k') | base/process/process_metrics_openbsd.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698