1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/vm/runtime/safepoint.hpp Sat Dec 01 00:00:00 2007 +0000 1.3 @@ -0,0 +1,234 @@ 1.4 +/* 1.5 + * Copyright 1997-2007 Sun Microsystems, Inc. All Rights Reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 1.23 + * CA 95054 USA or visit www.sun.com if you need additional information or 1.24 + * have any questions. 1.25 + * 1.26 + */ 1.27 + 1.28 +// 1.29 +// Safepoint synchronization 1.30 +//// 1.31 +// The VMThread or CMS_thread uses the SafepointSynchronize::begin/end 1.32 +// methods to enter/exit a safepoint region. The begin method will roll 1.33 +// all JavaThreads forward to a safepoint. 1.34 +// 1.35 +// JavaThreads must use the ThreadSafepointState abstraction (defined in 1.36 +// thread.hpp) to indicate that that they are at a safepoint. 1.37 +// 1.38 +// The Mutex/Condition variable and ObjectLocker classes calls the enter/ 1.39 +// exit safepoint methods, when a thread is blocked/restarted. Hence, all mutex exter/ 1.40 +// exit points *must* be at a safepoint. 1.41 + 1.42 + 1.43 +class ThreadSafepointState; 1.44 +class SnippetCache; 1.45 +class nmethod; 1.46 + 1.47 +// 1.48 +// Implements roll-forward to safepoint (safepoint synchronization) 1.49 +// 1.50 +class SafepointSynchronize : AllStatic { 1.51 + public: 1.52 + enum SynchronizeState { 1.53 + _not_synchronized = 0, // Threads not synchronized at a safepoint 1.54 + // Keep this value 0. See the coment in do_call_back() 1.55 + _synchronizing = 1, // Synchronizing in progress 1.56 + _synchronized = 2 // All Java threads are stopped at a safepoint. Only VM thread is running 1.57 + }; 1.58 + 1.59 + enum SafepointingThread { 1.60 + _null_thread = 0, 1.61 + _vm_thread = 1, 1.62 + _other_thread = 2 1.63 + }; 1.64 + 1.65 + enum SafepointTimeoutReason { 1.66 + _spinning_timeout = 0, 1.67 + _blocking_timeout = 1 1.68 + }; 1.69 + 1.70 + typedef struct { 1.71 + int _vmop_type; // type of VM operation triggers the safepoint 1.72 + int _nof_total_threads; // total number of Java threads 1.73 + int _nof_initial_running_threads; // total number of initially seen running threads 1.74 + int _nof_threads_wait_to_block; // total number of threads waiting for to block 1.75 + bool _page_armed; // true if polling page is armed, false otherwise 1.76 + int _nof_threads_hit_page_trap; // total number of threads hitting the page trap 1.77 + jlong _time_to_spin; // total time in millis spent in spinning 1.78 + jlong _time_to_wait_to_block; // total time in millis spent in waiting for to block 1.79 + jlong _time_to_sync; // total time in millis spent in getting to _synchronized 1.80 + jlong _time_to_exec_vmop; // total time in millis spent in vm operation itself 1.81 + jlong _time_elapsed_since_last_safepoint; // time elasped since last safepoint 1.82 + } SafepointStats; 1.83 + 1.84 + private: 1.85 + static volatile SynchronizeState _state; // Threads might read this flag directly, without acquireing the Threads_lock 1.86 + static volatile int _waiting_to_block; // No. of threads we are waiting for to block. 1.87 + 1.88 + // This counter is used for fast versions of jni_Get<Primitive>Field. 1.89 + // An even value means there is no ongoing safepoint operations. 1.90 + // The counter is incremented ONLY at the beginning and end of each 1.91 + // safepoint. The fact that Threads_lock is held throughout each pair of 1.92 + // increments (at the beginning and end of each safepoint) guarantees 1.93 + // race freedom. 1.94 +public: 1.95 + static volatile int _safepoint_counter; 1.96 +private: 1.97 + 1.98 + static jlong _last_safepoint; // Time of last safepoint 1.99 + 1.100 + // statistics 1.101 + static SafepointStats* _safepoint_stats; // array of SafepointStats struct 1.102 + static int _cur_stat_index; // current index to the above array 1.103 + static julong _safepoint_reasons[]; // safepoint count for each VM op 1.104 + static julong _coalesced_vmop_count;// coalesced vmop count 1.105 + static jlong _max_sync_time; // maximum sync time in nanos 1.106 + 1.107 + static void begin_statistics(int nof_threads, int nof_running); 1.108 + static void update_statistics_on_spin_end(); 1.109 + static void update_statistics_on_sync_end(jlong end_time); 1.110 + static void end_statistics(jlong end_time); 1.111 + static void print_statistics(); 1.112 + inline static void inc_page_trap_count() { 1.113 + Atomic::inc(&_safepoint_stats[_cur_stat_index]._nof_threads_hit_page_trap); 1.114 + } 1.115 + 1.116 + // For debug long safepoint 1.117 + static void print_safepoint_timeout(SafepointTimeoutReason timeout_reason); 1.118 + 1.119 +public: 1.120 + 1.121 + // Main entry points 1.122 + 1.123 + // Roll all threads forward to safepoint. Must be called by the 1.124 + // VMThread or CMS_thread. 1.125 + static void begin(); 1.126 + static void end(); // Start all suspended threads again... 1.127 + 1.128 + static bool safepoint_safe(JavaThread *thread, JavaThreadState state); 1.129 + 1.130 + // Query 1.131 + inline static bool is_at_safepoint() { return _state == _synchronized; } 1.132 + inline static bool is_synchronizing() { return _state == _synchronizing; } 1.133 + 1.134 + inline static bool do_call_back() { 1.135 + return (_state != _not_synchronized); 1.136 + } 1.137 + 1.138 + // Called when a thread volantary blocks 1.139 + static void block(JavaThread *thread); 1.140 + static void signal_thread_at_safepoint() { _waiting_to_block--; } 1.141 + 1.142 + // Exception handling for page polling 1.143 + static void handle_polling_page_exception(JavaThread *thread); 1.144 + 1.145 + // VM Thread interface for determining safepoint rate 1.146 + static long last_non_safepoint_interval() { return os::javaTimeMillis() - _last_safepoint; } 1.147 + static bool is_cleanup_needed(); 1.148 + static void do_cleanup_tasks(); 1.149 + 1.150 + // debugging 1.151 + static void print_state() PRODUCT_RETURN; 1.152 + static void safepoint_msg(const char* format, ...) PRODUCT_RETURN; 1.153 + 1.154 + static void deferred_initialize_stat(); 1.155 + static void print_stat_on_exit(); 1.156 + inline static void inc_vmop_coalesced_count() { _coalesced_vmop_count++; } 1.157 + 1.158 + static void set_is_at_safepoint() { _state = _synchronized; } 1.159 + static void set_is_not_at_safepoint() { _state = _not_synchronized; } 1.160 + 1.161 + // assembly support 1.162 + static address address_of_state() { return (address)&_state; } 1.163 + 1.164 + static address safepoint_counter_addr() { return (address)&_safepoint_counter; } 1.165 +}; 1.166 + 1.167 +// State class for a thread suspended at a safepoint 1.168 +class ThreadSafepointState: public CHeapObj { 1.169 + public: 1.170 + // These states are maintained by VM thread while threads are being brought 1.171 + // to a safepoint. After SafepointSynchronize::end(), they are reset to 1.172 + // _running. 1.173 + enum suspend_type { 1.174 + _running = 0, // Thread state not yet determined (i.e., not at a safepoint yet) 1.175 + _at_safepoint = 1, // Thread at a safepoint (f.ex., when blocked on a lock) 1.176 + _call_back = 2 // Keep executing and wait for callback (if thread is in interpreted or vm) 1.177 + }; 1.178 + private: 1.179 + volatile bool _at_poll_safepoint; // At polling page safepoint (NOT a poll return safepoint) 1.180 + // Thread has called back the safepoint code (for debugging) 1.181 + bool _has_called_back; 1.182 + 1.183 + JavaThread * _thread; 1.184 + volatile suspend_type _type; 1.185 + 1.186 + 1.187 + public: 1.188 + ThreadSafepointState(JavaThread *thread); 1.189 + 1.190 + // examine/roll-forward/restart 1.191 + void examine_state_of_thread(); 1.192 + void roll_forward(suspend_type type); 1.193 + void restart(); 1.194 + 1.195 + // Query 1.196 + JavaThread* thread() const { return _thread; } 1.197 + suspend_type type() const { return _type; } 1.198 + bool is_running() const { return (_type==_running); } 1.199 + 1.200 + // Support for safepoint timeout (debugging) 1.201 + bool has_called_back() const { return _has_called_back; } 1.202 + void set_has_called_back(bool val) { _has_called_back = val; } 1.203 + bool is_at_poll_safepoint() { return _at_poll_safepoint; } 1.204 + void set_at_poll_safepoint(bool val) { _at_poll_safepoint = val; } 1.205 + 1.206 + void handle_polling_page_exception(); 1.207 + 1.208 + // debugging 1.209 + void print_on(outputStream* st) const; 1.210 + void print() const { print_on(tty); } 1.211 + 1.212 + // Initialize 1.213 + static void create(JavaThread *thread); 1.214 + static void destroy(JavaThread *thread); 1.215 + 1.216 + void safepoint_msg(const char* format, ...) { 1.217 + if (ShowSafepointMsgs) { 1.218 + va_list ap; 1.219 + va_start(ap, format); 1.220 + tty->vprint_cr(format, ap); 1.221 + va_end(ap); 1.222 + } 1.223 + } 1.224 +}; 1.225 + 1.226 +// 1.227 +// CounterDecay 1.228 +// 1.229 +// Interates through invocation counters and decrements them. This 1.230 +// is done at each safepoint. 1.231 +// 1.232 +class CounterDecay : public AllStatic { 1.233 + static jlong _last_timestamp; 1.234 + public: 1.235 + static void decay(); 1.236 + static bool is_decay_needed() { return (os::javaTimeMillis() - _last_timestamp) > CounterDecayMinIntervalLength; } 1.237 +};