Edit

kc3-lang/angle/util/linux/LinuxTimer.cpp

Branch :

  • Show log

    Commit

  • Author : Shahbaz Youssefi
    Date : 2018-10-22 11:53:51
    Hash : 479918de
    Message : Get absolute time from timers Independent of start()/stop()/getElapsedTime() functionality, getAbsoluteTime() is added to return the time regardless of whether the timer is running. Bug: angleproject:2908 Change-Id: I056aeb6eddfba8757a139934c1cf68e00e860de6 Reviewed-on: https://chromium-review.googlesource.com/c/1296952 Commit-Queue: Shahbaz Youssefi <syoussefi@chromium.org> Reviewed-by: Geoff Lang <geofflang@chromium.org>

  • util/linux/LinuxTimer.cpp
  • //
    // Copyright (c) 2015 The ANGLE Project Authors. All rights reserved.
    // Use of this source code is governed by a BSD-style license that can be
    // found in the LICENSE file.
    //
    
    // LinuxTimer.cpp: Implementation of a high precision timer class on Linux
    
    #include "linux/LinuxTimer.h"
    #include <iostream>
    
    LinuxTimer::LinuxTimer()
        : mRunning(false)
    {
    }
    
    namespace
    {
    uint64_t getCurrentTimeNs()
    {
        struct timespec currentTime;
        clock_gettime(CLOCK_MONOTONIC, &currentTime);
        return currentTime.tv_sec * 1'000'000'000llu + currentTime.tv_nsec;
    }
    }  // anonymous namespace
    
    void LinuxTimer::start()
    {
        mStartTimeNs = getCurrentTimeNs();
        mRunning = true;
    }
    
    void LinuxTimer::stop()
    {
        mStopTimeNs = getCurrentTimeNs();
        mRunning = false;
    }
    
    double LinuxTimer::getElapsedTime() const
    {
        uint64_t endTimeNs;
        if (mRunning)
        {
            endTimeNs = getCurrentTimeNs();
        }
        else
        {
            endTimeNs = mStopTimeNs;
        }
    
        return (endTimeNs - mStartTimeNs) * 1e-9;
    }
    
    double LinuxTimer::getAbsoluteTime()
    {
        return getCurrentTimeNs() * 1e-9;
    }
    
    Timer *CreateTimer()
    {
        return new LinuxTimer();
    }