Skip to content

Commit

Permalink
Merge pull request #6 from Netflix/bpf-stats-fd
Browse files Browse the repository at this point in the history
Refactor fd handling to enable BPF stats
  • Loading branch information
jfernandez authored Feb 23, 2024
2 parents 0e4c4c0 + ba0095b commit d29e584
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@
* limitations under the License.
*
*/
use std::fs::File;
use std::io;
use std::os::fd::FromRawFd;
use std::os::fd::{FromRawFd, OwnedFd};
use std::time::Duration;

use anyhow::{anyhow, Result};
Expand Down Expand Up @@ -67,13 +66,14 @@ fn main() -> Result<()> {
return Err(anyhow!("This program must be run as root"));
}

// enable BPF stats while the program is running
// Try to enable BPF stats
let fd = unsafe { bpf_enable_stats(libbpf_sys::BPF_STATS_RUN_TIME) };
if fd < 0 {
return Err(anyhow!("Failed to enable BPF stats"));
}
// The fd will be closed when _file goes out of scope at the end of main.
let _file = unsafe { File::from_raw_fd(fd) };
// The file descriptor will be closed when `_owned_fd` goes out of scope.
// This guarantees that BPF stats will be disabled when the program exits.
let _owned_fd = unsafe { OwnedFd::from_raw_fd(fd) };

// setup terminal
enable_raw_mode()?;
Expand All @@ -88,7 +88,7 @@ fn main() -> Result<()> {
app.start_background_thread();
let res = run_draw_loop(&mut terminal, app);

// // restore terminal
// restore terminal
disable_raw_mode()?;
execute!(terminal.backend_mut(), LeaveAlternateScreen,)?;
terminal.show_cursor()?;
Expand Down

0 comments on commit d29e584

Please sign in to comment.