Rollup merge of #7034 - Ms2ger:mem, r=jdm

Create a run_with_memory_reporting method to reduce the boilerplate a…

…ssociated with registering memory reporters.

<!-- Reviewable:start -->
[<img src="https://reviewable.io/review_button.png" height=40 alt="Review on Reviewable"/>](https://reviewable.io/reviews/servo/servo/7034)
<!-- Reviewable:end -->
This commit is contained in:
Manish Goregaokar 2015-08-07 03:45:29 +05:30
commit 730ee4d341
6 changed files with 68 additions and 80 deletions

View file

@ -28,12 +28,11 @@ use script_task::StackRootTLS;
use devtools_traits::DevtoolScriptControlMsg;
use msg::constellation_msg::PipelineId;
use net_traits::load_whole_resource;
use profile_traits::mem::{self, Reporter, ReporterRequest};
use util::task::spawn_named;
use util::task_state;
use util::task_state::{SCRIPT, IN_WORKER};
use ipc_channel::ipc::{self, IpcReceiver};
use ipc_channel::ipc::IpcReceiver;
use ipc_channel::router::ROUTER;
use js::jsapi::{JSContext, RootedValue, HandleValue};
use js::jsapi::{JSAutoRequest, JSAutoCompartment};
@ -192,26 +191,12 @@ impl DedicatedWorkerGlobalScope {
scope.execute_script(source);
}
// Register this task as a memory reporter.
let reporter_name = format!("worker-reporter-{}", random::<u64>());
let (reporter_sender, reporter_receiver) = ipc::channel().unwrap();
ROUTER.add_route(reporter_receiver.to_opaque(), box move |reporter_request| {
// Just injects an appropriate event into the worker task's queue.
let reporter_request: ReporterRequest = reporter_request.to().unwrap();
parent_sender.send(ScriptMsg::CollectReports(
reporter_request.reports_channel)).unwrap()
});
scope.mem_profiler_chan().send(mem::ProfilerMsg::RegisterReporter(
reporter_name.clone(),
Reporter(reporter_sender)));
while let Ok(event) = global.receive_event() {
global.handle_event(event);
}
// Unregister this task as a memory reporter.
let msg = mem::ProfilerMsg::UnregisterReporter(reporter_name);
scope.mem_profiler_chan().send(msg);
scope.mem_profiler_chan().run_with_memory_reporting(|| {
while let Ok(event) = global.receive_event() {
global.handle_event(event);
}
}, reporter_name, parent_sender, ScriptMsg::CollectReports);
});
}
}

View file

@ -73,7 +73,7 @@ use net_traits::LoadData as NetLoadData;
use net_traits::{AsyncResponseTarget, ResourceTask, LoadConsumer, ControlMsg, Metadata};
use net_traits::image_cache_task::{ImageCacheChan, ImageCacheTask, ImageCacheResult};
use net_traits::storage_task::StorageTask;
use profile_traits::mem::{self, Report, Reporter, ReporterRequest, ReportKind, ReportsChan};
use profile_traits::mem::{self, Report, ReportKind, ReportsChan, OpaqueSender};
use string_cache::Atom;
use util::str::DOMString;
use util::task::spawn_named_with_send_on_failure;
@ -216,6 +216,12 @@ pub trait ScriptChan {
fn clone(&self) -> Box<ScriptChan+Send>;
}
impl OpaqueSender<ScriptMsg> for Box<ScriptChan+Send> {
fn send(&self, msg: ScriptMsg) {
ScriptChan::send(&**self, msg).unwrap();
}
}
/// An interface for receiving ScriptMsg values in an event loop. Used for synchronous DOM
/// APIs that need to abstract over multiple kinds of event loops (worker/main thread) with
/// different Receiver interfaces.
@ -437,24 +443,10 @@ impl ScriptTaskFactory for ScriptTask {
load_data.url.clone());
script_task.start_page_load(new_load, load_data);
// Register this task as a memory reporter.
let reporter_name = format!("script-reporter-{}", id.0);
let (reporter_sender, reporter_receiver) = ipc::channel().unwrap();
ROUTER.add_route(reporter_receiver.to_opaque(), box move |reporter_request| {
// Just injects an appropriate event into the worker task's queue.
let reporter_request: ReporterRequest = reporter_request.to().unwrap();
channel_for_reporter.send(ScriptMsg::CollectReports(
reporter_request.reports_channel)).unwrap()
});
let reporter = Reporter(reporter_sender);
let msg = mem::ProfilerMsg::RegisterReporter(reporter_name.clone(), reporter);
mem_profiler_chan.send(msg);
script_task.start();
// Unregister this task as a memory reporter.
let msg = mem::ProfilerMsg::UnregisterReporter(reporter_name);
mem_profiler_chan.send(msg);
mem_profiler_chan.run_with_memory_reporting(|| {
script_task.start();
}, reporter_name, channel_for_reporter, ScriptMsg::CollectReports);
// This must always be the very last operation performed before the task completes
failsafe.neuter();