Home | History | Annotate | Download | only in bench
      1 
      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  */
      8 #include "BenchSysTimer_mach.h"
      9 
     10 //Time
     11 #include <mach/mach.h>
     12 #include <mach/mach_time.h>
     13 
     14 static 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 
     36 static 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 
     53 void BenchSysTimer::startWall() {
     54     this->fStartWall = mach_absolute_time();
     55 }
     56 void BenchSysTimer::startCpu() {
     57     this->fStartCpu = macCpuTime();
     58 }
     59 
     60 double BenchSysTimer::endCpu() {
     61     time_value_t end_cpu = macCpuTime();
     62     return intervalInMSec(this->fStartCpu, end_cpu);
     63 }
     64 double 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 }
     77