- Notifications
You must be signed in to change notification settings - Fork 5.8k
/
Copy pathoopMapCache.cpp
626 lines (528 loc) · 20.2 KB
/
oopMapCache.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
/*
* Copyright (c) 1997, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*
*/
#include"interpreter/bytecodeStream.hpp"
#include"interpreter/oopMapCache.hpp"
#include"logging/log.hpp"
#include"logging/logStream.hpp"
#include"memory/allocation.inline.hpp"
#include"memory/resourceArea.hpp"
#include"oops/generateOopMap.hpp"
#include"oops/oop.inline.hpp"
#include"runtime/atomic.hpp"
#include"runtime/handles.inline.hpp"
#include"runtime/safepoint.hpp"
#include"runtime/signature.hpp"
#include"utilities/globalCounter.inline.hpp"
classOopMapCacheEntry: privateInterpreterOopMap {
friendclassInterpreterOopMap;
friendclassOopMapForCacheEntry;
friendclassOopMapCache;
friendclassVerifyClosure;
private:
OopMapCacheEntry* _next;
protected:
// Initialization
voidfill(const methodHandle& method, int bci);
// fills the bit mask for native calls
voidfill_for_native(const methodHandle& method);
voidset_mask(CellTypeState* vars, CellTypeState* stack, int stack_top);
// Deallocate bit masks and initialize fields
voidflush();
staticvoiddeallocate(OopMapCacheEntry* const entry);
private:
voidallocate_bit_mask(); // allocates the bit mask on C heap f necessary
voiddeallocate_bit_mask(); // allocates the bit mask on C heap f necessary
boolverify_mask(CellTypeState *vars, CellTypeState *stack, int max_locals, int stack_top);
public:
OopMapCacheEntry() : InterpreterOopMap() {
_next = nullptr;
}
};
// Implementation of OopMapForCacheEntry
// (subclass of GenerateOopMap, initializes an OopMapCacheEntry for a given method and bci)
classOopMapForCacheEntry: publicGenerateOopMap {
OopMapCacheEntry *_entry;
int _bci;
int _stack_top;
virtualboolreport_results() const { returnfalse; }
virtualboolpossible_gc_point (BytecodeStream *bcs);
virtualvoidfill_stackmap_prolog (int nof_gc_points);
virtualvoidfill_stackmap_epilog ();
virtualvoidfill_stackmap_for_opcodes (BytecodeStream *bcs,
CellTypeState* vars,
CellTypeState* stack,
int stack_top);
virtualvoidfill_init_vars (GrowableArray<intptr_t> *init_vars);
public:
OopMapForCacheEntry(const methodHandle& method, int bci, OopMapCacheEntry *entry);
// Computes stack map for (method,bci) and initialize entry
boolcompute_map(Thread* current);
intsize();
};
OopMapForCacheEntry::OopMapForCacheEntry(const methodHandle& method, int bci, OopMapCacheEntry* entry) : GenerateOopMap(method) {
_bci = bci;
_entry = entry;
_stack_top = -1;
}
boolOopMapForCacheEntry::compute_map(Thread* current) {
assert(!method()->is_native(), "cannot compute oop map for native methods");
// First check if it is a method where the stackmap is always empty
if (method()->code_size() == 0 || method()->max_locals() + method()->max_stack() == 0) {
_entry->set_mask_size(0);
} else {
ResourceMark rm;
if (!GenerateOopMap::compute_map(current)) {
fatal("Unrecoverable verification or out-of-memory error");
returnfalse;
}
result_for_basicblock(_bci);
}
returntrue;
}
boolOopMapForCacheEntry::possible_gc_point(BytecodeStream *bcs) {
returnfalse; // We are not reporting any result. We call result_for_basicblock directly
}
voidOopMapForCacheEntry::fill_stackmap_prolog(int nof_gc_points) {
// Do nothing
}
voidOopMapForCacheEntry::fill_stackmap_epilog() {
// Do nothing
}
voidOopMapForCacheEntry::fill_init_vars(GrowableArray<intptr_t> *init_vars) {
// Do nothing
}
voidOopMapForCacheEntry::fill_stackmap_for_opcodes(BytecodeStream *bcs,
CellTypeState* vars,
CellTypeState* stack,
int stack_top) {
// Only interested in one specific bci
if (bcs->bci() == _bci) {
_entry->set_mask(vars, stack, stack_top);
_stack_top = stack_top;
}
}
intOopMapForCacheEntry::size() {
assert(_stack_top != -1, "compute_map must be called first");
return ((method()->is_static()) ? 0 : 1) + method()->max_locals() + _stack_top;
}
// Implementation of InterpreterOopMap and OopMapCacheEntry
classVerifyClosure : publicOffsetClosure {
private:
OopMapCacheEntry* _entry;
bool _failed;
public:
VerifyClosure(OopMapCacheEntry* entry) { _entry = entry; _failed = false; }
voidoffset_do(int offset) { if (!_entry->is_oop(offset)) _failed = true; }
boolfailed() const { return _failed; }
};
InterpreterOopMap::InterpreterOopMap() {
initialize();
}
InterpreterOopMap::~InterpreterOopMap() {
if (has_valid_mask() && mask_size() > small_mask_limit) {
assert(_bit_mask[0] != 0, "should have pointer to C heap");
FREE_C_HEAP_ARRAY(uintptr_t, _bit_mask[0]);
}
}
boolInterpreterOopMap::is_empty() const {
bool result = _method == nullptr;
assert(_method != nullptr || (_bci == 0 &&
(_mask_size == 0 || _mask_size == USHRT_MAX) &&
_bit_mask[0] == 0), "Should be completely empty");
return result;
}
voidInterpreterOopMap::initialize() {
_method = nullptr;
_mask_size = USHRT_MAX; // This value should cause a failure quickly
_bci = 0;
_expression_stack_size = 0;
_num_oops = 0;
for (int i = 0; i < N; i++) _bit_mask[i] = 0;
}
voidInterpreterOopMap::iterate_oop(OffsetClosure* oop_closure) const {
int n = number_of_entries();
int word_index = 0;
uintptr_t value = 0;
uintptr_t mask = 0;
// iterate over entries
for (int i = 0; i < n; i++, mask <<= bits_per_entry) {
// get current word
if (mask == 0) {
value = bit_mask()[word_index++];
mask = 1;
}
// test for oop
if ((value & (mask << oop_bit_number)) != 0) oop_closure->offset_do(i);
}
}
voidInterpreterOopMap::print() const {
int n = number_of_entries();
tty->print("oop map for ");
method()->print_value();
tty->print(" @ %d = [%d] { ", bci(), n);
for (int i = 0; i < n; i++) {
if (is_dead(i)) tty->print("%d+ ", i);
else
if (is_oop(i)) tty->print("%d ", i);
}
tty->print_cr("}");
}
classMaskFillerForNative: publicNativeSignatureIterator {
private:
uintptr_t * _mask; // the bit mask to be filled
int _size; // the mask size in bits
int _num_oops;
voidset_one(int i) {
_num_oops++;
i *= InterpreterOopMap::bits_per_entry;
assert(0 <= i && i < _size, "offset out of bounds");
_mask[i / BitsPerWord] |= (((uintptr_t) 1 << InterpreterOopMap::oop_bit_number) << (i % BitsPerWord));
}
public:
voidpass_byte() { /* ignore */ }
voidpass_short() { /* ignore */ }
voidpass_int() { /* ignore */ }
voidpass_long() { /* ignore */ }
voidpass_float() { /* ignore */ }
voidpass_double() { /* ignore */ }
voidpass_object() { set_one(offset()); }
MaskFillerForNative(const methodHandle& method, uintptr_t* mask, int size) : NativeSignatureIterator(method) {
_mask = mask;
_size = size;
_num_oops = 0;
// initialize with 0
int i = (size + BitsPerWord - 1) / BitsPerWord;
while (i-- > 0) _mask[i] = 0;
}
voidgenerate() {
iterate();
}
intnum_oops() { return _num_oops; }
};
boolOopMapCacheEntry::verify_mask(CellTypeState* vars, CellTypeState* stack, int max_locals, int stack_top) {
// Check mask includes map
VerifyClosure blk(this);
iterate_oop(&blk);
if (blk.failed()) returnfalse;
// Check if map is generated correctly
// (Use ?: operator to make sure all 'true' & 'false' are represented exactly the same so we can use == afterwards)
constboollog = log_is_enabled(Trace, interpreter, oopmap);
LogStream st(Log(interpreter, oopmap)::trace());
if (log) st.print("Locals (%d): ", max_locals);
for(int i = 0; i < max_locals; i++) {
bool v1 = is_oop(i) ? true : false;
bool v2 = vars[i].is_reference() ? true : false;
assert(v1 == v2, "locals oop mask generation error");
if (log) st.print("%d", v1 ? 1 : 0);
}
if (log) st.cr();
if (log) st.print("Stack (%d): ", stack_top);
for(int j = 0; j < stack_top; j++) {
bool v1 = is_oop(max_locals + j) ? true : false;
bool v2 = stack[j].is_reference() ? true : false;
assert(v1 == v2, "stack oop mask generation error");
if (log) st.print("%d", v1 ? 1 : 0);
}
if (log) st.cr();
returntrue;
}
voidOopMapCacheEntry::allocate_bit_mask() {
if (mask_size() > small_mask_limit) {
assert(_bit_mask[0] == 0, "bit mask should be new or just flushed");
_bit_mask[0] = (intptr_t)
NEW_C_HEAP_ARRAY(uintptr_t, mask_word_size(), mtClass);
}
}
voidOopMapCacheEntry::deallocate_bit_mask() {
if (mask_size() > small_mask_limit && _bit_mask[0] != 0) {
assert(!Thread::current()->resource_area()->contains((void*)_bit_mask[0]),
"This bit mask should not be in the resource area");
FREE_C_HEAP_ARRAY(uintptr_t, _bit_mask[0]);
DEBUG_ONLY(_bit_mask[0] = 0;)
}
}
voidOopMapCacheEntry::fill_for_native(const methodHandle& mh) {
assert(mh->is_native(), "method must be native method");
set_mask_size(mh->size_of_parameters() * bits_per_entry);
allocate_bit_mask();
// fill mask for parameters
MaskFillerForNative mf(mh, bit_mask(), mask_size());
mf.generate();
_num_oops = mf.num_oops();
}
voidOopMapCacheEntry::fill(const methodHandle& method, int bci) {
// Flush entry to deallocate an existing entry
flush();
set_method(method());
set_bci(checked_cast<unsignedshort>(bci)); // bci is always u2
if (method->is_native()) {
// Native method activations have oops only among the parameters and one
// extra oop following the parameters (the mirror for static native methods).
fill_for_native(method);
} else {
OopMapForCacheEntry gen(method, bci, this);
if (!gen.compute_map(Thread::current())) {
fatal("Unrecoverable verification or out-of-memory error");
}
}
}
voidOopMapCacheEntry::set_mask(CellTypeState *vars, CellTypeState *stack, int stack_top) {
// compute bit mask size
int max_locals = method()->max_locals();
int n_entries = max_locals + stack_top;
set_mask_size(n_entries * bits_per_entry);
allocate_bit_mask();
set_expression_stack_size(stack_top);
// compute bits
int word_index = 0;
uintptr_t value = 0;
uintptr_t mask = 1;
_num_oops = 0;
CellTypeState* cell = vars;
for (int entry_index = 0; entry_index < n_entries; entry_index++, mask <<= bits_per_entry, cell++) {
// store last word
if (mask == 0) {
bit_mask()[word_index++] = value;
value = 0;
mask = 1;
}
// switch to stack when done with locals
if (entry_index == max_locals) {
cell = stack;
}
// set oop bit
if ( cell->is_reference()) {
value |= (mask << oop_bit_number );
_num_oops++;
}
// set dead bit
if (!cell->is_live()) {
value |= (mask << dead_bit_number);
assert(!cell->is_reference(), "dead value marked as oop");
}
}
// make sure last word is stored
bit_mask()[word_index] = value;
// verify bit mask
assert(verify_mask(vars, stack, max_locals, stack_top), "mask could not be verified");
}
voidOopMapCacheEntry::flush() {
deallocate_bit_mask();
initialize();
}
voidOopMapCacheEntry::deallocate(OopMapCacheEntry* const entry) {
entry->flush();
FREE_C_HEAP_OBJ(entry);
}
// Implementation of OopMapCache
voidInterpreterOopMap::copy_from(const OopMapCacheEntry* src) {
// The expectation is that this InterpreterOopMap is recently created
// and empty. It is used to get a copy of a cached entry.
assert(!has_valid_mask(), "InterpreterOopMap object can only be filled once");
assert(src->has_valid_mask(), "Cannot copy entry with an invalid mask");
set_method(src->method());
set_bci(src->bci());
set_mask_size(src->mask_size());
set_expression_stack_size(src->expression_stack_size());
_num_oops = src->num_oops();
// Is the bit mask contained in the entry?
if (src->mask_size() <= small_mask_limit) {
memcpy(_bit_mask, src->_bit_mask, mask_word_size() * BytesPerWord);
} else {
_bit_mask[0] = (uintptr_t) NEW_C_HEAP_ARRAY(uintptr_t, mask_word_size(), mtClass);
memcpy((void*) _bit_mask[0], (void*) src->_bit_mask[0], mask_word_size() * BytesPerWord);
}
}
inlineunsignedintOopMapCache::hash_value_for(const methodHandle& method, int bci) const {
// We use method->code_size() rather than method->identity_hash() below since
// the mark may not be present if a pointer to the method is already reversed.
return ((unsignedint) bci)
^ ((unsignedint) method->max_locals() << 2)
^ ((unsignedint) method->code_size() << 4)
^ ((unsignedint) method->size_of_parameters() << 6);
}
OopMapCacheEntry* volatile OopMapCache::_old_entries = nullptr;
OopMapCache::OopMapCache() {
for(int i = 0; i < size; i++) _array[i] = nullptr;
}
OopMapCache::~OopMapCache() {
// Deallocate oop maps that are allocated out-of-line
flush();
}
OopMapCacheEntry* OopMapCache::entry_at(int i) const {
returnAtomic::load_acquire(&(_array[i % size]));
}
boolOopMapCache::put_at(int i, OopMapCacheEntry* entry, OopMapCacheEntry* old) {
returnAtomic::cmpxchg(&_array[i % size], old, entry) == old;
}
voidOopMapCache::flush() {
for (int i = 0; i < size; i++) {
OopMapCacheEntry* entry = _array[i];
if (entry != nullptr) {
_array[i] = nullptr; // no barrier, only called in OopMapCache destructor
OopMapCacheEntry::deallocate(entry);
}
}
}
voidOopMapCache::flush_obsolete_entries() {
assert(SafepointSynchronize::is_at_safepoint(), "called by RedefineClasses in a safepoint");
for (int i = 0; i < size; i++) {
OopMapCacheEntry* entry = _array[i];
if (entry != nullptr && !entry->is_empty() && entry->method()->is_old()) {
// Cache entry is occupied by an old redefined method and we don't want
// to pin it down so flush the entry.
if (log_is_enabled(Debug, redefine, class, oopmap)) {
ResourceMark rm;
log_debug(redefine, class, interpreter, oopmap)
("flush: %s(%s): cached entry @%d",
entry->method()->name()->as_C_string(), entry->method()->signature()->as_C_string(), i);
}
_array[i] = nullptr;
OopMapCacheEntry::deallocate(entry);
}
}
}
// Lookup or compute/cache the entry.
voidOopMapCache::lookup(const methodHandle& method,
int bci,
InterpreterOopMap* entry_for) {
int probe = hash_value_for(method, bci);
if (log_is_enabled(Debug, interpreter, oopmap)) {
staticint count = 0;
ResourceMark rm;
log_debug(interpreter, oopmap)
("%d - Computing oopmap at bci %d for %s at hash %d", ++count, bci,
method()->name_and_sig_as_C_string(), probe);
}
// Search hashtable for match.
// Need a critical section to avoid race against concurrent reclamation.
{
GlobalCounter::CriticalSection cs(Thread::current());
for (int i = 0; i < probe_depth; i++) {
OopMapCacheEntry *entry = entry_at(probe + i);
if (entry != nullptr && !entry->is_empty() && entry->match(method, bci)) {
entry_for->copy_from(entry);
assert(!entry_for->is_empty(), "A non-empty oop map should be returned");
log_debug(interpreter, oopmap)("- found at hash %d", probe + i);
return;
}
}
}
// Entry is not in hashtable.
// Compute entry
OopMapCacheEntry* tmp = NEW_C_HEAP_OBJ(OopMapCacheEntry, mtClass);
tmp->initialize();
tmp->fill(method, bci);
entry_for->copy_from(tmp);
if (method->should_not_be_cached()) {
// It is either not safe or not a good idea to cache this Method*
// at this time. We give the caller of lookup() a copy of the
// interesting info via parameter entry_for, but we don't add it to
// the cache. See the gory details in Method*.cpp.
OopMapCacheEntry::deallocate(tmp);
return;
}
// First search for an empty slot
for (int i = 0; i < probe_depth; i++) {
OopMapCacheEntry* entry = entry_at(probe + i);
if (entry == nullptr) {
if (put_at(probe + i, tmp, nullptr)) {
assert(!entry_for->is_empty(), "A non-empty oop map should be returned");
return;
}
}
}
log_debug(interpreter, oopmap)("*** collision in oopmap cache - flushing item ***");
// No empty slot (uncommon case). Use (some approximation of a) LRU algorithm
// where the first entry in the collision array is replaced with the new one.
OopMapCacheEntry* old = entry_at(probe + 0);
if (put_at(probe + 0, tmp, old)) {
// Cannot deallocate old entry on the spot: it can still be used by readers
// that got a reference to it before we were able to replace it in the map.
// Instead of synchronizing on GlobalCounter here and incurring heavy thread
// walk, we do this clean up out of band.
enqueue_for_cleanup(old);
} else {
OopMapCacheEntry::deallocate(tmp);
}
assert(!entry_for->is_empty(), "A non-empty oop map should be returned");
return;
}
voidOopMapCache::enqueue_for_cleanup(OopMapCacheEntry* entry) {
while (true) {
OopMapCacheEntry* head = Atomic::load(&_old_entries);
entry->_next = head;
if (Atomic::cmpxchg(&_old_entries, head, entry) == head) {
// Enqueued successfully.
break;
}
}
if (log_is_enabled(Debug, interpreter, oopmap)) {
ResourceMark rm;
log_debug(interpreter, oopmap)("enqueue %s at bci %d for cleanup",
entry->method()->name_and_sig_as_C_string(), entry->bci());
}
}
boolOopMapCache::has_cleanup_work() {
returnAtomic::load(&_old_entries) != nullptr;
}
voidOopMapCache::try_trigger_cleanup() {
// See we can take the lock for the notification without blocking.
// This allows triggering the cleanup from GC paths, that can hold
// the service lock for e.g. oop iteration in service thread.
if (has_cleanup_work() && Service_lock->try_lock_without_rank_check()) {
Service_lock->notify_all();
Service_lock->unlock();
}
}
voidOopMapCache::cleanup() {
OopMapCacheEntry* entry = Atomic::xchg(&_old_entries, (OopMapCacheEntry*)nullptr);
if (entry == nullptr) {
// No work.
return;
}
// About to delete the entries than might still be accessed by other threads
// on lookup path. Need to sync up with them before proceeding.
GlobalCounter::write_synchronize();
while (entry != nullptr) {
if (log_is_enabled(Debug, interpreter, oopmap)) {
ResourceMark rm;
log_debug(interpreter, oopmap)("cleanup entry %s at bci %d",
entry->method()->name_and_sig_as_C_string(), entry->bci());
}
OopMapCacheEntry* next = entry->_next;
OopMapCacheEntry::deallocate(entry);
entry = next;
}
}
voidOopMapCache::compute_one_oop_map(const methodHandle& method, int bci, InterpreterOopMap* entry) {
// Due to the invariants above it's tricky to allocate a temporary OopMapCacheEntry on the stack
OopMapCacheEntry* tmp = NEW_C_HEAP_OBJ(OopMapCacheEntry, mtClass);
tmp->initialize();
tmp->fill(method, bci);
if (tmp->has_valid_mask()) {
entry->copy_from(tmp);
}
OopMapCacheEntry::deallocate(tmp);
}