blob: 5d28f40f2e80d7ba9a9f29be1b88471e12fe2cd5 [file] [log] [blame]
bungeman@google.combe9ad4e2011-06-07 19:16:02 +00001#include "BenchSysTimer_posix.h"
2
3//Time
4#include <time.h>
5
6static double intervalInMSec(const timespec start_clock
7 , const timespec end_clock)
8{
9 double duration_clock;
10 if ((end_clock.tv_nsec - start_clock.tv_nsec) < 0) {
11 duration_clock = (end_clock.tv_sec - start_clock.tv_sec-1)*1000;
12 duration_clock += (1000000000 + end_clock.tv_nsec - start_clock.tv_nsec)
13 / 1000000.0;
14 } else {
15 duration_clock = (end_clock.tv_sec - start_clock.tv_sec)*1000;
16 duration_clock += (end_clock.tv_nsec - start_clock.tv_nsec) / 1000000.0;
17 }
18 return duration_clock;
19}
20
21void BenchSysTimer::startWall() {
22 if (-1 == clock_gettime(CLOCK_MONOTONIC, &this->fWall)) {
23 timespec none = {0, 0};
24 this->fWall = none;
25 }
26}
27void BenchSysTimer::startCpu() {
28 if (-1 == clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &this->fCpu)) {
29 timespec none = {0, 0};
30 this->fCpu = none;
31 }
32}
33
34double BenchSysTimer::endCpu() {
35 timespec end_cpu;
36 if (-1 == clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &end_cpu)) {
37 timespec none = {0, 0};
38 end_cpu = none;
39 }
40 return intervalInMSec(this->fCpu, end_cpu);
41}
42
43double BenchSysTimer::endWall() {
44 timespec end_wall;
45 if (-1 == clock_gettime(CLOCK_MONOTONIC, &end_wall)) {
46 timespec none = {0, 0};
47 end_wall = none;
48 }
49 return intervalInMSec(this->fWall, end_wall);
50}