ngscopeclient v0.2.1
Loading...
Searching...
No Matches
HzClock.h
Go to the documentation of this file.
1/***********************************************************************************************************************
2* *
3* HzClock (for glscopeclient, et. al.) *
4* *
5* Copyright (c) 2022 Louis A. Goessling *
6* All rights reserved. *
7* *
8* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the *
9* following conditions are met: *
10* *
11* * Redistributions of source code must retain the above copyright notice, this list of conditions, and the *
12* following disclaimer. *
13* *
14* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the *
15* following disclaimer in the documentation and/or other materials provided with the distribution. *
16* *
17* * Neither the name of the author nor the names of any contributors may be used to endorse or promote products *
18* derived from this software without specific prior written permission. *
19* *
20* THIS SOFTWARE IS PROVIDED BY THE AUTHORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
21* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL *
22* THE AUTHORS BE HELD LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES *
23* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR *
24* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT *
25* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE *
26* POSSIBILITY OF SUCH DAMAGE. *
27* *
28***********************************************************************************************************************/
29
36#ifndef HzClock_h
37#define HzClock_h
38
39#ifdef _WIN32
40#include <windows.h>
41#else
42#include <time.h>
43#endif
44
45#include <math.h>
46#include <deque>
47
52{
53public:
54 HzClock(int depth = 32)
55 : m_depth(depth)
56 {
57 Reset();
58 }
59
60 void Reset()
61 {
62 std::lock_guard<std::mutex> lock(m_mutex);
63
64 m_lastMs = GetTime();
65
66 m_deltas.clear();
67 for (int i = 0; i < m_depth; i++)
68 {
69 m_deltas.push_back(0);
70 }
71
72 m_runningAverage = 0;
73 }
74
75 void Tick()
76 {
77 std::lock_guard<std::mutex> lock(m_mutex);
78
79 uint64_t now = GetTime() * 1000;
80 uint64_t delta = now - m_lastMs;
81
82 m_lastMs = now;
83
84 m_runningAverage -= (double)m_deltas.front() / (double)m_depth;
85 m_runningAverage += (double)delta / (double)m_depth;
86
87 m_deltas.pop_front();
88 m_deltas.push_back(delta);
89 }
90
91 double GetAverageMs()
92 {
93 return m_runningAverage;
94 }
95
96 double GetAverageHz()
97 {
98 return m_runningAverage==0 ? 0 : 1000./m_runningAverage;
99 }
100
101 double GetStdDev()
102 {
103 std::lock_guard<std::mutex> lock(m_mutex);
104
105 double dev = 0;
106
107 for (auto i : m_deltas)
108 {
109 dev += pow(i - m_runningAverage, 2);
110 }
111
112 return sqrt(dev / m_depth);
113 }
114
115 static double GetTime()
116 {
117#ifdef _WIN32
118 uint64_t tm;
119 static uint64_t freq = 0;
120 QueryPerformanceCounter(reinterpret_cast<LARGE_INTEGER*>(&tm));
121 double ret = tm;
122 if(freq == 0)
123 QueryPerformanceFrequency(reinterpret_cast<LARGE_INTEGER*>(&freq));
124 return ret / freq;
125#else
126 timespec t;
128 double d = static_cast<double>(t.tv_nsec) / 1E9f;
129 d += t.tv_sec;
130 return d;
131#endif
132 }
133
134protected:
135 std::mutex m_mutex;
136
137 int m_depth;
138 uint64_t m_lastMs;
139 std::deque<uint64_t> m_deltas;
140 double m_runningAverage;
141};
142
143#endif
Definition AcceleratorBuffer.h:204
Clock that measures rate at which it is called; windowed average.
Definition HzClock.h:52