-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #147 from 56quarters/more-profiling
Add support for profiling to `dns` binary
- Loading branch information
Showing
4 changed files
with
59 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,12 @@ | ||
#[cfg(not(feature = "profile"))] | ||
mod nop; | ||
#[cfg(not(feature = "profile"))] | ||
pub use nop::Profiler; | ||
#[cfg(feature = "profile")] | ||
pub use proto::Profiler; | ||
|
||
#[cfg(not(feature = "profile"))] | ||
mod nop; | ||
#[cfg(feature = "profile")] | ||
mod proto; | ||
#[cfg(feature = "profile")] | ||
pub use proto::Profiler; | ||
|
||
mod writer; | ||
pub use writer::Writer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
use crate::profile::Profiler; | ||
use mtop_client::MtopError; | ||
use std::fs::File; | ||
use std::io::Write; | ||
use std::path::Path; | ||
|
||
#[derive(Debug, Default)] | ||
pub struct Writer { | ||
profiler: Profiler, | ||
} | ||
|
||
impl Writer { | ||
pub fn finish<P>(&self, path: P) | ||
where | ||
P: AsRef<Path>, | ||
{ | ||
if let Err(e) = self.profiler.proto().and_then(|b| Self::write_file(&path, &b)) { | ||
tracing::warn!(message = "unable to collect and write pprof data", path = ?path.as_ref(), err = %e); | ||
} | ||
} | ||
|
||
fn write_file<P>(path: P, contents: &[u8]) -> Result<(), MtopError> | ||
where | ||
P: AsRef<Path>, | ||
{ | ||
if contents.is_empty() { | ||
return Err(MtopError::configuration("mtop built without profiling support")); | ||
} | ||
|
||
let mut file = File::options().create(true).truncate(true).write(true).open(path)?; | ||
file.write_all(contents)?; | ||
file.flush()?; | ||
Ok(file.sync_all()?) | ||
} | ||
} |