History

bpf: Sharing bpf runtime stats with BPF_ENABLE_STATS

Currently, sysctl kernel.bpf_stats_enabled controls BPF runtime stats. Typical userspace tools use kernel.bpf_stats_enabled as follows: 1. Enable kernel.bpf_stats_enabled; 2. Check program run_time_ns; 3. Sleep for the monitoring period; 4. Check program run_time_ns again, calculate the difference; 5. Disable kernel.bpf_stats_enabled. The problem with this approach is that only one userspace tool can toggle this sysctl. If multiple tools toggle the sysctl at the same time, the measurement may be inaccurate. To fix this problem while keep backward compatibility, introduce a new bpf command BPF_ENABLE_STATS. On success, this command enables stats and returns a valid fd. BPF_ENABLE_STATS takes argument "type". Currently, only one type, BPF_STATS_RUN_TIME, is supported. We can extend the command to support other types of stats in the future. With BPF_ENABLE_STATS, user space tool would have the following flow: 1. Get a fd with BPF_ENABLE_STATS, and make sure it is valid; 2. Check program run_time_ns; 3. Sleep for the monitoring period; 4. Check program run_time_ns again, calculate the difference; 5. Close the fd.

Song Liu committed 6 years ago

Commit d46edd61 parent c3210225 files · +115 −1Browse files

Showing the changes to include/uapi/linux (1 of 5 files). Show all

Unified · Split

include/uapi/linux/bpf.h+11−0
@@ -115,6 +115,7 @@ enum bpf_cmd { · 114 unmodified lines
115 BPF_LINK_UPDATE,115 BPF_LINK_UPDATE,
116 BPF_LINK_GET_FD_BY_ID,116 BPF_LINK_GET_FD_BY_ID,
117 BPF_LINK_GET_NEXT_ID,117 BPF_LINK_GET_NEXT_ID,
118 BPF_ENABLE_STATS,
118};119};
119 120
120enum bpf_map_type {121enum bpf_map_type {
@@ -390,6 +391,12 @@ enum { · 269 unmodified lines
390 */391 */
391#define BPF_F_QUERY_EFFECTIVE (1U << 0)392#define BPF_F_QUERY_EFFECTIVE (1U << 0)
392 393
394/* type for BPF_ENABLE_STATS */
395enum bpf_stats_type {
396 /* enabled run_time_ns and run_cnt */
397 BPF_STATS_RUN_TIME = 0,
398};
399
393enum bpf_stack_build_id_status {400enum bpf_stack_build_id_status {
394 /* user space need an empty entry to identify end of a trace */401 /* user space need an empty entry to identify end of a trace */
395 BPF_STACK_BUILD_ID_EMPTY = 0,402 BPF_STACK_BUILD_ID_EMPTY = 0,
@@ -601,6 +608,10 @@ union bpf_attr { · 205 unmodified lines
601 __u32 old_prog_fd;608 __u32 old_prog_fd;
602 } link_update;609 } link_update;
603 610
611 struct { /* struct used by BPF_ENABLE_STATS command */
612 __u32 type;
613 } enable_stats;
614
604} __attribute__((aligned(8)));615} __attribute__((aligned(8)));
605 616
606/* The description below is an attempt at providing documentation to eBPF617/* The description below is an attempt at providing documentation to eBPF