-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhooks.cc
More file actions
399 lines (348 loc) · 12.1 KB
/
hooks.cc
File metadata and controls
399 lines (348 loc) · 12.1 KB
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
#include "hooks.h"
#include <chrono>
#include <vector>
#include <valarray>
#include <iostream>
#include <fstream>
#include "json.hpp"
#if defined(_OPENMP)
#include <omp.h>
#endif
#if defined(USE_MPI)
#include <mpi.h>
#endif
#if defined(ENABLE_SNIPER_HOOKS)
#include <hooks_base.h>
#elif defined(ENABLE_GEM5_HOOKS)
#include <util/m5/m5op.h>
#elif defined(ENABLE_PIN_HOOKS)
// Nothing to include here
#elif defined(ENABLE_PERF_HOOKS)
#include <perf.h>
#endif
using json = nlohmann::json;
using std::cerr;
using std::string;
using std::vector;
class Hooks::impl
{
friend class Hooks;
// Stream for writing out json results
std::ofstream out;
// Name of the region we are currently in, or "" outside of a region
string region_name;
// Start and end times of the last region
std::chrono::time_point<std::chrono::steady_clock> t1, t2;
// Number of edges traversed during the current region (per thread)
vector<int64_t> num_traversed_edges;
// Dict of custom attributes that should be printed after every region_end
json attrs;
// Dict of custom results that should be printed after the next region_end
json stats;
#if defined(ENABLE_PERF_HOOKS)
// Names of perf events to collect this run
vector<string> perf_event_names;
// Number of perf events to collect each trial
int perf_group_size;
gBenchPerf_event perf_events;
gBenchPerf_multi perf;
int trial;
#endif
#if defined(_OPENMP)
static int get_num_threads() { return omp_get_max_threads(); }
static int get_thread_id() { return omp_get_thread_num(); }
#else
static int get_num_threads() { return 1; }
static int get_thread_id() { return 0; }
#endif
static string
get_output_filename()
{
if (const char* filename = getenv("HOOKS_FILENAME"))
{
return string(filename);
} else {
cerr << "WARNING: HOOKS_FILENAME unset, defaulting to stdout.\n";
return "/dev/stdout";
}
}
#if defined(ENABLE_PERF_HOOKS)
static vector<string>
get_perf_event_names()
{
vector<string> event_names = {"", "--perf-event"};
if (const char* env_names = getenv("PERF_EVENT_NAMES"))
{
char * names = new char[strlen(env_names) + 1];
strcpy(names, env_names);
char * p = strtok(names, " ");
while (p)
{
event_names.push_back(string(p));
p = strtok(NULL, " ");
}
delete[] names;
} else {
cerr << "WARNING: No perf events found in environment; set PERF_EVENT_NAMES.\n";
}
return event_names;
}
static int
get_perf_group_size()
{
if (const char* env_group_size = getenv("PERF_GROUP_SIZE"))
{
return atoi(env_group_size);
} else {
cerr << "WARNING: Perf group size unspecified, defaulting to 4\n";
return 4;
}
}
#endif
impl()
: out(get_output_filename(), std::ofstream::app)
, region_name("")
, num_traversed_edges(get_num_threads())
#if defined(ENABLE_PERF_HOOKS)
, perf_event_names(get_perf_event_names())
, perf_group_size(get_perf_group_size())
, perf_events(perf_event_names, false)
, perf(get_num_threads(), perf_events)
#endif
{
}
void __attribute__ ((noinline))
region_begin(string name)
{
// Check for mismatched begin/end pairs
if (region_name != "") {
cerr << "ERROR: called region_begin inside region\n";
exit(-1);
} else if (name == "") {
cerr << "ERROR: region name cannot be empty\n";
exit(-1);
} else {
region_name = name;
}
// Start the ROI
#if defined(ENABLE_SNIPER_HOOKS)
parmacs_roi_begin();
#elif defined(ENABLE_GEM5_HOOKS)
m5_reset_stats(0,0);
#elif defined(ENABLE_PIN_HOOKS)
__asm__("");
#elif defined(ENABLE_PERF_HOOKS)
// We can only collect perf_group_size events at a time
// Collecting more events is done via multiple trials
trial = attrs.find("trial") != attrs.end() ? attrs["trial"].get<int>() : 0;
// After all event groups have been collected, start over with the first one
int trial_max = (perf_event_names.size() + perf_group_size - 1) / perf_group_size;
trial = trial % trial_max;
#pragma omp parallel
{
int tid = get_thread_id();
num_traversed_edges[tid] = 0;
perf.open(tid, trial, perf_group_size);
perf.start(tid, trial, perf_group_size);
}
#endif
// Start the timer
t1 = std::chrono::steady_clock::now();
}
void __attribute__ ((noinline))
region_end()
{
// Stop the timer
t2 = std::chrono::steady_clock::now();
// End the ROI
#if defined(ENABLE_SNIPER_HOOKS)
parmacs_roi_end();
#elif defined(ENABLE_GEM5_HOOKS)
m5_dumpreset_stats(0,0);
#elif defined(ENABLE_PIN_HOOKS)
__asm__("");
#elif defined(ENABLE_PERF_HOOKS)
#pragma omp parallel
{
int tid = get_thread_id();
perf.stop(tid, trial, perf_group_size);
}
#endif
// Populate the results object
json results = attrs;
// Check for mismatched begin/end pairs
if (region_name == "") {
cerr << "ERROR: called region_end before region_begin\n";
exit(-1);
} else {
// Set region name in output and reset for next region
results["region_name"] = region_name;
region_name = "";
}
// Save # of traversed edges if the function was used
int64_t total_edges_traversed = 0;
for (int64_t n : num_traversed_edges) {
total_edges_traversed += n;
}
if (total_edges_traversed > 0) {
results["num_traversed_edges"] = num_traversed_edges;
}
// Record time elapsed
results["time_ms"] = std::chrono::duration<double, std::milli>(t2-t1).count();
// Copy stats to the results object
for (json::iterator it = stats.begin(); it != stats.end(); ++it){
results[it.key()] = it.value();
}
// Stats get cleared at the end of each region, attrs do not.
stats.clear();
#if defined(ENABLE_PERF_HOOKS)
// Copy recorded counters into the output
json counters = json::parse(perf.toString(trial, perf_group_size));
for (json::iterator it = counters.begin(); it != counters.end(); ++it) {
results[it.key()] = it.value();
}
#endif
#if defined(USE_MPI)
// Combine results from each rank so only rank 0 prints to stdout
int rank, comm_size;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &comm_size);
// Serialize results to string
string local_results_string = results.dump();
// Get string lengths from each process
int32_t local_string_length = local_results_string.size();
vector<int32_t> string_lengths(comm_size);
MPI_Gather(
&local_string_length, 1, MPI_INT64_T,
string_lengths.data(), 1, MPI_INT64_T,
0, MPI_COMM_WORLD
);
// Allocate storage for final string
int total_string_length = std::accumulate(string_lengths.begin(), string_lengths.end(), 0);
vector<char> global_results_vec(total_string_length);
// Figure out where to put each incoming string
std::valarray<int32_t> displacements(comm_size + 1);
std::partial_sum(string_lengths.begin(), string_lengths.end(), begin(displacements));
// Prefix sum gave us the end of each string, but we need the beginnings, so shift the whole list to the right
// We have one extra spot at the end of the string, so we aren't losing anything
displacements.shift(-1);
// Get strings from each process
MPI_Gatherv(
// Some old versions of mpi.h aren't const-correct
const_cast<char*>(local_results_string.c_str()), local_results_string.size(), MPI_CHAR,
global_results_vec.data(), string_lengths.data(), &displacements[0], MPI_CHAR,
0, MPI_COMM_WORLD
);
if (rank == 0)
{
// Parse out the results string from each process
vector<json> results_by_pid(comm_size);
for (int i = 0; i < comm_size; ++i)
{
// Find the string position in the data
char * s = global_results_vec.data() + displacements[i];
size_t len = displacements[i + 1] - displacements[i];
// Parse back into json object
results_by_pid[i] = json::parse(string(s,len));
}
// For each top-level key, build an array of values from each pid for that key
for (json::iterator it = results.begin(); it != results.end(); ++it)
{
string key = it.key();
results[key] = json::array();
for (int i = 1; i < comm_size; ++i)
{
results[key] += results_by_pid[i][key];
}
}
}
#endif
#if defined(HOOKS_PRETTY_PRINT)
// setw is overloaded to format json with indents
int indent = 2;
#else
int indent = 0;
#endif
#if defined(USE_MPI)
if (rank == 0){
#endif
// At this point we've accumulated all the data for this ROI into a json object (results)
// Finally, send it to the output stream
out << std::setw(indent) << results << std::endl;
#if defined(USE_MPI)
}
#endif
}
void traverse_edges(int64_t n) {
num_traversed_edges[get_thread_id()] += n;
}
template<typename T>
void
set_attr(std::string key, T value) {
attrs[key] = value;
}
template<typename T>
void
set_stat(std::string key, T value) {
stats[key] = value;
}
};
// Implementation of Hooks
// This is just a Singleton that forwards all calls to the private Hooks::implementation (impl)
// This minimizes the number of code changes that need to be made in calling code
Hooks&
Hooks::getInstance()
{
static Hooks instance;
return instance;
}
Hooks::Hooks() { pimpl = new Hooks::impl(); }
Hooks::~Hooks() { delete pimpl; }
void Hooks::region_begin(string name) { pimpl->region_begin(name); }
void Hooks::region_end() { pimpl->region_end(); }
void Hooks::set_attr(std::string key, uint64_t value) { pimpl->set_attr(key, value); }
void Hooks::set_attr(std::string key, int64_t value) { pimpl->set_attr(key, value); }
void Hooks::set_attr(std::string key, double value) { pimpl->set_attr(key, value); }
void Hooks::set_attr(std::string key, std::string value) { pimpl->set_attr(key, value); }
void Hooks::set_stat(std::string key, uint64_t value) { pimpl->set_stat(key, value); }
void Hooks::set_stat(std::string key, int64_t value) { pimpl->set_stat(key, value); }
void Hooks::set_stat(std::string key, double value) { pimpl->set_stat(key, value); }
void Hooks::set_stat(std::string key, std::string value) { pimpl->set_stat(key, value); }
void Hooks::traverse_edges(uint64_t n) { pimpl->traverse_edges(n); }
// Implementation of C interface
//
extern "C" void
hooks_region_begin(const char* name)
{
Hooks::getInstance().region_begin(name);
}
extern "C" void
hooks_region_end()
{
Hooks::getInstance().region_end();
}
extern "C" void
hooks_set_attr_i64(const char * key, int64_t value)
{
Hooks::getInstance().set_attr(key, value);
}
extern "C" void
hooks_set_attr_u64(const char * key, uint64_t value)
{
Hooks::getInstance().set_attr(key, value);
}
extern "C" void
hooks_set_attr_f64(const char * key, double value)
{
Hooks::getInstance().set_attr(key, value);
}
extern "C" void
hooks_set_attr_str(const char * key, const char* value)
{
Hooks::getInstance().set_attr(key, value);
}
extern "C" void
hooks_traverse_edges(uint64_t n)
{
Hooks::getInstance().traverse_edges(n);
}