Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/runtime/biasedLocking.hpp
32285 views
/*1* Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#ifndef SHARE_VM_RUNTIME_BIASEDLOCKING_HPP25#define SHARE_VM_RUNTIME_BIASEDLOCKING_HPP2627#include "runtime/handles.hpp"28#include "utilities/growableArray.hpp"2930// This class describes operations to implement Store-Free Biased31// Locking. The high-level properties of the scheme are similar to32// IBM's lock reservation, Dice-Moir-Scherer QR locks, and other biased33// locking mechanisms. The principal difference is in the handling of34// recursive locking which is how this technique achieves a more35// efficient fast path than these other schemes.36//37// The basic observation is that in HotSpot's current fast locking38// scheme, recursive locking (in the fast path) causes no update to39// the object header. The recursion is described simply by stack40// records containing a specific value (NULL). Only the last unlock by41// a given thread causes an update to the object header.42//43// This observation, coupled with the fact that HotSpot only compiles44// methods for which monitor matching is obeyed (and which therefore45// can not throw IllegalMonitorStateException), implies that we can46// completely eliminate modifications to the object header for47// recursive locking in compiled code, and perform similar recursion48// checks and throwing of IllegalMonitorStateException in the49// interpreter with little or no impact on the performance of the fast50// path.51//52// The basic algorithm is as follows (note, see below for more details53// and information). A pattern in the low three bits is reserved in54// the object header to indicate whether biasing of a given object's55// lock is currently being done or is allowed at all. If the bias56// pattern is present, the contents of the rest of the header are57// either the JavaThread* of the thread to which the lock is biased,58// or NULL, indicating that the lock is "anonymously biased". The59// first thread which locks an anonymously biased object biases the60// lock toward that thread. If another thread subsequently attempts to61// lock the same object, the bias is revoked.62//63// Because there are no updates to the object header at all during64// recursive locking while the lock is biased, the biased lock entry65// code is simply a test of the object header's value. If this test66// succeeds, the lock has been acquired by the thread. If this test67// fails, a bit test is done to see whether the bias bit is still68// set. If not, we fall back to HotSpot's original CAS-based locking69// scheme. If it is set, we attempt to CAS in a bias toward this70// thread. The latter operation is expected to be the rarest operation71// performed on these locks. We optimistically expect the biased lock72// entry to hit most of the time, and want the CAS-based fallthrough73// to occur quickly in the situations where the bias has been revoked.74//75// Revocation of the lock's bias is fairly straightforward. We want to76// restore the object's header and stack-based BasicObjectLocks and77// BasicLocks to the state they would have been in had the object been78// locked by HotSpot's usual fast locking scheme. To do this, we bring79// the system to a safepoint and walk the stack of the thread toward80// which the lock is biased. We find all of the lock records on the81// stack corresponding to this object, in particular the first /82// "highest" record. We fill in the highest lock record with the83// object's displaced header (which is a well-known value given that84// we don't maintain an identity hash nor age bits for the object85// while it's in the biased state) and all other lock records with 0,86// the value for recursive locks. When the safepoint is released, the87// formerly-biased thread and all other threads revert back to88// HotSpot's CAS-based locking.89//90// This scheme can not handle transfers of biases of single objects91// from thread to thread efficiently, but it can handle bulk transfers92// of such biases, which is a usage pattern showing up in some93// applications and benchmarks. We implement "bulk rebias" and "bulk94// revoke" operations using a "bias epoch" on a per-data-type basis.95// If too many bias revocations are occurring for a particular data96// type, the bias epoch for the data type is incremented at a97// safepoint, effectively meaning that all previous biases are98// invalid. The fast path locking case checks for an invalid epoch in99// the object header and attempts to rebias the object with a CAS if100// found, avoiding safepoints or bulk heap sweeps (the latter which101// was used in a prior version of this algorithm and did not scale102// well). If too many bias revocations persist, biasing is completely103// disabled for the data type by resetting the prototype header to the104// unbiased markOop. The fast-path locking code checks to see whether105// the instance's bias pattern differs from the prototype header's and106// causes the bias to be revoked without reaching a safepoint or,107// again, a bulk heap sweep.108109// Biased locking counters110class BiasedLockingCounters VALUE_OBJ_CLASS_SPEC {111private:112int _total_entry_count;113int _biased_lock_entry_count;114int _anonymously_biased_lock_entry_count;115int _rebiased_lock_entry_count;116int _revoked_lock_entry_count;117int _fast_path_entry_count;118int _slow_path_entry_count;119120public:121BiasedLockingCounters() :122_total_entry_count(0),123_biased_lock_entry_count(0),124_anonymously_biased_lock_entry_count(0),125_rebiased_lock_entry_count(0),126_revoked_lock_entry_count(0),127_fast_path_entry_count(0),128_slow_path_entry_count(0) {}129130int slow_path_entry_count(); // Compute this field if necessary131132int* total_entry_count_addr() { return &_total_entry_count; }133int* biased_lock_entry_count_addr() { return &_biased_lock_entry_count; }134int* anonymously_biased_lock_entry_count_addr() { return &_anonymously_biased_lock_entry_count; }135int* rebiased_lock_entry_count_addr() { return &_rebiased_lock_entry_count; }136int* revoked_lock_entry_count_addr() { return &_revoked_lock_entry_count; }137int* fast_path_entry_count_addr() { return &_fast_path_entry_count; }138int* slow_path_entry_count_addr() { return &_slow_path_entry_count; }139140bool nonzero() { return _total_entry_count > 0; }141142void print_on(outputStream* st);143void print() { print_on(tty); }144};145146147class BiasedLocking : AllStatic {148private:149static BiasedLockingCounters _counters;150151public:152static int* total_entry_count_addr();153static int* biased_lock_entry_count_addr();154static int* anonymously_biased_lock_entry_count_addr();155static int* rebiased_lock_entry_count_addr();156static int* revoked_lock_entry_count_addr();157static int* fast_path_entry_count_addr();158static int* slow_path_entry_count_addr();159160enum Condition {161NOT_BIASED = 1,162BIAS_REVOKED = 2,163BIAS_REVOKED_AND_REBIASED = 3164};165166// This initialization routine should only be called once and167// schedules a PeriodicTask to turn on biased locking a few seconds168// into the VM run to avoid startup time regressions169static void init();170171// This provides a global switch for leaving biased locking disabled172// for the first part of a run and enabling it later173static bool enabled();174175// This should be called by JavaThreads to revoke the bias of an object176static Condition revoke_and_rebias(Handle obj, bool attempt_rebias, TRAPS);177178// These do not allow rebiasing; they are used by deoptimization to179// ensure that monitors on the stack can be migrated180static void revoke(GrowableArray<Handle>* objs);181static void revoke_at_safepoint(Handle obj);182static void revoke_at_safepoint(GrowableArray<Handle>* objs);183184static void print_counters() { _counters.print(); }185static BiasedLockingCounters* counters() { return &_counters; }186187// These routines are GC-related and should not be called by end188// users. GCs which do not do preservation of mark words do not need189// to call these routines.190static void preserve_marks();191static void restore_marks();192};193194#endif // SHARE_VM_RUNTIME_BIASEDLOCKING_HPP195196197