Skip to contentSkip to conversation

selftests/bpf: add more stats into veristat

PATCH Superseded Mykyta Yatsenko 2 participants 2 messages Dec 6, 2024

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 selftests/bpf: add more stats into veristat

From: Mykyta Yatsenko <[email protected]>

Extend veristat to collect and print more stats, namely:
- program size in instructions
- jited program size in bytes
- program type
- attach type
- stack depth

---
 tools/testing/selftests/bpf/veristat.c | 68 +++++++++++++++++++++++---
 1 file changed, 62 insertions(+), 6 deletions(-)
tools/testing/selftests/bpf/veristat.c +62 -6
index e12ef953fba8..cda8c83ebf24 100644
@@ -26,6 +26,9 @@
 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
 #endif
 
+#define _STR(x) #x
+#define STR(x) _STR(x)
+
 enum stat_id {
 	VERDICT,
 	DURATION,
@@ -34,6 +37,11 @@ enum stat_id {
 	PEAK_STATES,
 	MAX_STATES_PER_INSN,
 	MARK_READ_MAX_LEN,
+	SIZE,
+	JITED_SIZE,
+	STACK,
+	PROG_TYPE,
+	ATTACH_TYPE,
 
 	FILE_NAME,
 	PROG_NAME,
@@ -640,19 +648,21 @@ static int append_filter_file(const char *path)
 }
 
 static const struct stat_specs default_output_spec = {
-	.spec_cnt = 7,
+	.spec_cnt = 8,
 	.ids = {
 		FILE_NAME, PROG_NAME, VERDICT, DURATION,
-		TOTAL_INSNS, TOTAL_STATES, PEAK_STATES,
+		TOTAL_INSNS, TOTAL_STATES, SIZE, JITED_SIZE
 	},
 };
 
 static const struct stat_specs default_csv_output_spec = {
-	.spec_cnt = 9,
+	.spec_cnt = 14,
 	.ids = {
 		FILE_NAME, PROG_NAME, VERDICT, DURATION,
 		TOTAL_INSNS, TOTAL_STATES, PEAK_STATES,
 		MAX_STATES_PER_INSN, MARK_READ_MAX_LEN,
+		SIZE, JITED_SIZE, PROG_TYPE, ATTACH_TYPE,
+		STACK,
 	},
 };
 
@@ -688,6 +698,11 @@ static struct stat_def {
 	[PEAK_STATES] = { "Peak states", {"peak_states"}, },
 	[MAX_STATES_PER_INSN] = { "Max states per insn", {"max_states_per_insn"}, },
 	[MARK_READ_MAX_LEN] = { "Max mark read length", {"max_mark_read_len", "mark_read"}, },
+	[SIZE] = { "Prog size", {"prog_size"}, },
+	[JITED_SIZE] = { "Jited size", {"prog_size_jited"}, },
+	[STACK] = {"Stack depth", {"stack_depth", "stack"}, },
+	[PROG_TYPE] = { "Program type", {"prog_type"}, },
+	[ATTACH_TYPE] = { "Attach type", {"attach_type", }, },
 };
 
 static bool parse_stat_id_var(const char *name, size_t len, int *id,
@@ -835,7 +850,8 @@ static char verif_log_buf[64 * 1024];
 static int parse_verif_log(char * const buf, size_t buf_sz, struct verif_stats *s)
 {
 	const char *cur;
-	int pos, lines;
+	int pos, lines, sub_stack;
+	char *save_ptr, *token, stack[PATH_MAX + 1] = {'\0'};
 
 	buf[buf_sz - 1] = '\0';
 
@@ -853,15 +869,24 @@ static int parse_verif_log(char * const buf, size_t buf_sz, struct verif_stats *
 
 		if (1 == sscanf(cur, "verification time %ld usec\n", &s->stats[DURATION]))
 			continue;
-		if (6 == sscanf(cur, "processed %ld insns (limit %*d) max_states_per_insn %ld total_states %ld peak_states %ld mark_read %ld",
+		if (5 == sscanf(cur, "processed %ld insns (limit %*d) max_states_per_insn %ld total_states %ld peak_states %ld mark_read %ld",
 				&s->stats[TOTAL_INSNS],
 				&s->stats[MAX_STATES_PER_INSN],
 				&s->stats[TOTAL_STATES],
 				&s->stats[PEAK_STATES],
 				&s->stats[MARK_READ_MAX_LEN]))
 			continue;
-	}
 
+		if (1 == sscanf(cur, "stack depth %" STR(PATH_MAX) "s", stack))
+			continue;
+	}
+	token = strtok_r(stack, "+", &save_ptr);
+	while (token && token - stack < PATH_MAX) {
+		if (sscanf(token, "%d", &sub_stack) == 0)
+			break;
+		s->stats[STACK] += sub_stack;
+		token = strtok_r(NULL, "+", &save_ptr);
+	}
 	return 0;
 }
 
@@ -1146,8 +1171,11 @@ static int process_prog(const char *filename, struct bpf_object *obj, struct bpf
 	char *buf;
 	int buf_sz, log_level;
 	struct verif_stats *stats;
+	struct bpf_prog_info info;
+	__u32 info_len = sizeof(info);
 	int err = 0;
 	void *tmp;
+	int fd;
 
 	if (!should_process_file_prog(base_filename, bpf_program__name(prog))) {
 		env.progs_skipped++;
@@ -1196,6 +1224,14 @@ static int process_prog(const char *filename, struct bpf_object *obj, struct bpf
 	stats->file_name = strdup(base_filename);
 	stats->prog_name = strdup(bpf_program__name(prog));
 	stats->stats[VERDICT] = err == 0; /* 1 - success, 0 - failure */
+	stats->stats[SIZE] = bpf_program__insn_cnt(prog);
+	stats->stats[PROG_TYPE] = bpf_program__type(prog);
+	stats->stats[ATTACH_TYPE] = bpf_program__expected_attach_type(prog);
+	fd = bpf_program__fd(prog);
+	memset(&info, 0, info_len);
+	if (fd > 0 && bpf_prog_get_info_by_fd(fd, &info, &info_len) == 0)
+		stats->stats[JITED_SIZE] = info.jited_prog_len;
+
 	parse_verif_log(buf, buf_sz, stats);
 
 	if (env.verbose) {
@@ -1309,6 +1345,11 @@ static int cmp_stat(const struct verif_stats *s1, const struct verif_stats *s2,
 	case PROG_NAME:
 		cmp = strcmp(s1->prog_name, s2->prog_name);
 		break;
+	case ATTACH_TYPE:
+	case PROG_TYPE:
+	case SIZE:
+	case JITED_SIZE:
+	case STACK:
 	case VERDICT:
 	case DURATION:
 	case TOTAL_INSNS:
@@ -1523,12 +1564,27 @@ static void prepare_value(const struct verif_stats *s, enum stat_id id,
 		else
 			*str = s->stats[VERDICT] ? "success" : "failure";
 		break;
+	case ATTACH_TYPE:
+		if (!s)
+			*str = "N/A";
+		else
+			*str = libbpf_bpf_attach_type_str(s->stats[ATTACH_TYPE]) ? : "N/A";
+		break;
+	case PROG_TYPE:
+		if (!s)
+			*str = "N/A";
+		else
+			*str = libbpf_bpf_prog_type_str(s->stats[PROG_TYPE]) ? : "N/A";
+		break;
 	case DURATION:
 	case TOTAL_INSNS:
 	case TOTAL_STATES:
 	case PEAK_STATES:
 	case MAX_STATES_PER_INSN:
 	case MARK_READ_MAX_LEN:
+	case STACK:
+	case SIZE:
+	case JITED_SIZE:
 		*val = s ? s->stats[id] : 0;
 		break;
 	default:
Signature · 1 line
2.47.1
#1 lore raw
Quoted 10 lines · Mykyta Yatsenko

From: Mykyta Yatsenko <[email protected]>

Extend veristat to collect and print more stats, namely:
- program size in instructions
- jited program size in bytes
- program type
- attach type
- stack depth

Quoted 5 lines
---
 tools/testing/selftests/bpf/veristat.c | 68 +++++++++++++++++++++++---
 1 file changed, 62 insertions(+), 6 deletions(-)

looks good besides the PATH_MAX use, let's fix that (plus a few nitpicks with style).

pw-bot: cr

Quoted 4 lines
tools/testing/selftests/bpf/veristat.c +0 -0
index e12ef953fba8..cda8c83ebf24 100644

[...]

tools/testing/selftests/bpf/veristat.c static struct stat_def { · line 701

Quoted 5 lines
tools/testing/selftests/bpf/veristat.c +1 -0
@@ -688,6 +698,11 @@ static struct stat_def {
        [PEAK_STATES] = { "Peak states", {"peak_states"}, },
        [MAX_STATES_PER_INSN] = { "Max states per insn", {"max_states_per_insn"}, },
        [MARK_READ_MAX_LEN] = { "Max mark read length", {"max_mark_read_len", "mark_read"}, },
+       [SIZE] = { "Prog size", {"prog_size"}, },

nit: "Prog size" -> "Program size", this is UI ;)

Quoted 8 lines
tools/testing/selftests/bpf/veristat.c +4 -0
+       [JITED_SIZE] = { "Jited size", {"prog_size_jited"}, },
+       [STACK] = {"Stack depth", {"stack_depth", "stack"}, },
+       [PROG_TYPE] = { "Program type", {"prog_type"}, },
+       [ATTACH_TYPE] = { "Attach type", {"attach_type", }, },
 };
 
 static bool parse_stat_id_var(const char *name, size_t len, int *id,

tools/testing/selftests/bpf/veristat.c static char verif_log_buf[64 * 1024]; · line 854

Quoted 6 lines
tools/testing/selftests/bpf/veristat.c +2 -1
@@ -835,7 +850,8 @@ static char verif_log_buf[64 * 1024];
 static int parse_verif_log(char * const buf, size_t buf_sz, struct verif_stats *s)
 {
        const char *cur;
-       int pos, lines;
+       int pos, lines, sub_stack;
+       char *save_ptr, *token, stack[PATH_MAX + 1] = {'\0'};

PATH_MAX is both an overkill and is unrelated to stack depth string. Let's just hard-code it to something like 256 or 512, and drop the STR/_STR stuff

Quoted 12 lines
tools/testing/selftests/bpf/veristat.c +1 -1
        buf[buf_sz - 1] = '\0';
 
@@ -853,15 +869,24 @@ static int parse_verif_log(char * const buf, size_t buf_sz, struct verif_stats *
 
                if (1 == sscanf(cur, "verification time %ld usec\n", &s->stats[DURATION]))
                        continue;
-               if (6 == sscanf(cur, "processed %ld insns (limit %*d) max_states_per_insn %ld total_states %ld peak_states %ld mark_read %ld",
+               if (5 == sscanf(cur, "processed %ld insns (limit %*d) max_states_per_insn %ld total_states %ld peak_states %ld mark_read %ld",
                                &s->stats[TOTAL_INSNS],
                                &s->stats[MAX_STATES_PER_INSN],
                                &s->stats[TOTAL_STATES],

tools/testing/selftests/bpf/veristat.c static int parse_verif_log(char * const buf, size_t buf_sz, struct verif_stats * · line 884

Quoted 10 lines
tools/testing/selftests/bpf/veristat.c +5 -1
                                &s->stats[PEAK_STATES],
                                &s->stats[MARK_READ_MAX_LEN]))
                        continue;
-       }
 
+               if (1 == sscanf(cur, "stack depth %" STR(PATH_MAX) "s", stack))
+                       continue;
+       }
+       token = strtok_r(stack, "+", &save_ptr);
+       while (token && token - stack < PATH_MAX) {

why this PATH_MAX condition? I'm not following what we are guarding against here, tbh

tools/testing/selftests/bpf/veristat.c static int parse_verif_log(char * const buf, size_t buf_sz, struct verif_stats * · line 889

Quoted 5 lines
tools/testing/selftests/bpf/veristat.c +5 -0
+               if (sscanf(token, "%d", &sub_stack) == 0)
+                       break;
+               s->stats[STACK] += sub_stack;
+               token = strtok_r(NULL, "+", &save_ptr);
+       }

for the strtok_r() loop, see parse_stats(), I think it's nicer than having to separate strtok_r() calls

Quoted 16 lines
tools/testing/selftests/bpf/veristat.c +3 -0
        return 0;
 }
 
@@ -1146,8 +1171,11 @@ static int process_prog(const char *filename, struct bpf_object *obj, struct bpf
        char *buf;
        int buf_sz, log_level;
        struct verif_stats *stats;
+       struct bpf_prog_info info;
+       __u32 info_len = sizeof(info);
        int err = 0;
        void *tmp;
+       int fd;
 
        if (!should_process_file_prog(base_filename, bpf_program__name(prog))) {
                env.progs_skipped++;

tools/testing/selftests/bpf/veristat.c static int process_prog(const char *filename, struct bpf_object *obj, struct bpf · line 1229

Quoted 6 lines
tools/testing/selftests/bpf/veristat.c +3 -0
@@ -1196,6 +1224,14 @@ static int process_prog(const char *filename, struct bpf_object *obj, struct bpf
        stats->file_name = strdup(base_filename);
        stats->prog_name = strdup(bpf_program__name(prog));
        stats->stats[VERDICT] = err == 0; /* 1 - success, 0 - failure */
+       stats->stats[SIZE] = bpf_program__insn_cnt(prog);
+       stats->stats[PROG_TYPE] = bpf_program__type(prog);
+       stats->stats[ATTACH_TYPE] = bpf_program__expected_attach_type(prog);

styling nit: I'd add an empty line here to separate multi-line jited_size logic a bit

tools/testing/selftests/bpf/veristat.c static int process_prog(const char *filename, struct bpf_object *obj, struct bpf · line 1231

Quoted 2 lines
tools/testing/selftests/bpf/veristat.c +2 -0
+       fd = bpf_program__fd(prog);
+       memset(&info, 0, info_len);

styling nit: and I'd invert here to keep fd > 0 check right next to fd = assignment

Quoted 23 lines
tools/testing/selftests/bpf/veristat.c +9 -0
+       if (fd > 0 && bpf_prog_get_info_by_fd(fd, &info, &info_len) == 0)
+               stats->stats[JITED_SIZE] = info.jited_prog_len;
+
        parse_verif_log(buf, buf_sz, stats);
 
        if (env.verbose) {
@@ -1309,6 +1345,11 @@ static int cmp_stat(const struct verif_stats *s1, const struct verif_stats *s2,
        case PROG_NAME:
                cmp = strcmp(s1->prog_name, s2->prog_name);
                break;
+       case ATTACH_TYPE:
+       case PROG_TYPE:
+       case SIZE:
+       case JITED_SIZE:
+       case STACK:
        case VERDICT:
        case DURATION:
        case TOTAL_INSNS:
@@ -1523,12 +1564,27 @@ static void prepare_value(const struct verif_stats *s, enum stat_id id,
                else
                        *str = s->stats[VERDICT] ? "success" : "failure";
                break;
+       case ATTACH_TYPE:

tools/testing/selftests/bpf/veristat.c static void prepare_value(const struct verif_stats *s, enum stat_id id, · line 1577

Quoted 10 lines
tools/testing/selftests/bpf/veristat.c +10 -0
+               if (!s)
+                       *str = "N/A";
+               else
+                       *str = libbpf_bpf_attach_type_str(s->stats[ATTACH_TYPE]) ? : "N/A";
+               break;
+       case PROG_TYPE:
+               if (!s)
+                       *str = "N/A";
+               else
+                       *str = libbpf_bpf_prog_type_str(s->stats[PROG_TYPE]) ? : "N/A";

another nitpick: we normally have "?:" together without a space between those characters

Quoted 16 lines
tools/testing/selftests/bpf/veristat.c +4 -1
+               break;
        case DURATION:
        case TOTAL_INSNS:
        case TOTAL_STATES:
        case PEAK_STATES:
        case MAX_STATES_PER_INSN:
        case MARK_READ_MAX_LEN:
+       case STACK:
+       case SIZE:
+       case JITED_SIZE:
                *val = s ? s->stats[id] : 0;
                break;
        default:
--

2.47.1