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>
36 NUM_STATS_CNT = FILE_NAME - VERDICT,
39 /* In comparison mode each stat can specify up to four different values:
42 * - absolute diff value;
43 * - relative (percentage) diff value.
45 * When specifying stat specs in comparison mode, user can use one of the
46 * following variant suffixes to specify which exact variant should be used for
47 * ordering or filtering:
48 * - `_a` for A side value;
49 * - `_b` for B side value;
50 * - `_diff` for absolute diff value;
51 * - `_pct` for relative (percentage) diff value.
53 * If no variant suffix is provided, then `_b` (control data) is assumed.
55 * As an example, let's say instructions stat has the following output:
57 * Insns (A) Insns (B) Insns (DIFF)
58 * --------- --------- --------------
59 * 21547 20920 -627 (-2.91%)
62 * - 21547 is A side value (insns_a);
63 * - 20920 is B side value (insns_b);
64 * - -627 is absolute diff value (insns_diff);
65 * - -2.91% is relative diff value (insns_pct).
67 * For verdict there is no verdict_pct variant.
68 * For file and program name, _a and _b variants are equivalent and there are
69 * no _diff or _pct variants.
82 long stats[NUM_STATS_CNT];
85 /* joined comparison mode stats */
86 struct verif_stats_join {
90 const struct verif_stats *stats_a;
91 const struct verif_stats *stats_b;
96 enum stat_id ids[ALL_STATS_CNT];
97 enum stat_variant variants[ALL_STATS_CNT];
98 bool asc[ALL_STATS_CNT];
99 int lens[ALL_STATS_CNT * 3]; /* 3x for comparison mode */
104 RESFMT_TABLE_CALCLEN, /* fake format to pre-calculate table's column widths */
115 OP_NEQ, /* != or <> */
123 enum filter_kind kind;
129 enum operator_kind op;
131 enum stat_variant stat_var;
143 bool comparison_mode;
146 struct verif_stats *prog_stats;
149 /* baseline_stats is allocated and used only in comparison mode */
150 struct verif_stats *baseline_stats;
151 int baseline_stat_cnt;
153 struct verif_stats_join *join_stats;
156 struct stat_specs output_spec;
157 struct stat_specs sort_spec;
159 struct filter *allow_filters;
160 struct filter *deny_filters;
161 int allow_filter_cnt;
170 static int libbpf_print_fn(enum libbpf_print_level level, const char *format, va_list args)
174 if (level == LIBBPF_DEBUG && !env.debug)
176 return vfprintf(stderr, format, args);
179 const char *argp_program_version = "veristat";
180 const char *argp_program_bug_address = "<bpf@vger.kernel.org>";
181 const char argp_program_doc[] =
182 "veristat BPF verifier stats collection and comparison tool.\n"
184 "USAGE: veristat <obj-file> [<obj-file>...]\n"
185 " OR: veristat -C <baseline.csv> <comparison.csv>\n";
187 static const struct argp_option opts[] = {
188 { NULL, 'h', NULL, OPTION_HIDDEN, "Show the full help" },
189 { "verbose", 'v', NULL, 0, "Verbose mode" },
190 { "log-level", 'l', "LEVEL", 0, "Verifier log level (default 0 for normal mode, 1 for verbose mode)" },
191 { "debug", 'd', NULL, 0, "Debug mode (turns on libbpf debug logging)" },
192 { "quiet", 'q', NULL, 0, "Quiet mode" },
193 { "emit", 'e', "SPEC", 0, "Specify stats to be emitted" },
194 { "sort", 's', "SPEC", 0, "Specify sort order" },
195 { "output-format", 'o', "FMT", 0, "Result output format (table, csv), default is table." },
196 { "compare", 'C', NULL, 0, "Comparison mode" },
197 { "replay", 'R', NULL, 0, "Replay mode" },
198 { "filter", 'f', "FILTER", 0, "Filter expressions (or @filename for file with expressions)." },
202 static int parse_stats(const char *stats_str, struct stat_specs *specs);
203 static int append_filter(struct filter **filters, int *cnt, const char *str);
204 static int append_filter_file(const char *path);
206 static error_t parse_arg(int key, char *arg, struct argp_state *state)
213 argp_state_help(state, stderr, ARGP_HELP_STD_HELP);
226 err = parse_stats(arg, &env.output_spec);
231 err = parse_stats(arg, &env.sort_spec);
236 if (strcmp(arg, "table") == 0) {
237 env.out_fmt = RESFMT_TABLE;
238 } else if (strcmp(arg, "csv") == 0) {
239 env.out_fmt = RESFMT_CSV;
241 fprintf(stderr, "Unrecognized output format '%s'\n", arg);
247 env.log_level = strtol(arg, NULL, 10);
249 fprintf(stderr, "invalid log level: %s\n", arg);
254 env.comparison_mode = true;
257 env.replay_mode = true;
261 err = append_filter_file(arg + 1);
262 else if (arg[0] == '!')
263 err = append_filter(&env.deny_filters, &env.deny_filter_cnt, arg + 1);
265 err = append_filter(&env.allow_filters, &env.allow_filter_cnt, arg);
267 fprintf(stderr, "Failed to collect program filter expressions: %d\n", err);
272 tmp = realloc(env.filenames, (env.filename_cnt + 1) * sizeof(*env.filenames));
276 env.filenames[env.filename_cnt] = strdup(arg);
277 if (!env.filenames[env.filename_cnt])
282 return ARGP_ERR_UNKNOWN;
287 static const struct argp argp = {
290 .doc = argp_program_doc,
294 /* Adapted from perf/util/string.c */
295 static bool glob_matches(const char *str, const char *pat)
297 while (*str && *pat && *pat != '*') {
303 /* Check wild card */
307 if (!*pat) /* Tail wild card matches all */
310 if (glob_matches(str++, pat))
313 return !*str && !*pat;
316 static bool is_bpf_obj_file(const char *path) {
318 int fd, err = -EINVAL;
321 fd = open(path, O_RDONLY | O_CLOEXEC);
323 return true; /* we'll fail later and propagate error */
325 /* ensure libelf is initialized */
326 (void)elf_version(EV_CURRENT);
328 elf = elf_begin(fd, ELF_C_READ, NULL);
332 if (elf_kind(elf) != ELF_K_ELF || gelf_getclass(elf) != ELFCLASS64)
335 ehdr = elf64_getehdr(elf);
336 /* Old LLVM set e_machine to EM_NONE */
337 if (!ehdr || ehdr->e_type != ET_REL || (ehdr->e_machine && ehdr->e_machine != EM_BPF))
348 static bool should_process_file_prog(const char *filename, const char *prog_name)
351 int i, allow_cnt = 0;
353 for (i = 0; i < env.deny_filter_cnt; i++) {
354 f = &env.deny_filters[i];
355 if (f->kind != FILTER_NAME)
358 if (f->any_glob && glob_matches(filename, f->any_glob))
360 if (f->any_glob && prog_name && glob_matches(prog_name, f->any_glob))
362 if (f->file_glob && glob_matches(filename, f->file_glob))
364 if (f->prog_glob && prog_name && glob_matches(prog_name, f->prog_glob))
368 for (i = 0; i < env.allow_filter_cnt; i++) {
369 f = &env.allow_filters[i];
370 if (f->kind != FILTER_NAME)
375 if (glob_matches(filename, f->any_glob))
377 /* If we don't know program name yet, any_glob filter
378 * has to assume that current BPF object file might be
379 * relevant; we'll check again later on after opening
380 * BPF object file, at which point program name will
383 if (!prog_name || glob_matches(prog_name, f->any_glob))
386 if (f->file_glob && !glob_matches(filename, f->file_glob))
388 if (f->prog_glob && prog_name && !glob_matches(prog_name, f->prog_glob))
394 /* if there are no file/prog name allow filters, allow all progs,
395 * unless they are denied earlier explicitly
397 return allow_cnt == 0;
401 enum operator_kind op_kind;
404 /* Order of these definitions matter to avoid situations like '<'
405 * matching part of what is actually a '<>' operator. That is,
406 * substrings should go last.
418 static bool parse_stat_id_var(const char *name, size_t len, int *id, enum stat_variant *var);
420 static int append_filter(struct filter **filters, int *cnt, const char *str)
427 tmp = realloc(*filters, (*cnt + 1) * sizeof(**filters));
432 f = &(*filters)[*cnt];
433 memset(f, 0, sizeof(*f));
435 /* First, let's check if it's a stats filter of the following form:
436 * <stat><op><value, where:
437 * - <stat> is one of supported numerical stats (verdict is also
438 * considered numerical, failure == 0, success == 1);
439 * - <op> is comparison operator (see `operators` definitions);
440 * - <value> is an integer (or failure/success, or false/true as
441 * special aliases for 0 and 1, respectively).
442 * If the form doesn't match what user provided, we assume file/prog
445 for (i = 0; i < ARRAY_SIZE(operators); i++) {
446 enum stat_variant var;
449 const char *end = str;
452 op_str = operators[i].op_str;
453 p = strstr(str, op_str);
457 if (!parse_stat_id_var(str, p - str, &id, &var)) {
458 fprintf(stderr, "Unrecognized stat name in '%s'!\n", str);
461 if (id >= FILE_NAME) {
462 fprintf(stderr, "Non-integer stat is specified in '%s'!\n", str);
468 if (strcasecmp(p, "true") == 0 ||
469 strcasecmp(p, "t") == 0 ||
470 strcasecmp(p, "success") == 0 ||
471 strcasecmp(p, "succ") == 0 ||
472 strcasecmp(p, "s") == 0 ||
473 strcasecmp(p, "match") == 0 ||
474 strcasecmp(p, "m") == 0) {
476 } else if (strcasecmp(p, "false") == 0 ||
477 strcasecmp(p, "f") == 0 ||
478 strcasecmp(p, "failure") == 0 ||
479 strcasecmp(p, "fail") == 0 ||
480 strcasecmp(p, "mismatch") == 0 ||
481 strcasecmp(p, "mis") == 0) {
485 val = strtol(p, (char **)&end, 10);
486 if (errno || end == p || *end != '\0' ) {
487 fprintf(stderr, "Invalid integer value in '%s'!\n", str);
492 f->kind = FILTER_STAT;
495 f->op = operators[i].op_kind;
502 /* File/prog filter can be specified either as '<glob>' or
503 * '<file-glob>/<prog-glob>'. In the former case <glob> is applied to
504 * both file and program names. This seems to be way more useful in
505 * practice. If user needs full control, they can use '/<prog-glob>'
506 * form to glob just program name, or '<file-glob>/' to glob only file
507 * name. But usually common <glob> seems to be the most useful and
510 f->kind = FILTER_NAME;
511 p = strchr(str, '/');
513 f->any_glob = strdup(str);
518 /* non-empty file glob */
519 f->file_glob = strndup(str, p - str);
523 if (strlen(p + 1) > 0) {
524 /* non-empty prog glob */
525 f->prog_glob = strdup(p + 1);
538 static int append_filter_file(const char *path)
544 f = fopen(path, "r");
547 fprintf(stderr, "Failed to open filters in '%s': %d\n", path, err);
551 while (fscanf(f, " %1023[^\n]\n", buf) == 1) {
552 /* lines starting with # are comments, skip them */
553 if (buf[0] == '\0' || buf[0] == '#')
555 /* lines starting with ! are negative match filters */
557 err = append_filter(&env.deny_filters, &env.deny_filter_cnt, buf + 1);
559 err = append_filter(&env.allow_filters, &env.allow_filter_cnt, buf);
569 static const struct stat_specs default_output_spec = {
572 FILE_NAME, PROG_NAME, VERDICT, DURATION,
573 TOTAL_INSNS, TOTAL_STATES, PEAK_STATES,
577 static const struct stat_specs default_csv_output_spec = {
580 FILE_NAME, PROG_NAME, VERDICT, DURATION,
581 TOTAL_INSNS, TOTAL_STATES, PEAK_STATES,
582 MAX_STATES_PER_INSN, MARK_READ_MAX_LEN,
586 static const struct stat_specs default_sort_spec = {
589 FILE_NAME, PROG_NAME,
591 .asc = { true, true, },
594 /* sorting for comparison mode to join two data sets */
595 static const struct stat_specs join_sort_spec = {
598 FILE_NAME, PROG_NAME,
600 .asc = { true, true, },
603 static struct stat_def {
605 const char *names[4];
609 [FILE_NAME] = { "File", {"file_name", "filename", "file"}, true /* asc */, true /* left */ },
610 [PROG_NAME] = { "Program", {"prog_name", "progname", "prog"}, true /* asc */, true /* left */ },
611 [VERDICT] = { "Verdict", {"verdict"}, true /* asc: failure, success */, true /* left */ },
612 [DURATION] = { "Duration (us)", {"duration", "dur"}, },
613 [TOTAL_INSNS] = { "Insns", {"total_insns", "insns"}, },
614 [TOTAL_STATES] = { "States", {"total_states", "states"}, },
615 [PEAK_STATES] = { "Peak states", {"peak_states"}, },
616 [MAX_STATES_PER_INSN] = { "Max states per insn", {"max_states_per_insn"}, },
617 [MARK_READ_MAX_LEN] = { "Max mark read length", {"max_mark_read_len", "mark_read"}, },
620 static bool parse_stat_id_var(const char *name, size_t len, int *id, enum stat_variant *var)
622 static const char *var_sfxs[] = {
625 [VARIANT_DIFF] = "_diff",
626 [VARIANT_PCT] = "_pct",
630 for (i = 0; i < ARRAY_SIZE(stat_defs); i++) {
631 struct stat_def *def = &stat_defs[i];
632 size_t alias_len, sfx_len;
635 for (j = 0; j < ARRAY_SIZE(stat_defs[i].names); j++) {
636 alias = def->names[j];
640 alias_len = strlen(alias);
641 if (strncmp(name, alias, alias_len) != 0)
644 if (alias_len == len) {
645 /* If no variant suffix is specified, we
646 * assume control group (just in case we are
647 * in comparison mode. Variant is ignored in
648 * non-comparison mode.
655 for (k = 0; k < ARRAY_SIZE(var_sfxs); k++) {
656 sfx_len = strlen(var_sfxs[k]);
657 if (alias_len + sfx_len != len)
660 if (strncmp(name + alias_len, var_sfxs[k], sfx_len) == 0) {
661 *var = (enum stat_variant)k;
672 static bool is_asc_sym(char c)
677 static bool is_desc_sym(char c)
679 return c == 'v' || c == 'V' || c == '.' || c == '!' || c == '_';
682 static int parse_stat(const char *stat_name, struct stat_specs *specs)
685 bool has_order = false, is_asc = false;
686 size_t len = strlen(stat_name);
687 enum stat_variant var;
689 if (specs->spec_cnt >= ARRAY_SIZE(specs->ids)) {
690 fprintf(stderr, "Can't specify more than %zd stats\n", ARRAY_SIZE(specs->ids));
694 if (len > 1 && (is_asc_sym(stat_name[len - 1]) || is_desc_sym(stat_name[len - 1]))) {
696 is_asc = is_asc_sym(stat_name[len - 1]);
700 if (!parse_stat_id_var(stat_name, len, &id, &var)) {
701 fprintf(stderr, "Unrecognized stat name '%s'\n", stat_name);
705 specs->ids[specs->spec_cnt] = id;
706 specs->variants[specs->spec_cnt] = var;
707 specs->asc[specs->spec_cnt] = has_order ? is_asc : stat_defs[id].asc_by_default;
713 static int parse_stats(const char *stats_str, struct stat_specs *specs)
715 char *input, *state = NULL, *next;
718 input = strdup(stats_str);
722 while ((next = strtok_r(state ? NULL : input, ",", &state))) {
723 err = parse_stat(next, specs);
731 static void free_verif_stats(struct verif_stats *stats, size_t stat_cnt)
738 for (i = 0; i < stat_cnt; i++) {
739 free(stats[i].file_name);
740 free(stats[i].prog_name);
745 static char verif_log_buf[64 * 1024];
747 #define MAX_PARSED_LOG_LINES 100
749 static int parse_verif_log(char * const buf, size_t buf_sz, struct verif_stats *s)
754 buf[buf_sz - 1] = '\0';
756 for (pos = strlen(buf) - 1, lines = 0; pos >= 0 && lines < MAX_PARSED_LOG_LINES; lines++) {
757 /* find previous endline or otherwise take the start of log buf */
758 for (cur = &buf[pos]; cur > buf && cur[0] != '\n'; cur--, pos--) {
760 /* next time start from end of previous line (or pos goes to <0) */
762 /* if we found endline, point right after endline symbol;
763 * otherwise, stay at the beginning of log buf
768 if (1 == sscanf(cur, "verification time %ld usec\n", &s->stats[DURATION]))
770 if (6 == sscanf(cur, "processed %ld insns (limit %*d) max_states_per_insn %ld total_states %ld peak_states %ld mark_read %ld",
771 &s->stats[TOTAL_INSNS],
772 &s->stats[MAX_STATES_PER_INSN],
773 &s->stats[TOTAL_STATES],
774 &s->stats[PEAK_STATES],
775 &s->stats[MARK_READ_MAX_LEN]))
782 static int guess_prog_type_by_ctx_name(const char *ctx_name,
783 enum bpf_prog_type *prog_type,
784 enum bpf_attach_type *attach_type)
786 /* We need to guess program type based on its declared context type.
787 * This guess can't be perfect as many different program types might
788 * share the same context type. So we can only hope to reasonably
789 * well guess this and get lucky.
791 * Just in case, we support both UAPI-side type names and
792 * kernel-internal names.
795 const char *uapi_name;
796 const char *kern_name;
797 enum bpf_prog_type prog_type;
798 enum bpf_attach_type attach_type;
800 /* __sk_buff is most ambiguous, for now we assume cgroup_skb */
801 { "__sk_buff", "sk_buff", BPF_PROG_TYPE_SCHED_CLS },
802 { "bpf_sock", "sock", BPF_PROG_TYPE_CGROUP_SOCK, BPF_CGROUP_INET4_POST_BIND },
803 { "bpf_sock_addr", "bpf_sock_addr_kern", BPF_PROG_TYPE_CGROUP_SOCK_ADDR, BPF_CGROUP_INET4_BIND },
804 { "bpf_sock_ops", "bpf_sock_ops_kern", BPF_PROG_TYPE_SOCK_OPS, BPF_CGROUP_SOCK_OPS },
805 { "sk_msg_md", "sk_msg", BPF_PROG_TYPE_SK_MSG, BPF_SK_MSG_VERDICT },
806 { "bpf_cgroup_dev_ctx", "bpf_cgroup_dev_ctx", BPF_PROG_TYPE_CGROUP_DEVICE, BPF_CGROUP_DEVICE },
807 { "bpf_sysctl", "bpf_sysctl_kern", BPF_PROG_TYPE_CGROUP_SYSCTL, BPF_CGROUP_SYSCTL },
808 { "bpf_sockopt", "bpf_sockopt_kern", BPF_PROG_TYPE_CGROUP_SOCKOPT, BPF_CGROUP_SETSOCKOPT },
809 { "sk_reuseport_md", "sk_reuseport_kern", BPF_PROG_TYPE_SK_REUSEPORT, BPF_SK_REUSEPORT_SELECT_OR_MIGRATE },
810 { "bpf_sk_lookup", "bpf_sk_lookup_kern", BPF_PROG_TYPE_SK_LOOKUP, BPF_SK_LOOKUP },
811 { "xdp_md", "xdp_buff", BPF_PROG_TYPE_XDP, BPF_XDP },
812 /* tracing types with no expected attach type */
813 { "bpf_user_pt_regs_t", "pt_regs", BPF_PROG_TYPE_KPROBE },
814 { "bpf_perf_event_data", "bpf_perf_event_data_kern", BPF_PROG_TYPE_PERF_EVENT },
815 /* raw_tp programs use u64[] from kernel side, we don't want
816 * to match on that, probably; so NULL for kern-side type
818 { "bpf_raw_tracepoint_args", NULL, BPF_PROG_TYPE_RAW_TRACEPOINT },
825 for (i = 0; i < ARRAY_SIZE(ctx_map); i++) {
826 if (strcmp(ctx_map[i].uapi_name, ctx_name) == 0 ||
827 (ctx_map[i].kern_name && strcmp(ctx_map[i].kern_name, ctx_name) == 0)) {
828 *prog_type = ctx_map[i].prog_type;
829 *attach_type = ctx_map[i].attach_type;
837 static void fixup_obj(struct bpf_object *obj, struct bpf_program *prog, const char *filename)
841 bpf_object__for_each_map(map, obj) {
842 /* disable pinning */
843 bpf_map__set_pin_path(map, NULL);
845 /* fix up map size, if necessary */
846 switch (bpf_map__type(map)) {
847 case BPF_MAP_TYPE_SK_STORAGE:
848 case BPF_MAP_TYPE_TASK_STORAGE:
849 case BPF_MAP_TYPE_INODE_STORAGE:
850 case BPF_MAP_TYPE_CGROUP_STORAGE:
853 if (bpf_map__max_entries(map) == 0)
854 bpf_map__set_max_entries(map, 1);
858 /* SEC(freplace) programs can't be loaded with veristat as is,
859 * but we can try guessing their target program's expected type by
860 * looking at the type of program's first argument and substituting
861 * corresponding program type
863 if (bpf_program__type(prog) == BPF_PROG_TYPE_EXT) {
864 const struct btf *btf = bpf_object__btf(obj);
865 const char *prog_name = bpf_program__name(prog);
866 enum bpf_prog_type prog_type;
867 enum bpf_attach_type attach_type;
868 const struct btf_type *t;
869 const char *ctx_name;
873 goto skip_freplace_fixup;
875 id = btf__find_by_name_kind(btf, prog_name, BTF_KIND_FUNC);
876 t = btf__type_by_id(btf, id);
877 t = btf__type_by_id(btf, t->type);
878 if (!btf_is_func_proto(t) || btf_vlen(t) != 1)
879 goto skip_freplace_fixup;
881 /* context argument is a pointer to a struct/typedef */
882 t = btf__type_by_id(btf, btf_params(t)[0].type);
883 while (t && btf_is_mod(t))
884 t = btf__type_by_id(btf, t->type);
885 if (!t || !btf_is_ptr(t))
886 goto skip_freplace_fixup;
887 t = btf__type_by_id(btf, t->type);
888 while (t && btf_is_mod(t))
889 t = btf__type_by_id(btf, t->type);
891 goto skip_freplace_fixup;
893 ctx_name = btf__name_by_offset(btf, t->name_off);
895 if (guess_prog_type_by_ctx_name(ctx_name, &prog_type, &attach_type) == 0) {
896 bpf_program__set_type(prog, prog_type);
897 bpf_program__set_expected_attach_type(prog, attach_type);
900 printf("Using guessed program type '%s' for %s/%s...\n",
901 libbpf_bpf_prog_type_str(prog_type),
902 filename, prog_name);
906 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",
907 ctx_name, filename, prog_name);
915 static int process_prog(const char *filename, struct bpf_object *obj, struct bpf_program *prog)
917 const char *prog_name = bpf_program__name(prog);
918 const char *base_filename = basename(filename);
919 size_t buf_sz = sizeof(verif_log_buf);
920 char *buf = verif_log_buf;
921 struct verif_stats *stats;
925 if (!should_process_file_prog(base_filename, bpf_program__name(prog))) {
930 tmp = realloc(env.prog_stats, (env.prog_stat_cnt + 1) * sizeof(*env.prog_stats));
933 env.prog_stats = tmp;
934 stats = &env.prog_stats[env.prog_stat_cnt++];
935 memset(stats, 0, sizeof(*stats));
938 buf_sz = 16 * 1024 * 1024;
939 buf = malloc(buf_sz);
942 bpf_program__set_log_buf(prog, buf, buf_sz);
943 bpf_program__set_log_level(prog, env.log_level | 4); /* stats + log */
945 bpf_program__set_log_buf(prog, buf, buf_sz);
946 bpf_program__set_log_level(prog, 4); /* only verifier stats */
948 verif_log_buf[0] = '\0';
950 /* increase chances of successful BPF object loading */
951 fixup_obj(obj, prog, base_filename);
953 err = bpf_object__load(obj);
954 env.progs_processed++;
956 stats->file_name = strdup(base_filename);
957 stats->prog_name = strdup(bpf_program__name(prog));
958 stats->stats[VERDICT] = err == 0; /* 1 - success, 0 - failure */
959 parse_verif_log(buf, buf_sz, stats);
962 printf("PROCESSING %s/%s, DURATION US: %ld, VERDICT: %s, VERIFIER LOG:\n%s\n",
963 filename, prog_name, stats->stats[DURATION],
964 err ? "failure" : "success", buf);
967 if (verif_log_buf != buf)
973 static int process_obj(const char *filename)
975 struct bpf_object *obj = NULL, *tobj;
976 struct bpf_program *prog, *tprog, *lprog;
977 libbpf_print_fn_t old_libbpf_print_fn;
978 LIBBPF_OPTS(bpf_object_open_opts, opts);
979 int err = 0, prog_cnt = 0;
981 if (!should_process_file_prog(basename(filename), NULL)) {
983 printf("Skipping '%s' due to filters...\n", filename);
987 if (!is_bpf_obj_file(filename)) {
989 printf("Skipping '%s' as it's not a BPF object file...\n", filename);
994 if (!env.quiet && env.out_fmt == RESFMT_TABLE)
995 printf("Processing '%s'...\n", basename(filename));
997 old_libbpf_print_fn = libbpf_set_print(libbpf_print_fn);
998 obj = bpf_object__open_file(filename, &opts);
1000 /* if libbpf can't open BPF object file, it could be because
1001 * that BPF object file is incomplete and has to be statically
1002 * linked into a final BPF object file; instead of bailing
1003 * out, report it into stderr, mark it as skipped, and
1006 fprintf(stderr, "Failed to open '%s': %d\n", filename, -errno);
1007 env.files_skipped++;
1012 env.files_processed++;
1014 bpf_object__for_each_program(prog, obj) {
1018 if (prog_cnt == 1) {
1019 prog = bpf_object__next_program(obj, NULL);
1020 bpf_program__set_autoload(prog, true);
1021 process_prog(filename, obj, prog);
1025 bpf_object__for_each_program(prog, obj) {
1026 const char *prog_name = bpf_program__name(prog);
1028 tobj = bpf_object__open_file(filename, &opts);
1031 fprintf(stderr, "Failed to open '%s': %d\n", filename, err);
1035 bpf_object__for_each_program(tprog, tobj) {
1036 const char *tprog_name = bpf_program__name(tprog);
1038 if (strcmp(prog_name, tprog_name) == 0) {
1039 bpf_program__set_autoload(tprog, true);
1042 bpf_program__set_autoload(tprog, false);
1046 process_prog(filename, tobj, lprog);
1047 bpf_object__close(tobj);
1051 bpf_object__close(obj);
1052 libbpf_set_print(old_libbpf_print_fn);
1056 static int cmp_stat(const struct verif_stats *s1, const struct verif_stats *s2,
1057 enum stat_id id, bool asc)
1063 cmp = strcmp(s1->file_name, s2->file_name);
1066 cmp = strcmp(s1->prog_name, s2->prog_name);
1073 case MAX_STATES_PER_INSN:
1074 case MARK_READ_MAX_LEN: {
1075 long v1 = s1->stats[id];
1076 long v2 = s2->stats[id];
1079 cmp = v1 < v2 ? -1 : 1;
1083 fprintf(stderr, "Unrecognized stat #%d\n", id);
1087 return asc ? cmp : -cmp;
1090 static int cmp_prog_stats(const void *v1, const void *v2)
1092 const struct verif_stats *s1 = v1, *s2 = v2;
1095 for (i = 0; i < env.sort_spec.spec_cnt; i++) {
1096 cmp = cmp_stat(s1, s2, env.sort_spec.ids[i], env.sort_spec.asc[i]);
1101 /* always disambiguate with file+prog, which are unique */
1102 cmp = strcmp(s1->file_name, s2->file_name);
1105 return strcmp(s1->prog_name, s2->prog_name);
1108 static void fetch_join_stat_value(const struct verif_stats_join *s,
1109 enum stat_id id, enum stat_variant var,
1110 const char **str_val,
1115 if (id == FILE_NAME) {
1116 *str_val = s->file_name;
1119 if (id == PROG_NAME) {
1120 *str_val = s->prog_name;
1124 v1 = s->stats_a ? s->stats_a->stats[id] : 0;
1125 v2 = s->stats_b ? s->stats_b->stats[id] : 0;
1130 *num_val = -DBL_MAX;
1132 *num_val = s->stats_a->stats[id];
1136 *num_val = -DBL_MAX;
1138 *num_val = s->stats_b->stats[id];
1141 if (!s->stats_a || !s->stats_b)
1142 *num_val = -DBL_MAX;
1143 else if (id == VERDICT)
1144 *num_val = v1 == v2 ? 1.0 /* MATCH */ : 0.0 /* MISMATCH */;
1146 *num_val = (double)(v2 - v1);
1149 if (!s->stats_a || !s->stats_b) {
1150 *num_val = -DBL_MAX;
1151 } else if (v1 == 0) {
1155 *num_val = v2 < v1 ? -100.0 : 100.0;
1157 *num_val = (v2 - v1) * 100.0 / v1;
1163 static int cmp_join_stat(const struct verif_stats_join *s1,
1164 const struct verif_stats_join *s2,
1165 enum stat_id id, enum stat_variant var, bool asc)
1167 const char *str1 = NULL, *str2 = NULL;
1171 fetch_join_stat_value(s1, id, var, &str1, &v1);
1172 fetch_join_stat_value(s2, id, var, &str2, &v2);
1175 cmp = strcmp(str1, str2);
1177 cmp = v1 < v2 ? -1 : 1;
1179 return asc ? cmp : -cmp;
1182 static int cmp_join_stats(const void *v1, const void *v2)
1184 const struct verif_stats_join *s1 = v1, *s2 = v2;
1187 for (i = 0; i < env.sort_spec.spec_cnt; i++) {
1188 cmp = cmp_join_stat(s1, s2,
1189 env.sort_spec.ids[i],
1190 env.sort_spec.variants[i],
1191 env.sort_spec.asc[i]);
1196 /* always disambiguate with file+prog, which are unique */
1197 cmp = strcmp(s1->file_name, s2->file_name);
1200 return strcmp(s1->prog_name, s2->prog_name);
1203 #define HEADER_CHAR '-'
1204 #define COLUMN_SEP " "
1206 static void output_header_underlines(void)
1210 for (i = 0; i < env.output_spec.spec_cnt; i++) {
1211 len = env.output_spec.lens[i];
1213 printf("%s", i == 0 ? "" : COLUMN_SEP);
1214 for (j = 0; j < len; j++)
1215 printf("%c", HEADER_CHAR);
1220 static void output_headers(enum resfmt fmt)
1222 const char *fmt_str;
1225 for (i = 0; i < env.output_spec.spec_cnt; i++) {
1226 int id = env.output_spec.ids[i];
1227 int *max_len = &env.output_spec.lens[i];
1230 case RESFMT_TABLE_CALCLEN:
1231 len = snprintf(NULL, 0, "%s", stat_defs[id].header);
1236 fmt_str = stat_defs[id].left_aligned ? "%s%-*s" : "%s%*s";
1237 printf(fmt_str, i == 0 ? "" : COLUMN_SEP, *max_len, stat_defs[id].header);
1238 if (i == env.output_spec.spec_cnt - 1)
1242 printf("%s%s", i == 0 ? "" : ",", stat_defs[id].names[0]);
1243 if (i == env.output_spec.spec_cnt - 1)
1249 if (fmt == RESFMT_TABLE)
1250 output_header_underlines();
1253 static void prepare_value(const struct verif_stats *s, enum stat_id id,
1254 const char **str, long *val)
1258 *str = s ? s->file_name : "N/A";
1261 *str = s ? s->prog_name : "N/A";
1267 *str = s->stats[VERDICT] ? "success" : "failure";
1273 case MAX_STATES_PER_INSN:
1274 case MARK_READ_MAX_LEN:
1275 *val = s ? s->stats[id] : 0;
1278 fprintf(stderr, "Unrecognized stat #%d\n", id);
1283 static void output_stats(const struct verif_stats *s, enum resfmt fmt, bool last)
1287 for (i = 0; i < env.output_spec.spec_cnt; i++) {
1288 int id = env.output_spec.ids[i];
1289 int *max_len = &env.output_spec.lens[i], len;
1290 const char *str = NULL;
1293 prepare_value(s, id, &str, &val);
1296 case RESFMT_TABLE_CALCLEN:
1298 len = snprintf(NULL, 0, "%s", str);
1300 len = snprintf(NULL, 0, "%ld", val);
1306 printf("%s%-*s", i == 0 ? "" : COLUMN_SEP, *max_len, str);
1308 printf("%s%*ld", i == 0 ? "" : COLUMN_SEP, *max_len, val);
1309 if (i == env.output_spec.spec_cnt - 1)
1314 printf("%s%s", i == 0 ? "" : ",", str);
1316 printf("%s%ld", i == 0 ? "" : ",", val);
1317 if (i == env.output_spec.spec_cnt - 1)
1323 if (last && fmt == RESFMT_TABLE) {
1324 output_header_underlines();
1325 printf("Done. Processed %d files, %d programs. Skipped %d files, %d programs.\n",
1326 env.files_processed, env.files_skipped, env.progs_processed, env.progs_skipped);
1330 static int parse_stat_value(const char *str, enum stat_id id, struct verif_stats *st)
1334 st->file_name = strdup(str);
1339 st->prog_name = strdup(str);
1344 if (strcmp(str, "success") == 0) {
1345 st->stats[VERDICT] = true;
1346 } else if (strcmp(str, "failure") == 0) {
1347 st->stats[VERDICT] = false;
1349 fprintf(stderr, "Unrecognized verification verdict '%s'\n", str);
1357 case MAX_STATES_PER_INSN:
1358 case MARK_READ_MAX_LEN: {
1362 if (sscanf(str, "%ld %n", &val, &n) != 1 || n != strlen(str)) {
1364 fprintf(stderr, "Failed to parse '%s' as integer\n", str);
1368 st->stats[id] = val;
1372 fprintf(stderr, "Unrecognized stat #%d\n", id);
1378 static int parse_stats_csv(const char *filename, struct stat_specs *specs,
1379 struct verif_stats **statsp, int *stat_cntp)
1386 f = fopen(filename, "r");
1389 fprintf(stderr, "Failed to open '%s': %d\n", filename, err);
1395 while (fgets(line, sizeof(line), f)) {
1396 char *input = line, *state = NULL, *next;
1397 struct verif_stats *st = NULL;
1403 tmp = realloc(*statsp, (*stat_cntp + 1) * sizeof(**statsp));
1410 st = &(*statsp)[*stat_cntp];
1411 memset(st, 0, sizeof(*st));
1416 while ((next = strtok_r(state ? NULL : input, ",\n", &state))) {
1418 /* for the first line, set up spec stats */
1419 err = parse_stat(next, specs);
1425 /* for all other lines, parse values based on spec */
1426 if (col >= specs->spec_cnt) {
1427 fprintf(stderr, "Found extraneous column #%d in row #%d of '%s'\n",
1428 col, *stat_cntp, filename);
1432 err = parse_stat_value(next, specs->ids[col], st);
1443 if (col < specs->spec_cnt) {
1444 fprintf(stderr, "Not enough columns in row #%d in '%s'\n",
1445 *stat_cntp, filename);
1450 if (!st->file_name || !st->prog_name) {
1451 fprintf(stderr, "Row #%d in '%s' is missing file and/or program name\n",
1452 *stat_cntp, filename);
1457 /* in comparison mode we can only check filters after we
1458 * parsed entire line; if row should be ignored we pretend we
1461 if (!should_process_file_prog(st->file_name, st->prog_name)) {
1462 free(st->file_name);
1463 free(st->prog_name);
1470 fprintf(stderr, "Failed I/O for '%s': %d\n", filename, err);
1478 /* empty/zero stats for mismatched rows */
1479 static const struct verif_stats fallback_stats = { .file_name = "", .prog_name = "" };
1481 static bool is_key_stat(enum stat_id id)
1483 return id == FILE_NAME || id == PROG_NAME;
1486 static void output_comp_header_underlines(void)
1490 for (i = 0; i < env.output_spec.spec_cnt; i++) {
1491 int id = env.output_spec.ids[i];
1492 int max_j = is_key_stat(id) ? 1 : 3;
1494 for (j = 0; j < max_j; j++) {
1495 int len = env.output_spec.lens[3 * i + j];
1497 printf("%s", i + j == 0 ? "" : COLUMN_SEP);
1499 for (k = 0; k < len; k++)
1500 printf("%c", HEADER_CHAR);
1506 static void output_comp_headers(enum resfmt fmt)
1508 static const char *table_sfxs[3] = {" (A)", " (B)", " (DIFF)"};
1509 static const char *name_sfxs[3] = {"_base", "_comp", "_diff"};
1512 for (i = 0; i < env.output_spec.spec_cnt; i++) {
1513 int id = env.output_spec.ids[i];
1514 /* key stats don't have A/B/DIFF columns, they are common for both data sets */
1515 int max_j = is_key_stat(id) ? 1 : 3;
1517 for (j = 0; j < max_j; j++) {
1518 int *max_len = &env.output_spec.lens[3 * i + j];
1519 bool last = (i == env.output_spec.spec_cnt - 1) && (j == max_j - 1);
1523 case RESFMT_TABLE_CALCLEN:
1524 sfx = is_key_stat(id) ? "" : table_sfxs[j];
1525 len = snprintf(NULL, 0, "%s%s", stat_defs[id].header, sfx);
1530 sfx = is_key_stat(id) ? "" : table_sfxs[j];
1531 printf("%s%-*s%s", i + j == 0 ? "" : COLUMN_SEP,
1532 *max_len - (int)strlen(sfx), stat_defs[id].header, sfx);
1537 sfx = is_key_stat(id) ? "" : name_sfxs[j];
1538 printf("%s%s%s", i + j == 0 ? "" : ",", stat_defs[id].names[0], sfx);
1546 if (fmt == RESFMT_TABLE)
1547 output_comp_header_underlines();
1550 static void output_comp_stats(const struct verif_stats_join *join_stats,
1551 enum resfmt fmt, bool last)
1553 const struct verif_stats *base = join_stats->stats_a;
1554 const struct verif_stats *comp = join_stats->stats_b;
1555 char base_buf[1024] = {}, comp_buf[1024] = {}, diff_buf[1024] = {};
1558 for (i = 0; i < env.output_spec.spec_cnt; i++) {
1559 int id = env.output_spec.ids[i], len;
1560 int *max_len_base = &env.output_spec.lens[3 * i + 0];
1561 int *max_len_comp = &env.output_spec.lens[3 * i + 1];
1562 int *max_len_diff = &env.output_spec.lens[3 * i + 2];
1563 const char *base_str = NULL, *comp_str = NULL;
1564 long base_val = 0, comp_val = 0, diff_val = 0;
1566 prepare_value(base, id, &base_str, &base_val);
1567 prepare_value(comp, id, &comp_str, &comp_val);
1569 /* normalize all the outputs to be in string buffers for simplicity */
1570 if (is_key_stat(id)) {
1571 /* key stats (file and program name) are always strings */
1573 snprintf(base_buf, sizeof(base_buf), "%s", base_str);
1575 snprintf(base_buf, sizeof(base_buf), "%s", comp_str);
1576 } else if (base_str) {
1577 snprintf(base_buf, sizeof(base_buf), "%s", base_str);
1578 snprintf(comp_buf, sizeof(comp_buf), "%s", comp_str);
1580 snprintf(diff_buf, sizeof(diff_buf), "%s", "N/A");
1581 else if (strcmp(base_str, comp_str) == 0)
1582 snprintf(diff_buf, sizeof(diff_buf), "%s", "MATCH");
1584 snprintf(diff_buf, sizeof(diff_buf), "%s", "MISMATCH");
1589 snprintf(base_buf, sizeof(base_buf), "%ld", base_val);
1591 snprintf(base_buf, sizeof(base_buf), "%s", "N/A");
1593 snprintf(comp_buf, sizeof(comp_buf), "%ld", comp_val);
1595 snprintf(comp_buf, sizeof(comp_buf), "%s", "N/A");
1597 diff_val = comp_val - base_val;
1598 if (!base || !comp) {
1599 snprintf(diff_buf, sizeof(diff_buf), "%s", "N/A");
1601 if (base_val == 0) {
1602 if (comp_val == base_val)
1603 p = 0.0; /* avoid +0 (+100%) case */
1605 p = comp_val < base_val ? -100.0 : 100.0;
1607 p = diff_val * 100.0 / base_val;
1609 snprintf(diff_buf, sizeof(diff_buf), "%+ld (%+.2lf%%)", diff_val, p);
1614 case RESFMT_TABLE_CALCLEN:
1615 len = strlen(base_buf);
1616 if (len > *max_len_base)
1617 *max_len_base = len;
1618 if (!is_key_stat(id)) {
1619 len = strlen(comp_buf);
1620 if (len > *max_len_comp)
1621 *max_len_comp = len;
1622 len = strlen(diff_buf);
1623 if (len > *max_len_diff)
1624 *max_len_diff = len;
1627 case RESFMT_TABLE: {
1628 /* string outputs are left-aligned, number outputs are right-aligned */
1629 const char *fmt = base_str ? "%s%-*s" : "%s%*s";
1631 printf(fmt, i == 0 ? "" : COLUMN_SEP, *max_len_base, base_buf);
1632 if (!is_key_stat(id)) {
1633 printf(fmt, COLUMN_SEP, *max_len_comp, comp_buf);
1634 printf(fmt, COLUMN_SEP, *max_len_diff, diff_buf);
1636 if (i == env.output_spec.spec_cnt - 1)
1641 printf("%s%s", i == 0 ? "" : ",", base_buf);
1642 if (!is_key_stat(id)) {
1643 printf("%s%s", i == 0 ? "" : ",", comp_buf);
1644 printf("%s%s", i == 0 ? "" : ",", diff_buf);
1646 if (i == env.output_spec.spec_cnt - 1)
1652 if (last && fmt == RESFMT_TABLE)
1653 output_comp_header_underlines();
1656 static int cmp_stats_key(const struct verif_stats *base, const struct verif_stats *comp)
1660 r = strcmp(base->file_name, comp->file_name);
1663 return strcmp(base->prog_name, comp->prog_name);
1666 static bool is_join_stat_filter_matched(struct filter *f, const struct verif_stats_join *stats)
1668 static const double eps = 1e-9;
1669 const char *str = NULL;
1672 fetch_join_stat_value(stats, f->stat_id, f->stat_var, &str, &value);
1675 case OP_EQ: return value > f->value - eps && value < f->value + eps;
1676 case OP_NEQ: return value < f->value - eps || value > f->value + eps;
1677 case OP_LT: return value < f->value - eps;
1678 case OP_LE: return value <= f->value + eps;
1679 case OP_GT: return value > f->value + eps;
1680 case OP_GE: return value >= f->value - eps;
1683 fprintf(stderr, "BUG: unknown filter op %d!\n", f->op);
1687 static bool should_output_join_stats(const struct verif_stats_join *stats)
1690 int i, allow_cnt = 0;
1692 for (i = 0; i < env.deny_filter_cnt; i++) {
1693 f = &env.deny_filters[i];
1694 if (f->kind != FILTER_STAT)
1697 if (is_join_stat_filter_matched(f, stats))
1701 for (i = 0; i < env.allow_filter_cnt; i++) {
1702 f = &env.allow_filters[i];
1703 if (f->kind != FILTER_STAT)
1707 if (is_join_stat_filter_matched(f, stats))
1711 /* if there are no stat allowed filters, pass everything through */
1712 return allow_cnt == 0;
1715 static int handle_comparison_mode(void)
1717 struct stat_specs base_specs = {}, comp_specs = {};
1718 struct stat_specs tmp_sort_spec;
1719 enum resfmt cur_fmt;
1720 int err, i, j, last_idx;
1722 if (env.filename_cnt != 2) {
1723 fprintf(stderr, "Comparison mode expects exactly two input CSV files!\n\n");
1724 argp_help(&argp, stderr, ARGP_HELP_USAGE, "veristat");
1728 err = parse_stats_csv(env.filenames[0], &base_specs,
1729 &env.baseline_stats, &env.baseline_stat_cnt);
1731 fprintf(stderr, "Failed to parse stats from '%s': %d\n", env.filenames[0], err);
1734 err = parse_stats_csv(env.filenames[1], &comp_specs,
1735 &env.prog_stats, &env.prog_stat_cnt);
1737 fprintf(stderr, "Failed to parse stats from '%s': %d\n", env.filenames[1], err);
1741 /* To keep it simple we validate that the set and order of stats in
1742 * both CSVs are exactly the same. This can be lifted with a bit more
1743 * pre-processing later.
1745 if (base_specs.spec_cnt != comp_specs.spec_cnt) {
1746 fprintf(stderr, "Number of stats in '%s' and '%s' differs (%d != %d)!\n",
1747 env.filenames[0], env.filenames[1],
1748 base_specs.spec_cnt, comp_specs.spec_cnt);
1751 for (i = 0; i < base_specs.spec_cnt; i++) {
1752 if (base_specs.ids[i] != comp_specs.ids[i]) {
1753 fprintf(stderr, "Stats composition differs between '%s' and '%s' (%s != %s)!\n",
1754 env.filenames[0], env.filenames[1],
1755 stat_defs[base_specs.ids[i]].names[0],
1756 stat_defs[comp_specs.ids[i]].names[0]);
1761 /* Replace user-specified sorting spec with file+prog sorting rule to
1762 * be able to join two datasets correctly. Once we are done, we will
1763 * restore the original sort spec.
1765 tmp_sort_spec = env.sort_spec;
1766 env.sort_spec = join_sort_spec;
1767 qsort(env.prog_stats, env.prog_stat_cnt, sizeof(*env.prog_stats), cmp_prog_stats);
1768 qsort(env.baseline_stats, env.baseline_stat_cnt, sizeof(*env.baseline_stats), cmp_prog_stats);
1769 env.sort_spec = tmp_sort_spec;
1771 /* Join two datasets together. If baseline and comparison datasets
1772 * have different subset of rows (we match by 'object + prog' as
1773 * a unique key) then assume empty/missing/zero value for rows that
1774 * are missing in the opposite data set.
1777 while (i < env.baseline_stat_cnt || j < env.prog_stat_cnt) {
1778 const struct verif_stats *base, *comp;
1779 struct verif_stats_join *join;
1783 base = i < env.baseline_stat_cnt ? &env.baseline_stats[i] : &fallback_stats;
1784 comp = j < env.prog_stat_cnt ? &env.prog_stats[j] : &fallback_stats;
1786 if (!base->file_name || !base->prog_name) {
1787 fprintf(stderr, "Entry #%d in '%s' doesn't have file and/or program name specified!\n",
1788 i, env.filenames[0]);
1791 if (!comp->file_name || !comp->prog_name) {
1792 fprintf(stderr, "Entry #%d in '%s' doesn't have file and/or program name specified!\n",
1793 j, env.filenames[1]);
1797 tmp = realloc(env.join_stats, (env.join_stat_cnt + 1) * sizeof(*env.join_stats));
1800 env.join_stats = tmp;
1802 join = &env.join_stats[env.join_stat_cnt];
1803 memset(join, 0, sizeof(*join));
1805 r = cmp_stats_key(base, comp);
1807 join->file_name = base->file_name;
1808 join->prog_name = base->prog_name;
1809 join->stats_a = base;
1810 join->stats_b = comp;
1813 } else if (comp == &fallback_stats || r < 0) {
1814 join->file_name = base->file_name;
1815 join->prog_name = base->prog_name;
1816 join->stats_a = base;
1817 join->stats_b = NULL;
1820 join->file_name = comp->file_name;
1821 join->prog_name = comp->prog_name;
1822 join->stats_a = NULL;
1823 join->stats_b = comp;
1826 env.join_stat_cnt += 1;
1829 /* now sort joined results accorsing to sort spec */
1830 qsort(env.join_stats, env.join_stat_cnt, sizeof(*env.join_stats), cmp_join_stats);
1832 /* for human-readable table output we need to do extra pass to
1833 * calculate column widths, so we substitute current output format
1834 * with RESFMT_TABLE_CALCLEN and later revert it back to RESFMT_TABLE
1835 * and do everything again.
1837 if (env.out_fmt == RESFMT_TABLE)
1838 cur_fmt = RESFMT_TABLE_CALCLEN;
1840 cur_fmt = env.out_fmt;
1843 output_comp_headers(cur_fmt);
1845 for (i = 0; i < env.join_stat_cnt; i++) {
1846 const struct verif_stats_join *join = &env.join_stats[i];
1848 if (!should_output_join_stats(join))
1851 if (cur_fmt == RESFMT_TABLE_CALCLEN)
1854 output_comp_stats(join, cur_fmt, i == last_idx);
1857 if (cur_fmt == RESFMT_TABLE_CALCLEN) {
1858 cur_fmt = RESFMT_TABLE;
1859 goto one_more_time; /* ... this time with feeling */
1865 static bool is_stat_filter_matched(struct filter *f, const struct verif_stats *stats)
1867 long value = stats->stats[f->stat_id];
1870 case OP_EQ: return value == f->value;
1871 case OP_NEQ: return value != f->value;
1872 case OP_LT: return value < f->value;
1873 case OP_LE: return value <= f->value;
1874 case OP_GT: return value > f->value;
1875 case OP_GE: return value >= f->value;
1878 fprintf(stderr, "BUG: unknown filter op %d!\n", f->op);
1882 static bool should_output_stats(const struct verif_stats *stats)
1885 int i, allow_cnt = 0;
1887 for (i = 0; i < env.deny_filter_cnt; i++) {
1888 f = &env.deny_filters[i];
1889 if (f->kind != FILTER_STAT)
1892 if (is_stat_filter_matched(f, stats))
1896 for (i = 0; i < env.allow_filter_cnt; i++) {
1897 f = &env.allow_filters[i];
1898 if (f->kind != FILTER_STAT)
1902 if (is_stat_filter_matched(f, stats))
1906 /* if there are no stat allowed filters, pass everything through */
1907 return allow_cnt == 0;
1910 static void output_prog_stats(void)
1912 const struct verif_stats *stats;
1913 int i, last_stat_idx = 0;
1915 if (env.out_fmt == RESFMT_TABLE) {
1916 /* calculate column widths */
1917 output_headers(RESFMT_TABLE_CALCLEN);
1918 for (i = 0; i < env.prog_stat_cnt; i++) {
1919 stats = &env.prog_stats[i];
1920 if (!should_output_stats(stats))
1922 output_stats(stats, RESFMT_TABLE_CALCLEN, false);
1927 /* actually output the table */
1928 output_headers(env.out_fmt);
1929 for (i = 0; i < env.prog_stat_cnt; i++) {
1930 stats = &env.prog_stats[i];
1931 if (!should_output_stats(stats))
1933 output_stats(stats, env.out_fmt, i == last_stat_idx);
1937 static int handle_verif_mode(void)
1941 if (env.filename_cnt == 0) {
1942 fprintf(stderr, "Please provide path to BPF object file!\n\n");
1943 argp_help(&argp, stderr, ARGP_HELP_USAGE, "veristat");
1947 for (i = 0; i < env.filename_cnt; i++) {
1948 err = process_obj(env.filenames[i]);
1950 fprintf(stderr, "Failed to process '%s': %d\n", env.filenames[i], err);
1955 qsort(env.prog_stats, env.prog_stat_cnt, sizeof(*env.prog_stats), cmp_prog_stats);
1957 output_prog_stats();
1962 static int handle_replay_mode(void)
1964 struct stat_specs specs = {};
1967 if (env.filename_cnt != 1) {
1968 fprintf(stderr, "Replay mode expects exactly one input CSV file!\n\n");
1969 argp_help(&argp, stderr, ARGP_HELP_USAGE, "veristat");
1973 err = parse_stats_csv(env.filenames[0], &specs,
1974 &env.prog_stats, &env.prog_stat_cnt);
1976 fprintf(stderr, "Failed to parse stats from '%s': %d\n", env.filenames[0], err);
1980 qsort(env.prog_stats, env.prog_stat_cnt, sizeof(*env.prog_stats), cmp_prog_stats);
1982 output_prog_stats();
1987 int main(int argc, char **argv)
1991 if (argp_parse(&argp, argc, argv, 0, NULL, NULL))
1994 if (env.verbose && env.quiet) {
1995 fprintf(stderr, "Verbose and quiet modes are incompatible, please specify just one or neither!\n\n");
1996 argp_help(&argp, stderr, ARGP_HELP_USAGE, "veristat");
1999 if (env.verbose && env.log_level == 0)
2002 if (env.output_spec.spec_cnt == 0) {
2003 if (env.out_fmt == RESFMT_CSV)
2004 env.output_spec = default_csv_output_spec;
2006 env.output_spec = default_output_spec;
2008 if (env.sort_spec.spec_cnt == 0)
2009 env.sort_spec = default_sort_spec;
2011 if (env.comparison_mode && env.replay_mode) {
2012 fprintf(stderr, "Can't specify replay and comparison mode at the same time!\n\n");
2013 argp_help(&argp, stderr, ARGP_HELP_USAGE, "veristat");
2017 if (env.comparison_mode)
2018 err = handle_comparison_mode();
2019 else if (env.replay_mode)
2020 err = handle_replay_mode();
2022 err = handle_verif_mode();
2024 free_verif_stats(env.prog_stats, env.prog_stat_cnt);
2025 free_verif_stats(env.baseline_stats, env.baseline_stat_cnt);
2026 free(env.join_stats);
2027 for (i = 0; i < env.filename_cnt; i++)
2028 free(env.filenames[i]);
2029 free(env.filenames);
2030 for (i = 0; i < env.allow_filter_cnt; i++) {
2031 free(env.allow_filters[i].any_glob);
2032 free(env.allow_filters[i].file_glob);
2033 free(env.allow_filters[i].prog_glob);
2035 free(env.allow_filters);
2036 for (i = 0; i < env.deny_filter_cnt; i++) {
2037 free(env.deny_filters[i].any_glob);
2038 free(env.deny_filters[i].file_glob);
2039 free(env.deny_filters[i].prog_glob);
2041 free(env.deny_filters);