2011-07-28 14:26:00 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2011 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
2014-06-20 18:29:20 +00:00
|
|
|
#include "SysTimer_posix.h"
|
2011-06-07 19:16:02 +00:00
|
|
|
|
2014-06-20 18:29:20 +00:00
|
|
|
static double interval_in_ms(timespec start_clock, timespec end_clock)
|
2011-06-07 19:16:02 +00:00
|
|
|
{
|
|
|
|
double duration_clock;
|
|
|
|
if ((end_clock.tv_nsec - start_clock.tv_nsec) < 0) {
|
2014-06-20 18:29:20 +00:00
|
|
|
duration_clock = (end_clock.tv_sec - start_clock.tv_sec - 1) * 1000;
|
|
|
|
duration_clock += (1000000000 + end_clock.tv_nsec - start_clock.tv_nsec) / 1000000.0;
|
2011-06-07 19:16:02 +00:00
|
|
|
} else {
|
2014-06-20 18:29:20 +00:00
|
|
|
duration_clock = (end_clock.tv_sec - start_clock.tv_sec) * 1000;
|
2011-06-07 19:16:02 +00:00
|
|
|
duration_clock += (end_clock.tv_nsec - start_clock.tv_nsec) / 1000000.0;
|
|
|
|
}
|
|
|
|
return duration_clock;
|
|
|
|
}
|
|
|
|
|
2014-06-20 18:29:20 +00:00
|
|
|
void SysTimer::startWall() {
|
|
|
|
if (-1 == clock_gettime(CLOCK_MONOTONIC, &fWall)) {
|
2011-06-07 19:16:02 +00:00
|
|
|
timespec none = {0, 0};
|
2014-06-20 18:29:20 +00:00
|
|
|
fWall = none;
|
2011-06-07 19:16:02 +00:00
|
|
|
}
|
|
|
|
}
|
2014-06-20 18:29:20 +00:00
|
|
|
void SysTimer::startCpu() {
|
|
|
|
if (-1 == clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &fCpu)) {
|
2011-06-07 19:16:02 +00:00
|
|
|
timespec none = {0, 0};
|
2014-06-20 18:29:20 +00:00
|
|
|
fCpu = none;
|
2011-06-07 19:16:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-20 18:29:20 +00:00
|
|
|
double SysTimer::endCpu() {
|
2011-06-07 19:16:02 +00:00
|
|
|
timespec end_cpu;
|
|
|
|
if (-1 == clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &end_cpu)) {
|
|
|
|
timespec none = {0, 0};
|
|
|
|
end_cpu = none;
|
|
|
|
}
|
2014-06-20 18:29:20 +00:00
|
|
|
return interval_in_ms(fCpu, end_cpu);
|
2011-06-07 19:16:02 +00:00
|
|
|
}
|
|
|
|
|
2014-06-20 18:29:20 +00:00
|
|
|
double SysTimer::endWall() {
|
2011-06-07 19:16:02 +00:00
|
|
|
timespec end_wall;
|
|
|
|
if (-1 == clock_gettime(CLOCK_MONOTONIC, &end_wall)) {
|
|
|
|
timespec none = {0, 0};
|
|
|
|
end_wall = none;
|
|
|
|
}
|
2014-06-20 18:29:20 +00:00
|
|
|
return interval_in_ms(fWall, end_wall);
|
2011-06-07 19:16:02 +00:00
|
|
|
}
|