Corentin Wallez | fe2f3d6 | 2015-05-05 17:37:39 -0400 | [diff] [blame] | 1 | // |
| 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 Madill | ba319ba | 2018-12-29 10:29:33 -0500 | [diff] [blame^] | 9 | #include "util/linux/LinuxTimer.h" |
Corentin Wallez | 5b2545b | 2015-05-21 15:26:15 -0400 | [diff] [blame] | 10 | #include <iostream> |
Corentin Wallez | fe2f3d6 | 2015-05-05 17:37:39 -0400 | [diff] [blame] | 11 | |
Jamie Madill | ba319ba | 2018-12-29 10:29:33 -0500 | [diff] [blame^] | 12 | LinuxTimer::LinuxTimer() : mRunning(false) {} |
Corentin Wallez | fe2f3d6 | 2015-05-05 17:37:39 -0400 | [diff] [blame] | 13 | |
Shahbaz Youssefi | 479918d | 2018-10-22 11:53:51 -0400 | [diff] [blame] | 14 | namespace |
| 15 | { |
| 16 | uint64_t getCurrentTimeNs() |
| 17 | { |
| 18 | struct timespec currentTime; |
| 19 | clock_gettime(CLOCK_MONOTONIC, ¤tTime); |
| 20 | return currentTime.tv_sec * 1'000'000'000llu + currentTime.tv_nsec; |
| 21 | } |
| 22 | } // anonymous namespace |
| 23 | |
Corentin Wallez | fe2f3d6 | 2015-05-05 17:37:39 -0400 | [diff] [blame] | 24 | void LinuxTimer::start() |
| 25 | { |
Shahbaz Youssefi | 479918d | 2018-10-22 11:53:51 -0400 | [diff] [blame] | 26 | mStartTimeNs = getCurrentTimeNs(); |
Jamie Madill | ba319ba | 2018-12-29 10:29:33 -0500 | [diff] [blame^] | 27 | mRunning = true; |
Corentin Wallez | fe2f3d6 | 2015-05-05 17:37:39 -0400 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | void LinuxTimer::stop() |
| 31 | { |
Shahbaz Youssefi | 479918d | 2018-10-22 11:53:51 -0400 | [diff] [blame] | 32 | mStopTimeNs = getCurrentTimeNs(); |
Jamie Madill | ba319ba | 2018-12-29 10:29:33 -0500 | [diff] [blame^] | 33 | mRunning = false; |
Corentin Wallez | fe2f3d6 | 2015-05-05 17:37:39 -0400 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | double LinuxTimer::getElapsedTime() const |
| 37 | { |
Shahbaz Youssefi | 479918d | 2018-10-22 11:53:51 -0400 | [diff] [blame] | 38 | uint64_t endTimeNs; |
Corentin Wallez | fe2f3d6 | 2015-05-05 17:37:39 -0400 | [diff] [blame] | 39 | if (mRunning) |
| 40 | { |
Shahbaz Youssefi | 479918d | 2018-10-22 11:53:51 -0400 | [diff] [blame] | 41 | endTimeNs = getCurrentTimeNs(); |
Corentin Wallez | fe2f3d6 | 2015-05-05 17:37:39 -0400 | [diff] [blame] | 42 | } |
| 43 | else |
| 44 | { |
Shahbaz Youssefi | 479918d | 2018-10-22 11:53:51 -0400 | [diff] [blame] | 45 | endTimeNs = mStopTimeNs; |
Corentin Wallez | fe2f3d6 | 2015-05-05 17:37:39 -0400 | [diff] [blame] | 46 | } |
| 47 | |
Shahbaz Youssefi | 479918d | 2018-10-22 11:53:51 -0400 | [diff] [blame] | 48 | return (endTimeNs - mStartTimeNs) * 1e-9; |
| 49 | } |
| 50 | |
| 51 | double LinuxTimer::getAbsoluteTime() |
| 52 | { |
| 53 | return getCurrentTimeNs() * 1e-9; |
Corentin Wallez | fe2f3d6 | 2015-05-05 17:37:39 -0400 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | Timer *CreateTimer() |
| 57 | { |
| 58 | return new LinuxTimer(); |
| 59 | } |