tikv / pprof-rs

A Rust CPU profiler implemented with the help of backtrace-rs
Apache License 2.0
1.3k stars 103 forks source link

Question: I want to pprof every 30s. should I put `ProfilerGuard` in loop? #238

Closed yuyang-ok closed 9 months ago

yuyang-ok commented 9 months ago

If I want pprof every 30s , just pprof current 30s cpu profiling. My code kind of like this.

let guard = pprof::ProfilerGuardBuilder::default()
            .frequency(1000)
            .blocklist(&["libc", "libgcc", "pthread", "vdso"])
            .build()
            .unwrap();
        loop {
            std::thread::sleep(Duration::from_secs(second));
            let r = guard.report().build().unwrap();
            let file = std::fs::File::create("/tmp/fabric_flamegraph.svg").unwrap();
            let mut options = pprof::flamegraph::Options::default();
            options.image_width = Some(2500);
            r.flamegraph_with_options(file, &mut options).unwrap();
            println!("generated flamegraph");
        }

should I put let guard in loop ?

YangKeao commented 9 months ago

Yes.

Profiling stopped when the guard is dropped, so putting it in the loop will stop the existing profiler before each iteration ends.

yuyang-ok commented 9 months ago

@YangKeao Thanks.