1 // 2 // Copyright 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 // PosixTimer.cpp: Implementation of a high precision timer class on POSIX 8 9 #include "util/posix/PosixTimer.h" 10 #include <iostream> 11 PosixTimer()12PosixTimer::PosixTimer() : mRunning(false) {} 13 14 namespace 15 { getCurrentTimeNs()16uint64_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 start()24void PosixTimer::start() 25 { 26 mStartTimeNs = getCurrentTimeNs(); 27 mRunning = true; 28 } 29 stop()30void PosixTimer::stop() 31 { 32 mStopTimeNs = getCurrentTimeNs(); 33 mRunning = false; 34 } 35 getElapsedTime() const36double PosixTimer::getElapsedTime() const 37 { 38 uint64_t endTimeNs; 39 if (mRunning) 40 { 41 endTimeNs = getCurrentTimeNs(); 42 } 43 else 44 { 45 endTimeNs = mStopTimeNs; 46 } 47 48 return (endTimeNs - mStartTimeNs) * 1e-9; 49 } 50 getAbsoluteTime()51double PosixTimer::getAbsoluteTime() 52 { 53 return getCurrentTimeNs() * 1e-9; 54 } 55 CreateTimer()56Timer *CreateTimer() 57 { 58 return new PosixTimer(); 59 } 60