1 /*
  2  * Copyright (c) 2019, 2021, 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/zThreadLocalData.hpp"
 26 #include "gc/z/zObjArrayAllocator.hpp"
 27 #include "gc/z/zUtils.inline.hpp"
 28 #include "oops/arrayKlass.hpp"
 29 #include "runtime/interfaceSupport.inline.hpp"
 30 #include "utilities/debug.hpp"
 31 
 32 ZObjArrayAllocator::ZObjArrayAllocator(Klass* klass, size_t word_size, int length, bool do_zero, Thread* thread) :
 33     ObjArrayAllocator(klass, word_size, length, do_zero, thread) {}
 34 
 35 void ZObjArrayAllocator::yield_for_safepoint() const {
 36   ThreadBlockInVM tbivm(JavaThread::cast(_thread));
 37 }
 38 
 39 oop ZObjArrayAllocator::initialize(HeapWord* mem) const {
 40   // ZGC specializes the initialization by performing segmented clearing
 41   // to allow shorter time-to-safepoints.
 42 
 43   if (!_do_zero) {
 44     // No need for ZGC specialization
 45     return ObjArrayAllocator::initialize(mem);
 46   }
 47 
 48   // A max segment size of 64K was chosen because microbenchmarking
 49   // suggested that it offered a good trade-off between allocation
 50   // time and time-to-safepoint
 51   const size_t segment_max = ZUtils::bytes_to_words(64 * K);
 52   const BasicType element_type = ArrayKlass::cast(_klass)->element_type();
 53   int base_offset = arrayOopDesc::base_offset_in_bytes(element_type);
 54 
 55   // Clear leading 32 bit, if necessary.
 56   if (!is_aligned(base_offset, HeapWordSize)) {
 57     assert(is_aligned(base_offset, BytesPerInt), "array base must be 32 bit aligned");
 58     *reinterpret_cast<jint*>(reinterpret_cast<char*>(mem) + base_offset) = 0;
 59     base_offset += BytesPerInt;
 60   }
 61   assert(is_aligned(base_offset, HeapWordSize), "remaining array base must be 64 bit aligned");
 62 
 63   const size_t header = heap_word_size(base_offset);
 64   const size_t payload_size = _word_size - header;
 65   if (payload_size <= segment_max) {
 66     // To small to use segmented clearing
 67     return ObjArrayAllocator::initialize(mem);
 68   }
 69 
 70   // Segmented clearing
 71 
 72   // The array is going to be exposed before it has been completely
 73   // cleared, therefore we can't expose the header at the end of this
 74   // function. Instead explicitly initialize it according to our needs.
 75   oopDesc::release_set_mark(mem, _klass->prototype_header());

 76   assert(_length >= 0, "length should be non-negative");
 77   arrayOopDesc::set_length(mem, _length);
 78 
 79   // Keep the array alive across safepoints through an invisible
 80   // root. Invisible roots are not visited by the heap itarator
 81   // and the marking logic will not attempt to follow its elements.
 82   // Relocation knows how to dodge iterating over such objects.
 83   ZThreadLocalData::set_invisible_root(_thread, (oop*)&mem);
 84 
 85   for (size_t processed = 0; processed < payload_size; processed += segment_max) {
 86     // Calculate segment
 87     HeapWord* const start = (HeapWord*)(mem + header + processed);
 88     const size_t remaining = payload_size - processed;
 89     const size_t segment_size = MIN2(remaining, segment_max);
 90 
 91     // Clear segment
 92     Copy::zero_to_words(start, segment_size);
 93 
 94     // Safepoint
 95     yield_for_safepoint();
 96   }
 97 
 98   ZThreadLocalData::clear_invisible_root(_thread);
 99 
100   return cast_to_oop(mem);
101 }
--- EOF ---