12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * version 2 for more details (a copy is included in the LICENSE file that
14 * accompanied this code).
15 *
16 * You should have received a copy of the GNU General Public License version
17 * 2 along with this work; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21 * or visit www.oracle.com if you need additional information or have any
22 * questions.
23 *
24 */
25 #ifndef SHARE_GC_SHARED_TASKTERMINATOR_HPP
26 #define SHARE_GC_SHARED_TASKTERMINATOR_HPP
27
28 #include "memory/allocation.hpp"
29 #include "runtime/mutex.hpp"
30 #include "runtime/thread.hpp"
31
32 // Define this to enable additional tracing probes.
33 #undef TRACESPINNING
34
35 class TaskQueueSetSuper;
36 class TerminatorTerminator;
37
38 /*
39 * Provides a task termination protocol.
40 *
41 * This is an enhanced implementation of Google's OWST work stealing task termination
42 * protocol (OWST stands for Optimized Work Stealing Threads).
43 *
44 * It is described in the paper:
45 * "Wessam Hassanein. 2016. Understanding and improving JVM GC work
46 * stealing at the data center scale. In Proceedings of the 2016 ACM
47 * SIGPLAN International Symposium on Memory Management (ISMM 2016). ACM,
48 * New York, NY, USA, 46-54. DOI: https://doi.org/10.1145/2926697.2926706"
49 *
50 * Instead of a dedicated spin-master, our implementation will let spin-master relinquish
51 * the role before it goes to sleep/wait, allowing newly arrived threads to compete for the role.
52 * The intention of above enhancement is to reduce spin-master's latency on detecting new tasks
53 * for stealing and termination condition.
54 */
55 class TaskTerminator : public CHeapObj<mtGC> {
56 uint _n_threads;
57 TaskQueueSetSuper* _queue_set;
58
59 DEFINE_PAD_MINUS_SIZE(0, DEFAULT_CACHE_LINE_SIZE, 0);
60 volatile uint _offered_termination;
61 DEFINE_PAD_MINUS_SIZE(1, DEFAULT_CACHE_LINE_SIZE, sizeof(volatile uint));
62
63 #ifdef ASSERT
64 bool peek_in_queue_set();
65 #endif
66 void yield();
67
68 Monitor* _blocker;
69 Thread* _spin_master;
70
71 #ifdef TRACESPINNING
72 static uint _total_yields;
73 static uint _total_spins;
74 static uint _total_peeks;
75 #endif
76
77 // If we should exit current termination protocol
78 bool exit_termination(size_t tasks, TerminatorTerminator* terminator);
79
80 size_t tasks_in_queue_set() const;
81
82 // Perform spin-master task.
83 // Return true if termination condition is detected, otherwise return false
84 bool do_spin_master_work(TerminatorTerminator* terminator);
85
86 NONCOPYABLE(TaskTerminator);
87
88 public:
89 TaskTerminator(uint n_threads, TaskQueueSetSuper* queue_set);
90 ~TaskTerminator();
91
92 // The current thread has no work, and is ready to terminate if everyone
93 // else is. If returns "true", all threads are terminated. If returns
94 // "false", available work has been observed in one of the task queues,
95 // so the global task is not complete.
96 bool offer_termination() {
97 return offer_termination(NULL);
98 }
99
100 // As above, but it also terminates if the should_exit_termination()
101 // method of the terminator parameter returns true. If terminator is
102 // NULL, then it is ignored.
103 bool offer_termination(TerminatorTerminator* terminator);
104
105 // Reset the terminator, so that it may be reused again.
106 // The caller is responsible for ensuring that this is done
107 // in an MT-safe manner, once the previous round of use of
108 // the terminator is finished.
109 void reset_for_reuse();
110 // Same as above but the number of parallel threads is set to the
111 // given number.
112 void reset_for_reuse(uint n_threads);
113
114 #ifdef TRACESPINNING
115 static uint total_yields() { return _total_yields; }
116 static uint total_spins() { return _total_spins; }
117 static uint total_peeks() { return _total_peeks; }
118 static void print_termination_counts();
119 #endif
120 };
121
122 #endif // SHARE_GC_SHARED_TASKTERMINATOR_HPP
|
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * version 2 for more details (a copy is included in the LICENSE file that
14 * accompanied this code).
15 *
16 * You should have received a copy of the GNU General Public License version
17 * 2 along with this work; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21 * or visit www.oracle.com if you need additional information or have any
22 * questions.
23 *
24 */
25 #ifndef SHARE_GC_SHARED_TASKTERMINATOR_HPP
26 #define SHARE_GC_SHARED_TASKTERMINATOR_HPP
27
28 #include "memory/allocation.hpp"
29 #include "runtime/mutex.hpp"
30 #include "runtime/thread.hpp"
31
32 class TaskQueueSetSuper;
33 class TerminatorTerminator;
34
35 /*
36 * Provides a task termination protocol.
37 *
38 * This is an enhanced implementation of Google's OWST work stealing task termination
39 * protocol (OWST stands for Optimized Work Stealing Threads).
40 *
41 * It is described in the paper:
42 * "Wessam Hassanein. 2016. Understanding and improving JVM GC work
43 * stealing at the data center scale. In Proceedings of the 2016 ACM
44 * SIGPLAN International Symposium on Memory Management (ISMM 2016). ACM,
45 * New York, NY, USA, 46-54. DOI: https://doi.org/10.1145/2926697.2926706"
46 *
47 * Instead of a dedicated spin-master, our implementation will let spin-master relinquish
48 * the role before it goes to sleep/wait, allowing newly arrived threads to compete for the role.
49 * The intention of above enhancement is to reduce spin-master's latency on detecting new tasks
50 * for stealing and termination condition.
51 */
52 class TaskTerminator : public CHeapObj<mtGC> {
53 uint _n_threads;
54 TaskQueueSetSuper* _queue_set;
55
56 DEFINE_PAD_MINUS_SIZE(0, DEFAULT_CACHE_LINE_SIZE, 0);
57 volatile uint _offered_termination;
58 DEFINE_PAD_MINUS_SIZE(1, DEFAULT_CACHE_LINE_SIZE, sizeof(volatile uint));
59
60 #ifdef ASSERT
61 bool peek_in_queue_set();
62 #endif
63 void yield();
64
65 Monitor* _blocker;
66 Thread* _spin_master;
67
68 // If we should exit current termination protocol
69 bool exit_termination(size_t tasks, TerminatorTerminator* terminator);
70
71 size_t tasks_in_queue_set() const;
72
73 // Perform spin-master task.
74 // Return true if termination condition is detected, otherwise return false
75 bool do_spin_master_work(TerminatorTerminator* terminator);
76
77 NONCOPYABLE(TaskTerminator);
78
79 public:
80 TaskTerminator(uint n_threads, TaskQueueSetSuper* queue_set);
81 ~TaskTerminator();
82
83 // The current thread has no work, and is ready to terminate if everyone
84 // else is. If returns "true", all threads are terminated. If returns
85 // "false", available work has been observed in one of the task queues,
86 // so the global task is not complete.
87 bool offer_termination() {
88 return offer_termination(NULL);
89 }
90
91 // As above, but it also terminates if the should_exit_termination()
92 // method of the terminator parameter returns true. If terminator is
93 // NULL, then it is ignored.
94 bool offer_termination(TerminatorTerminator* terminator);
95
96 // Reset the terminator, so that it may be reused again.
97 // The caller is responsible for ensuring that this is done
98 // in an MT-safe manner, once the previous round of use of
99 // the terminator is finished.
100 void reset_for_reuse();
101 // Same as above but the number of parallel threads is set to the
102 // given number.
103 void reset_for_reuse(uint n_threads);
104 };
105
106 #endif // SHARE_GC_SHARED_TASKTERMINATOR_HPP
|