1/* 2 * Copyright 2011 Google Inc. 3 * 4 * Use of this source code is governed by a BSD-style license that can be 5 * found in the LICENSE file. 6 */ 7#include "SysTimer_posix.h" 8 9static double interval_in_ms(timespec start_clock, timespec end_clock) 10{ 11 double duration_clock; 12 if ((end_clock.tv_nsec - start_clock.tv_nsec) < 0) { 13 duration_clock = (end_clock.tv_sec - start_clock.tv_sec - 1) * 1000; 14 duration_clock += (1000000000 + end_clock.tv_nsec - start_clock.tv_nsec) / 1000000.0; 15 } else { 16 duration_clock = (end_clock.tv_sec - start_clock.tv_sec) * 1000; 17 duration_clock += (end_clock.tv_nsec - start_clock.tv_nsec) / 1000000.0; 18 } 19 return duration_clock; 20} 21 22void SysTimer::startWall() { 23 if (-1 == clock_gettime(CLOCK_MONOTONIC, &fWall)) { 24 timespec none = {0, 0}; 25 fWall = none; 26 } 27} 28void SysTimer::startCpu() { 29 if (-1 == clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &fCpu)) { 30 timespec none = {0, 0}; 31 fCpu = none; 32 } 33} 34 35double SysTimer::endCpu() { 36 timespec end_cpu; 37 if (-1 == clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &end_cpu)) { 38 timespec none = {0, 0}; 39 end_cpu = none; 40 } 41 return interval_in_ms(fCpu, end_cpu); 42} 43 44double SysTimer::endWall() { 45 timespec end_wall; 46 if (-1 == clock_gettime(CLOCK_MONOTONIC, &end_wall)) { 47 timespec none = {0, 0}; 48 end_wall = none; 49 } 50 return interval_in_ms(fWall, end_wall); 51} 52