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