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 permanently.
35 Shutdown,
36 /// Stop all GC threads so that the VM can call `fork()`.
37 StopForFork,
38}
39
40impl WorkerGoals {
41 /// Set the `goal` as requested. Return `true` if the requested state of the `goal` changed
42 /// from `false` to `true`.
43 pub fn set_request(&mut self, goal: WorkerGoal) -> bool {
44 if !self.requests[goal] {
45 self.requests[goal] = true;
46 true
47 } else {
48 false
49 }
50 }
51
52 /// Move the highest priority goal from the pending requests to the current request. Return
53 /// that goal, or `None` if no goal has been requested.
54 pub fn poll_next_goal(&mut self) -> Option<WorkerGoal> {
55 for (goal, requested) in self.requests.iter_mut() {
56 if *requested {
57 *requested = false;
58 self.current = Some(goal);
59 probe!(mmtk, goal_set, goal);
60 return Some(goal);
61 }
62 }
63 None
64 }
65
66 /// Get the current goal if exists.
67 pub fn current(&self) -> Option<WorkerGoal> {
68 self.current
69 }
70
71 /// Called when the current goal is completed. This will clear the current goal.
72 pub fn on_current_goal_completed(&mut self) {
73 probe!(mmtk, goal_complete);
74 self.current = None
75 }
76
77 /// Test if the given `goal` is requested. Used for debug purpose, only. The workers always
78 /// respond to the request of the highest priority first.
79 pub fn debug_is_requested(&self, goal: WorkerGoal) -> bool {
80 self.requests[goal]
81 }
82}
83
84#[cfg(test)]
85mod tests {
86 use super::{WorkerGoal, WorkerGoals};
87
88 #[test]
89 fn test_poll_none() {
90 let mut goals = WorkerGoals::default();
91 let next_goal = goals.poll_next_goal();
92
93 assert!(next_goal.is_none());
94 assert!(goals.current().is_none());
95 }
96
97 #[test]
98 fn test_poll_one() {
99 let mut goals = WorkerGoals::default();
100 goals.set_request(WorkerGoal::StopForFork);
101 let next_goal = goals.poll_next_goal();
102
103 assert!(matches!(next_goal, Some(WorkerGoal::StopForFork)));
104 assert!(matches!(goals.current(), Some(WorkerGoal::StopForFork)));
105 }
106
107 #[test]
108 fn test_goals_priority() {
109 let mut goals = WorkerGoals::default();
110 goals.set_request(WorkerGoal::StopForFork);
111 goals.set_request(WorkerGoal::Gc);
112
113 let next_goal = goals.poll_next_goal();
114
115 assert!(matches!(next_goal, Some(WorkerGoal::Gc)));
116 assert!(matches!(goals.current(), Some(WorkerGoal::Gc)));
117 }
118}