d3_core/scheduler/
traits.rs

1use self::sched::SchedStats;
2use super::*;
3use crate::machine_impl::*;
4use crossbeam::deque;
5use d3_derive::*;
6
7/// These are traits that are used for major components
8
9/// Messages which can be sent to the system monitor
10#[allow(dead_code)]
11#[derive(Debug, Clone)]
12pub enum MonitorMessage {
13    // Terminate the monitor
14    Terminate,
15    // Add Sender
16    AddSender(CoreStatsSender),
17    // Remove Sender
18    RemoveSender(CoreStatsSender),
19    // Sent by an executor when it parks
20    Parked(usize),
21    // Sent by a executor as it completes, signalling that its joinable
22    Terminated(usize),
23    // Sent by an executor, providing periodic stats
24    ExecutorStats(ExecutorStats),
25    // Sent by the scheduler, providing periodic stats
26    SchedStats(SchedStats),
27    // Sent by the scheduler, providing machine info
28    MachineInfo(ShareableMachine),
29}
30pub type MonitorSender = crossbeam::channel::Sender<MonitorMessage>;
31
32#[derive(Debug, Copy, Clone, MachineImpl)]
33pub enum CoreStatsMessage {
34    // Sent by an executor, providing periodic stats
35    ExecutorStats(ExecutorStats),
36    // Sent by the scheduler, providing periodic stats
37    SchedStats(SchedStats),
38}
39pub type CoreStatsSender = Sender<CoreStatsMessage>;
40
41// The factory for the system monitor
42pub trait MonitorFactory {
43    /// get a clone of the sender for the system monitor
44    fn get_sender(&self) -> MonitorSender;
45    /// start the system monitor
46    fn start(&self, executor: ExecutorControlObj) -> MonitorControlObj;
47}
48pub type MonitorFactoryObj = Arc<dyn MonitorFactory>;
49
50// The controller for the system monitor.
51pub trait MonitorControl: Send + Sync {
52    /// stop the system monitor
53    fn stop(&self);
54    /// add a stats sender to the system monitor
55    fn add_sender(&self, sender: CoreStatsSender);
56    /// remove a stats sender to the system monitor
57    fn remove_sender(&self, sender: CoreStatsSender);
58}
59pub type MonitorControlObj = Arc<dyn MonitorControl>;
60
61/// The factory for the executor
62pub trait ExecutorFactory {
63    /// set the number of executor threads
64    fn with_workers(&self, workers: usize);
65    /// get the system queues: run_queue, wait_queue
66    fn get_queues(&self) -> (TaskInjector, SchedTaskInjector);
67    /// start the executor
68    fn start(&self, monitor: MonitorSender, scheduler: SchedSender) -> ExecutorControlObj;
69}
70pub type ExecutorFactoryObj = Arc<dyn ExecutorFactory>;
71
72/// The model for a system queue
73pub type TaskInjector = Arc<deque::Injector<Task>>;
74pub type SchedTaskInjector = Arc<deque::Injector<SchedTask>>;
75
76pub trait ExecutorControl: Send + Sync {
77    /// notifies the executor that an executor is parked
78    fn parked_executor(&self, id: usize);
79    /// Wake parked threads
80    fn wake_parked_threads(&self);
81    /// notifies the executor that an executor completed and can be joined
82    fn joinable_executor(&self, id: usize);
83    /// get run_queue
84    fn get_run_queue(&self) -> TaskInjector;
85    /// stop the executor
86    fn stop(&self);
87    /// request stats from the executors
88    fn request_stats(&self);
89}
90pub type ExecutorControlObj = Arc<dyn ExecutorControl>;
91
92/// The schdeuler and executor commands
93#[allow(dead_code)]
94pub enum SchedCmd {
95    Start,
96    Stop,
97    Terminate(bool),
98    New(ShareableMachine),
99    SendComplete(usize),
100    Remove(usize),
101    ErrorRecv(usize),
102    RecvBlock(usize),
103    RequestStats,
104    RequestMachineInfo,
105    // Executor stuff
106    RebuildStealers,
107}
108pub type SchedSender = crossbeam::channel::Sender<SchedCmd>;
109pub type SchedReceiver = crossbeam::channel::Receiver<SchedCmd>;
110
111pub trait SchedulerFactory {
112    fn get_sender(&self) -> SchedSender;
113    // start has to return a sized object trait, I prefer Arc over Box
114    fn start(&self, monitor: MonitorSender, queues: (TaskInjector, SchedTaskInjector)) -> Arc<dyn Scheduler>;
115}
116/// The scheduler trait
117pub trait Scheduler: Send + Sync {
118    /// assigns a new machine, making it eligable for scheduling and running
119    fn assign_machine(&self, machine: ShareableMachine);
120    /// request stats from the scheduler
121    fn request_stats(&self);
122    /// request machine info
123    fn request_machine_info(&self);
124    /// stop the scheduler
125    fn stop(&self);
126}