Skip to main content

u_schedule/dispatching/
context.rs

1//! Scheduling context for dispatching rule evaluation.
2
3use std::collections::HashMap;
4
5/// Runtime scheduling state passed to dispatching rules.
6///
7/// Contains the current simulation clock, remaining work estimates,
8/// resource utilization, and arrival times needed by context-aware rules.
9///
10/// All times are in milliseconds relative to the scheduling epoch (t=0).
11#[derive(Debug, Clone, Default)]
12pub struct SchedulingContext {
13    /// Current simulation time (ms).
14    pub current_time_ms: i64,
15    /// Remaining processing work per task (task_id → ms).
16    pub remaining_work: HashMap<String, i64>,
17    /// Queue length at next resource per task.
18    pub next_queue_length: HashMap<String, usize>,
19    /// Current resource utilization (resource_id → 0.0..1.0).
20    pub resource_utilization: HashMap<String, f64>,
21    /// Task arrival times (task_id → ms).
22    pub arrival_times: HashMap<String, i64>,
23    /// Average processing time across all tasks (for ATC normalization).
24    pub average_processing_time: Option<f64>,
25}
26
27impl SchedulingContext {
28    /// Creates a context at the given time.
29    pub fn at_time(current_time_ms: i64) -> Self {
30        Self {
31            current_time_ms,
32            ..Default::default()
33        }
34    }
35
36    /// Sets remaining work for a task.
37    pub fn with_remaining_work(mut self, task_id: impl Into<String>, ms: i64) -> Self {
38        self.remaining_work.insert(task_id.into(), ms);
39        self
40    }
41
42    /// Sets queue length for a task.
43    pub fn with_next_queue(mut self, task_id: impl Into<String>, length: usize) -> Self {
44        self.next_queue_length.insert(task_id.into(), length);
45        self
46    }
47
48    /// Sets resource utilization.
49    pub fn with_utilization(mut self, resource_id: impl Into<String>, load: f64) -> Self {
50        self.resource_utilization.insert(resource_id.into(), load);
51        self
52    }
53
54    /// Sets arrival time for a task.
55    pub fn with_arrival_time(mut self, task_id: impl Into<String>, time_ms: i64) -> Self {
56        self.arrival_times.insert(task_id.into(), time_ms);
57        self
58    }
59
60    /// Sets the average processing time.
61    pub fn with_average_processing_time(mut self, avg_ms: f64) -> Self {
62        self.average_processing_time = Some(avg_ms);
63        self
64    }
65}