mmtk/scheduler/worker_goals.rs
1//! This module contain "goals" which are larger than work packets, and describes what workers are
2//! working towards on a high level.
3//!
4//! A "goal" is represented by a `WorkerGoal`. All workers work towards a single goal at a time.
5//! The current goal influences the behavior of GC workers, especially the last parked worker.
6//! For example,
7//!
8//! - When in the progress of GC, the last parker will try to open buckets or announce the GC
9//! has finished.
10//! - When stopping for fork, every waken worker should save its thread state (giving in the
11//! `GCWorker` struct) and exit.
12//!
13//! The struct `WorkerGoals` keeps the set of goals requested by mutators, but GC workers will only
14//! respond to one request at a time, and will favor higher-priority goals.
15
16use enum_map::{Enum, EnumMap};
17
18/// This current and reqeusted goals.
19#[derive(Default, Debug)]
20pub(crate) struct WorkerGoals {
21 /// The current goal.
22 current: Option<WorkerGoal>,
23 /// Requests received from mutators. `requests[goal]` is true if the `goal` is requested.
24 requests: EnumMap<WorkerGoal, bool>,
25}
26
27/// A goal, i.e. something that workers should work together to achieve.
28///
29/// Members of this `enum` should be listed from the highest priority to the lowest priority.
30#[derive(Debug, Enum, Clone, Copy)]
31pub(crate) enum WorkerGoal {
32 /// Do a garbage collection.
33 Gc,
34 /// Stop all GC threads so that the VM can call `fork()`.
35 StopForFork,
36}
37
38impl WorkerGoals {
39 /// Set the `goal` as requested. Return `true` if the requested state of the `goal` changed
40 /// from `false` to `true`.
41 pub fn set_request(&mut self, goal: WorkerGoal) -> bool {
42 if !self.requests[goal] {
43 self.requests[goal] = true;
44 true
45 } else {
46 false
47 }
48 }
49
50 /// Move the highest priority goal from the pending requests to the current request. Return
51 /// that goal, or `None` if no goal has been requested.
52 pub fn poll_next_goal(&mut self) -> Option<WorkerGoal> {
53 for (goal, requested) in self.requests.iter_mut() {
54 if *requested {
55 *requested = false;
56 self.current = Some(goal);
57 probe!(mmtk, goal_set, goal);
58 return Some(goal);
59 }
60 }
61 None
62 }
63
64 /// Get the current goal if exists.
65 pub fn current(&self) -> Option<WorkerGoal> {
66 self.current
67 }
68
69 /// Called when the current goal is completed. This will clear the current goal.
70 pub fn on_current_goal_completed(&mut self) {
71 probe!(mmtk, goal_complete);
72 self.current = None
73 }
74
75 /// Test if the given `goal` is requested. Used for debug purpose, only. The workers always
76 /// respond to the request of the highest priority first.
77 pub fn debug_is_requested(&self, goal: WorkerGoal) -> bool {
78 self.requests[goal]
79 }
80}
81
82#[cfg(test)]
83mod tests {
84 use super::{WorkerGoal, WorkerGoals};
85
86 #[test]
87 fn test_poll_none() {
88 let mut goals = WorkerGoals::default();
89 let next_goal = goals.poll_next_goal();
90
91 assert!(next_goal.is_none());
92 assert!(goals.current().is_none());
93 }
94
95 #[test]
96 fn test_poll_one() {
97 let mut goals = WorkerGoals::default();
98 goals.set_request(WorkerGoal::StopForFork);
99 let next_goal = goals.poll_next_goal();
100
101 assert!(matches!(next_goal, Some(WorkerGoal::StopForFork)));
102 assert!(matches!(goals.current(), Some(WorkerGoal::StopForFork)));
103 }
104
105 #[test]
106 fn test_goals_priority() {
107 let mut goals = WorkerGoals::default();
108 goals.set_request(WorkerGoal::StopForFork);
109 goals.set_request(WorkerGoal::Gc);
110
111 let next_goal = goals.poll_next_goal();
112
113 assert!(matches!(next_goal, Some(WorkerGoal::Gc)));
114 assert!(matches!(goals.current(), Some(WorkerGoal::Gc)));
115 }
116}