3 * Copyright 2011 Google Inc.
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
8 #include "BenchSysTimer_posix.h"
13 static double intervalInMSec(const timespec start_clock
14 , const timespec end_clock)
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)
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;
25 return duration_clock;
28 void BenchSysTimer::startWall() {
29 if (-1 == clock_gettime(CLOCK_MONOTONIC, &this->fWall)) {
30 timespec none = {0, 0};
34 void BenchSysTimer::startCpu() {
35 if (-1 == clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &this->fCpu)) {
36 timespec none = {0, 0};
41 double BenchSysTimer::endCpu() {
43 if (-1 == clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &end_cpu)) {
44 timespec none = {0, 0};
47 return intervalInMSec(this->fCpu, end_cpu);
50 double BenchSysTimer::endWall() {
52 if (-1 == clock_gettime(CLOCK_MONOTONIC, &end_wall)) {
53 timespec none = {0, 0};
56 return intervalInMSec(this->fWall, end_wall);