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