|
a/sc2src/chrono.cpp |
|
b/sc2src/chrono.cpp |
|
... |
|
... |
16 |
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
16 |
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
17 |
*/
|
17 |
*/
|
18 |
|
18 |
|
19 |
// Measure and display time intervals.
|
19 |
// Measure and display time intervals.
|
20 |
|
20 |
|
21 |
#include <stdio.h>
|
21 |
#include "chrono.h"
|
22 |
#include <stdlib.h>
|
|
|
23 |
#include <time.h>
|
|
|
24 |
#include <sys/time.h>
|
|
|
25 |
#include <iostream>
|
|
|
26 |
|
22 |
|
27 |
#include "chrono.h"
|
23 |
#include <chrono>
|
28 |
|
24 |
|
29 |
using namespace std;
|
25 |
using namespace std;
|
30 |
|
26 |
|
31 |
////////////////////
|
|
|
32 |
|
|
|
33 |
#ifndef CLOCK_REALTIME
|
|
|
34 |
#define CLOCK_REALTIME 1
|
|
|
35 |
#endif
|
|
|
36 |
|
|
|
37 |
#define MILLIS(TS1, TS2) \
|
|
|
38 |
((long long)((TS2).tv_sec - (TS1).tv_sec) * 1000LL + \
|
|
|
39 |
((TS2).tv_nsec - (TS1).tv_nsec) / 1000000)
|
|
|
40 |
|
|
|
41 |
#define MICROS(TS1, TS2) \
|
|
|
42 |
((long long)((TS2).tv_sec - (TS1).tv_sec) * 1000000LL + \
|
|
|
43 |
((TS2).tv_nsec - (TS1).tv_nsec) / 1000)
|
|
|
44 |
|
|
|
45 |
#define SECONDS(TS1, TS2) \
|
|
|
46 |
(float((TS2).tv_sec - (TS1).tv_sec) + \
|
|
|
47 |
float((TS2).tv_nsec - (TS1).tv_nsec) * 1e-9)
|
|
|
48 |
|
|
|
49 |
// We use gettimeofday instead of clock_gettime for now and get only
|
|
|
50 |
// uS resolution, because clock_gettime is more configuration trouble
|
|
|
51 |
// than it's worth
|
|
|
52 |
static void gettime(int, Chrono::TimeSpec *ts)
|
|
|
53 |
{
|
|
|
54 |
struct timeval tv;
|
|
|
55 |
gettimeofday(&tv, 0);
|
|
|
56 |
ts->tv_sec = tv.tv_sec;
|
|
|
57 |
ts->tv_nsec = tv.tv_usec * 1000;
|
|
|
58 |
}
|
|
|
59 |
///// End system interface (used to be much more complicated in the 199xs...)
|
|
|
60 |
|
|
|
61 |
Chrono::TimeSpec Chrono::o_now;
|
27 |
Chrono::TimePoint Chrono::o_now;
|
62 |
|
28 |
|
63 |
void Chrono::refnow()
|
29 |
void Chrono::refnow()
|
64 |
{
|
30 |
{
|
65 |
gettime(CLOCK_REALTIME, &o_now);
|
31 |
o_now = chrono::steady_clock::now();
|
66 |
}
|
|
|
67 |
|
|
|
68 |
long long Chrono::amicros() const
|
|
|
69 |
{
|
|
|
70 |
TimeSpec ts;
|
|
|
71 |
ts.tv_sec = 0;
|
|
|
72 |
ts.tv_nsec = 0;
|
|
|
73 |
return MICROS(ts, m_orig);
|
|
|
74 |
}
|
32 |
}
|
75 |
|
33 |
|
76 |
Chrono::Chrono()
|
34 |
Chrono::Chrono()
|
|
|
35 |
: m_orig(chrono::steady_clock::now())
|
77 |
{
|
36 |
{
|
78 |
restart();
|
|
|
79 |
}
|
37 |
}
|
80 |
|
38 |
|
81 |
// Reset and return value before rest in milliseconds
|
|
|
82 |
long Chrono::restart()
|
39 |
long Chrono::restart()
|
83 |
{
|
40 |
{
|
84 |
TimeSpec now;
|
41 |
auto nnow = chrono::steady_clock::now();
|
85 |
gettime(CLOCK_REALTIME, &now);
|
42 |
auto ms =
|
86 |
long ret = MILLIS(m_orig, now);
|
43 |
chrono::duration_cast<chrono::milliseconds>(nnow - m_orig);
|
87 |
m_orig = now;
|
44 |
m_orig = nnow;
|
88 |
return ret;
|
45 |
return ms.count();
|
89 |
}
|
46 |
}
|
|
|
47 |
|
90 |
long Chrono::urestart()
|
48 |
long Chrono::urestart()
|
91 |
{
|
49 |
{
|
92 |
TimeSpec now;
|
50 |
auto nnow = chrono::steady_clock::now();
|
93 |
gettime(CLOCK_REALTIME, &now);
|
51 |
auto ms =
|
94 |
long ret = MICROS(m_orig, now);
|
52 |
chrono::duration_cast<chrono::microseconds>(nnow - m_orig);
|
95 |
m_orig = now;
|
53 |
m_orig = nnow;
|
96 |
return ret;
|
54 |
return ms.count();
|
97 |
}
|
55 |
}
|
98 |
|
56 |
|
99 |
// Get current timer value, milliseconds
|
|
|
100 |
long Chrono::millis(bool frozen)
|
57 |
long Chrono::millis(bool frozen)
|
101 |
{
|
58 |
{
|
102 |
if (frozen) {
|
59 |
if (frozen) {
|
103 |
return MILLIS(m_orig, o_now);
|
60 |
return chrono::duration_cast<chrono::milliseconds>
|
|
|
61 |
(o_now - m_orig).count();
|
104 |
} else {
|
62 |
} else {
|
105 |
TimeSpec now;
|
63 |
return chrono::duration_cast<chrono::milliseconds>
|
106 |
gettime(CLOCK_REALTIME, &now);
|
64 |
(chrono::steady_clock::now() - m_orig).count();
|
107 |
return MILLIS(m_orig, now);
|
|
|
108 |
}
|
65 |
}
|
109 |
}
|
66 |
}
|
110 |
|
67 |
|
111 |
//
|
|
|
112 |
long Chrono::micros(bool frozen)
|
68 |
long Chrono::micros(bool frozen)
|
113 |
{
|
69 |
{
|
114 |
if (frozen) {
|
70 |
if (frozen) {
|
115 |
return MICROS(m_orig, o_now);
|
71 |
return chrono::duration_cast<chrono::microseconds>
|
|
|
72 |
(o_now - m_orig).count();
|
116 |
} else {
|
73 |
} else {
|
117 |
TimeSpec now;
|
74 |
return chrono::duration_cast<chrono::microseconds>
|
118 |
gettime(CLOCK_REALTIME, &now);
|
75 |
(chrono::steady_clock::now() - m_orig).count();
|
119 |
return MICROS(m_orig, now);
|
|
|
120 |
}
|
76 |
}
|
121 |
}
|
77 |
}
|
122 |
|
78 |
|
123 |
float Chrono::secs(bool frozen)
|
79 |
float Chrono::secs(bool frozen)
|
124 |
{
|
80 |
{
|
125 |
if (frozen) {
|
81 |
if (frozen) {
|
126 |
return SECONDS(m_orig, o_now);
|
82 |
return chrono::duration_cast<chrono::seconds>
|
|
|
83 |
(o_now - m_orig).count();
|
127 |
} else {
|
84 |
} else {
|
128 |
TimeSpec now;
|
85 |
return (chrono::duration_cast<chrono::seconds>
|
129 |
gettime(CLOCK_REALTIME, &now);
|
86 |
(chrono::steady_clock::now() - m_orig)).count();
|
130 |
return SECONDS(m_orig, now);
|
|
|
131 |
}
|
87 |
}
|
132 |
}
|
88 |
}
|
133 |
|
89 |
|
134 |
#else
|
90 |
#else
|
135 |
|
91 |
|
136 |
///////////////////// test driver
|
92 |
// Test
|
137 |
|
|
|
138 |
|
|
|
139 |
#include <stdio.h>
|
93 |
#include <stdio.h>
|
140 |
#include <signal.h>
|
94 |
#include <signal.h>
|
141 |
#include <unistd.h>
|
95 |
#include <unistd.h>
|
142 |
#include <stdlib.h>
|
96 |
#include <stdlib.h>
|
143 |
|
|
|
144 |
#include <iostream>
|
|
|
145 |
|
97 |
|
146 |
#include "chrono.h"
|
98 |
#include "chrono.h"
|
147 |
|
99 |
|
148 |
using namespace std;
|
100 |
using namespace std;
|
149 |
|
101 |
|
|
... |
|
... |
159 |
Chrono rchrono;
|
111 |
Chrono rchrono;
|
160 |
|
112 |
|
161 |
void
|
113 |
void
|
162 |
showsecs(long msecs)
|
114 |
showsecs(long msecs)
|
163 |
{
|
115 |
{
|
164 |
fprintf(stderr, "%3.5f S", ((float)msecs) / 1000.0);
|
116 |
fprintf(stderr, "%3.5f S", (double(msecs)) / 1000.0);
|
165 |
}
|
117 |
}
|
166 |
|
118 |
|
167 |
void
|
119 |
void
|
168 |
sigint(int sig)
|
120 |
sigint(int sig)
|
169 |
{
|
121 |
{
|
|
... |
|
... |
172 |
|
124 |
|
173 |
fprintf(stderr, "Absolute interval: ");
|
125 |
fprintf(stderr, "Absolute interval: ");
|
174 |
showsecs(achrono.millis());
|
126 |
showsecs(achrono.millis());
|
175 |
fprintf(stderr, ". Relative interval: ");
|
127 |
fprintf(stderr, ". Relative interval: ");
|
176 |
showsecs(rchrono.restart());
|
128 |
showsecs(rchrono.restart());
|
177 |
cerr << " Abs micros: " << rchrono.amicros() <<
|
|
|
178 |
" Relabs micros: " << rchrono.amicros() - 1430477861905884LL
|
|
|
179 |
<< endl;
|
|
|
180 |
fprintf(stderr, ".\n");
|
129 |
fprintf(stderr, ".\n");
|
181 |
if (sig == SIGQUIT) {
|
130 |
if (sig == SIGQUIT) {
|
182 |
exit(0);
|
131 |
exit(0);
|
183 |
}
|
132 |
}
|
184 |
}
|
133 |
}
|
185 |
|
134 |
|
186 |
int main(int argc, char **argv)
|
135 |
int main(int argc, char **argv)
|
187 |
{
|
136 |
{
|
188 |
|
|
|
189 |
thisprog = argv[0];
|
137 |
thisprog = argv[0];
|
190 |
argc--;
|
138 |
argc--; argv++;
|
191 |
argv++;
|
|
|
192 |
|
|
|
193 |
if (argc != 0) {
|
139 |
if (argc != 0) {
|
194 |
Usage();
|
140 |
Usage();
|
195 |
}
|
141 |
}
|
196 |
|
142 |
sleep(1);
|
197 |
for (int i = 0; i < 50000000; i++);
|
|
|
198 |
fprintf(stderr, "Start secs: %.2f\n", achrono.secs());
|
143 |
fprintf(stderr, "Initial micros: %ld\n", achrono.micros());;
|
199 |
|
|
|
200 |
|
|
|
201 |
fprintf(stderr, "Type ^C for intermediate result, ^\\ to stop\n");
|
144 |
fprintf(stderr, "Type ^C for intermediate result, ^\\ to stop\n");
|
202 |
signal(SIGINT, sigint);
|
145 |
signal(SIGINT, sigint);
|
203 |
signal(SIGQUIT, sigint);
|
146 |
signal(SIGQUIT, sigint);
|
204 |
achrono.restart();
|
147 |
achrono.restart();
|
205 |
rchrono.restart();
|
148 |
rchrono.restart();
|