ngscopeclient 0.1-dev+51fbda87c
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 m_lastMs = GetTime();
63
64 m_deltas.clear();
65 for (int i = 0; i < m_depth; i++)
66 {
67 m_deltas.push_back(0);
68 }
69
70 m_runningAverage = 0;
71 }
72
73 void Tick()
74 {
75 uint64_t now = GetTime() * 1000;
76 uint64_t delta = now - m_lastMs;
77
78 m_lastMs = now;
79
80 m_runningAverage -= (double)m_deltas.front() / (double)m_depth;
81 m_runningAverage += (double)delta / (double)m_depth;
82
83 m_deltas.pop_front();
84 m_deltas.push_back(delta);
85 }
86
87 double GetAverageMs()
88 {
89 return m_runningAverage;
90 }
91
92 double GetAverageHz()
93 {
94 return m_runningAverage==0 ? 0 : 1000./m_runningAverage;
95 }
96
97 double GetStdDev()
98 {
99 double dev = 0;
100
101 for (auto i : m_deltas)
102 {
103 dev += pow(i - m_runningAverage, 2);
104 }
105
106 return sqrt(dev / m_depth);
107 }
108
109 static double GetTime()
110 {
111#ifdef _WIN32
112 uint64_t tm;
113 static uint64_t freq = 0;
114 QueryPerformanceCounter(reinterpret_cast<LARGE_INTEGER*>(&tm));
115 double ret = tm;
116 if(freq == 0)
117 QueryPerformanceFrequency(reinterpret_cast<LARGE_INTEGER*>(&freq));
118 return ret / freq;
119#else
120 timespec t;
121 clock_gettime(CLOCK_REALTIME,&t);
122 double d = static_cast<double>(t.tv_nsec) / 1E9f;
123 d += t.tv_sec;
124 return d;
125#endif
126 }
127
128protected:
129 int m_depth;
130 uint64_t m_lastMs;
131 std::deque<uint64_t> m_deltas;
132 double m_runningAverage;
133};
134
135#endif
Clock that measures rate at which it is called; windowed average.
Definition: HzClock.h:52