On Fri, Jun 13, 2025 at 12:21 AM Eduard Zingerman <eddyz87@xxxxxxxxx> wrote: > > This commit adds a new field mem_peak / "Peak memory (MiB)" field to a > set of gathered statistics. The field is intended as an estimate for > peak verifier memory consumption for processing of a given program. > > Mechanically stat is collected as follows: > - At the beginning of handle_verif_mode() a new cgroup is created > and veristat process is moved into this cgroup. > - At each program load: > - bpf_object__load() is split into bpf_object__prepare() and > bpf_object__load() to avoid accounting for memory allocated for > maps; > - before bpf_object__load(): > - a write to "memory.peak" file of the new cgroup is used to reset > cgroup statistics; > - updated value is read from "memory.peak" file and stashed; > - after bpf_object__load() "memory.peak" is read again and > difference between new and stashed values is used as a metric. > > If any of the above steps fails veristat proceeds w/o collecting > mem_peak information for a program, reporting mem_peak as -1. > > While memcg provides data in bytes (converted from pages), veristat > converts it to megabytes to avoid jitter when comparing results of > different executions. > > The change has no measurable impact on veristat running time. > > A correlation between "Peak states" and "Peak memory" fields provides > a sanity check for gathered statistics, e.g. a sample of data for > sched_ext programs: > > Program Peak states Peak memory (MiB) > ------------------------ ----------- ----------------- > lavd_select_cpu 2153 44 > lavd_enqueue 1982 41 > lavd_dispatch 3480 28 > layered_dispatch 1417 17 > layered_enqueue 760 11 > lavd_cpu_offline 349 6 > lavd_cpu_online 349 6 > lavd_init 394 6 > rusty_init 350 5 > layered_select_cpu 391 4 > ... > rusty_stopping 134 1 > arena_topology_node_init 170 0 > > Signed-off-by: Eduard Zingerman <eddyz87@xxxxxxxxx> > --- > tools/testing/selftests/bpf/Makefile | 8 + > tools/testing/selftests/bpf/veristat.c | 248 ++++++++++++++++++++++++- > 2 files changed, 249 insertions(+), 7 deletions(-) > > diff --git a/tools/testing/selftests/bpf/Makefile b/tools/testing/selftests/bpf/Makefile > index cf5ed3bee573..dd598ca771c5 100644 > --- a/tools/testing/selftests/bpf/Makefile > +++ b/tools/testing/selftests/bpf/Makefile > @@ -841,6 +841,14 @@ $(OUTPUT)/bench: $(OUTPUT)/bench.o \ > $(call msg,BINARY,,$@) > $(Q)$(CC) $(CFLAGS) $(LDFLAGS) $(filter %.a %.o,$^) $(LDLIBS) -o $@ > > +# This works around GCC warning about snprintf truncating strings like: > +# > +# char a[PATH_MAX], b[PATH_MAX]; > +# snprintf(a, "%s/foo", b); // triggers -Wformat-truncation > +ifeq ($(LLVM),) > +$(OUTPUT)/veristat.o: CFLAGS+=-Wno-format-truncation fixed formatting around +=, and dropped LLVM check, I think that should be handled by -Wno-unused-command-line-argument we set earlier (`make LLVM=1 veristat` succeeded without any warnings for me, FWIW) applied to bpf-next, thanks > +endif > + > $(OUTPUT)/veristat.o: $(BPFOBJ) > $(OUTPUT)/veristat: $(OUTPUT)/veristat.o > $(call msg,BINARY,,$@) [...] > +/* > + * Creates a cgroup at /sys/fs/cgroup/veristat-accounting-<pid>, > + * moves current process to this cgroup. > + */ > +static void create_stat_cgroup(void) > +{ > + char cgroup_fs_mount[PATH_MAX]; > + char buf[PATH_MAX]; I hard-coded these to 4096 given we hard-coded 4095 below. [...]