src/share/vm/runtime/safepoint.hpp

Tue, 11 May 2010 14:35:43 -0700

author
prr
date
Tue, 11 May 2010 14:35:43 -0700
changeset 1840
fb57d4cf76c2
parent 1726
4b0f2f4918ed
child 1907
c18cbe5936b8
permissions
-rw-r--r--

6931180: Migration to recent versions of MS Platform SDK
6951582: Build problems on win64
Summary: Changes to enable building JDK7 with Microsoft Visual Studio 2010
Reviewed-by: ohair, art, ccheung, dcubed

duke@435 1 /*
duke@435 2 * Copyright 1997-2007 Sun Microsystems, Inc. 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 *
duke@435 19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@435 20 * CA 95054 USA or visit www.sun.com if you need additional information or
duke@435 21 * have any questions.
duke@435 22 *
duke@435 23 */
duke@435 24
duke@435 25 //
duke@435 26 // Safepoint synchronization
duke@435 27 ////
duke@435 28 // The VMThread or CMS_thread uses the SafepointSynchronize::begin/end
duke@435 29 // methods to enter/exit a safepoint region. The begin method will roll
duke@435 30 // all JavaThreads forward to a safepoint.
duke@435 31 //
duke@435 32 // JavaThreads must use the ThreadSafepointState abstraction (defined in
duke@435 33 // thread.hpp) to indicate that that they are at a safepoint.
duke@435 34 //
duke@435 35 // The Mutex/Condition variable and ObjectLocker classes calls the enter/
duke@435 36 // exit safepoint methods, when a thread is blocked/restarted. Hence, all mutex exter/
duke@435 37 // exit points *must* be at a safepoint.
duke@435 38
duke@435 39
duke@435 40 class ThreadSafepointState;
duke@435 41 class SnippetCache;
duke@435 42 class nmethod;
duke@435 43
duke@435 44 //
duke@435 45 // Implements roll-forward to safepoint (safepoint synchronization)
duke@435 46 //
duke@435 47 class SafepointSynchronize : AllStatic {
duke@435 48 public:
duke@435 49 enum SynchronizeState {
duke@435 50 _not_synchronized = 0, // Threads not synchronized at a safepoint
duke@435 51 // Keep this value 0. See the coment in do_call_back()
duke@435 52 _synchronizing = 1, // Synchronizing in progress
duke@435 53 _synchronized = 2 // All Java threads are stopped at a safepoint. Only VM thread is running
duke@435 54 };
duke@435 55
duke@435 56 enum SafepointingThread {
duke@435 57 _null_thread = 0,
duke@435 58 _vm_thread = 1,
duke@435 59 _other_thread = 2
duke@435 60 };
duke@435 61
duke@435 62 enum SafepointTimeoutReason {
duke@435 63 _spinning_timeout = 0,
duke@435 64 _blocking_timeout = 1
duke@435 65 };
duke@435 66
duke@435 67 typedef struct {
xlu@1726 68 float _time_stamp; // record when the current safepoint occurs in seconds
duke@435 69 int _vmop_type; // type of VM operation triggers the safepoint
duke@435 70 int _nof_total_threads; // total number of Java threads
duke@435 71 int _nof_initial_running_threads; // total number of initially seen running threads
duke@435 72 int _nof_threads_wait_to_block; // total number of threads waiting for to block
duke@435 73 bool _page_armed; // true if polling page is armed, false otherwise
duke@435 74 int _nof_threads_hit_page_trap; // total number of threads hitting the page trap
duke@435 75 jlong _time_to_spin; // total time in millis spent in spinning
duke@435 76 jlong _time_to_wait_to_block; // total time in millis spent in waiting for to block
xlu@1726 77 jlong _time_to_do_cleanups; // total time in millis spent in performing cleanups
duke@435 78 jlong _time_to_sync; // total time in millis spent in getting to _synchronized
duke@435 79 jlong _time_to_exec_vmop; // total time in millis spent in vm operation itself
duke@435 80 } SafepointStats;
duke@435 81
duke@435 82 private:
duke@435 83 static volatile SynchronizeState _state; // Threads might read this flag directly, without acquireing the Threads_lock
xlu@1726 84 static volatile int _waiting_to_block; // number of threads we are waiting for to block
duke@435 85
duke@435 86 // This counter is used for fast versions of jni_Get<Primitive>Field.
duke@435 87 // An even value means there is no ongoing safepoint operations.
duke@435 88 // The counter is incremented ONLY at the beginning and end of each
duke@435 89 // safepoint. The fact that Threads_lock is held throughout each pair of
duke@435 90 // increments (at the beginning and end of each safepoint) guarantees
duke@435 91 // race freedom.
duke@435 92 public:
duke@435 93 static volatile int _safepoint_counter;
duke@435 94 private:
xlu@1726 95 static long _end_of_last_safepoint; // Time of last safepoint in milliseconds
duke@435 96
duke@435 97 // statistics
xlu@1726 98 static jlong _safepoint_begin_time; // time when safepoint begins
xlu@1726 99 static SafepointStats* _safepoint_stats; // array of SafepointStats struct
xlu@1726 100 static int _cur_stat_index; // current index to the above array
xlu@1726 101 static julong _safepoint_reasons[]; // safepoint count for each VM op
xlu@1726 102 static julong _coalesced_vmop_count; // coalesced vmop count
xlu@1726 103 static jlong _max_sync_time; // maximum sync time in nanos
xlu@1726 104 static jlong _max_vmop_time; // maximum vm operation time in nanos
xlu@1726 105 static float _ts_of_current_safepoint; // time stamp of current safepoint in seconds
duke@435 106
duke@435 107 static void begin_statistics(int nof_threads, int nof_running);
duke@435 108 static void update_statistics_on_spin_end();
duke@435 109 static void update_statistics_on_sync_end(jlong end_time);
xlu@1726 110 static void update_statistics_on_cleanup_end(jlong end_time);
duke@435 111 static void end_statistics(jlong end_time);
duke@435 112 static void print_statistics();
duke@435 113 inline static void inc_page_trap_count() {
duke@435 114 Atomic::inc(&_safepoint_stats[_cur_stat_index]._nof_threads_hit_page_trap);
duke@435 115 }
duke@435 116
duke@435 117 // For debug long safepoint
duke@435 118 static void print_safepoint_timeout(SafepointTimeoutReason timeout_reason);
duke@435 119
duke@435 120 public:
duke@435 121
duke@435 122 // Main entry points
duke@435 123
duke@435 124 // Roll all threads forward to safepoint. Must be called by the
duke@435 125 // VMThread or CMS_thread.
duke@435 126 static void begin();
duke@435 127 static void end(); // Start all suspended threads again...
duke@435 128
duke@435 129 static bool safepoint_safe(JavaThread *thread, JavaThreadState state);
duke@435 130
duke@435 131 // Query
duke@435 132 inline static bool is_at_safepoint() { return _state == _synchronized; }
duke@435 133 inline static bool is_synchronizing() { return _state == _synchronizing; }
duke@435 134
duke@435 135 inline static bool do_call_back() {
duke@435 136 return (_state != _not_synchronized);
duke@435 137 }
duke@435 138
duke@435 139 // Called when a thread volantary blocks
duke@435 140 static void block(JavaThread *thread);
duke@435 141 static void signal_thread_at_safepoint() { _waiting_to_block--; }
duke@435 142
duke@435 143 // Exception handling for page polling
duke@435 144 static void handle_polling_page_exception(JavaThread *thread);
duke@435 145
duke@435 146 // VM Thread interface for determining safepoint rate
xlu@1726 147 static long last_non_safepoint_interval() {
xlu@1726 148 return os::javaTimeMillis() - _end_of_last_safepoint;
xlu@1726 149 }
duke@435 150 static bool is_cleanup_needed();
duke@435 151 static void do_cleanup_tasks();
duke@435 152
duke@435 153 // debugging
duke@435 154 static void print_state() PRODUCT_RETURN;
duke@435 155 static void safepoint_msg(const char* format, ...) PRODUCT_RETURN;
duke@435 156
duke@435 157 static void deferred_initialize_stat();
duke@435 158 static void print_stat_on_exit();
duke@435 159 inline static void inc_vmop_coalesced_count() { _coalesced_vmop_count++; }
duke@435 160
duke@435 161 static void set_is_at_safepoint() { _state = _synchronized; }
duke@435 162 static void set_is_not_at_safepoint() { _state = _not_synchronized; }
duke@435 163
duke@435 164 // assembly support
duke@435 165 static address address_of_state() { return (address)&_state; }
duke@435 166
duke@435 167 static address safepoint_counter_addr() { return (address)&_safepoint_counter; }
duke@435 168 };
duke@435 169
duke@435 170 // State class for a thread suspended at a safepoint
duke@435 171 class ThreadSafepointState: public CHeapObj {
duke@435 172 public:
duke@435 173 // These states are maintained by VM thread while threads are being brought
duke@435 174 // to a safepoint. After SafepointSynchronize::end(), they are reset to
duke@435 175 // _running.
duke@435 176 enum suspend_type {
duke@435 177 _running = 0, // Thread state not yet determined (i.e., not at a safepoint yet)
duke@435 178 _at_safepoint = 1, // Thread at a safepoint (f.ex., when blocked on a lock)
duke@435 179 _call_back = 2 // Keep executing and wait for callback (if thread is in interpreted or vm)
duke@435 180 };
duke@435 181 private:
duke@435 182 volatile bool _at_poll_safepoint; // At polling page safepoint (NOT a poll return safepoint)
duke@435 183 // Thread has called back the safepoint code (for debugging)
duke@435 184 bool _has_called_back;
duke@435 185
duke@435 186 JavaThread * _thread;
duke@435 187 volatile suspend_type _type;
duke@435 188
duke@435 189
duke@435 190 public:
duke@435 191 ThreadSafepointState(JavaThread *thread);
duke@435 192
duke@435 193 // examine/roll-forward/restart
duke@435 194 void examine_state_of_thread();
duke@435 195 void roll_forward(suspend_type type);
duke@435 196 void restart();
duke@435 197
duke@435 198 // Query
duke@435 199 JavaThread* thread() const { return _thread; }
duke@435 200 suspend_type type() const { return _type; }
duke@435 201 bool is_running() const { return (_type==_running); }
duke@435 202
duke@435 203 // Support for safepoint timeout (debugging)
duke@435 204 bool has_called_back() const { return _has_called_back; }
duke@435 205 void set_has_called_back(bool val) { _has_called_back = val; }
duke@435 206 bool is_at_poll_safepoint() { return _at_poll_safepoint; }
duke@435 207 void set_at_poll_safepoint(bool val) { _at_poll_safepoint = val; }
duke@435 208
duke@435 209 void handle_polling_page_exception();
duke@435 210
duke@435 211 // debugging
duke@435 212 void print_on(outputStream* st) const;
duke@435 213 void print() const { print_on(tty); }
duke@435 214
duke@435 215 // Initialize
duke@435 216 static void create(JavaThread *thread);
duke@435 217 static void destroy(JavaThread *thread);
duke@435 218
duke@435 219 void safepoint_msg(const char* format, ...) {
duke@435 220 if (ShowSafepointMsgs) {
duke@435 221 va_list ap;
duke@435 222 va_start(ap, format);
duke@435 223 tty->vprint_cr(format, ap);
duke@435 224 va_end(ap);
duke@435 225 }
duke@435 226 }
duke@435 227 };
duke@435 228
duke@435 229 //
duke@435 230 // CounterDecay
duke@435 231 //
duke@435 232 // Interates through invocation counters and decrements them. This
duke@435 233 // is done at each safepoint.
duke@435 234 //
duke@435 235 class CounterDecay : public AllStatic {
duke@435 236 static jlong _last_timestamp;
duke@435 237 public:
duke@435 238 static void decay();
duke@435 239 static bool is_decay_needed() { return (os::javaTimeMillis() - _last_timestamp) > CounterDecayMinIntervalLength; }
duke@435 240 };

mercurial