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 | |
| 9 | #include "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 | |
| 12 | LinuxTimer::LinuxTimer() |
| 13 | : mRunning(false) |
| 14 | { |
| 15 | } |
| 16 | |
| 17 | void LinuxTimer::start() |
| 18 | { |
Corentin Wallez | 5b2545b | 2015-05-21 15:26:15 -0400 | [diff] [blame^] | 19 | clock_gettime(CLOCK_MONOTONIC, &mStartTime); |
Corentin Wallez | fe2f3d6 | 2015-05-05 17:37:39 -0400 | [diff] [blame] | 20 | mRunning = true; |
| 21 | } |
| 22 | |
| 23 | void LinuxTimer::stop() |
| 24 | { |
Corentin Wallez | 5b2545b | 2015-05-21 15:26:15 -0400 | [diff] [blame^] | 25 | clock_gettime(CLOCK_MONOTONIC, &mStopTime); |
Corentin Wallez | fe2f3d6 | 2015-05-05 17:37:39 -0400 | [diff] [blame] | 26 | mRunning = false; |
| 27 | } |
| 28 | |
| 29 | double LinuxTimer::getElapsedTime() const |
| 30 | { |
| 31 | struct timespec endTime; |
| 32 | if (mRunning) |
| 33 | { |
Corentin Wallez | 5b2545b | 2015-05-21 15:26:15 -0400 | [diff] [blame^] | 34 | clock_gettime(CLOCK_MONOTONIC, &endTime); |
Corentin Wallez | fe2f3d6 | 2015-05-05 17:37:39 -0400 | [diff] [blame] | 35 | } |
| 36 | else |
| 37 | { |
| 38 | endTime = mStopTime; |
| 39 | } |
| 40 | |
Corentin Wallez | 5b2545b | 2015-05-21 15:26:15 -0400 | [diff] [blame^] | 41 | double startSeconds = mStartTime.tv_sec + (1.0 / 1000000000) * mStartTime.tv_nsec; |
| 42 | double endSeconds = endTime.tv_sec + (1.0 / 1000000000) * endTime.tv_nsec; |
| 43 | return endSeconds - startSeconds; |
Corentin Wallez | fe2f3d6 | 2015-05-05 17:37:39 -0400 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | Timer *CreateTimer() |
| 47 | { |
| 48 | return new LinuxTimer(); |
| 49 | } |