src/hotspot/share/gc/z/zObjArrayAllocator.cpp
changeset 58066 8407928b9fe5
parent 57851 6728c41f2a08
child 58679 9c3209ff7550
equal deleted inserted replaced
58065:3fee0e6b54f5 58066:8407928b9fe5
    23 
    23 
    24 #include "precompiled.hpp"
    24 #include "precompiled.hpp"
    25 #include "gc/z/zThreadLocalData.hpp"
    25 #include "gc/z/zThreadLocalData.hpp"
    26 #include "gc/z/zObjArrayAllocator.hpp"
    26 #include "gc/z/zObjArrayAllocator.hpp"
    27 #include "gc/z/zUtils.inline.hpp"
    27 #include "gc/z/zUtils.inline.hpp"
    28 #include "memory/universe.hpp"
       
    29 #include "oops/arrayKlass.hpp"
    28 #include "oops/arrayKlass.hpp"
    30 #include "runtime/interfaceSupport.inline.hpp"
    29 #include "runtime/interfaceSupport.inline.hpp"
    31 #include "runtime/handles.hpp"
       
    32 #include "runtime/os.hpp"
       
    33 #include "utilities/debug.hpp"
       
    34 #include "utilities/globalDefinitions.hpp"
       
    35 
       
    36 // To avoid delaying safepoints, clearing of arrays is split up in segments
       
    37 // with safepoint polling inbetween. However, we can't have a not-yet-cleared
       
    38 // array of oops on the heap when we safepoint since the GC will then stumble
       
    39 // across uninitialized oops. To avoid this we let an array of oops be an
       
    40 // array of a primitive type of the same size until the clearing has completed.
       
    41 // A max segment size of 64K was chosen because benchmarking suggests that is
       
    42 // offers a good trade-off between allocation time and time-to-safepoint.
       
    43 
       
    44 static Klass* substitute_object_array_klass(Klass* klass) {
       
    45   if (!klass->is_objArray_klass()) {
       
    46     return klass;
       
    47   }
       
    48 
       
    49   Klass* const substitute_klass = Universe::longArrayKlassObj();
       
    50   const BasicType type = ArrayKlass::cast(klass)->element_type();
       
    51   const BasicType substitute_type = ArrayKlass::cast(substitute_klass)->element_type();
       
    52   assert(type2aelembytes(type) == type2aelembytes(substitute_type), "Element size mismatch");
       
    53   return substitute_klass;
       
    54 }
       
    55 
    30 
    56 ZObjArrayAllocator::ZObjArrayAllocator(Klass* klass, size_t word_size, int length, Thread* thread) :
    31 ZObjArrayAllocator::ZObjArrayAllocator(Klass* klass, size_t word_size, int length, Thread* thread) :
    57     ObjArrayAllocator(substitute_object_array_klass(klass), word_size, length, false /* do_zero */, thread),
    32     ObjArrayAllocator(klass, word_size, length, false /* do_zero */, thread) {}
    58     _final_klass(klass) {}
       
    59 
    33 
    60 oop ZObjArrayAllocator::finish(HeapWord* mem) const {
    34 oop ZObjArrayAllocator::finish(HeapWord* mem) const {
    61   // Set mark word and initial klass pointer
    35   // Initialize object header and length field
    62   ObjArrayAllocator::finish(mem);
    36   ObjArrayAllocator::finish(mem);
    63 
    37 
    64   // Keep the array alive across safepoints, but make it invisible
    38   // Keep the array alive across safepoints through an invisible
    65   // to the heap itarator until the final klass pointer has been set
    39   // root. Invisible roots are not visited by the heap itarator
       
    40   // and the marking logic will not attempt to follow its elements.
    66   ZThreadLocalData::set_invisible_root(_thread, (oop*)&mem);
    41   ZThreadLocalData::set_invisible_root(_thread, (oop*)&mem);
    67 
    42 
       
    43   // A max segment size of 64K was chosen because microbenchmarking
       
    44   // suggested that it offered a good trade-off between allocation
       
    45   // time and time-to-safepoint
    68   const size_t segment_max = ZUtils::bytes_to_words(64 * K);
    46   const size_t segment_max = ZUtils::bytes_to_words(64 * K);
    69   const size_t skip = arrayOopDesc::header_size(ArrayKlass::cast(_klass)->element_type());
    47   const size_t skip = arrayOopDesc::header_size(ArrayKlass::cast(_klass)->element_type());
    70   size_t remaining = _word_size - skip;
    48   size_t remaining = _word_size - skip;
    71 
    49 
    72   while (remaining > 0) {
    50   while (remaining > 0) {
    79       // Safepoint
    57       // Safepoint
    80       ThreadBlockInVM tbivm((JavaThread*)_thread);
    58       ThreadBlockInVM tbivm((JavaThread*)_thread);
    81     }
    59     }
    82   }
    60   }
    83 
    61 
    84   if (_klass != _final_klass) {
       
    85     // Set final klass pointer
       
    86     oopDesc::release_set_klass(mem, _final_klass);
       
    87   }
       
    88 
       
    89   // Make the array visible to the heap iterator
       
    90   ZThreadLocalData::clear_invisible_root(_thread);
    62   ZThreadLocalData::clear_invisible_root(_thread);
    91 
    63 
    92   return oop(mem);
    64   return oop(mem);
    93 }
    65 }