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" |
| 10 | |
| 11 | LinuxTimer::LinuxTimer() |
| 12 | : mRunning(false) |
| 13 | { |
| 14 | } |
| 15 | |
| 16 | void LinuxTimer::start() |
| 17 | { |
| 18 | clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &mStartTime); |
| 19 | mRunning = true; |
| 20 | } |
| 21 | |
| 22 | void LinuxTimer::stop() |
| 23 | { |
| 24 | clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &mStopTime); |
| 25 | mRunning = false; |
| 26 | } |
| 27 | |
| 28 | double LinuxTimer::getElapsedTime() const |
| 29 | { |
| 30 | struct timespec endTime; |
| 31 | if (mRunning) |
| 32 | { |
| 33 | clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &endTime); |
| 34 | } |
| 35 | else |
| 36 | { |
| 37 | endTime = mStopTime; |
| 38 | } |
| 39 | |
| 40 | return endTime.tv_sec + (1.0 / 1000000000) * endTime.tv_nsec; |
| 41 | } |
| 42 | |
| 43 | Timer *CreateTimer() |
| 44 | { |
| 45 | return new LinuxTimer(); |
| 46 | } |