Commit 5b2545bf by Corentin Wallez

Correct LinuxTimer time calculations

Changes to CLOCK_MONOTONIC as CLOCK_PROCESS_CPUTIME counts only user time and not kernel time. BUG=angleproject:892 Change-Id: I3d5aee26ee2bacd7449fdd7795ad8c2b289d7324 Reviewed-on: https://chromium-review.googlesource.com/272652Reviewed-by: 's avatarJamie Madill <jmadill@chromium.org> Reviewed-by: 's avatarGeoff Lang <geofflang@chromium.org> Tested-by: 's avatarCorentin Wallez <cwallez@chromium.org>
parent 1b21fd39
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
// LinuxTimer.cpp: Implementation of a high precision timer class on Linux // LinuxTimer.cpp: Implementation of a high precision timer class on Linux
#include "linux/LinuxTimer.h" #include "linux/LinuxTimer.h"
#include <iostream>
LinuxTimer::LinuxTimer() LinuxTimer::LinuxTimer()
: mRunning(false) : mRunning(false)
...@@ -15,13 +16,13 @@ LinuxTimer::LinuxTimer() ...@@ -15,13 +16,13 @@ LinuxTimer::LinuxTimer()
void LinuxTimer::start() void LinuxTimer::start()
{ {
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &mStartTime); clock_gettime(CLOCK_MONOTONIC, &mStartTime);
mRunning = true; mRunning = true;
} }
void LinuxTimer::stop() void LinuxTimer::stop()
{ {
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &mStopTime); clock_gettime(CLOCK_MONOTONIC, &mStopTime);
mRunning = false; mRunning = false;
} }
...@@ -30,14 +31,16 @@ double LinuxTimer::getElapsedTime() const ...@@ -30,14 +31,16 @@ double LinuxTimer::getElapsedTime() const
struct timespec endTime; struct timespec endTime;
if (mRunning) if (mRunning)
{ {
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &endTime); clock_gettime(CLOCK_MONOTONIC, &endTime);
} }
else else
{ {
endTime = mStopTime; endTime = mStopTime;
} }
return endTime.tv_sec + (1.0 / 1000000000) * endTime.tv_nsec; double startSeconds = mStartTime.tv_sec + (1.0 / 1000000000) * mStartTime.tv_nsec;
double endSeconds = endTime.tv_sec + (1.0 / 1000000000) * endTime.tv_nsec;
return endSeconds - startSeconds;
} }
Timer *CreateTimer() Timer *CreateTimer()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment