1 /*
   2  * Copyright (c) 2017, 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 #ifndef SHARE_VM_GC_G1_G1MARKSTACK_INLINE_HPP
  26 #define SHARE_VM_GC_G1_G1MARKSTACK_INLINE_HPP
  27 
  28 #include "gc/g1/g1Allocator.inline.hpp"
  29 #include "gc/g1/g1ConcurrentMarkBitMap.inline.hpp"
  30 #include "gc/g1/g1FullGCMarker.hpp"
  31 #include "gc/g1/g1StringDedup.hpp"
  32 #include "gc/g1/g1StringDedupQueue.hpp"
  33 #include "gc/shared/preservedMarks.inline.hpp"
  34 #include "utilities/debug.hpp"
  35 
  36 inline bool G1FullGCMarker::mark_object(oop obj) {
  37   // Not marking closed archive objects.
  38   if (G1ArchiveAllocator::is_closed_archive_object(obj)) {
  39     return false;
  40   }
  41 
  42   // Try to mark.
  43   if (!_bitmap->par_mark(obj)) {
  44     // Lost mark race.
  45     return false;
  46   }
  47 
  48   // Marked by us, preserve if needed.
  49   markOop mark = obj->mark();
  50   if (mark->must_be_preserved(obj) &&
  51       !G1ArchiveAllocator::is_open_archive_object(obj)) {
  52     preserved_stack()->push(obj, mark);
  53   }
  54 
  55   // Check if deduplicatable string.
  56   if (G1StringDedup::is_enabled()) {
  57     G1StringDedup::enqueue_from_mark(obj, _worker_id);
  58   }
  59   return true;
  60 }
  61 
  62 template <class T> inline void G1FullGCMarker::mark_and_push(T* p) {
  63   T heap_oop = oopDesc::load_heap_oop(p);
  64   if (!oopDesc::is_null(heap_oop)) {
  65     oop obj = oopDesc::decode_heap_oop_not_null(heap_oop);
  66     if (mark_object(obj)) {
  67       _oop_stack.push(obj);
  68       assert(_bitmap->is_marked(obj), "Must be marked now - map self");
  69     } else {
  70       assert(_bitmap->is_marked(obj) || G1ArchiveAllocator::is_closed_archive_object(obj),
  71              "Must be marked by other or closed archive object");
  72     }
  73   }
  74 }
  75 
  76 inline bool G1FullGCMarker::is_empty() {
  77   return _oop_stack.is_empty() && _objarray_stack.is_empty();
  78 }
  79 
  80 inline bool G1FullGCMarker::pop_object(oop& oop) {
  81   return _oop_stack.pop_overflow(oop) || _oop_stack.pop_local(oop);
  82 }
  83 
  84 inline void G1FullGCMarker::push_objarray(oop obj, size_t index) {
  85   ObjArrayTask task(obj, index);
  86   assert(task.is_valid(), "bad ObjArrayTask");
  87   _objarray_stack.push(task);
  88 }
  89 
  90 inline bool G1FullGCMarker::pop_objarray(ObjArrayTask& arr) {
  91   return _objarray_stack.pop_overflow(arr) || _objarray_stack.pop_local(arr);
  92 }
  93 
  94 inline void G1FullGCMarker::follow_array(objArrayOop array) {
  95   follow_klass(array->klass());
  96   // Don't push empty arrays to avoid unnecessary work.
  97   if (array->length() > 0) {
  98     push_objarray(array, 0);
  99   }
 100 }
 101 
 102 void G1FullGCMarker::follow_array_chunk(objArrayOop array, int index) {
 103   const int len = array->length();
 104   const int beg_index = index;
 105   assert(beg_index < len || len == 0, "index too large");
 106 
 107   const int stride = MIN2(len - beg_index, (int) ObjArrayMarkingStride);
 108   const int end_index = beg_index + stride;
 109 
 110   // Push the continuation first to allow more efficient work stealing.
 111   if (end_index < len) {
 112     push_objarray(array, end_index);
 113   }
 114 
 115   array->oop_iterate_range(mark_closure(), beg_index, end_index);
 116 
 117   if (VerifyDuringGC) {
 118     _verify_closure.set_containing_obj(array);
 119     NoHeaderExtendedOopClosure no(&_verify_closure);
 120     array->oop_iterate_range(&no, beg_index, end_index);
 121     if (_verify_closure.failures()) {
 122       assert(false, "Failed");
 123     }
 124   }
 125 }
 126 
 127 inline void G1FullGCMarker::follow_object(oop obj) {
 128   assert(_bitmap->is_marked(obj), "should be marked");
 129   if (obj->is_objArray()) {
 130     // Handle object arrays explicitly to allow them to
 131     // be split into chunks if needed.
 132     follow_array((objArrayOop)obj);
 133   } else {
 134     obj->oop_iterate(mark_closure());
 135     if (VerifyDuringGC) {
 136       if (obj->is_instance() && InstanceKlass::cast(obj->klass())->is_reference_instance_klass()) {
 137         return;
 138       }
 139       _verify_closure.set_containing_obj(obj);
 140       obj->oop_iterate_no_header(&_verify_closure);
 141       if (_verify_closure.failures()) {
 142         log_warning(gc, verify)("Failed after %d", _verify_closure._cc);
 143         assert(false, "Failed");
 144       }
 145     }
 146   }
 147 }
 148 
 149 void G1FullGCMarker::drain_stack() {
 150   do {
 151     oop obj;
 152     while (pop_object(obj)) {
 153       assert(_bitmap->is_marked(obj), "must be marked");
 154       follow_object(obj);
 155     }
 156     // Process ObjArrays one at a time to avoid marking stack bloat.
 157     ObjArrayTask task;
 158     if (pop_objarray(task)) {
 159       follow_array_chunk(objArrayOop(task.obj()), task.index());
 160     }
 161   } while (!is_empty());
 162 }
 163 
 164 inline void G1FullGCMarker::follow_klass(Klass* k) {
 165   oop op = k->klass_holder();
 166   mark_and_push(&op);
 167 }
 168 
 169 inline void G1FullGCMarker::follow_cld(ClassLoaderData* cld) {
 170   _cld_closure.do_cld(cld);
 171 }
 172 
 173 #endif