tracing_profile/
utils.rs

1// Copyright 2025 Irreducible Inc.
2
3/// Creates a [`tracing`] event with the resident set size at its peak in megabytes.
4///
5/// The name of the event is `max rss mib`.
6pub fn emit_max_rss() {
7    if let Some(max_rss) = get_max_rss() {
8        let max_rss_mb = if cfg!(target_os = "linux") {
9            // The maxrss is in kbytes for Linux.
10            max_rss / 1024
11        } else if cfg!(target_os = "macos") {
12            // ... and in bytes for BSD/macOS.
13            max_rss / 1024 / 1024
14        } else {
15            // don't risk confusing.
16            0
17        };
18        tracing::event!(
19            name: "max rss mib",
20            tracing::Level::INFO,
21            value = max_rss_mb,
22            counter = true
23        );
24    }
25    fn get_max_rss() -> Option<i64> {
26        use nix::sys::resource;
27
28        resource::getrusage(resource::UsageWho::RUSAGE_SELF)
29            .ok()
30            .map(|usage| usage.max_rss())
31    }
32}