-
Notifications
You must be signed in to change notification settings - Fork 6
feat: track child processes #168
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
Open
not-matthias
wants to merge
6
commits into
main
Choose a base branch
from
cod-1744-child-processes-are-not-visible-in-the-flamegraphs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
350835e
refactor: add shared codspeed-bpf crate
not-matthias 2ced094
feat: add exectrack artifact types
not-matthias f43a667
feat: add exectrack cli
not-matthias 2221798
chore: add exectrack tests
not-matthias 0ecb974
feat: wrap walltime cmd with exectrack
not-matthias 73669d7
fixup: exclude exectrack from regular tests
not-matthias File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| [package] | ||
| name = "codspeed-bpf" | ||
| version = "0.1.0" | ||
| edition = "2024" | ||
| repository = "https://github.com/CodSpeedHQ/runner" | ||
|
|
||
| [features] | ||
| # Enable build utilities for BPF compilation | ||
| build = ["dep:libbpf-cargo", "dep:vmlinux", "dep:bindgen"] | ||
|
|
||
| [dependencies] | ||
| anyhow = { workspace = true } | ||
| libbpf-rs = { version = "0.25.0", features = ["vendored"] } | ||
| libc = { workspace = true } | ||
| log = { workspace = true } | ||
| paste = "1.0" | ||
|
|
||
| # Build dependencies (optional, only needed for build feature) | ||
| libbpf-cargo = { version = "0.25.0", optional = true } | ||
| vmlinux = { git = "https://github.com/libbpf/vmlinux.h.git", rev = "991dd4b8dfd8c9d62ce8999521b24f61d9b7fc52", optional = true } | ||
| bindgen = { version = "0.71", optional = true } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| //! Build utilities for BPF programs using codspeed-bpf | ||
| //! | ||
| //! This module provides helpers for building BPF programs that use codspeed-bpf headers. | ||
| //! | ||
| //! To use this in your build.rs, add codspeed-bpf with the "build" feature to your | ||
| //! [build-dependencies]: | ||
| //! | ||
| //! ```toml | ||
| //! [build-dependencies] | ||
| //! codspeed-bpf = { path = "../codspeed-bpf", features = ["build"] } | ||
| //! ``` | ||
| //! | ||
| //! Then in your build.rs: | ||
| //! | ||
| //! ```ignore | ||
| //! fn main() { | ||
| //! codspeed_bpf::build::build_bpf("exectrack", "src/ebpf/c/exectrack.bpf.c"); | ||
| //! codspeed_bpf::build::generate_bindings("wrapper.h"); | ||
| //! } | ||
| //! ``` | ||
|
|
||
| use std::{env, path::PathBuf}; | ||
|
|
||
| /// Build a BPF program with codspeed-bpf headers available | ||
| /// | ||
| /// # Arguments | ||
| /// * `program_name` - The name of the BPF program (e.g., "exectrack", "memtrack") | ||
| /// * `source_file` - The path to the BPF source file (e.g., "src/ebpf/c/exectrack.bpf.c") | ||
| /// | ||
| /// This function will: | ||
| /// 1. Compile the BPF program using libbpf-cargo | ||
| /// 2. Include codspeed-bpf headers in the clang search path | ||
| /// 3. Generate a skeleton into OUT_DIR/{program_name}.skel.rs | ||
| pub fn build_bpf(program_name: &str, source_file: &str) { | ||
| use libbpf_cargo::SkeletonBuilder; | ||
|
|
||
| println!("cargo:rerun-if-changed=src/ebpf/c"); | ||
|
|
||
| let arch = env::var("CARGO_CFG_TARGET_ARCH") | ||
| .expect("CARGO_CFG_TARGET_ARCH must be set in build script"); | ||
|
|
||
| let output = | ||
| PathBuf::from(env::var("OUT_DIR").unwrap()).join(format!("{program_name}.skel.rs")); | ||
|
|
||
| // Get the path to codspeed-bpf's C headers | ||
| let codspeed_bpf_include = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()) | ||
| .parent() | ||
| .unwrap() | ||
| .join("codspeed-bpf/src/c"); | ||
|
|
||
| SkeletonBuilder::new() | ||
| .source(source_file) | ||
| .clang_args([ | ||
| "-I", | ||
| &vmlinux::include_path_root().join(arch).to_string_lossy(), | ||
| "-I", | ||
| &codspeed_bpf_include.to_string_lossy(), | ||
| ]) | ||
| .build_and_generate(&output) | ||
| .unwrap_or_else(|_| panic!("Failed to build {program_name}.bpf.c")); | ||
| } | ||
|
|
||
| /// Generate Rust bindings for a C header file | ||
| /// | ||
| /// # Arguments | ||
| /// * `header_file` - The path to the header file (e.g., "wrapper.h") | ||
| /// | ||
| /// This function will: | ||
| /// 1. Use bindgen to generate Rust bindings | ||
| /// 2. Write the output to OUT_DIR/event.rs | ||
| pub fn generate_bindings(header_file: &str) { | ||
| let bindings = bindgen::Builder::default() | ||
| .header(header_file) | ||
| .parse_callbacks(Box::new(bindgen::CargoCallbacks::new())) | ||
| .generate() | ||
| .expect("Unable to generate bindings"); | ||
|
|
||
| let out_file = PathBuf::from(env::var("OUT_DIR").unwrap()).join("event.rs"); | ||
| std::fs::write(&out_file, bindings.to_string()).expect("Couldn't write bindings!"); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| #ifndef __CODSPEED_COMMON_H__ | ||
| #define __CODSPEED_COMMON_H__ | ||
|
|
||
| /* Macros for common map definitions */ | ||
| #define BPF_HASH_MAP(name, key_type, value_type, max_ents) \ | ||
| struct { \ | ||
| __uint(type, BPF_MAP_TYPE_HASH); \ | ||
| __uint(max_entries, max_ents); \ | ||
| __type(key, key_type); \ | ||
| __type(value, value_type); \ | ||
| } name SEC(".maps") | ||
|
|
||
| #define BPF_ARRAY_MAP(name, value_type, max_ents) \ | ||
| struct { \ | ||
| __uint(type, BPF_MAP_TYPE_ARRAY); \ | ||
| __uint(max_entries, max_ents); \ | ||
| __type(key, __u32); \ | ||
| __type(value, value_type); \ | ||
| } name SEC(".maps") | ||
|
|
||
| #define BPF_RINGBUF(name, size) \ | ||
| struct { \ | ||
| __uint(type, BPF_MAP_TYPE_RINGBUF); \ | ||
| __uint(max_entries, size); \ | ||
| } name SEC(".maps") | ||
|
|
||
| #endif /* __CODSPEED_COMMON_H__ */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| #ifndef __CODSPEED_PROCESS_TRACKING_H__ | ||
| #define __CODSPEED_PROCESS_TRACKING_H__ | ||
|
|
||
| #include "common.h" | ||
|
|
||
| /* Standard process tracking maps - define these in your BPF program | ||
| * This macro creates the map definitions. The helper functions below will | ||
| * reference these maps, so call this macro before including this header's helpers. | ||
| */ | ||
| #define PROCESS_TRACKING_MAPS() \ | ||
| BPF_HASH_MAP(tracked_pids, __u32, __u8, 10000); \ | ||
| BPF_HASH_MAP(pids_ppid, __u32, __u32, 10000); \ | ||
| /* Helper functions defined below */ \ | ||
| static __always_inline int is_tracked(__u32 pid); \ | ||
| static __always_inline int handle_fork(__u32 parent_pid, __u32 child_pid); \ | ||
| static __always_inline int handle_exit(__u32 pid); \ | ||
| /* is_tracked implementation */ \ | ||
| static __always_inline int is_tracked(__u32 pid) { \ | ||
| if (bpf_map_lookup_elem(&tracked_pids, &pid)) { \ | ||
| return 1; \ | ||
| } \ | ||
| _Pragma("unroll") \ | ||
| for (int i = 0; i < 5; i++) { \ | ||
| __u32* ppid = bpf_map_lookup_elem(&pids_ppid, &pid); \ | ||
| if (!ppid) { \ | ||
| break; \ | ||
| } \ | ||
| pid = *ppid; \ | ||
| if (bpf_map_lookup_elem(&tracked_pids, &pid)) { \ | ||
| return 1; \ | ||
| } \ | ||
| } \ | ||
| return 0; \ | ||
| } \ | ||
| /* handle_fork implementation */ \ | ||
| static __always_inline int handle_fork(__u32 parent_pid, __u32 child_pid) { \ | ||
| if (is_tracked(parent_pid)) { \ | ||
| __u8 marker = 1; \ | ||
| bpf_map_update_elem(&tracked_pids, &child_pid, &marker, BPF_ANY); \ | ||
| bpf_map_update_elem(&pids_ppid, &child_pid, &parent_pid, BPF_ANY); \ | ||
| return 1; \ | ||
| } \ | ||
| return 0; \ | ||
| } \ | ||
| /* handle_exit implementation */ \ | ||
| static __always_inline int handle_exit(__u32 pid) { \ | ||
| bpf_map_delete_elem(&tracked_pids, &pid); \ | ||
| bpf_map_delete_elem(&pids_ppid, &pid); \ | ||
| return 0; \ | ||
| } | ||
|
|
||
| #endif /* __CODSPEED_PROCESS_TRACKING_H__ */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| //! Shared BPF utilities for CodSpeed tracing tools | ||
| //! | ||
| //! This crate provides common functionality for BPF-based tracing: | ||
| //! - Process hierarchy tracking | ||
| //! - Ring buffer polling | ||
| //! - Probe attachment macros | ||
| //! - Build utilities for BPF programs | ||
|
|
||
| pub mod macros; | ||
| pub mod poller; | ||
| pub mod process_tracking; | ||
|
|
||
| #[cfg(feature = "build")] | ||
| pub mod build; | ||
|
|
||
| pub use poller::{EventHandler, RingBufferPoller}; | ||
| pub use process_tracking::{ProcessTracking, bump_memlock_rlimit}; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The CI workflow excludes both 'memtrack' and 'exectrack' from tests, but the syntax is incorrect. The command should be
--exclude memtrack --exclude exectrack(with two separate --exclude flags) instead of--exclude memtrack exectrack.