@@ -80,21 +80,24 @@ fn main() -> Result<()> {
8080
8181 let kernel_version = KernelVersion :: current ( ) ?;
8282 let _owned_fd: OwnedFd ;
83- let stats_syscall_supported = kernel_version . ge ( & KernelVersion :: new ( 5 , 8 , 0 ) ) ;
83+ let mut stats_enabled_via_procfs = false ;
8484
85- // enable BPF stats via syscall if supported
86- // otherwise, enable via procfs
87- if stats_syscall_supported {
85+ // enable BPF stats via syscall if kernel version >= 5.8
86+ if kernel_version >= KernelVersion :: new ( 5 , 8 , 0 ) {
8887 let fd = unsafe { bpf_enable_stats ( libbpf_sys:: BPF_STATS_RUN_TIME ) } ;
8988 if fd < 0 {
9089 return Err ( anyhow ! ( "Failed to enable BPF stats via syscall" ) ) ;
9190 }
9291 _owned_fd = unsafe { OwnedFd :: from_raw_fd ( fd) } ;
93- } else {
94- fs:: write ( PROCFS_BPF_STATS_ENABLED , b"1" ) . context ( format ! (
95- "Failed to enable BPF stats via {}" ,
96- PROCFS_BPF_STATS_ENABLED
97- ) ) ?;
92+ } else { // otherwise, enable via procfs
93+ // but first check if procfs bpf stats were already enabled
94+ if !procfs_bpf_stats_is_enabled ( ) ? {
95+ fs:: write ( PROCFS_BPF_STATS_ENABLED , b"1" ) . context ( format ! (
96+ "Failed to enable BPF stats via {}" ,
97+ PROCFS_BPF_STATS_ENABLED
98+ ) ) ?;
99+ stats_enabled_via_procfs = true ;
100+ }
98101 }
99102
100103 // setup terminal
@@ -108,8 +111,8 @@ fn main() -> Result<()> {
108111 // capture panic to disable BPF stats via procfs and restore terminal
109112 let previous_hook = panic:: take_hook ( ) ;
110113 panic:: set_hook ( Box :: new ( move |panic_info| {
111- if !stats_syscall_supported {
112- if let Err ( err) = disable_bpf_stats_procfs ( ) {
114+ if stats_enabled_via_procfs {
115+ if let Err ( err) = procs_bfs_stats_disable ( ) {
113116 eprintln ! ( "Failed to disable BPF stats via procfs: {:?}" , err) ;
114117 }
115118 }
@@ -133,8 +136,8 @@ fn main() -> Result<()> {
133136 terminal. clear ( ) ?;
134137
135138 // disable BPF stats via procfs if needed
136- if !stats_syscall_supported {
137- disable_bpf_stats_procfs ( ) ?;
139+ if stats_enabled_via_procfs {
140+ procs_bfs_stats_disable ( ) ?;
138141 }
139142
140143 #[ allow( clippy:: question_mark) ]
@@ -145,14 +148,20 @@ fn main() -> Result<()> {
145148 Ok ( ( ) )
146149}
147150
148- fn disable_bpf_stats_procfs ( ) -> Result < ( ) > {
151+ fn procs_bfs_stats_disable ( ) -> Result < ( ) > {
149152 fs:: write ( PROCFS_BPF_STATS_ENABLED , b"0" ) . context ( format ! (
150153 "Failed to disable BPF stats via {}" ,
151154 PROCFS_BPF_STATS_ENABLED
152155 ) ) ?;
153156 Ok ( ( ) )
154157}
155158
159+ fn procfs_bpf_stats_is_enabled ( ) -> Result < bool > {
160+ fs:: read_to_string ( PROCFS_BPF_STATS_ENABLED )
161+ . context ( format ! ( "Failed to read from {}" , PROCFS_BPF_STATS_ENABLED ) )
162+ . map ( |value| value. trim ( ) == "1" )
163+ }
164+
156165fn run_draw_loop < B : Backend > ( terminal : & mut Terminal < B > , mut app : App ) -> Result < ( ) > {
157166 loop {
158167 terminal. draw ( |f| ui ( f, & mut app) ) ?;
0 commit comments