1 /*
  2  * Copyright (c) 2015, 2021, Red Hat, Inc. 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_SHENANDOAH_SHENANDOAHMARK_INLINE_HPP
 26 #define SHARE_GC_SHENANDOAH_SHENANDOAHMARK_INLINE_HPP
 27 
 28 #include "gc/shenandoah/shenandoahMark.hpp"
 29 
 30 #include "gc/shenandoah/shenandoahAsserts.hpp"
 31 #include "gc/shenandoah/shenandoahBarrierSet.inline.hpp"
 32 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
 33 #include "gc/shenandoah/shenandoahMarkingContext.inline.hpp"
 34 #include "gc/shenandoah/shenandoahStringDedup.inline.hpp"
 35 #include "gc/shenandoah/shenandoahTaskqueue.inline.hpp"
 36 #include "gc/shenandoah/shenandoahUtils.hpp"
 37 #include "memory/iterator.inline.hpp"
 38 #include "oops/compressedOops.inline.hpp"
 39 #include "oops/oop.inline.hpp"
 40 #include "runtime/prefetch.inline.hpp"
 41 #include "utilities/powerOfTwo.hpp"
 42 
 43 template <StringDedupMode STRING_DEDUP>
 44 void ShenandoahMark::dedup_string(oop obj, StringDedup::Requests* const req) {
 45   if (STRING_DEDUP == ENQUEUE_DEDUP) {
 46     if (ShenandoahStringDedup::is_candidate(obj)) {
 47       req->add(obj);
 48     }
 49   } else if (STRING_DEDUP == ALWAYS_DEDUP) {
 50     if (ShenandoahStringDedup::is_string_candidate(obj) &&
 51         !ShenandoahStringDedup::dedup_requested(obj)) {
 52         req->add(obj);
 53     }
 54   }
 55 }
 56 
 57 template <class T, StringDedupMode STRING_DEDUP>
 58 void ShenandoahMark::do_task(ShenandoahObjToScanQueue* q, T* cl, ShenandoahLiveData* live_data, StringDedup::Requests* const req, ShenandoahMarkTask* task) {
 59   oop obj = task->obj();
 60 
 61   shenandoah_assert_not_forwarded(NULL, obj);
 62   shenandoah_assert_marked(NULL, obj);
 63   shenandoah_assert_not_in_cset_except(NULL, obj, ShenandoahHeap::heap()->cancelled_gc());
 64 
 65   // Are we in weak subgraph scan?
 66   bool weak = task->is_weak();
 67   cl->set_weak(weak);
 68 
 69   if (task->is_not_chunked()) {
 70     if (obj->is_instance()) {
 71       // Case 1: Normal oop, process as usual.
 72       obj->oop_iterate(cl);
 73       dedup_string<STRING_DEDUP>(obj, req);
 74     } else if (obj->is_objArray()) {
 75       // Case 2: Object array instance and no chunk is set. Must be the first
 76       // time we visit it, start the chunked processing.
 77       do_chunked_array_start<T>(q, cl, obj, weak);
 78     } else {
 79       // Case 3: Primitive array. Do nothing, no oops there. We use the same
 80       // performance tweak TypeArrayKlass::oop_oop_iterate_impl is using:
 81       // We skip iterating over the klass pointer since we know that
 82       // Universe::TypeArrayKlass never moves.
 83       assert (obj->is_typeArray(), "should be type array");
 84     }
 85     // Count liveness the last: push the outstanding work to the queues first
 86     // Avoid double-counting objects that are visited twice due to upgrade
 87     // from final- to strong mark.
 88     if (task->count_liveness()) {
 89       count_liveness(live_data, obj);
 90     }
 91   } else {
 92     // Case 4: Array chunk, has sensible chunk id. Process it.
 93     do_chunked_array<T>(q, cl, obj, task->chunk(), task->pow(), weak);
 94   }
 95 }
 96 
 97 inline void ShenandoahMark::count_liveness(ShenandoahLiveData* live_data, oop obj) {
 98   ShenandoahHeap* const heap = ShenandoahHeap::heap();
 99   size_t region_idx = heap->heap_region_index_containing(obj);
100   ShenandoahHeapRegion* region = heap->get_region(region_idx);
101   size_t size = obj->size();
102 
103   if (!region->is_humongous_start()) {
104     assert(!region->is_humongous(), "Cannot have continuations here");
105     ShenandoahLiveData cur = live_data[region_idx];
106     size_t new_val = size + cur;
107     if (new_val >= SHENANDOAH_LIVEDATA_MAX) {
108       // overflow, flush to region data
109       region->increase_live_data_gc_words(new_val);
110       live_data[region_idx] = 0;
111     } else {
112       // still good, remember in locals
113       live_data[region_idx] = (ShenandoahLiveData) new_val;
114     }
115   } else {
116     shenandoah_assert_in_correct_region(NULL, obj);
117     size_t num_regions = ShenandoahHeapRegion::required_regions(size * HeapWordSize);
118 
119     for (size_t i = region_idx; i < region_idx + num_regions; i++) {
120       ShenandoahHeapRegion* chain_reg = heap->get_region(i);
121       assert(chain_reg->is_humongous(), "Expecting a humongous region");
122       chain_reg->increase_live_data_gc_words(chain_reg->used() >> LogHeapWordSize);
123     }
124   }
125 }
126 
127 template <class T>
128 inline void ShenandoahMark::do_chunked_array_start(ShenandoahObjToScanQueue* q, T* cl, oop obj, bool weak) {
129   assert(obj->is_objArray(), "expect object array");
130   objArrayOop array = objArrayOop(obj);
131   int len = array->length();
132 
133   // Mark objArray klass metadata
134   if (Devirtualizer::do_metadata(cl)) {
135     Devirtualizer::do_klass(cl, array->klass());
136   }
137 
138   if (len <= (int) ObjArrayMarkingStride*2) {
139     // A few slices only, process directly
140     array->oop_iterate_range(cl, 0, len);
141   } else {
142     int bits = log2i_graceful(len);
143     // Compensate for non-power-of-two arrays, cover the array in excess:
144     if (len != (1 << bits)) bits++;
145 
146     // Only allow full chunks on the queue. This frees do_chunked_array() from checking from/to
147     // boundaries against array->length(), touching the array header on every chunk.
148     //
149     // To do this, we cut the prefix in full-sized chunks, and submit them on the queue.
150     // If the array is not divided in chunk sizes, then there would be an irregular tail,
151     // which we will process separately.
152 
153     int last_idx = 0;
154 
155     int chunk = 1;
156     int pow = bits;
157 
158     // Handle overflow
159     if (pow >= 31) {
160       assert (pow == 31, "sanity");
161       pow--;
162       chunk = 2;
163       last_idx = (1 << pow);
164       bool pushed = q->push(ShenandoahMarkTask(array, true, weak, 1, pow));
165       assert(pushed, "overflow queue should always succeed pushing");
166     }
167 
168     // Split out tasks, as suggested in ShenandoahMarkTask docs. Record the last
169     // successful right boundary to figure out the irregular tail.
170     while ((1 << pow) > (int)ObjArrayMarkingStride &&
171            (chunk*2 < ShenandoahMarkTask::chunk_size())) {
172       pow--;
173       int left_chunk = chunk*2 - 1;
174       int right_chunk = chunk*2;
175       int left_chunk_end = left_chunk * (1 << pow);
176       if (left_chunk_end < len) {
177         bool pushed = q->push(ShenandoahMarkTask(array, true, weak, left_chunk, pow));
178         assert(pushed, "overflow queue should always succeed pushing");
179         chunk = right_chunk;
180         last_idx = left_chunk_end;
181       } else {
182         chunk = left_chunk;
183       }
184     }
185 
186     // Process the irregular tail, if present
187     int from = last_idx;
188     if (from < len) {
189       array->oop_iterate_range(cl, from, len);
190     }
191   }
192 }
193 
194 template <class T>
195 inline void ShenandoahMark::do_chunked_array(ShenandoahObjToScanQueue* q, T* cl, oop obj, int chunk, int pow, bool weak) {
196   assert(obj->is_objArray(), "expect object array");
197   objArrayOop array = objArrayOop(obj);
198 
199   assert (ObjArrayMarkingStride > 0, "sanity");
200 
201   // Split out tasks, as suggested in ShenandoahMarkTask docs. Avoid pushing tasks that
202   // are known to start beyond the array.
203   while ((1 << pow) > (int)ObjArrayMarkingStride && (chunk*2 < ShenandoahMarkTask::chunk_size())) {
204     pow--;
205     chunk *= 2;
206     bool pushed = q->push(ShenandoahMarkTask(array, true, weak, chunk - 1, pow));
207     assert(pushed, "overflow queue should always succeed pushing");
208   }
209 
210   int chunk_size = 1 << pow;
211 
212   int from = (chunk - 1) * chunk_size;
213   int to = chunk * chunk_size;
214 
215 #ifdef ASSERT
216   int len = array->length();
217   assert (0 <= from && from < len, "from is sane: %d/%d", from, len);
218   assert (0 < to && to <= len, "to is sane: %d/%d", to, len);
219 #endif
220 
221   array->oop_iterate_range(cl, from, to);
222 }
223 
224 class ShenandoahSATBBufferClosure : public SATBBufferClosure {
225 private:
226   ShenandoahObjToScanQueue* _queue;
227   ShenandoahHeap* _heap;
228   ShenandoahMarkingContext* const _mark_context;
229 public:
230   ShenandoahSATBBufferClosure(ShenandoahObjToScanQueue* q) :
231     _queue(q),
232     _heap(ShenandoahHeap::heap()),
233     _mark_context(_heap->marking_context())
234   {
235   }
236 
237   void do_buffer(void **buffer, size_t size) {
238     assert(size == 0 || !_heap->has_forwarded_objects(), "Forwarded objects are not expected here");
239     for (size_t i = 0; i < size; ++i) {
240       oop *p = (oop *) &buffer[i];
241       ShenandoahMark::mark_through_ref<oop>(p, _queue, _mark_context, false);
242     }
243   }
244 };
245 
246 template<class T>
247 inline void ShenandoahMark::mark_through_ref(T* p, ShenandoahObjToScanQueue* q, ShenandoahMarkingContext* const mark_context, bool weak) {
248   T o = RawAccess<>::oop_load(p);
249   if (!CompressedOops::is_null(o)) {
250     oop obj = CompressedOops::decode_not_null(o);
251 
252     shenandoah_assert_not_forwarded(p, obj);
253     shenandoah_assert_not_in_cset_except(p, obj, ShenandoahHeap::heap()->cancelled_gc());
254 
255     bool skip_live = false;
256     bool marked;
257     if (weak) {
258       marked = mark_context->mark_weak(obj);
259     } else {
260       marked = mark_context->mark_strong(obj, /* was_upgraded = */ skip_live);
261     }
262     if (marked) {
263       bool pushed = q->push(ShenandoahMarkTask(obj, skip_live, weak));
264       assert(pushed, "overflow queue should always succeed pushing");
265     }
266 
267     shenandoah_assert_marked(p, obj);
268   }
269 }
270 
271 ShenandoahObjToScanQueueSet* ShenandoahMark::task_queues() const {
272   return _task_queues;
273 }
274 
275 ShenandoahObjToScanQueue* ShenandoahMark::get_queue(uint index) const {
276   return _task_queues->queue(index);
277 }
278 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHMARK_INLINE_HPP