Skip to content

Commit 403475d

Browse files
plidenstefankfisksci-aws
committed
8227226: ZGC: Segmented array clearing
Co-authored-by: Stefan Karlsson <stefan.karlsson@oracle.com> Co-authored-by: Erik Osterlund <erik.osterlund@oracle.com> Co-authored-by: Ryan Sciampacone <sci@amazon.com> Reviewed-by: eosterlund
1 parent d096e03 commit 403475d

File tree

6 files changed

+139
-4
lines changed

6 files changed

+139
-4
lines changed

src/hotspot/share/gc/shared/memAllocator.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -226,18 +226,18 @@ void MemAllocator::Allocation::notify_allocation_jfr_sampler() {
226226
size_t size_in_bytes = _allocator._word_size * HeapWordSize;
227227

228228
if (_allocated_outside_tlab) {
229-
AllocTracer::send_allocation_outside_tlab(_allocator._klass, mem, size_in_bytes, _thread);
229+
AllocTracer::send_allocation_outside_tlab(obj()->klass(), mem, size_in_bytes, _thread);
230230
} else if (_allocated_tlab_size != 0) {
231231
// TLAB was refilled
232-
AllocTracer::send_allocation_in_new_tlab(_allocator._klass, mem, _allocated_tlab_size * HeapWordSize,
232+
AllocTracer::send_allocation_in_new_tlab(obj()->klass(), mem, _allocated_tlab_size * HeapWordSize,
233233
size_in_bytes, _thread);
234234
}
235235
}
236236

237237
void MemAllocator::Allocation::notify_allocation_dtrace_sampler() {
238238
if (DTraceAllocProbes) {
239239
// support for Dtrace object alloc event (no-op most of the time)
240-
Klass* klass = _allocator._klass;
240+
Klass* klass = obj()->klass();
241241
size_t word_size = _allocator._word_size;
242242
if (klass != NULL && klass->name() != NULL) {
243243
SharedRuntime::dtrace_object_alloc(obj(), (int)word_size);

src/hotspot/share/gc/shared/memAllocator.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class MemAllocator: StackObj {
5959
// This finish constructing an oop by installing the mark word and the Klass* pointer
6060
// last. At the point when the Klass pointer is initialized, this is a constructed object
6161
// that must be parseable as an oop by concurrent collectors.
62-
oop finish(HeapWord* mem) const;
62+
virtual oop finish(HeapWord* mem) const;
6363

6464
// Raw memory allocation. This may or may not use TLAB allocations to satisfy the
6565
// allocation. A GC implementation may override this function to satisfy the allocation

src/hotspot/share/gc/z/zCollectedHeap.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "gc/z/zGlobals.hpp"
2929
#include "gc/z/zHeap.inline.hpp"
3030
#include "gc/z/zNMethod.hpp"
31+
#include "gc/z/zObjArrayAllocator.hpp"
3132
#include "gc/z/zServiceability.hpp"
3233
#include "gc/z/zStat.hpp"
3334
#include "gc/z/zUtils.inline.hpp"
@@ -127,6 +128,15 @@ HeapWord* ZCollectedHeap::allocate_new_tlab(size_t min_size, size_t requested_si
127128
return (HeapWord*)addr;
128129
}
129130

131+
oop ZCollectedHeap::array_allocate(Klass* klass, int size, int length, bool do_zero, TRAPS) {
132+
if (!do_zero) {
133+
return CollectedHeap::array_allocate(klass, size, length, false /* do_zero */, THREAD);
134+
}
135+
136+
ZObjArrayAllocator allocator(klass, size, length, THREAD);
137+
return allocator.allocate();
138+
}
139+
130140
HeapWord* ZCollectedHeap::mem_allocate(size_t size, bool* gc_overhead_limit_was_exceeded) {
131141
const size_t size_in_bytes = ZUtils::words_to_bytes(align_object_size(size));
132142
return (HeapWord*)_heap.alloc_object(size_in_bytes);

src/hotspot/share/gc/z/zCollectedHeap.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ class ZCollectedHeap : public CollectedHeap {
7575

7676
virtual uint32_t hash_oop(oop obj) const;
7777

78+
virtual oop array_allocate(Klass* klass, int size, int length, bool do_zero, TRAPS);
7879
virtual HeapWord* mem_allocate(size_t size, bool* gc_overhead_limit_was_exceeded);
7980
virtual MetaWord* satisfy_failed_metadata_allocation(ClassLoaderData* loader_data,
8081
size_t size,
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* Copyright (c) 2019, 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+
#include "precompiled.hpp"
25+
#include "gc/z/zObjArrayAllocator.hpp"
26+
#include "gc/z/zUtils.inline.hpp"
27+
#include "memory/universe.hpp"
28+
#include "oops/arrayKlass.hpp"
29+
#include "runtime/interfaceSupport.inline.hpp"
30+
#include "runtime/handles.hpp"
31+
#include "runtime/os.hpp"
32+
#include "utilities/debug.hpp"
33+
#include "utilities/globalDefinitions.hpp"
34+
35+
// To avoid delaying safepoints, clearing of arrays is split up in segments
36+
// with safepoint polling inbetween. However, we can't have a not-yet-cleared
37+
// array of oops on the heap when we safepoint since the GC will then stumble
38+
// across uninitialized oops. To avoid this we let an array of oops be an
39+
// array of a primitive type of the same size until the clearing has completed.
40+
// A max segment size of 64K was chosen because benchmarking suggests that is
41+
// offers a good trade-off between allocation time and time-to-safepoint.
42+
43+
static Klass* substitute_object_array_klass(Klass* klass) {
44+
if (!klass->is_objArray_klass()) {
45+
return klass;
46+
}
47+
48+
Klass* const substitute_klass = Universe::longArrayKlassObj();
49+
const BasicType type = ArrayKlass::cast(klass)->element_type();
50+
const BasicType substitute_type = ArrayKlass::cast(substitute_klass)->element_type();
51+
assert(type2aelembytes(type) == type2aelembytes(substitute_type), "Element size mismatch");
52+
return substitute_klass;
53+
}
54+
55+
ZObjArrayAllocator::ZObjArrayAllocator(Klass* klass, size_t word_size, int length, Thread* thread) :
56+
ObjArrayAllocator(substitute_object_array_klass(klass), word_size, length, false /* do_zero */, thread),
57+
_final_klass(klass) {}
58+
59+
oop ZObjArrayAllocator::finish(HeapWord* mem) const {
60+
HandleMark hm;
61+
Handle array(_thread, ObjArrayAllocator::finish(mem));
62+
63+
const size_t segment_max = ZUtils::bytes_to_words(64 * K);
64+
const size_t skip = arrayOopDesc::header_size(ArrayKlass::cast(_klass)->element_type());
65+
size_t remaining = _word_size - skip;
66+
67+
while (remaining > 0) {
68+
// Clear segment
69+
const size_t segment = MIN2(remaining, segment_max);
70+
Copy::zero_to_words((HeapWord*)array() + (_word_size - remaining), segment);
71+
remaining -= segment;
72+
73+
if (remaining > 0) {
74+
// Safepoint
75+
ThreadBlockInVM tbivm((JavaThread*)_thread);
76+
}
77+
}
78+
79+
if (_klass != _final_klass) {
80+
// Set final klass
81+
oopDesc::release_set_klass((HeapWord*)array(), _final_klass);
82+
}
83+
84+
return array();
85+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright (c) 2019, 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+
#ifndef SHARE_GC_Z_ZOBJARRAYALLOCATOR_HPP
25+
#define SHARE_GC_Z_ZOBJARRAYALLOCATOR_HPP
26+
27+
#include "gc/shared/memAllocator.hpp"
28+
29+
class ZObjArrayAllocator : public ObjArrayAllocator {
30+
private:
31+
Klass* const _final_klass;
32+
33+
public:
34+
ZObjArrayAllocator(Klass* klass, size_t word_size, int length, Thread* thread);
35+
36+
virtual oop finish(HeapWord* mem) const;
37+
};
38+
39+
#endif // SHARE_GC_Z_ZOBJARRAYALLOCATOR_HPP

0 commit comments

Comments
 (0)