1 /*
2 * Copyright 1997-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 // The markOop describes the header of an object.
26 //
27 // Note that the mark is not a real oop but just a word.
28 // It is placed in the oop hierarchy for historical reasons.
29 //
30 // Bit-format of an object header (most significant first):
31 //
32 //
33 // 32 bits: hash:25/31 age:4 biased_lock:1 lock:2
34 // 64 bits: unused:0/23 hash:27/33 cms:2 age:4 biased_lock:1 lock:2
35 //
36 // - hash contains the identity hash value: largest value is
37 // 31 bits, see os::random(). Also, 64-bit vm's require
38 // a hash value no bigger than 32 bits because they will not
39 // properly generate a mask larger than that: see library_call.cpp
40 // and c1_CodePatterns_sparc.cpp.
41 //
42 // - the biased lock pattern is used to bias a lock toward a given
43 // thread. When this pattern is set in the low three bits, the lock
44 // is either biased toward a given thread or "anonymously" biased,
45 // indicating that it is possible for it to be biased. When the
46 // lock is biased toward a given thread, locking and unlocking can
47 // be performed by that thread without using atomic operations.
48 // When a lock's bias is revoked, it reverts back to the normal
49 // locking scheme described below.
50 //
51 // Note that we are overloading the meaning of the "unlocked" state
52 // of the header. Because we steal a bit from the age we can
53 // guarantee that the bias pattern will never be seen for a truly
54 // unlocked object.
55 //
56 // Note also that the biased state contains the age bits normally
57 // contained in the object header. Large increases in scavenge
58 // times were seen when these bits were absent and an arbitrary age
59 // assigned to all biased objects, because they tended to consume a
60 // significant fraction of the eden semispaces and were not
61 // promoted promptly, causing an increase in the amount of copying
62 // performed. The runtime system aligns all JavaThread* pointers to
63 // a very large value (currently 128 bytes) to make room for the
64 // age bits when biased locking is enabled.
65 //
66 // [JavaThread* | epoch | age | 1 | 01] lock is biased toward given thread
67 // [0 | epoch | age | 1 | 01] lock is anonymously biased
68 //
69 // - the two lock bits are used to describe three states: locked/unlocked and monitor.
70 //
71 // [ptr | 00] locked ptr points to real header on stack
72 // [header | 0 | 01] unlocked regular object header
73 // [ptr | 10] monitor inflated lock (header is wapped out)
74 // [ptr | 11] marked used by markSweep to mark an object
75 // not valid at any other time
76 //
77 // We assume that stack/thread pointers have the lowest two bits cleared.
78
79 class BasicLock;
80 class ObjectMonitor;
81 class JavaThread;
82
83 class markOopDesc: public oopDesc {
84 private:
85 // Conversion
86 uintptr_t value() const { return (uintptr_t) this; }
87
88 public:
89 // Constants
90 enum { age_bits = 4,
91 lock_bits = 2,
92 biased_lock_bits = 1,
93 max_hash_bits = BitsPerWord - age_bits - lock_bits - biased_lock_bits,
94 hash_bits = max_hash_bits > 31 ? 31 : max_hash_bits,
95 cms_bits = LP64_ONLY(2) NOT_LP64(0),
96 epoch_bits = 2
97 };
98
99 // The biased locking code currently requires that the age bits be
100 // contiguous to the lock bits. Class data sharing would prefer the
101 // hash bits to be lower down to provide more random hash codes for
102 // shared read-only symbolOop objects, because these objects' mark
103 // words are set to their own address with marked_value in the lock
104 // bit, and using lower bits would make their identity hash values
105 // more random. However, the performance decision was made in favor
106 // of the biased locking code.
107
108 enum { lock_shift = 0,
109 biased_lock_shift = lock_bits,
110 age_shift = lock_bits + biased_lock_bits,
111 cms_shift = age_shift + age_bits,
112 hash_shift = cms_shift + cms_bits,
113 epoch_shift = hash_shift
114 };
115
116 enum { lock_mask = right_n_bits(lock_bits),
117 lock_mask_in_place = lock_mask << lock_shift,
118 biased_lock_mask = right_n_bits(lock_bits + biased_lock_bits),
119 biased_lock_mask_in_place= biased_lock_mask << lock_shift,
120 biased_lock_bit_in_place = 1 << biased_lock_shift,
121 age_mask = right_n_bits(age_bits),
122 age_mask_in_place = age_mask << age_shift,
123 epoch_mask = right_n_bits(epoch_bits),
124 epoch_mask_in_place = epoch_mask << epoch_shift,
125 cms_mask = right_n_bits(cms_bits),
126 cms_mask_in_place = cms_mask << cms_shift
127 #ifndef _WIN64
128 ,hash_mask = right_n_bits(hash_bits),
129 hash_mask_in_place = (address_word)hash_mask << hash_shift
130 #endif
131 };
132
133 // Alignment of JavaThread pointers encoded in object header required by biased locking
134 enum { biased_lock_alignment = 2 << (epoch_shift + epoch_bits)
135 };
136
137 #ifdef _WIN64
138 // These values are too big for Win64
139 const static uintptr_t hash_mask = right_n_bits(hash_bits);
140 const static uintptr_t hash_mask_in_place =
141 (address_word)hash_mask << hash_shift;
142 #endif
143
144 enum { locked_value = 0,
145 unlocked_value = 1,
146 monitor_value = 2,
147 marked_value = 3,
148 biased_lock_pattern = 5
149 };
150
151 enum { no_hash = 0 }; // no hash value assigned
152
153 enum { no_hash_in_place = (address_word)no_hash << hash_shift,
154 no_lock_in_place = unlocked_value
155 };
156
157 enum { max_age = age_mask };
158
159 enum { max_bias_epoch = epoch_mask };
160
161 // Biased Locking accessors.
162 // These must be checked by all code which calls into the
163 // ObjectSynchronizer and other code. The biasing is not understood
164 // by the lower-level CAS-based locking code, although the runtime
165 // fixes up biased locks to be compatible with it when a bias is
166 // revoked.
167 bool has_bias_pattern() const {
168 return (mask_bits(value(), biased_lock_mask_in_place) == biased_lock_pattern);
169 }
170 JavaThread* biased_locker() const {
171 assert(has_bias_pattern(), "should not call this otherwise");
172 return (JavaThread*) ((intptr_t) (mask_bits(value(), ~(biased_lock_mask_in_place | age_mask_in_place | epoch_mask_in_place))));
173 }
174 // Indicates that the mark has the bias bit set but that it has not
175 // yet been biased toward a particular thread
176 bool is_biased_anonymously() const {
177 return (has_bias_pattern() && (biased_locker() == NULL));
178 }
179 // Indicates epoch in which this bias was acquired. If the epoch
180 // changes due to too many bias revocations occurring, the biases
181 // from the previous epochs are all considered invalid.
182 int bias_epoch() const {
183 assert(has_bias_pattern(), "should not call this otherwise");
184 return (mask_bits(value(), epoch_mask_in_place) >> epoch_shift);
185 }
186 markOop set_bias_epoch(int epoch) {
187 assert(has_bias_pattern(), "should not call this otherwise");
188 assert((epoch & (~epoch_mask)) == 0, "epoch overflow");
189 return markOop(mask_bits(value(), ~epoch_mask_in_place) | (epoch << epoch_shift));
190 }
191 markOop incr_bias_epoch() {
192 return set_bias_epoch((1 + bias_epoch()) & epoch_mask);
193 }
194 // Prototype mark for initialization
195 static markOop biased_locking_prototype() {
196 return markOop( biased_lock_pattern );
197 }
198
199 // lock accessors (note that these assume lock_shift == 0)
200 bool is_locked() const {
201 return (mask_bits(value(), lock_mask_in_place) != unlocked_value);
202 }
203 bool is_unlocked() const {
204 return (mask_bits(value(), biased_lock_mask_in_place) == unlocked_value);
205 }
206 bool is_marked() const {
207 return (mask_bits(value(), lock_mask_in_place) == marked_value);
208 }
209 bool is_neutral() const { return (mask_bits(value(), biased_lock_mask_in_place) == unlocked_value); }
210
211 // Special temporary state of the markOop while being inflated.
212 // Code that looks at mark outside a lock need to take this into account.
213 bool is_being_inflated() const { return (value() == 0); }
214
215 // Distinguished markword value - used when inflating over
216 // an existing stacklock. 0 indicates the markword is "BUSY".
217 // Lockword mutators that use a LD...CAS idiom should always
218 // check for and avoid overwriting a 0 value installed by some
219 // other thread. (They should spin or block instead. The 0 value
220 // is transient and *should* be short-lived).
221 static markOop INFLATING() { return (markOop) 0; } // inflate-in-progress
222
223 // Should this header be preserved during GC?
224 bool must_be_preserved(oop obj_containing_mark) const {
225 if (!UseBiasedLocking)
226 return (!is_unlocked() || !has_no_hash());
227 return must_be_preserved_with_bias(obj_containing_mark);
228 }
229 inline bool must_be_preserved_with_bias(oop obj_containing_mark) const;
230
231 // Should this header (including its age bits) be preserved in the
232 // case of a promotion failure during scavenge?
233 // Note that we special case this situation. We want to avoid
234 // calling BiasedLocking::preserve_marks()/restore_marks() (which
235 // decrease the number of mark words that need to be preserved
236 // during GC) during each scavenge. During scavenges in which there
237 // is no promotion failure, we actually don't need to call the above
238 // routines at all, since we don't mutate and re-initialize the
239 // marks of promoted objects using init_mark(). However, during
240 // scavenges which result in promotion failure, we do re-initialize
241 // the mark words of objects, meaning that we should have called
242 // these mark word preservation routines. Currently there's no good
243 // place in which to call them in any of the scavengers (although
244 // guarded by appropriate locks we could make one), but the
245 // observation is that promotion failures are quite rare and
246 // reducing the number of mark words preserved during them isn't a
247 // high priority.
248 bool must_be_preserved_for_promotion_failure(oop obj_containing_mark) const {
249 if (!UseBiasedLocking)
250 return (this != prototype());
251 return must_be_preserved_with_bias_for_promotion_failure(obj_containing_mark);
252 }
253 inline bool must_be_preserved_with_bias_for_promotion_failure(oop obj_containing_mark) const;
254
255 // Should this header be preserved during a scavenge where CMS is
256 // the old generation?
257 // (This is basically the same body as must_be_preserved_for_promotion_failure(),
258 // but takes the klassOop as argument instead)
259 bool must_be_preserved_for_cms_scavenge(klassOop klass_of_obj_containing_mark) const {
260 if (!UseBiasedLocking)
261 return (this != prototype());
262 return must_be_preserved_with_bias_for_cms_scavenge(klass_of_obj_containing_mark);
263 }
264 inline bool must_be_preserved_with_bias_for_cms_scavenge(klassOop klass_of_obj_containing_mark) const;
265
266 // WARNING: The following routines are used EXCLUSIVELY by
267 // synchronization functions. They are not really gc safe.
268 // They must get updated if markOop layout get changed.
269 markOop set_unlocked() const {
270 return markOop(value() | unlocked_value);
271 }
272 bool has_locker() const {
273 return ((value() & lock_mask_in_place) == locked_value);
274 }
275 BasicLock* locker() const {
276 assert(has_locker(), "check");
277 return (BasicLock*) value();
278 }
279 bool has_monitor() const {
280 return ((value() & monitor_value) != 0);
281 }
282 ObjectMonitor* monitor() const {
283 assert(has_monitor(), "check");
284 // Use xor instead of &~ to provide one extra tag-bit check.
285 return (ObjectMonitor*) (value() ^ monitor_value);
286 }
287 bool has_displaced_mark_helper() const {
288 return ((value() & unlocked_value) == 0);
289 }
290 markOop displaced_mark_helper() const {
291 assert(has_displaced_mark_helper(), "check");
292 intptr_t ptr = (value() & ~monitor_value);
293 return *(markOop*)ptr;
294 }
295 void set_displaced_mark_helper(markOop m) const {
296 assert(has_displaced_mark_helper(), "check");
297 intptr_t ptr = (value() & ~monitor_value);
298 *(markOop*)ptr = m;
299 }
300 markOop copy_set_hash(intptr_t hash) const {
301 intptr_t tmp = value() & (~hash_mask_in_place);
302 tmp |= ((hash & hash_mask) << hash_shift);
303 return (markOop)tmp;
304 }
305 // it is only used to be stored into BasicLock as the
306 // indicator that the lock is using heavyweight monitor
307 static markOop unused_mark() {
308 return (markOop) marked_value;
309 }
310 // the following two functions create the markOop to be
311 // stored into object header, it encodes monitor info
312 static markOop encode(BasicLock* lock) {
313 return (markOop) lock;
314 }
315 static markOop encode(ObjectMonitor* monitor) {
316 intptr_t tmp = (intptr_t) monitor;
317 return (markOop) (tmp | monitor_value);
318 }
319 static markOop encode(JavaThread* thread, int age, int bias_epoch) {
320 intptr_t tmp = (intptr_t) thread;
321 assert(UseBiasedLocking && ((tmp & (epoch_mask_in_place | age_mask_in_place | biased_lock_mask_in_place)) == 0), "misaligned JavaThread pointer");
322 assert(age <= max_age, "age too large");
323 assert(bias_epoch <= max_bias_epoch, "bias epoch too large");
324 return (markOop) (tmp | (bias_epoch << epoch_shift) | (age << age_shift) | biased_lock_pattern);
325 }
326
327 // used to encode pointers during GC
328 markOop clear_lock_bits() { return markOop(value() & ~lock_mask_in_place); }
329
330 // age operations
331 markOop set_marked() { return markOop((value() & ~lock_mask_in_place) | marked_value); }
332
333 int age() const { return mask_bits(value() >> age_shift, age_mask); }
334 markOop set_age(int v) const {
335 assert((v & ~age_mask) == 0, "shouldn't overflow age field");
336 return markOop((value() & ~age_mask_in_place) | (((intptr_t)v & age_mask) << age_shift));
337 }
338 markOop incr_age() const { return age() == max_age ? markOop(this) : set_age(age() + 1); }
339
340 // hash operations
341 intptr_t hash() const {
342 return mask_bits(value() >> hash_shift, hash_mask);
343 }
344
345 bool has_no_hash() const {
346 return hash() == no_hash;
347 }
348
349 // Prototype mark for initialization
350 static markOop prototype() {
351 return markOop( no_hash_in_place | no_lock_in_place );
352 }
353
354 // Helper function for restoration of unmarked mark oops during GC
355 static inline markOop prototype_for_object(oop obj);
356
357 // Debugging
358 void print_on(outputStream* st) const;
359
360 // Prepare address of oop for placement into mark
361 inline static markOop encode_pointer_as_mark(void* p) { return markOop(p)->set_marked(); }
362
363 // Recover address of oop from encoded form used in mark
364 inline void* decode_pointer() { if (UseBiasedLocking && has_bias_pattern()) return NULL; return clear_lock_bits(); }
365
366 // see the definition in markOop.cpp for the gory details
367 bool should_not_be_cached() const;
368
369 // These markOops indicate cms free chunk blocks and not objects.
370 // In 64 bit, the markOop is set to distinguish them from oops.
371 // 0x1 is free chunk and 0x3 is can't coalesce, no other bits should be
372 // set other than unlocked.
373 // These are defined in 32 bit mode for vmStructs.
374 const static uintptr_t cms_free_chunk_pattern = 0x1;
375 const static uintptr_t cms_no_coalesce_pattern = 0x3;
376
377 // Constants for the size field.
378 enum { size_shift = cms_shift + cms_bits,
379 size_bits = 35 // need for compressed oops 32G
380 };
381 // These values are too big for Win64
382 const static uintptr_t size_mask = LP64_ONLY(right_n_bits(size_bits))
383 NOT_LP64(0);
384 const static uintptr_t size_mask_in_place =
385 (address_word)size_mask << size_shift;
386
387 #ifdef _LP64
388 static markOop cms_free_prototype() {
389 return markOop(((intptr_t)prototype() & ~cms_mask_in_place) |
390 ((cms_free_chunk_pattern & cms_mask) << cms_shift));
391 }
392 markOop set_cms_no_coalesce() {
393 return markOop(((intptr_t)value() & ~cms_mask_in_place) |
394 ((cms_no_coalesce_pattern & cms_mask) << cms_shift));
395 }
396 uintptr_t cms_encoding() const {
397 return mask_bits(value() >> cms_shift, cms_mask);
398 }
399 bool is_cms_free_chunk() const {
400 return is_neutral() &&
401 (cms_encoding() & cms_free_chunk_pattern) == cms_free_chunk_pattern;
402 }
403 bool is_cms_no_coalesce() const {
404 return is_neutral() && (cms_encoding() == cms_no_coalesce_pattern);
405 }
406
407 size_t get_size() const { return (size_t)(value() >> size_shift); }
408 static markOop set_size_and_free(size_t size) {
409 assert((size & ~size_mask) == 0, "shouldn't overflow size field");
410 return markOop(((intptr_t)cms_free_prototype() & ~size_mask_in_place) |
411 (((intptr_t)size & size_mask) << size_shift));
412 }
413 #endif // _LP64
414 };