1 /*
  2  * Copyright (c) 2017, 2024, 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_GC_G1_G1FULLGCMARKER_INLINE_HPP
 26 #define SHARE_GC_G1_G1FULLGCMARKER_INLINE_HPP
 27 
 28 #include "gc/g1/g1FullGCMarker.hpp"
 29 
 30 #include "classfile/classLoaderData.hpp"
 31 #include "classfile/javaClasses.inline.hpp"
 32 #include "gc/g1/g1Allocator.inline.hpp"
 33 #include "gc/g1/g1ConcurrentMarkBitMap.inline.hpp"
 34 #include "gc/g1/g1FullCollector.inline.hpp"
 35 #include "gc/g1/g1FullGCOopClosures.inline.hpp"
 36 #include "gc/g1/g1OopClosures.inline.hpp"
 37 #include "gc/g1/g1RegionMarkStatsCache.hpp"
 38 #include "gc/g1/g1StringDedup.hpp"
 39 #include "gc/shared/continuationGCSupport.inline.hpp"
 40 #include "gc/shared/preservedMarks.inline.hpp"
 41 #include "gc/shared/stringdedup/stringDedup.hpp"
 42 #include "oops/access.inline.hpp"
 43 #include "oops/compressedOops.inline.hpp"
 44 #include "oops/oop.inline.hpp"
 45 #include "utilities/debug.hpp"
 46 
 47 inline bool G1FullGCMarker::mark_object(oop obj) {
 48   // Try to mark.
 49   if (!_bitmap->par_mark(obj)) {
 50     // Lost mark race.
 51     return false;
 52   }
 53 
 54   // Check if deduplicatable string.
 55   if (StringDedup::is_enabled() &&
 56       java_lang_String::is_instance(obj) &&
 57       G1StringDedup::is_candidate_from_mark(obj)) {
 58     _string_dedup_requests.add(obj);
 59   }
 60 
 61   ContinuationGCSupport::transform_stack_chunk(obj);
 62 
 63   // Collect live words.
 64   _mark_stats_cache.add_live_words(obj);
 65 
 66   return true;
 67 }
 68 
 69 template <class T> inline void G1FullGCMarker::mark_and_push(T* p) {
 70   T heap_oop = RawAccess<>::oop_load(p);
 71   if (!CompressedOops::is_null(heap_oop)) {
 72     oop obj = CompressedOops::decode_not_null(heap_oop);
 73     if (mark_object(obj)) {
 74       _oop_stack.push(obj);
 75     }
 76     assert(_bitmap->is_marked(obj), "Must be marked");
 77   }
 78 }
 79 
 80 inline bool G1FullGCMarker::is_empty() {
 81   return _oop_stack.is_empty() && _objarray_stack.is_empty();
 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 void G1FullGCMarker::follow_array(objArrayOop array) {
 91   mark_closure()->do_klass(array->klass());
 92   // Don't push empty arrays to avoid unnecessary work.
 93   if (array->length() > 0) {
 94     push_objarray(array, 0);
 95   }
 96 }
 97 
 98 void G1FullGCMarker::follow_array_chunk(objArrayOop array, int index) {
 99   const int len = array->length();
100   const int beg_index = index;
101   assert(beg_index < len || len == 0, "index too large");
102 
103   const int stride = MIN2(len - beg_index, (int) ObjArrayMarkingStride);
104   const int end_index = beg_index + stride;
105 
106   // Push the continuation first to allow more efficient work stealing.
107   if (end_index < len) {
108     push_objarray(array, end_index);
109   }
110 
111   array->oop_iterate_range(mark_closure(), beg_index, end_index);
112 }
113 
114 inline void G1FullGCMarker::follow_object(oop obj) {
115   assert(_bitmap->is_marked(obj), "should be marked");
116   if (obj->is_objArray()) {
117     // Handle object arrays explicitly to allow them to
118     // be split into chunks if needed.
119     follow_array((objArrayOop)obj);
120   } else {
121     obj->oop_iterate(mark_closure());
122   }
123 }
124 
125 inline void G1FullGCMarker::publish_and_drain_oop_tasks() {
126   oop obj;
127   while (_oop_stack.pop_overflow(obj)) {
128     if (!_oop_stack.try_push_to_taskqueue(obj)) {
129       assert(_bitmap->is_marked(obj), "must be marked");
130       follow_object(obj);
131     }
132   }
133   while (_oop_stack.pop_local(obj)) {
134     assert(_bitmap->is_marked(obj), "must be marked");
135     follow_object(obj);
136   }
137 }
138 
139 inline bool G1FullGCMarker::publish_or_pop_objarray_tasks(ObjArrayTask& task) {
140   // It is desirable to move as much as possible work from the overflow queue to
141   // the shared queue as quickly as possible.
142   while (_objarray_stack.pop_overflow(task)) {
143     if (!_objarray_stack.try_push_to_taskqueue(task)) {
144       return true;
145     }
146   }
147   return false;
148 }
149 
150 void G1FullGCMarker::follow_marking_stacks() {
151   do {
152     // First, drain regular oop stack.
153     publish_and_drain_oop_tasks();
154 
155     // Then process ObjArrays one at a time to avoid marking stack bloat.
156     ObjArrayTask task;
157     if (publish_or_pop_objarray_tasks(task) ||
158         _objarray_stack.pop_local(task)) {
159       follow_array_chunk(objArrayOop(task.obj()), task.index());
160     }
161   } while (!is_empty());
162 }
163 
164 #endif // SHARE_GC_G1_G1FULLGCMARKER_INLINE_HPP