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