24 |
24 |
25 #ifndef SHARE_RUNTIME_SAFEPOINT_HPP |
25 #ifndef SHARE_RUNTIME_SAFEPOINT_HPP |
26 #define SHARE_RUNTIME_SAFEPOINT_HPP |
26 #define SHARE_RUNTIME_SAFEPOINT_HPP |
27 |
27 |
28 #include "memory/allocation.hpp" |
28 #include "memory/allocation.hpp" |
29 #include "runtime/mutexLocker.hpp" |
|
30 #include "runtime/os.hpp" |
29 #include "runtime/os.hpp" |
31 #include "utilities/globalDefinitions.hpp" |
30 #include "runtime/thread.hpp" |
32 #include "utilities/ostream.hpp" |
31 #include "utilities/ostream.hpp" |
|
32 #include "utilities/waitBarrier.hpp" |
33 |
33 |
34 // |
34 // |
35 // Safepoint synchronization |
35 // Safepoint synchronization |
36 //// |
36 //// |
37 // The VMThread or CMS_thread uses the SafepointSynchronize::begin/end |
37 // The VMThread uses the SafepointSynchronize::begin/end |
38 // methods to enter/exit a safepoint region. The begin method will roll |
38 // methods to enter/exit a safepoint region. The begin method will roll |
39 // all JavaThreads forward to a safepoint. |
39 // all JavaThreads forward to a safepoint. |
40 // |
40 // |
41 // JavaThreads must use the ThreadSafepointState abstraction (defined in |
41 // JavaThreads must use the ThreadSafepointState abstraction (defined in |
42 // thread.hpp) to indicate that that they are at a safepoint. |
42 // thread.hpp) to indicate that that they are at a safepoint. |
43 // |
43 // |
44 // The Mutex/Condition variable and ObjectLocker classes calls the enter/ |
44 // The Mutex/Condition variable and ObjectLocker classes calls the enter/ |
45 // exit safepoint methods, when a thread is blocked/restarted. Hence, all mutex exter/ |
45 // exit safepoint methods, when a thread is blocked/restarted. Hence, all mutex exter/ |
46 // exit points *must* be at a safepoint. |
46 // exit points *must* be at a safepoint. |
47 |
47 |
48 |
|
49 class ThreadSafepointState; |
48 class ThreadSafepointState; |
50 class JavaThread; |
|
51 |
49 |
52 // |
50 // |
53 // Implements roll-forward to safepoint (safepoint synchronization) |
51 // Implements roll-forward to safepoint (safepoint synchronization) |
54 // |
52 // |
55 class SafepointSynchronize : AllStatic { |
53 class SafepointSynchronize : AllStatic { |
56 public: |
54 public: |
57 enum SynchronizeState { |
55 enum SynchronizeState { |
58 _not_synchronized = 0, // Threads not synchronized at a safepoint |
56 _not_synchronized = 0, // Threads not synchronized at a safepoint. Keep this value 0. |
59 // Keep this value 0. See the comment in do_call_back() |
|
60 _synchronizing = 1, // Synchronizing in progress |
57 _synchronizing = 1, // Synchronizing in progress |
61 _synchronized = 2 // All Java threads are stopped at a safepoint. Only VM thread is running |
58 _synchronized = 2 // All Java threads are running in native, blocked in OS or stopped at safepoint. |
62 }; |
59 // VM thread and any NonJavaThread may be running. |
63 |
|
64 enum SafepointingThread { |
|
65 _null_thread = 0, |
|
66 _vm_thread = 1, |
|
67 _other_thread = 2 |
|
68 }; |
|
69 |
|
70 enum SafepointTimeoutReason { |
|
71 _spinning_timeout = 0, |
|
72 _blocking_timeout = 1 |
|
73 }; |
60 }; |
74 |
61 |
75 // The enums are listed in the order of the tasks when done serially. |
62 // The enums are listed in the order of the tasks when done serially. |
76 enum SafepointCleanupTasks { |
63 enum SafepointCleanupTasks { |
77 SAFEPOINT_CLEANUP_DEFLATE_MONITORS, |
64 SAFEPOINT_CLEANUP_DEFLATE_MONITORS, |
84 // Leave this one last. |
71 // Leave this one last. |
85 SAFEPOINT_CLEANUP_NUM_TASKS |
72 SAFEPOINT_CLEANUP_NUM_TASKS |
86 }; |
73 }; |
87 |
74 |
88 private: |
75 private: |
89 static volatile SynchronizeState _state; // Threads might read this flag directly, without acquiring the Threads_lock |
76 friend class SafepointMechanism; |
90 static volatile int _waiting_to_block; // number of threads we are waiting for to block |
77 friend class ThreadSafepointState; |
91 static int _current_jni_active_count; // Counts the number of active critical natives during the safepoint |
78 friend class HandshakeState; |
92 static int _defer_thr_suspend_loop_count; // Iterations before blocking VM threads |
79 |
|
80 enum SafepointTimeoutReason { |
|
81 _spinning_timeout = 0, |
|
82 _blocking_timeout = 1 |
|
83 }; |
|
84 |
|
85 // Threads might read this flag directly, without acquiring the Threads_lock: |
|
86 static volatile SynchronizeState _state; |
|
87 // Number of threads we are waiting for to block: |
|
88 static int _waiting_to_block; |
|
89 // Counts the number of active critical natives during the safepoint: |
|
90 static int _current_jni_active_count; |
93 |
91 |
94 // This counter is used for fast versions of jni_Get<Primitive>Field. |
92 // This counter is used for fast versions of jni_Get<Primitive>Field. |
95 // An even value means there is no ongoing safepoint operations. |
93 // An even value means there are no ongoing safepoint operations. |
96 // The counter is incremented ONLY at the beginning and end of each |
94 // The counter is incremented ONLY at the beginning and end of each |
97 // safepoint. The fact that Threads_lock is held throughout each pair of |
95 // safepoint. |
98 // increments (at the beginning and end of each safepoint) guarantees |
|
99 // race freedom. |
|
100 static volatile uint64_t _safepoint_counter; |
96 static volatile uint64_t _safepoint_counter; |
101 |
97 |
102 private: |
98 // JavaThreads that need to block for the safepoint will stop on the |
103 static long _end_of_last_safepoint; // Time of last safepoint in milliseconds |
99 // _wait_barrier, where they can quickly be started again. |
104 static julong _coalesced_vmop_count; // coalesced vmop count |
100 static WaitBarrier* _wait_barrier; |
|
101 static long _end_of_last_safepoint; // Time of last safepoint in milliseconds |
|
102 static julong _coalesced_vmop_count; // coalesced vmop count |
105 |
103 |
106 // Statistics |
104 // Statistics |
107 static void begin_statistics(int nof_threads, int nof_running); |
105 static void begin_statistics(int nof_threads, int nof_running); |
108 static void update_statistics_on_spin_end(); |
106 static void update_statistics_on_spin_end(); |
109 static void update_statistics_on_sync_end(jlong end_time); |
107 static void update_statistics_on_sync_end(jlong end_time); |
112 static void print_statistics(); |
110 static void print_statistics(); |
113 |
111 |
114 // For debug long safepoint |
112 // For debug long safepoint |
115 static void print_safepoint_timeout(SafepointTimeoutReason timeout_reason); |
113 static void print_safepoint_timeout(SafepointTimeoutReason timeout_reason); |
116 |
114 |
|
115 // Helper methods for safepoint procedure: |
|
116 static void arm_safepoint(); |
|
117 static int synchronize_threads(jlong safepoint_limit_time, int nof_threads, int* initial_running); |
|
118 static void disarm_safepoint(); |
|
119 static void increment_jni_active_count(); |
|
120 static void decrement_waiting_to_block(); |
|
121 |
|
122 // Used in safepoint_safe to do a stable load of the thread state. |
|
123 static bool try_stable_load_state(JavaThreadState *state, |
|
124 JavaThread *thread, |
|
125 uint64_t safepoint_count); |
|
126 |
|
127 // Called when a thread voluntarily blocks |
|
128 static void block(JavaThread *thread); |
|
129 |
|
130 // Called from VMThread during handshakes. |
|
131 // If true the VMThread may safely process the handshake operation for the JavaThread. |
|
132 static bool handshake_safe(JavaThread *thread); |
|
133 |
117 public: |
134 public: |
118 |
135 |
119 // Main entry points |
136 static void init(Thread* vmthread); |
120 |
137 |
121 // Roll all threads forward to safepoint. Must be called by the |
138 // Roll all threads forward to safepoint. Must be called by the VMThread. |
122 // VMThread or CMS_thread. |
|
123 static void begin(); |
139 static void begin(); |
124 static void end(); // Start all suspended threads again... |
140 static void end(); // Start all suspended threads again... |
125 |
141 |
126 static bool safepoint_safe(JavaThread *thread, JavaThreadState state); |
142 // The value for a not set safepoint id. |
127 |
143 static const uint64_t InactiveSafepointCounter; |
128 static void check_for_lazy_critical_native(JavaThread *thread, JavaThreadState state); |
|
129 |
144 |
130 // Query |
145 // Query |
131 inline static bool is_at_safepoint() { return _state == _synchronized; } |
146 static bool is_at_safepoint() { return _state == _synchronized; } |
132 inline static bool is_synchronizing() { return _state == _synchronizing; } |
147 static bool is_synchronizing() { return _state == _synchronizing; } |
133 inline static uint64_t safepoint_counter() { return _safepoint_counter; } |
148 static uint64_t safepoint_counter() { return _safepoint_counter; } |
134 |
149 static bool is_same_safepoint(uint64_t counter) { return (SafepointSynchronize::safepoint_counter() - counter) < 2; } |
135 inline static void increment_jni_active_count() { |
|
136 assert_locked_or_safepoint(Safepoint_lock); |
|
137 _current_jni_active_count++; |
|
138 } |
|
139 |
|
140 private: |
|
141 inline static bool do_call_back() { |
|
142 return (_state != _not_synchronized); |
|
143 } |
|
144 |
|
145 // Called when a thread voluntarily blocks |
|
146 static void block(JavaThread *thread, bool block_in_safepoint_check = true); |
|
147 |
|
148 friend class SafepointMechanism; |
|
149 |
|
150 public: |
|
151 static void signal_thread_at_safepoint() { _waiting_to_block--; } |
|
152 |
|
153 // Exception handling for page polling |
150 // Exception handling for page polling |
154 static void handle_polling_page_exception(JavaThread *thread); |
151 static void handle_polling_page_exception(JavaThread *thread); |
155 |
152 |
156 // VM Thread interface for determining safepoint rate |
153 // VM Thread interface for determining safepoint rate |
157 static long last_non_safepoint_interval() { |
154 static long last_non_safepoint_interval() { |
162 } |
159 } |
163 static bool is_cleanup_needed(); |
160 static bool is_cleanup_needed(); |
164 static void do_cleanup_tasks(); |
161 static void do_cleanup_tasks(); |
165 |
162 |
166 static void print_stat_on_exit(); |
163 static void print_stat_on_exit(); |
167 inline static void inc_vmop_coalesced_count() { _coalesced_vmop_count++; } |
164 static void inc_vmop_coalesced_count() { _coalesced_vmop_count++; } |
168 |
165 |
169 static void set_is_at_safepoint() { _state = _synchronized; } |
166 static void set_is_at_safepoint() { _state = _synchronized; } |
170 static void set_is_not_at_safepoint() { _state = _not_synchronized; } |
167 static void set_is_not_at_safepoint() { _state = _not_synchronized; } |
171 |
168 |
172 // Assembly support |
169 // Assembly support |
173 static address address_of_state() { return (address)&_state; } |
170 static address address_of_state() { return (address)&_state; } |
174 |
171 |
175 // Only used for making sure that no safepoint has happened in |
172 // Only used for making sure that no safepoint has happened in |
176 // JNI_FastGetField. Therefore only the low 32-bits are needed |
173 // JNI_FastGetField. Therefore only the low 32-bits are needed |
177 // even if this is a 64-bit counter. |
174 // even if this is a 64-bit counter. |
178 static address safepoint_counter_addr() { |
175 static address safepoint_counter_addr() { |
199 #define assert_not_at_safepoint_msg(...) \ |
196 #define assert_not_at_safepoint_msg(...) \ |
200 assert(!SafepointSynchronize::is_at_safepoint(), __VA_ARGS__) |
197 assert(!SafepointSynchronize::is_at_safepoint(), __VA_ARGS__) |
201 |
198 |
202 // State class for a thread suspended at a safepoint |
199 // State class for a thread suspended at a safepoint |
203 class ThreadSafepointState: public CHeapObj<mtInternal> { |
200 class ThreadSafepointState: public CHeapObj<mtInternal> { |
204 public: |
|
205 // These states are maintained by VM thread while threads are being brought |
|
206 // to a safepoint. After SafepointSynchronize::end(), they are reset to |
|
207 // _running. |
|
208 enum suspend_type { |
|
209 _running = 0, // Thread state not yet determined (i.e., not at a safepoint yet) |
|
210 _at_safepoint = 1, // Thread at a safepoint (f.ex., when blocked on a lock) |
|
211 _call_back = 2 // Keep executing and wait for callback (if thread is in interpreted or vm) |
|
212 }; |
|
213 private: |
201 private: |
214 volatile bool _at_poll_safepoint; // At polling page safepoint (NOT a poll return safepoint) |
202 // At polling page safepoint (NOT a poll return safepoint): |
215 // Thread has called back the safepoint code (for debugging) |
203 volatile bool _at_poll_safepoint; |
216 bool _has_called_back; |
204 JavaThread* _thread; |
217 |
205 bool _safepoint_safe; |
218 JavaThread * _thread; |
206 volatile uint64_t _safepoint_id; |
219 volatile suspend_type _type; |
207 JavaThreadState _orig_thread_state; |
220 JavaThreadState _orig_thread_state; |
208 |
221 |
209 ThreadSafepointState* _next; |
|
210 |
|
211 void account_safe_thread(); |
222 |
212 |
223 public: |
213 public: |
224 ThreadSafepointState(JavaThread *thread); |
214 ThreadSafepointState(JavaThread *thread); |
225 |
215 |
226 // examine/roll-forward/restart |
216 // Linked list support: |
227 void examine_state_of_thread(); |
217 ThreadSafepointState* get_next() const { return _next; } |
228 void roll_forward(suspend_type type); |
218 void set_next(ThreadSafepointState* value) { _next = value; } |
|
219 ThreadSafepointState** next_ptr() { return &_next; } |
|
220 |
|
221 // examine/restart |
|
222 void examine_state_of_thread(uint64_t safepoint_count); |
229 void restart(); |
223 void restart(); |
230 |
224 |
231 // Query |
225 // Query |
232 JavaThread* thread() const { return _thread; } |
226 JavaThread* thread() const { return _thread; } |
233 suspend_type type() const { return _type; } |
227 bool is_running() const { return !_safepoint_safe; } |
234 bool is_running() const { return (_type==_running); } |
228 |
|
229 uint64_t get_safepoint_id() const; |
|
230 void reset_safepoint_id(); |
|
231 void set_safepoint_id(uint64_t sid); |
|
232 |
235 JavaThreadState orig_thread_state() const { return _orig_thread_state; } |
233 JavaThreadState orig_thread_state() const { return _orig_thread_state; } |
236 |
234 |
237 // Support for safepoint timeout (debugging) |
235 // Support for safepoint timeout (debugging) |
238 bool has_called_back() const { return _has_called_back; } |
236 bool is_at_poll_safepoint() { return _at_poll_safepoint; } |
239 void set_has_called_back(bool val) { _has_called_back = val; } |
237 void set_at_poll_safepoint(bool val) { _at_poll_safepoint = val; } |
240 bool is_at_poll_safepoint() { return _at_poll_safepoint; } |
|
241 void set_at_poll_safepoint(bool val) { _at_poll_safepoint = val; } |
|
242 |
238 |
243 void handle_polling_page_exception(); |
239 void handle_polling_page_exception(); |
244 |
240 |
245 // debugging |
241 // debugging |
246 void print_on(outputStream* st) const; |
242 void print_on(outputStream* st) const; |