blob: c837ca34a01124a0ad18ec8baeda7db3d1e900ce [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 }
20
21 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 }
31
32 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();
66
67 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}