| 1 | /* | 
| 2 |  * Copyright (c) 1997, 2019, 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_RUNTIME_SAFEPOINT_HPP | 
| 26 | #define SHARE_RUNTIME_SAFEPOINT_HPP | 
| 27 |  | 
| 28 | #include "memory/allocation.hpp" | 
| 29 | #include "runtime/os.hpp" | 
| 30 | #include "runtime/thread.hpp" | 
| 31 | #include "runtime/vmOperations.hpp" | 
| 32 | #include "utilities/ostream.hpp" | 
| 33 | #include "utilities/waitBarrier.hpp" | 
| 34 |  | 
| 35 | // | 
| 36 | // Safepoint synchronization | 
| 37 | //// | 
| 38 | // The VMThread uses the SafepointSynchronize::begin/end | 
| 39 | // methods to enter/exit a safepoint region. The begin method will roll | 
| 40 | // all JavaThreads forward to a safepoint. | 
| 41 | // | 
| 42 | // JavaThreads must use the ThreadSafepointState abstraction (defined in | 
| 43 | // thread.hpp) to indicate that that they are at a safepoint. | 
| 44 | // | 
| 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/ | 
| 47 | // exit points *must* be at a safepoint. | 
| 48 |  | 
| 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 | }; | 
| 58 |  | 
| 59 | // | 
| 60 | // Implements roll-forward to safepoint (safepoint synchronization) | 
| 61 | // | 
| 62 | class SafepointSynchronize : AllStatic { | 
| 63 |  public: | 
| 64 |   enum SynchronizeState { | 
| 65 |       _not_synchronized = 0,                   // Threads not synchronized at a safepoint. Keep this value 0. | 
| 66 |       _synchronizing    = 1,                   // Synchronizing in progress | 
| 67 |       _synchronized     = 2                    // All Java threads are running in native, blocked in OS or stopped at safepoint. | 
| 68 |                                                // VM thread and any NonJavaThread may be running. | 
| 69 |   }; | 
| 70 |  | 
| 71 |   // The enums are listed in the order of the tasks when done serially. | 
| 72 |   enum SafepointCleanupTasks { | 
| 73 |     SAFEPOINT_CLEANUP_DEFLATE_MONITORS, | 
| 74 |     SAFEPOINT_CLEANUP_UPDATE_INLINE_CACHES, | 
| 75 |     SAFEPOINT_CLEANUP_COMPILATION_POLICY, | 
| 76 |     SAFEPOINT_CLEANUP_SYMBOL_TABLE_REHASH, | 
| 77 |     SAFEPOINT_CLEANUP_STRING_TABLE_REHASH, | 
| 78 |     SAFEPOINT_CLEANUP_CLD_PURGE, | 
| 79 |     SAFEPOINT_CLEANUP_SYSTEM_DICTIONARY_RESIZE, | 
| 80 |     SAFEPOINT_CLEANUP_REQUEST_OOPSTORAGE_CLEANUP, | 
| 81 |     // Leave this one last. | 
| 82 |     SAFEPOINT_CLEANUP_NUM_TASKS | 
| 83 |   }; | 
| 84 |  | 
| 85 |  private: | 
| 86 |   friend class SafepointMechanism; | 
| 87 |   friend class ThreadSafepointState; | 
| 88 |   friend class HandshakeState; | 
| 89 |   friend class SafepointStateTracker; | 
| 90 |  | 
| 91 |   // Threads might read this flag directly, without acquiring the Threads_lock: | 
| 92 |   static volatile SynchronizeState _state; | 
| 93 |   // Number of threads we are waiting for to block: | 
| 94 |   static int              _waiting_to_block; | 
| 95 |   // Counts the number of active critical natives during the safepoint: | 
| 96 |   static int              _current_jni_active_count; | 
| 97 |  | 
| 98 |   // This counter is used for fast versions of jni_Get<Primitive>Field. | 
| 99 |   // An even value means there are no ongoing safepoint operations. | 
| 100 |   // The counter is incremented ONLY at the beginning and end of each | 
| 101 |   // safepoint. | 
| 102 |   static volatile uint64_t _safepoint_counter; | 
| 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 |  | 
| 109 |   // JavaThreads that need to block for the safepoint will stop on the | 
| 110 |   // _wait_barrier, where they can quickly be started again. | 
| 111 |   static WaitBarrier* _wait_barrier; | 
| 112 |   static long         _end_of_last_safepoint;     // Time of last safepoint in milliseconds | 
| 113 |   static julong       _coalesced_vmop_count;     // coalesced vmop count | 
| 114 |  | 
| 115 |   // Statistics | 
| 116 |   static void begin_statistics(int nof_threads, int nof_running); | 
| 117 |   static void update_statistics_on_spin_end(); | 
| 118 |   static void update_statistics_on_sync_end(jlong end_time); | 
| 119 |   static void update_statistics_on_cleanup_end(jlong end_time); | 
| 120 |   static void end_statistics(jlong end_time); | 
| 121 |   static void print_statistics(); | 
| 122 |  | 
| 123 |   // For debug long safepoint | 
| 124 |   static void print_safepoint_timeout(); | 
| 125 |  | 
| 126 |   // Helper methods for safepoint procedure: | 
| 127 |   static void arm_safepoint(); | 
| 128 |   static int synchronize_threads(jlong safepoint_limit_time, int nof_threads, int* initial_running); | 
| 129 |   static void disarm_safepoint(); | 
| 130 |   static void increment_jni_active_count(); | 
| 131 |   static void decrement_waiting_to_block(); | 
| 132 |   static bool thread_not_running(ThreadSafepointState *cur_state); | 
| 133 |  | 
| 134 |   // Used in safepoint_safe to do a stable load of the thread state. | 
| 135 |   static bool try_stable_load_state(JavaThreadState *state, | 
| 136 |                                     JavaThread *thread, | 
| 137 |                                     uint64_t safepoint_count); | 
| 138 |  | 
| 139 |   // Called when a thread voluntarily blocks | 
| 140 |   static void block(JavaThread *thread); | 
| 141 |  | 
| 142 |   // Called from VMThread during handshakes. | 
| 143 |   // If true the VMThread may safely process the handshake operation for the JavaThread. | 
| 144 |   static bool handshake_safe(JavaThread *thread); | 
| 145 |  | 
| 146 |   static uint64_t safepoint_counter()             { return _safepoint_counter; } | 
| 147 |  | 
| 148 | public: | 
| 149 |  | 
| 150 |   static void init(Thread* vmthread); | 
| 151 |  | 
| 152 |   // Roll all threads forward to safepoint. Must be called by the VMThread. | 
| 153 |   static void begin(); | 
| 154 |   static void end();                    // Start all suspended threads again... | 
| 155 |  | 
| 156 |   // The value for a not set safepoint id. | 
| 157 |   static const uint64_t InactiveSafepointCounter; | 
| 158 |  | 
| 159 |   // Query | 
| 160 |   static bool is_at_safepoint()                   { return _state == _synchronized; } | 
| 161 |   static bool is_synchronizing()                  { return _state == _synchronizing; } | 
| 162 |  | 
| 163 |   static uint64_t safepoint_id() { | 
| 164 |     return _safepoint_id; | 
| 165 |   } | 
| 166 |  | 
| 167 |   static SafepointStateTracker safepoint_state_tracker() { | 
| 168 |     return SafepointStateTracker(safepoint_id(), is_at_safepoint()); | 
| 169 |   } | 
| 170 |  | 
| 171 |   // Exception handling for page polling | 
| 172 |   static void handle_polling_page_exception(JavaThread *thread); | 
| 173 |  | 
| 174 |   static bool is_cleanup_needed(); | 
| 175 |   static void do_cleanup_tasks(); | 
| 176 |  | 
| 177 |   static void set_is_at_safepoint()             { _state = _synchronized; } | 
| 178 |   static void set_is_not_at_safepoint()         { _state = _not_synchronized; } | 
| 179 |  | 
| 180 |   // Assembly support | 
| 181 |   static address address_of_state()             { return (address)&_state; } | 
| 182 |  | 
| 183 |   // Only used for making sure that no safepoint has happened in | 
| 184 |   // JNI_FastGetField. Therefore only the low 32-bits are needed | 
| 185 |   // even if this is a 64-bit counter. | 
| 186 |   static address safepoint_counter_addr() { | 
| 187 | #ifdef VM_LITTLE_ENDIAN | 
| 188 |     return (address)&_safepoint_counter; | 
| 189 | #else /* BIG */ | 
| 190 |     // Return pointer to the 32 LSB: | 
| 191 |     return (address) (((uint32_t*)(&_safepoint_counter)) + 1); | 
| 192 | #endif | 
| 193 |   } | 
| 194 | }; | 
| 195 |  | 
| 196 | // Some helper assert macros for safepoint checks. | 
| 197 |  | 
| 198 | #define assert_at_safepoint()                                           \ | 
| 199 |   assert(SafepointSynchronize::is_at_safepoint(), "should be at a safepoint") | 
| 200 |  | 
| 201 | #define assert_at_safepoint_msg(...)                                    \ | 
| 202 |   assert(SafepointSynchronize::is_at_safepoint(), __VA_ARGS__) | 
| 203 |  | 
| 204 | #define assert_not_at_safepoint()                                       \ | 
| 205 |   assert(!SafepointSynchronize::is_at_safepoint(), "should not be at a safepoint") | 
| 206 |  | 
| 207 | #define assert_not_at_safepoint_msg(...)                                \ | 
| 208 |   assert(!SafepointSynchronize::is_at_safepoint(), __VA_ARGS__) | 
| 209 |  | 
| 210 | // State class for a thread suspended at a safepoint | 
| 211 | class ThreadSafepointState: public CHeapObj<mtThread> { | 
| 212 |  private: | 
| 213 |   // At polling page safepoint (NOT a poll return safepoint): | 
| 214 |   volatile bool                   _at_poll_safepoint; | 
| 215 |   JavaThread*                     _thread; | 
| 216 |   bool                            _safepoint_safe; | 
| 217 |   volatile uint64_t               _safepoint_id; | 
| 218 |   JavaThreadState                 _orig_thread_state; | 
| 219 |  | 
| 220 |   ThreadSafepointState*           _next; | 
| 221 |  | 
| 222 |   void account_safe_thread(); | 
| 223 |  | 
| 224 |  public: | 
| 225 |   ThreadSafepointState(JavaThread *thread); | 
| 226 |  | 
| 227 |   // Linked list support: | 
| 228 |   ThreadSafepointState* get_next() const { return _next; } | 
| 229 |   void set_next(ThreadSafepointState* value) { _next = value; } | 
| 230 |   ThreadSafepointState** next_ptr() { return &_next; } | 
| 231 |  | 
| 232 |   // examine/restart | 
| 233 |   void examine_state_of_thread(uint64_t safepoint_count); | 
| 234 |   void restart(); | 
| 235 |  | 
| 236 |   // Query | 
| 237 |   JavaThread*  thread() const         { return _thread; } | 
| 238 |   bool         is_running() const     { return !_safepoint_safe; } | 
| 239 |  | 
| 240 |   uint64_t get_safepoint_id() const; | 
| 241 |   void     reset_safepoint_id(); | 
| 242 |   void     set_safepoint_id(uint64_t sid); | 
| 243 |  | 
| 244 |   JavaThreadState orig_thread_state() const { return _orig_thread_state; } | 
| 245 |  | 
| 246 |   // Support for safepoint timeout (debugging) | 
| 247 |   bool is_at_poll_safepoint()           { return _at_poll_safepoint; } | 
| 248 |   void set_at_poll_safepoint(bool val)  { _at_poll_safepoint = val; } | 
| 249 |  | 
| 250 |   void handle_polling_page_exception(); | 
| 251 |  | 
| 252 |   // debugging | 
| 253 |   void print_on(outputStream* st) const; | 
| 254 |   void print() const; | 
| 255 |  | 
| 256 |   // Initialize | 
| 257 |   static void create(JavaThread *thread); | 
| 258 |   static void destroy(JavaThread *thread); | 
| 259 | }; | 
| 260 |  | 
| 261 | class SafepointTracing : public AllStatic { | 
| 262 | private: | 
| 263 |   // Absolute | 
| 264 |   static jlong _last_safepoint_begin_time_ns; | 
| 265 |   static jlong _last_safepoint_sync_time_ns; | 
| 266 |   static jlong _last_safepoint_cleanup_time_ns; | 
| 267 |   static jlong _last_safepoint_end_time_ns; | 
| 268 |   // amount of ms since epoch | 
| 269 |   static jlong _last_safepoint_end_time_epoch_ms; | 
| 270 |   // Relative | 
| 271 |   static jlong _last_app_time_ns; | 
| 272 |  | 
| 273 |   static int _nof_threads; | 
| 274 |   static int _nof_running; | 
| 275 |   static int _page_trap; | 
| 276 |  | 
| 277 |   static VM_Operation::VMOp_Type _current_type; | 
| 278 |   static jlong     _max_sync_time; | 
| 279 |   static jlong     _max_vmop_time; | 
| 280 |   static uint64_t  _op_count[VM_Operation::VMOp_Terminating]; | 
| 281 |  | 
| 282 |   static void statistics_log(); | 
| 283 |  | 
| 284 | public: | 
| 285 |   static void init(); | 
| 286 |  | 
| 287 |   static void begin(VM_Operation::VMOp_Type type); | 
| 288 |   static void synchronized(int nof_threads, int nof_running, int traps); | 
| 289 |   static void cleanup(); | 
| 290 |   static void end(); | 
| 291 |  | 
| 292 |   static void statistics_exit_log(); | 
| 293 |  | 
| 294 |   static jlong time_since_last_safepoint_ms() { | 
| 295 |     return (os::javaTimeNanos() - _last_safepoint_end_time_ns) / (NANOUNITS / MILLIUNITS); | 
| 296 |   } | 
| 297 |  | 
| 298 |   static jlong end_of_last_safepoint_epoch_ms() { | 
| 299 |     return _last_safepoint_end_time_epoch_ms; | 
| 300 |   } | 
| 301 |  | 
| 302 |   static jlong start_of_safepoint() { | 
| 303 |     return _last_safepoint_begin_time_ns; | 
| 304 |   } | 
| 305 | }; | 
| 306 |  | 
| 307 | #endif // SHARE_RUNTIME_SAFEPOINT_HPP | 
| 308 |  |