-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathwyinstr.cpp
167 lines (141 loc) · 4.71 KB
/
wyinstr.cpp
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
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <map>
#include <memory>
#include <mutex>
#include <stack>
using callsite_id = std::pair<const char *, int64_t>;
size_t hash_c_string(const char *p, size_t s) {
size_t result = 0;
const size_t prime = 31;
for (size_t i = 0; i < s; ++i) {
result = p[i] + (result * prime);
}
return result;
}
struct prof_report {
prof_report(int8_t num_args) : _num_calls(1), _num_args(num_args) {
_unique_arg_evals = (int64_t *)calloc(num_args, sizeof(int64_t));
_arg_evals = (int64_t *)calloc(num_args, sizeof(int64_t));
}
// ~prof_report() {
// free(_unique_arg_evals);
// free(_arg_evals);
// }
int64_t _num_calls;
int8_t _num_args;
int64_t *_unique_arg_evals;
int64_t *_arg_evals;
};
static bool initialized = false;
static std::map<callsite_id, std::unique_ptr<struct prof_report>> profile_info;
static std::stack<callsite_id> call_stack;
std::recursive_mutex wyinstr_mutex;
static char const *first_fun_name = "__wyinstr_pre_main";
extern "C" void __attribute__((noinline)) _wyinstr_init_prof() {
std::lock_guard<std::recursive_mutex> lock(wyinstr_mutex);
initialized = true;
const callsite_id first = std::make_pair(first_fun_name, 0);
profile_info.insert(
std::make_pair(first, std::make_unique<struct prof_report>(2)));
call_stack.push(first);
#ifdef DEBUG
fprintf(stderr, "Initialized profiling! Top of stack: <%s, %li>\n",
call_stack.top().first, call_stack.top().second);
#endif
}
extern "C" void __attribute__((noinline))
_wyinstr_init_call(const char *fun_name, int64_t callinstr_id,
int8_t num_args) {
std::lock_guard<std::recursive_mutex> lock(wyinstr_mutex);
if (!initialized) {
return;
}
#ifdef DEBUG
fprintf(stderr, "Adding call from callsite <%s,%li> with %d arguments!\n",
fun_name, callinstr_id, num_args);
#endif
const callsite_id id = std::make_pair(fun_name, callinstr_id);
// first time callsite is reached, instantiate new profile report
if (!profile_info.count(id)) {
#ifdef DEBUG
fprintf(stderr,
"First call on this callsite!. Inserting profile report... Profile "
"map size: %li\n",
profile_info.size());
#endif
profile_info.insert(
std::make_pair(id, std::make_unique<struct prof_report>(num_args)));
}
struct prof_report *report = profile_info[id].get();
report->_num_calls += 1;
call_stack.push(id);
#ifdef DEBUG
fprintf(stderr, "New top of stack: <%s, %li>\n", fun_name, callinstr_id);
#endif
}
extern "C" __attribute__((noinline)) void _wyinstr_mark_eval(int8_t arg_index,
int64_t *bits) {
std::lock_guard<std::recursive_mutex> lock(wyinstr_mutex);
if (!initialized) {
return;
}
#ifdef DEBUG
fprintf(stderr, "Logging eval of arg: %d\n", arg_index);
#endif
callsite_id id = call_stack.top();
struct prof_report *report = profile_info[id].get();
// first arg eval in this call, increment unique counter
if ((*bits & (1 << arg_index)) == 0) {
*bits = *bits | (1 << arg_index);
report->_unique_arg_evals[arg_index] += 1;
}
// increment total eval counter
report->_arg_evals[arg_index] += 1;
#ifdef DEBUG
fprintf(stderr, "Total arg evals: %li\n", report->_arg_evals[arg_index]);
#endif
}
extern "C" __attribute__((noinline)) int64_t _wyinstr_initbits() {
return static_cast<int64_t>(0);
}
extern "C" __attribute__((noinline)) void _wyinstr_end_call() {
std::lock_guard<std::recursive_mutex> lock(wyinstr_mutex);
if (!initialized) {
return;
}
#ifdef DEBUG
fprintf(
stderr,
"Ending call from callsite <%s, %li>. Stack size before popping: %li\n",
call_stack.top().first, call_stack.top().second, call_stack.size());
#endif
call_stack.pop();
#ifdef DEBUG
if (!call_stack.empty()) {
fprintf(stderr, "New top: <%s, %li>.\n", call_stack.top().first,
call_stack.top().second);
}
#endif
}
extern "C" __attribute__((noinline)) void _wyinstr_dump(const char *mod_name) {
std::lock_guard<std::recursive_mutex> lock(wyinstr_mutex);
std::string filename = std::string(mod_name) + ".csv";
FILE *outfile = fopen(filename.c_str(), "w");
fprintf(outfile,
"fun_name,call_id,total_calls,num_args,unique_evals,total_evals\n");
for (auto &[key, value] : profile_info) {
fprintf(outfile, "%s,%li,%li,%d,", key.first, key.second, value->_num_calls,
value->_num_args);
for (int8_t i = 0; i < value->_num_args; ++i) {
fprintf(outfile, "%li,", value->_unique_arg_evals[i]);
}
for (int8_t i = 0; i < value->_num_args; ++i) {
fprintf(outfile, "%li,", value->_arg_evals[i]);
}
fprintf(outfile, "\n");
fflush(outfile);
}
fclose(outfile);
}