src/share/vm/runtime/safepoint.hpp

Wed, 09 Apr 2008 15:10:22 -0700

author
rasbold
date
Wed, 09 Apr 2008 15:10:22 -0700
changeset 544
9f4457a14b58
parent 435
a61af66fc99e
child 1726
4b0f2f4918ed
permissions
-rw-r--r--

Merge

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 {
duke@435 68 int _vmop_type; // type of VM operation triggers the safepoint
duke@435 69 int _nof_total_threads; // total number of Java threads
duke@435 70 int _nof_initial_running_threads; // total number of initially seen running threads
duke@435 71 int _nof_threads_wait_to_block; // total number of threads waiting for to block
duke@435 72 bool _page_armed; // true if polling page is armed, false otherwise
duke@435 73 int _nof_threads_hit_page_trap; // total number of threads hitting the page trap
duke@435 74 jlong _time_to_spin; // total time in millis spent in spinning
duke@435 75 jlong _time_to_wait_to_block; // total time in millis spent in waiting for to block
duke@435 76 jlong _time_to_sync; // total time in millis spent in getting to _synchronized
duke@435 77 jlong _time_to_exec_vmop; // total time in millis spent in vm operation itself
duke@435 78 jlong _time_elapsed_since_last_safepoint; // time elasped since last safepoint
duke@435 79 } SafepointStats;
duke@435 80
duke@435 81 private:
duke@435 82 static volatile SynchronizeState _state; // Threads might read this flag directly, without acquireing the Threads_lock
duke@435 83 static volatile int _waiting_to_block; // No. of threads we are waiting for to block.
duke@435 84
duke@435 85 // This counter is used for fast versions of jni_Get<Primitive>Field.
duke@435 86 // An even value means there is no ongoing safepoint operations.
duke@435 87 // The counter is incremented ONLY at the beginning and end of each
duke@435 88 // safepoint. The fact that Threads_lock is held throughout each pair of
duke@435 89 // increments (at the beginning and end of each safepoint) guarantees
duke@435 90 // race freedom.
duke@435 91 public:
duke@435 92 static volatile int _safepoint_counter;
duke@435 93 private:
duke@435 94
duke@435 95 static jlong _last_safepoint; // Time of last safepoint
duke@435 96
duke@435 97 // statistics
duke@435 98 static SafepointStats* _safepoint_stats; // array of SafepointStats struct
duke@435 99 static int _cur_stat_index; // current index to the above array
duke@435 100 static julong _safepoint_reasons[]; // safepoint count for each VM op
duke@435 101 static julong _coalesced_vmop_count;// coalesced vmop count
duke@435 102 static jlong _max_sync_time; // maximum sync time in nanos
duke@435 103
duke@435 104 static void begin_statistics(int nof_threads, int nof_running);
duke@435 105 static void update_statistics_on_spin_end();
duke@435 106 static void update_statistics_on_sync_end(jlong end_time);
duke@435 107 static void end_statistics(jlong end_time);
duke@435 108 static void print_statistics();
duke@435 109 inline static void inc_page_trap_count() {
duke@435 110 Atomic::inc(&_safepoint_stats[_cur_stat_index]._nof_threads_hit_page_trap);
duke@435 111 }
duke@435 112
duke@435 113 // For debug long safepoint
duke@435 114 static void print_safepoint_timeout(SafepointTimeoutReason timeout_reason);
duke@435 115
duke@435 116 public:
duke@435 117
duke@435 118 // Main entry points
duke@435 119
duke@435 120 // Roll all threads forward to safepoint. Must be called by the
duke@435 121 // VMThread or CMS_thread.
duke@435 122 static void begin();
duke@435 123 static void end(); // Start all suspended threads again...
duke@435 124
duke@435 125 static bool safepoint_safe(JavaThread *thread, JavaThreadState state);
duke@435 126
duke@435 127 // Query
duke@435 128 inline static bool is_at_safepoint() { return _state == _synchronized; }
duke@435 129 inline static bool is_synchronizing() { return _state == _synchronizing; }
duke@435 130
duke@435 131 inline static bool do_call_back() {
duke@435 132 return (_state != _not_synchronized);
duke@435 133 }
duke@435 134
duke@435 135 // Called when a thread volantary blocks
duke@435 136 static void block(JavaThread *thread);
duke@435 137 static void signal_thread_at_safepoint() { _waiting_to_block--; }
duke@435 138
duke@435 139 // Exception handling for page polling
duke@435 140 static void handle_polling_page_exception(JavaThread *thread);
duke@435 141
duke@435 142 // VM Thread interface for determining safepoint rate
duke@435 143 static long last_non_safepoint_interval() { return os::javaTimeMillis() - _last_safepoint; }
duke@435 144 static bool is_cleanup_needed();
duke@435 145 static void do_cleanup_tasks();
duke@435 146
duke@435 147 // debugging
duke@435 148 static void print_state() PRODUCT_RETURN;
duke@435 149 static void safepoint_msg(const char* format, ...) PRODUCT_RETURN;
duke@435 150
duke@435 151 static void deferred_initialize_stat();
duke@435 152 static void print_stat_on_exit();
duke@435 153 inline static void inc_vmop_coalesced_count() { _coalesced_vmop_count++; }
duke@435 154
duke@435 155 static void set_is_at_safepoint() { _state = _synchronized; }
duke@435 156 static void set_is_not_at_safepoint() { _state = _not_synchronized; }
duke@435 157
duke@435 158 // assembly support
duke@435 159 static address address_of_state() { return (address)&_state; }
duke@435 160
duke@435 161 static address safepoint_counter_addr() { return (address)&_safepoint_counter; }
duke@435 162 };
duke@435 163
duke@435 164 // State class for a thread suspended at a safepoint
duke@435 165 class ThreadSafepointState: public CHeapObj {
duke@435 166 public:
duke@435 167 // These states are maintained by VM thread while threads are being brought
duke@435 168 // to a safepoint. After SafepointSynchronize::end(), they are reset to
duke@435 169 // _running.
duke@435 170 enum suspend_type {
duke@435 171 _running = 0, // Thread state not yet determined (i.e., not at a safepoint yet)
duke@435 172 _at_safepoint = 1, // Thread at a safepoint (f.ex., when blocked on a lock)
duke@435 173 _call_back = 2 // Keep executing and wait for callback (if thread is in interpreted or vm)
duke@435 174 };
duke@435 175 private:
duke@435 176 volatile bool _at_poll_safepoint; // At polling page safepoint (NOT a poll return safepoint)
duke@435 177 // Thread has called back the safepoint code (for debugging)
duke@435 178 bool _has_called_back;
duke@435 179
duke@435 180 JavaThread * _thread;
duke@435 181 volatile suspend_type _type;
duke@435 182
duke@435 183
duke@435 184 public:
duke@435 185 ThreadSafepointState(JavaThread *thread);
duke@435 186
duke@435 187 // examine/roll-forward/restart
duke@435 188 void examine_state_of_thread();
duke@435 189 void roll_forward(suspend_type type);
duke@435 190 void restart();
duke@435 191
duke@435 192 // Query
duke@435 193 JavaThread* thread() const { return _thread; }
duke@435 194 suspend_type type() const { return _type; }
duke@435 195 bool is_running() const { return (_type==_running); }
duke@435 196
duke@435 197 // Support for safepoint timeout (debugging)
duke@435 198 bool has_called_back() const { return _has_called_back; }
duke@435 199 void set_has_called_back(bool val) { _has_called_back = val; }
duke@435 200 bool is_at_poll_safepoint() { return _at_poll_safepoint; }
duke@435 201 void set_at_poll_safepoint(bool val) { _at_poll_safepoint = val; }
duke@435 202
duke@435 203 void handle_polling_page_exception();
duke@435 204
duke@435 205 // debugging
duke@435 206 void print_on(outputStream* st) const;
duke@435 207 void print() const { print_on(tty); }
duke@435 208
duke@435 209 // Initialize
duke@435 210 static void create(JavaThread *thread);
duke@435 211 static void destroy(JavaThread *thread);
duke@435 212
duke@435 213 void safepoint_msg(const char* format, ...) {
duke@435 214 if (ShowSafepointMsgs) {
duke@435 215 va_list ap;
duke@435 216 va_start(ap, format);
duke@435 217 tty->vprint_cr(format, ap);
duke@435 218 va_end(ap);
duke@435 219 }
duke@435 220 }
duke@435 221 };
duke@435 222
duke@435 223 //
duke@435 224 // CounterDecay
duke@435 225 //
duke@435 226 // Interates through invocation counters and decrements them. This
duke@435 227 // is done at each safepoint.
duke@435 228 //
duke@435 229 class CounterDecay : public AllStatic {
duke@435 230 static jlong _last_timestamp;
duke@435 231 public:
duke@435 232 static void decay();
duke@435 233 static bool is_decay_needed() { return (os::javaTimeMillis() - _last_timestamp) > CounterDecayMinIntervalLength; }
duke@435 234 };

mercurial