Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 16 additions & 39 deletions crates/analyzer/src/fsm/collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,66 +9,43 @@ use rustc_hash::FxHashMap as HashMap;
use uuid::Uuid;

use crate::{
fsm::{Fsm, Transition},
fsm::Fsm,
resource::{Usage, Using},
};

/// Trait for types that hold a collection of [`Fsm`]s.
pub trait FsmCollection<F, T>
where
F: Fsm<TransitionType = T>,
T: Transition,
{
fn fsms<'a>(&'a self) -> impl Iterator<Item = &'a F> + 'a
where
F: 'a;
/// Trait for types that hold a collection of [`Fsm`]s of a single type.
///
/// An application with several FSM kinds unifies them under one [`Self::Fsm`]
/// type (e.g. an enum) so a single collection spans all of them.
pub trait FsmCollection {
/// The FSM type held by this collection.
type Fsm: Fsm;

fn contains_fsm_type(&self, type_name: &str) -> bool;
fn fsms(&self) -> impl Iterator<Item = &Self::Fsm>;
}

/// An in-memory collection of [`Fsm`]s.
pub struct InMemoryFsms<F, T>
where
F: Fsm<TransitionType = T>,
T: Transition,
{
pub struct InMemoryFsms<F: Fsm> {
pub fsms: HashMap<Uuid, F>,
pub fsm_type_names: HashSet<String>,
}

impl<F, T> FsmCollection<F, T> for InMemoryFsms<F, T>
where
F: Fsm<TransitionType = T>,
T: Transition,
{
fn fsms<'a>(&'a self) -> impl Iterator<Item = &'a F> + 'a
where
T: 'a,
{
self.fsms.values()
}
impl<F: Fsm> FsmCollection for InMemoryFsms<F> {
type Fsm = F;

fn contains_fsm_type(&self, type_name: &str) -> bool {
self.fsm_type_names.contains(type_name)
fn fsms(&self) -> impl Iterator<Item = &F> {
self.fsms.values()
}
}

impl<F, T> Using for InMemoryFsms<F, T>
where
F: Fsm<TransitionType = T> + Using,
T: Transition,
{
impl<F: Fsm + Using> Using for InMemoryFsms<F> {
fn usages<'a>(&'a self) -> impl Iterator<Item = impl Usage<'a>> {
self.fsms.values().flat_map(|fsm| fsm.usages())
}
}

#[cfg(test)]
impl<F, T> InMemoryFsms<F, T>
where
F: Fsm<TransitionType = T>,
T: Transition,
{
impl<F: Fsm> InMemoryFsms<F> {
pub(crate) fn new() -> Self {
Self {
fsms: Default::default(),
Expand Down
2 changes: 1 addition & 1 deletion crates/analyzer/src/fsm/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ impl RtFsmsBuilder {
.push(transition);
}

pub fn try_build(self) -> AnalyzerResult<InMemoryFsms<RtFsm, RtFsmTransition>> {
pub fn try_build(self) -> AnalyzerResult<InMemoryFsms<RtFsm>> {
// Build all FSMs.
let mut fsms: HashMap<Uuid, RtFsm> =
HashMap::with_capacity_and_hasher(self.fsms.capacity(), Default::default());
Expand Down
14 changes: 7 additions & 7 deletions crates/analyzer/src/timeline/binned/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ mod tests {
build_root_and_memory(&mut resources, resource_id);
let resources = resources.try_build().unwrap();

let mut fsms = InMemoryFsms::<RtFsm, RtFsmTransition>::new();
let mut fsms = InMemoryFsms::<RtFsm>::new();

// Produce triangle-ish memory utilization using 4 FSMs
for i in 0..4 {
Expand Down Expand Up @@ -296,7 +296,7 @@ mod tests {
build_root_and_memory(&mut resources, resource_b_id);
let resources = resources.try_build().unwrap();

let mut fsms = InMemoryFsms::<RtFsm, RtFsmTransition>::new();
let mut fsms = InMemoryFsms::<RtFsm>::new();

// Produce triangle-ish memory utilization using 4 FSMs
for i in 0..4 {
Expand Down Expand Up @@ -382,7 +382,7 @@ mod tests {
builder.push_group_raw(ROOT_RESOURCE_ID, "test", "test", None);
let mut resources = builder.try_build().unwrap();

let mut fsms = InMemoryFsms::<RtFsm, RtFsmTransition>::new();
let mut fsms = InMemoryFsms::<RtFsm>::new();

// Add a resource with 2 capacities.
resources.insert_type(ResourceTypeDecl::new(
Expand Down Expand Up @@ -484,7 +484,7 @@ mod tests {
build_root_and_memory(&mut resources, resource_id);
let resources = resources.try_build().unwrap();

let mut fsms = InMemoryFsms::<RtFsm, RtFsmTransition>::new();
let mut fsms = InMemoryFsms::<RtFsm>::new();

// Produce triangle-ish memory utilization using 4 FSMs with 2 usage states
for i in 0..4 {
Expand Down Expand Up @@ -605,7 +605,7 @@ mod tests {
build_root_and_memory(&mut resources, resource_b_id);
let resources = resources.try_build().unwrap();

let mut fsms = InMemoryFsms::<RtFsm, RtFsmTransition>::new();
let mut fsms = InMemoryFsms::<RtFsm>::new();

// Produce the same utilization as previous test but spread it out across two leaf resources.
for i in 0..4 {
Expand Down Expand Up @@ -771,7 +771,7 @@ mod tests {
.unwrap()
};

let mut outside_fsms = InMemoryFsms::<RtFsm, RtFsmTransition>::new();
let mut outside_fsms = InMemoryFsms::<RtFsm>::new();
outside_fsms.insert(make_fsm(0, 500));
outside_fsms.insert(make_fsm(2500, 3000));

Expand All @@ -780,7 +780,7 @@ mod tests {
outside_builder.try_extend(outside_fsms.usages()).unwrap();
assert!(!outside_builder.build().long_entities.contains(&resource_id));

let mut inside_fsms = InMemoryFsms::<RtFsm, RtFsmTransition>::new();
let mut inside_fsms = InMemoryFsms::<RtFsm>::new();
inside_fsms.insert(make_fsm(500, 1500));
inside_fsms.insert(make_fsm(1100, 1900));

Expand Down
20 changes: 4 additions & 16 deletions examples/simulator/analyzer/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,23 +132,11 @@ impl SimulatorModel {
}
}

impl
FsmCollection<
Task,
quent_analyzer::fsm::events::TransitionEvent<
quent_simulator_instrumentation::task::TaskTransition,
>,
> for SimulatorModel
{
fn fsms<'a>(&'a self) -> impl Iterator<Item = &'a Task> + 'a
where
Task: 'a,
{
self.tasks.values()
}
impl FsmCollection for SimulatorModel {
type Fsm = Task;

fn contains_fsm_type(&self, type_name: &str) -> bool {
!self.tasks.is_empty() && type_name == "task"
fn fsms(&self) -> impl Iterator<Item = &Task> {
self.tasks.values()
}
}

Expand Down
Loading