use std::num::NonZeroU32;
use crate::snapshot::CacheSnapshot;
pub(crate) mod recording;
#[non_exhaustive]
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum LatencyMode {
#[default]
Off,
Full,
Sampled {
interval: NonZeroU32,
},
}
#[non_exhaustive]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct StatsOptions {
pub activity_counters: bool,
pub request_counters: bool,
pub l1_latency: LatencyMode,
pub l2_latency: LatencyMode,
pub mutation_latency: LatencyMode,
pub io_latency: bool,
pub shards: usize,
}
impl Default for StatsOptions {
fn default() -> Self {
Self {
activity_counters: false,
request_counters: false,
l1_latency: LatencyMode::Off,
l2_latency: LatencyMode::Off,
mutation_latency: LatencyMode::Off,
io_latency: false,
shards: 16,
}
}
}
impl StatsOptions {
pub(crate) fn requests_enabled(self) -> bool {
self.request_counters
|| self.l1_latency != LatencyMode::Off
|| self.l2_latency != LatencyMode::Off
|| self.mutation_latency != LatencyMode::Off
}
pub(crate) fn latency_mode(self, scope: RequestLatencyScope) -> LatencyMode {
match scope {
RequestLatencyScope::L1Hit => self.l1_latency,
RequestLatencyScope::L2Lookup => self.l2_latency,
RequestLatencyScope::Mutation => self.mutation_latency,
}
}
}
#[non_exhaustive]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum RequestOperation {
Get,
Put,
PutL2,
Delete,
}
impl RequestOperation {
pub const fn as_str(self) -> &'static str {
match self {
Self::Get => "get",
Self::Put => "put",
Self::PutL2 => "put_l2",
Self::Delete => "delete",
}
}
}
#[non_exhaustive]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum RequestOutcome {
L1Hit,
L2Hit,
Miss,
Accepted,
Overloaded,
InvalidInput,
Unavailable,
Error,
Cancelled,
}
impl RequestOutcome {
pub const fn as_str(self) -> &'static str {
match self {
Self::L1Hit => "l1_hit",
Self::L2Hit => "l2_hit",
Self::Miss => "miss",
Self::Accepted => "accepted",
Self::Overloaded => "overloaded",
Self::InvalidInput => "invalid_input",
Self::Unavailable => "unavailable",
Self::Error => "error",
Self::Cancelled => "cancelled",
}
}
}
#[non_exhaustive]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum IoRole {
Read,
Write,
Reclaim,
}
impl IoRole {
pub const fn as_str(self) -> &'static str {
match self {
Self::Read => "read",
Self::Write => "write",
Self::Reclaim => "reclaim",
}
}
}
#[non_exhaustive]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum IoOutcome {
Completed,
Cancelled,
Failed,
}
impl IoOutcome {
pub const fn as_str(self) -> &'static str {
match self {
Self::Completed => "completed",
Self::Cancelled => "cancelled",
Self::Failed => "failed",
}
}
}
pub const LATENCY_BUCKET_UPPER_BOUNDS_NS: &[u64] = &[
0,
50,
100,
250,
500,
1_000,
2_500,
5_000,
10_000,
25_000,
50_000,
100_000,
250_000,
500_000,
1_000_000,
2_500_000,
5_000_000,
10_000_000,
25_000_000,
50_000_000,
100_000_000,
250_000_000,
500_000_000,
1_000_000_000,
2_500_000_000,
5_000_000_000,
10_000_000_000,
30_000_000_000,
60_000_000_000,
];
#[non_exhaustive]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct LatencySnapshot {
pub bucket_counts: Box<[u64]>,
pub count: u64,
pub sum_ns: u128,
pub valid: bool,
}
#[non_exhaustive]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum RequestLatencyScope {
L1Hit,
L2Lookup,
Mutation,
}
impl RequestLatencyScope {
pub(crate) fn for_request(operation: RequestOperation, outcome: RequestOutcome) -> Self {
match operation {
RequestOperation::Get if outcome == RequestOutcome::L1Hit => Self::L1Hit,
RequestOperation::Get => Self::L2Lookup,
_ => Self::Mutation,
}
}
}
#[non_exhaustive]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct RequestStatsSnapshot {
pub operation: RequestOperation,
pub outcome: RequestOutcome,
pub count: Option<u64>,
pub latency_scope: RequestLatencyScope,
pub latency_mode: LatencyMode,
pub latency: Option<LatencySnapshot>,
}
#[non_exhaustive]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct IoLatencySnapshot {
pub role: IoRole,
pub outcome: IoOutcome,
pub latency: LatencySnapshot,
}
#[non_exhaustive]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct CacheStatsSnapshot {
pub summary: CacheSnapshot,
pub options: StatsOptions,
pub recorder_bytes: usize,
pub requests: Vec<RequestStatsSnapshot>,
pub io_latency: Vec<IoLatencySnapshot>,
}