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