Skip to content

Commit

Permalink
feat: add pprof flamegraphs behind a feature flag (#6147)
Browse files Browse the repository at this point in the history
Closes TURBO-1445
  • Loading branch information
arlyon authored Oct 18, 2023
1 parent d11f8d7 commit 66446da
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 0 deletions.
69 changes: 69 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions crates/turborepo-lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ lazy-regex = "2.5.0"
node-semver = "2.1.0"
num_cpus = "1.15.0"
owo-colors.workspace = true
pprof = { version = "0.12.1", features = [
"prost-codec",
"frame-pointer",
], optional = true }
rayon = "1.7.0"
regex.workspace = true
svix-ksuid = { version = "0.7.0", features = ["serde"] }
Expand Down
40 changes: 40 additions & 0 deletions crates/turborepo-lib/src/tracing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ pub struct TurboSubscriber {

chrome_update: Handle<Option<ChromeLog>, DaemonLogLayered>,
chrome_guard: Mutex<Option<tracing_chrome::FlushGuard>>,

#[cfg(feature = "pprof")]
pprof_guard: pprof::ProfilerGuard<'static>,
}

impl TurboSubscriber {
Expand Down Expand Up @@ -130,13 +133,22 @@ impl TurboSubscriber {
.with(logrotate)
.with(chrome);

#[cfg(feature = "pprof")]
let pprof_guard = pprof::ProfilerGuardBuilder::default()
.frequency(1000)
.blocklist(&["libc", "libgcc", "pthread", "vdso"])
.build()
.unwrap();

registry.init();

Self {
daemon_update,
daemon_guard: Mutex::new(None),
chrome_update,
chrome_guard: Mutex::new(None),
#[cfg(feature = "pprof")]
pprof_guard,
}
}

Expand Down Expand Up @@ -180,6 +192,34 @@ impl TurboSubscriber {
}
}

impl Drop for TurboSubscriber {
fn drop(&mut self) {
// drop the guard so that the non-blocking file writer stops
#[cfg(feature = "pprof")]
if let Ok(report) = self.pprof_guard.report().build() {
use std::io::Write; // only import trait if we need it

use prost::Message;

let mut file = std::fs::File::create("pprof.pb").unwrap();
let mut content = Vec::new();

let Ok(profile) = report.pprof() else {
tracing::error!("failed to generate pprof report");
return;
};
if let Err(e) = profile.encode(&mut content) {
tracing::error!("failed to encode pprof profile: {}", e);
};
if let Err(e) = file.write_all(&content) {
tracing::error!("failed to write pprof profile: {}", e)
};
} else {
tracing::error!("failed to generate pprof report")
}
}
}

/// The formatter for TURBOREPO
///
/// This is a port of the go formatter, which follows a few main rules:
Expand Down
1 change: 1 addition & 0 deletions crates/turborepo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ http = ["turborepo-lib/http"]
go-daemon = ["turborepo-lib/go-daemon"]
run-stub = ["turborepo-lib/run-stub"]
go-binary = []
pprof = ["turborepo-lib/pprof"]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[build-dependencies]
Expand Down

0 comments on commit 66446da

Please sign in to comment.