Path: blob/master/src/hotspot/share/runtime/biasedLocking.hpp
40951 views
/*1* Copyright (c) 2005, 2021, 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_RUNTIME_BIASEDLOCKING_HPP25#define SHARE_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 execute79// a handshake with the JavaThread that biased the lock. Inside the80// handshake we walk the biaser stack searching for all of the lock81// records corresponding to this object, in particular the first / "highest"82// record. We fill in the highest lock record with the object's displaced83// header (which is a well-known value given that we don't maintain an84// identity hash nor age bits for the object while it's in the biased85// state) and all other lock records with 0, the value for recursive locks.86// Alternatively, we can revoke the bias of an object inside a safepoint87// if we are already in one and we detect that we need to perform a88// revocation.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 markWord. 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 {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 _handshakes_count;118int _fast_path_entry_count;119int _slow_path_entry_count;120121public:122BiasedLockingCounters() :123_total_entry_count(0),124_biased_lock_entry_count(0),125_anonymously_biased_lock_entry_count(0),126_rebiased_lock_entry_count(0),127_revoked_lock_entry_count(0),128_handshakes_count(0),129_fast_path_entry_count(0),130_slow_path_entry_count(0) {}131132int slow_path_entry_count() const; // Compute this field if necessary133134int* total_entry_count_addr() { return &_total_entry_count; }135int* biased_lock_entry_count_addr() { return &_biased_lock_entry_count; }136int* anonymously_biased_lock_entry_count_addr() { return &_anonymously_biased_lock_entry_count; }137int* rebiased_lock_entry_count_addr() { return &_rebiased_lock_entry_count; }138int* revoked_lock_entry_count_addr() { return &_revoked_lock_entry_count; }139int* handshakes_count_addr() { return &_handshakes_count; }140int* fast_path_entry_count_addr() { return &_fast_path_entry_count; }141int* slow_path_entry_count_addr() { return &_slow_path_entry_count; }142143bool nonzero() { return _total_entry_count > 0; }144145void print_on(outputStream* st) const;146void print() const;147};148149150class BiasedLocking : AllStatic {151friend class VM_BulkRevokeBias;152friend class RevokeOneBias;153154private:155static BiasedLockingCounters _counters;156157public:158static int* total_entry_count_addr();159static int* biased_lock_entry_count_addr();160static int* anonymously_biased_lock_entry_count_addr();161static int* rebiased_lock_entry_count_addr();162static int* revoked_lock_entry_count_addr();163static int* handshakes_count_addr();164static int* fast_path_entry_count_addr();165static int* slow_path_entry_count_addr();166167enum Condition {168NOT_BIASED = 1,169BIAS_REVOKED = 2,170NOT_REVOKED = 3171};172173private:174static void single_revoke_at_safepoint(oop obj, bool is_bulk, JavaThread* requester, JavaThread** biaser);175static void bulk_revoke_at_safepoint(oop o, bool bulk_rebias, JavaThread* requester);176static Condition single_revoke_with_handshake(Handle obj, JavaThread *requester, JavaThread *biaser);177static void walk_stack_and_revoke(oop obj, JavaThread* biased_locker);178179public:180// This initialization routine should only be called once and181// schedules a PeriodicTask to turn on biased locking a few seconds182// into the VM run to avoid startup time regressions183static void init();184185// This provides a global switch for leaving biased locking disabled186// for the first part of a run and enabling it later187static bool enabled();188189// This should be called by JavaThreads to revoke the bias of an object190static void revoke(JavaThread* current, Handle obj);191192// This must only be called by a JavaThread to revoke the bias of an owned object.193static void revoke_own_lock(JavaThread* current, Handle obj);194195static void revoke_at_safepoint(Handle obj);196197// These are used by deoptimization to ensure that monitors on the stack198// can be migrated199static void revoke(GrowableArray<Handle>* objs, JavaThread *biaser);200201static void print_counters() { _counters.print(); }202static BiasedLockingCounters* counters() { return &_counters; }203204// These routines are GC-related and should not be called by end205// users. GCs which do not do preservation of mark words do not need206// to call these routines.207static void preserve_marks();208static void restore_marks();209};210211#endif // SHARE_RUNTIME_BIASEDLOCKING_HPP212213214