Debian Codegraph @

debian/tests/test_modules/llparse-test-fixture/src/native/fixture.c

raw symbols SCIP
1 #include <assert.h>
2 #include <inttypes.h>
3 #include <stdarg.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7
8 #include "fixture.h"
9
10 #if defined(_MSC_VER)
11 #if defined(_MSC_EXTENSIONS)
12 #define DELTA_EPOCH_IN_MICROSECS 11644473600000000Ui64
13 #else
14 #define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL
15 #endif
16
17 #include <windows.h>
18
19 static int gettimeofday(struct timeval* tv, void* tz) {
20 assert(tv != NULL);
21 assert(tz == NULL);
22
23 FILETIME ft;
24 unsigned __int64 tmpres = 0;
25
26 GetSystemTimeAsFileTime(&ft);
27
28 tmpres |= ft.dwHighDateTime;
29 tmpres <<= 32;
30 tmpres |= ft.dwLowDateTime;
31
32 tmpres /= 10; /*convert into microseconds*/
33 /*converting file time to unix epoch*/
34 tmpres -= DELTA_EPOCH_IN_MICROSECS;
35 tv->tv_sec = (long)(tmpres / 1000000UL);
36 tv->tv_usec = (long)(tmpres % 1000000UL);
37
38 return 0;
39 }
40 #else
41 #include <sys/time.h>
42 #endif /* defined(_MSC_VER) */
43
44 #ifdef LLPARSE__TEST_INIT
45 void LLPARSE__TEST_INIT(llparse_t* p);
46 #endif /* LLPARSE__TEST_INIT */
47
48 #ifdef LLPARSE__TEST_FINISH
49 void LLPARSE__TEST_FINISH(llparse_t* p);
50 #endif /* LLPARSE__TEST_FINISH */
51
52 /* NOTE: include of parser is inserted through `-include` clang argument */
53
54 /* 8 gb */
55 static const int64_t kBytes = 8LL << 30;
56
57 int llparse__in_bench = 0;
58 static int llparse__in_loop;
59 static const char* start;
60
61 void llparse__print(const char* p, const char* endp,
62 const char* fmt, ...) {
63 va_list ap;
64 char buf[16384];
65 int len;
66
67 if (llparse__in_bench)
68 return;
69
70 va_start(ap, fmt);
71 len = vsnprintf(buf, sizeof(buf), fmt, ap);
72 va_end(ap);
73
74 if (p == NULL) {
75 if (len == 0)
76 fprintf(stdout, "off=NULL\n");
77 else
78 fprintf(stdout, "off=NULL %s\n", buf);
79 } else {
80 if (len == 0)
81 fprintf(stdout, "off=%d\n", (int) (p - start));
82 else
83 fprintf(stdout, "off=%d %s\n", (int) (p - start), buf);
84 }
85 return;
86 }
87
88
89 int llparse__print_span(const char* name, const char* p, const char* endp) {
90 if (llparse__in_bench)
91 return 0;
92
93 for (;;) {
94 const char* cr;
95 const char* lf;
96 int len;
97
98 cr = memchr(p, '\r', endp - p);
99 lf = memchr(p, '\n', endp - p);
100 if (cr != NULL && lf != NULL) {
101 if (cr < lf) {
102 lf = NULL;
103 } else {
104 cr = NULL;
105 }
106 }
107
108 if (lf != NULL) {
109 len = (int) (lf - p);
110 } else if (cr != NULL) {
111 len = (int) (cr - p);
112 } else {
113 len = (int) (endp - p);
114 }
115 if (len != 0 || (lf == NULL && cr == NULL)) {
116 llparse__print(p, endp, "len=%d span[%s]=\"%.*s\"",
117 len, name, len, p);
118 p += len;
119 }
120
121 if (lf != NULL) {
122 llparse__print(p, endp, "len=1 span[%s]=lf", name);
123 assert(p != endp);
124 p++;
125 } else if (cr != NULL) {
126 llparse__print(p, endp, "len=1 span[%s]=cr", name);
127 assert(p != endp);
128 p++;
129 }
130
131 if (p == endp)
132 break;
133 }
134 return 0;
135 }
136
137
138 void llparse__debug(llparse_t* s, const char* p, const char* endp,
139 const char* msg) {
140 if (p == endp) {
141 fprintf(stderr, "off=%-3d next=null debug=%s\n", (int) (p - start), msg);
142 } else {
143 fprintf(stderr, "off=%-3d next=%02x debug=%s\n", (int) (p - start), *p,
144 msg);
145 }
146 }
147
148
149 static int llparse__run_one(llparse_t* s, const char* input, int len) {
150 int code;
151 const char* p;
152 const char* endp;
153 unsigned int paused;
154
155 p = input;
156 endp = input + len;
157
158 paused = 0;
159 for (;;) {
160 code = llparse_execute(s, input, endp);
161
162 if (code != LLPARSE__ERROR_PAUSE)
163 break;
164
165 if (paused && input == s->error_pos) {
166 llparse__debug(s, input, endp, "Can\'t make progress after pause");
167 return -1;
168 }
169
170 llparse__print(s->error_pos, endp, "pause");
171
172 /* Resume */
173 s->error = 0;
174 input = s->error_pos;
175 paused = 1;
176 }
177
178 if (code != 0) {
179 if (code != s->error) {
180 llparse__print(s->error_pos, endp,
181 "error code mismatch got=%d expected=%d", code, s->error);
182 return -1;
183 }
184
185 llparse__print(s->error_pos, endp, "error code=%d reason=\"%s\"", code,
186 s->reason);
187 }
188
189 return code;
190 }
191
192
193 static int llparse__run_loop(const char* input, int len) {
194 llparse_t s;
195
196 llparse_init(&s);
197 #ifdef LLPARSE__TEST_INIT
198 LLPARSE__TEST_INIT(&s);
199 #endif /* LLPARSE__TEST_INIT */
200
201 for (;;) {
202 int code;
203
204 code = llparse__run_one(&s, input, len);
205 if (code != 0)
206 return code;
207 }
208
209 #ifdef LLPARSE__TEST_FINISH
210 LLPARSE__TEST_FINISH(&s);
211 #endif /* LLPARSE__TEST_FINISH */
212
213 return 0;
214 }
215
216
217 static int llparse__run_bench(const char* input, int len) {
218 llparse_t s;
219 int64_t i;
220 struct timeval start;
221 struct timeval end;
222 double bw;
223 double time;
224 double total;
225 int64_t iterations;
226
227 llparse_init(&s);
228 #ifdef LLPARSE__TEST_INIT
229 LLPARSE__TEST_INIT(&s);
230 #endif /* LLPARSE__TEST_INIT */
231
232 iterations = kBytes / (int64_t) len;
233
234 gettimeofday(&start, NULL);
235 for (i = 0; i < iterations; i++) {
236 int code;
237
238 code = llparse__run_one(&s, input, len);
239 if (code != 0)
240 return code;
241 }
242 gettimeofday(&end, NULL);
243
244 #ifdef LLPARSE__TEST_FINISH
245 LLPARSE__TEST_FINISH(&s);
246 #endif /* LLPARSE__TEST_FINISH */
247
248 time = (end.tv_sec - start.tv_sec);
249 time += (double) (end.tv_usec - start.tv_usec) * 1e-6;
250 total = (double) iterations * len;
251 bw = (double) total / time;
252
253 fprintf(stdout, "%.2f mb | %.2f mb/s | %.2f ops/sec | %.2f s\n",
254 (double) total / (1024 * 1024),
255 bw / (1024 * 1024),
256 (double) iterations / time,
257 time);
258
259 return 0;
260 }
261
262
263 static int llparse__run_scan(int scan, const char* input, int len) {
264 llparse_t s;
265 llparse_init(&s);
266 #ifdef LLPARSE__TEST_INIT
267 LLPARSE__TEST_INIT(&s);
268 #endif /* LLPARSE__TEST_INIT */
269
270 if (scan <= 0) {
271 fprintf(stderr, "Invalid scan value\n");
272 return -1;
273 }
274
275 while (len > 0) {
276 int max;
277 int code;
278
279 max = len > scan ? scan : len;
280
281 code = llparse__run_one(&s, input, max);
282
283 /* Continue with next scan */
284 if (code != 0)
285 return 0;
286
287 input += max;
288 len -= max;
289 }
290
291 #ifdef LLPARSE__TEST_FINISH
292 LLPARSE__TEST_FINISH(&s);
293 #endif /* LLPARSE__TEST_FINISH */
294
295 return 0;
296 }
297
298
299 static int llparse__run_stdin() {
300 llparse_t s;
301
302 llparse_init(&s);
303 #ifdef LLPARSE__TEST_INIT
304 LLPARSE__TEST_INIT(&s);
305 #endif /* LLPARSE__TEST_INIT */
306
307 for (;;) {
308 char buf[16384];
309 const char* input;
310 int code;
311
312 input = fgets(buf, sizeof(buf), stdin);
313 if (input == NULL)
314 break;
315
316 start = input;
317 code = llparse__run_one(&s, input, strlen(input));
318 if (code != 0) {
319 fprintf(stderr, "code=%d error=%d reason=%s\n", code, s.error, s.reason);
320 return -1;
321 }
322 }
323
324 #ifdef LLPARSE__TEST_FINISH
325 LLPARSE__TEST_FINISH(&s);
326 #endif /* LLPARSE__TEST_FINISH */
327
328 return 0;
329 }
330
331
332 static int llparse__print_usage(char** argv) {
333 fprintf(stderr, "Usage:\n");
334 fprintf(stderr, " %s <from>:to [input]\n", argv[0]);
335 fprintf(stderr, " %s bench [input]\n", argv[0]);
336 fprintf(stderr, " %s -\n", argv[0]);
337 return -1;
338 }
339
340
341 int main(int argc, char** argv) {
342 const char* input;
343 int len;
344 struct {
345 int from;
346 int to;
347 } scan;
348 int i;
349
350 if (argc >= 2 && strcmp(argv[1], "-") == 0)
351 return llparse__run_stdin();
352
353 if (argc < 3)
354 return llparse__print_usage(argv);
355
356 if (strcmp(argv[1], "bench") == 0) {
357 llparse__in_bench = 1;
358 } else if (strcmp(argv[1], "loop") == 0) {
359 llparse__in_bench = 1;
360 llparse__in_loop = 1;
361 } else {
362 const char* colon;
363 char* endptr;
364
365 colon = strchr(argv[1], ':');
366 if (colon == NULL)
367 return llparse__print_usage(argv);
368
369 scan.from = (int) strtol(argv[1], &endptr, 10);
370 if (endptr != colon)
371 return llparse__print_usage(argv);
372
373 scan.to = (int) strtol(colon + 1, &endptr, 10);
374 if (endptr != argv[1] + strlen(argv[1]))
375 return llparse__print_usage(argv);
376 }
377
378 input = argv[2];
379 len = strlen(input);
380
381 if (llparse__in_bench && len == 0) {
382 fprintf(stderr, "Input can\'t be empty for benchmark");
383 return -1;
384 }
385
386 start = input;
387
388 if (llparse__in_loop)
389 return llparse__run_loop(input, len);
390
391 if (llparse__in_bench)
392 return llparse__run_bench(input, len);
393
394 for (i = scan.from; i < scan.to; i++) {
395 int err;
396
397 fprintf(stdout, "===== SCAN %d START =====\n", i);
398 err = llparse__run_scan(i, input, len);
399 if (err != 0)
400 return err;
401 }
402
403 return 0;
404 }