aoqi@0: /* aoqi@0: * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@0: #ifndef SHARE_VM_RUNTIME_BIASEDLOCKING_HPP aoqi@0: #define SHARE_VM_RUNTIME_BIASEDLOCKING_HPP aoqi@0: aoqi@0: #include "runtime/handles.hpp" aoqi@0: #include "utilities/growableArray.hpp" aoqi@0: aoqi@0: // This class describes operations to implement Store-Free Biased aoqi@0: // Locking. The high-level properties of the scheme are similar to aoqi@0: // IBM's lock reservation, Dice-Moir-Scherer QR locks, and other biased aoqi@0: // locking mechanisms. The principal difference is in the handling of aoqi@0: // recursive locking which is how this technique achieves a more aoqi@0: // efficient fast path than these other schemes. aoqi@0: // aoqi@0: // The basic observation is that in HotSpot's current fast locking aoqi@0: // scheme, recursive locking (in the fast path) causes no update to aoqi@0: // the object header. The recursion is described simply by stack aoqi@0: // records containing a specific value (NULL). Only the last unlock by aoqi@0: // a given thread causes an update to the object header. aoqi@0: // aoqi@0: // This observation, coupled with the fact that HotSpot only compiles aoqi@0: // methods for which monitor matching is obeyed (and which therefore aoqi@0: // can not throw IllegalMonitorStateException), implies that we can aoqi@0: // completely eliminate modifications to the object header for aoqi@0: // recursive locking in compiled code, and perform similar recursion aoqi@0: // checks and throwing of IllegalMonitorStateException in the aoqi@0: // interpreter with little or no impact on the performance of the fast aoqi@0: // path. aoqi@0: // aoqi@0: // The basic algorithm is as follows (note, see below for more details aoqi@0: // and information). A pattern in the low three bits is reserved in aoqi@0: // the object header to indicate whether biasing of a given object's aoqi@0: // lock is currently being done or is allowed at all. If the bias aoqi@0: // pattern is present, the contents of the rest of the header are aoqi@0: // either the JavaThread* of the thread to which the lock is biased, aoqi@0: // or NULL, indicating that the lock is "anonymously biased". The aoqi@0: // first thread which locks an anonymously biased object biases the aoqi@0: // lock toward that thread. If another thread subsequently attempts to aoqi@0: // lock the same object, the bias is revoked. aoqi@0: // aoqi@0: // Because there are no updates to the object header at all during aoqi@0: // recursive locking while the lock is biased, the biased lock entry aoqi@0: // code is simply a test of the object header's value. If this test aoqi@0: // succeeds, the lock has been acquired by the thread. If this test aoqi@0: // fails, a bit test is done to see whether the bias bit is still aoqi@0: // set. If not, we fall back to HotSpot's original CAS-based locking aoqi@0: // scheme. If it is set, we attempt to CAS in a bias toward this aoqi@0: // thread. The latter operation is expected to be the rarest operation aoqi@0: // performed on these locks. We optimistically expect the biased lock aoqi@0: // entry to hit most of the time, and want the CAS-based fallthrough aoqi@0: // to occur quickly in the situations where the bias has been revoked. aoqi@0: // aoqi@0: // Revocation of the lock's bias is fairly straightforward. We want to aoqi@0: // restore the object's header and stack-based BasicObjectLocks and aoqi@0: // BasicLocks to the state they would have been in had the object been aoqi@0: // locked by HotSpot's usual fast locking scheme. To do this, we bring aoqi@0: // the system to a safepoint and walk the stack of the thread toward aoqi@0: // which the lock is biased. We find all of the lock records on the aoqi@0: // stack corresponding to this object, in particular the first / aoqi@0: // "highest" record. We fill in the highest lock record with the aoqi@0: // object's displaced header (which is a well-known value given that aoqi@0: // we don't maintain an identity hash nor age bits for the object aoqi@0: // while it's in the biased state) and all other lock records with 0, aoqi@0: // the value for recursive locks. When the safepoint is released, the aoqi@0: // formerly-biased thread and all other threads revert back to aoqi@0: // HotSpot's CAS-based locking. aoqi@0: // aoqi@0: // This scheme can not handle transfers of biases of single objects aoqi@0: // from thread to thread efficiently, but it can handle bulk transfers aoqi@0: // of such biases, which is a usage pattern showing up in some aoqi@0: // applications and benchmarks. We implement "bulk rebias" and "bulk aoqi@0: // revoke" operations using a "bias epoch" on a per-data-type basis. aoqi@0: // If too many bias revocations are occurring for a particular data aoqi@0: // type, the bias epoch for the data type is incremented at a aoqi@0: // safepoint, effectively meaning that all previous biases are aoqi@0: // invalid. The fast path locking case checks for an invalid epoch in aoqi@0: // the object header and attempts to rebias the object with a CAS if aoqi@0: // found, avoiding safepoints or bulk heap sweeps (the latter which aoqi@0: // was used in a prior version of this algorithm and did not scale aoqi@0: // well). If too many bias revocations persist, biasing is completely aoqi@0: // disabled for the data type by resetting the prototype header to the aoqi@0: // unbiased markOop. The fast-path locking code checks to see whether aoqi@0: // the instance's bias pattern differs from the prototype header's and aoqi@0: // causes the bias to be revoked without reaching a safepoint or, aoqi@0: // again, a bulk heap sweep. aoqi@0: aoqi@0: // Biased locking counters aoqi@0: class BiasedLockingCounters VALUE_OBJ_CLASS_SPEC { aoqi@0: private: aoqi@0: int _total_entry_count; aoqi@0: int _biased_lock_entry_count; aoqi@0: int _anonymously_biased_lock_entry_count; aoqi@0: int _rebiased_lock_entry_count; aoqi@0: int _revoked_lock_entry_count; aoqi@0: int _fast_path_entry_count; aoqi@0: int _slow_path_entry_count; aoqi@0: aoqi@0: public: aoqi@0: BiasedLockingCounters() : aoqi@0: _total_entry_count(0), aoqi@0: _biased_lock_entry_count(0), aoqi@0: _anonymously_biased_lock_entry_count(0), aoqi@0: _rebiased_lock_entry_count(0), aoqi@0: _revoked_lock_entry_count(0), aoqi@0: _fast_path_entry_count(0), aoqi@0: _slow_path_entry_count(0) {} aoqi@0: aoqi@0: int slow_path_entry_count(); // Compute this field if necessary aoqi@0: aoqi@0: int* total_entry_count_addr() { return &_total_entry_count; } aoqi@0: int* biased_lock_entry_count_addr() { return &_biased_lock_entry_count; } aoqi@0: int* anonymously_biased_lock_entry_count_addr() { return &_anonymously_biased_lock_entry_count; } aoqi@0: int* rebiased_lock_entry_count_addr() { return &_rebiased_lock_entry_count; } aoqi@0: int* revoked_lock_entry_count_addr() { return &_revoked_lock_entry_count; } aoqi@0: int* fast_path_entry_count_addr() { return &_fast_path_entry_count; } aoqi@0: int* slow_path_entry_count_addr() { return &_slow_path_entry_count; } aoqi@0: aoqi@0: bool nonzero() { return _total_entry_count > 0; } aoqi@0: aoqi@0: void print_on(outputStream* st); aoqi@0: void print() { print_on(tty); } aoqi@0: }; aoqi@0: aoqi@0: aoqi@0: class BiasedLocking : AllStatic { aoqi@0: private: aoqi@0: static BiasedLockingCounters _counters; aoqi@0: aoqi@0: public: aoqi@0: static int* total_entry_count_addr(); aoqi@0: static int* biased_lock_entry_count_addr(); aoqi@0: static int* anonymously_biased_lock_entry_count_addr(); aoqi@0: static int* rebiased_lock_entry_count_addr(); aoqi@0: static int* revoked_lock_entry_count_addr(); aoqi@0: static int* fast_path_entry_count_addr(); aoqi@0: static int* slow_path_entry_count_addr(); aoqi@0: aoqi@0: enum Condition { aoqi@0: NOT_BIASED = 1, aoqi@0: BIAS_REVOKED = 2, aoqi@0: BIAS_REVOKED_AND_REBIASED = 3 aoqi@0: }; aoqi@0: aoqi@0: // This initialization routine should only be called once and aoqi@0: // schedules a PeriodicTask to turn on biased locking a few seconds aoqi@0: // into the VM run to avoid startup time regressions aoqi@0: static void init(); aoqi@0: aoqi@0: // This provides a global switch for leaving biased locking disabled aoqi@0: // for the first part of a run and enabling it later aoqi@0: static bool enabled(); aoqi@0: aoqi@0: // This should be called by JavaThreads to revoke the bias of an object aoqi@0: static Condition revoke_and_rebias(Handle obj, bool attempt_rebias, TRAPS); aoqi@0: aoqi@0: // These do not allow rebiasing; they are used by deoptimization to aoqi@0: // ensure that monitors on the stack can be migrated aoqi@0: static void revoke(GrowableArray* objs); aoqi@0: static void revoke_at_safepoint(Handle obj); aoqi@0: static void revoke_at_safepoint(GrowableArray* objs); aoqi@0: aoqi@0: static void print_counters() { _counters.print(); } aoqi@0: static BiasedLockingCounters* counters() { return &_counters; } aoqi@0: aoqi@0: // These routines are GC-related and should not be called by end aoqi@0: // users. GCs which do not do preservation of mark words do not need aoqi@0: // to call these routines. aoqi@0: static void preserve_marks(); aoqi@0: static void restore_marks(); aoqi@0: }; aoqi@0: aoqi@0: #endif // SHARE_VM_RUNTIME_BIASEDLOCKING_HPP