src/share/vm/runtime/biasedLocking.hpp

Wed, 27 Apr 2016 01:25:04 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:25:04 +0800
changeset 0
f90c822e73f8
child 6876
710a3c8b516e
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/hotspot/
changeset: 6782:28b50d07f6f8
tag: jdk8u25-b17

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #ifndef SHARE_VM_RUNTIME_BIASEDLOCKING_HPP
aoqi@0 26 #define SHARE_VM_RUNTIME_BIASEDLOCKING_HPP
aoqi@0 27
aoqi@0 28 #include "runtime/handles.hpp"
aoqi@0 29 #include "utilities/growableArray.hpp"
aoqi@0 30
aoqi@0 31 // This class describes operations to implement Store-Free Biased
aoqi@0 32 // Locking. The high-level properties of the scheme are similar to
aoqi@0 33 // IBM's lock reservation, Dice-Moir-Scherer QR locks, and other biased
aoqi@0 34 // locking mechanisms. The principal difference is in the handling of
aoqi@0 35 // recursive locking which is how this technique achieves a more
aoqi@0 36 // efficient fast path than these other schemes.
aoqi@0 37 //
aoqi@0 38 // The basic observation is that in HotSpot's current fast locking
aoqi@0 39 // scheme, recursive locking (in the fast path) causes no update to
aoqi@0 40 // the object header. The recursion is described simply by stack
aoqi@0 41 // records containing a specific value (NULL). Only the last unlock by
aoqi@0 42 // a given thread causes an update to the object header.
aoqi@0 43 //
aoqi@0 44 // This observation, coupled with the fact that HotSpot only compiles
aoqi@0 45 // methods for which monitor matching is obeyed (and which therefore
aoqi@0 46 // can not throw IllegalMonitorStateException), implies that we can
aoqi@0 47 // completely eliminate modifications to the object header for
aoqi@0 48 // recursive locking in compiled code, and perform similar recursion
aoqi@0 49 // checks and throwing of IllegalMonitorStateException in the
aoqi@0 50 // interpreter with little or no impact on the performance of the fast
aoqi@0 51 // path.
aoqi@0 52 //
aoqi@0 53 // The basic algorithm is as follows (note, see below for more details
aoqi@0 54 // and information). A pattern in the low three bits is reserved in
aoqi@0 55 // the object header to indicate whether biasing of a given object's
aoqi@0 56 // lock is currently being done or is allowed at all. If the bias
aoqi@0 57 // pattern is present, the contents of the rest of the header are
aoqi@0 58 // either the JavaThread* of the thread to which the lock is biased,
aoqi@0 59 // or NULL, indicating that the lock is "anonymously biased". The
aoqi@0 60 // first thread which locks an anonymously biased object biases the
aoqi@0 61 // lock toward that thread. If another thread subsequently attempts to
aoqi@0 62 // lock the same object, the bias is revoked.
aoqi@0 63 //
aoqi@0 64 // Because there are no updates to the object header at all during
aoqi@0 65 // recursive locking while the lock is biased, the biased lock entry
aoqi@0 66 // code is simply a test of the object header's value. If this test
aoqi@0 67 // succeeds, the lock has been acquired by the thread. If this test
aoqi@0 68 // fails, a bit test is done to see whether the bias bit is still
aoqi@0 69 // set. If not, we fall back to HotSpot's original CAS-based locking
aoqi@0 70 // scheme. If it is set, we attempt to CAS in a bias toward this
aoqi@0 71 // thread. The latter operation is expected to be the rarest operation
aoqi@0 72 // performed on these locks. We optimistically expect the biased lock
aoqi@0 73 // entry to hit most of the time, and want the CAS-based fallthrough
aoqi@0 74 // to occur quickly in the situations where the bias has been revoked.
aoqi@0 75 //
aoqi@0 76 // Revocation of the lock's bias is fairly straightforward. We want to
aoqi@0 77 // restore the object's header and stack-based BasicObjectLocks and
aoqi@0 78 // BasicLocks to the state they would have been in had the object been
aoqi@0 79 // locked by HotSpot's usual fast locking scheme. To do this, we bring
aoqi@0 80 // the system to a safepoint and walk the stack of the thread toward
aoqi@0 81 // which the lock is biased. We find all of the lock records on the
aoqi@0 82 // stack corresponding to this object, in particular the first /
aoqi@0 83 // "highest" record. We fill in the highest lock record with the
aoqi@0 84 // object's displaced header (which is a well-known value given that
aoqi@0 85 // we don't maintain an identity hash nor age bits for the object
aoqi@0 86 // while it's in the biased state) and all other lock records with 0,
aoqi@0 87 // the value for recursive locks. When the safepoint is released, the
aoqi@0 88 // formerly-biased thread and all other threads revert back to
aoqi@0 89 // HotSpot's CAS-based locking.
aoqi@0 90 //
aoqi@0 91 // This scheme can not handle transfers of biases of single objects
aoqi@0 92 // from thread to thread efficiently, but it can handle bulk transfers
aoqi@0 93 // of such biases, which is a usage pattern showing up in some
aoqi@0 94 // applications and benchmarks. We implement "bulk rebias" and "bulk
aoqi@0 95 // revoke" operations using a "bias epoch" on a per-data-type basis.
aoqi@0 96 // If too many bias revocations are occurring for a particular data
aoqi@0 97 // type, the bias epoch for the data type is incremented at a
aoqi@0 98 // safepoint, effectively meaning that all previous biases are
aoqi@0 99 // invalid. The fast path locking case checks for an invalid epoch in
aoqi@0 100 // the object header and attempts to rebias the object with a CAS if
aoqi@0 101 // found, avoiding safepoints or bulk heap sweeps (the latter which
aoqi@0 102 // was used in a prior version of this algorithm and did not scale
aoqi@0 103 // well). If too many bias revocations persist, biasing is completely
aoqi@0 104 // disabled for the data type by resetting the prototype header to the
aoqi@0 105 // unbiased markOop. The fast-path locking code checks to see whether
aoqi@0 106 // the instance's bias pattern differs from the prototype header's and
aoqi@0 107 // causes the bias to be revoked without reaching a safepoint or,
aoqi@0 108 // again, a bulk heap sweep.
aoqi@0 109
aoqi@0 110 // Biased locking counters
aoqi@0 111 class BiasedLockingCounters VALUE_OBJ_CLASS_SPEC {
aoqi@0 112 private:
aoqi@0 113 int _total_entry_count;
aoqi@0 114 int _biased_lock_entry_count;
aoqi@0 115 int _anonymously_biased_lock_entry_count;
aoqi@0 116 int _rebiased_lock_entry_count;
aoqi@0 117 int _revoked_lock_entry_count;
aoqi@0 118 int _fast_path_entry_count;
aoqi@0 119 int _slow_path_entry_count;
aoqi@0 120
aoqi@0 121 public:
aoqi@0 122 BiasedLockingCounters() :
aoqi@0 123 _total_entry_count(0),
aoqi@0 124 _biased_lock_entry_count(0),
aoqi@0 125 _anonymously_biased_lock_entry_count(0),
aoqi@0 126 _rebiased_lock_entry_count(0),
aoqi@0 127 _revoked_lock_entry_count(0),
aoqi@0 128 _fast_path_entry_count(0),
aoqi@0 129 _slow_path_entry_count(0) {}
aoqi@0 130
aoqi@0 131 int slow_path_entry_count(); // Compute this field if necessary
aoqi@0 132
aoqi@0 133 int* total_entry_count_addr() { return &_total_entry_count; }
aoqi@0 134 int* biased_lock_entry_count_addr() { return &_biased_lock_entry_count; }
aoqi@0 135 int* anonymously_biased_lock_entry_count_addr() { return &_anonymously_biased_lock_entry_count; }
aoqi@0 136 int* rebiased_lock_entry_count_addr() { return &_rebiased_lock_entry_count; }
aoqi@0 137 int* revoked_lock_entry_count_addr() { return &_revoked_lock_entry_count; }
aoqi@0 138 int* fast_path_entry_count_addr() { return &_fast_path_entry_count; }
aoqi@0 139 int* slow_path_entry_count_addr() { return &_slow_path_entry_count; }
aoqi@0 140
aoqi@0 141 bool nonzero() { return _total_entry_count > 0; }
aoqi@0 142
aoqi@0 143 void print_on(outputStream* st);
aoqi@0 144 void print() { print_on(tty); }
aoqi@0 145 };
aoqi@0 146
aoqi@0 147
aoqi@0 148 class BiasedLocking : AllStatic {
aoqi@0 149 private:
aoqi@0 150 static BiasedLockingCounters _counters;
aoqi@0 151
aoqi@0 152 public:
aoqi@0 153 static int* total_entry_count_addr();
aoqi@0 154 static int* biased_lock_entry_count_addr();
aoqi@0 155 static int* anonymously_biased_lock_entry_count_addr();
aoqi@0 156 static int* rebiased_lock_entry_count_addr();
aoqi@0 157 static int* revoked_lock_entry_count_addr();
aoqi@0 158 static int* fast_path_entry_count_addr();
aoqi@0 159 static int* slow_path_entry_count_addr();
aoqi@0 160
aoqi@0 161 enum Condition {
aoqi@0 162 NOT_BIASED = 1,
aoqi@0 163 BIAS_REVOKED = 2,
aoqi@0 164 BIAS_REVOKED_AND_REBIASED = 3
aoqi@0 165 };
aoqi@0 166
aoqi@0 167 // This initialization routine should only be called once and
aoqi@0 168 // schedules a PeriodicTask to turn on biased locking a few seconds
aoqi@0 169 // into the VM run to avoid startup time regressions
aoqi@0 170 static void init();
aoqi@0 171
aoqi@0 172 // This provides a global switch for leaving biased locking disabled
aoqi@0 173 // for the first part of a run and enabling it later
aoqi@0 174 static bool enabled();
aoqi@0 175
aoqi@0 176 // This should be called by JavaThreads to revoke the bias of an object
aoqi@0 177 static Condition revoke_and_rebias(Handle obj, bool attempt_rebias, TRAPS);
aoqi@0 178
aoqi@0 179 // These do not allow rebiasing; they are used by deoptimization to
aoqi@0 180 // ensure that monitors on the stack can be migrated
aoqi@0 181 static void revoke(GrowableArray<Handle>* objs);
aoqi@0 182 static void revoke_at_safepoint(Handle obj);
aoqi@0 183 static void revoke_at_safepoint(GrowableArray<Handle>* objs);
aoqi@0 184
aoqi@0 185 static void print_counters() { _counters.print(); }
aoqi@0 186 static BiasedLockingCounters* counters() { return &_counters; }
aoqi@0 187
aoqi@0 188 // These routines are GC-related and should not be called by end
aoqi@0 189 // users. GCs which do not do preservation of mark words do not need
aoqi@0 190 // to call these routines.
aoqi@0 191 static void preserve_marks();
aoqi@0 192 static void restore_marks();
aoqi@0 193 };
aoqi@0 194
aoqi@0 195 #endif // SHARE_VM_RUNTIME_BIASEDLOCKING_HPP

mercurial