blob: 1968205808887946b2b0a9aae935c7dba7e52900 [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
17void LinuxTimer::start()
18{
Corentin Wallez5b2545b2015-05-21 15:26:15 -040019 clock_gettime(CLOCK_MONOTONIC, &mStartTime);
Corentin Wallezfe2f3d62015-05-05 17:37:39 -040020 mRunning = true;
21}
22
23void LinuxTimer::stop()
24{
Corentin Wallez5b2545b2015-05-21 15:26:15 -040025 clock_gettime(CLOCK_MONOTONIC, &mStopTime);
Corentin Wallezfe2f3d62015-05-05 17:37:39 -040026 mRunning = false;
27}
28
29double LinuxTimer::getElapsedTime() const
30{
31 struct timespec endTime;
32 if (mRunning)
33 {
Corentin Wallez5b2545b2015-05-21 15:26:15 -040034 clock_gettime(CLOCK_MONOTONIC, &endTime);
Corentin Wallezfe2f3d62015-05-05 17:37:39 -040035 }
36 else
37 {
38 endTime = mStopTime;
39 }
40
Corentin Wallez5b2545b2015-05-21 15:26:15 -040041 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 Wallezfe2f3d62015-05-05 17:37:39 -040044}
45
46Timer *CreateTimer()
47{
48 return new LinuxTimer();
49}