Skip to content

Commit 82c1f13

Browse files
mykyta5anakryiko
authored andcommitted
selftests/bpf: Add more stats into veristat
Extend veristat to collect and print more stats, namely: - program size in instructions - jited program size in bytes - program type - attach type - stack depth Signed-off-by: Mykyta Yatsenko <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 442bc81 commit 82c1f13

File tree

1 file changed

+58
-6
lines changed

1 file changed

+58
-6
lines changed

tools/testing/selftests/bpf/veristat.c

Lines changed: 58 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ enum stat_id {
3434
PEAK_STATES,
3535
MAX_STATES_PER_INSN,
3636
MARK_READ_MAX_LEN,
37+
SIZE,
38+
JITED_SIZE,
39+
STACK,
40+
PROG_TYPE,
41+
ATTACH_TYPE,
3742

3843
FILE_NAME,
3944
PROG_NAME,
@@ -640,19 +645,21 @@ static int append_filter_file(const char *path)
640645
}
641646

642647
static const struct stat_specs default_output_spec = {
643-
.spec_cnt = 7,
648+
.spec_cnt = 8,
644649
.ids = {
645650
FILE_NAME, PROG_NAME, VERDICT, DURATION,
646-
TOTAL_INSNS, TOTAL_STATES, PEAK_STATES,
651+
TOTAL_INSNS, TOTAL_STATES, SIZE, JITED_SIZE
647652
},
648653
};
649654

650655
static const struct stat_specs default_csv_output_spec = {
651-
.spec_cnt = 9,
656+
.spec_cnt = 14,
652657
.ids = {
653658
FILE_NAME, PROG_NAME, VERDICT, DURATION,
654659
TOTAL_INSNS, TOTAL_STATES, PEAK_STATES,
655660
MAX_STATES_PER_INSN, MARK_READ_MAX_LEN,
661+
SIZE, JITED_SIZE, PROG_TYPE, ATTACH_TYPE,
662+
STACK,
656663
},
657664
};
658665

@@ -688,6 +695,11 @@ static struct stat_def {
688695
[PEAK_STATES] = { "Peak states", {"peak_states"}, },
689696
[MAX_STATES_PER_INSN] = { "Max states per insn", {"max_states_per_insn"}, },
690697
[MARK_READ_MAX_LEN] = { "Max mark read length", {"max_mark_read_len", "mark_read"}, },
698+
[SIZE] = { "Program size", {"prog_size"}, },
699+
[JITED_SIZE] = { "Jited size", {"prog_size_jited"}, },
700+
[STACK] = {"Stack depth", {"stack_depth", "stack"}, },
701+
[PROG_TYPE] = { "Program type", {"prog_type"}, },
702+
[ATTACH_TYPE] = { "Attach type", {"attach_type", }, },
691703
};
692704

693705
static bool parse_stat_id_var(const char *name, size_t len, int *id,
@@ -835,7 +847,8 @@ static char verif_log_buf[64 * 1024];
835847
static int parse_verif_log(char * const buf, size_t buf_sz, struct verif_stats *s)
836848
{
837849
const char *cur;
838-
int pos, lines;
850+
int pos, lines, sub_stack, cnt = 0;
851+
char *state = NULL, *token, stack[512];
839852

840853
buf[buf_sz - 1] = '\0';
841854

@@ -853,15 +866,22 @@ static int parse_verif_log(char * const buf, size_t buf_sz, struct verif_stats *
853866

854867
if (1 == sscanf(cur, "verification time %ld usec\n", &s->stats[DURATION]))
855868
continue;
856-
if (6 == sscanf(cur, "processed %ld insns (limit %*d) max_states_per_insn %ld total_states %ld peak_states %ld mark_read %ld",
869+
if (5 == sscanf(cur, "processed %ld insns (limit %*d) max_states_per_insn %ld total_states %ld peak_states %ld mark_read %ld",
857870
&s->stats[TOTAL_INSNS],
858871
&s->stats[MAX_STATES_PER_INSN],
859872
&s->stats[TOTAL_STATES],
860873
&s->stats[PEAK_STATES],
861874
&s->stats[MARK_READ_MAX_LEN]))
862875
continue;
863-
}
864876

877+
if (1 == sscanf(cur, "stack depth %511s", stack))
878+
continue;
879+
}
880+
while ((token = strtok_r(cnt++ ? NULL : stack, "+", &state))) {
881+
if (sscanf(token, "%d", &sub_stack) == 0)
882+
break;
883+
s->stats[STACK] += sub_stack;
884+
}
865885
return 0;
866886
}
867887

@@ -1146,8 +1166,11 @@ static int process_prog(const char *filename, struct bpf_object *obj, struct bpf
11461166
char *buf;
11471167
int buf_sz, log_level;
11481168
struct verif_stats *stats;
1169+
struct bpf_prog_info info;
1170+
__u32 info_len = sizeof(info);
11491171
int err = 0;
11501172
void *tmp;
1173+
int fd;
11511174

11521175
if (!should_process_file_prog(base_filename, bpf_program__name(prog))) {
11531176
env.progs_skipped++;
@@ -1196,6 +1219,15 @@ static int process_prog(const char *filename, struct bpf_object *obj, struct bpf
11961219
stats->file_name = strdup(base_filename);
11971220
stats->prog_name = strdup(bpf_program__name(prog));
11981221
stats->stats[VERDICT] = err == 0; /* 1 - success, 0 - failure */
1222+
stats->stats[SIZE] = bpf_program__insn_cnt(prog);
1223+
stats->stats[PROG_TYPE] = bpf_program__type(prog);
1224+
stats->stats[ATTACH_TYPE] = bpf_program__expected_attach_type(prog);
1225+
1226+
memset(&info, 0, info_len);
1227+
fd = bpf_program__fd(prog);
1228+
if (fd > 0 && bpf_prog_get_info_by_fd(fd, &info, &info_len) == 0)
1229+
stats->stats[JITED_SIZE] = info.jited_prog_len;
1230+
11991231
parse_verif_log(buf, buf_sz, stats);
12001232

12011233
if (env.verbose) {
@@ -1309,6 +1341,11 @@ static int cmp_stat(const struct verif_stats *s1, const struct verif_stats *s2,
13091341
case PROG_NAME:
13101342
cmp = strcmp(s1->prog_name, s2->prog_name);
13111343
break;
1344+
case ATTACH_TYPE:
1345+
case PROG_TYPE:
1346+
case SIZE:
1347+
case JITED_SIZE:
1348+
case STACK:
13121349
case VERDICT:
13131350
case DURATION:
13141351
case TOTAL_INSNS:
@@ -1523,12 +1560,27 @@ static void prepare_value(const struct verif_stats *s, enum stat_id id,
15231560
else
15241561
*str = s->stats[VERDICT] ? "success" : "failure";
15251562
break;
1563+
case ATTACH_TYPE:
1564+
if (!s)
1565+
*str = "N/A";
1566+
else
1567+
*str = libbpf_bpf_attach_type_str(s->stats[ATTACH_TYPE]) ?: "N/A";
1568+
break;
1569+
case PROG_TYPE:
1570+
if (!s)
1571+
*str = "N/A";
1572+
else
1573+
*str = libbpf_bpf_prog_type_str(s->stats[PROG_TYPE]) ?: "N/A";
1574+
break;
15261575
case DURATION:
15271576
case TOTAL_INSNS:
15281577
case TOTAL_STATES:
15291578
case PEAK_STATES:
15301579
case MAX_STATES_PER_INSN:
15311580
case MARK_READ_MAX_LEN:
1581+
case STACK:
1582+
case SIZE:
1583+
case JITED_SIZE:
15321584
*val = s ? s->stats[id] : 0;
15331585
break;
15341586
default:

0 commit comments

Comments
 (0)