rev 47413 : Introduce SafepointMechanism
1 /* 2 * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #ifndef SHARE_VM_RUNTIME_SAFEPOINT_HPP 26 #define SHARE_VM_RUNTIME_SAFEPOINT_HPP 27 28 #include "asm/assembler.hpp" 29 #include "code/nmethod.hpp" 30 #include "memory/allocation.hpp" 31 #include "runtime/extendedPC.hpp" 32 #include "runtime/mutexLocker.hpp" 33 #include "runtime/os.hpp" 34 #include "utilities/ostream.hpp" 35 36 // 37 // Safepoint synchronization 38 //// 39 // The VMThread or CMS_thread uses the SafepointSynchronize::begin/end 40 // methods to enter/exit a safepoint region. The begin method will roll 41 // all JavaThreads forward to a safepoint. 42 // 43 // JavaThreads must use the ThreadSafepointState abstraction (defined in 44 // thread.hpp) to indicate that that they are at a safepoint. 45 // 46 // The Mutex/Condition variable and ObjectLocker classes calls the enter/ 47 // exit safepoint methods, when a thread is blocked/restarted. Hence, all mutex exter/ 48 // exit points *must* be at a safepoint. 49 50 51 class ThreadSafepointState; 52 class SnippetCache; 53 class nmethod; 54 55 // 56 // Implements roll-forward to safepoint (safepoint synchronization) 57 // 58 class SafepointSynchronize : AllStatic { 59 public: 60 enum SynchronizeState { 61 _not_synchronized = 0, // Threads not synchronized at a safepoint 62 // Keep this value 0. See the comment in do_call_back() 63 _synchronizing = 1, // Synchronizing in progress 64 _synchronized = 2 // All Java threads are stopped at a safepoint. Only VM thread is running 65 }; 66 67 enum SafepointingThread { 68 _null_thread = 0, 69 _vm_thread = 1, 70 _other_thread = 2 71 }; 72 73 enum SafepointTimeoutReason { 74 _spinning_timeout = 0, 75 _blocking_timeout = 1 76 }; 77 78 // The enums are listed in the order of the tasks when done serially. 79 enum SafepointCleanupTasks { 80 SAFEPOINT_CLEANUP_DEFLATE_MONITORS, 81 SAFEPOINT_CLEANUP_UPDATE_INLINE_CACHES, 82 SAFEPOINT_CLEANUP_COMPILATION_POLICY, 83 SAFEPOINT_CLEANUP_SYMBOL_TABLE_REHASH, 84 SAFEPOINT_CLEANUP_STRING_TABLE_REHASH, 85 SAFEPOINT_CLEANUP_CLD_PURGE, 86 // Leave this one last. 87 SAFEPOINT_CLEANUP_NUM_TASKS 88 }; 89 90 typedef struct { 91 float _time_stamp; // record when the current safepoint occurs in seconds 92 int _vmop_type; // type of VM operation triggers the safepoint 93 int _nof_total_threads; // total number of Java threads 94 int _nof_initial_running_threads; // total number of initially seen running threads 95 int _nof_threads_wait_to_block; // total number of threads waiting for to block 96 bool _page_armed; // true if polling page is armed, false otherwise 97 int _nof_threads_hit_page_trap; // total number of threads hitting the page trap 98 jlong _time_to_spin; // total time in millis spent in spinning 99 jlong _time_to_wait_to_block; // total time in millis spent in waiting for to block 100 jlong _time_to_do_cleanups; // total time in millis spent in performing cleanups 101 jlong _time_to_sync; // total time in millis spent in getting to _synchronized 102 jlong _time_to_exec_vmop; // total time in millis spent in vm operation itself 103 } SafepointStats; 104 105 private: 106 static volatile SynchronizeState _state; // Threads might read this flag directly, without acquiring the Threads_lock 107 static volatile int _waiting_to_block; // number of threads we are waiting for to block 108 static int _current_jni_active_count; // Counts the number of active critical natives during the safepoint 109 110 // This counter is used for fast versions of jni_Get<Primitive>Field. 111 // An even value means there is no ongoing safepoint operations. 112 // The counter is incremented ONLY at the beginning and end of each 113 // safepoint. The fact that Threads_lock is held throughout each pair of 114 // increments (at the beginning and end of each safepoint) guarantees 115 // race freedom. 116 public: 117 static volatile int _safepoint_counter; 118 private: 119 static long _end_of_last_safepoint; // Time of last safepoint in milliseconds 120 121 // Statistics 122 static jlong _safepoint_begin_time; // time when safepoint begins 123 static SafepointStats* _safepoint_stats; // array of SafepointStats struct 124 static int _cur_stat_index; // current index to the above array 125 static julong _safepoint_reasons[]; // safepoint count for each VM op 126 static julong _coalesced_vmop_count; // coalesced vmop count 127 static jlong _max_sync_time; // maximum sync time in nanos 128 static jlong _max_vmop_time; // maximum vm operation time in nanos 129 static float _ts_of_current_safepoint; // time stamp of current safepoint in seconds 130 131 static void begin_statistics(int nof_threads, int nof_running); 132 static void update_statistics_on_spin_end(); 133 static void update_statistics_on_sync_end(jlong end_time); 134 static void update_statistics_on_cleanup_end(jlong end_time); 135 static void end_statistics(jlong end_time); 136 static void print_statistics(); 137 inline static void inc_page_trap_count() { 138 Atomic::inc(&_safepoint_stats[_cur_stat_index]._nof_threads_hit_page_trap); 139 } 140 141 // For debug long safepoint 142 static void print_safepoint_timeout(SafepointTimeoutReason timeout_reason); 143 144 public: 145 146 // Main entry points 147 148 // Roll all threads forward to safepoint. Must be called by the 149 // VMThread or CMS_thread. 150 static void begin(); 151 static void end(); // Start all suspended threads again... 152 153 static bool safepoint_safe(JavaThread *thread, JavaThreadState state); 154 155 static void check_for_lazy_critical_native(JavaThread *thread, JavaThreadState state); 156 157 // Query 158 inline static bool is_at_safepoint() { return _state == _synchronized; } 159 inline static bool is_synchronizing() { return _state == _synchronizing; } 160 inline static int safepoint_counter() { return _safepoint_counter; } 161 162 inline static void increment_jni_active_count() { 163 assert_locked_or_safepoint(Safepoint_lock); 164 _current_jni_active_count++; 165 } 166 167 private: 168 inline static bool do_call_back() { 169 return (_state != _not_synchronized); 170 } 171 172 // Called when a thread voluntarily blocks 173 static void block(JavaThread *thread); 174 175 friend class SafepointMechanism; 176 177 public: 178 static void signal_thread_at_safepoint() { _waiting_to_block--; } 179 180 // Exception handling for page polling 181 static void handle_polling_page_exception(JavaThread *thread); 182 183 // VM Thread interface for determining safepoint rate 184 static long last_non_safepoint_interval() { 185 return os::javaTimeMillis() - _end_of_last_safepoint; 186 } 187 static long end_of_last_safepoint() { 188 return _end_of_last_safepoint; 189 } 190 static bool is_cleanup_needed(); 191 static void do_cleanup_tasks(); 192 193 // Debugging 194 static void print_state() PRODUCT_RETURN; 195 static void safepoint_msg(const char* format, ...) ATTRIBUTE_PRINTF(1, 2) PRODUCT_RETURN; 196 197 static void deferred_initialize_stat(); 198 static void print_stat_on_exit(); 199 inline static void inc_vmop_coalesced_count() { _coalesced_vmop_count++; } 200 201 static void set_is_at_safepoint() { _state = _synchronized; } 202 static void set_is_not_at_safepoint() { _state = _not_synchronized; } 203 204 // Assembly support 205 static address address_of_state() { return (address)&_state; } 206 207 static address safepoint_counter_addr() { return (address)&_safepoint_counter; } 208 }; 209 210 // State class for a thread suspended at a safepoint 211 class ThreadSafepointState: public CHeapObj<mtInternal> { 212 public: 213 // These states are maintained by VM thread while threads are being brought 214 // to a safepoint. After SafepointSynchronize::end(), they are reset to 215 // _running. 216 enum suspend_type { 217 _running = 0, // Thread state not yet determined (i.e., not at a safepoint yet) 218 _at_safepoint = 1, // Thread at a safepoint (f.ex., when blocked on a lock) 219 _call_back = 2 // Keep executing and wait for callback (if thread is in interpreted or vm) 220 }; 221 private: 222 volatile bool _at_poll_safepoint; // At polling page safepoint (NOT a poll return safepoint) 223 // Thread has called back the safepoint code (for debugging) 224 bool _has_called_back; 225 226 JavaThread * _thread; 227 volatile suspend_type _type; 228 JavaThreadState _orig_thread_state; 229 230 231 public: 232 ThreadSafepointState(JavaThread *thread); 233 234 // examine/roll-forward/restart 235 void examine_state_of_thread(); 236 void roll_forward(suspend_type type); 237 void restart(); 238 239 // Query 240 JavaThread* thread() const { return _thread; } 241 suspend_type type() const { return _type; } 242 bool is_running() const { return (_type==_running); } 243 JavaThreadState orig_thread_state() const { return _orig_thread_state; } 244 245 // Support for safepoint timeout (debugging) 246 bool has_called_back() const { return _has_called_back; } 247 void set_has_called_back(bool val) { _has_called_back = val; } 248 bool is_at_poll_safepoint() { return _at_poll_safepoint; } 249 void set_at_poll_safepoint(bool val) { _at_poll_safepoint = val; } 250 251 void handle_polling_page_exception(); 252 253 // debugging 254 void print_on(outputStream* st) const; 255 void print() const { print_on(tty); } 256 257 // Initialize 258 static void create(JavaThread *thread); 259 static void destroy(JavaThread *thread); 260 261 void safepoint_msg(const char* format, ...) ATTRIBUTE_PRINTF(2, 3) { 262 if (ShowSafepointMsgs) { 263 va_list ap; 264 va_start(ap, format); 265 tty->vprint_cr(format, ap); 266 va_end(ap); 267 } 268 } 269 }; 270 271 272 273 #endif // SHARE_VM_RUNTIME_SAFEPOINT_HPP --- EOF ---