Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/hotspot/share/gc/shared/cardTable.cpp
40957 views
1
/*
2
* Copyright (c) 2000, 2020, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*
23
*/
24
25
#include "precompiled.hpp"
26
#include "gc/shared/cardTable.hpp"
27
#include "gc/shared/collectedHeap.hpp"
28
#include "gc/shared/space.inline.hpp"
29
#include "logging/log.hpp"
30
#include "memory/virtualspace.hpp"
31
#include "runtime/java.hpp"
32
#include "runtime/os.hpp"
33
#include "services/memTracker.hpp"
34
#include "utilities/align.hpp"
35
36
size_t CardTable::compute_byte_map_size() {
37
assert(_guard_index == cards_required(_whole_heap.word_size()) - 1,
38
"uninitialized, check declaration order");
39
assert(_page_size != 0, "uninitialized, check declaration order");
40
const size_t granularity = os::vm_allocation_granularity();
41
return align_up(_guard_index + 1, MAX2(_page_size, granularity));
42
}
43
44
CardTable::CardTable(MemRegion whole_heap) :
45
_whole_heap(whole_heap),
46
_guard_index(0),
47
_last_valid_index(0),
48
_page_size(os::vm_page_size()),
49
_byte_map_size(0),
50
_byte_map(NULL),
51
_byte_map_base(NULL),
52
_cur_covered_regions(0),
53
_covered(MemRegion::create_array(_max_covered_regions, mtGC)),
54
_committed(MemRegion::create_array(_max_covered_regions, mtGC)),
55
_guard_region()
56
{
57
assert((uintptr_t(_whole_heap.start()) & (card_size - 1)) == 0, "heap must start at card boundary");
58
assert((uintptr_t(_whole_heap.end()) & (card_size - 1)) == 0, "heap must end at card boundary");
59
60
assert(card_size <= 512, "card_size must be less than 512"); // why?
61
}
62
63
CardTable::~CardTable() {
64
MemRegion::destroy_array(_covered, _max_covered_regions);
65
MemRegion::destroy_array(_committed, _max_covered_regions);
66
}
67
68
void CardTable::initialize() {
69
_guard_index = cards_required(_whole_heap.word_size()) - 1;
70
_last_valid_index = _guard_index - 1;
71
72
_byte_map_size = compute_byte_map_size();
73
74
HeapWord* low_bound = _whole_heap.start();
75
HeapWord* high_bound = _whole_heap.end();
76
77
_cur_covered_regions = 0;
78
79
const size_t rs_align = _page_size == (size_t) os::vm_page_size() ? 0 :
80
MAX2(_page_size, (size_t) os::vm_allocation_granularity());
81
ReservedSpace heap_rs(_byte_map_size, rs_align, _page_size);
82
83
MemTracker::record_virtual_memory_type((address)heap_rs.base(), mtGC);
84
85
os::trace_page_sizes("Card Table", _guard_index + 1, _guard_index + 1,
86
_page_size, heap_rs.base(), heap_rs.size());
87
if (!heap_rs.is_reserved()) {
88
vm_exit_during_initialization("Could not reserve enough space for the "
89
"card marking array");
90
}
91
92
// The assembler store_check code will do an unsigned shift of the oop,
93
// then add it to _byte_map_base, i.e.
94
//
95
// _byte_map = _byte_map_base + (uintptr_t(low_bound) >> card_shift)
96
_byte_map = (CardValue*) heap_rs.base();
97
_byte_map_base = _byte_map - (uintptr_t(low_bound) >> card_shift);
98
assert(byte_for(low_bound) == &_byte_map[0], "Checking start of map");
99
assert(byte_for(high_bound-1) <= &_byte_map[_last_valid_index], "Checking end of map");
100
101
CardValue* guard_card = &_byte_map[_guard_index];
102
HeapWord* guard_page = align_down((HeapWord*)guard_card, _page_size);
103
_guard_region = MemRegion(guard_page, _page_size);
104
os::commit_memory_or_exit((char*)guard_page, _page_size, _page_size,
105
!ExecMem, "card table last card");
106
*guard_card = last_card;
107
108
log_trace(gc, barrier)("CardTable::CardTable: ");
109
log_trace(gc, barrier)(" &_byte_map[0]: " INTPTR_FORMAT " &_byte_map[_last_valid_index]: " INTPTR_FORMAT,
110
p2i(&_byte_map[0]), p2i(&_byte_map[_last_valid_index]));
111
log_trace(gc, barrier)(" _byte_map_base: " INTPTR_FORMAT, p2i(_byte_map_base));
112
}
113
114
int CardTable::find_covering_region_by_base(HeapWord* base) {
115
int i;
116
for (i = 0; i < _cur_covered_regions; i++) {
117
if (_covered[i].start() == base) return i;
118
if (_covered[i].start() > base) break;
119
}
120
// If we didn't find it, create a new one.
121
assert(_cur_covered_regions < _max_covered_regions,
122
"too many covered regions");
123
// Move the ones above up, to maintain sorted order.
124
for (int j = _cur_covered_regions; j > i; j--) {
125
_covered[j] = _covered[j-1];
126
_committed[j] = _committed[j-1];
127
}
128
int res = i;
129
_cur_covered_regions++;
130
_covered[res].set_start(base);
131
_covered[res].set_word_size(0);
132
CardValue* ct_start = byte_for(base);
133
HeapWord* ct_start_aligned = align_down((HeapWord*)ct_start, _page_size);
134
_committed[res].set_start(ct_start_aligned);
135
_committed[res].set_word_size(0);
136
return res;
137
}
138
139
int CardTable::find_covering_region_containing(HeapWord* addr) {
140
for (int i = 0; i < _cur_covered_regions; i++) {
141
if (_covered[i].contains(addr)) {
142
return i;
143
}
144
}
145
assert(0, "address outside of heap?");
146
return -1;
147
}
148
149
HeapWord* CardTable::largest_prev_committed_end(int ind) const {
150
HeapWord* max_end = NULL;
151
for (int j = 0; j < ind; j++) {
152
HeapWord* this_end = _committed[j].end();
153
if (this_end > max_end) max_end = this_end;
154
}
155
return max_end;
156
}
157
158
MemRegion CardTable::committed_unique_to_self(int self, MemRegion mr) const {
159
MemRegion result = mr;
160
for (int r = 0; r < _cur_covered_regions; r += 1) {
161
if (r != self) {
162
result = result.minus(_committed[r]);
163
}
164
}
165
// Never include the guard page.
166
result = result.minus(_guard_region);
167
return result;
168
}
169
170
void CardTable::resize_covered_region(MemRegion new_region) {
171
// We don't change the start of a region, only the end.
172
assert(_whole_heap.contains(new_region),
173
"attempt to cover area not in reserved area");
174
debug_only(verify_guard();)
175
// collided is true if the expansion would push into another committed region
176
debug_only(bool collided = false;)
177
int const ind = find_covering_region_by_base(new_region.start());
178
MemRegion const old_region = _covered[ind];
179
assert(old_region.start() == new_region.start(), "just checking");
180
if (new_region.word_size() != old_region.word_size()) {
181
// Commit new or uncommit old pages, if necessary.
182
MemRegion cur_committed = _committed[ind];
183
// Extend the end of this _committed region
184
// to cover the end of any lower _committed regions.
185
// This forms overlapping regions, but never interior regions.
186
HeapWord* const max_prev_end = largest_prev_committed_end(ind);
187
if (max_prev_end > cur_committed.end()) {
188
cur_committed.set_end(max_prev_end);
189
}
190
// Align the end up to a page size (starts are already aligned).
191
HeapWord* new_end = (HeapWord*) byte_after(new_region.last());
192
HeapWord* new_end_aligned = align_up(new_end, _page_size);
193
assert(new_end_aligned >= new_end, "align up, but less");
194
// Check the other regions (excludes "ind") to ensure that
195
// the new_end_aligned does not intrude onto the committed
196
// space of another region.
197
int ri = 0;
198
for (ri = ind + 1; ri < _cur_covered_regions; ri++) {
199
if (new_end_aligned > _committed[ri].start()) {
200
assert(new_end_aligned <= _committed[ri].end(),
201
"An earlier committed region can't cover a later committed region");
202
// Any region containing the new end
203
// should start at or beyond the region found (ind)
204
// for the new end (committed regions are not expected to
205
// be proper subsets of other committed regions).
206
assert(_committed[ri].start() >= _committed[ind].start(),
207
"New end of committed region is inconsistent");
208
new_end_aligned = _committed[ri].start();
209
// new_end_aligned can be equal to the start of its
210
// committed region (i.e., of "ind") if a second
211
// region following "ind" also start at the same location
212
// as "ind".
213
assert(new_end_aligned >= _committed[ind].start(),
214
"New end of committed region is before start");
215
debug_only(collided = true;)
216
// Should only collide with 1 region
217
break;
218
}
219
}
220
#ifdef ASSERT
221
for (++ri; ri < _cur_covered_regions; ri++) {
222
assert(!_committed[ri].contains(new_end_aligned),
223
"New end of committed region is in a second committed region");
224
}
225
#endif
226
// The guard page is always committed and should not be committed over.
227
// "guarded" is used for assertion checking below and recalls the fact
228
// that the would-be end of the new committed region would have
229
// penetrated the guard page.
230
HeapWord* new_end_for_commit = new_end_aligned;
231
232
DEBUG_ONLY(bool guarded = false;)
233
if (new_end_for_commit > _guard_region.start()) {
234
new_end_for_commit = _guard_region.start();
235
DEBUG_ONLY(guarded = true;)
236
}
237
238
if (new_end_for_commit > cur_committed.end()) {
239
// Must commit new pages.
240
MemRegion const new_committed =
241
MemRegion(cur_committed.end(), new_end_for_commit);
242
243
assert(!new_committed.is_empty(), "Region should not be empty here");
244
os::commit_memory_or_exit((char*)new_committed.start(),
245
new_committed.byte_size(), _page_size,
246
!ExecMem, "card table expansion");
247
// Use new_end_aligned (as opposed to new_end_for_commit) because
248
// the cur_committed region may include the guard region.
249
} else if (new_end_aligned < cur_committed.end()) {
250
// Must uncommit pages.
251
MemRegion const uncommit_region =
252
committed_unique_to_self(ind, MemRegion(new_end_aligned,
253
cur_committed.end()));
254
if (!uncommit_region.is_empty()) {
255
if (!os::uncommit_memory((char*)uncommit_region.start(),
256
uncommit_region.byte_size())) {
257
assert(false, "Card table contraction failed");
258
// The call failed so don't change the end of the
259
// committed region. This is better than taking the
260
// VM down.
261
new_end_aligned = _committed[ind].end();
262
}
263
}
264
}
265
// In any case, we can reset the end of the current committed entry.
266
_committed[ind].set_end(new_end_aligned);
267
268
#ifdef ASSERT
269
// Check that the last card in the new region is committed according
270
// to the tables.
271
bool covered = false;
272
for (int cr = 0; cr < _cur_covered_regions; cr++) {
273
if (_committed[cr].contains(new_end - 1)) {
274
covered = true;
275
break;
276
}
277
}
278
assert(covered, "Card for end of new region not committed");
279
#endif
280
281
// The default of 0 is not necessarily clean cards.
282
CardValue* entry;
283
if (old_region.last() < _whole_heap.start()) {
284
entry = byte_for(_whole_heap.start());
285
} else {
286
entry = byte_after(old_region.last());
287
}
288
assert(index_for(new_region.last()) < _guard_index,
289
"The guard card will be overwritten");
290
// This line commented out cleans the newly expanded region and
291
// not the aligned up expanded region.
292
// CardValue* const end = byte_after(new_region.last());
293
CardValue* const end = (CardValue*) new_end_for_commit;
294
assert((end >= byte_after(new_region.last())) || collided || guarded,
295
"Expect to be beyond new region unless impacting another region");
296
// do nothing if we resized downward.
297
#ifdef ASSERT
298
for (int ri = 0; ri < _cur_covered_regions; ri++) {
299
if (ri != ind) {
300
// The end of the new committed region should not
301
// be in any existing region unless it matches
302
// the start of the next region.
303
assert(!_committed[ri].contains(end) ||
304
(_committed[ri].start() == (HeapWord*) end),
305
"Overlapping committed regions");
306
}
307
}
308
#endif
309
if (entry < end) {
310
memset(entry, clean_card, pointer_delta(end, entry, sizeof(CardValue)));
311
}
312
}
313
// In any case, the covered size changes.
314
_covered[ind].set_word_size(new_region.word_size());
315
316
log_trace(gc, barrier)("CardTable::resize_covered_region: ");
317
log_trace(gc, barrier)(" _covered[%d].start(): " INTPTR_FORMAT " _covered[%d].last(): " INTPTR_FORMAT,
318
ind, p2i(_covered[ind].start()), ind, p2i(_covered[ind].last()));
319
log_trace(gc, barrier)(" _committed[%d].start(): " INTPTR_FORMAT " _committed[%d].last(): " INTPTR_FORMAT,
320
ind, p2i(_committed[ind].start()), ind, p2i(_committed[ind].last()));
321
log_trace(gc, barrier)(" byte_for(start): " INTPTR_FORMAT " byte_for(last): " INTPTR_FORMAT,
322
p2i(byte_for(_covered[ind].start())), p2i(byte_for(_covered[ind].last())));
323
log_trace(gc, barrier)(" addr_for(start): " INTPTR_FORMAT " addr_for(last): " INTPTR_FORMAT,
324
p2i(addr_for((CardValue*) _committed[ind].start())), p2i(addr_for((CardValue*) _committed[ind].last())));
325
326
// Touch the last card of the covered region to show that it
327
// is committed (or SEGV).
328
debug_only((void) (*byte_for(_covered[ind].last()));)
329
debug_only(verify_guard();)
330
}
331
332
// Note that these versions are precise! The scanning code has to handle the
333
// fact that the write barrier may be either precise or imprecise.
334
void CardTable::dirty_MemRegion(MemRegion mr) {
335
assert(align_down(mr.start(), HeapWordSize) == mr.start(), "Unaligned start");
336
assert(align_up (mr.end(), HeapWordSize) == mr.end(), "Unaligned end" );
337
CardValue* cur = byte_for(mr.start());
338
CardValue* last = byte_after(mr.last());
339
while (cur < last) {
340
*cur = dirty_card;
341
cur++;
342
}
343
}
344
345
void CardTable::clear_MemRegion(MemRegion mr) {
346
// Be conservative: only clean cards entirely contained within the
347
// region.
348
CardValue* cur;
349
if (mr.start() == _whole_heap.start()) {
350
cur = byte_for(mr.start());
351
} else {
352
assert(mr.start() > _whole_heap.start(), "mr is not covered.");
353
cur = byte_after(mr.start() - 1);
354
}
355
CardValue* last = byte_after(mr.last());
356
memset(cur, clean_card, pointer_delta(last, cur, sizeof(CardValue)));
357
}
358
359
void CardTable::clear(MemRegion mr) {
360
for (int i = 0; i < _cur_covered_regions; i++) {
361
MemRegion mri = mr.intersection(_covered[i]);
362
if (!mri.is_empty()) clear_MemRegion(mri);
363
}
364
}
365
366
void CardTable::dirty(MemRegion mr) {
367
CardValue* first = byte_for(mr.start());
368
CardValue* last = byte_after(mr.last());
369
memset(first, dirty_card, last-first);
370
}
371
372
// Unlike several other card table methods, dirty_card_iterate()
373
// iterates over dirty cards ranges in increasing address order.
374
void CardTable::dirty_card_iterate(MemRegion mr, MemRegionClosure* cl) {
375
for (int i = 0; i < _cur_covered_regions; i++) {
376
MemRegion mri = mr.intersection(_covered[i]);
377
if (!mri.is_empty()) {
378
CardValue *cur_entry, *next_entry, *limit;
379
for (cur_entry = byte_for(mri.start()), limit = byte_for(mri.last());
380
cur_entry <= limit;
381
cur_entry = next_entry) {
382
next_entry = cur_entry + 1;
383
if (*cur_entry == dirty_card) {
384
size_t dirty_cards;
385
// Accumulate maximal dirty card range, starting at cur_entry
386
for (dirty_cards = 1;
387
next_entry <= limit && *next_entry == dirty_card;
388
dirty_cards++, next_entry++);
389
MemRegion cur_cards(addr_for(cur_entry),
390
dirty_cards*card_size_in_words);
391
cl->do_MemRegion(cur_cards);
392
}
393
}
394
}
395
}
396
}
397
398
MemRegion CardTable::dirty_card_range_after_reset(MemRegion mr,
399
bool reset,
400
int reset_val) {
401
for (int i = 0; i < _cur_covered_regions; i++) {
402
MemRegion mri = mr.intersection(_covered[i]);
403
if (!mri.is_empty()) {
404
CardValue* cur_entry, *next_entry, *limit;
405
for (cur_entry = byte_for(mri.start()), limit = byte_for(mri.last());
406
cur_entry <= limit;
407
cur_entry = next_entry) {
408
next_entry = cur_entry + 1;
409
if (*cur_entry == dirty_card) {
410
size_t dirty_cards;
411
// Accumulate maximal dirty card range, starting at cur_entry
412
for (dirty_cards = 1;
413
next_entry <= limit && *next_entry == dirty_card;
414
dirty_cards++, next_entry++);
415
MemRegion cur_cards(addr_for(cur_entry),
416
dirty_cards*card_size_in_words);
417
if (reset) {
418
for (size_t i = 0; i < dirty_cards; i++) {
419
cur_entry[i] = reset_val;
420
}
421
}
422
return cur_cards;
423
}
424
}
425
}
426
}
427
return MemRegion(mr.end(), mr.end());
428
}
429
430
uintx CardTable::ct_max_alignment_constraint() {
431
return card_size * os::vm_page_size();
432
}
433
434
void CardTable::verify_guard() {
435
// For product build verification
436
guarantee(_byte_map[_guard_index] == last_card,
437
"card table guard has been modified");
438
}
439
440
void CardTable::invalidate(MemRegion mr) {
441
assert(align_down(mr.start(), HeapWordSize) == mr.start(), "Unaligned start");
442
assert(align_up (mr.end(), HeapWordSize) == mr.end(), "Unaligned end" );
443
for (int i = 0; i < _cur_covered_regions; i++) {
444
MemRegion mri = mr.intersection(_covered[i]);
445
if (!mri.is_empty()) dirty_MemRegion(mri);
446
}
447
}
448
449
void CardTable::verify() {
450
verify_guard();
451
}
452
453
#ifndef PRODUCT
454
void CardTable::verify_region(MemRegion mr, CardValue val, bool val_equals) {
455
CardValue* start = byte_for(mr.start());
456
CardValue* end = byte_for(mr.last());
457
bool failures = false;
458
for (CardValue* curr = start; curr <= end; ++curr) {
459
CardValue curr_val = *curr;
460
bool failed = (val_equals) ? (curr_val != val) : (curr_val == val);
461
if (failed) {
462
if (!failures) {
463
log_error(gc, verify)("== CT verification failed: [" INTPTR_FORMAT "," INTPTR_FORMAT "]", p2i(start), p2i(end));
464
log_error(gc, verify)("== %sexpecting value: %d", (val_equals) ? "" : "not ", val);
465
failures = true;
466
}
467
log_error(gc, verify)("== card " PTR_FORMAT " [" PTR_FORMAT "," PTR_FORMAT "], val: %d",
468
p2i(curr), p2i(addr_for(curr)),
469
p2i((HeapWord*) (((size_t) addr_for(curr)) + card_size)),
470
(int) curr_val);
471
}
472
}
473
guarantee(!failures, "there should not have been any failures");
474
}
475
476
void CardTable::verify_not_dirty_region(MemRegion mr) {
477
verify_region(mr, dirty_card, false /* val_equals */);
478
}
479
480
void CardTable::verify_dirty_region(MemRegion mr) {
481
verify_region(mr, dirty_card, true /* val_equals */);
482
}
483
#endif
484
485
void CardTable::print_on(outputStream* st) const {
486
st->print_cr("Card table byte_map: [" INTPTR_FORMAT "," INTPTR_FORMAT "] _byte_map_base: " INTPTR_FORMAT,
487
p2i(_byte_map), p2i(_byte_map + _byte_map_size), p2i(_byte_map_base));
488
}
489
490