src/share/vm/runtime/biasedLocking.hpp

Tue, 18 Mar 2014 19:07:22 +0100

author
pliden
date
Tue, 18 Mar 2014 19:07:22 +0100
changeset 6413
595c0f60d50d
parent 2314
f95d63e2154a
child 6876
710a3c8b516e
permissions
-rw-r--r--

8029075: String deduplication in G1
Summary: Implementation of JEP 192, http://openjdk.java.net/jeps/192
Reviewed-by: brutisso, tschatzl, coleenp

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

mercurial