Skip to content

Commit 085588e

Browse files
committed
selftests/bpf: track amount of memory used by liveness analysis
Temporarily extended veristat collected data with: - amount of memory consumed for liveness bit masks; - number of memory allocations done for liveness.c:func_instance internals (O(number of unique callchains)). Will not be included in the final submission. Signed-off-by: Eduard Zingerman <eddyz87@gmail.com>
1 parent eb052a4 commit 085588e

File tree

4 files changed

+30
-8
lines changed

4 files changed

+30
-8
lines changed

include/linux/bpf_verifier.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -828,6 +828,8 @@ struct bpf_verifier_env {
828828
/* array of pointers to bpf_scc_info indexed by SCC id */
829829
struct bpf_scc_info **scc_info;
830830
u32 scc_cnt;
831+
u32 num_liveness_allocs;
832+
u32 total_liveness_mem;
831833
};
832834

833835
static inline struct bpf_func_info_aux *subprog_aux(struct bpf_verifier_env *env, int subprog)

kernel/bpf/liveness.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,9 @@ static struct func_instance *__lookup_instance(struct bpf_verifier_env *env,
199199
memcpy(&result->callchain, callchain, sizeof(*callchain));
200200
result->insn_cnt = subprog_sz;
201201
hash_add(liveness->func_instances, &result->hl_node, key);
202+
env->num_liveness_allocs++;
203+
env->total_liveness_mem += size;
204+
env->total_liveness_mem += sizeof(*result->must_write_set) * subprog_sz;
202205
return result;
203206
}
204207

@@ -267,6 +270,8 @@ static struct per_frame_masks *alloc_frame_masks(struct bpf_verifier_env *env,
267270
instance->frames[frame] = arr;
268271
if (!arr)
269272
return ERR_PTR(-ENOMEM);
273+
env->num_liveness_allocs++;
274+
env->total_liveness_mem += instance->insn_cnt * sizeof(*arr);
270275
}
271276
return get_frame_masks(instance, frame, insn_idx);
272277
}

kernel/bpf/verifier.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23179,10 +23179,11 @@ static void print_verification_stats(struct bpf_verifier_env *env)
2317923179
verbose(env, "\n");
2318023180
}
2318123181
verbose(env, "processed %d insns (limit %d) max_states_per_insn %d "
23182-
"total_states %d peak_states %d mark_read %d\n",
23182+
"total_states %d peak_states %d mark_read %d num_liveness_allocs %d total_liveness_mem %d\n",
2318323183
env->insn_processed, BPF_COMPLEXITY_LIMIT_INSNS,
2318423184
env->max_states_per_insn, env->total_states,
23185-
env->peak_states, env->longest_mark_read_walk);
23185+
env->peak_states, env->longest_mark_read_walk,
23186+
env->num_liveness_allocs, env->total_liveness_mem);
2318623187
}
2318723188

2318823189
int bpf_prog_ctx_arg_info_init(struct bpf_prog *prog,

tools/testing/selftests/bpf/veristat.c

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ enum stat_id {
5151
PROG_TYPE,
5252
ATTACH_TYPE,
5353
MEMORY_PEAK,
54+
LIVENESS_ALLOCS,
55+
LIVENESS_MEM,
5456

5557
FILE_NAME,
5658
PROG_NAME,
@@ -738,10 +740,11 @@ static int append_filter_file(const char *path)
738740
}
739741

740742
static const struct stat_specs default_output_spec = {
741-
.spec_cnt = 8,
743+
.spec_cnt = 10,
742744
.ids = {
743745
FILE_NAME, PROG_NAME, VERDICT, DURATION,
744-
TOTAL_INSNS, TOTAL_STATES, SIZE, JITED_SIZE
746+
TOTAL_INSNS, TOTAL_STATES, SIZE, JITED_SIZE,
747+
LIVENESS_ALLOCS, LIVENESS_MEM,
745748
},
746749
};
747750

@@ -789,13 +792,13 @@ static int append_file_from_file(const char *path)
789792
}
790793

791794
static const struct stat_specs default_csv_output_spec = {
792-
.spec_cnt = 15,
795+
.spec_cnt = 17,
793796
.ids = {
794797
FILE_NAME, PROG_NAME, VERDICT, DURATION,
795798
TOTAL_INSNS, TOTAL_STATES, PEAK_STATES,
796799
MAX_STATES_PER_INSN, MARK_READ_MAX_LEN,
797800
SIZE, JITED_SIZE, PROG_TYPE, ATTACH_TYPE,
798-
STACK, MEMORY_PEAK,
801+
STACK, MEMORY_PEAK, LIVENESS_ALLOCS, LIVENESS_MEM,
799802
},
800803
};
801804

@@ -837,6 +840,8 @@ static struct stat_def {
837840
[PROG_TYPE] = { "Program type", {"prog_type"}, },
838841
[ATTACH_TYPE] = { "Attach type", {"attach_type", }, },
839842
[MEMORY_PEAK] = { "Peak memory (MiB)", {"mem_peak", }, },
843+
[LIVENESS_ALLOCS] = { "liveness allocs", {"liveness_allocs", }, },
844+
[LIVENESS_MEM] = { "liveness mem", {"liveness_mem", }, },
840845
};
841846

842847
static bool parse_stat_id_var(const char *name, size_t len, int *id,
@@ -1015,12 +1020,15 @@ static int parse_verif_log(char * const buf, size_t buf_sz, struct verif_stats *
10151020

10161021
if (1 == sscanf(cur, "verification time %ld usec\n", &s->stats[DURATION]))
10171022
continue;
1018-
if (5 == sscanf(cur, "processed %ld insns (limit %*d) max_states_per_insn %ld total_states %ld peak_states %ld mark_read %ld",
1023+
if (5 == sscanf(cur, "processed %ld insns (limit %*d) max_states_per_insn %ld total_states %ld peak_states %ld mark_read %ld num_liveness_allocs %ld total_liveness_mem %ld",
10191024
&s->stats[TOTAL_INSNS],
10201025
&s->stats[MAX_STATES_PER_INSN],
10211026
&s->stats[TOTAL_STATES],
10221027
&s->stats[PEAK_STATES],
1023-
&s->stats[MARK_READ_MAX_LEN]))
1028+
&s->stats[MARK_READ_MAX_LEN],
1029+
&s->stats[LIVENESS_ALLOCS],
1030+
&s->stats[LIVENESS_MEM]
1031+
))
10241032
continue;
10251033

10261034
if (1 == sscanf(cur, "stack depth %511s", stack))
@@ -2299,6 +2307,8 @@ static int cmp_stat(const struct verif_stats *s1, const struct verif_stats *s2,
22992307
case PEAK_STATES:
23002308
case MAX_STATES_PER_INSN:
23012309
case MEMORY_PEAK:
2310+
case LIVENESS_ALLOCS:
2311+
case LIVENESS_MEM:
23022312
case MARK_READ_MAX_LEN: {
23032313
long v1 = s1->stats[id];
23042314
long v2 = s2->stats[id];
@@ -2528,6 +2538,8 @@ static void prepare_value(const struct verif_stats *s, enum stat_id id,
25282538
case STACK:
25292539
case SIZE:
25302540
case JITED_SIZE:
2541+
case LIVENESS_ALLOCS:
2542+
case LIVENESS_MEM:
25312543
case MEMORY_PEAK:
25322544
*val = s ? s->stats[id] : 0;
25332545
break;
@@ -2615,6 +2627,8 @@ static int parse_stat_value(const char *str, enum stat_id id, struct verif_stats
26152627
case MARK_READ_MAX_LEN:
26162628
case SIZE:
26172629
case JITED_SIZE:
2630+
case LIVENESS_ALLOCS:
2631+
case LIVENESS_MEM:
26182632
case MEMORY_PEAK:
26192633
case STACK: {
26202634
long val;

0 commit comments

Comments
 (0)