-
Notifications
You must be signed in to change notification settings - Fork 14
/
Stopwatch.h
200 lines (155 loc) · 5.57 KB
/
Stopwatch.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
* Stopwatch.h
*
* Created on: 29 Sep 2011
* Author: thomas
*
*/
#pragma once
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <unistd.h>
#include <string.h>
#include <sys/time.h>
#include <iostream>
#include <map>
#include <string>
#include <vector>
#define SEND_INTERVAL_MS 10000
#ifndef DISABLE_STOPWATCH
#define STOPWATCH(name, expression) \
do { \
const uint64_t startTime = Stopwatch::getInstance().getCurrentSystemTime(); \
expression const uint64_t endTime = Stopwatch::getInstance().getCurrentSystemTime(); \
Stopwatch::getInstance().addStopwatchTiming(name, endTime - startTime); \
} while (false)
#define TICK(name) \
do { \
Stopwatch::getInstance().tick(name, Stopwatch::getInstance().getCurrentSystemTime()); \
} while (false)
#define TOCK(name) \
do { \
Stopwatch::getInstance().tock(name, Stopwatch::getInstance().getCurrentSystemTime()); \
} while (false)
#else
#define STOPWATCH(name, expression) expression
#define TOCK(name) ((void)0)
#define TICK(name) ((void)0)
#endif
class Stopwatch {
public:
static Stopwatch& getInstance() {
static Stopwatch instance;
return instance;
}
void addStopwatchTiming(std::string name, uint64_t duration) {
if (duration > 0) {
timingsMs[name] = (float)(duration) / 1000.0f;
}
}
void setCustomSignature(uint64_t newSignature) {
signature = newSignature;
}
const std::map<std::string, float>& getTimings() {
return timingsMs;
}
void printAll() {
for (std::map<std::string, float>::const_iterator it = timingsMs.begin(); it != timingsMs.end();
it++) {
std::cout << it->first << ": " << it->second << "ms" << std::endl;
}
std::cout << std::endl;
}
void pulse(std::string name) {
timingsMs[name] = 1;
}
void sendAll() {
gettimeofday(&clock, 0);
if ((currentSend = (clock.tv_sec * 1000000 + clock.tv_usec)) - lastSend > SEND_INTERVAL_MS) {
int size = 0;
uint8_t* data = serialiseTimings(size);
sendto(sockfd, data, size, 0, (struct sockaddr*)&servaddr, sizeof(servaddr));
free(data);
lastSend = currentSend;
}
}
static uint64_t getCurrentSystemTime() {
timeval tv;
gettimeofday(&tv, 0);
uint64_t time = (uint64_t)(tv.tv_sec * 1000000 + tv.tv_usec);
return time;
}
void tick(std::string name, uint64_t start) {
ticksUs[name] = start;
}
void tock(std::string name, uint64_t end) {
tocksUs[name] = end;
const auto tickUs = ticksUs.find(name);
if (tickUs != ticksUs.end()) {
const float duration = (float)(end - tickUs->second) / 1000.0f;
if (duration > 0) {
timingsMs[name] = duration;
}
}
}
private:
Stopwatch() {
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
servaddr.sin_port = htons(45454);
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
gettimeofday(&clock, 0);
signature = clock.tv_sec * 1000000 + clock.tv_usec;
currentSend = lastSend = clock.tv_sec * 1000000 + clock.tv_usec;
}
virtual ~Stopwatch() {
close(sockfd);
}
uint8_t* serialiseTimings(int& packetSizeBytes) {
// Packet structure: [int32_t packetSizeBytes, uint64_t signature, then for each entry]
packetSizeBytes = sizeof(int) + sizeof(uint64_t);
// For each entry, length + 1 to include the null terminator for the strings
auto sumUpMapSizeBytes = [&](const auto& nameValueMap) {
for (const auto& [name, value] : nameValueMap) {
packetSizeBytes += sizeof(uint8_t) + (name.length() + 1) + sizeof(decltype(value));
}
};
sumUpMapSizeBytes(timingsMs);
sumUpMapSizeBytes(ticksUs);
sumUpMapSizeBytes(tocksUs);
uint8_t* dataPointer = (uint8_t*)calloc(packetSizeBytes, sizeof(uint8_t));
// First byte in the packet is the size in bytes of all data
memcpy(dataPointer, &packetSizeBytes, sizeof(int));
// Signature unique to each process
memcpy(dataPointer + sizeof(int), &signature, sizeof(uint64_t));
uint8_t* interleavedTypeNameValuePointer = dataPointer + sizeof(int) + sizeof(uint64_t);
auto serializeMap = [&](const uint8_t type, const auto& nameValueMap) {
for (const auto& [name, value] : nameValueMap) {
// Write the type of measurement
memcpy(interleavedTypeNameValuePointer++, &type, sizeof(uint8_t));
// Write the name
memcpy(interleavedTypeNameValuePointer, name.c_str(), name.length() + 1);
interleavedTypeNameValuePointer += name.length() + 1;
// Write the value
memcpy(interleavedTypeNameValuePointer, &value, sizeof(decltype(value)));
interleavedTypeNameValuePointer += sizeof(decltype(value));
}
};
serializeMap(0, timingsMs);
serializeMap(1, ticksUs);
serializeMap(2, tocksUs);
return dataPointer;
}
timeval clock;
long long int currentSend, lastSend;
uint64_t signature;
int sockfd;
struct sockaddr_in servaddr;
std::map<std::string, float> timingsMs;
std::map<std::string, uint64_t> tocksUs;
std::map<std::string, uint64_t> ticksUs;
};