Skip to content

Commit

Permalink
Auto merge of #1395 - alexcrichton:profile, r=brson
Browse files Browse the repository at this point in the history
* Don't use `{:?}` in profiling output, it's too noisy
* Allow an integer to be specified with `CARGO_PROFILE` indicating how many
  levels deep should be printed.
  • Loading branch information
bors committed Mar 9, 2015
2 parents 202131f + ffbb8cc commit e4f0662
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
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

0 comments on commit e4f0662

Please sign in to comment.