annotate src/share/vm/runtime/biasedLocking.hpp @ 61:5a76ab815e34

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