Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add panic hook to disable BPF stats and restore terminal on panic #33

Merged
merged 1 commit into from
Apr 18, 2024

Conversation

jfernandez
Copy link
Contributor

Previously if there was a panic we would not disable BPF stats when using the procfs toggle (kernels older than 5.8). This adds a custom panic hook that disables BPF stats if needed, and then attempts to restore the terminal.

fs::write(PROCFS_BPF_STATS_ENABLED, b"0").context(format!(
"Failed to disable BPF stats via {}",
PROCFS_BPF_STATS_ENABLED
))?;
Copy link
Member

@haze haze Apr 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Explicitly throw away the value here by mapping the result

@@ -131,6 +145,14 @@ fn main() -> Result<()> {
Ok(())
}

fn disable_bpf_stats_procfs() -> Result<()> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably impossible to manage this comprehensively, but, ... does this cripple state for other consumers of stats?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Before 5.8 there was only this global toggle. In 5.8+ there is a ref-count model via the bpf syscall, so we'll only increment the count and decrement when starting/exiting bpftop. The kernel will only disable stats if the refcount it at zero, so other consumers of stats won't be impacted if they hold a ref.

What we can do is for 5.7 and below, verify first that the global procfs toggle is enabled. If it was already enabled when the bpftop runs, then we don't disable it on exit/panic. I can follow up with a second PR for this behavior.

@jfernandez jfernandez merged commit 259ae38 into main Apr 18, 2024
1 check passed
@jfernandez jfernandez deleted the panic-hook branch April 18, 2024 20:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants