use std::fmt;
use reverie::BackendStatsRequest;
use reverie::BackendStatsSnapshot;
use reverie::BackendStatsSource;
use reverie::DispatchStats;
use crate::Backend;
const TARGET: &str = "hermit::backend_stats";
pub(crate) fn request(summary_json: &Option<std::path::PathBuf>) -> BackendStatsRequest {
BackendStatsRequest::new(
summary_json.is_some() || tracing::enabled!(target: TARGET, tracing::Level::DEBUG),
)
}
pub(crate) fn report<S>(
selected_backend: Backend,
request: BackendStatsRequest,
source: &S,
) -> Option<DispatchStats>
where
S: BackendStatsSource,
{
assert_eq!(
selected_backend.as_str(),
S::Snapshot::BACKEND_NAME,
"backend statistics source does not match selected backend"
);
let snapshot = request.collect(source)?;
let dispatch = snapshot.dispatch_stats();
tracing::debug!(
target: TARGET,
backend = %selected_backend.as_str(),
stats = %snapshot,
dispatch = %DisplayDispatch(dispatch.as_ref()),
"backend run complete",
);
dispatch
}
pub(crate) fn report_if_collected<S>(
selected_backend: Backend,
request: BackendStatsRequest,
source: Option<&S>,
) -> Option<DispatchStats>
where
S: BackendStatsSource,
{
report(selected_backend, request, source?)
}
struct DisplayDispatch<'a>(Option<&'a DispatchStats>);
impl fmt::Display for DisplayDispatch<'_> {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.0 {
Some(record) => record.fmt(formatter),
None => formatter.write_str("none"),
}
}
}
#[cfg(test)]
mod tests {
use std::cell::Cell;
use super::*;
struct CountingSource {
snapshots: Cell<usize>,
}
struct CountingSnapshot;
impl fmt::Display for CountingSnapshot {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str("counting")
}
}
impl BackendStatsSnapshot for CountingSnapshot {
const BACKEND_NAME: &'static str = "ptrace";
fn dispatch_stats(&self) -> Option<DispatchStats> {
Some(DispatchStats::new(
Self::BACKEND_NAME,
reverie::DispatchCounters {
ptrace_seccomp_stops: Some(7),
..reverie::DispatchCounters::ZERO
},
reverie::SiteCounters::NONE_PATCHED,
))
}
}
impl BackendStatsSource for CountingSource {
type Snapshot = CountingSnapshot;
fn backend_stats(&self) -> Self::Snapshot {
self.snapshots.set(self.snapshots.get() + 1);
CountingSnapshot
}
}
#[test]
fn disabled_report_does_not_snapshot_backend() {
let source = CountingSource {
snapshots: Cell::new(0),
};
assert_eq!(
report(Backend::Ptrace, BackendStatsRequest::DISABLED, &source),
None
);
assert_eq!(source.snapshots.get(), 0);
let record = report(Backend::Ptrace, BackendStatsRequest::ENABLED, &source)
.expect("an enabled report returns the backend's dispatch record");
assert_eq!(source.snapshots.get(), 1);
assert_eq!(record.backend, "ptrace");
assert_eq!(record.counters.dispatches(), Some(7));
}
#[test]
fn a_requested_summary_collects_without_debug_logging() {
assert!(request(&Some(std::path::PathBuf::from("/summary.json"))).is_enabled());
}
#[test]
#[should_panic(expected = "backend statistics source does not match selected backend")]
fn report_rejects_a_mismatched_backend_source_in_release_builds() {
let source = CountingSource {
snapshots: Cell::new(0),
};
report(Backend::Liteinst, BackendStatsRequest::ENABLED, &source);
}
struct LiteinstLikeSource;
struct LiteinstLikeSnapshot;
impl fmt::Display for LiteinstLikeSnapshot {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str("liteinst-like")
}
}
impl BackendStatsSnapshot for LiteinstLikeSnapshot {
const BACKEND_NAME: &'static str = "liteinst";
fn dispatch_stats(&self) -> Option<DispatchStats> {
None
}
}
impl BackendStatsSource for LiteinstLikeSource {
type Snapshot = LiteinstLikeSnapshot;
fn backend_stats(&self) -> Self::Snapshot {
LiteinstLikeSnapshot
}
}
#[test]
fn report_accepts_a_matched_non_ptrace_source() {
report(
Backend::Liteinst,
BackendStatsRequest::DISABLED,
&LiteinstLikeSource,
);
report(
Backend::Liteinst,
BackendStatsRequest::ENABLED,
&LiteinstLikeSource,
);
}
#[test]
#[should_panic(expected = "backend statistics source does not match selected backend")]
fn report_checks_backend_wiring_even_when_stats_are_not_collected() {
let source = CountingSource {
snapshots: Cell::new(0),
};
report(Backend::Liteinst, BackendStatsRequest::DISABLED, &source);
}
}