blob: e74b399ac51352787867c42b46e14b66e05dddb1 [file] [log] [blame]
Corentin Wallezfe2f3d62015-05-05 17:37:39 -04001//
2// Copyright (c) 2015 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7// LinuxTimer.cpp: Implementation of a high precision timer class on Linux
8
Jamie Madillba319ba2018-12-29 10:29:33 -05009#include "util/linux/LinuxTimer.h"
Corentin Wallez5b2545b2015-05-21 15:26:15 -040010#include <iostream>
Corentin Wallezfe2f3d62015-05-05 17:37:39 -040011
Jamie Madillba319ba2018-12-29 10:29:33 -050012LinuxTimer::LinuxTimer() : mRunning(false) {}
Corentin Wallezfe2f3d62015-05-05 17:37:39 -040013
Shahbaz Youssefi479918d2018-10-22 11:53:51 -040014namespace
15{
16uint64_t getCurrentTimeNs()
17{
18 struct timespec currentTime;
19 clock_gettime(CLOCK_MONOTONIC, &currentTime);
20 return currentTime.tv_sec * 1'000'000'000llu + currentTime.tv_nsec;
21}
22} // anonymous namespace
23
Corentin Wallezfe2f3d62015-05-05 17:37:39 -040024void LinuxTimer::start()
25{
Shahbaz Youssefi479918d2018-10-22 11:53:51 -040026 mStartTimeNs = getCurrentTimeNs();
Jamie Madillba319ba2018-12-29 10:29:33 -050027 mRunning = true;
Corentin Wallezfe2f3d62015-05-05 17:37:39 -040028}
29
30void LinuxTimer::stop()
31{
Shahbaz Youssefi479918d2018-10-22 11:53:51 -040032 mStopTimeNs = getCurrentTimeNs();
Jamie Madillba319ba2018-12-29 10:29:33 -050033 mRunning = false;
Corentin Wallezfe2f3d62015-05-05 17:37:39 -040034}
35
36double LinuxTimer::getElapsedTime() const
37{
Shahbaz Youssefi479918d2018-10-22 11:53:51 -040038 uint64_t endTimeNs;
Corentin Wallezfe2f3d62015-05-05 17:37:39 -040039 if (mRunning)
40 {
Shahbaz Youssefi479918d2018-10-22 11:53:51 -040041 endTimeNs = getCurrentTimeNs();
Corentin Wallezfe2f3d62015-05-05 17:37:39 -040042 }
43 else
44 {
Shahbaz Youssefi479918d2018-10-22 11:53:51 -040045 endTimeNs = mStopTimeNs;
Corentin Wallezfe2f3d62015-05-05 17:37:39 -040046 }
47
Shahbaz Youssefi479918d2018-10-22 11:53:51 -040048 return (endTimeNs - mStartTimeNs) * 1e-9;
49}
50
51double LinuxTimer::getAbsoluteTime()
52{
53 return getCurrentTimeNs() * 1e-9;
Corentin Wallezfe2f3d62015-05-05 17:37:39 -040054}
55
56Timer *CreateTimer()
57{
58 return new LinuxTimer();
59}