1 /*
  2  * Copyright Amazon.com Inc. 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 
 26 #include "precompiled.hpp"
 27 
 28 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
 29 #include "gc/shenandoah/shenandoahOopClosures.inline.hpp"
 30 #include "gc/shenandoah/shenandoahReferenceProcessor.hpp"
 31 #include "gc/shenandoah/shenandoahScanRemembered.inline.hpp"
 32 #include "logging/log.hpp"
 33 
 34 ShenandoahDirectCardMarkRememberedSet::ShenandoahDirectCardMarkRememberedSet(ShenandoahCardTable* card_table, size_t total_card_count) {
 35   _heap = ShenandoahHeap::heap();
 36   _card_table = card_table;
 37   _total_card_count = total_card_count;
 38   _cluster_count = total_card_count / ShenandoahCardCluster<ShenandoahDirectCardMarkRememberedSet>::CardsPerCluster;
 39   _card_shift = CardTable::card_shift();
 40 
 41   _byte_map = _card_table->byte_for_index(0);
 42 
 43   _whole_heap_base = _card_table->addr_for(_byte_map);
 44   _byte_map_base = _byte_map - (uintptr_t(_whole_heap_base) >> _card_shift);
 45 
 46   assert(total_card_count % ShenandoahCardCluster<ShenandoahDirectCardMarkRememberedSet>::CardsPerCluster == 0, "Invalid card count.");
 47   assert(total_card_count > 0, "Card count cannot be zero.");
 48 }
 49 
 50 ShenandoahScanRememberedTask::ShenandoahScanRememberedTask(ShenandoahObjToScanQueueSet* queue_set,
 51                                                            ShenandoahObjToScanQueueSet* old_queue_set,
 52                                                            ShenandoahReferenceProcessor* rp,
 53                                                            ShenandoahRegionChunkIterator* work_list, bool is_concurrent) :
 54   WorkerTask("Scan Remembered Set"),
 55   _queue_set(queue_set), _old_queue_set(old_queue_set), _rp(rp), _work_list(work_list), _is_concurrent(is_concurrent) {}
 56 
 57 void ShenandoahScanRememberedTask::work(uint worker_id) {
 58   if (_is_concurrent) {
 59     // This sets up a thread local reference to the worker_id which is needed by the weak reference processor.
 60     ShenandoahConcurrentWorkerSession worker_session(worker_id);
 61     ShenandoahSuspendibleThreadSetJoiner stsj(ShenandoahSuspendibleWorkers);
 62     do_work(worker_id);
 63   } else {
 64     // This sets up a thread local reference to the worker_id which is needed by the weak reference processor.
 65     ShenandoahParallelWorkerSession worker_session(worker_id);
 66     do_work(worker_id);
 67   }
 68 }
 69 
 70 void ShenandoahScanRememberedTask::do_work(uint worker_id) {
 71   ShenandoahWorkerTimingsTracker x(ShenandoahPhaseTimings::init_scan_rset, ShenandoahPhaseTimings::ScanClusters, worker_id);
 72 
 73   ShenandoahObjToScanQueue* q = _queue_set->queue(worker_id);
 74   ShenandoahObjToScanQueue* old = _old_queue_set == nullptr ? nullptr : _old_queue_set->queue(worker_id);
 75   ShenandoahMarkRefsClosure<YOUNG> cl(q, _rp, old);
 76   ShenandoahHeap* heap = ShenandoahHeap::heap();
 77   RememberedScanner* scanner = heap->card_scan();
 78 
 79   // set up thread local closure for shen ref processor
 80   _rp->set_mark_closure(worker_id, &cl);
 81   struct ShenandoahRegionChunk assignment;
 82   while (_work_list->next(&assignment)) {
 83     ShenandoahHeapRegion* region = assignment._r;
 84     log_debug(gc)("ShenandoahScanRememberedTask::do_work(%u), processing slice of region "
 85                   SIZE_FORMAT " at offset " SIZE_FORMAT ", size: " SIZE_FORMAT,
 86                   worker_id, region->index(), assignment._chunk_offset, assignment._chunk_size);
 87     if (region->is_old()) {
 88       size_t cluster_size =
 89         CardTable::card_size_in_words() * ShenandoahCardCluster<ShenandoahDirectCardMarkRememberedSet>::CardsPerCluster;
 90       size_t clusters = assignment._chunk_size / cluster_size;
 91       assert(clusters * cluster_size == assignment._chunk_size, "Chunk assignments must align on cluster boundaries");
 92       HeapWord* end_of_range = region->bottom() + assignment._chunk_offset + assignment._chunk_size;
 93 
 94       // During concurrent mark, region->top() equals TAMS with respect to the current young-gen pass.
 95       if (end_of_range > region->top()) {
 96         end_of_range = region->top();
 97       }
 98       scanner->process_region_slice(region, assignment._chunk_offset, clusters, end_of_range, &cl, false, worker_id);
 99     }
100 #ifdef ENABLE_REMEMBERED_SET_CANCELLATION
101     // This check is currently disabled to avoid crashes that occur
102     // when we try to cancel remembered set scanning; it should be re-enabled
103     // after the issues are fixed, as it would allow more prompt cancellation and
104     // transition to degenerated / full GCs. Note that work that has been assigned/
105     // claimed above must be completed before we return here upon cancellation.
106     if (heap->check_cancelled_gc_and_yield(_is_concurrent)) {
107       return;
108     }
109 #endif
110   }
111 }
112 
113 size_t ShenandoahRegionChunkIterator::calc_regular_group_size() {
114   // The group size is calculated from the number of regions.  Suppose the heap has N regions.  The first group processes
115   // N/2 regions.  The second group processes N/4 regions, the third group N/8 regions and so on.
116   // Note that infinite series N/2 + N/4 + N/8 + N/16 + ...  sums to N.
117   //
118   // The normal group size is the number of regions / 2.
119   //
120   // In the case that the region_size_words is greater than _maximum_chunk_size_words, the first group_size is
121   // larger than the normal group size because each chunk in the group will be smaller than the region size.
122   //
123   // The last group also has more than the normal entries because it finishes the total scanning effort.  The chunk sizes are
124   // different for each group.  The intention is that the first group processes roughly half of the heap, the second processes
125   // half of the remaining heap, the third processes half of what remains and so on.  The smallest chunk size
126   // is represented by _smallest_chunk_size_words.  We do not divide work any smaller than this.
127   //
128 
129   size_t group_size = _heap->num_regions() / 2;
130   return group_size;
131 }
132 
133 size_t ShenandoahRegionChunkIterator::calc_first_group_chunk_size_b4_rebalance() {
134   size_t words_in_first_chunk = ShenandoahHeapRegion::region_size_words();
135   return words_in_first_chunk;
136 }
137 
138 size_t ShenandoahRegionChunkIterator::calc_num_groups() {
139   size_t total_heap_size = _heap->num_regions() * ShenandoahHeapRegion::region_size_words();
140   size_t num_groups = 0;
141   size_t cumulative_group_span = 0;
142   size_t current_group_span = _first_group_chunk_size_b4_rebalance * _regular_group_size;
143   size_t smallest_group_span = smallest_chunk_size_words() * _regular_group_size;
144   while ((num_groups < _maximum_groups) && (cumulative_group_span + current_group_span <= total_heap_size)) {
145     num_groups++;
146     cumulative_group_span += current_group_span;
147     if (current_group_span <= smallest_group_span) {
148       break;
149     } else {
150       current_group_span /= 2;    // Each group spans half of what the preceding group spanned.
151     }
152   }
153   // Loop post condition:
154   //   num_groups <= _maximum_groups
155   //   cumulative_group_span is the memory spanned by num_groups
156   //   current_group_span is the span of the last fully populated group (assuming loop iterates at least once)
157   //   each of num_groups is fully populated with _regular_group_size chunks in each
158   // Non post conditions:
159   //   cumulative_group_span may be less than total_heap size for one or more of the folowing reasons
160   //   a) The number of regions remaining to be spanned is smaller than a complete group, or
161   //   b) We have filled up all groups through _maximum_groups and still have not spanned all regions
162 
163   if (cumulative_group_span < total_heap_size) {
164     // We've got more regions to span
165     if ((num_groups < _maximum_groups) && (current_group_span > smallest_group_span)) {
166       num_groups++;             // Place all remaining regions into a new not-full group (chunk_size half that of previous group)
167     }
168     // Else we are unable to create a new group because we've exceed the number of allowed groups or have reached the
169     // minimum chunk size.
170 
171     // Any remaining regions will be treated as if they are part of the most recently created group.  This group will
172     // have more than _regular_group_size chunks within it.
173   }
174   return num_groups;
175 }
176 
177 size_t ShenandoahRegionChunkIterator::calc_total_chunks() {
178   size_t region_size_words = ShenandoahHeapRegion::region_size_words();
179   size_t unspanned_heap_size = _heap->num_regions() * region_size_words;
180   size_t num_chunks = 0;
181   size_t cumulative_group_span = 0;
182   size_t current_group_span = _first_group_chunk_size_b4_rebalance * _regular_group_size;
183   size_t smallest_group_span = smallest_chunk_size_words() * _regular_group_size;
184 
185   // The first group gets special handling because the first chunk size can be no larger than _largest_chunk_size_words
186   if (region_size_words > _maximum_chunk_size_words) {
187     // In the case that we shrink the first group's chunk size, certain other groups will also be subsumed within the first group
188     size_t effective_chunk_size = _first_group_chunk_size_b4_rebalance;
189     while (effective_chunk_size >= _maximum_chunk_size_words) {
190       num_chunks += current_group_span / _maximum_chunk_size_words;
191       unspanned_heap_size -= current_group_span;
192       effective_chunk_size /= 2;
193       current_group_span /= 2;
194     }
195   } else {
196     num_chunks = _regular_group_size;
197     unspanned_heap_size -= current_group_span;
198     current_group_span /= 2;
199   }
200   size_t spanned_groups = 1;
201   while (unspanned_heap_size > 0) {
202     if (current_group_span <= unspanned_heap_size) {
203       unspanned_heap_size -= current_group_span;
204       num_chunks += _regular_group_size;
205       spanned_groups++;
206 
207       // _num_groups is the number of groups required to span the configured heap size.  We are not allowed
208       // to change the number of groups.  The last group is responsible for spanning all chunks not spanned
209       // by previously processed groups.
210       if (spanned_groups >= _num_groups) {
211         // The last group has more than _regular_group_size entries.
212         size_t chunk_span = current_group_span / _regular_group_size;
213         size_t extra_chunks = unspanned_heap_size / chunk_span;
214         assert (extra_chunks * chunk_span == unspanned_heap_size, "Chunks must precisely span regions");
215         num_chunks += extra_chunks;
216         return num_chunks;
217       } else if (current_group_span <= smallest_group_span) {
218         // We cannot introduce new groups because we've reached the lower bound on group size.  So this last
219         // group may hold extra chunks.
220         size_t chunk_span = smallest_chunk_size_words();
221         size_t extra_chunks = unspanned_heap_size / chunk_span;
222         assert (extra_chunks * chunk_span == unspanned_heap_size, "Chunks must precisely span regions");
223         num_chunks += extra_chunks;
224         return num_chunks;
225       } else {
226         current_group_span /= 2;
227       }
228     } else {
229       // This last group has fewer than _regular_group_size entries.
230       size_t chunk_span = current_group_span / _regular_group_size;
231       size_t last_group_size = unspanned_heap_size / chunk_span;
232       assert (last_group_size * chunk_span == unspanned_heap_size, "Chunks must precisely span regions");
233       num_chunks += last_group_size;
234       return num_chunks;
235     }
236   }
237   return num_chunks;
238 }
239 
240 ShenandoahRegionChunkIterator::ShenandoahRegionChunkIterator(size_t worker_count) :
241     ShenandoahRegionChunkIterator(ShenandoahHeap::heap(), worker_count)
242 {
243 }
244 
245 ShenandoahRegionChunkIterator::ShenandoahRegionChunkIterator(ShenandoahHeap* heap, size_t worker_count) :
246     _heap(heap),
247     _regular_group_size(calc_regular_group_size()),
248     _first_group_chunk_size_b4_rebalance(calc_first_group_chunk_size_b4_rebalance()),
249     _num_groups(calc_num_groups()),
250     _total_chunks(calc_total_chunks()),
251     _index(0)
252 {
253 #ifdef ASSERT
254   size_t expected_chunk_size_words = _clusters_in_smallest_chunk * CardTable::card_size_in_words() * ShenandoahCardCluster<ShenandoahDirectCardMarkRememberedSet>::CardsPerCluster;
255   assert(smallest_chunk_size_words() == expected_chunk_size_words, "_smallest_chunk_size (" SIZE_FORMAT") is not valid because it does not equal (" SIZE_FORMAT ")",
256          smallest_chunk_size_words(), expected_chunk_size_words);
257 #endif
258   assert(_num_groups <= _maximum_groups,
259          "The number of remembered set scanning groups must be less than or equal to maximum groups");
260   assert(smallest_chunk_size_words() << (_maximum_groups - 1) == _maximum_chunk_size_words,
261          "Maximum number of groups needs to span maximum chunk size to smallest chunk size");
262 
263   size_t words_in_region = ShenandoahHeapRegion::region_size_words();
264   _region_index[0] = 0;
265   _group_offset[0] = 0;
266   if (words_in_region > _maximum_chunk_size_words) {
267     // In the case that we shrink the first group's chunk size, certain other groups will also be subsumed within the first group
268     size_t num_chunks = 0;
269     size_t effective_chunk_size = _first_group_chunk_size_b4_rebalance;
270     size_t  current_group_span = effective_chunk_size * _regular_group_size;
271     while (effective_chunk_size >= _maximum_chunk_size_words) {
272       num_chunks += current_group_span / _maximum_chunk_size_words;
273       effective_chunk_size /= 2;
274       current_group_span /= 2;
275     }
276     _group_entries[0] = num_chunks;
277     _group_chunk_size[0] = _maximum_chunk_size_words;
278   } else {
279     _group_entries[0] = _regular_group_size;
280     _group_chunk_size[0] = _first_group_chunk_size_b4_rebalance;
281   }
282 
283   size_t previous_group_span = _group_entries[0] * _group_chunk_size[0];
284   for (size_t i = 1; i < _num_groups; i++) {
285     size_t previous_group_entries = (i == 1)? _group_entries[0]: (_group_entries[i-1] - _group_entries[i-2]);
286     _group_chunk_size[i] = _group_chunk_size[i-1] / 2;
287     size_t chunks_in_group = _regular_group_size;
288     size_t this_group_span = _group_chunk_size[i] * chunks_in_group;
289     size_t total_span_of_groups = previous_group_span + this_group_span;
290     _region_index[i] = previous_group_span / words_in_region;
291     _group_offset[i] = previous_group_span % words_in_region;
292     _group_entries[i] = _group_entries[i-1] + _regular_group_size;
293     previous_group_span = total_span_of_groups;
294   }
295   if (_group_entries[_num_groups-1] < _total_chunks) {
296     assert((_total_chunks - _group_entries[_num_groups-1]) * _group_chunk_size[_num_groups-1] + previous_group_span ==
297            heap->num_regions() * words_in_region, "Total region chunks (" SIZE_FORMAT
298            ") do not span total heap regions (" SIZE_FORMAT ")", _total_chunks, _heap->num_regions());
299     previous_group_span += (_total_chunks - _group_entries[_num_groups-1]) * _group_chunk_size[_num_groups-1];
300     _group_entries[_num_groups-1] = _total_chunks;
301   }
302   assert(previous_group_span == heap->num_regions() * words_in_region, "Total region chunks (" SIZE_FORMAT
303          ") do not span total heap regions (" SIZE_FORMAT "): " SIZE_FORMAT " does not equal " SIZE_FORMAT,
304          _total_chunks, _heap->num_regions(), previous_group_span, heap->num_regions() * words_in_region);
305 
306   // Not necessary, but keeps things tidy
307   for (size_t i = _num_groups; i < _maximum_groups; i++) {
308     _region_index[i] = 0;
309     _group_offset[i] = 0;
310     _group_entries[i] = _group_entries[i-1];
311     _group_chunk_size[i] = 0;
312   }
313 }
314 
315 void ShenandoahRegionChunkIterator::reset() {
316   _index = 0;
317 }
318 
319 ShenandoahVerifyNoYoungRefsClosure::ShenandoahVerifyNoYoungRefsClosure():
320   _heap(ShenandoahHeap::heap()) {
321   assert(_heap->mode()->is_generational(), "Don't use when non-generational");
322 }