Skip to content

Commit fc0f4ed

Browse files
bors[bot]matklad
andcommitted
Merge #1442
1442: add cpuprofile to ra_prof r=matklad a=matklad Co-authored-by: Aleksey Kladov <[email protected]>
2 parents 0129790 + d621533 commit fc0f4ed

File tree

4 files changed

+90
-0
lines changed

4 files changed

+90
-0
lines changed

Cargo.lock

Lines changed: 52 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/ra_batch/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,9 @@ ra_ide_api = { path = "../ra_ide_api" }
1515
ra_hir = { path = "../ra_hir" }
1616
ra_project_model = { path = "../ra_project_model" }
1717

18+
[dependencies.ra_prof]
19+
path = "../ra_prof"
20+
# features = [ "cpuprofiler" ]
21+
1822
[dev-dependencies]
1923
test_utils = { path = "../test_utils" }

crates/ra_prof/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ publish = false
99
once_cell = "0.2.0"
1010
itertools = "0.8.0"
1111
backtrace = "0.3.28"
12+
cpuprofiler = { version = "0.0.3", optional = true }

crates/ra_prof/src/lib.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,39 @@ impl Drop for Scope {
255255
}
256256
}
257257

258+
/// A wrapper around https://github.com/AtheMathmo/cpuprofiler
259+
///
260+
/// It can be used to capture sampling profiles of sections of code.
261+
/// It is not exactly out-of-the-box, as it relies on gperftools.
262+
/// See the docs for the crate for more!
263+
#[derive(Debug)]
264+
pub struct CpuProfiler {
265+
_private: (),
266+
}
267+
268+
pub fn cpu_profiler() -> CpuProfiler {
269+
#[cfg(feature = "cpuprofiler")]
270+
{
271+
cpuprofiler::PROFILER.lock().unwrap().start("./out.profile").unwrap();
272+
}
273+
274+
#[cfg(not(feature = "cpuprofiler"))]
275+
{
276+
eprintln!("cpuprofiler feature is disabled")
277+
}
278+
279+
CpuProfiler { _private: () }
280+
}
281+
282+
impl Drop for CpuProfiler {
283+
fn drop(&mut self) {
284+
#[cfg(feature = "cpuprofiler")]
285+
{
286+
cpuprofiler::PROFILER.lock().unwrap().stop().unwrap();
287+
}
288+
}
289+
}
290+
258291
#[cfg(test)]
259292
mod tests {
260293
use super::*;

0 commit comments

Comments
 (0)