-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathutils.h
251 lines (216 loc) · 7.42 KB
/
utils.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#ifndef UTILS_H
#define UTILS_H 1
#include "headers.h"
#include "stats.h"
#define PAGE_SIZE (4LU*1024LU)
#define ONE_GB (1024*1024*1024)
#define die(msg, args...) \
do { \
fprintf(stderr,"(%s,%d) " msg "\n", __FUNCTION__ , __LINE__, ##args); \
exit(-1); \
} while(0)
#define perr(msg, args...) \
do { \
perror("Error: "); \
fprintf(stderr,"(%s,%d) " msg "\n", __FUNCTION__ , __LINE__, ##args); \
exit(-1); \
} while(0)
#ifdef __x86_64__
#define rdtscll(val) { \
unsigned int __a,__d; \
asm volatile("rdtsc" : "=a" (__a), "=d" (__d)); \
(val) = ((unsigned long)__a) | (((unsigned long)__d)<<32); \
}
#else
#define rdtscll(val) __asm__ __volatile__("rdtsc" : "=A" (val))
#endif
#define NOP10() asm("nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;")
#define maybe_unused __attribute__((unused))
#define STRINGIZE_(x) #x
#define STRINGIZE(x) STRINGIZE_(x)
/*
* Cute timer macros
* Usage:
* declare_timer;
* start_timer {
* ...
* } stop_timer("Took %lu us", elapsed);
*/
#define declare_timer uint64_t elapsed; \
struct timeval st, et;
#define start_timer gettimeofday(&st,NULL);
#define stop_timer(msg, args...) ;do { \
gettimeofday(&et,NULL); \
elapsed = ((et.tv_sec - st.tv_sec) * 1000000) + (et.tv_usec - st.tv_usec) + 1; \
printf("(%s,%d) [%6lums] " msg "\n", __FUNCTION__ , __LINE__, elapsed/1000, ##args); \
} while(0)
/*
* Cute debug timer.
* declare_debug_timer;
* start_debug_timer {
* ...
* } stop_debug_timer(1000, "WARNING, this section took more than 1ms!");
*/
#define declare_debug_timer __attribute__((unused)) uint64_t __s, __e;
#if DEBUG
#define start_debug_timer rdtscll(__s);
#define stop_debug_timer(alert_thres, msg, args...) ; do { \
rdtscll(__e); \
uint64_t __elapsed = cycles_to_us(__e - __s); \
if(__elapsed > (alert_thres)) \
printf("(%s,%d) [%6luus] " msg "\n", __FUNCTION__ , __LINE__, __elapsed, ##args); \
} while(0)
#else
#define start_debug_timer
#define stop_debug_timer(alert_thres, msg, args...)
#endif
/*
* Foreach macro
*/
#define foreach(item, array) \
for(int keep = 1, \
count = 0,\
size = sizeof (array) / sizeof *(array); \
keep && count != size; \
keep = !keep, count++) \
for(item = *((array) + count); keep; keep = !keep)
/*
* Cute way to print a message periodically in a loop.
* declare_periodic_count;
* for(...) {
* periodic_count(1000, "Hello"); // prints hello and the number of iterations every second
* }
*/
#define declare_periodic_count \
uint64_t __real_start = 0, __start, __last, __nb_count; \
if(!__real_start) { \
rdtscll(__real_start); \
__start = __real_start; \
__nb_count = 0; \
}
#define periodic_count(period, msg, args...) \
do { \
rdtscll(__last); \
__nb_count++; \
if(cycles_to_us(__last - __start) > ((period)*1000LU)) { \
printf("(%s,%d) [%3lus] [%7lu ops/s] " msg "\n", __FUNCTION__ , __LINE__, cycles_to_us(__last - __real_start)/1000000LU, __nb_count*1000000LU/cycles_to_us(__last - __start), ##args); \
__nb_count = 0; \
__start = __last; \
} \
} while(0);
/*
* Cute way to get a breakdown of where time is spent.
* declare_breakdown;
* for(...) {
* fun1(); __1
* fun2(); __2
* ...
* show_breakdown_periodic(1000, "fun1", "fun2", ...); // "fun1 X% fun2 Y%" every second
* }
*/
#define declare_breakdown \
struct __breakdown { \
uint64_t real_start; \
uint64_t start; \
uint64_t now; \
uint64_t evt1; \
uint64_t evt2; \
uint64_t evt3; \
uint64_t evt4; \
uint64_t evt5; \
uint64_t loops; \
uint64_t count; \
} __breakdown = {}; \
rdtscll(__breakdown.real_start); \
__breakdown.start = __breakdown.real_start;
#define __1 \
do { \
rdtscll(__breakdown.now); \
__breakdown.evt1 += __breakdown.now - __breakdown.start; \
__breakdown.start = __breakdown.now; \
} while(0);
#define __2 \
do { \
rdtscll(__breakdown.now); \
__breakdown.evt2 += __breakdown.now - __breakdown.start; \
__breakdown.start = __breakdown.now; \
} while(0);
#define __3 \
do { \
rdtscll(__breakdown.now); \
__breakdown.evt3 += __breakdown.now - __breakdown.start; \
__breakdown.start = __breakdown.now; \
} while(0);
#define __4 \
do { \
rdtscll(__breakdown.now); \
__breakdown.evt4 += __breakdown.now - __breakdown.start; \
__breakdown.start = __breakdown.now; \
} while(0);
#define __5 \
do { \
rdtscll(__breakdown.now); \
__breakdown.evt5 += __breakdown.now - __breakdown.start; \
__breakdown.start = __breakdown.now; \
} while(0);
#define show_breakdown_periodic(period, _count, _evt1, _evt2, _evt3, _evt4, _evt5) \
do { \
__breakdown.loops++; \
rdtscll(__breakdown.now); \
uint64_t elapsed = __breakdown.now - __breakdown.real_start; \
if(cycles_to_us(elapsed) > ((period)*1000LU)) { \
uint64_t count_diff = _count - __breakdown.count; \
__breakdown.count = _count; \
printf("[WORKER BREAKDOWN] " _evt1 "%3lu%% - " _evt2 " %3lu%% - " _evt3 " %3lu%% - " _evt4 " %3lu%% - " _evt5 " %3lu%% - %7lu ops - %7lu ops/s - %3lu ops / loop - %lu cycles/op\n", \
__breakdown.evt1*100LU/elapsed, \
__breakdown.evt2*100LU/elapsed, \
__breakdown.evt3*100LU/elapsed, \
__breakdown.evt4*100LU/elapsed, \
__breakdown.evt5*100LU/elapsed, \
count_diff, \
count_diff * period * 1000 / cycles_to_us(elapsed), \
count_diff/__breakdown.loops, \
elapsed / count_diff \
); \
__breakdown.real_start = __breakdown.now; \
__breakdown.evt1 = 0; \
__breakdown.evt2 = 0; \
__breakdown.evt3 = 0; \
__breakdown.evt4 = 0; \
__breakdown.evt5 = 0; \
__breakdown.loops = 0; \
} \
} while(0);
#endif
/*
* Cute way to measure memory usage.
*/
#define declare_memory_counter \
struct rusage __memory; \
uint64_t __previous_mem = 0, __used_mem; \
getrusage(RUSAGE_SELF, &__memory); \
__previous_mem = __memory.ru_maxrss;
#define get_memory_usage(msg, args...) \
getrusage(RUSAGE_SELF, &__memory); \
__used_mem = __memory.ru_maxrss - __previous_mem; \
__previous_mem = __memory.ru_maxrss; \
printf("(%s,%d) [%lu MB total - %lu MB since last measure] " msg "\n", __FUNCTION__ , __LINE__, __previous_mem/1024, __used_mem/1024, ##args);
/*
* Busy waiting
*/
#define wait_for(cycles) \
do { \
uint64_t _s, _e; \
rdtscll(_s); \
while(1) { \
rdtscll(_e); \
if(_e - _s >= cycles) \
break; \
} \
} while(0);
/*
* Helper functions
*/
uint64_t cycles_to_us(uint64_t cycles);
void shuffle(size_t *array, size_t n);
void pin_me_on(int core);