use std::fmt;
use crate::cpuset::CpuSet;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Severity {
Warn,
Info,
}
impl Severity {
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::Warn => "warn",
Self::Info => "info",
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Status {
Finding,
Ok,
Unknown(String),
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Finding {
pub id: &'static str,
pub severity: Severity,
pub status: Status,
pub message: String,
pub evidence: &'static str,
}
impl Finding {
pub(crate) const fn found(
id: &'static str,
severity: Severity,
evidence: &'static str,
message: String,
) -> Self {
Self {
id,
severity,
status: Status::Finding,
message,
evidence,
}
}
pub(crate) const fn ok(
id: &'static str,
severity: Severity,
evidence: &'static str,
message: String,
) -> Self {
Self {
id,
severity,
status: Status::Ok,
message,
evidence,
}
}
pub(crate) fn unknown(
id: &'static str,
severity: Severity,
evidence: &'static str,
message: String,
reason: impl fmt::Display,
) -> Self {
Self {
id,
severity,
status: Status::Unknown(reason.to_string()),
message,
evidence,
}
}
#[must_use]
pub const fn is_finding(&self) -> bool {
matches!(self.status, Status::Finding)
}
#[must_use]
pub const fn is_warning(&self) -> bool {
self.is_finding() && matches!(self.severity, Severity::Warn)
}
#[must_use]
pub const fn is_unknown(&self) -> bool {
matches!(self.status, Status::Unknown(_))
}
}
impl fmt::Display for Finding {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match &self.status {
Status::Finding => write!(
f,
"{} {}: {} (evidence: {})",
self.id,
self.severity.as_str(),
self.message,
self.evidence
),
Status::Ok => write!(f, "{} ok: {}", self.id, self.message),
Status::Unknown(reason) => write!(f, "{} unknown: {}: {reason}", self.id, self.message),
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Component {
Md,
Io,
Rt,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Role {
Loop,
Sender,
Receiver,
SharedAgent,
SqPoll,
Conductor,
EalLcore,
Housekeeping,
}
impl Role {
#[must_use]
pub const fn name(self) -> &'static str {
match self {
Self::Loop => "loop",
Self::Sender => "sender",
Self::Receiver => "receiver",
Self::SharedAgent => "shared agent",
Self::SqPoll => "SQPOLL thread",
Self::Conductor => "conductor",
Self::EalLcore => "EAL lcore",
Self::Housekeeping => "housekeeping",
}
}
#[must_use]
pub const fn spins(self) -> bool {
matches!(
self,
Self::Loop | Self::Sender | Self::Receiver | Self::SharedAgent | Self::SqPoll
)
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct HotCpu {
pub cpu: u32,
pub role: Role,
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct HotSet {
cpus: Vec<HotCpu>,
}
impl HotSet {
#[must_use]
pub const fn new() -> Self {
Self { cpus: Vec::new() }
}
#[must_use]
pub fn with(mut self, cpu: u32, role: Role) -> Self {
self.push(cpu, role);
self
}
pub fn push(&mut self, cpu: u32, role: Role) {
let h = HotCpu { cpu, role };
if !self.cpus.contains(&h) {
self.cpus.push(h);
}
}
pub fn iter(&self) -> impl Iterator<Item = &HotCpu> {
self.cpus.iter()
}
#[must_use]
pub fn spinning(&self) -> CpuSet {
self.cpus
.iter()
.filter(|h| h.role.spins())
.map(|h| h.cpu)
.collect()
}
#[must_use]
pub fn all(&self) -> CpuSet {
self.cpus.iter().map(|h| h.cpu).collect()
}
#[must_use]
pub fn roles(&self, cpu: u32) -> Vec<Role> {
self.cpus
.iter()
.filter(|h| h.cpu == cpu)
.map(|h| h.role)
.collect()
}
#[must_use]
pub fn describe(&self, cpu: u32) -> String {
let roles: Vec<&str> = self.roles(cpu).iter().map(|r| r.name()).collect();
if roles.is_empty() {
format!("CPU {cpu}")
} else {
format!("CPU {cpu} ({})", roles.join(", "))
}
}
pub(crate) fn describe_all(&self, cpus: &CpuSet) -> String {
cpus.iter()
.map(|c| self.describe(c))
.collect::<Vec<_>>()
.join(", ")
}
}