45 // The Mutex/Condition variable and ObjectLocker classes calls the enter/ |
45 // The Mutex/Condition variable and ObjectLocker classes calls the enter/ |
46 // exit safepoint methods, when a thread is blocked/restarted. Hence, all mutex exter/ |
46 // exit safepoint methods, when a thread is blocked/restarted. Hence, all mutex exter/ |
47 // exit points *must* be at a safepoint. |
47 // exit points *must* be at a safepoint. |
48 |
48 |
49 class ThreadSafepointState; |
49 class ThreadSafepointState; |
|
50 |
|
51 class SafepointStateTracker { |
|
52 uint64_t _safepoint_id; |
|
53 bool _at_safepoint; |
|
54 public: |
|
55 SafepointStateTracker(uint64_t safepoint_id, bool at_safepoint); |
|
56 bool safepoint_state_changed(); |
|
57 }; |
50 |
58 |
51 // |
59 // |
52 // Implements roll-forward to safepoint (safepoint synchronization) |
60 // Implements roll-forward to safepoint (safepoint synchronization) |
53 // |
61 // |
54 class SafepointSynchronize : AllStatic { |
62 class SafepointSynchronize : AllStatic { |
67 SAFEPOINT_CLEANUP_COMPILATION_POLICY, |
75 SAFEPOINT_CLEANUP_COMPILATION_POLICY, |
68 SAFEPOINT_CLEANUP_SYMBOL_TABLE_REHASH, |
76 SAFEPOINT_CLEANUP_SYMBOL_TABLE_REHASH, |
69 SAFEPOINT_CLEANUP_STRING_TABLE_REHASH, |
77 SAFEPOINT_CLEANUP_STRING_TABLE_REHASH, |
70 SAFEPOINT_CLEANUP_CLD_PURGE, |
78 SAFEPOINT_CLEANUP_CLD_PURGE, |
71 SAFEPOINT_CLEANUP_SYSTEM_DICTIONARY_RESIZE, |
79 SAFEPOINT_CLEANUP_SYSTEM_DICTIONARY_RESIZE, |
|
80 SAFEPOINT_CLEANUP_REQUEST_OOPSTORAGE_CLEANUP, |
72 // Leave this one last. |
81 // Leave this one last. |
73 SAFEPOINT_CLEANUP_NUM_TASKS |
82 SAFEPOINT_CLEANUP_NUM_TASKS |
74 }; |
83 }; |
75 |
84 |
76 private: |
85 private: |
77 friend class SafepointMechanism; |
86 friend class SafepointMechanism; |
78 friend class ThreadSafepointState; |
87 friend class ThreadSafepointState; |
79 friend class HandshakeState; |
88 friend class HandshakeState; |
|
89 friend class SafepointStateTracker; |
80 |
90 |
81 // Threads might read this flag directly, without acquiring the Threads_lock: |
91 // Threads might read this flag directly, without acquiring the Threads_lock: |
82 static volatile SynchronizeState _state; |
92 static volatile SynchronizeState _state; |
83 // Number of threads we are waiting for to block: |
93 // Number of threads we are waiting for to block: |
84 static int _waiting_to_block; |
94 static int _waiting_to_block; |
89 // An even value means there are no ongoing safepoint operations. |
99 // An even value means there are no ongoing safepoint operations. |
90 // The counter is incremented ONLY at the beginning and end of each |
100 // The counter is incremented ONLY at the beginning and end of each |
91 // safepoint. |
101 // safepoint. |
92 static volatile uint64_t _safepoint_counter; |
102 static volatile uint64_t _safepoint_counter; |
93 |
103 |
|
104 // A change in this counter or a change in the result of |
|
105 // is_at_safepoint() are used by SafepointStateTracker:: |
|
106 // safepoint_state_changed() to determine its answer. |
|
107 static uint64_t _safepoint_id; |
|
108 |
94 // JavaThreads that need to block for the safepoint will stop on the |
109 // JavaThreads that need to block for the safepoint will stop on the |
95 // _wait_barrier, where they can quickly be started again. |
110 // _wait_barrier, where they can quickly be started again. |
96 static WaitBarrier* _wait_barrier; |
111 static WaitBarrier* _wait_barrier; |
97 static long _end_of_last_safepoint; // Time of last safepoint in milliseconds |
112 static long _end_of_last_safepoint; // Time of last safepoint in milliseconds |
98 static julong _coalesced_vmop_count; // coalesced vmop count |
113 static julong _coalesced_vmop_count; // coalesced vmop count |
99 |
|
100 // Statistics |
|
101 static void begin_statistics(int nof_threads, int nof_running); |
|
102 static void update_statistics_on_spin_end(); |
|
103 static void update_statistics_on_sync_end(jlong end_time); |
|
104 static void update_statistics_on_cleanup_end(jlong end_time); |
|
105 static void end_statistics(jlong end_time); |
|
106 static void print_statistics(); |
|
107 |
114 |
108 // For debug long safepoint |
115 // For debug long safepoint |
109 static void print_safepoint_timeout(); |
116 static void print_safepoint_timeout(); |
110 |
117 |
111 // Helper methods for safepoint procedure: |
118 // Helper methods for safepoint procedure: |
112 static void arm_safepoint(); |
119 static void arm_safepoint(); |
113 static int synchronize_threads(jlong safepoint_limit_time, int nof_threads, int* initial_running); |
120 static int synchronize_threads(jlong safepoint_limit_time, int nof_threads, int* initial_running); |
114 static void disarm_safepoint(); |
121 static void disarm_safepoint(); |
115 static void increment_jni_active_count(); |
122 static void increment_jni_active_count(); |
116 static void decrement_waiting_to_block(); |
123 static void decrement_waiting_to_block(); |
|
124 static bool thread_not_running(ThreadSafepointState *cur_state); |
117 |
125 |
118 // Used in safepoint_safe to do a stable load of the thread state. |
126 // Used in safepoint_safe to do a stable load of the thread state. |
119 static bool try_stable_load_state(JavaThreadState *state, |
127 static bool try_stable_load_state(JavaThreadState *state, |
120 JavaThread *thread, |
128 JavaThread *thread, |
121 uint64_t safepoint_count); |
129 uint64_t safepoint_count); |
125 |
133 |
126 // Called from VMThread during handshakes. |
134 // Called from VMThread during handshakes. |
127 // If true the VMThread may safely process the handshake operation for the JavaThread. |
135 // If true the VMThread may safely process the handshake operation for the JavaThread. |
128 static bool handshake_safe(JavaThread *thread); |
136 static bool handshake_safe(JavaThread *thread); |
129 |
137 |
|
138 static uint64_t safepoint_counter() { return _safepoint_counter; } |
|
139 |
130 public: |
140 public: |
131 |
141 |
132 static void init(Thread* vmthread); |
142 static void init(Thread* vmthread); |
133 |
143 |
134 // Roll all threads forward to safepoint. Must be called by the VMThread. |
144 // Roll all threads forward to safepoint. Must be called by the VMThread. |
139 static const uint64_t InactiveSafepointCounter; |
149 static const uint64_t InactiveSafepointCounter; |
140 |
150 |
141 // Query |
151 // Query |
142 static bool is_at_safepoint() { return _state == _synchronized; } |
152 static bool is_at_safepoint() { return _state == _synchronized; } |
143 static bool is_synchronizing() { return _state == _synchronizing; } |
153 static bool is_synchronizing() { return _state == _synchronizing; } |
144 static uint64_t safepoint_counter() { return _safepoint_counter; } |
154 |
145 static bool is_same_safepoint(uint64_t counter) { return (SafepointSynchronize::safepoint_counter() - counter) < 2; } |
155 static uint64_t safepoint_id() { |
|
156 return _safepoint_id; |
|
157 } |
|
158 |
|
159 static SafepointStateTracker safepoint_state_tracker() { |
|
160 return SafepointStateTracker(safepoint_id(), is_at_safepoint()); |
|
161 } |
|
162 |
146 // Exception handling for page polling |
163 // Exception handling for page polling |
147 static void handle_polling_page_exception(JavaThread *thread); |
164 static void handle_polling_page_exception(JavaThread *thread); |
148 |
165 |
149 static bool is_cleanup_needed(); |
166 static bool is_cleanup_needed(); |
150 static void do_cleanup_tasks(); |
167 static void do_cleanup_tasks(); |
188 // At polling page safepoint (NOT a poll return safepoint): |
205 // At polling page safepoint (NOT a poll return safepoint): |
189 volatile bool _at_poll_safepoint; |
206 volatile bool _at_poll_safepoint; |
190 JavaThread* _thread; |
207 JavaThread* _thread; |
191 bool _safepoint_safe; |
208 bool _safepoint_safe; |
192 volatile uint64_t _safepoint_id; |
209 volatile uint64_t _safepoint_id; |
193 JavaThreadState _orig_thread_state; |
|
194 |
210 |
195 ThreadSafepointState* _next; |
211 ThreadSafepointState* _next; |
196 |
212 |
197 void account_safe_thread(); |
213 void account_safe_thread(); |
198 |
214 |
214 |
230 |
215 uint64_t get_safepoint_id() const; |
231 uint64_t get_safepoint_id() const; |
216 void reset_safepoint_id(); |
232 void reset_safepoint_id(); |
217 void set_safepoint_id(uint64_t sid); |
233 void set_safepoint_id(uint64_t sid); |
218 |
234 |
219 JavaThreadState orig_thread_state() const { return _orig_thread_state; } |
|
220 |
|
221 // Support for safepoint timeout (debugging) |
235 // Support for safepoint timeout (debugging) |
222 bool is_at_poll_safepoint() { return _at_poll_safepoint; } |
236 bool is_at_poll_safepoint() { return _at_poll_safepoint; } |
223 void set_at_poll_safepoint(bool val) { _at_poll_safepoint = val; } |
237 void set_at_poll_safepoint(bool val) { _at_poll_safepoint = val; } |
224 |
238 |
225 void handle_polling_page_exception(); |
239 void handle_polling_page_exception(); |
226 |
240 |
227 // debugging |
241 // debugging |
228 void print_on(outputStream* st) const; |
242 void print_on(outputStream* st) const; |
229 void print() const; |
|
230 |
243 |
231 // Initialize |
244 // Initialize |
232 static void create(JavaThread *thread); |
245 static void create(JavaThread *thread); |
233 static void destroy(JavaThread *thread); |
246 static void destroy(JavaThread *thread); |
234 }; |
247 }; |