duke@435: /* never@3494: * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. duke@435: * duke@435: */ duke@435: stefank@2314: #ifndef SHARE_VM_RUNTIME_SAFEPOINT_HPP stefank@2314: #define SHARE_VM_RUNTIME_SAFEPOINT_HPP stefank@2314: stefank@2314: #include "asm/assembler.hpp" stefank@2314: #include "code/nmethod.hpp" stefank@2314: #include "memory/allocation.hpp" stefank@2314: #include "runtime/extendedPC.hpp" never@3494: #include "runtime/mutexLocker.hpp" stefank@2314: #include "runtime/os.hpp" stefank@2314: #include "utilities/ostream.hpp" stefank@2314: duke@435: // duke@435: // Safepoint synchronization duke@435: //// duke@435: // The VMThread or CMS_thread uses the SafepointSynchronize::begin/end duke@435: // methods to enter/exit a safepoint region. The begin method will roll duke@435: // all JavaThreads forward to a safepoint. duke@435: // duke@435: // JavaThreads must use the ThreadSafepointState abstraction (defined in duke@435: // thread.hpp) to indicate that that they are at a safepoint. duke@435: // duke@435: // The Mutex/Condition variable and ObjectLocker classes calls the enter/ duke@435: // exit safepoint methods, when a thread is blocked/restarted. Hence, all mutex exter/ duke@435: // exit points *must* be at a safepoint. duke@435: duke@435: duke@435: class ThreadSafepointState; duke@435: class SnippetCache; duke@435: class nmethod; duke@435: duke@435: // duke@435: // Implements roll-forward to safepoint (safepoint synchronization) duke@435: // duke@435: class SafepointSynchronize : AllStatic { duke@435: public: duke@435: enum SynchronizeState { duke@435: _not_synchronized = 0, // Threads not synchronized at a safepoint duke@435: // Keep this value 0. See the coment in do_call_back() duke@435: _synchronizing = 1, // Synchronizing in progress duke@435: _synchronized = 2 // All Java threads are stopped at a safepoint. Only VM thread is running duke@435: }; duke@435: duke@435: enum SafepointingThread { duke@435: _null_thread = 0, duke@435: _vm_thread = 1, duke@435: _other_thread = 2 duke@435: }; duke@435: duke@435: enum SafepointTimeoutReason { duke@435: _spinning_timeout = 0, duke@435: _blocking_timeout = 1 duke@435: }; duke@435: duke@435: typedef struct { xlu@1726: float _time_stamp; // record when the current safepoint occurs in seconds duke@435: int _vmop_type; // type of VM operation triggers the safepoint duke@435: int _nof_total_threads; // total number of Java threads duke@435: int _nof_initial_running_threads; // total number of initially seen running threads duke@435: int _nof_threads_wait_to_block; // total number of threads waiting for to block duke@435: bool _page_armed; // true if polling page is armed, false otherwise duke@435: int _nof_threads_hit_page_trap; // total number of threads hitting the page trap duke@435: jlong _time_to_spin; // total time in millis spent in spinning duke@435: jlong _time_to_wait_to_block; // total time in millis spent in waiting for to block xlu@1726: jlong _time_to_do_cleanups; // total time in millis spent in performing cleanups duke@435: jlong _time_to_sync; // total time in millis spent in getting to _synchronized duke@435: jlong _time_to_exec_vmop; // total time in millis spent in vm operation itself duke@435: } SafepointStats; duke@435: duke@435: private: duke@435: static volatile SynchronizeState _state; // Threads might read this flag directly, without acquireing the Threads_lock xlu@1726: static volatile int _waiting_to_block; // number of threads we are waiting for to block never@3494: static int _current_jni_active_count; // Counts the number of active critical natives during the safepoint duke@435: duke@435: // This counter is used for fast versions of jni_GetField. duke@435: // An even value means there is no ongoing safepoint operations. duke@435: // The counter is incremented ONLY at the beginning and end of each duke@435: // safepoint. The fact that Threads_lock is held throughout each pair of duke@435: // increments (at the beginning and end of each safepoint) guarantees duke@435: // race freedom. duke@435: public: duke@435: static volatile int _safepoint_counter; duke@435: private: xlu@1726: static long _end_of_last_safepoint; // Time of last safepoint in milliseconds duke@435: duke@435: // statistics xlu@1726: static jlong _safepoint_begin_time; // time when safepoint begins xlu@1726: static SafepointStats* _safepoint_stats; // array of SafepointStats struct xlu@1726: static int _cur_stat_index; // current index to the above array xlu@1726: static julong _safepoint_reasons[]; // safepoint count for each VM op xlu@1726: static julong _coalesced_vmop_count; // coalesced vmop count xlu@1726: static jlong _max_sync_time; // maximum sync time in nanos xlu@1726: static jlong _max_vmop_time; // maximum vm operation time in nanos xlu@1726: static float _ts_of_current_safepoint; // time stamp of current safepoint in seconds duke@435: duke@435: static void begin_statistics(int nof_threads, int nof_running); duke@435: static void update_statistics_on_spin_end(); duke@435: static void update_statistics_on_sync_end(jlong end_time); xlu@1726: static void update_statistics_on_cleanup_end(jlong end_time); duke@435: static void end_statistics(jlong end_time); duke@435: static void print_statistics(); duke@435: inline static void inc_page_trap_count() { duke@435: Atomic::inc(&_safepoint_stats[_cur_stat_index]._nof_threads_hit_page_trap); duke@435: } duke@435: duke@435: // For debug long safepoint duke@435: static void print_safepoint_timeout(SafepointTimeoutReason timeout_reason); duke@435: duke@435: public: duke@435: duke@435: // Main entry points duke@435: duke@435: // Roll all threads forward to safepoint. Must be called by the duke@435: // VMThread or CMS_thread. duke@435: static void begin(); duke@435: static void end(); // Start all suspended threads again... duke@435: duke@435: static bool safepoint_safe(JavaThread *thread, JavaThreadState state); duke@435: never@3500: static void check_for_lazy_critical_native(JavaThread *thread, JavaThreadState state); never@3500: duke@435: // Query duke@435: inline static bool is_at_safepoint() { return _state == _synchronized; } duke@435: inline static bool is_synchronizing() { return _state == _synchronizing; } duke@435: duke@435: inline static bool do_call_back() { duke@435: return (_state != _not_synchronized); duke@435: } duke@435: never@3494: inline static void increment_jni_active_count() { never@3494: assert_locked_or_safepoint(Safepoint_lock); never@3494: _current_jni_active_count++; never@3494: } never@3494: duke@435: // Called when a thread volantary blocks duke@435: static void block(JavaThread *thread); duke@435: static void signal_thread_at_safepoint() { _waiting_to_block--; } duke@435: duke@435: // Exception handling for page polling duke@435: static void handle_polling_page_exception(JavaThread *thread); duke@435: duke@435: // VM Thread interface for determining safepoint rate xlu@1726: static long last_non_safepoint_interval() { xlu@1726: return os::javaTimeMillis() - _end_of_last_safepoint; xlu@1726: } iveresov@2138: static long end_of_last_safepoint() { iveresov@2138: return _end_of_last_safepoint; iveresov@2138: } duke@435: static bool is_cleanup_needed(); duke@435: static void do_cleanup_tasks(); duke@435: duke@435: // debugging duke@435: static void print_state() PRODUCT_RETURN; duke@435: static void safepoint_msg(const char* format, ...) PRODUCT_RETURN; duke@435: duke@435: static void deferred_initialize_stat(); duke@435: static void print_stat_on_exit(); duke@435: inline static void inc_vmop_coalesced_count() { _coalesced_vmop_count++; } duke@435: duke@435: static void set_is_at_safepoint() { _state = _synchronized; } duke@435: static void set_is_not_at_safepoint() { _state = _not_synchronized; } duke@435: duke@435: // assembly support duke@435: static address address_of_state() { return (address)&_state; } duke@435: duke@435: static address safepoint_counter_addr() { return (address)&_safepoint_counter; } duke@435: }; duke@435: duke@435: // State class for a thread suspended at a safepoint zgu@3900: class ThreadSafepointState: public CHeapObj { duke@435: public: duke@435: // These states are maintained by VM thread while threads are being brought duke@435: // to a safepoint. After SafepointSynchronize::end(), they are reset to duke@435: // _running. duke@435: enum suspend_type { duke@435: _running = 0, // Thread state not yet determined (i.e., not at a safepoint yet) duke@435: _at_safepoint = 1, // Thread at a safepoint (f.ex., when blocked on a lock) duke@435: _call_back = 2 // Keep executing and wait for callback (if thread is in interpreted or vm) duke@435: }; duke@435: private: duke@435: volatile bool _at_poll_safepoint; // At polling page safepoint (NOT a poll return safepoint) duke@435: // Thread has called back the safepoint code (for debugging) duke@435: bool _has_called_back; duke@435: duke@435: JavaThread * _thread; duke@435: volatile suspend_type _type; never@2082: JavaThreadState _orig_thread_state; duke@435: duke@435: duke@435: public: duke@435: ThreadSafepointState(JavaThread *thread); duke@435: duke@435: // examine/roll-forward/restart duke@435: void examine_state_of_thread(); duke@435: void roll_forward(suspend_type type); duke@435: void restart(); duke@435: duke@435: // Query duke@435: JavaThread* thread() const { return _thread; } duke@435: suspend_type type() const { return _type; } duke@435: bool is_running() const { return (_type==_running); } never@2082: JavaThreadState orig_thread_state() const { return _orig_thread_state; } duke@435: duke@435: // Support for safepoint timeout (debugging) duke@435: bool has_called_back() const { return _has_called_back; } duke@435: void set_has_called_back(bool val) { _has_called_back = val; } duke@435: bool is_at_poll_safepoint() { return _at_poll_safepoint; } duke@435: void set_at_poll_safepoint(bool val) { _at_poll_safepoint = val; } duke@435: duke@435: void handle_polling_page_exception(); duke@435: duke@435: // debugging duke@435: void print_on(outputStream* st) const; duke@435: void print() const { print_on(tty); } duke@435: duke@435: // Initialize duke@435: static void create(JavaThread *thread); duke@435: static void destroy(JavaThread *thread); duke@435: duke@435: void safepoint_msg(const char* format, ...) { duke@435: if (ShowSafepointMsgs) { duke@435: va_list ap; duke@435: va_start(ap, format); duke@435: tty->vprint_cr(format, ap); duke@435: va_end(ap); duke@435: } duke@435: } duke@435: }; duke@435: iveresov@2138: stefank@2314: stefank@2314: #endif // SHARE_VM_RUNTIME_SAFEPOINT_HPP