blob: e9067e342d96708083080fedbefcafcf0a6c4f9d [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
9#include "linux/LinuxTimer.h"
Corentin Wallez5b2545b2015-05-21 15:26:15 -040010#include <iostream>
Corentin Wallezfe2f3d62015-05-05 17:37:39 -040011
12LinuxTimer::LinuxTimer()
13 : mRunning(false)
14{
15}
16
Shahbaz Youssefi479918d2018-10-22 11:53:51 -040017namespace
18{
19uint64_t getCurrentTimeNs()
20{
21 struct timespec currentTime;
22 clock_gettime(CLOCK_MONOTONIC, &currentTime);
23 return currentTime.tv_sec * 1'000'000'000llu + currentTime.tv_nsec;
24}
25} // anonymous namespace
26
Corentin Wallezfe2f3d62015-05-05 17:37:39 -040027void LinuxTimer::start()
28{
Shahbaz Youssefi479918d2018-10-22 11:53:51 -040029 mStartTimeNs = getCurrentTimeNs();
Corentin Wallezfe2f3d62015-05-05 17:37:39 -040030 mRunning = true;
31}
32
33void LinuxTimer::stop()
34{
Shahbaz Youssefi479918d2018-10-22 11:53:51 -040035 mStopTimeNs = getCurrentTimeNs();
Corentin Wallezfe2f3d62015-05-05 17:37:39 -040036 mRunning = false;
37}
38
39double LinuxTimer::getElapsedTime() const
40{
Shahbaz Youssefi479918d2018-10-22 11:53:51 -040041 uint64_t endTimeNs;
Corentin Wallezfe2f3d62015-05-05 17:37:39 -040042 if (mRunning)
43 {
Shahbaz Youssefi479918d2018-10-22 11:53:51 -040044 endTimeNs = getCurrentTimeNs();
Corentin Wallezfe2f3d62015-05-05 17:37:39 -040045 }
46 else
47 {
Shahbaz Youssefi479918d2018-10-22 11:53:51 -040048 endTimeNs = mStopTimeNs;
Corentin Wallezfe2f3d62015-05-05 17:37:39 -040049 }
50
Shahbaz Youssefi479918d2018-10-22 11:53:51 -040051 return (endTimeNs - mStartTimeNs) * 1e-9;
52}
53
54double LinuxTimer::getAbsoluteTime()
55{
56 return getCurrentTimeNs() * 1e-9;
Corentin Wallezfe2f3d62015-05-05 17:37:39 -040057}
58
59Timer *CreateTimer()
60{
61 return new LinuxTimer();
62}