/
zRelocate.cpp
419 lines (347 loc) · 12.2 KB
/
zRelocate.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
/*
* Copyright (c) 2015, 2021, 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 "precompiled.hpp"
#include "gc/shared/gc_globals.hpp"
#include "gc/z/zAbort.inline.hpp"
#include "gc/z/zAddress.inline.hpp"
#include "gc/z/zBarrier.inline.hpp"
#include "gc/z/zForwarding.inline.hpp"
#include "gc/z/zHeap.inline.hpp"
#include "gc/z/zPage.inline.hpp"
#include "gc/z/zRelocate.hpp"
#include "gc/z/zRelocationSet.inline.hpp"
#include "gc/z/zStat.hpp"
#include "gc/z/zTask.hpp"
#include "gc/z/zThread.inline.hpp"
#include "gc/z/zWorkers.hpp"
#include "prims/jvmtiTagMap.hpp"
#include "runtime/atomic.hpp"
#include "utilities/debug.hpp"
ZRelocate::ZRelocate(ZWorkers* workers) :
_workers(workers) {}
static uintptr_t forwarding_index(ZForwarding* forwarding, uintptr_t from_addr) {
const uintptr_t from_offset = ZAddress::offset(from_addr);
return (from_offset - forwarding->start()) >> forwarding->object_alignment_shift();
}
static uintptr_t forwarding_find(ZForwarding* forwarding, uintptr_t from_addr, ZForwardingCursor* cursor) {
const uintptr_t from_index = forwarding_index(forwarding, from_addr);
const ZForwardingEntry entry = forwarding->find(from_index, cursor);
return entry.populated() ? ZAddress::good(entry.to_offset()) : 0;
}
static uintptr_t forwarding_insert(ZForwarding* forwarding, uintptr_t from_addr, uintptr_t to_addr, ZForwardingCursor* cursor) {
const uintptr_t from_index = forwarding_index(forwarding, from_addr);
const uintptr_t to_offset = ZAddress::offset(to_addr);
const uintptr_t to_offset_final = forwarding->insert(from_index, to_offset, cursor);
return ZAddress::good(to_offset_final);
}
static uintptr_t relocate_object_inner(ZForwarding* forwarding, uintptr_t from_addr, ZForwardingCursor* cursor) {
assert(ZHeap::heap()->is_object_live(from_addr), "Should be live");
// Allocate object
const size_t size = ZUtils::object_size(from_addr);
const uintptr_t to_addr = ZHeap::heap()->alloc_object_for_relocation(size);
if (to_addr == 0) {
// Allocation failed
return 0;
}
// Copy object
ZUtils::object_copy_disjoint(from_addr, to_addr, size);
// Insert forwarding
const uintptr_t to_addr_final = forwarding_insert(forwarding, from_addr, to_addr, cursor);
if (to_addr_final != to_addr) {
// Already relocated, try undo allocation
ZHeap::heap()->undo_alloc_object_for_relocation(to_addr, size);
}
return to_addr_final;
}
uintptr_t ZRelocate::relocate_object(ZForwarding* forwarding, uintptr_t from_addr) const {
ZForwardingCursor cursor;
// Lookup forwarding
uintptr_t to_addr = forwarding_find(forwarding, from_addr, &cursor);
if (to_addr != 0) {
// Already relocated
return to_addr;
}
// Relocate object
if (forwarding->retain_page()) {
to_addr = relocate_object_inner(forwarding, from_addr, &cursor);
forwarding->release_page();
if (to_addr != 0) {
// Success
return to_addr;
}
// Failed to relocate object. Wait for a worker thread to complete
// relocation of this page, and then forward the object. If the GC
// aborts the relocation phase before the page has been relocated,
// then wait return false and we just forward the object in-place.
if (!forwarding->wait_page_released()) {
// Forward object in-place
return forwarding_insert(forwarding, from_addr, from_addr, &cursor);
}
}
// Forward object
return forward_object(forwarding, from_addr);
}
uintptr_t ZRelocate::forward_object(ZForwarding* forwarding, uintptr_t from_addr) const {
ZForwardingCursor cursor;
const uintptr_t to_addr = forwarding_find(forwarding, from_addr, &cursor);
assert(to_addr != 0, "Should be forwarded");
return to_addr;
}
static ZPage* alloc_page(const ZForwarding* forwarding) {
if (ZStressRelocateInPlace) {
// Simulate failure to allocate a new page. This will
// cause the page being relocated to be relocated in-place.
return NULL;
}
ZAllocationFlags flags;
flags.set_non_blocking();
flags.set_worker_relocation();
return ZHeap::heap()->alloc_page(forwarding->type(), forwarding->size(), flags);
}
static void free_page(ZPage* page) {
ZHeap::heap()->free_page(page, true /* reclaimed */);
}
static bool should_free_target_page(ZPage* page) {
// Free target page if it is empty. We can end up with an empty target
// page if we allocated a new target page, and then lost the race to
// relocate the remaining objects, leaving the target page empty when
// relocation completed.
return page != NULL && page->top() == page->start();
}
class ZRelocateSmallAllocator {
private:
volatile size_t _in_place_count;
public:
ZRelocateSmallAllocator() :
_in_place_count(0) {}
ZPage* alloc_target_page(ZForwarding* forwarding, ZPage* target) {
ZPage* const page = alloc_page(forwarding);
if (page == NULL) {
Atomic::inc(&_in_place_count);
}
return page;
}
void share_target_page(ZPage* page) {
// Does nothing
}
void free_target_page(ZPage* page) {
if (should_free_target_page(page)) {
free_page(page);
}
}
void free_relocated_page(ZPage* page) {
free_page(page);
}
uintptr_t alloc_object(ZPage* page, size_t size) const {
return (page != NULL) ? page->alloc_object(size) : 0;
}
void undo_alloc_object(ZPage* page, uintptr_t addr, size_t size) const {
page->undo_alloc_object(addr, size);
}
const size_t in_place_count() const {
return _in_place_count;
}
};
class ZRelocateMediumAllocator {
private:
ZConditionLock _lock;
ZPage* _shared;
bool _in_place;
volatile size_t _in_place_count;
public:
ZRelocateMediumAllocator() :
_lock(),
_shared(NULL),
_in_place(false),
_in_place_count(0) {}
~ZRelocateMediumAllocator() {
if (should_free_target_page(_shared)) {
free_page(_shared);
}
}
ZPage* alloc_target_page(ZForwarding* forwarding, ZPage* target) {
ZLocker<ZConditionLock> locker(&_lock);
// Wait for any ongoing in-place relocation to complete
while (_in_place) {
_lock.wait();
}
// Allocate a new page only if the shared page is the same as the
// current target page. The shared page will be different from the
// current target page if another thread shared a page, or allocated
// a new page.
if (_shared == target) {
_shared = alloc_page(forwarding);
if (_shared == NULL) {
Atomic::inc(&_in_place_count);
_in_place = true;
}
}
return _shared;
}
void share_target_page(ZPage* page) {
ZLocker<ZConditionLock> locker(&_lock);
assert(_in_place, "Invalid state");
assert(_shared == NULL, "Invalid state");
assert(page != NULL, "Invalid page");
_shared = page;
_in_place = false;
_lock.notify_all();
}
void free_target_page(ZPage* page) {
// Does nothing
}
void free_relocated_page(ZPage* page) {
free_page(page);
}
uintptr_t alloc_object(ZPage* page, size_t size) const {
return (page != NULL) ? page->alloc_object_atomic(size) : 0;
}
void undo_alloc_object(ZPage* page, uintptr_t addr, size_t size) const {
page->undo_alloc_object_atomic(addr, size);
}
const size_t in_place_count() const {
return _in_place_count;
}
};
template <typename Allocator>
class ZRelocateClosure : public ObjectClosure {
private:
Allocator* const _allocator;
ZForwarding* _forwarding;
ZPage* _target;
bool relocate_object(uintptr_t from_addr) const {
ZForwardingCursor cursor;
// Lookup forwarding
if (forwarding_find(_forwarding, from_addr, &cursor) != 0) {
// Already relocated
return true;
}
// Allocate object
const size_t size = ZUtils::object_size(from_addr);
const uintptr_t to_addr = _allocator->alloc_object(_target, size);
if (to_addr == 0) {
// Allocation failed
return false;
}
// Copy object. Use conjoint copying if we are relocating
// in-place and the new object overlapps with the old object.
if (_forwarding->in_place() && to_addr + size > from_addr) {
ZUtils::object_copy_conjoint(from_addr, to_addr, size);
} else {
ZUtils::object_copy_disjoint(from_addr, to_addr, size);
}
// Insert forwarding
if (forwarding_insert(_forwarding, from_addr, to_addr, &cursor) != to_addr) {
// Already relocated, undo allocation
_allocator->undo_alloc_object(_target, to_addr, size);
}
return true;
}
virtual void do_object(oop obj) {
const uintptr_t addr = ZOop::to_address(obj);
assert(ZHeap::heap()->is_object_live(addr), "Should be live");
while (!relocate_object(addr)) {
// Allocate a new target page, or if that fails, use the page being
// relocated as the new target, which will cause it to be relocated
// in-place.
_target = _allocator->alloc_target_page(_forwarding, _target);
if (_target != NULL) {
continue;
}
// Claim the page being relocated to block other threads from accessing
// it, or its forwarding table, until it has been released (relocation
// completed).
_target = _forwarding->claim_page();
_target->reset_for_in_place_relocation();
_forwarding->set_in_place();
}
}
public:
ZRelocateClosure(Allocator* allocator) :
_allocator(allocator),
_forwarding(NULL),
_target(NULL) {}
~ZRelocateClosure() {
_allocator->free_target_page(_target);
}
void do_forwarding(ZForwarding* forwarding) {
_forwarding = forwarding;
// Check if we should abort
if (ZAbort::should_abort()) {
_forwarding->abort_page();
return;
}
// Relocate objects
_forwarding->object_iterate(this);
// Verify
if (ZVerifyForwarding) {
_forwarding->verify();
}
// Release relocated page
_forwarding->release_page();
if (_forwarding->in_place()) {
// The relocated page has been relocated in-place and should not
// be freed. Keep it as target page until it is full, and offer to
// share it with other worker threads.
_allocator->share_target_page(_target);
} else {
// Detach and free relocated page
ZPage* const page = _forwarding->detach_page();
_allocator->free_relocated_page(page);
}
}
};
class ZRelocateTask : public ZTask {
private:
ZRelocationSetParallelIterator _iter;
ZRelocateSmallAllocator _small_allocator;
ZRelocateMediumAllocator _medium_allocator;
static bool is_small(ZForwarding* forwarding) {
return forwarding->type() == ZPageTypeSmall;
}
public:
ZRelocateTask(ZRelocationSet* relocation_set) :
ZTask("ZRelocateTask"),
_iter(relocation_set),
_small_allocator(),
_medium_allocator() {}
~ZRelocateTask() {
ZStatRelocation::set_at_relocate_end(_small_allocator.in_place_count(),
_medium_allocator.in_place_count());
}
virtual void work() {
ZRelocateClosure<ZRelocateSmallAllocator> small(&_small_allocator);
ZRelocateClosure<ZRelocateMediumAllocator> medium(&_medium_allocator);
for (ZForwarding* forwarding; _iter.next(&forwarding);) {
if (is_small(forwarding)) {
small.do_forwarding(forwarding);
} else {
medium.do_forwarding(forwarding);
}
}
}
};
void ZRelocate::relocate(ZRelocationSet* relocation_set) {
ZRelocateTask task(relocation_set);
_workers->run_concurrent(&task);
}