1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2022 Meta Platforms, Inc. and affiliates. */
7 #include <linux/compiler.h>
15 #include <sys/sysinfo.h>
17 #include <bpf/libbpf.h>
35 NUM_STATS_CNT = FILE_NAME - VERDICT,
38 /* In comparison mode each stat can specify up to four different values:
41 * - absolute diff value;
42 * - relative (percentage) diff value.
44 * When specifying stat specs in comparison mode, user can use one of the
45 * following variant suffixes to specify which exact variant should be used for
46 * ordering or filtering:
47 * - `_a` for A side value;
48 * - `_b` for B side value;
49 * - `_diff` for absolute diff value;
50 * - `_pct` for relative (percentage) diff value.
52 * If no variant suffix is provided, then `_b` (control data) is assumed.
54 * As an example, let's say instructions stat has the following output:
56 * Insns (A) Insns (B) Insns (DIFF)
57 * --------- --------- --------------
58 * 21547 20920 -627 (-2.91%)
61 * - 21547 is A side value (insns_a);
62 * - 20920 is B side value (insns_b);
63 * - -627 is absolute diff value (insns_diff);
64 * - -2.91% is relative diff value (insns_pct).
66 * For verdict there is no verdict_pct variant.
67 * For file and program name, _a and _b variants are equivalent and there are
68 * no _diff or _pct variants.
81 long stats[NUM_STATS_CNT];
84 /* joined comparison mode stats */
85 struct verif_stats_join {
89 const struct verif_stats *stats_a;
90 const struct verif_stats *stats_b;
95 enum stat_id ids[ALL_STATS_CNT];
96 enum stat_variant variants[ALL_STATS_CNT];
97 bool asc[ALL_STATS_CNT];
98 int lens[ALL_STATS_CNT * 3]; /* 3x for comparison mode */
103 RESFMT_TABLE_CALCLEN, /* fake format to pre-calculate table's column widths */
114 OP_NEQ, /* != or <> */
122 enum filter_kind kind;
128 enum operator_kind op;
130 enum stat_variant stat_var;
141 bool comparison_mode;
144 struct verif_stats *prog_stats;
147 /* baseline_stats is allocated and used only in comparison mode */
148 struct verif_stats *baseline_stats;
149 int baseline_stat_cnt;
151 struct verif_stats_join *join_stats;
154 struct stat_specs output_spec;
155 struct stat_specs sort_spec;
157 struct filter *allow_filters;
158 struct filter *deny_filters;
159 int allow_filter_cnt;
168 static int libbpf_print_fn(enum libbpf_print_level level, const char *format, va_list args)
172 if (level == LIBBPF_DEBUG /* && !env.verbose */)
174 return vfprintf(stderr, format, args);
177 const char *argp_program_version = "veristat";
178 const char *argp_program_bug_address = "<bpf@vger.kernel.org>";
179 const char argp_program_doc[] =
180 "veristat BPF verifier stats collection and comparison tool.\n"
182 "USAGE: veristat <obj-file> [<obj-file>...]\n"
183 " OR: veristat -C <baseline.csv> <comparison.csv>\n";
185 static const struct argp_option opts[] = {
186 { NULL, 'h', NULL, OPTION_HIDDEN, "Show the full help" },
187 { "verbose", 'v', NULL, 0, "Verbose mode" },
188 { "log-level", 'l', "LEVEL", 0, "Verifier log level (default 0 for normal mode, 1 for verbose mode)" },
189 { "quiet", 'q', NULL, 0, "Quiet mode" },
190 { "emit", 'e', "SPEC", 0, "Specify stats to be emitted" },
191 { "sort", 's', "SPEC", 0, "Specify sort order" },
192 { "output-format", 'o', "FMT", 0, "Result output format (table, csv), default is table." },
193 { "compare", 'C', NULL, 0, "Comparison mode" },
194 { "replay", 'R', NULL, 0, "Replay mode" },
195 { "filter", 'f', "FILTER", 0, "Filter expressions (or @filename for file with expressions)." },
199 static int parse_stats(const char *stats_str, struct stat_specs *specs);
200 static int append_filter(struct filter **filters, int *cnt, const char *str);
201 static int append_filter_file(const char *path);
203 static error_t parse_arg(int key, char *arg, struct argp_state *state)
210 argp_state_help(state, stderr, ARGP_HELP_STD_HELP);
219 err = parse_stats(arg, &env.output_spec);
224 err = parse_stats(arg, &env.sort_spec);
229 if (strcmp(arg, "table") == 0) {
230 env.out_fmt = RESFMT_TABLE;
231 } else if (strcmp(arg, "csv") == 0) {
232 env.out_fmt = RESFMT_CSV;
234 fprintf(stderr, "Unrecognized output format '%s'\n", arg);
240 env.log_level = strtol(arg, NULL, 10);
242 fprintf(stderr, "invalid log level: %s\n", arg);
247 env.comparison_mode = true;
250 env.replay_mode = true;
254 err = append_filter_file(arg + 1);
255 else if (arg[0] == '!')
256 err = append_filter(&env.deny_filters, &env.deny_filter_cnt, arg + 1);
258 err = append_filter(&env.allow_filters, &env.allow_filter_cnt, arg);
260 fprintf(stderr, "Failed to collect program filter expressions: %d\n", err);
265 tmp = realloc(env.filenames, (env.filename_cnt + 1) * sizeof(*env.filenames));
269 env.filenames[env.filename_cnt] = strdup(arg);
270 if (!env.filenames[env.filename_cnt])
275 return ARGP_ERR_UNKNOWN;
280 static const struct argp argp = {
283 .doc = argp_program_doc,
287 /* Adapted from perf/util/string.c */
288 static bool glob_matches(const char *str, const char *pat)
290 while (*str && *pat && *pat != '*') {
296 /* Check wild card */
300 if (!*pat) /* Tail wild card matches all */
303 if (glob_matches(str++, pat))
306 return !*str && !*pat;
309 static bool is_bpf_obj_file(const char *path) {
311 int fd, err = -EINVAL;
314 fd = open(path, O_RDONLY | O_CLOEXEC);
316 return true; /* we'll fail later and propagate error */
318 /* ensure libelf is initialized */
319 (void)elf_version(EV_CURRENT);
321 elf = elf_begin(fd, ELF_C_READ, NULL);
325 if (elf_kind(elf) != ELF_K_ELF || gelf_getclass(elf) != ELFCLASS64)
328 ehdr = elf64_getehdr(elf);
329 /* Old LLVM set e_machine to EM_NONE */
330 if (!ehdr || ehdr->e_type != ET_REL || (ehdr->e_machine && ehdr->e_machine != EM_BPF))
341 static bool should_process_file_prog(const char *filename, const char *prog_name)
344 int i, allow_cnt = 0;
346 for (i = 0; i < env.deny_filter_cnt; i++) {
347 f = &env.deny_filters[i];
348 if (f->kind != FILTER_NAME)
351 if (f->any_glob && glob_matches(filename, f->any_glob))
353 if (f->any_glob && prog_name && glob_matches(prog_name, f->any_glob))
355 if (f->file_glob && glob_matches(filename, f->file_glob))
357 if (f->prog_glob && prog_name && glob_matches(prog_name, f->prog_glob))
361 for (i = 0; i < env.allow_filter_cnt; i++) {
362 f = &env.allow_filters[i];
363 if (f->kind != FILTER_NAME)
368 if (glob_matches(filename, f->any_glob))
370 /* If we don't know program name yet, any_glob filter
371 * has to assume that current BPF object file might be
372 * relevant; we'll check again later on after opening
373 * BPF object file, at which point program name will
376 if (!prog_name || glob_matches(prog_name, f->any_glob))
379 if (f->file_glob && !glob_matches(filename, f->file_glob))
381 if (f->prog_glob && prog_name && !glob_matches(prog_name, f->prog_glob))
387 /* if there are no file/prog name allow filters, allow all progs,
388 * unless they are denied earlier explicitly
390 return allow_cnt == 0;
394 enum operator_kind op_kind;
397 /* Order of these definitions matter to avoid situations like '<'
398 * matching part of what is actually a '<>' operator. That is,
399 * substrings should go last.
411 static bool parse_stat_id_var(const char *name, size_t len, int *id, enum stat_variant *var);
413 static int append_filter(struct filter **filters, int *cnt, const char *str)
420 tmp = realloc(*filters, (*cnt + 1) * sizeof(**filters));
425 f = &(*filters)[*cnt];
426 memset(f, 0, sizeof(*f));
428 /* First, let's check if it's a stats filter of the following form:
429 * <stat><op><value, where:
430 * - <stat> is one of supported numerical stats (verdict is also
431 * considered numerical, failure == 0, success == 1);
432 * - <op> is comparison operator (see `operators` definitions);
433 * - <value> is an integer (or failure/success, or false/true as
434 * special aliases for 0 and 1, respectively).
435 * If the form doesn't match what user provided, we assume file/prog
438 for (i = 0; i < ARRAY_SIZE(operators); i++) {
439 enum stat_variant var;
442 const char *end = str;
445 op_str = operators[i].op_str;
446 p = strstr(str, op_str);
450 if (!parse_stat_id_var(str, p - str, &id, &var)) {
451 fprintf(stderr, "Unrecognized stat name in '%s'!\n", str);
454 if (id >= FILE_NAME) {
455 fprintf(stderr, "Non-integer stat is specified in '%s'!\n", str);
461 if (strcasecmp(p, "true") == 0 ||
462 strcasecmp(p, "t") == 0 ||
463 strcasecmp(p, "success") == 0 ||
464 strcasecmp(p, "succ") == 0 ||
465 strcasecmp(p, "s") == 0 ||
466 strcasecmp(p, "match") == 0 ||
467 strcasecmp(p, "m") == 0) {
469 } else if (strcasecmp(p, "false") == 0 ||
470 strcasecmp(p, "f") == 0 ||
471 strcasecmp(p, "failure") == 0 ||
472 strcasecmp(p, "fail") == 0 ||
473 strcasecmp(p, "mismatch") == 0 ||
474 strcasecmp(p, "mis") == 0) {
478 val = strtol(p, (char **)&end, 10);
479 if (errno || end == p || *end != '\0' ) {
480 fprintf(stderr, "Invalid integer value in '%s'!\n", str);
485 f->kind = FILTER_STAT;
488 f->op = operators[i].op_kind;
495 /* File/prog filter can be specified either as '<glob>' or
496 * '<file-glob>/<prog-glob>'. In the former case <glob> is applied to
497 * both file and program names. This seems to be way more useful in
498 * practice. If user needs full control, they can use '/<prog-glob>'
499 * form to glob just program name, or '<file-glob>/' to glob only file
500 * name. But usually common <glob> seems to be the most useful and
503 f->kind = FILTER_NAME;
504 p = strchr(str, '/');
506 f->any_glob = strdup(str);
511 /* non-empty file glob */
512 f->file_glob = strndup(str, p - str);
516 if (strlen(p + 1) > 0) {
517 /* non-empty prog glob */
518 f->prog_glob = strdup(p + 1);
531 static int append_filter_file(const char *path)
537 f = fopen(path, "r");
540 fprintf(stderr, "Failed to open filters in '%s': %d\n", path, err);
544 while (fscanf(f, " %1023[^\n]\n", buf) == 1) {
545 /* lines starting with # are comments, skip them */
546 if (buf[0] == '\0' || buf[0] == '#')
548 /* lines starting with ! are negative match filters */
550 err = append_filter(&env.deny_filters, &env.deny_filter_cnt, buf + 1);
552 err = append_filter(&env.allow_filters, &env.allow_filter_cnt, buf);
562 static const struct stat_specs default_output_spec = {
565 FILE_NAME, PROG_NAME, VERDICT, DURATION,
566 TOTAL_INSNS, TOTAL_STATES, PEAK_STATES,
570 static const struct stat_specs default_csv_output_spec = {
573 FILE_NAME, PROG_NAME, VERDICT, DURATION,
574 TOTAL_INSNS, TOTAL_STATES, PEAK_STATES,
575 MAX_STATES_PER_INSN, MARK_READ_MAX_LEN,
579 static const struct stat_specs default_sort_spec = {
582 FILE_NAME, PROG_NAME,
584 .asc = { true, true, },
587 /* sorting for comparison mode to join two data sets */
588 static const struct stat_specs join_sort_spec = {
591 FILE_NAME, PROG_NAME,
593 .asc = { true, true, },
596 static struct stat_def {
598 const char *names[4];
602 [FILE_NAME] = { "File", {"file_name", "filename", "file"}, true /* asc */, true /* left */ },
603 [PROG_NAME] = { "Program", {"prog_name", "progname", "prog"}, true /* asc */, true /* left */ },
604 [VERDICT] = { "Verdict", {"verdict"}, true /* asc: failure, success */, true /* left */ },
605 [DURATION] = { "Duration (us)", {"duration", "dur"}, },
606 [TOTAL_INSNS] = { "Insns", {"total_insns", "insns"}, },
607 [TOTAL_STATES] = { "States", {"total_states", "states"}, },
608 [PEAK_STATES] = { "Peak states", {"peak_states"}, },
609 [MAX_STATES_PER_INSN] = { "Max states per insn", {"max_states_per_insn"}, },
610 [MARK_READ_MAX_LEN] = { "Max mark read length", {"max_mark_read_len", "mark_read"}, },
613 static bool parse_stat_id_var(const char *name, size_t len, int *id, enum stat_variant *var)
615 static const char *var_sfxs[] = {
618 [VARIANT_DIFF] = "_diff",
619 [VARIANT_PCT] = "_pct",
623 for (i = 0; i < ARRAY_SIZE(stat_defs); i++) {
624 struct stat_def *def = &stat_defs[i];
625 size_t alias_len, sfx_len;
628 for (j = 0; j < ARRAY_SIZE(stat_defs[i].names); j++) {
629 alias = def->names[j];
633 alias_len = strlen(alias);
634 if (strncmp(name, alias, alias_len) != 0)
637 if (alias_len == len) {
638 /* If no variant suffix is specified, we
639 * assume control group (just in case we are
640 * in comparison mode. Variant is ignored in
641 * non-comparison mode.
648 for (k = 0; k < ARRAY_SIZE(var_sfxs); k++) {
649 sfx_len = strlen(var_sfxs[k]);
650 if (alias_len + sfx_len != len)
653 if (strncmp(name + alias_len, var_sfxs[k], sfx_len) == 0) {
654 *var = (enum stat_variant)k;
665 static bool is_asc_sym(char c)
670 static bool is_desc_sym(char c)
672 return c == 'v' || c == 'V' || c == '.' || c == '!' || c == '_';
675 static int parse_stat(const char *stat_name, struct stat_specs *specs)
678 bool has_order = false, is_asc = false;
679 size_t len = strlen(stat_name);
680 enum stat_variant var;
682 if (specs->spec_cnt >= ARRAY_SIZE(specs->ids)) {
683 fprintf(stderr, "Can't specify more than %zd stats\n", ARRAY_SIZE(specs->ids));
687 if (len > 1 && (is_asc_sym(stat_name[len - 1]) || is_desc_sym(stat_name[len - 1]))) {
689 is_asc = is_asc_sym(stat_name[len - 1]);
693 if (!parse_stat_id_var(stat_name, len, &id, &var)) {
694 fprintf(stderr, "Unrecognized stat name '%s'\n", stat_name);
698 specs->ids[specs->spec_cnt] = id;
699 specs->variants[specs->spec_cnt] = var;
700 specs->asc[specs->spec_cnt] = has_order ? is_asc : stat_defs[id].asc_by_default;
706 static int parse_stats(const char *stats_str, struct stat_specs *specs)
708 char *input, *state = NULL, *next;
711 input = strdup(stats_str);
715 while ((next = strtok_r(state ? NULL : input, ",", &state))) {
716 err = parse_stat(next, specs);
724 static void free_verif_stats(struct verif_stats *stats, size_t stat_cnt)
731 for (i = 0; i < stat_cnt; i++) {
732 free(stats[i].file_name);
733 free(stats[i].prog_name);
738 static char verif_log_buf[64 * 1024];
740 #define MAX_PARSED_LOG_LINES 100
742 static int parse_verif_log(char * const buf, size_t buf_sz, struct verif_stats *s)
747 buf[buf_sz - 1] = '\0';
749 for (pos = strlen(buf) - 1, lines = 0; pos >= 0 && lines < MAX_PARSED_LOG_LINES; lines++) {
750 /* find previous endline or otherwise take the start of log buf */
751 for (cur = &buf[pos]; cur > buf && cur[0] != '\n'; cur--, pos--) {
753 /* next time start from end of previous line (or pos goes to <0) */
755 /* if we found endline, point right after endline symbol;
756 * otherwise, stay at the beginning of log buf
761 if (1 == sscanf(cur, "verification time %ld usec\n", &s->stats[DURATION]))
763 if (6 == sscanf(cur, "processed %ld insns (limit %*d) max_states_per_insn %ld total_states %ld peak_states %ld mark_read %ld",
764 &s->stats[TOTAL_INSNS],
765 &s->stats[MAX_STATES_PER_INSN],
766 &s->stats[TOTAL_STATES],
767 &s->stats[PEAK_STATES],
768 &s->stats[MARK_READ_MAX_LEN]))
775 static void fixup_obj(struct bpf_object *obj)
779 bpf_object__for_each_map(map, obj) {
780 /* disable pinning */
781 bpf_map__set_pin_path(map, NULL);
783 /* fix up map size, if necessary */
784 switch (bpf_map__type(map)) {
785 case BPF_MAP_TYPE_SK_STORAGE:
786 case BPF_MAP_TYPE_TASK_STORAGE:
787 case BPF_MAP_TYPE_INODE_STORAGE:
788 case BPF_MAP_TYPE_CGROUP_STORAGE:
791 if (bpf_map__max_entries(map) == 0)
792 bpf_map__set_max_entries(map, 1);
797 static int process_prog(const char *filename, struct bpf_object *obj, struct bpf_program *prog)
799 const char *prog_name = bpf_program__name(prog);
800 size_t buf_sz = sizeof(verif_log_buf);
801 char *buf = verif_log_buf;
802 struct verif_stats *stats;
806 if (!should_process_file_prog(basename(filename), bpf_program__name(prog))) {
811 tmp = realloc(env.prog_stats, (env.prog_stat_cnt + 1) * sizeof(*env.prog_stats));
814 env.prog_stats = tmp;
815 stats = &env.prog_stats[env.prog_stat_cnt++];
816 memset(stats, 0, sizeof(*stats));
819 buf_sz = 16 * 1024 * 1024;
820 buf = malloc(buf_sz);
823 bpf_program__set_log_buf(prog, buf, buf_sz);
824 bpf_program__set_log_level(prog, env.log_level | 4); /* stats + log */
826 bpf_program__set_log_buf(prog, buf, buf_sz);
827 bpf_program__set_log_level(prog, 4); /* only verifier stats */
829 verif_log_buf[0] = '\0';
831 /* increase chances of successful BPF object loading */
834 err = bpf_object__load(obj);
835 env.progs_processed++;
837 stats->file_name = strdup(basename(filename));
838 stats->prog_name = strdup(bpf_program__name(prog));
839 stats->stats[VERDICT] = err == 0; /* 1 - success, 0 - failure */
840 parse_verif_log(buf, buf_sz, stats);
843 printf("PROCESSING %s/%s, DURATION US: %ld, VERDICT: %s, VERIFIER LOG:\n%s\n",
844 filename, prog_name, stats->stats[DURATION],
845 err ? "failure" : "success", buf);
848 if (verif_log_buf != buf)
854 static int process_obj(const char *filename)
856 struct bpf_object *obj = NULL, *tobj;
857 struct bpf_program *prog, *tprog, *lprog;
858 libbpf_print_fn_t old_libbpf_print_fn;
859 LIBBPF_OPTS(bpf_object_open_opts, opts);
860 int err = 0, prog_cnt = 0;
862 if (!should_process_file_prog(basename(filename), NULL)) {
864 printf("Skipping '%s' due to filters...\n", filename);
868 if (!is_bpf_obj_file(filename)) {
870 printf("Skipping '%s' as it's not a BPF object file...\n", filename);
875 if (!env.quiet && env.out_fmt == RESFMT_TABLE)
876 printf("Processing '%s'...\n", basename(filename));
878 old_libbpf_print_fn = libbpf_set_print(libbpf_print_fn);
879 obj = bpf_object__open_file(filename, &opts);
881 /* if libbpf can't open BPF object file, it could be because
882 * that BPF object file is incomplete and has to be statically
883 * linked into a final BPF object file; instead of bailing
884 * out, report it into stderr, mark it as skipped, and
887 fprintf(stderr, "Failed to open '%s': %d\n", filename, -errno);
893 env.files_processed++;
895 bpf_object__for_each_program(prog, obj) {
900 prog = bpf_object__next_program(obj, NULL);
901 bpf_program__set_autoload(prog, true);
902 process_prog(filename, obj, prog);
906 bpf_object__for_each_program(prog, obj) {
907 const char *prog_name = bpf_program__name(prog);
909 tobj = bpf_object__open_file(filename, &opts);
912 fprintf(stderr, "Failed to open '%s': %d\n", filename, err);
916 bpf_object__for_each_program(tprog, tobj) {
917 const char *tprog_name = bpf_program__name(tprog);
919 if (strcmp(prog_name, tprog_name) == 0) {
920 bpf_program__set_autoload(tprog, true);
923 bpf_program__set_autoload(tprog, false);
927 process_prog(filename, tobj, lprog);
928 bpf_object__close(tobj);
932 bpf_object__close(obj);
933 libbpf_set_print(old_libbpf_print_fn);
937 static int cmp_stat(const struct verif_stats *s1, const struct verif_stats *s2,
938 enum stat_id id, bool asc)
944 cmp = strcmp(s1->file_name, s2->file_name);
947 cmp = strcmp(s1->prog_name, s2->prog_name);
954 case MAX_STATES_PER_INSN:
955 case MARK_READ_MAX_LEN: {
956 long v1 = s1->stats[id];
957 long v2 = s2->stats[id];
960 cmp = v1 < v2 ? -1 : 1;
964 fprintf(stderr, "Unrecognized stat #%d\n", id);
968 return asc ? cmp : -cmp;
971 static int cmp_prog_stats(const void *v1, const void *v2)
973 const struct verif_stats *s1 = v1, *s2 = v2;
976 for (i = 0; i < env.sort_spec.spec_cnt; i++) {
977 cmp = cmp_stat(s1, s2, env.sort_spec.ids[i], env.sort_spec.asc[i]);
982 /* always disambiguate with file+prog, which are unique */
983 cmp = strcmp(s1->file_name, s2->file_name);
986 return strcmp(s1->prog_name, s2->prog_name);
989 static void fetch_join_stat_value(const struct verif_stats_join *s,
990 enum stat_id id, enum stat_variant var,
991 const char **str_val,
996 if (id == FILE_NAME) {
997 *str_val = s->file_name;
1000 if (id == PROG_NAME) {
1001 *str_val = s->prog_name;
1005 v1 = s->stats_a ? s->stats_a->stats[id] : 0;
1006 v2 = s->stats_b ? s->stats_b->stats[id] : 0;
1011 *num_val = -DBL_MAX;
1013 *num_val = s->stats_a->stats[id];
1017 *num_val = -DBL_MAX;
1019 *num_val = s->stats_b->stats[id];
1022 if (!s->stats_a || !s->stats_b)
1023 *num_val = -DBL_MAX;
1024 else if (id == VERDICT)
1025 *num_val = v1 == v2 ? 1.0 /* MATCH */ : 0.0 /* MISMATCH */;
1027 *num_val = (double)(v2 - v1);
1030 if (!s->stats_a || !s->stats_b) {
1031 *num_val = -DBL_MAX;
1032 } else if (v1 == 0) {
1036 *num_val = v2 < v1 ? -100.0 : 100.0;
1038 *num_val = (v2 - v1) * 100.0 / v1;
1044 static int cmp_join_stat(const struct verif_stats_join *s1,
1045 const struct verif_stats_join *s2,
1046 enum stat_id id, enum stat_variant var, bool asc)
1048 const char *str1 = NULL, *str2 = NULL;
1052 fetch_join_stat_value(s1, id, var, &str1, &v1);
1053 fetch_join_stat_value(s2, id, var, &str2, &v2);
1056 cmp = strcmp(str1, str2);
1058 cmp = v1 < v2 ? -1 : 1;
1060 return asc ? cmp : -cmp;
1063 static int cmp_join_stats(const void *v1, const void *v2)
1065 const struct verif_stats_join *s1 = v1, *s2 = v2;
1068 for (i = 0; i < env.sort_spec.spec_cnt; i++) {
1069 cmp = cmp_join_stat(s1, s2,
1070 env.sort_spec.ids[i],
1071 env.sort_spec.variants[i],
1072 env.sort_spec.asc[i]);
1077 /* always disambiguate with file+prog, which are unique */
1078 cmp = strcmp(s1->file_name, s2->file_name);
1081 return strcmp(s1->prog_name, s2->prog_name);
1084 #define HEADER_CHAR '-'
1085 #define COLUMN_SEP " "
1087 static void output_header_underlines(void)
1091 for (i = 0; i < env.output_spec.spec_cnt; i++) {
1092 len = env.output_spec.lens[i];
1094 printf("%s", i == 0 ? "" : COLUMN_SEP);
1095 for (j = 0; j < len; j++)
1096 printf("%c", HEADER_CHAR);
1101 static void output_headers(enum resfmt fmt)
1103 const char *fmt_str;
1106 for (i = 0; i < env.output_spec.spec_cnt; i++) {
1107 int id = env.output_spec.ids[i];
1108 int *max_len = &env.output_spec.lens[i];
1111 case RESFMT_TABLE_CALCLEN:
1112 len = snprintf(NULL, 0, "%s", stat_defs[id].header);
1117 fmt_str = stat_defs[id].left_aligned ? "%s%-*s" : "%s%*s";
1118 printf(fmt_str, i == 0 ? "" : COLUMN_SEP, *max_len, stat_defs[id].header);
1119 if (i == env.output_spec.spec_cnt - 1)
1123 printf("%s%s", i == 0 ? "" : ",", stat_defs[id].names[0]);
1124 if (i == env.output_spec.spec_cnt - 1)
1130 if (fmt == RESFMT_TABLE)
1131 output_header_underlines();
1134 static void prepare_value(const struct verif_stats *s, enum stat_id id,
1135 const char **str, long *val)
1139 *str = s ? s->file_name : "N/A";
1142 *str = s ? s->prog_name : "N/A";
1148 *str = s->stats[VERDICT] ? "success" : "failure";
1154 case MAX_STATES_PER_INSN:
1155 case MARK_READ_MAX_LEN:
1156 *val = s ? s->stats[id] : 0;
1159 fprintf(stderr, "Unrecognized stat #%d\n", id);
1164 static void output_stats(const struct verif_stats *s, enum resfmt fmt, bool last)
1168 for (i = 0; i < env.output_spec.spec_cnt; i++) {
1169 int id = env.output_spec.ids[i];
1170 int *max_len = &env.output_spec.lens[i], len;
1171 const char *str = NULL;
1174 prepare_value(s, id, &str, &val);
1177 case RESFMT_TABLE_CALCLEN:
1179 len = snprintf(NULL, 0, "%s", str);
1181 len = snprintf(NULL, 0, "%ld", val);
1187 printf("%s%-*s", i == 0 ? "" : COLUMN_SEP, *max_len, str);
1189 printf("%s%*ld", i == 0 ? "" : COLUMN_SEP, *max_len, val);
1190 if (i == env.output_spec.spec_cnt - 1)
1195 printf("%s%s", i == 0 ? "" : ",", str);
1197 printf("%s%ld", i == 0 ? "" : ",", val);
1198 if (i == env.output_spec.spec_cnt - 1)
1204 if (last && fmt == RESFMT_TABLE) {
1205 output_header_underlines();
1206 printf("Done. Processed %d files, %d programs. Skipped %d files, %d programs.\n",
1207 env.files_processed, env.files_skipped, env.progs_processed, env.progs_skipped);
1211 static int parse_stat_value(const char *str, enum stat_id id, struct verif_stats *st)
1215 st->file_name = strdup(str);
1220 st->prog_name = strdup(str);
1225 if (strcmp(str, "success") == 0) {
1226 st->stats[VERDICT] = true;
1227 } else if (strcmp(str, "failure") == 0) {
1228 st->stats[VERDICT] = false;
1230 fprintf(stderr, "Unrecognized verification verdict '%s'\n", str);
1238 case MAX_STATES_PER_INSN:
1239 case MARK_READ_MAX_LEN: {
1243 if (sscanf(str, "%ld %n", &val, &n) != 1 || n != strlen(str)) {
1245 fprintf(stderr, "Failed to parse '%s' as integer\n", str);
1249 st->stats[id] = val;
1253 fprintf(stderr, "Unrecognized stat #%d\n", id);
1259 static int parse_stats_csv(const char *filename, struct stat_specs *specs,
1260 struct verif_stats **statsp, int *stat_cntp)
1267 f = fopen(filename, "r");
1270 fprintf(stderr, "Failed to open '%s': %d\n", filename, err);
1276 while (fgets(line, sizeof(line), f)) {
1277 char *input = line, *state = NULL, *next;
1278 struct verif_stats *st = NULL;
1284 tmp = realloc(*statsp, (*stat_cntp + 1) * sizeof(**statsp));
1291 st = &(*statsp)[*stat_cntp];
1292 memset(st, 0, sizeof(*st));
1297 while ((next = strtok_r(state ? NULL : input, ",\n", &state))) {
1299 /* for the first line, set up spec stats */
1300 err = parse_stat(next, specs);
1306 /* for all other lines, parse values based on spec */
1307 if (col >= specs->spec_cnt) {
1308 fprintf(stderr, "Found extraneous column #%d in row #%d of '%s'\n",
1309 col, *stat_cntp, filename);
1313 err = parse_stat_value(next, specs->ids[col], st);
1324 if (col < specs->spec_cnt) {
1325 fprintf(stderr, "Not enough columns in row #%d in '%s'\n",
1326 *stat_cntp, filename);
1331 if (!st->file_name || !st->prog_name) {
1332 fprintf(stderr, "Row #%d in '%s' is missing file and/or program name\n",
1333 *stat_cntp, filename);
1338 /* in comparison mode we can only check filters after we
1339 * parsed entire line; if row should be ignored we pretend we
1342 if (!should_process_file_prog(st->file_name, st->prog_name)) {
1343 free(st->file_name);
1344 free(st->prog_name);
1351 fprintf(stderr, "Failed I/O for '%s': %d\n", filename, err);
1359 /* empty/zero stats for mismatched rows */
1360 static const struct verif_stats fallback_stats = { .file_name = "", .prog_name = "" };
1362 static bool is_key_stat(enum stat_id id)
1364 return id == FILE_NAME || id == PROG_NAME;
1367 static void output_comp_header_underlines(void)
1371 for (i = 0; i < env.output_spec.spec_cnt; i++) {
1372 int id = env.output_spec.ids[i];
1373 int max_j = is_key_stat(id) ? 1 : 3;
1375 for (j = 0; j < max_j; j++) {
1376 int len = env.output_spec.lens[3 * i + j];
1378 printf("%s", i + j == 0 ? "" : COLUMN_SEP);
1380 for (k = 0; k < len; k++)
1381 printf("%c", HEADER_CHAR);
1387 static void output_comp_headers(enum resfmt fmt)
1389 static const char *table_sfxs[3] = {" (A)", " (B)", " (DIFF)"};
1390 static const char *name_sfxs[3] = {"_base", "_comp", "_diff"};
1393 for (i = 0; i < env.output_spec.spec_cnt; i++) {
1394 int id = env.output_spec.ids[i];
1395 /* key stats don't have A/B/DIFF columns, they are common for both data sets */
1396 int max_j = is_key_stat(id) ? 1 : 3;
1398 for (j = 0; j < max_j; j++) {
1399 int *max_len = &env.output_spec.lens[3 * i + j];
1400 bool last = (i == env.output_spec.spec_cnt - 1) && (j == max_j - 1);
1404 case RESFMT_TABLE_CALCLEN:
1405 sfx = is_key_stat(id) ? "" : table_sfxs[j];
1406 len = snprintf(NULL, 0, "%s%s", stat_defs[id].header, sfx);
1411 sfx = is_key_stat(id) ? "" : table_sfxs[j];
1412 printf("%s%-*s%s", i + j == 0 ? "" : COLUMN_SEP,
1413 *max_len - (int)strlen(sfx), stat_defs[id].header, sfx);
1418 sfx = is_key_stat(id) ? "" : name_sfxs[j];
1419 printf("%s%s%s", i + j == 0 ? "" : ",", stat_defs[id].names[0], sfx);
1427 if (fmt == RESFMT_TABLE)
1428 output_comp_header_underlines();
1431 static void output_comp_stats(const struct verif_stats_join *join_stats,
1432 enum resfmt fmt, bool last)
1434 const struct verif_stats *base = join_stats->stats_a;
1435 const struct verif_stats *comp = join_stats->stats_b;
1436 char base_buf[1024] = {}, comp_buf[1024] = {}, diff_buf[1024] = {};
1439 for (i = 0; i < env.output_spec.spec_cnt; i++) {
1440 int id = env.output_spec.ids[i], len;
1441 int *max_len_base = &env.output_spec.lens[3 * i + 0];
1442 int *max_len_comp = &env.output_spec.lens[3 * i + 1];
1443 int *max_len_diff = &env.output_spec.lens[3 * i + 2];
1444 const char *base_str = NULL, *comp_str = NULL;
1445 long base_val = 0, comp_val = 0, diff_val = 0;
1447 prepare_value(base, id, &base_str, &base_val);
1448 prepare_value(comp, id, &comp_str, &comp_val);
1450 /* normalize all the outputs to be in string buffers for simplicity */
1451 if (is_key_stat(id)) {
1452 /* key stats (file and program name) are always strings */
1454 snprintf(base_buf, sizeof(base_buf), "%s", base_str);
1456 snprintf(base_buf, sizeof(base_buf), "%s", comp_str);
1457 } else if (base_str) {
1458 snprintf(base_buf, sizeof(base_buf), "%s", base_str);
1459 snprintf(comp_buf, sizeof(comp_buf), "%s", comp_str);
1461 snprintf(diff_buf, sizeof(diff_buf), "%s", "N/A");
1462 else if (strcmp(base_str, comp_str) == 0)
1463 snprintf(diff_buf, sizeof(diff_buf), "%s", "MATCH");
1465 snprintf(diff_buf, sizeof(diff_buf), "%s", "MISMATCH");
1470 snprintf(base_buf, sizeof(base_buf), "%ld", base_val);
1472 snprintf(base_buf, sizeof(base_buf), "%s", "N/A");
1474 snprintf(comp_buf, sizeof(comp_buf), "%ld", comp_val);
1476 snprintf(comp_buf, sizeof(comp_buf), "%s", "N/A");
1478 diff_val = comp_val - base_val;
1479 if (!base || !comp) {
1480 snprintf(diff_buf, sizeof(diff_buf), "%s", "N/A");
1482 if (base_val == 0) {
1483 if (comp_val == base_val)
1484 p = 0.0; /* avoid +0 (+100%) case */
1486 p = comp_val < base_val ? -100.0 : 100.0;
1488 p = diff_val * 100.0 / base_val;
1490 snprintf(diff_buf, sizeof(diff_buf), "%+ld (%+.2lf%%)", diff_val, p);
1495 case RESFMT_TABLE_CALCLEN:
1496 len = strlen(base_buf);
1497 if (len > *max_len_base)
1498 *max_len_base = len;
1499 if (!is_key_stat(id)) {
1500 len = strlen(comp_buf);
1501 if (len > *max_len_comp)
1502 *max_len_comp = len;
1503 len = strlen(diff_buf);
1504 if (len > *max_len_diff)
1505 *max_len_diff = len;
1508 case RESFMT_TABLE: {
1509 /* string outputs are left-aligned, number outputs are right-aligned */
1510 const char *fmt = base_str ? "%s%-*s" : "%s%*s";
1512 printf(fmt, i == 0 ? "" : COLUMN_SEP, *max_len_base, base_buf);
1513 if (!is_key_stat(id)) {
1514 printf(fmt, COLUMN_SEP, *max_len_comp, comp_buf);
1515 printf(fmt, COLUMN_SEP, *max_len_diff, diff_buf);
1517 if (i == env.output_spec.spec_cnt - 1)
1522 printf("%s%s", i == 0 ? "" : ",", base_buf);
1523 if (!is_key_stat(id)) {
1524 printf("%s%s", i == 0 ? "" : ",", comp_buf);
1525 printf("%s%s", i == 0 ? "" : ",", diff_buf);
1527 if (i == env.output_spec.spec_cnt - 1)
1533 if (last && fmt == RESFMT_TABLE)
1534 output_comp_header_underlines();
1537 static int cmp_stats_key(const struct verif_stats *base, const struct verif_stats *comp)
1541 r = strcmp(base->file_name, comp->file_name);
1544 return strcmp(base->prog_name, comp->prog_name);
1547 static bool is_join_stat_filter_matched(struct filter *f, const struct verif_stats_join *stats)
1549 static const double eps = 1e-9;
1550 const char *str = NULL;
1553 fetch_join_stat_value(stats, f->stat_id, f->stat_var, &str, &value);
1556 case OP_EQ: return value > f->value - eps && value < f->value + eps;
1557 case OP_NEQ: return value < f->value - eps || value > f->value + eps;
1558 case OP_LT: return value < f->value - eps;
1559 case OP_LE: return value <= f->value + eps;
1560 case OP_GT: return value > f->value + eps;
1561 case OP_GE: return value >= f->value - eps;
1564 fprintf(stderr, "BUG: unknown filter op %d!\n", f->op);
1568 static bool should_output_join_stats(const struct verif_stats_join *stats)
1571 int i, allow_cnt = 0;
1573 for (i = 0; i < env.deny_filter_cnt; i++) {
1574 f = &env.deny_filters[i];
1575 if (f->kind != FILTER_STAT)
1578 if (is_join_stat_filter_matched(f, stats))
1582 for (i = 0; i < env.allow_filter_cnt; i++) {
1583 f = &env.allow_filters[i];
1584 if (f->kind != FILTER_STAT)
1588 if (is_join_stat_filter_matched(f, stats))
1592 /* if there are no stat allowed filters, pass everything through */
1593 return allow_cnt == 0;
1596 static int handle_comparison_mode(void)
1598 struct stat_specs base_specs = {}, comp_specs = {};
1599 struct stat_specs tmp_sort_spec;
1600 enum resfmt cur_fmt;
1601 int err, i, j, last_idx;
1603 if (env.filename_cnt != 2) {
1604 fprintf(stderr, "Comparison mode expects exactly two input CSV files!\n\n");
1605 argp_help(&argp, stderr, ARGP_HELP_USAGE, "veristat");
1609 err = parse_stats_csv(env.filenames[0], &base_specs,
1610 &env.baseline_stats, &env.baseline_stat_cnt);
1612 fprintf(stderr, "Failed to parse stats from '%s': %d\n", env.filenames[0], err);
1615 err = parse_stats_csv(env.filenames[1], &comp_specs,
1616 &env.prog_stats, &env.prog_stat_cnt);
1618 fprintf(stderr, "Failed to parse stats from '%s': %d\n", env.filenames[1], err);
1622 /* To keep it simple we validate that the set and order of stats in
1623 * both CSVs are exactly the same. This can be lifted with a bit more
1624 * pre-processing later.
1626 if (base_specs.spec_cnt != comp_specs.spec_cnt) {
1627 fprintf(stderr, "Number of stats in '%s' and '%s' differs (%d != %d)!\n",
1628 env.filenames[0], env.filenames[1],
1629 base_specs.spec_cnt, comp_specs.spec_cnt);
1632 for (i = 0; i < base_specs.spec_cnt; i++) {
1633 if (base_specs.ids[i] != comp_specs.ids[i]) {
1634 fprintf(stderr, "Stats composition differs between '%s' and '%s' (%s != %s)!\n",
1635 env.filenames[0], env.filenames[1],
1636 stat_defs[base_specs.ids[i]].names[0],
1637 stat_defs[comp_specs.ids[i]].names[0]);
1642 /* Replace user-specified sorting spec with file+prog sorting rule to
1643 * be able to join two datasets correctly. Once we are done, we will
1644 * restore the original sort spec.
1646 tmp_sort_spec = env.sort_spec;
1647 env.sort_spec = join_sort_spec;
1648 qsort(env.prog_stats, env.prog_stat_cnt, sizeof(*env.prog_stats), cmp_prog_stats);
1649 qsort(env.baseline_stats, env.baseline_stat_cnt, sizeof(*env.baseline_stats), cmp_prog_stats);
1650 env.sort_spec = tmp_sort_spec;
1652 /* Join two datasets together. If baseline and comparison datasets
1653 * have different subset of rows (we match by 'object + prog' as
1654 * a unique key) then assume empty/missing/zero value for rows that
1655 * are missing in the opposite data set.
1658 while (i < env.baseline_stat_cnt || j < env.prog_stat_cnt) {
1659 const struct verif_stats *base, *comp;
1660 struct verif_stats_join *join;
1664 base = i < env.baseline_stat_cnt ? &env.baseline_stats[i] : &fallback_stats;
1665 comp = j < env.prog_stat_cnt ? &env.prog_stats[j] : &fallback_stats;
1667 if (!base->file_name || !base->prog_name) {
1668 fprintf(stderr, "Entry #%d in '%s' doesn't have file and/or program name specified!\n",
1669 i, env.filenames[0]);
1672 if (!comp->file_name || !comp->prog_name) {
1673 fprintf(stderr, "Entry #%d in '%s' doesn't have file and/or program name specified!\n",
1674 j, env.filenames[1]);
1678 tmp = realloc(env.join_stats, (env.join_stat_cnt + 1) * sizeof(*env.join_stats));
1681 env.join_stats = tmp;
1683 join = &env.join_stats[env.join_stat_cnt];
1684 memset(join, 0, sizeof(*join));
1686 r = cmp_stats_key(base, comp);
1688 join->file_name = base->file_name;
1689 join->prog_name = base->prog_name;
1690 join->stats_a = base;
1691 join->stats_b = comp;
1694 } else if (comp == &fallback_stats || r < 0) {
1695 join->file_name = base->file_name;
1696 join->prog_name = base->prog_name;
1697 join->stats_a = base;
1698 join->stats_b = NULL;
1701 join->file_name = comp->file_name;
1702 join->prog_name = comp->prog_name;
1703 join->stats_a = NULL;
1704 join->stats_b = comp;
1707 env.join_stat_cnt += 1;
1710 /* now sort joined results accorsing to sort spec */
1711 qsort(env.join_stats, env.join_stat_cnt, sizeof(*env.join_stats), cmp_join_stats);
1713 /* for human-readable table output we need to do extra pass to
1714 * calculate column widths, so we substitute current output format
1715 * with RESFMT_TABLE_CALCLEN and later revert it back to RESFMT_TABLE
1716 * and do everything again.
1718 if (env.out_fmt == RESFMT_TABLE)
1719 cur_fmt = RESFMT_TABLE_CALCLEN;
1721 cur_fmt = env.out_fmt;
1724 output_comp_headers(cur_fmt);
1726 for (i = 0; i < env.join_stat_cnt; i++) {
1727 const struct verif_stats_join *join = &env.join_stats[i];
1729 if (!should_output_join_stats(join))
1732 if (cur_fmt == RESFMT_TABLE_CALCLEN)
1735 output_comp_stats(join, cur_fmt, i == last_idx);
1738 if (cur_fmt == RESFMT_TABLE_CALCLEN) {
1739 cur_fmt = RESFMT_TABLE;
1740 goto one_more_time; /* ... this time with feeling */
1746 static bool is_stat_filter_matched(struct filter *f, const struct verif_stats *stats)
1748 long value = stats->stats[f->stat_id];
1751 case OP_EQ: return value == f->value;
1752 case OP_NEQ: return value != f->value;
1753 case OP_LT: return value < f->value;
1754 case OP_LE: return value <= f->value;
1755 case OP_GT: return value > f->value;
1756 case OP_GE: return value >= f->value;
1759 fprintf(stderr, "BUG: unknown filter op %d!\n", f->op);
1763 static bool should_output_stats(const struct verif_stats *stats)
1766 int i, allow_cnt = 0;
1768 for (i = 0; i < env.deny_filter_cnt; i++) {
1769 f = &env.deny_filters[i];
1770 if (f->kind != FILTER_STAT)
1773 if (is_stat_filter_matched(f, stats))
1777 for (i = 0; i < env.allow_filter_cnt; i++) {
1778 f = &env.allow_filters[i];
1779 if (f->kind != FILTER_STAT)
1783 if (is_stat_filter_matched(f, stats))
1787 /* if there are no stat allowed filters, pass everything through */
1788 return allow_cnt == 0;
1791 static void output_prog_stats(void)
1793 const struct verif_stats *stats;
1794 int i, last_stat_idx = 0;
1796 if (env.out_fmt == RESFMT_TABLE) {
1797 /* calculate column widths */
1798 output_headers(RESFMT_TABLE_CALCLEN);
1799 for (i = 0; i < env.prog_stat_cnt; i++) {
1800 stats = &env.prog_stats[i];
1801 if (!should_output_stats(stats))
1803 output_stats(stats, RESFMT_TABLE_CALCLEN, false);
1808 /* actually output the table */
1809 output_headers(env.out_fmt);
1810 for (i = 0; i < env.prog_stat_cnt; i++) {
1811 stats = &env.prog_stats[i];
1812 if (!should_output_stats(stats))
1814 output_stats(stats, env.out_fmt, i == last_stat_idx);
1818 static int handle_verif_mode(void)
1822 if (env.filename_cnt == 0) {
1823 fprintf(stderr, "Please provide path to BPF object file!\n\n");
1824 argp_help(&argp, stderr, ARGP_HELP_USAGE, "veristat");
1828 for (i = 0; i < env.filename_cnt; i++) {
1829 err = process_obj(env.filenames[i]);
1831 fprintf(stderr, "Failed to process '%s': %d\n", env.filenames[i], err);
1836 qsort(env.prog_stats, env.prog_stat_cnt, sizeof(*env.prog_stats), cmp_prog_stats);
1838 output_prog_stats();
1843 static int handle_replay_mode(void)
1845 struct stat_specs specs = {};
1848 if (env.filename_cnt != 1) {
1849 fprintf(stderr, "Replay mode expects exactly one input CSV file!\n\n");
1850 argp_help(&argp, stderr, ARGP_HELP_USAGE, "veristat");
1854 err = parse_stats_csv(env.filenames[0], &specs,
1855 &env.prog_stats, &env.prog_stat_cnt);
1857 fprintf(stderr, "Failed to parse stats from '%s': %d\n", env.filenames[0], err);
1861 qsort(env.prog_stats, env.prog_stat_cnt, sizeof(*env.prog_stats), cmp_prog_stats);
1863 output_prog_stats();
1868 int main(int argc, char **argv)
1872 if (argp_parse(&argp, argc, argv, 0, NULL, NULL))
1875 if (env.verbose && env.quiet) {
1876 fprintf(stderr, "Verbose and quiet modes are incompatible, please specify just one or neither!\n\n");
1877 argp_help(&argp, stderr, ARGP_HELP_USAGE, "veristat");
1880 if (env.verbose && env.log_level == 0)
1883 if (env.output_spec.spec_cnt == 0) {
1884 if (env.out_fmt == RESFMT_CSV)
1885 env.output_spec = default_csv_output_spec;
1887 env.output_spec = default_output_spec;
1889 if (env.sort_spec.spec_cnt == 0)
1890 env.sort_spec = default_sort_spec;
1892 if (env.comparison_mode && env.replay_mode) {
1893 fprintf(stderr, "Can't specify replay and comparison mode at the same time!\n\n");
1894 argp_help(&argp, stderr, ARGP_HELP_USAGE, "veristat");
1898 if (env.comparison_mode)
1899 err = handle_comparison_mode();
1900 else if (env.replay_mode)
1901 err = handle_replay_mode();
1903 err = handle_verif_mode();
1905 free_verif_stats(env.prog_stats, env.prog_stat_cnt);
1906 free_verif_stats(env.baseline_stats, env.baseline_stat_cnt);
1907 free(env.join_stats);
1908 for (i = 0; i < env.filename_cnt; i++)
1909 free(env.filenames[i]);
1910 free(env.filenames);
1911 for (i = 0; i < env.allow_filter_cnt; i++) {
1912 free(env.allow_filters[i].any_glob);
1913 free(env.allow_filters[i].file_glob);
1914 free(env.allow_filters[i].prog_glob);
1916 free(env.allow_filters);
1917 for (i = 0; i < env.deny_filter_cnt; i++) {
1918 free(env.deny_filters[i].any_glob);
1919 free(env.deny_filters[i].file_glob);
1920 free(env.deny_filters[i].prog_glob);
1922 free(env.deny_filters);