Skip to content

Commit

Permalink
format components/profile_traits
Browse files Browse the repository at this point in the history
  • Loading branch information
AnshulMalik committed Sep 10, 2018
1 parent 6cb39fa commit ce9231c
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 57 deletions.
6 changes: 1 addition & 5 deletions components/profile_traits/energy.rs
Expand Up @@ -2,7 +2,6 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */


#[cfg(feature = "energy-profiling")]
pub fn read_energy_uj() -> u64 {
energymon::read_energy_uj()
Expand Down Expand Up @@ -33,7 +32,6 @@ mod energymon {
use std::mem;
use std::sync::{Once, ONCE_INIT};


static mut EM: Option<*mut EnergyMon> = None;

fn init() {
Expand All @@ -60,9 +58,7 @@ mod energymon {

pub fn get_min_interval_ms() -> u32 {
init();
unsafe {
EM.map_or(0, |em| ((*em).interval_us() as f64 / 1000.0).ceil() as u32)
}
unsafe { EM.map_or(0, |em| ((*em).interval_us() as f64 / 1000.0).ceil() as u32) }
}

}
18 changes: 14 additions & 4 deletions components/profile_traits/ipc.rs
Expand Up @@ -10,12 +10,18 @@ use time;
use time::ProfilerCategory;
use time::ProfilerChan;

pub struct IpcReceiver<T> where T: for<'de> Deserialize<'de> + Serialize {
pub struct IpcReceiver<T>
where
T: for<'de> Deserialize<'de> + Serialize,
{
ipc_receiver: ipc::IpcReceiver<T>,
time_profile_chan: ProfilerChan,
}

impl<T> IpcReceiver<T> where T: for<'de> Deserialize<'de> + Serialize {
impl<T> IpcReceiver<T>
where
T: for<'de> Deserialize<'de> + Serialize,
{
pub fn recv(&self) -> Result<T, bincode::Error> {
time::profile(
ProfilerCategory::IpcReceiver,
Expand All @@ -34,8 +40,12 @@ impl<T> IpcReceiver<T> where T: for<'de> Deserialize<'de> + Serialize {
}
}

pub fn channel<T>(time_profile_chan: ProfilerChan) -> Result<(ipc::IpcSender<T>, IpcReceiver<T>), Error>
where T: for<'de> Deserialize<'de> + Serialize, {
pub fn channel<T>(
time_profile_chan: ProfilerChan,
) -> Result<(ipc::IpcSender<T>, IpcReceiver<T>), Error>
where
T: for<'de> Deserialize<'de> + Serialize,
{
let (ipc_sender, ipc_receiver) = ipc::channel()?;
let profiled_ipc_receiver = IpcReceiver {
ipc_receiver,
Expand Down
3 changes: 2 additions & 1 deletion components/profile_traits/lib.rs
Expand Up @@ -12,7 +12,8 @@ extern crate bincode;
extern crate ipc_channel;
#[macro_use]
extern crate log;
#[macro_use] extern crate serde;
#[macro_use]
extern crate serde;
extern crate servo_config;
extern crate signpost;

Expand Down
61 changes: 39 additions & 22 deletions components/profile_traits/mem.rs
Expand Up @@ -21,15 +21,24 @@ pub trait OpaqueSender<T> {
impl<T> OpaqueSender<T> for Sender<T> {
fn send(&self, message: T) {
if let Err(e) = Sender::send(self, message) {
warn!("Error communicating with the target thread from the profiler: {}", e);
warn!(
"Error communicating with the target thread from the profiler: {}",
e
);
}
}
}

impl<T> OpaqueSender<T> for IpcSender<T> where T: serde::Serialize {
impl<T> OpaqueSender<T> for IpcSender<T>
where
T: serde::Serialize,
{
fn send(&self, message: T) {
if let Err(e) = IpcSender::send(self, message) {
warn!("Error communicating with the target thread from the profiler: {}", e);
warn!(
"Error communicating with the target thread from the profiler: {}",
e
);
}
}
}
Expand All @@ -50,24 +59,32 @@ impl ProfilerChan {
}

/// Runs `f()` with memory profiling.
pub fn run_with_memory_reporting<F, M, T, C>(&self, f: F,
reporter_name: String,
channel_for_reporter: C,
msg: M)
where F: FnOnce(),
M: Fn(ReportsChan) -> T + Send + 'static,
T: Send + 'static,
C: OpaqueSender<T> + Send + 'static
pub fn run_with_memory_reporting<F, M, T, C>(
&self,
f: F,
reporter_name: String,
channel_for_reporter: C,
msg: M,
) where
F: FnOnce(),
M: Fn(ReportsChan) -> T + Send + 'static,
T: Send + 'static,
C: OpaqueSender<T> + Send + 'static,
{
// Register the memory reporter.
let (reporter_sender, reporter_receiver) = ipc::channel().unwrap();
ROUTER.add_route(reporter_receiver.to_opaque(), Box::new(move |message| {
// Just injects an appropriate event into the paint thread's queue.
let request: ReporterRequest = message.to().unwrap();
channel_for_reporter.send(msg(request.reports_channel));
}));
self.send(ProfilerMsg::RegisterReporter(reporter_name.clone(),
Reporter(reporter_sender)));
ROUTER.add_route(
reporter_receiver.to_opaque(),
Box::new(move |message| {
// Just injects an appropriate event into the paint thread's queue.
let request: ReporterRequest = message.to().unwrap();
channel_for_reporter.send(msg(request.reports_channel));
}),
);
self.send(ProfilerMsg::RegisterReporter(
reporter_name.clone(),
Reporter(reporter_sender),
));

f();

Expand Down Expand Up @@ -154,9 +171,10 @@ pub struct Reporter(pub IpcSender<ReporterRequest>);
impl Reporter {
/// Collect one or more memory reports. Returns true on success, and false on failure.
pub fn collect_reports(&self, reports_chan: ReportsChan) {
self.0.send(ReporterRequest {
reports_channel: reports_chan,
}).unwrap()
self.0
.send(ReporterRequest {
reports_channel: reports_chan,
}).unwrap()
}
}

Expand Down Expand Up @@ -188,4 +206,3 @@ pub enum ProfilerMsg {
/// Tells the memory profiler to shut down.
Exit,
}

65 changes: 40 additions & 25 deletions components/profile_traits/time.rs
Expand Up @@ -37,9 +37,16 @@ pub enum ProfilerData {
#[derive(Clone, Deserialize, Serialize)]
pub enum ProfilerMsg {
/// Normal message used for reporting time
Time((ProfilerCategory, Option<TimerMetadata>), (u64, u64), (u64, u64)),
Time(
(ProfilerCategory, Option<TimerMetadata>),
(u64, u64),
(u64, u64),
),
/// Message used to get time spend entries for a particular ProfilerBuckets (in nanoseconds)
Get((ProfilerCategory, Option<TimerMetadata>), IpcSender<ProfilerData>),
Get(
(ProfilerCategory, Option<TimerMetadata>),
IpcSender<ProfilerData>,
),
/// Message used to force print the profiling metrics
Print,
/// Tells the profiler to shut down.
Expand Down Expand Up @@ -118,12 +125,14 @@ pub enum TimerMetadataReflowType {
FirstReflow,
}

pub fn profile<T, F>(category: ProfilerCategory,
meta: Option<TimerMetadata>,
profiler_chan: ProfilerChan,
callback: F)
-> T
where F: FnOnce() -> T,
pub fn profile<T, F>(
category: ProfilerCategory,
meta: Option<TimerMetadata>,
profiler_chan: ProfilerChan,
callback: F,
) -> T
where
F: FnOnce() -> T,
{
if opts::get().signpost {
signpost::start(category as u32, &[0, 0, 0, (category as usize) >> 4]);
Expand All @@ -139,24 +148,30 @@ pub fn profile<T, F>(category: ProfilerCategory,
signpost::end(category as u32, &[0, 0, 0, (category as usize) >> 4]);
}

send_profile_data(category,
meta,
&profiler_chan,
start_time,
end_time,
start_energy,
end_energy);
send_profile_data(
category,
meta,
&profiler_chan,
start_time,
end_time,
start_energy,
end_energy,
);
val
}

pub fn send_profile_data(category: ProfilerCategory,
meta: Option<TimerMetadata>,
profiler_chan: &ProfilerChan,
start_time: u64,
end_time: u64,
start_energy: u64,
end_energy: u64) {
profiler_chan.send(ProfilerMsg::Time((category, meta),
(start_time, end_time),
(start_energy, end_energy)));
pub fn send_profile_data(
category: ProfilerCategory,
meta: Option<TimerMetadata>,
profiler_chan: &ProfilerChan,
start_time: u64,
end_time: u64,
start_energy: u64,
end_energy: u64,
) {
profiler_chan.send(ProfilerMsg::Time(
(category, meta),
(start_time, end_time),
(start_energy, end_energy),
));
}

0 comments on commit ce9231c

Please sign in to comment.