src/share/vm/runtime/safepoint.hpp

Mon, 12 Aug 2019 18:30:40 +0300

author
apetushkov
date
Mon, 12 Aug 2019 18:30:40 +0300
changeset 9858
b985cbb00e68
parent 6680
78bbf4d43a14
child 9896
1b8c45b8216a
permissions
-rw-r--r--

8223147: JFR Backport
8199712: Flight Recorder
8203346: JFR: Inconsistent signature of jfr_add_string_constant
8195817: JFR.stop should require name of recording
8195818: JFR.start should increase autogenerated name by one
8195819: Remove recording=x from jcmd JFR.check output
8203921: JFR thread sampling is missing fixes from JDK-8194552
8203929: Limit amount of data for JFR.dump
8203664: JFR start failure after AppCDS archive created with JFR StartFlightRecording
8003209: JFR events for network utilization
8207392: [PPC64] Implement JFR profiling
8202835: jfr/event/os/TestSystemProcess.java fails on missing events
Summary: Backport JFR from JDK11. Initial integration
Reviewed-by: neugens

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

mercurial