Skip to content

Commit

Permalink
Merge pull request #147 from 56quarters/more-profiling
Browse files Browse the repository at this point in the history
Add support for profiling to `dns` binary
  • Loading branch information
56quarters authored Jun 23, 2024
2 parents 10c6e77 + b29ab64 commit 713dc12
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 40 deletions.
15 changes: 14 additions & 1 deletion mtop/src/bin/dns.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use clap::{Args, Parser, Subcommand, ValueHint};
use mtop::profile;
use mtop_client::dns::{Flags, Message, MessageId, Name, Question, Record, RecordClass, RecordType};
use std::fmt::Write;
use std::io::Cursor;
Expand All @@ -21,6 +22,11 @@ struct DnsConfig {
#[arg(long, default_value_t = DEFAULT_LOG_LEVEL)]
log_level: Level,

/// Output pprof protobuf profile data to this file if profiling support was enabled
/// at build time.
#[arg(long, value_hint = ValueHint::FilePath)]
profile_output: Option<PathBuf>,

#[command(subcommand)]
mode: Action,
}
Expand Down Expand Up @@ -91,11 +97,18 @@ async fn main() -> ExitCode {
mtop::tracing::console_subscriber(opts.log_level).expect("failed to setup console logging");
tracing::subscriber::set_global_default(console_subscriber).expect("failed to initialize console logging");

match &opts.mode {
let profiling = profile::Writer::default();
let code = match &opts.mode {
Action::Query(cmd) => run_query(cmd).await,
Action::Read(cmd) => run_read(cmd).await,
Action::Write(cmd) => run_write(cmd).await,
};

if let Some(p) = opts.profile_output {
profiling.finish(p);
}

code
}

async fn run_query(cmd: &QueryCommand) -> ExitCode {
Expand Down
38 changes: 3 additions & 35 deletions mtop/src/bin/mc.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
use clap::{Args, Parser, Subcommand, ValueHint};
use mtop::bench::{Bencher, Percent, Summary};
use mtop::check::{Checker, TimingBundle};
use mtop::profile::Profiler;
use mtop::profile;
use mtop_client::{
DiscoveryDefault, MemcachedClient, MemcachedPool, Meta, MtopError, PoolConfig, SelectorRendezvous, Server,
TLSConfig, Timeout, Value,
};
use std::fs::File;
use std::io::Write;
use std::path::{Path, PathBuf};
use std::path::PathBuf;
use std::process::ExitCode;
use std::time::Duration;
use std::{env, io};
Expand Down Expand Up @@ -322,7 +320,7 @@ async fn main() -> ExitCode {
return ExitCode::FAILURE;
};

let profiling = Profiling::default();
let profiling = profile::Writer::default();
let code = match &opts.mode {
Action::Add(cmd) => run_add(&opts, cmd, &client).await,
Action::Bench(cmd) => run_bench(&opts, cmd, client).await,
Expand All @@ -344,36 +342,6 @@ async fn main() -> ExitCode {
code
}

#[derive(Debug, Default)]
pub struct Profiling {
profiler: Profiler,
}

impl Profiling {
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()?)
}
}

async fn new_client(opts: &McConfig, servers: &[Server]) -> Result<MemcachedClient, MtopError> {
let tls = TLSConfig {
enabled: opts.tls_enabled,
Expand Down
11 changes: 7 additions & 4 deletions mtop/src/profile/mod.rs
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;
35 changes: 35 additions & 0 deletions mtop/src/profile/writer.rs
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()?)
}
}

0 comments on commit 713dc12

Please sign in to comment.