bungeman@google.com | be9ad4e | 2011-06-07 19:16:02 +0000 | [diff] [blame^] | 1 | #include "BenchSysTimer_mach.h" |
| 2 | |
| 3 | //Time |
| 4 | #include <mach/mach.h> |
| 5 | #include <mach/mach_time.h> |
| 6 | |
| 7 | static time_value_t macCpuTime() { |
| 8 | mach_port_t task = mach_task_self(); |
| 9 | if (task == MACH_PORT_NULL) { |
| 10 | time_value_t none = {0, 0}; |
| 11 | return none; |
| 12 | } |
| 13 | |
| 14 | task_thread_times_info thread_info_data; |
| 15 | mach_msg_type_number_t thread_info_count = TASK_THREAD_TIMES_INFO_COUNT; |
| 16 | if (KERN_SUCCESS != task_info(task, |
| 17 | TASK_THREAD_TIMES_INFO, |
| 18 | reinterpret_cast<task_info_t>(&thread_info_data), |
| 19 | &thread_info_count)) |
| 20 | { |
| 21 | time_value_t none = {0, 0}; |
| 22 | return none; |
| 23 | } |
| 24 | |
| 25 | time_value_add(&thread_info_data.user_time, &thread_info_data.system_time) |
| 26 | return thread_info_data.user_time; |
| 27 | } |
| 28 | |
| 29 | static double intervalInMSec(const time_value_t start_clock |
| 30 | , const time_value_t end_clock) |
| 31 | { |
| 32 | double duration_clock; |
| 33 | if ((end_clock.microseconds - start_clock.microseconds) < 0) { |
| 34 | duration_clock = (end_clock.seconds - start_clock.seconds-1)*1000; |
| 35 | duration_clock += (1000000 |
| 36 | + end_clock.microseconds |
| 37 | - start_clock.microseconds) / 1000.0; |
| 38 | } else { |
| 39 | duration_clock = (end_clock.seconds - start_clock.seconds)*1000; |
| 40 | duration_clock += (end_clock.microseconds - start_clock.microseconds) |
| 41 | / 1000.0; |
| 42 | } |
| 43 | return duration_clock; |
| 44 | } |
| 45 | |
| 46 | void BenchSysTimer::startWall() { |
| 47 | this->fStartWall = mach_absolute_time(); |
| 48 | } |
| 49 | void BenchSysTimer::startCpu() { |
| 50 | this->fStartCpu = macCpuTime(); |
| 51 | } |
| 52 | |
| 53 | double BenchSysTimer::endCpu() { |
| 54 | time_value_t end_cpu = macCpuTime(); |
| 55 | return intervalInMSec(this->fStartCpu, end_cpu); |
| 56 | } |
| 57 | double BenchSysTimer::endWall() { |
| 58 | uint64_t end_wall = mach_absolute_time(); |
| 59 | |
| 60 | uint64_t elapsed = end_wall - this->fStartWall; |
| 61 | mach_timebase_info_data_t sTimebaseInfo; |
| 62 | if (KERN_SUCCESS != mach_timebase_info(&sTimebaseInfo)) { |
| 63 | return 0; |
| 64 | } else { |
| 65 | uint64_t elapsedNano = elapsed * sTimebaseInfo.numer |
| 66 | / sTimebaseInfo.denom; |
| 67 | return elapsedNano / 1000000; |
| 68 | } |
| 69 | } |