blob: cf3f1c1f7d408514d57fddac2aae1c4c9b2eb9a2 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
bungeman@google.combe9ad4e2011-06-07 19:16:02 +00008#include "BenchSysTimer_mach.h"
9
10//Time
11#include <mach/mach.h>
12#include <mach/mach_time.h>
13
14static time_value_t macCpuTime() {
15 mach_port_t task = mach_task_self();
16 if (task == MACH_PORT_NULL) {
17 time_value_t none = {0, 0};
18 return none;
19 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000020
bungeman@google.combe9ad4e2011-06-07 19:16:02 +000021 task_thread_times_info thread_info_data;
22 mach_msg_type_number_t thread_info_count = TASK_THREAD_TIMES_INFO_COUNT;
23 if (KERN_SUCCESS != task_info(task,
24 TASK_THREAD_TIMES_INFO,
25 reinterpret_cast<task_info_t>(&thread_info_data),
26 &thread_info_count))
27 {
28 time_value_t none = {0, 0};
29 return none;
30 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000031
bungeman@google.combe9ad4e2011-06-07 19:16:02 +000032 time_value_add(&thread_info_data.user_time, &thread_info_data.system_time)
33 return thread_info_data.user_time;
34}
35
36static double intervalInMSec(const time_value_t start_clock
37 , const time_value_t end_clock)
38{
39 double duration_clock;
40 if ((end_clock.microseconds - start_clock.microseconds) < 0) {
41 duration_clock = (end_clock.seconds - start_clock.seconds-1)*1000;
42 duration_clock += (1000000
43 + end_clock.microseconds
44 - start_clock.microseconds) / 1000.0;
45 } else {
46 duration_clock = (end_clock.seconds - start_clock.seconds)*1000;
47 duration_clock += (end_clock.microseconds - start_clock.microseconds)
48 / 1000.0;
49 }
50 return duration_clock;
51}
52
53void BenchSysTimer::startWall() {
54 this->fStartWall = mach_absolute_time();
55}
56void BenchSysTimer::startCpu() {
57 this->fStartCpu = macCpuTime();
58}
59
60double BenchSysTimer::endCpu() {
61 time_value_t end_cpu = macCpuTime();
62 return intervalInMSec(this->fStartCpu, end_cpu);
63}
64double BenchSysTimer::endWall() {
65 uint64_t end_wall = mach_absolute_time();
rmistry@google.comfbfcd562012-08-23 18:09:54 +000066
bungeman@google.combe9ad4e2011-06-07 19:16:02 +000067 uint64_t elapsed = end_wall - this->fStartWall;
68 mach_timebase_info_data_t sTimebaseInfo;
69 if (KERN_SUCCESS != mach_timebase_info(&sTimebaseInfo)) {
70 return 0;
71 } else {
72 uint64_t elapsedNano = elapsed * sTimebaseInfo.numer
73 / sTimebaseInfo.denom;
74 return elapsedNano / 1000000;
75 }
76}