Project
Loading...
Searching...
No Matches
timer.cxx
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11
14
15#include "timer.h"
16#ifdef _WIN32
17#include <windows.h>
18#include <winbase.h>
19#elif defined(__MACH__) || defined(__APPLE__)
20#include <mach/clock.h>
21#include <mach/mach.h>
22#else
23#include <ctime>
24#endif
25
26inline double HighResTimer::GetTime()
27{
28#ifdef _WIN32
29 __int64 istart;
30 QueryPerformanceCounter((LARGE_INTEGER*)&istart);
31 return ((double)istart);
32#elif defined(__MACH__) || defined(__APPLE__)
33 clock_serv_t cclock;
34 mach_timespec_t mts;
35 host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
36 clock_get_time(cclock, &mts);
37 mach_port_deallocate(mach_task_self(), cclock);
38 return ((double)mts.tv_sec * 1.0e9 + (double)mts.tv_nsec);
39#else
40 timespec tv;
41 clock_gettime(CLOCK_REALTIME, &tv);
42 return ((double)tv.tv_sec * 1.0e9 + (double)tv.tv_nsec);
43#endif
44}
45
46inline double HighResTimer::GetFrequency()
47{
48#ifdef _WIN32
49 __int64 ifreq;
50 QueryPerformanceFrequency((LARGE_INTEGER*)&ifreq);
51 return ((double)ifreq);
52#else
53 return (1.0e9);
54#endif
55}
56
58{
59 StartTime = GetTime();
60 running = 1;
61}
62
64{
65 ElapsedTime = 0;
66 Start();
67}
68
70{
71 if (running == 0) {
72 return;
73 }
74 running = 0;
75 double EndTime = 0;
76 EndTime = GetTime();
77 ElapsedTime += EndTime - StartTime;
78 StartTime = 0;
79}
80
82{
83 StartTime = 0.;
84 running = 0;
85}
86
88{
89 if (running == 0) {
90 return;
91 }
92 running = 0;
93 double EndTime = 0;
94 EndTime = GetTime();
95 ElapsedTime += EndTime - StartTime;
96 StartTime = 0;
97 startTimer.StartTime = EndTime;
98 startTimer.running = 1;
99}
100
102{
103 ElapsedTime = 0;
104 StartTime = 0;
105 running = 0;
106}
107
108double HighResTimer::GetElapsedTime() { return ElapsedTime / Frequency; }
109
111{
112 if (running == 0) {
113 double retVal = GetElapsedTime();
114 if (reset) {
115 Reset();
116 }
117 return (retVal);
118 }
119 double CurrentTime = GetTime();
120 double retVal = (CurrentTime - StartTime + ElapsedTime) / Frequency;
121 if (reset) {
122 ElapsedTime = 0;
123 Start();
124 }
125 return (retVal);
126}
127
128void HighResTimer::AddTime(double t) { ElapsedTime += t * Frequency; }
129
130double HighResTimer::Frequency = HighResTimer::GetFrequency();
int32_t retVal
void AddTime(double t)
Definition timer.cxx:128
void Reset()
Definition timer.cxx:101
void Start()
Definition timer.cxx:57
double GetCurrentElapsedTime(bool reset=false)
Definition timer.cxx:110
double GetElapsedTime()
Definition timer.cxx:108
void Abort()
Definition timer.cxx:81
void StopAndStart(HighResTimer &startTimer)
Definition timer.cxx:87
void Stop()
Definition timer.cxx:69
void ResetStart()
Definition timer.cxx:63