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_posix.h" 9 10 //Time 11 #include <time.h> 12 13 static double intervalInMSec(const timespec start_clock 14 , const timespec end_clock) 15 { 16 double duration_clock; 17 if ((end_clock.tv_nsec - start_clock.tv_nsec) < 0) { 18 duration_clock = (end_clock.tv_sec - start_clock.tv_sec-1)*1000; 19 duration_clock += (1000000000 + end_clock.tv_nsec - start_clock.tv_nsec) 20 / 1000000.0; 21 } else { 22 duration_clock = (end_clock.tv_sec - start_clock.tv_sec)*1000; 23 duration_clock += (end_clock.tv_nsec - start_clock.tv_nsec) / 1000000.0; 24 } 25 return duration_clock; 26 } 27 28 void BenchSysTimer::startWall() { 29 if (-1 == clock_gettime(CLOCK_MONOTONIC, &this->fWall)) { 30 timespec none = {0, 0}; 31 this->fWall = none; 32 } 33 } 34 void BenchSysTimer::startCpu() { 35 if (-1 == clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &this->fCpu)) { 36 timespec none = {0, 0}; 37 this->fCpu = none; 38 } 39 } 40 41 double BenchSysTimer::endCpu() { 42 timespec end_cpu; 43 if (-1 == clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &end_cpu)) { 44 timespec none = {0, 0}; 45 end_cpu = none; 46 } 47 return intervalInMSec(this->fCpu, end_cpu); 48 } 49 50 double BenchSysTimer::endWall() { 51 timespec end_wall; 52 if (-1 == clock_gettime(CLOCK_MONOTONIC, &end_wall)) { 53 timespec none = {0, 0}; 54 end_wall = none; 55 } 56 return intervalInMSec(this->fWall, end_wall); 57 } 58