Skip to content
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

Improve the profiling output of Cargo #1395

Merged
merged 1 commit into from
Mar 9, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/cargo/core/resolver/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ pub fn resolve(summary: &Summary, method: Method,
activations: HashMap::new(),
visited: Rc::new(RefCell::new(HashSet::new())),
});
let _p = profile::start(format!("resolving: {:?}", summary));
let _p = profile::start(format!("resolving: {}", summary.package_id()));
match try!(activate(cx, registry, &summary, method)) {
Ok(cx) => {
debug!("resolved: {:?}", cx.resolve);
Expand Down
4 changes: 2 additions & 2 deletions src/cargo/ops/cargo_rustc/fingerprint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ pub fn prepare_target<'a, 'b>(cx: &mut Context<'a, 'b>,
pkg: &'a Package,
target: &'a Target,
kind: Kind) -> CargoResult<Preparation> {
let _p = profile::start(format!("fingerprint: {} / {:?}",
pkg.package_id(), target));
let _p = profile::start(format!("fingerprint: {} / {}",
pkg.package_id(), target.name()));
let new = dir(cx, pkg, kind);
let loc = new.join(&filename(target));

Expand Down
18 changes: 12 additions & 6 deletions src/cargo/util/profile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ pub struct Profiler {
desc: String,
}

fn enabled() -> bool { env::var_os("CARGO_PROFILE").is_some() }
fn enabled_level() -> Option<usize> {
env::var("CARGO_PROFILE").ok().and_then(|s| s.parse().ok())
}

pub fn start<T: fmt::Display>(desc: T) -> Profiler {
if !enabled() { return Profiler { desc: String::new() } }
if enabled_level().is_none() { return Profiler { desc: String::new() } }

PROFILE_STACK.with(|stack| stack.borrow_mut().push(time::precise_time_ns()));

Expand All @@ -28,22 +30,26 @@ pub fn start<T: fmt::Display>(desc: T) -> Profiler {

impl Drop for Profiler {
fn drop(&mut self) {
if !enabled() { return }
let enabled = match enabled_level() {
Some(i) => i,
None => return,
};

let start = PROFILE_STACK.with(|stack| stack.borrow_mut().pop().unwrap());
let end = time::precise_time_ns();

let stack_len = PROFILE_STACK.with(|stack| stack.borrow().len());
if stack_len == 0 {
fn print(lvl: usize, msgs: &[Message]) {
fn print(lvl: usize, msgs: &[Message], enabled: usize) {
if lvl > enabled { return }
let mut last = 0;
for (i, &(l, time, ref msg)) in msgs.iter().enumerate() {
if l != lvl { continue }
println!("{} {:6}ms - {}",
repeat(" ").take(lvl + 1).collect::<String>(),
time / 1000000, msg);

print(lvl + 1, &msgs[last..i]);
print(lvl + 1, &msgs[last..i], enabled);
last = i;
}

Expand All @@ -52,7 +58,7 @@ impl Drop for Profiler {
let mut msgs = msgs_rc.borrow_mut();
msgs.push((0, end - start,
mem::replace(&mut self.desc, String::new())));
print(0, &msgs);
print(0, &msgs, enabled);
});
} else {
MESSAGES.with(|msgs| {
Expand Down