#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(usize)]
pub enum Op {
SystemCall,
Macrostep,
Microstep,
InternalEvent,
Candidate,
Guard,
Exit,
Enter,
Action,
ContextMerge,
Expr,
Effect,
TimerSchedule,
TimerCancel,
ActorStart,
ActorStop,
Message,
EffectStored,
ActiveState,
ContextValue,
ActorScan,
}
impl Op {
pub const ALL: [Op; 21] = [
Op::SystemCall,
Op::Macrostep,
Op::Microstep,
Op::InternalEvent,
Op::Candidate,
Op::Guard,
Op::Exit,
Op::Enter,
Op::Action,
Op::ContextMerge,
Op::Expr,
Op::Effect,
Op::TimerSchedule,
Op::TimerCancel,
Op::ActorStart,
Op::ActorStop,
Op::Message,
Op::EffectStored,
Op::ActiveState,
Op::ContextValue,
Op::ActorScan,
];
pub fn name(self) -> &'static str {
match self {
Op::SystemCall => "system_call",
Op::Macrostep => "macrostep",
Op::Microstep => "microstep",
Op::InternalEvent => "internal_event",
Op::Candidate => "candidate",
Op::Guard => "guard",
Op::Exit => "exit",
Op::Enter => "enter",
Op::Action => "action",
Op::ContextMerge => "context_merge",
Op::Expr => "expr",
Op::Effect => "effect",
Op::TimerSchedule => "timer_schedule",
Op::TimerCancel => "timer_cancel",
Op::ActorStart => "actor_start",
Op::ActorStop => "actor_stop",
Op::Message => "message",
Op::EffectStored => "effect_stored",
Op::ActiveState => "active_state",
Op::ContextValue => "context_value",
Op::ActorScan => "actor_scan",
}
}
}
pub type Counts = [u64; Op::ALL.len()];
#[cfg(feature = "op-counts")]
thread_local! {
static COUNTS: std::cell::Cell<Counts> = const { std::cell::Cell::new([0; Op::ALL.len()]) };
static ENABLED: std::cell::Cell<bool> = const { std::cell::Cell::new(false) };
}
pub fn set_enabled(on: bool) {
#[cfg(feature = "op-counts")]
ENABLED.with(|e| e.set(on));
#[cfg(not(feature = "op-counts"))]
let _ = on;
}
#[inline(always)]
pub fn enabled() -> bool {
#[cfg(feature = "op-counts")]
return ENABLED.with(|e| e.get());
#[cfg(not(feature = "op-counts"))]
false
}
#[inline(always)]
pub fn count(op: Op) {
count_n(op, 1);
}
#[inline(always)]
pub fn count_n(op: Op, n: u64) {
#[cfg(feature = "op-counts")]
if enabled() {
COUNTS.with(|c| {
let mut counts = c.get();
counts[op as usize] += n;
c.set(counts);
});
}
#[cfg(not(feature = "op-counts"))]
let _ = (op, n);
}
pub fn json_size(value: &serde_json::Value) -> u64 {
1 + match value {
serde_json::Value::Array(items) => items.iter().map(json_size).sum(),
serde_json::Value::Object(map) => map.values().map(json_size).sum(),
_ => 0,
}
}
pub fn take() -> Counts {
#[cfg(feature = "op-counts")]
return COUNTS.with(|c| c.replace([0; Op::ALL.len()]));
#[cfg(not(feature = "op-counts"))]
[0; Op::ALL.len()]
}