-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathperf.cc
More file actions
299 lines (278 loc) · 8.64 KB
/
Copy pathperf.cc
File metadata and controls
299 lines (278 loc) · 8.64 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
/*
Written by Reini Urban, 2022
*/
#include <cstddef>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <sys/stat.h>
#include "perf.h"
#include <vector>
#include <string>
#include <cstring>
#include <climits>
#include <algorithm>
using namespace std;
vector<string> options = {
"-a chm -p",
"-a chm3 -p",
"-a bdz -p",
"-a chm -h wyhash -p",
"-a chm3 -h wyhash -p",
"-a bdz -h wyhash -p",
"-a chm -h fnv -p",
"-a chm3 -h fnv -p",
"-a bpz -h fnv -p",
"-a chm -h crc -p",
"-a chm3 -h crc -p",
"-a bpz -h crc -p",
"-I -p",
"-I -a chm3 -p",
"-I -a bdz -p",
};
const uint32_t sizes[] = { 200, 400, 800, 2000, 4000, 8000, 20000, 100000,
500000, 2000000 };
#ifndef WORDS
#define WORDS "/usr/share/dict/words"
#endif
// in seconds
#define NBPERF_TIMEOUT 15
#define PERF_PRE "_perf_"
static char perf_exe[32] = PERF_PRE "2000000";
static char perf_in[40] = PERF_PRE "2000000.nbperf";
static char perf_c[40] = PERF_PRE "2000000.c";
static char cflags[256] = "-O2";
#define PICK(n) ((unsigned)rand()) % (n)
char buf[128];
static size_t random_word(char *buf, const size_t buflen) {
const char* const alpha =
"abcdefeghijklmnopqrstuvwxyz0123456789_ABCDEFEGHIJKLMNOPQRSTUVWXYZ";
size_t len = 2 + PICK(buflen / 2);
const size_t l = len;
while(len--)
*buf++ = alpha[PICK(sizeof(alpha)-1)];
*buf = '\0';
return l;
}
static int random_int(char *buf, const size_t buflen) {
snprintf(buf, buflen, "%d", rand());
return 1;
}
static inline void set_names (const char *alg, const char *hash,
const size_t size, const bool isword)
{
const char *w = isword ? "": "int";
snprintf(perf_exe, sizeof perf_exe, "%s%s%s_%s%zu", PERF_PRE, w, alg, hash, size);
snprintf(perf_in, sizeof perf_in, "%s%s", perf_exe, ".nbperf");
snprintf(perf_c, sizeof perf_c, "%s%s", perf_exe, ".c");
}
static inline void cleanup_files (void) {
unlink(perf_in);
unlink(perf_c);
unlink(perf_exe);
}
static inline int create_set (const char *alg, const string hash,
const size_t size, const bool isword) {
char cmd[160];
static unsigned lines = 0;
int ret = 0;
set_names (alg, hash.c_str(), size, isword);
printf("Creating %s\n", perf_exe);
if (!isword) {
FILE *f = fopen("words.tmp","w");
for (unsigned i=0; i<size; i++)
{
random_int(buf, 128);
fprintf(f, "%s\n", buf);
}
fclose(f);
snprintf(cmd, sizeof cmd, "sort -n <words.tmp | uniq >%s", perf_in);
ret = system(cmd);
unlink("words.tmp");
return ret;
}
if (!lines)
{
ret = system("wc -l " WORDS " >words.wc");
FILE *f = fopen("words.wc","r");
ret = fscanf(f, "%u", &lines);
unlink("words.wc");
}
if (lines < size)
{
FILE *f = fopen("words.tmp","w");
for (unsigned i=0; i<size; i++)
{
random_word(buf, 128);
fprintf(f, "%s\n", buf);
}
fclose(f);
snprintf(cmd, sizeof cmd, "sort <words.tmp | uniq >%s", perf_in);
ret = system(cmd);
unlink("words.tmp");
}
else
{
snprintf(cmd, sizeof cmd, "head -n %zu " WORDS
" | grep -v \",\" | sort | uniq >%s", size + 2, perf_in);
ret = system(cmd);
}
return ret;
}
static inline int run_nbperf (FILE* f, const uint32_t size, const char *cmd) {
uint64_t t = timer_start();
int ret = system(cmd);
t = timer_end () - t;
fprintf(f, "%20u %20lu\n", size, t / PERF_LOOP);
return ret;
}
static inline int compile_result (bool needs_mi_vector, const char *defines) {
char cmd[256];
snprintf(cmd, sizeof cmd, "cc %s -I. %s %s perf_test.c %s -o %s",
cflags, defines, perf_c, needs_mi_vector ? "mi_vector_hash.c" : "", perf_exe);
printf("%s\n", cmd);
return system(cmd);
}
static inline int run_result (const char *log, const uint32_t size) {
char cmd[256];
snprintf(cmd, sizeof cmd, "./%s %s %s %u", perf_exe, perf_in,
log, size);
printf("%s\n", cmd);
return system(cmd);
}
// measure creation-time (only nbperf, not cc), and run-time.
// also measure compiled-size.
// use sample sizes from 20, 200, 2k, 20k, 200k, 2m for all variants.
int main (int argc, char **argv)
{
char logcomp[36] = "nbperf.log";
char logrun[36] = "run.log";
char logsize[36] = "size.log";
srand(0xbeef);
if (getenv("CFLAGS"))
strcpy (cflags, getenv("CFLAGS"));
if (argc > 1) {
if (strcmp(argv[1], "--c") == 0) {
options = { "-p -a chm", "-pM", "-p -c -2",
"-p -a chm3", "-pM -a chm3", "-p -a chm3 -c -2",
"-p -a bdz", "-pM -a bdz", "-p -a bdz -c -2" };
strncpy(logcomp, "nbperf-c-2.log", sizeof logcomp);
strncpy(logrun, "run-c-2.log", sizeof logrun);
strncpy(logsize, "size-c-2.log", sizeof logsize);
} else {
options.clear();
options.push_back("");
for (int i = 1; i < argc; i++) {
if (i > 1)
options[0] += " ";
options[0] += argv[i];
}
string str = options[0];
auto end_pos = remove(str.begin(), str.end(), ' ');
str.erase(end_pos, str.end());
snprintf(logcomp, sizeof logcomp, "nbperf%s.log", str.c_str());
snprintf(logrun, sizeof logrun, "run%s.log", str.c_str());
snprintf(logsize, sizeof logsize, "size%s.log", str.c_str());
}
}
FILE *comp = fopen(logcomp, "w");
FILE *run = fopen(logrun, "w");
FILE *fsize = fopen(logsize, "w");
for (auto option : options) {
const bool isword = option.find("-I") == string::npos;
const bool is_bdz = option.find("-a bdz") != string::npos;
const bool is_chm3 = option.find("-a chm3") != string::npos;
const bool is_chm = !is_bdz && !is_chm3;
const char *alg = is_chm ? "chm"
: is_chm3 ? "chm3"
: is_bdz ? "bdz"
: NULL;
string hash;
size_t hpos = option.find("-h ");
if (hpos != string::npos) {
string rest = option.substr(hpos + 3);
size_t ppos = rest.find(" ");
if (ppos != string::npos)
hash = rest.substr(0, ppos);
else
hash = string(rest);
}
printf("------------------------ %s ------------------------\n", option.c_str());
fprintf(comp, "option: %s\n", option.c_str());
fprintf(run, "option: %s\n", option.c_str());
//fclose(comp);
fclose(run);
fprintf(fsize, "option: %s\n", option.c_str());
for(unsigned i=0; i<(sizeof sizes)/(sizeof *sizes); i++)
{
char cmd[160];
int ret = 0;
const uint32_t size = sizes[i];
ret = create_set (alg, hash, sizes[i], isword);
if (ret != 0)
continue;
const bool needs_mi_vector = option.find("-h ") == string::npos;
//const bool is_fnv = option.find("-h fnv") != string::npos;
//if (size == 20000 && is_fnv)
// option += " -f";
if (is_bdz)
snprintf(cmd, sizeof cmd,
"timeout %d ./nbperf %s -m %s.map -o %s %s",
NBPERF_TIMEOUT, option.c_str(), perf_in, perf_c, perf_in);
else
snprintf(cmd, sizeof cmd,
"timeout %d ./nbperf %s -o %s %s",
NBPERF_TIMEOUT, option.c_str(), perf_c, perf_in);
printf("size %u: %s\n", size, cmd);
//#ifndef DUMMY
ret = run_nbperf (comp, size, cmd);
//#endif
printf(" nbperf: %20u => %d\n", size, ret);
if (ret != 0)
continue;
#ifndef DUMMY
string defines = "";
if (!isword)
defines += "-D_INTKEYS ";
if (is_bdz && isword)
defines += "-Dbdz ";
#if defined __amd64__ || defined __x86_64__
if (hash == "crc")
defines += "-march=native";
#endif
ret = compile_result (needs_mi_vector, defines.c_str());
printf(" => %d\n", ret);
#else
ret = 0;
#endif
if (ret == 0) {
struct stat st;
stat (perf_exe, &st);
printf(" %s (%lu):\n", perf_exe, st.st_size);
fprintf(fsize, "%20u %20lu\n", size, st.st_size);
#ifndef DUMMY
run_result (logrun, size);
#endif
}
}
run = fopen(logrun, "a");
//comp = fopen("nbperf.log", "a");
}
#if 1
for(unsigned i=0; i<(sizeof sizes)/(sizeof *sizes); i++)
{
const uint32_t size = sizes[i];
for (auto alg: {"chm", "chm3", "bdz"}) {
for (auto hash: {"", "wyhash", "fnv", "crc"}) {
set_names (alg, hash, size, true);
cleanup_files ();
}
set_names (alg, "", size, false);
cleanup_files ();
}
}
#endif
fclose(comp);
fclose(run);
fclose(fsize);
}