Remove the fake memory profiler thread.

Currently if you don't specify the '-m' option, a fake do-nothing memory
profiler thread gets created instead of the real one. It ignores all
events except for `Exit`. And the timer thread doesn't get created so no
`Print` events are sent.

This changeset instead always creates the real thread. If you specify
'-m' the *timer* thread is still absent and it won't print anything.
However, the memory profiler thread will respond to register/unregister
events, which is good, because if there's a bug in those (e.g.
double-registration of a particular name) it'll show up in invocations
that lack '-m'.
This commit is contained in:
Nicholas Nethercote 2015-03-11 19:51:58 -07:00
parent ece2711185
commit fa9ca206ef

View file

@ -243,38 +243,28 @@ pub struct MemoryProfiler {
impl MemoryProfiler { impl MemoryProfiler {
pub fn create(period: Option<f64>) -> MemoryProfilerChan { pub fn create(period: Option<f64>) -> MemoryProfilerChan {
let (chan, port) = channel(); let (chan, port) = channel();
match period {
Some(period) => { // Create the timer thread if a period was provided.
let period = Duration::milliseconds((period * 1000f64) as i64); if let Some(period) = period {
let chan = chan.clone(); let period_ms = Duration::milliseconds((period * 1000f64) as i64);
spawn_named("Memory profiler timer".to_owned(), move || { let chan = chan.clone();
loop { spawn_named("Memory profiler timer".to_owned(), move || {
sleep(period); loop {
if chan.send(MemoryProfilerMsg::Print).is_err() { sleep(period_ms);
break; if chan.send(MemoryProfilerMsg::Print).is_err() {
} break;
} }
}); }
// Spawn the memory profiler. });
spawn_named("Memory profiler".to_owned(), move || {
let mut memory_profiler = MemoryProfiler::new(port);
memory_profiler.start();
});
}
None => {
// No-op to handle messages when the memory profiler is
// inactive.
spawn_named("Memory profiler".to_owned(), move || {
loop {
match port.recv() {
Err(_) | Ok(MemoryProfilerMsg::Exit) => break,
_ => {}
}
}
});
}
} }
// Always spawn the memory profiler. If there is no timer thread it won't receive regular
// `Print` events, but it will still receive the other events.
spawn_named("Memory profiler".to_owned(), move || {
let mut memory_profiler = MemoryProfiler::new(port);
memory_profiler.start();
});
MemoryProfilerChan(chan) MemoryProfilerChan(chan)
} }