1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 /* Copyright (c) 2022 Meta Platforms, Inc. and affiliates. */
3 #define _GNU_SOURCE
4 #include <argp.h>
5 #include <libgen.h>
6 #include <string.h>
7 #include <stdlib.h>
8 #include <sched.h>
9 #include <pthread.h>
10 #include <dirent.h>
11 #include <signal.h>
12 #include <fcntl.h>
13 #include <unistd.h>
14 #include <sys/time.h>
15 #include <sys/sysinfo.h>
16 #include <sys/stat.h>
17 #include <bpf/libbpf.h>
18 #include <bpf/btf.h>
19 #include <libelf.h>
20 #include <gelf.h>
21 #include <float.h>
22 #include <math.h>
23
24 #ifndef ARRAY_SIZE
25 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
26 #endif
27
28 enum stat_id {
29 VERDICT,
30 DURATION,
31 TOTAL_INSNS,
32 TOTAL_STATES,
33 PEAK_STATES,
34 MAX_STATES_PER_INSN,
35 MARK_READ_MAX_LEN,
36
37 FILE_NAME,
38 PROG_NAME,
39
40 ALL_STATS_CNT,
41 NUM_STATS_CNT = FILE_NAME - VERDICT,
42 };
43
44 /* In comparison mode each stat can specify up to four different values:
45 * - A side value;
46 * - B side value;
47 * - absolute diff value;
48 * - relative (percentage) diff value.
49 *
50 * When specifying stat specs in comparison mode, user can use one of the
51 * following variant suffixes to specify which exact variant should be used for
52 * ordering or filtering:
53 * - `_a` for A side value;
54 * - `_b` for B side value;
55 * - `_diff` for absolute diff value;
56 * - `_pct` for relative (percentage) diff value.
57 *
58 * If no variant suffix is provided, then `_b` (control data) is assumed.
59 *
60 * As an example, let's say instructions stat has the following output:
61 *
62 * Insns (A) Insns (B) Insns (DIFF)
63 * --------- --------- --------------
64 * 21547 20920 -627 (-2.91%)
65 *
66 * Then:
67 * - 21547 is A side value (insns_a);
68 * - 20920 is B side value (insns_b);
69 * - -627 is absolute diff value (insns_diff);
70 * - -2.91% is relative diff value (insns_pct).
71 *
72 * For verdict there is no verdict_pct variant.
73 * For file and program name, _a and _b variants are equivalent and there are
74 * no _diff or _pct variants.
75 */
76 enum stat_variant {
77 VARIANT_A,
78 VARIANT_B,
79 VARIANT_DIFF,
80 VARIANT_PCT,
81 };
82
83 struct verif_stats {
84 char *file_name;
85 char *prog_name;
86
87 long stats[NUM_STATS_CNT];
88 };
89
90 /* joined comparison mode stats */
91 struct verif_stats_join {
92 char *file_name;
93 char *prog_name;
94
95 const struct verif_stats *stats_a;
96 const struct verif_stats *stats_b;
97 };
98
99 struct stat_specs {
100 int spec_cnt;
101 enum stat_id ids[ALL_STATS_CNT];
102 enum stat_variant variants[ALL_STATS_CNT];
103 bool asc[ALL_STATS_CNT];
104 bool abs[ALL_STATS_CNT];
105 int lens[ALL_STATS_CNT * 3]; /* 3x for comparison mode */
106 };
107
108 enum resfmt {
109 RESFMT_TABLE,
110 RESFMT_TABLE_CALCLEN, /* fake format to pre-calculate table's column widths */
111 RESFMT_CSV,
112 };
113
114 enum filter_kind {
115 FILTER_NAME,
116 FILTER_STAT,
117 };
118
119 enum operator_kind {
120 OP_EQ, /* == or = */
121 OP_NEQ, /* != or <> */
122 OP_LT, /* < */
123 OP_LE, /* <= */
124 OP_GT, /* > */
125 OP_GE, /* >= */
126 };
127
128 struct filter {
129 enum filter_kind kind;
130 /* FILTER_NAME */
131 char *any_glob;
132 char *file_glob;
133 char *prog_glob;
134 /* FILTER_STAT */
135 enum operator_kind op;
136 int stat_id;
137 enum stat_variant stat_var;
138 long value;
139 bool abs;
140 };
141
142 static struct env {
143 char **filenames;
144 int filename_cnt;
145 bool verbose;
146 bool debug;
147 bool quiet;
148 bool force_checkpoints;
149 bool force_reg_invariants;
150 enum resfmt out_fmt;
151 bool show_version;
152 bool comparison_mode;
153 bool replay_mode;
154 int top_n;
155
156 int log_level;
157 int log_size;
158 bool log_fixed;
159
160 struct verif_stats *prog_stats;
161 int prog_stat_cnt;
162
163 /* baseline_stats is allocated and used only in comparison mode */
164 struct verif_stats *baseline_stats;
165 int baseline_stat_cnt;
166
167 struct verif_stats_join *join_stats;
168 int join_stat_cnt;
169
170 struct stat_specs output_spec;
171 struct stat_specs sort_spec;
172
173 struct filter *allow_filters;
174 struct filter *deny_filters;
175 int allow_filter_cnt;
176 int deny_filter_cnt;
177
178 int files_processed;
179 int files_skipped;
180 int progs_processed;
181 int progs_skipped;
182 } env;
183
libbpf_print_fn(enum libbpf_print_level level,const char * format,va_list args)184 static int libbpf_print_fn(enum libbpf_print_level level, const char *format, va_list args)
185 {
186 if (!env.verbose)
187 return 0;
188 if (level == LIBBPF_DEBUG && !env.debug)
189 return 0;
190 return vfprintf(stderr, format, args);
191 }
192
193 #ifndef VERISTAT_VERSION
194 #define VERISTAT_VERSION "<kernel>"
195 #endif
196
197 const char *argp_program_version = "veristat v" VERISTAT_VERSION;
198 const char *argp_program_bug_address = "<bpf@vger.kernel.org>";
199 const char argp_program_doc[] =
200 "veristat BPF verifier stats collection and comparison tool.\n"
201 "\n"
202 "USAGE: veristat <obj-file> [<obj-file>...]\n"
203 " OR: veristat -C <baseline.csv> <comparison.csv>\n"
204 " OR: veristat -R <results.csv>\n";
205
206 enum {
207 OPT_LOG_FIXED = 1000,
208 OPT_LOG_SIZE = 1001,
209 };
210
211 static const struct argp_option opts[] = {
212 { NULL, 'h', NULL, OPTION_HIDDEN, "Show the full help" },
213 { "version", 'V', NULL, 0, "Print version" },
214 { "verbose", 'v', NULL, 0, "Verbose mode" },
215 { "debug", 'd', NULL, 0, "Debug mode (turns on libbpf debug logging)" },
216 { "log-level", 'l', "LEVEL", 0, "Verifier log level (default 0 for normal mode, 1 for verbose mode)" },
217 { "log-fixed", OPT_LOG_FIXED, NULL, 0, "Disable verifier log rotation" },
218 { "log-size", OPT_LOG_SIZE, "BYTES", 0, "Customize verifier log size (default to 16MB)" },
219 { "top-n", 'n', "N", 0, "Emit only up to first N results." },
220 { "quiet", 'q', NULL, 0, "Quiet mode" },
221 { "emit", 'e', "SPEC", 0, "Specify stats to be emitted" },
222 { "sort", 's', "SPEC", 0, "Specify sort order" },
223 { "output-format", 'o', "FMT", 0, "Result output format (table, csv), default is table." },
224 { "compare", 'C', NULL, 0, "Comparison mode" },
225 { "replay", 'R', NULL, 0, "Replay mode" },
226 { "filter", 'f', "FILTER", 0, "Filter expressions (or @filename for file with expressions)." },
227 { "test-states", 't', NULL, 0,
228 "Force frequent BPF verifier state checkpointing (set BPF_F_TEST_STATE_FREQ program flag)" },
229 { "test-reg-invariants", 'r', NULL, 0,
230 "Force BPF verifier failure on register invariant violation (BPF_F_TEST_REG_INVARIANTS program flag)" },
231 {},
232 };
233
234 static int parse_stats(const char *stats_str, struct stat_specs *specs);
235 static int append_filter(struct filter **filters, int *cnt, const char *str);
236 static int append_filter_file(const char *path);
237
parse_arg(int key,char * arg,struct argp_state * state)238 static error_t parse_arg(int key, char *arg, struct argp_state *state)
239 {
240 void *tmp;
241 int err;
242
243 switch (key) {
244 case 'h':
245 argp_state_help(state, stderr, ARGP_HELP_STD_HELP);
246 break;
247 case 'V':
248 env.show_version = true;
249 break;
250 case 'v':
251 env.verbose = true;
252 break;
253 case 'd':
254 env.debug = true;
255 env.verbose = true;
256 break;
257 case 'q':
258 env.quiet = true;
259 break;
260 case 'e':
261 err = parse_stats(arg, &env.output_spec);
262 if (err)
263 return err;
264 break;
265 case 's':
266 err = parse_stats(arg, &env.sort_spec);
267 if (err)
268 return err;
269 break;
270 case 'o':
271 if (strcmp(arg, "table") == 0) {
272 env.out_fmt = RESFMT_TABLE;
273 } else if (strcmp(arg, "csv") == 0) {
274 env.out_fmt = RESFMT_CSV;
275 } else {
276 fprintf(stderr, "Unrecognized output format '%s'\n", arg);
277 return -EINVAL;
278 }
279 break;
280 case 'l':
281 errno = 0;
282 env.log_level = strtol(arg, NULL, 10);
283 if (errno) {
284 fprintf(stderr, "invalid log level: %s\n", arg);
285 argp_usage(state);
286 }
287 break;
288 case OPT_LOG_FIXED:
289 env.log_fixed = true;
290 break;
291 case OPT_LOG_SIZE:
292 errno = 0;
293 env.log_size = strtol(arg, NULL, 10);
294 if (errno) {
295 fprintf(stderr, "invalid log size: %s\n", arg);
296 argp_usage(state);
297 }
298 break;
299 case 't':
300 env.force_checkpoints = true;
301 break;
302 case 'r':
303 env.force_reg_invariants = true;
304 break;
305 case 'n':
306 errno = 0;
307 env.top_n = strtol(arg, NULL, 10);
308 if (errno) {
309 fprintf(stderr, "invalid top N specifier: %s\n", arg);
310 argp_usage(state);
311 }
312 break;
313 case 'C':
314 env.comparison_mode = true;
315 break;
316 case 'R':
317 env.replay_mode = true;
318 break;
319 case 'f':
320 if (arg[0] == '@')
321 err = append_filter_file(arg + 1);
322 else if (arg[0] == '!')
323 err = append_filter(&env.deny_filters, &env.deny_filter_cnt, arg + 1);
324 else
325 err = append_filter(&env.allow_filters, &env.allow_filter_cnt, arg);
326 if (err) {
327 fprintf(stderr, "Failed to collect program filter expressions: %d\n", err);
328 return err;
329 }
330 break;
331 case ARGP_KEY_ARG:
332 tmp = realloc(env.filenames, (env.filename_cnt + 1) * sizeof(*env.filenames));
333 if (!tmp)
334 return -ENOMEM;
335 env.filenames = tmp;
336 env.filenames[env.filename_cnt] = strdup(arg);
337 if (!env.filenames[env.filename_cnt])
338 return -ENOMEM;
339 env.filename_cnt++;
340 break;
341 default:
342 return ARGP_ERR_UNKNOWN;
343 }
344 return 0;
345 }
346
347 static const struct argp argp = {
348 .options = opts,
349 .parser = parse_arg,
350 .doc = argp_program_doc,
351 };
352
353
354 /* Adapted from perf/util/string.c */
glob_matches(const char * str,const char * pat)355 static bool glob_matches(const char *str, const char *pat)
356 {
357 while (*str && *pat && *pat != '*') {
358 if (*str != *pat)
359 return false;
360 str++;
361 pat++;
362 }
363 /* Check wild card */
364 if (*pat == '*') {
365 while (*pat == '*')
366 pat++;
367 if (!*pat) /* Tail wild card matches all */
368 return true;
369 while (*str)
370 if (glob_matches(str++, pat))
371 return true;
372 }
373 return !*str && !*pat;
374 }
375
is_bpf_obj_file(const char * path)376 static bool is_bpf_obj_file(const char *path) {
377 Elf64_Ehdr *ehdr;
378 int fd, err = -EINVAL;
379 Elf *elf = NULL;
380
381 fd = open(path, O_RDONLY | O_CLOEXEC);
382 if (fd < 0)
383 return true; /* we'll fail later and propagate error */
384
385 /* ensure libelf is initialized */
386 (void)elf_version(EV_CURRENT);
387
388 elf = elf_begin(fd, ELF_C_READ, NULL);
389 if (!elf)
390 goto cleanup;
391
392 if (elf_kind(elf) != ELF_K_ELF || gelf_getclass(elf) != ELFCLASS64)
393 goto cleanup;
394
395 ehdr = elf64_getehdr(elf);
396 /* Old LLVM set e_machine to EM_NONE */
397 if (!ehdr || ehdr->e_type != ET_REL || (ehdr->e_machine && ehdr->e_machine != EM_BPF))
398 goto cleanup;
399
400 err = 0;
401 cleanup:
402 if (elf)
403 elf_end(elf);
404 close(fd);
405 return err == 0;
406 }
407
should_process_file_prog(const char * filename,const char * prog_name)408 static bool should_process_file_prog(const char *filename, const char *prog_name)
409 {
410 struct filter *f;
411 int i, allow_cnt = 0;
412
413 for (i = 0; i < env.deny_filter_cnt; i++) {
414 f = &env.deny_filters[i];
415 if (f->kind != FILTER_NAME)
416 continue;
417
418 if (f->any_glob && glob_matches(filename, f->any_glob))
419 return false;
420 if (f->any_glob && prog_name && glob_matches(prog_name, f->any_glob))
421 return false;
422 if (f->file_glob && glob_matches(filename, f->file_glob))
423 return false;
424 if (f->prog_glob && prog_name && glob_matches(prog_name, f->prog_glob))
425 return false;
426 }
427
428 for (i = 0; i < env.allow_filter_cnt; i++) {
429 f = &env.allow_filters[i];
430 if (f->kind != FILTER_NAME)
431 continue;
432
433 allow_cnt++;
434 if (f->any_glob) {
435 if (glob_matches(filename, f->any_glob))
436 return true;
437 /* If we don't know program name yet, any_glob filter
438 * has to assume that current BPF object file might be
439 * relevant; we'll check again later on after opening
440 * BPF object file, at which point program name will
441 * be known finally.
442 */
443 if (!prog_name || glob_matches(prog_name, f->any_glob))
444 return true;
445 } else {
446 if (f->file_glob && !glob_matches(filename, f->file_glob))
447 continue;
448 if (f->prog_glob && prog_name && !glob_matches(prog_name, f->prog_glob))
449 continue;
450 return true;
451 }
452 }
453
454 /* if there are no file/prog name allow filters, allow all progs,
455 * unless they are denied earlier explicitly
456 */
457 return allow_cnt == 0;
458 }
459
460 static struct {
461 enum operator_kind op_kind;
462 const char *op_str;
463 } operators[] = {
464 /* Order of these definitions matter to avoid situations like '<'
465 * matching part of what is actually a '<>' operator. That is,
466 * substrings should go last.
467 */
468 { OP_EQ, "==" },
469 { OP_NEQ, "!=" },
470 { OP_NEQ, "<>" },
471 { OP_LE, "<=" },
472 { OP_LT, "<" },
473 { OP_GE, ">=" },
474 { OP_GT, ">" },
475 { OP_EQ, "=" },
476 };
477
478 static bool parse_stat_id_var(const char *name, size_t len, int *id,
479 enum stat_variant *var, bool *is_abs);
480
append_filter(struct filter ** filters,int * cnt,const char * str)481 static int append_filter(struct filter **filters, int *cnt, const char *str)
482 {
483 struct filter *f;
484 void *tmp;
485 const char *p;
486 int i;
487
488 tmp = realloc(*filters, (*cnt + 1) * sizeof(**filters));
489 if (!tmp)
490 return -ENOMEM;
491 *filters = tmp;
492
493 f = &(*filters)[*cnt];
494 memset(f, 0, sizeof(*f));
495
496 /* First, let's check if it's a stats filter of the following form:
497 * <stat><op><value, where:
498 * - <stat> is one of supported numerical stats (verdict is also
499 * considered numerical, failure == 0, success == 1);
500 * - <op> is comparison operator (see `operators` definitions);
501 * - <value> is an integer (or failure/success, or false/true as
502 * special aliases for 0 and 1, respectively).
503 * If the form doesn't match what user provided, we assume file/prog
504 * glob filter.
505 */
506 for (i = 0; i < ARRAY_SIZE(operators); i++) {
507 enum stat_variant var;
508 int id;
509 long val;
510 const char *end = str;
511 const char *op_str;
512 bool is_abs;
513
514 op_str = operators[i].op_str;
515 p = strstr(str, op_str);
516 if (!p)
517 continue;
518
519 if (!parse_stat_id_var(str, p - str, &id, &var, &is_abs)) {
520 fprintf(stderr, "Unrecognized stat name in '%s'!\n", str);
521 return -EINVAL;
522 }
523 if (id >= FILE_NAME) {
524 fprintf(stderr, "Non-integer stat is specified in '%s'!\n", str);
525 return -EINVAL;
526 }
527
528 p += strlen(op_str);
529
530 if (strcasecmp(p, "true") == 0 ||
531 strcasecmp(p, "t") == 0 ||
532 strcasecmp(p, "success") == 0 ||
533 strcasecmp(p, "succ") == 0 ||
534 strcasecmp(p, "s") == 0 ||
535 strcasecmp(p, "match") == 0 ||
536 strcasecmp(p, "m") == 0) {
537 val = 1;
538 } else if (strcasecmp(p, "false") == 0 ||
539 strcasecmp(p, "f") == 0 ||
540 strcasecmp(p, "failure") == 0 ||
541 strcasecmp(p, "fail") == 0 ||
542 strcasecmp(p, "mismatch") == 0 ||
543 strcasecmp(p, "mis") == 0) {
544 val = 0;
545 } else {
546 errno = 0;
547 val = strtol(p, (char **)&end, 10);
548 if (errno || end == p || *end != '\0' ) {
549 fprintf(stderr, "Invalid integer value in '%s'!\n", str);
550 return -EINVAL;
551 }
552 }
553
554 f->kind = FILTER_STAT;
555 f->stat_id = id;
556 f->stat_var = var;
557 f->op = operators[i].op_kind;
558 f->abs = true;
559 f->value = val;
560
561 *cnt += 1;
562 return 0;
563 }
564
565 /* File/prog filter can be specified either as '<glob>' or
566 * '<file-glob>/<prog-glob>'. In the former case <glob> is applied to
567 * both file and program names. This seems to be way more useful in
568 * practice. If user needs full control, they can use '/<prog-glob>'
569 * form to glob just program name, or '<file-glob>/' to glob only file
570 * name. But usually common <glob> seems to be the most useful and
571 * ergonomic way.
572 */
573 f->kind = FILTER_NAME;
574 p = strchr(str, '/');
575 if (!p) {
576 f->any_glob = strdup(str);
577 if (!f->any_glob)
578 return -ENOMEM;
579 } else {
580 if (str != p) {
581 /* non-empty file glob */
582 f->file_glob = strndup(str, p - str);
583 if (!f->file_glob)
584 return -ENOMEM;
585 }
586 if (strlen(p + 1) > 0) {
587 /* non-empty prog glob */
588 f->prog_glob = strdup(p + 1);
589 if (!f->prog_glob) {
590 free(f->file_glob);
591 f->file_glob = NULL;
592 return -ENOMEM;
593 }
594 }
595 }
596
597 *cnt += 1;
598 return 0;
599 }
600
append_filter_file(const char * path)601 static int append_filter_file(const char *path)
602 {
603 char buf[1024];
604 FILE *f;
605 int err = 0;
606
607 f = fopen(path, "r");
608 if (!f) {
609 err = -errno;
610 fprintf(stderr, "Failed to open filters in '%s': %d\n", path, err);
611 return err;
612 }
613
614 while (fscanf(f, " %1023[^\n]\n", buf) == 1) {
615 /* lines starting with # are comments, skip them */
616 if (buf[0] == '\0' || buf[0] == '#')
617 continue;
618 /* lines starting with ! are negative match filters */
619 if (buf[0] == '!')
620 err = append_filter(&env.deny_filters, &env.deny_filter_cnt, buf + 1);
621 else
622 err = append_filter(&env.allow_filters, &env.allow_filter_cnt, buf);
623 if (err)
624 goto cleanup;
625 }
626
627 cleanup:
628 fclose(f);
629 return err;
630 }
631
632 static const struct stat_specs default_output_spec = {
633 .spec_cnt = 7,
634 .ids = {
635 FILE_NAME, PROG_NAME, VERDICT, DURATION,
636 TOTAL_INSNS, TOTAL_STATES, PEAK_STATES,
637 },
638 };
639
640 static const struct stat_specs default_csv_output_spec = {
641 .spec_cnt = 9,
642 .ids = {
643 FILE_NAME, PROG_NAME, VERDICT, DURATION,
644 TOTAL_INSNS, TOTAL_STATES, PEAK_STATES,
645 MAX_STATES_PER_INSN, MARK_READ_MAX_LEN,
646 },
647 };
648
649 static const struct stat_specs default_sort_spec = {
650 .spec_cnt = 2,
651 .ids = {
652 FILE_NAME, PROG_NAME,
653 },
654 .asc = { true, true, },
655 };
656
657 /* sorting for comparison mode to join two data sets */
658 static const struct stat_specs join_sort_spec = {
659 .spec_cnt = 2,
660 .ids = {
661 FILE_NAME, PROG_NAME,
662 },
663 .asc = { true, true, },
664 };
665
666 static struct stat_def {
667 const char *header;
668 const char *names[4];
669 bool asc_by_default;
670 bool left_aligned;
671 } stat_defs[] = {
672 [FILE_NAME] = { "File", {"file_name", "filename", "file"}, true /* asc */, true /* left */ },
673 [PROG_NAME] = { "Program", {"prog_name", "progname", "prog"}, true /* asc */, true /* left */ },
674 [VERDICT] = { "Verdict", {"verdict"}, true /* asc: failure, success */, true /* left */ },
675 [DURATION] = { "Duration (us)", {"duration", "dur"}, },
676 [TOTAL_INSNS] = { "Insns", {"total_insns", "insns"}, },
677 [TOTAL_STATES] = { "States", {"total_states", "states"}, },
678 [PEAK_STATES] = { "Peak states", {"peak_states"}, },
679 [MAX_STATES_PER_INSN] = { "Max states per insn", {"max_states_per_insn"}, },
680 [MARK_READ_MAX_LEN] = { "Max mark read length", {"max_mark_read_len", "mark_read"}, },
681 };
682
parse_stat_id_var(const char * name,size_t len,int * id,enum stat_variant * var,bool * is_abs)683 static bool parse_stat_id_var(const char *name, size_t len, int *id,
684 enum stat_variant *var, bool *is_abs)
685 {
686 static const char *var_sfxs[] = {
687 [VARIANT_A] = "_a",
688 [VARIANT_B] = "_b",
689 [VARIANT_DIFF] = "_diff",
690 [VARIANT_PCT] = "_pct",
691 };
692 int i, j, k;
693
694 /* |<stat>| means we take absolute value of given stat */
695 *is_abs = false;
696 if (len > 2 && name[0] == '|' && name[len - 1] == '|') {
697 *is_abs = true;
698 name += 1;
699 len -= 2;
700 }
701
702 for (i = 0; i < ARRAY_SIZE(stat_defs); i++) {
703 struct stat_def *def = &stat_defs[i];
704 size_t alias_len, sfx_len;
705 const char *alias;
706
707 for (j = 0; j < ARRAY_SIZE(stat_defs[i].names); j++) {
708 alias = def->names[j];
709 if (!alias)
710 continue;
711
712 alias_len = strlen(alias);
713 if (strncmp(name, alias, alias_len) != 0)
714 continue;
715
716 if (alias_len == len) {
717 /* If no variant suffix is specified, we
718 * assume control group (just in case we are
719 * in comparison mode. Variant is ignored in
720 * non-comparison mode.
721 */
722 *var = VARIANT_B;
723 *id = i;
724 return true;
725 }
726
727 for (k = 0; k < ARRAY_SIZE(var_sfxs); k++) {
728 sfx_len = strlen(var_sfxs[k]);
729 if (alias_len + sfx_len != len)
730 continue;
731
732 if (strncmp(name + alias_len, var_sfxs[k], sfx_len) == 0) {
733 *var = (enum stat_variant)k;
734 *id = i;
735 return true;
736 }
737 }
738 }
739 }
740
741 return false;
742 }
743
is_asc_sym(char c)744 static bool is_asc_sym(char c)
745 {
746 return c == '^';
747 }
748
is_desc_sym(char c)749 static bool is_desc_sym(char c)
750 {
751 return c == 'v' || c == 'V' || c == '.' || c == '!' || c == '_';
752 }
753
parse_stat(const char * stat_name,struct stat_specs * specs)754 static int parse_stat(const char *stat_name, struct stat_specs *specs)
755 {
756 int id;
757 bool has_order = false, is_asc = false, is_abs = false;
758 size_t len = strlen(stat_name);
759 enum stat_variant var;
760
761 if (specs->spec_cnt >= ARRAY_SIZE(specs->ids)) {
762 fprintf(stderr, "Can't specify more than %zd stats\n", ARRAY_SIZE(specs->ids));
763 return -E2BIG;
764 }
765
766 if (len > 1 && (is_asc_sym(stat_name[len - 1]) || is_desc_sym(stat_name[len - 1]))) {
767 has_order = true;
768 is_asc = is_asc_sym(stat_name[len - 1]);
769 len -= 1;
770 }
771
772 if (!parse_stat_id_var(stat_name, len, &id, &var, &is_abs)) {
773 fprintf(stderr, "Unrecognized stat name '%s'\n", stat_name);
774 return -ESRCH;
775 }
776
777 specs->ids[specs->spec_cnt] = id;
778 specs->variants[specs->spec_cnt] = var;
779 specs->asc[specs->spec_cnt] = has_order ? is_asc : stat_defs[id].asc_by_default;
780 specs->abs[specs->spec_cnt] = is_abs;
781 specs->spec_cnt++;
782
783 return 0;
784 }
785
parse_stats(const char * stats_str,struct stat_specs * specs)786 static int parse_stats(const char *stats_str, struct stat_specs *specs)
787 {
788 char *input, *state = NULL, *next;
789 int err, cnt = 0;
790
791 input = strdup(stats_str);
792 if (!input)
793 return -ENOMEM;
794
795 while ((next = strtok_r(cnt++ ? NULL : input, ",", &state))) {
796 err = parse_stat(next, specs);
797 if (err) {
798 free(input);
799 return err;
800 }
801 }
802
803 free(input);
804 return 0;
805 }
806
free_verif_stats(struct verif_stats * stats,size_t stat_cnt)807 static void free_verif_stats(struct verif_stats *stats, size_t stat_cnt)
808 {
809 int i;
810
811 if (!stats)
812 return;
813
814 for (i = 0; i < stat_cnt; i++) {
815 free(stats[i].file_name);
816 free(stats[i].prog_name);
817 }
818 free(stats);
819 }
820
821 static char verif_log_buf[64 * 1024];
822
823 #define MAX_PARSED_LOG_LINES 100
824
parse_verif_log(char * const buf,size_t buf_sz,struct verif_stats * s)825 static int parse_verif_log(char * const buf, size_t buf_sz, struct verif_stats *s)
826 {
827 const char *cur;
828 int pos, lines;
829
830 buf[buf_sz - 1] = '\0';
831
832 for (pos = strlen(buf) - 1, lines = 0; pos >= 0 && lines < MAX_PARSED_LOG_LINES; lines++) {
833 /* find previous endline or otherwise take the start of log buf */
834 for (cur = &buf[pos]; cur > buf && cur[0] != '\n'; cur--, pos--) {
835 }
836 /* next time start from end of previous line (or pos goes to <0) */
837 pos--;
838 /* if we found endline, point right after endline symbol;
839 * otherwise, stay at the beginning of log buf
840 */
841 if (cur[0] == '\n')
842 cur++;
843
844 if (1 == sscanf(cur, "verification time %ld usec\n", &s->stats[DURATION]))
845 continue;
846 if (6 == sscanf(cur, "processed %ld insns (limit %*d) max_states_per_insn %ld total_states %ld peak_states %ld mark_read %ld",
847 &s->stats[TOTAL_INSNS],
848 &s->stats[MAX_STATES_PER_INSN],
849 &s->stats[TOTAL_STATES],
850 &s->stats[PEAK_STATES],
851 &s->stats[MARK_READ_MAX_LEN]))
852 continue;
853 }
854
855 return 0;
856 }
857
guess_prog_type_by_ctx_name(const char * ctx_name,enum bpf_prog_type * prog_type,enum bpf_attach_type * attach_type)858 static int guess_prog_type_by_ctx_name(const char *ctx_name,
859 enum bpf_prog_type *prog_type,
860 enum bpf_attach_type *attach_type)
861 {
862 /* We need to guess program type based on its declared context type.
863 * This guess can't be perfect as many different program types might
864 * share the same context type. So we can only hope to reasonably
865 * well guess this and get lucky.
866 *
867 * Just in case, we support both UAPI-side type names and
868 * kernel-internal names.
869 */
870 static struct {
871 const char *uapi_name;
872 const char *kern_name;
873 enum bpf_prog_type prog_type;
874 enum bpf_attach_type attach_type;
875 } ctx_map[] = {
876 /* __sk_buff is most ambiguous, we assume TC program */
877 { "__sk_buff", "sk_buff", BPF_PROG_TYPE_SCHED_CLS },
878 { "bpf_sock", "sock", BPF_PROG_TYPE_CGROUP_SOCK, BPF_CGROUP_INET4_POST_BIND },
879 { "bpf_sock_addr", "bpf_sock_addr_kern", BPF_PROG_TYPE_CGROUP_SOCK_ADDR, BPF_CGROUP_INET4_BIND },
880 { "bpf_sock_ops", "bpf_sock_ops_kern", BPF_PROG_TYPE_SOCK_OPS, BPF_CGROUP_SOCK_OPS },
881 { "sk_msg_md", "sk_msg", BPF_PROG_TYPE_SK_MSG, BPF_SK_MSG_VERDICT },
882 { "bpf_cgroup_dev_ctx", "bpf_cgroup_dev_ctx", BPF_PROG_TYPE_CGROUP_DEVICE, BPF_CGROUP_DEVICE },
883 { "bpf_sysctl", "bpf_sysctl_kern", BPF_PROG_TYPE_CGROUP_SYSCTL, BPF_CGROUP_SYSCTL },
884 { "bpf_sockopt", "bpf_sockopt_kern", BPF_PROG_TYPE_CGROUP_SOCKOPT, BPF_CGROUP_SETSOCKOPT },
885 { "sk_reuseport_md", "sk_reuseport_kern", BPF_PROG_TYPE_SK_REUSEPORT, BPF_SK_REUSEPORT_SELECT_OR_MIGRATE },
886 { "bpf_sk_lookup", "bpf_sk_lookup_kern", BPF_PROG_TYPE_SK_LOOKUP, BPF_SK_LOOKUP },
887 { "xdp_md", "xdp_buff", BPF_PROG_TYPE_XDP, BPF_XDP },
888 /* tracing types with no expected attach type */
889 { "bpf_user_pt_regs_t", "pt_regs", BPF_PROG_TYPE_KPROBE },
890 { "bpf_perf_event_data", "bpf_perf_event_data_kern", BPF_PROG_TYPE_PERF_EVENT },
891 /* raw_tp programs use u64[] from kernel side, we don't want
892 * to match on that, probably; so NULL for kern-side type
893 */
894 { "bpf_raw_tracepoint_args", NULL, BPF_PROG_TYPE_RAW_TRACEPOINT },
895 };
896 int i;
897
898 if (!ctx_name)
899 return -EINVAL;
900
901 for (i = 0; i < ARRAY_SIZE(ctx_map); i++) {
902 if (strcmp(ctx_map[i].uapi_name, ctx_name) == 0 ||
903 (ctx_map[i].kern_name && strcmp(ctx_map[i].kern_name, ctx_name) == 0)) {
904 *prog_type = ctx_map[i].prog_type;
905 *attach_type = ctx_map[i].attach_type;
906 return 0;
907 }
908 }
909
910 return -ESRCH;
911 }
912
fixup_obj(struct bpf_object * obj,struct bpf_program * prog,const char * filename)913 static void fixup_obj(struct bpf_object *obj, struct bpf_program *prog, const char *filename)
914 {
915 struct bpf_map *map;
916
917 bpf_object__for_each_map(map, obj) {
918 /* disable pinning */
919 bpf_map__set_pin_path(map, NULL);
920
921 /* fix up map size, if necessary */
922 switch (bpf_map__type(map)) {
923 case BPF_MAP_TYPE_SK_STORAGE:
924 case BPF_MAP_TYPE_TASK_STORAGE:
925 case BPF_MAP_TYPE_INODE_STORAGE:
926 case BPF_MAP_TYPE_CGROUP_STORAGE:
927 break;
928 default:
929 if (bpf_map__max_entries(map) == 0)
930 bpf_map__set_max_entries(map, 1);
931 }
932 }
933
934 /* SEC(freplace) programs can't be loaded with veristat as is,
935 * but we can try guessing their target program's expected type by
936 * looking at the type of program's first argument and substituting
937 * corresponding program type
938 */
939 if (bpf_program__type(prog) == BPF_PROG_TYPE_EXT) {
940 const struct btf *btf = bpf_object__btf(obj);
941 const char *prog_name = bpf_program__name(prog);
942 enum bpf_prog_type prog_type;
943 enum bpf_attach_type attach_type;
944 const struct btf_type *t;
945 const char *ctx_name;
946 int id;
947
948 if (!btf)
949 goto skip_freplace_fixup;
950
951 id = btf__find_by_name_kind(btf, prog_name, BTF_KIND_FUNC);
952 t = btf__type_by_id(btf, id);
953 t = btf__type_by_id(btf, t->type);
954 if (!btf_is_func_proto(t) || btf_vlen(t) != 1)
955 goto skip_freplace_fixup;
956
957 /* context argument is a pointer to a struct/typedef */
958 t = btf__type_by_id(btf, btf_params(t)[0].type);
959 while (t && btf_is_mod(t))
960 t = btf__type_by_id(btf, t->type);
961 if (!t || !btf_is_ptr(t))
962 goto skip_freplace_fixup;
963 t = btf__type_by_id(btf, t->type);
964 while (t && btf_is_mod(t))
965 t = btf__type_by_id(btf, t->type);
966 if (!t)
967 goto skip_freplace_fixup;
968
969 ctx_name = btf__name_by_offset(btf, t->name_off);
970
971 if (guess_prog_type_by_ctx_name(ctx_name, &prog_type, &attach_type) == 0) {
972 bpf_program__set_type(prog, prog_type);
973 bpf_program__set_expected_attach_type(prog, attach_type);
974
975 if (!env.quiet) {
976 printf("Using guessed program type '%s' for %s/%s...\n",
977 libbpf_bpf_prog_type_str(prog_type),
978 filename, prog_name);
979 }
980 } else {
981 if (!env.quiet) {
982 printf("Failed to guess program type for freplace program with context type name '%s' for %s/%s. Consider using canonical type names to help veristat...\n",
983 ctx_name, filename, prog_name);
984 }
985 }
986 }
987 skip_freplace_fixup:
988 return;
989 }
990
process_prog(const char * filename,struct bpf_object * obj,struct bpf_program * prog)991 static int process_prog(const char *filename, struct bpf_object *obj, struct bpf_program *prog)
992 {
993 const char *base_filename = basename(strdupa(filename));
994 const char *prog_name = bpf_program__name(prog);
995 char *buf;
996 int buf_sz, log_level;
997 struct verif_stats *stats;
998 int err = 0;
999 void *tmp;
1000
1001 if (!should_process_file_prog(base_filename, bpf_program__name(prog))) {
1002 env.progs_skipped++;
1003 return 0;
1004 }
1005
1006 tmp = realloc(env.prog_stats, (env.prog_stat_cnt + 1) * sizeof(*env.prog_stats));
1007 if (!tmp)
1008 return -ENOMEM;
1009 env.prog_stats = tmp;
1010 stats = &env.prog_stats[env.prog_stat_cnt++];
1011 memset(stats, 0, sizeof(*stats));
1012
1013 if (env.verbose) {
1014 buf_sz = env.log_size ? env.log_size : 16 * 1024 * 1024;
1015 buf = malloc(buf_sz);
1016 if (!buf)
1017 return -ENOMEM;
1018 /* ensure we always request stats */
1019 log_level = env.log_level | 4 | (env.log_fixed ? 8 : 0);
1020 } else {
1021 buf = verif_log_buf;
1022 buf_sz = sizeof(verif_log_buf);
1023 /* request only verifier stats */
1024 log_level = 4 | (env.log_fixed ? 8 : 0);
1025 }
1026 verif_log_buf[0] = '\0';
1027
1028 bpf_program__set_log_buf(prog, buf, buf_sz);
1029 bpf_program__set_log_level(prog, log_level);
1030
1031 /* increase chances of successful BPF object loading */
1032 fixup_obj(obj, prog, base_filename);
1033
1034 if (env.force_checkpoints)
1035 bpf_program__set_flags(prog, bpf_program__flags(prog) | BPF_F_TEST_STATE_FREQ);
1036 if (env.force_reg_invariants)
1037 bpf_program__set_flags(prog, bpf_program__flags(prog) | BPF_F_TEST_REG_INVARIANTS);
1038
1039 err = bpf_object__load(obj);
1040 env.progs_processed++;
1041
1042 stats->file_name = strdup(base_filename);
1043 stats->prog_name = strdup(bpf_program__name(prog));
1044 stats->stats[VERDICT] = err == 0; /* 1 - success, 0 - failure */
1045 parse_verif_log(buf, buf_sz, stats);
1046
1047 if (env.verbose) {
1048 printf("PROCESSING %s/%s, DURATION US: %ld, VERDICT: %s, VERIFIER LOG:\n%s\n",
1049 filename, prog_name, stats->stats[DURATION],
1050 err ? "failure" : "success", buf);
1051 }
1052
1053 if (verif_log_buf != buf)
1054 free(buf);
1055
1056 return 0;
1057 };
1058
process_obj(const char * filename)1059 static int process_obj(const char *filename)
1060 {
1061 const char *base_filename = basename(strdupa(filename));
1062 struct bpf_object *obj = NULL, *tobj;
1063 struct bpf_program *prog, *tprog, *lprog;
1064 libbpf_print_fn_t old_libbpf_print_fn;
1065 LIBBPF_OPTS(bpf_object_open_opts, opts);
1066 int err = 0, prog_cnt = 0;
1067
1068 if (!should_process_file_prog(base_filename, NULL)) {
1069 if (env.verbose)
1070 printf("Skipping '%s' due to filters...\n", filename);
1071 env.files_skipped++;
1072 return 0;
1073 }
1074 if (!is_bpf_obj_file(filename)) {
1075 if (env.verbose)
1076 printf("Skipping '%s' as it's not a BPF object file...\n", filename);
1077 env.files_skipped++;
1078 return 0;
1079 }
1080
1081 if (!env.quiet && env.out_fmt == RESFMT_TABLE)
1082 printf("Processing '%s'...\n", base_filename);
1083
1084 old_libbpf_print_fn = libbpf_set_print(libbpf_print_fn);
1085 obj = bpf_object__open_file(filename, &opts);
1086 if (!obj) {
1087 /* if libbpf can't open BPF object file, it could be because
1088 * that BPF object file is incomplete and has to be statically
1089 * linked into a final BPF object file; instead of bailing
1090 * out, report it into stderr, mark it as skipped, and
1091 * proceed
1092 */
1093 fprintf(stderr, "Failed to open '%s': %d\n", filename, -errno);
1094 env.files_skipped++;
1095 err = 0;
1096 goto cleanup;
1097 }
1098
1099 env.files_processed++;
1100
1101 bpf_object__for_each_program(prog, obj) {
1102 prog_cnt++;
1103 }
1104
1105 if (prog_cnt == 1) {
1106 prog = bpf_object__next_program(obj, NULL);
1107 bpf_program__set_autoload(prog, true);
1108 process_prog(filename, obj, prog);
1109 goto cleanup;
1110 }
1111
1112 bpf_object__for_each_program(prog, obj) {
1113 const char *prog_name = bpf_program__name(prog);
1114
1115 tobj = bpf_object__open_file(filename, &opts);
1116 if (!tobj) {
1117 err = -errno;
1118 fprintf(stderr, "Failed to open '%s': %d\n", filename, err);
1119 goto cleanup;
1120 }
1121
1122 lprog = NULL;
1123 bpf_object__for_each_program(tprog, tobj) {
1124 const char *tprog_name = bpf_program__name(tprog);
1125
1126 if (strcmp(prog_name, tprog_name) == 0) {
1127 bpf_program__set_autoload(tprog, true);
1128 lprog = tprog;
1129 } else {
1130 bpf_program__set_autoload(tprog, false);
1131 }
1132 }
1133
1134 process_prog(filename, tobj, lprog);
1135 bpf_object__close(tobj);
1136 }
1137
1138 cleanup:
1139 bpf_object__close(obj);
1140 libbpf_set_print(old_libbpf_print_fn);
1141 return err;
1142 }
1143
cmp_stat(const struct verif_stats * s1,const struct verif_stats * s2,enum stat_id id,bool asc,bool abs)1144 static int cmp_stat(const struct verif_stats *s1, const struct verif_stats *s2,
1145 enum stat_id id, bool asc, bool abs)
1146 {
1147 int cmp = 0;
1148
1149 switch (id) {
1150 case FILE_NAME:
1151 cmp = strcmp(s1->file_name, s2->file_name);
1152 break;
1153 case PROG_NAME:
1154 cmp = strcmp(s1->prog_name, s2->prog_name);
1155 break;
1156 case VERDICT:
1157 case DURATION:
1158 case TOTAL_INSNS:
1159 case TOTAL_STATES:
1160 case PEAK_STATES:
1161 case MAX_STATES_PER_INSN:
1162 case MARK_READ_MAX_LEN: {
1163 long v1 = s1->stats[id];
1164 long v2 = s2->stats[id];
1165
1166 if (abs) {
1167 v1 = v1 < 0 ? -v1 : v1;
1168 v2 = v2 < 0 ? -v2 : v2;
1169 }
1170
1171 if (v1 != v2)
1172 cmp = v1 < v2 ? -1 : 1;
1173 break;
1174 }
1175 default:
1176 fprintf(stderr, "Unrecognized stat #%d\n", id);
1177 exit(1);
1178 }
1179
1180 return asc ? cmp : -cmp;
1181 }
1182
cmp_prog_stats(const void * v1,const void * v2)1183 static int cmp_prog_stats(const void *v1, const void *v2)
1184 {
1185 const struct verif_stats *s1 = v1, *s2 = v2;
1186 int i, cmp;
1187
1188 for (i = 0; i < env.sort_spec.spec_cnt; i++) {
1189 cmp = cmp_stat(s1, s2, env.sort_spec.ids[i],
1190 env.sort_spec.asc[i], env.sort_spec.abs[i]);
1191 if (cmp != 0)
1192 return cmp;
1193 }
1194
1195 /* always disambiguate with file+prog, which are unique */
1196 cmp = strcmp(s1->file_name, s2->file_name);
1197 if (cmp != 0)
1198 return cmp;
1199 return strcmp(s1->prog_name, s2->prog_name);
1200 }
1201
fetch_join_stat_value(const struct verif_stats_join * s,enum stat_id id,enum stat_variant var,const char ** str_val,double * num_val)1202 static void fetch_join_stat_value(const struct verif_stats_join *s,
1203 enum stat_id id, enum stat_variant var,
1204 const char **str_val,
1205 double *num_val)
1206 {
1207 long v1, v2;
1208
1209 if (id == FILE_NAME) {
1210 *str_val = s->file_name;
1211 return;
1212 }
1213 if (id == PROG_NAME) {
1214 *str_val = s->prog_name;
1215 return;
1216 }
1217
1218 v1 = s->stats_a ? s->stats_a->stats[id] : 0;
1219 v2 = s->stats_b ? s->stats_b->stats[id] : 0;
1220
1221 switch (var) {
1222 case VARIANT_A:
1223 if (!s->stats_a)
1224 *num_val = -DBL_MAX;
1225 else
1226 *num_val = s->stats_a->stats[id];
1227 return;
1228 case VARIANT_B:
1229 if (!s->stats_b)
1230 *num_val = -DBL_MAX;
1231 else
1232 *num_val = s->stats_b->stats[id];
1233 return;
1234 case VARIANT_DIFF:
1235 if (!s->stats_a || !s->stats_b)
1236 *num_val = -DBL_MAX;
1237 else if (id == VERDICT)
1238 *num_val = v1 == v2 ? 1.0 /* MATCH */ : 0.0 /* MISMATCH */;
1239 else
1240 *num_val = (double)(v2 - v1);
1241 return;
1242 case VARIANT_PCT:
1243 if (!s->stats_a || !s->stats_b) {
1244 *num_val = -DBL_MAX;
1245 } else if (v1 == 0) {
1246 if (v1 == v2)
1247 *num_val = 0.0;
1248 else
1249 *num_val = v2 < v1 ? -100.0 : 100.0;
1250 } else {
1251 *num_val = (v2 - v1) * 100.0 / v1;
1252 }
1253 return;
1254 }
1255 }
1256
cmp_join_stat(const struct verif_stats_join * s1,const struct verif_stats_join * s2,enum stat_id id,enum stat_variant var,bool asc,bool abs)1257 static int cmp_join_stat(const struct verif_stats_join *s1,
1258 const struct verif_stats_join *s2,
1259 enum stat_id id, enum stat_variant var,
1260 bool asc, bool abs)
1261 {
1262 const char *str1 = NULL, *str2 = NULL;
1263 double v1 = 0.0, v2 = 0.0;
1264 int cmp = 0;
1265
1266 fetch_join_stat_value(s1, id, var, &str1, &v1);
1267 fetch_join_stat_value(s2, id, var, &str2, &v2);
1268
1269 if (abs) {
1270 v1 = fabs(v1);
1271 v2 = fabs(v2);
1272 }
1273
1274 if (str1)
1275 cmp = strcmp(str1, str2);
1276 else if (v1 != v2)
1277 cmp = v1 < v2 ? -1 : 1;
1278
1279 return asc ? cmp : -cmp;
1280 }
1281
cmp_join_stats(const void * v1,const void * v2)1282 static int cmp_join_stats(const void *v1, const void *v2)
1283 {
1284 const struct verif_stats_join *s1 = v1, *s2 = v2;
1285 int i, cmp;
1286
1287 for (i = 0; i < env.sort_spec.spec_cnt; i++) {
1288 cmp = cmp_join_stat(s1, s2,
1289 env.sort_spec.ids[i],
1290 env.sort_spec.variants[i],
1291 env.sort_spec.asc[i],
1292 env.sort_spec.abs[i]);
1293 if (cmp != 0)
1294 return cmp;
1295 }
1296
1297 /* always disambiguate with file+prog, which are unique */
1298 cmp = strcmp(s1->file_name, s2->file_name);
1299 if (cmp != 0)
1300 return cmp;
1301 return strcmp(s1->prog_name, s2->prog_name);
1302 }
1303
1304 #define HEADER_CHAR '-'
1305 #define COLUMN_SEP " "
1306
output_header_underlines(void)1307 static void output_header_underlines(void)
1308 {
1309 int i, j, len;
1310
1311 for (i = 0; i < env.output_spec.spec_cnt; i++) {
1312 len = env.output_spec.lens[i];
1313
1314 printf("%s", i == 0 ? "" : COLUMN_SEP);
1315 for (j = 0; j < len; j++)
1316 printf("%c", HEADER_CHAR);
1317 }
1318 printf("\n");
1319 }
1320
output_headers(enum resfmt fmt)1321 static void output_headers(enum resfmt fmt)
1322 {
1323 const char *fmt_str;
1324 int i, len;
1325
1326 for (i = 0; i < env.output_spec.spec_cnt; i++) {
1327 int id = env.output_spec.ids[i];
1328 int *max_len = &env.output_spec.lens[i];
1329
1330 switch (fmt) {
1331 case RESFMT_TABLE_CALCLEN:
1332 len = snprintf(NULL, 0, "%s", stat_defs[id].header);
1333 if (len > *max_len)
1334 *max_len = len;
1335 break;
1336 case RESFMT_TABLE:
1337 fmt_str = stat_defs[id].left_aligned ? "%s%-*s" : "%s%*s";
1338 printf(fmt_str, i == 0 ? "" : COLUMN_SEP, *max_len, stat_defs[id].header);
1339 if (i == env.output_spec.spec_cnt - 1)
1340 printf("\n");
1341 break;
1342 case RESFMT_CSV:
1343 printf("%s%s", i == 0 ? "" : ",", stat_defs[id].names[0]);
1344 if (i == env.output_spec.spec_cnt - 1)
1345 printf("\n");
1346 break;
1347 }
1348 }
1349
1350 if (fmt == RESFMT_TABLE)
1351 output_header_underlines();
1352 }
1353
prepare_value(const struct verif_stats * s,enum stat_id id,const char ** str,long * val)1354 static void prepare_value(const struct verif_stats *s, enum stat_id id,
1355 const char **str, long *val)
1356 {
1357 switch (id) {
1358 case FILE_NAME:
1359 *str = s ? s->file_name : "N/A";
1360 break;
1361 case PROG_NAME:
1362 *str = s ? s->prog_name : "N/A";
1363 break;
1364 case VERDICT:
1365 if (!s)
1366 *str = "N/A";
1367 else
1368 *str = s->stats[VERDICT] ? "success" : "failure";
1369 break;
1370 case DURATION:
1371 case TOTAL_INSNS:
1372 case TOTAL_STATES:
1373 case PEAK_STATES:
1374 case MAX_STATES_PER_INSN:
1375 case MARK_READ_MAX_LEN:
1376 *val = s ? s->stats[id] : 0;
1377 break;
1378 default:
1379 fprintf(stderr, "Unrecognized stat #%d\n", id);
1380 exit(1);
1381 }
1382 }
1383
output_stats(const struct verif_stats * s,enum resfmt fmt,bool last)1384 static void output_stats(const struct verif_stats *s, enum resfmt fmt, bool last)
1385 {
1386 int i;
1387
1388 for (i = 0; i < env.output_spec.spec_cnt; i++) {
1389 int id = env.output_spec.ids[i];
1390 int *max_len = &env.output_spec.lens[i], len;
1391 const char *str = NULL;
1392 long val = 0;
1393
1394 prepare_value(s, id, &str, &val);
1395
1396 switch (fmt) {
1397 case RESFMT_TABLE_CALCLEN:
1398 if (str)
1399 len = snprintf(NULL, 0, "%s", str);
1400 else
1401 len = snprintf(NULL, 0, "%ld", val);
1402 if (len > *max_len)
1403 *max_len = len;
1404 break;
1405 case RESFMT_TABLE:
1406 if (str)
1407 printf("%s%-*s", i == 0 ? "" : COLUMN_SEP, *max_len, str);
1408 else
1409 printf("%s%*ld", i == 0 ? "" : COLUMN_SEP, *max_len, val);
1410 if (i == env.output_spec.spec_cnt - 1)
1411 printf("\n");
1412 break;
1413 case RESFMT_CSV:
1414 if (str)
1415 printf("%s%s", i == 0 ? "" : ",", str);
1416 else
1417 printf("%s%ld", i == 0 ? "" : ",", val);
1418 if (i == env.output_spec.spec_cnt - 1)
1419 printf("\n");
1420 break;
1421 }
1422 }
1423
1424 if (last && fmt == RESFMT_TABLE) {
1425 output_header_underlines();
1426 printf("Done. Processed %d files, %d programs. Skipped %d files, %d programs.\n",
1427 env.files_processed, env.files_skipped, env.progs_processed, env.progs_skipped);
1428 }
1429 }
1430
parse_stat_value(const char * str,enum stat_id id,struct verif_stats * st)1431 static int parse_stat_value(const char *str, enum stat_id id, struct verif_stats *st)
1432 {
1433 switch (id) {
1434 case FILE_NAME:
1435 st->file_name = strdup(str);
1436 if (!st->file_name)
1437 return -ENOMEM;
1438 break;
1439 case PROG_NAME:
1440 st->prog_name = strdup(str);
1441 if (!st->prog_name)
1442 return -ENOMEM;
1443 break;
1444 case VERDICT:
1445 if (strcmp(str, "success") == 0) {
1446 st->stats[VERDICT] = true;
1447 } else if (strcmp(str, "failure") == 0) {
1448 st->stats[VERDICT] = false;
1449 } else {
1450 fprintf(stderr, "Unrecognized verification verdict '%s'\n", str);
1451 return -EINVAL;
1452 }
1453 break;
1454 case DURATION:
1455 case TOTAL_INSNS:
1456 case TOTAL_STATES:
1457 case PEAK_STATES:
1458 case MAX_STATES_PER_INSN:
1459 case MARK_READ_MAX_LEN: {
1460 long val;
1461 int err, n;
1462
1463 if (sscanf(str, "%ld %n", &val, &n) != 1 || n != strlen(str)) {
1464 err = -errno;
1465 fprintf(stderr, "Failed to parse '%s' as integer\n", str);
1466 return err;
1467 }
1468
1469 st->stats[id] = val;
1470 break;
1471 }
1472 default:
1473 fprintf(stderr, "Unrecognized stat #%d\n", id);
1474 return -EINVAL;
1475 }
1476 return 0;
1477 }
1478
parse_stats_csv(const char * filename,struct stat_specs * specs,struct verif_stats ** statsp,int * stat_cntp)1479 static int parse_stats_csv(const char *filename, struct stat_specs *specs,
1480 struct verif_stats **statsp, int *stat_cntp)
1481 {
1482 char line[4096];
1483 FILE *f;
1484 int err = 0;
1485 bool header = true;
1486
1487 f = fopen(filename, "r");
1488 if (!f) {
1489 err = -errno;
1490 fprintf(stderr, "Failed to open '%s': %d\n", filename, err);
1491 return err;
1492 }
1493
1494 *stat_cntp = 0;
1495
1496 while (fgets(line, sizeof(line), f)) {
1497 char *input = line, *state = NULL, *next;
1498 struct verif_stats *st = NULL;
1499 int col = 0, cnt = 0;
1500
1501 if (!header) {
1502 void *tmp;
1503
1504 tmp = realloc(*statsp, (*stat_cntp + 1) * sizeof(**statsp));
1505 if (!tmp) {
1506 err = -ENOMEM;
1507 goto cleanup;
1508 }
1509 *statsp = tmp;
1510
1511 st = &(*statsp)[*stat_cntp];
1512 memset(st, 0, sizeof(*st));
1513
1514 *stat_cntp += 1;
1515 }
1516
1517 while ((next = strtok_r(cnt++ ? NULL : input, ",\n", &state))) {
1518 if (header) {
1519 /* for the first line, set up spec stats */
1520 err = parse_stat(next, specs);
1521 if (err)
1522 goto cleanup;
1523 continue;
1524 }
1525
1526 /* for all other lines, parse values based on spec */
1527 if (col >= specs->spec_cnt) {
1528 fprintf(stderr, "Found extraneous column #%d in row #%d of '%s'\n",
1529 col, *stat_cntp, filename);
1530 err = -EINVAL;
1531 goto cleanup;
1532 }
1533 err = parse_stat_value(next, specs->ids[col], st);
1534 if (err)
1535 goto cleanup;
1536 col++;
1537 }
1538
1539 if (header) {
1540 header = false;
1541 continue;
1542 }
1543
1544 if (col < specs->spec_cnt) {
1545 fprintf(stderr, "Not enough columns in row #%d in '%s'\n",
1546 *stat_cntp, filename);
1547 err = -EINVAL;
1548 goto cleanup;
1549 }
1550
1551 if (!st->file_name || !st->prog_name) {
1552 fprintf(stderr, "Row #%d in '%s' is missing file and/or program name\n",
1553 *stat_cntp, filename);
1554 err = -EINVAL;
1555 goto cleanup;
1556 }
1557
1558 /* in comparison mode we can only check filters after we
1559 * parsed entire line; if row should be ignored we pretend we
1560 * never parsed it
1561 */
1562 if (!should_process_file_prog(st->file_name, st->prog_name)) {
1563 free(st->file_name);
1564 free(st->prog_name);
1565 *stat_cntp -= 1;
1566 }
1567 }
1568
1569 if (!feof(f)) {
1570 err = -errno;
1571 fprintf(stderr, "Failed I/O for '%s': %d\n", filename, err);
1572 }
1573
1574 cleanup:
1575 fclose(f);
1576 return err;
1577 }
1578
1579 /* empty/zero stats for mismatched rows */
1580 static const struct verif_stats fallback_stats = { .file_name = "", .prog_name = "" };
1581
is_key_stat(enum stat_id id)1582 static bool is_key_stat(enum stat_id id)
1583 {
1584 return id == FILE_NAME || id == PROG_NAME;
1585 }
1586
output_comp_header_underlines(void)1587 static void output_comp_header_underlines(void)
1588 {
1589 int i, j, k;
1590
1591 for (i = 0; i < env.output_spec.spec_cnt; i++) {
1592 int id = env.output_spec.ids[i];
1593 int max_j = is_key_stat(id) ? 1 : 3;
1594
1595 for (j = 0; j < max_j; j++) {
1596 int len = env.output_spec.lens[3 * i + j];
1597
1598 printf("%s", i + j == 0 ? "" : COLUMN_SEP);
1599
1600 for (k = 0; k < len; k++)
1601 printf("%c", HEADER_CHAR);
1602 }
1603 }
1604 printf("\n");
1605 }
1606
output_comp_headers(enum resfmt fmt)1607 static void output_comp_headers(enum resfmt fmt)
1608 {
1609 static const char *table_sfxs[3] = {" (A)", " (B)", " (DIFF)"};
1610 static const char *name_sfxs[3] = {"_base", "_comp", "_diff"};
1611 int i, j, len;
1612
1613 for (i = 0; i < env.output_spec.spec_cnt; i++) {
1614 int id = env.output_spec.ids[i];
1615 /* key stats don't have A/B/DIFF columns, they are common for both data sets */
1616 int max_j = is_key_stat(id) ? 1 : 3;
1617
1618 for (j = 0; j < max_j; j++) {
1619 int *max_len = &env.output_spec.lens[3 * i + j];
1620 bool last = (i == env.output_spec.spec_cnt - 1) && (j == max_j - 1);
1621 const char *sfx;
1622
1623 switch (fmt) {
1624 case RESFMT_TABLE_CALCLEN:
1625 sfx = is_key_stat(id) ? "" : table_sfxs[j];
1626 len = snprintf(NULL, 0, "%s%s", stat_defs[id].header, sfx);
1627 if (len > *max_len)
1628 *max_len = len;
1629 break;
1630 case RESFMT_TABLE:
1631 sfx = is_key_stat(id) ? "" : table_sfxs[j];
1632 printf("%s%-*s%s", i + j == 0 ? "" : COLUMN_SEP,
1633 *max_len - (int)strlen(sfx), stat_defs[id].header, sfx);
1634 if (last)
1635 printf("\n");
1636 break;
1637 case RESFMT_CSV:
1638 sfx = is_key_stat(id) ? "" : name_sfxs[j];
1639 printf("%s%s%s", i + j == 0 ? "" : ",", stat_defs[id].names[0], sfx);
1640 if (last)
1641 printf("\n");
1642 break;
1643 }
1644 }
1645 }
1646
1647 if (fmt == RESFMT_TABLE)
1648 output_comp_header_underlines();
1649 }
1650
output_comp_stats(const struct verif_stats_join * join_stats,enum resfmt fmt,bool last)1651 static void output_comp_stats(const struct verif_stats_join *join_stats,
1652 enum resfmt fmt, bool last)
1653 {
1654 const struct verif_stats *base = join_stats->stats_a;
1655 const struct verif_stats *comp = join_stats->stats_b;
1656 char base_buf[1024] = {}, comp_buf[1024] = {}, diff_buf[1024] = {};
1657 int i;
1658
1659 for (i = 0; i < env.output_spec.spec_cnt; i++) {
1660 int id = env.output_spec.ids[i], len;
1661 int *max_len_base = &env.output_spec.lens[3 * i + 0];
1662 int *max_len_comp = &env.output_spec.lens[3 * i + 1];
1663 int *max_len_diff = &env.output_spec.lens[3 * i + 2];
1664 const char *base_str = NULL, *comp_str = NULL;
1665 long base_val = 0, comp_val = 0, diff_val = 0;
1666
1667 prepare_value(base, id, &base_str, &base_val);
1668 prepare_value(comp, id, &comp_str, &comp_val);
1669
1670 /* normalize all the outputs to be in string buffers for simplicity */
1671 if (is_key_stat(id)) {
1672 /* key stats (file and program name) are always strings */
1673 if (base)
1674 snprintf(base_buf, sizeof(base_buf), "%s", base_str);
1675 else
1676 snprintf(base_buf, sizeof(base_buf), "%s", comp_str);
1677 } else if (base_str) {
1678 snprintf(base_buf, sizeof(base_buf), "%s", base_str);
1679 snprintf(comp_buf, sizeof(comp_buf), "%s", comp_str);
1680 if (!base || !comp)
1681 snprintf(diff_buf, sizeof(diff_buf), "%s", "N/A");
1682 else if (strcmp(base_str, comp_str) == 0)
1683 snprintf(diff_buf, sizeof(diff_buf), "%s", "MATCH");
1684 else
1685 snprintf(diff_buf, sizeof(diff_buf), "%s", "MISMATCH");
1686 } else {
1687 double p = 0.0;
1688
1689 if (base)
1690 snprintf(base_buf, sizeof(base_buf), "%ld", base_val);
1691 else
1692 snprintf(base_buf, sizeof(base_buf), "%s", "N/A");
1693 if (comp)
1694 snprintf(comp_buf, sizeof(comp_buf), "%ld", comp_val);
1695 else
1696 snprintf(comp_buf, sizeof(comp_buf), "%s", "N/A");
1697
1698 diff_val = comp_val - base_val;
1699 if (!base || !comp) {
1700 snprintf(diff_buf, sizeof(diff_buf), "%s", "N/A");
1701 } else {
1702 if (base_val == 0) {
1703 if (comp_val == base_val)
1704 p = 0.0; /* avoid +0 (+100%) case */
1705 else
1706 p = comp_val < base_val ? -100.0 : 100.0;
1707 } else {
1708 p = diff_val * 100.0 / base_val;
1709 }
1710 snprintf(diff_buf, sizeof(diff_buf), "%+ld (%+.2lf%%)", diff_val, p);
1711 }
1712 }
1713
1714 switch (fmt) {
1715 case RESFMT_TABLE_CALCLEN:
1716 len = strlen(base_buf);
1717 if (len > *max_len_base)
1718 *max_len_base = len;
1719 if (!is_key_stat(id)) {
1720 len = strlen(comp_buf);
1721 if (len > *max_len_comp)
1722 *max_len_comp = len;
1723 len = strlen(diff_buf);
1724 if (len > *max_len_diff)
1725 *max_len_diff = len;
1726 }
1727 break;
1728 case RESFMT_TABLE: {
1729 /* string outputs are left-aligned, number outputs are right-aligned */
1730 const char *fmt = base_str ? "%s%-*s" : "%s%*s";
1731
1732 printf(fmt, i == 0 ? "" : COLUMN_SEP, *max_len_base, base_buf);
1733 if (!is_key_stat(id)) {
1734 printf(fmt, COLUMN_SEP, *max_len_comp, comp_buf);
1735 printf(fmt, COLUMN_SEP, *max_len_diff, diff_buf);
1736 }
1737 if (i == env.output_spec.spec_cnt - 1)
1738 printf("\n");
1739 break;
1740 }
1741 case RESFMT_CSV:
1742 printf("%s%s", i == 0 ? "" : ",", base_buf);
1743 if (!is_key_stat(id)) {
1744 printf("%s%s", i == 0 ? "" : ",", comp_buf);
1745 printf("%s%s", i == 0 ? "" : ",", diff_buf);
1746 }
1747 if (i == env.output_spec.spec_cnt - 1)
1748 printf("\n");
1749 break;
1750 }
1751 }
1752
1753 if (last && fmt == RESFMT_TABLE)
1754 output_comp_header_underlines();
1755 }
1756
cmp_stats_key(const struct verif_stats * base,const struct verif_stats * comp)1757 static int cmp_stats_key(const struct verif_stats *base, const struct verif_stats *comp)
1758 {
1759 int r;
1760
1761 r = strcmp(base->file_name, comp->file_name);
1762 if (r != 0)
1763 return r;
1764 return strcmp(base->prog_name, comp->prog_name);
1765 }
1766
is_join_stat_filter_matched(struct filter * f,const struct verif_stats_join * stats)1767 static bool is_join_stat_filter_matched(struct filter *f, const struct verif_stats_join *stats)
1768 {
1769 static const double eps = 1e-9;
1770 const char *str = NULL;
1771 double value = 0.0;
1772
1773 fetch_join_stat_value(stats, f->stat_id, f->stat_var, &str, &value);
1774
1775 if (f->abs)
1776 value = fabs(value);
1777
1778 switch (f->op) {
1779 case OP_EQ: return value > f->value - eps && value < f->value + eps;
1780 case OP_NEQ: return value < f->value - eps || value > f->value + eps;
1781 case OP_LT: return value < f->value - eps;
1782 case OP_LE: return value <= f->value + eps;
1783 case OP_GT: return value > f->value + eps;
1784 case OP_GE: return value >= f->value - eps;
1785 }
1786
1787 fprintf(stderr, "BUG: unknown filter op %d!\n", f->op);
1788 return false;
1789 }
1790
should_output_join_stats(const struct verif_stats_join * stats)1791 static bool should_output_join_stats(const struct verif_stats_join *stats)
1792 {
1793 struct filter *f;
1794 int i, allow_cnt = 0;
1795
1796 for (i = 0; i < env.deny_filter_cnt; i++) {
1797 f = &env.deny_filters[i];
1798 if (f->kind != FILTER_STAT)
1799 continue;
1800
1801 if (is_join_stat_filter_matched(f, stats))
1802 return false;
1803 }
1804
1805 for (i = 0; i < env.allow_filter_cnt; i++) {
1806 f = &env.allow_filters[i];
1807 if (f->kind != FILTER_STAT)
1808 continue;
1809 allow_cnt++;
1810
1811 if (is_join_stat_filter_matched(f, stats))
1812 return true;
1813 }
1814
1815 /* if there are no stat allowed filters, pass everything through */
1816 return allow_cnt == 0;
1817 }
1818
handle_comparison_mode(void)1819 static int handle_comparison_mode(void)
1820 {
1821 struct stat_specs base_specs = {}, comp_specs = {};
1822 struct stat_specs tmp_sort_spec;
1823 enum resfmt cur_fmt;
1824 int err, i, j, last_idx, cnt;
1825
1826 if (env.filename_cnt != 2) {
1827 fprintf(stderr, "Comparison mode expects exactly two input CSV files!\n\n");
1828 argp_help(&argp, stderr, ARGP_HELP_USAGE, "veristat");
1829 return -EINVAL;
1830 }
1831
1832 err = parse_stats_csv(env.filenames[0], &base_specs,
1833 &env.baseline_stats, &env.baseline_stat_cnt);
1834 if (err) {
1835 fprintf(stderr, "Failed to parse stats from '%s': %d\n", env.filenames[0], err);
1836 return err;
1837 }
1838 err = parse_stats_csv(env.filenames[1], &comp_specs,
1839 &env.prog_stats, &env.prog_stat_cnt);
1840 if (err) {
1841 fprintf(stderr, "Failed to parse stats from '%s': %d\n", env.filenames[1], err);
1842 return err;
1843 }
1844
1845 /* To keep it simple we validate that the set and order of stats in
1846 * both CSVs are exactly the same. This can be lifted with a bit more
1847 * pre-processing later.
1848 */
1849 if (base_specs.spec_cnt != comp_specs.spec_cnt) {
1850 fprintf(stderr, "Number of stats in '%s' and '%s' differs (%d != %d)!\n",
1851 env.filenames[0], env.filenames[1],
1852 base_specs.spec_cnt, comp_specs.spec_cnt);
1853 return -EINVAL;
1854 }
1855 for (i = 0; i < base_specs.spec_cnt; i++) {
1856 if (base_specs.ids[i] != comp_specs.ids[i]) {
1857 fprintf(stderr, "Stats composition differs between '%s' and '%s' (%s != %s)!\n",
1858 env.filenames[0], env.filenames[1],
1859 stat_defs[base_specs.ids[i]].names[0],
1860 stat_defs[comp_specs.ids[i]].names[0]);
1861 return -EINVAL;
1862 }
1863 }
1864
1865 /* Replace user-specified sorting spec with file+prog sorting rule to
1866 * be able to join two datasets correctly. Once we are done, we will
1867 * restore the original sort spec.
1868 */
1869 tmp_sort_spec = env.sort_spec;
1870 env.sort_spec = join_sort_spec;
1871 qsort(env.prog_stats, env.prog_stat_cnt, sizeof(*env.prog_stats), cmp_prog_stats);
1872 qsort(env.baseline_stats, env.baseline_stat_cnt, sizeof(*env.baseline_stats), cmp_prog_stats);
1873 env.sort_spec = tmp_sort_spec;
1874
1875 /* Join two datasets together. If baseline and comparison datasets
1876 * have different subset of rows (we match by 'object + prog' as
1877 * a unique key) then assume empty/missing/zero value for rows that
1878 * are missing in the opposite data set.
1879 */
1880 i = j = 0;
1881 while (i < env.baseline_stat_cnt || j < env.prog_stat_cnt) {
1882 const struct verif_stats *base, *comp;
1883 struct verif_stats_join *join;
1884 void *tmp;
1885 int r;
1886
1887 base = i < env.baseline_stat_cnt ? &env.baseline_stats[i] : &fallback_stats;
1888 comp = j < env.prog_stat_cnt ? &env.prog_stats[j] : &fallback_stats;
1889
1890 if (!base->file_name || !base->prog_name) {
1891 fprintf(stderr, "Entry #%d in '%s' doesn't have file and/or program name specified!\n",
1892 i, env.filenames[0]);
1893 return -EINVAL;
1894 }
1895 if (!comp->file_name || !comp->prog_name) {
1896 fprintf(stderr, "Entry #%d in '%s' doesn't have file and/or program name specified!\n",
1897 j, env.filenames[1]);
1898 return -EINVAL;
1899 }
1900
1901 tmp = realloc(env.join_stats, (env.join_stat_cnt + 1) * sizeof(*env.join_stats));
1902 if (!tmp)
1903 return -ENOMEM;
1904 env.join_stats = tmp;
1905
1906 join = &env.join_stats[env.join_stat_cnt];
1907 memset(join, 0, sizeof(*join));
1908
1909 r = cmp_stats_key(base, comp);
1910 if (r == 0) {
1911 join->file_name = base->file_name;
1912 join->prog_name = base->prog_name;
1913 join->stats_a = base;
1914 join->stats_b = comp;
1915 i++;
1916 j++;
1917 } else if (base != &fallback_stats && (comp == &fallback_stats || r < 0)) {
1918 join->file_name = base->file_name;
1919 join->prog_name = base->prog_name;
1920 join->stats_a = base;
1921 join->stats_b = NULL;
1922 i++;
1923 } else if (comp != &fallback_stats && (base == &fallback_stats || r > 0)) {
1924 join->file_name = comp->file_name;
1925 join->prog_name = comp->prog_name;
1926 join->stats_a = NULL;
1927 join->stats_b = comp;
1928 j++;
1929 } else {
1930 fprintf(stderr, "%s:%d: should never reach here i=%i, j=%i",
1931 __FILE__, __LINE__, i, j);
1932 return -EINVAL;
1933 }
1934 env.join_stat_cnt += 1;
1935 }
1936
1937 /* now sort joined results according to sort spec */
1938 qsort(env.join_stats, env.join_stat_cnt, sizeof(*env.join_stats), cmp_join_stats);
1939
1940 /* for human-readable table output we need to do extra pass to
1941 * calculate column widths, so we substitute current output format
1942 * with RESFMT_TABLE_CALCLEN and later revert it back to RESFMT_TABLE
1943 * and do everything again.
1944 */
1945 if (env.out_fmt == RESFMT_TABLE)
1946 cur_fmt = RESFMT_TABLE_CALCLEN;
1947 else
1948 cur_fmt = env.out_fmt;
1949
1950 one_more_time:
1951 output_comp_headers(cur_fmt);
1952
1953 last_idx = -1;
1954 cnt = 0;
1955 for (i = 0; i < env.join_stat_cnt; i++) {
1956 const struct verif_stats_join *join = &env.join_stats[i];
1957
1958 if (!should_output_join_stats(join))
1959 continue;
1960
1961 if (env.top_n && cnt >= env.top_n)
1962 break;
1963
1964 if (cur_fmt == RESFMT_TABLE_CALCLEN)
1965 last_idx = i;
1966
1967 output_comp_stats(join, cur_fmt, i == last_idx);
1968
1969 cnt++;
1970 }
1971
1972 if (cur_fmt == RESFMT_TABLE_CALCLEN) {
1973 cur_fmt = RESFMT_TABLE;
1974 goto one_more_time; /* ... this time with feeling */
1975 }
1976
1977 return 0;
1978 }
1979
is_stat_filter_matched(struct filter * f,const struct verif_stats * stats)1980 static bool is_stat_filter_matched(struct filter *f, const struct verif_stats *stats)
1981 {
1982 long value = stats->stats[f->stat_id];
1983
1984 if (f->abs)
1985 value = value < 0 ? -value : value;
1986
1987 switch (f->op) {
1988 case OP_EQ: return value == f->value;
1989 case OP_NEQ: return value != f->value;
1990 case OP_LT: return value < f->value;
1991 case OP_LE: return value <= f->value;
1992 case OP_GT: return value > f->value;
1993 case OP_GE: return value >= f->value;
1994 }
1995
1996 fprintf(stderr, "BUG: unknown filter op %d!\n", f->op);
1997 return false;
1998 }
1999
should_output_stats(const struct verif_stats * stats)2000 static bool should_output_stats(const struct verif_stats *stats)
2001 {
2002 struct filter *f;
2003 int i, allow_cnt = 0;
2004
2005 for (i = 0; i < env.deny_filter_cnt; i++) {
2006 f = &env.deny_filters[i];
2007 if (f->kind != FILTER_STAT)
2008 continue;
2009
2010 if (is_stat_filter_matched(f, stats))
2011 return false;
2012 }
2013
2014 for (i = 0; i < env.allow_filter_cnt; i++) {
2015 f = &env.allow_filters[i];
2016 if (f->kind != FILTER_STAT)
2017 continue;
2018 allow_cnt++;
2019
2020 if (is_stat_filter_matched(f, stats))
2021 return true;
2022 }
2023
2024 /* if there are no stat allowed filters, pass everything through */
2025 return allow_cnt == 0;
2026 }
2027
output_prog_stats(void)2028 static void output_prog_stats(void)
2029 {
2030 const struct verif_stats *stats;
2031 int i, last_stat_idx = 0, cnt = 0;
2032
2033 if (env.out_fmt == RESFMT_TABLE) {
2034 /* calculate column widths */
2035 output_headers(RESFMT_TABLE_CALCLEN);
2036 for (i = 0; i < env.prog_stat_cnt; i++) {
2037 stats = &env.prog_stats[i];
2038 if (!should_output_stats(stats))
2039 continue;
2040 output_stats(stats, RESFMT_TABLE_CALCLEN, false);
2041 last_stat_idx = i;
2042 }
2043 }
2044
2045 /* actually output the table */
2046 output_headers(env.out_fmt);
2047 for (i = 0; i < env.prog_stat_cnt; i++) {
2048 stats = &env.prog_stats[i];
2049 if (!should_output_stats(stats))
2050 continue;
2051 if (env.top_n && cnt >= env.top_n)
2052 break;
2053 output_stats(stats, env.out_fmt, i == last_stat_idx);
2054 cnt++;
2055 }
2056 }
2057
handle_verif_mode(void)2058 static int handle_verif_mode(void)
2059 {
2060 int i, err;
2061
2062 if (env.filename_cnt == 0) {
2063 fprintf(stderr, "Please provide path to BPF object file!\n\n");
2064 argp_help(&argp, stderr, ARGP_HELP_USAGE, "veristat");
2065 return -EINVAL;
2066 }
2067
2068 for (i = 0; i < env.filename_cnt; i++) {
2069 err = process_obj(env.filenames[i]);
2070 if (err) {
2071 fprintf(stderr, "Failed to process '%s': %d\n", env.filenames[i], err);
2072 return err;
2073 }
2074 }
2075
2076 qsort(env.prog_stats, env.prog_stat_cnt, sizeof(*env.prog_stats), cmp_prog_stats);
2077
2078 output_prog_stats();
2079
2080 return 0;
2081 }
2082
handle_replay_mode(void)2083 static int handle_replay_mode(void)
2084 {
2085 struct stat_specs specs = {};
2086 int err;
2087
2088 if (env.filename_cnt != 1) {
2089 fprintf(stderr, "Replay mode expects exactly one input CSV file!\n\n");
2090 argp_help(&argp, stderr, ARGP_HELP_USAGE, "veristat");
2091 return -EINVAL;
2092 }
2093
2094 err = parse_stats_csv(env.filenames[0], &specs,
2095 &env.prog_stats, &env.prog_stat_cnt);
2096 if (err) {
2097 fprintf(stderr, "Failed to parse stats from '%s': %d\n", env.filenames[0], err);
2098 return err;
2099 }
2100
2101 qsort(env.prog_stats, env.prog_stat_cnt, sizeof(*env.prog_stats), cmp_prog_stats);
2102
2103 output_prog_stats();
2104
2105 return 0;
2106 }
2107
main(int argc,char ** argv)2108 int main(int argc, char **argv)
2109 {
2110 int err = 0, i;
2111
2112 if (argp_parse(&argp, argc, argv, 0, NULL, NULL))
2113 return 1;
2114
2115 if (env.show_version) {
2116 printf("%s\n", argp_program_version);
2117 return 0;
2118 }
2119
2120 if (env.verbose && env.quiet) {
2121 fprintf(stderr, "Verbose and quiet modes are incompatible, please specify just one or neither!\n\n");
2122 argp_help(&argp, stderr, ARGP_HELP_USAGE, "veristat");
2123 return 1;
2124 }
2125 if (env.verbose && env.log_level == 0)
2126 env.log_level = 1;
2127
2128 if (env.output_spec.spec_cnt == 0) {
2129 if (env.out_fmt == RESFMT_CSV)
2130 env.output_spec = default_csv_output_spec;
2131 else
2132 env.output_spec = default_output_spec;
2133 }
2134 if (env.sort_spec.spec_cnt == 0)
2135 env.sort_spec = default_sort_spec;
2136
2137 if (env.comparison_mode && env.replay_mode) {
2138 fprintf(stderr, "Can't specify replay and comparison mode at the same time!\n\n");
2139 argp_help(&argp, stderr, ARGP_HELP_USAGE, "veristat");
2140 return 1;
2141 }
2142
2143 if (env.comparison_mode)
2144 err = handle_comparison_mode();
2145 else if (env.replay_mode)
2146 err = handle_replay_mode();
2147 else
2148 err = handle_verif_mode();
2149
2150 free_verif_stats(env.prog_stats, env.prog_stat_cnt);
2151 free_verif_stats(env.baseline_stats, env.baseline_stat_cnt);
2152 free(env.join_stats);
2153 for (i = 0; i < env.filename_cnt; i++)
2154 free(env.filenames[i]);
2155 free(env.filenames);
2156 for (i = 0; i < env.allow_filter_cnt; i++) {
2157 free(env.allow_filters[i].any_glob);
2158 free(env.allow_filters[i].file_glob);
2159 free(env.allow_filters[i].prog_glob);
2160 }
2161 free(env.allow_filters);
2162 for (i = 0; i < env.deny_filter_cnt; i++) {
2163 free(env.deny_filters[i].any_glob);
2164 free(env.deny_filters[i].file_glob);
2165 free(env.deny_filters[i].prog_glob);
2166 }
2167 free(env.deny_filters);
2168 return -err;
2169 }
2170