1 | /* |
2 | * Copyright (c) 2002, 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 | #include "precompiled.hpp" |
26 | #include "classfile/javaClasses.inline.hpp" |
27 | #include "gc/parallel/gcTaskManager.hpp" |
28 | #include "gc/parallel/mutableSpace.hpp" |
29 | #include "gc/parallel/parallelScavengeHeap.hpp" |
30 | #include "gc/parallel/psOldGen.hpp" |
31 | #include "gc/parallel/psPromotionManager.inline.hpp" |
32 | #include "gc/parallel/psScavenge.inline.hpp" |
33 | #include "gc/shared/gcTrace.hpp" |
34 | #include "gc/shared/preservedMarks.inline.hpp" |
35 | #include "gc/shared/taskqueue.inline.hpp" |
36 | #include "logging/log.hpp" |
37 | #include "logging/logStream.hpp" |
38 | #include "memory/allocation.inline.hpp" |
39 | #include "memory/iterator.inline.hpp" |
40 | #include "memory/memRegion.hpp" |
41 | #include "memory/padded.inline.hpp" |
42 | #include "memory/resourceArea.hpp" |
43 | #include "oops/access.inline.hpp" |
44 | #include "oops/compressedOops.inline.hpp" |
45 | |
46 | PaddedEnd<PSPromotionManager>* PSPromotionManager::_manager_array = NULL; |
47 | OopStarTaskQueueSet* PSPromotionManager::_stack_array_depth = NULL; |
48 | PreservedMarksSet* PSPromotionManager::_preserved_marks_set = NULL; |
49 | PSOldGen* PSPromotionManager::_old_gen = NULL; |
50 | MutableSpace* PSPromotionManager::_young_space = NULL; |
51 | |
52 | void PSPromotionManager::initialize() { |
53 | ParallelScavengeHeap* heap = ParallelScavengeHeap::heap(); |
54 | |
55 | _old_gen = heap->old_gen(); |
56 | _young_space = heap->young_gen()->to_space(); |
57 | |
58 | const uint promotion_manager_num = ParallelGCThreads + 1; |
59 | |
60 | // To prevent false sharing, we pad the PSPromotionManagers |
61 | // and make sure that the first instance starts at a cache line. |
62 | assert(_manager_array == NULL, "Attempt to initialize twice" ); |
63 | _manager_array = PaddedArray<PSPromotionManager, mtGC>::create_unfreeable(promotion_manager_num); |
64 | guarantee(_manager_array != NULL, "Could not initialize promotion manager" ); |
65 | |
66 | _stack_array_depth = new OopStarTaskQueueSet(ParallelGCThreads); |
67 | guarantee(_stack_array_depth != NULL, "Could not initialize promotion manager" ); |
68 | |
69 | // Create and register the PSPromotionManager(s) for the worker threads. |
70 | for(uint i=0; i<ParallelGCThreads; i++) { |
71 | stack_array_depth()->register_queue(i, _manager_array[i].claimed_stack_depth()); |
72 | } |
73 | // The VMThread gets its own PSPromotionManager, which is not available |
74 | // for work stealing. |
75 | |
76 | assert(_preserved_marks_set == NULL, "Attempt to initialize twice" ); |
77 | _preserved_marks_set = new PreservedMarksSet(true /* in_c_heap */); |
78 | guarantee(_preserved_marks_set != NULL, "Could not initialize preserved marks set" ); |
79 | _preserved_marks_set->init(promotion_manager_num); |
80 | for (uint i = 0; i < promotion_manager_num; i += 1) { |
81 | _manager_array[i].register_preserved_marks(_preserved_marks_set->get(i)); |
82 | } |
83 | } |
84 | |
85 | // Helper functions to get around the circular dependency between |
86 | // psScavenge.inline.hpp and psPromotionManager.inline.hpp. |
87 | bool PSPromotionManager::should_scavenge(oop* p, bool check_to_space) { |
88 | return PSScavenge::should_scavenge(p, check_to_space); |
89 | } |
90 | bool PSPromotionManager::should_scavenge(narrowOop* p, bool check_to_space) { |
91 | return PSScavenge::should_scavenge(p, check_to_space); |
92 | } |
93 | |
94 | PSPromotionManager* PSPromotionManager::gc_thread_promotion_manager(uint index) { |
95 | assert(index < ParallelGCThreads, "index out of range" ); |
96 | assert(_manager_array != NULL, "Sanity" ); |
97 | return &_manager_array[index]; |
98 | } |
99 | |
100 | PSPromotionManager* PSPromotionManager::vm_thread_promotion_manager() { |
101 | assert(_manager_array != NULL, "Sanity" ); |
102 | return &_manager_array[ParallelGCThreads]; |
103 | } |
104 | |
105 | void PSPromotionManager::pre_scavenge() { |
106 | ParallelScavengeHeap* heap = ParallelScavengeHeap::heap(); |
107 | |
108 | _preserved_marks_set->assert_empty(); |
109 | _young_space = heap->young_gen()->to_space(); |
110 | |
111 | for(uint i=0; i<ParallelGCThreads+1; i++) { |
112 | manager_array(i)->reset(); |
113 | } |
114 | } |
115 | |
116 | bool PSPromotionManager::post_scavenge(YoungGCTracer& gc_tracer) { |
117 | bool promotion_failure_occurred = false; |
118 | |
119 | TASKQUEUE_STATS_ONLY(print_taskqueue_stats()); |
120 | for (uint i = 0; i < ParallelGCThreads + 1; i++) { |
121 | PSPromotionManager* manager = manager_array(i); |
122 | assert(manager->claimed_stack_depth()->is_empty(), "should be empty" ); |
123 | if (manager->_promotion_failed_info.has_failed()) { |
124 | gc_tracer.report_promotion_failed(manager->_promotion_failed_info); |
125 | promotion_failure_occurred = true; |
126 | } |
127 | manager->flush_labs(); |
128 | } |
129 | if (!promotion_failure_occurred) { |
130 | // If there was no promotion failure, the preserved mark stacks |
131 | // should be empty. |
132 | _preserved_marks_set->assert_empty(); |
133 | } |
134 | return promotion_failure_occurred; |
135 | } |
136 | |
137 | #if TASKQUEUE_STATS |
138 | void |
139 | PSPromotionManager::print_local_stats(outputStream* const out, uint i) const { |
140 | #define FMT " " SIZE_FORMAT_W(10) |
141 | out->print_cr("%3u" FMT FMT FMT FMT, i, _masked_pushes, _masked_steals, |
142 | _arrays_chunked, _array_chunks_processed); |
143 | #undef FMT |
144 | } |
145 | |
146 | static const char* const pm_stats_hdr[] = { |
147 | " --------masked------- arrays array" , |
148 | "thr push steal chunked chunks" , |
149 | "--- ---------- ---------- ---------- ----------" |
150 | }; |
151 | |
152 | void |
153 | PSPromotionManager::print_taskqueue_stats() { |
154 | if (!log_is_enabled(Trace, gc, task, stats)) { |
155 | return; |
156 | } |
157 | Log(gc, task, stats) log; |
158 | ResourceMark rm; |
159 | LogStream ls(log.trace()); |
160 | outputStream* out = &ls; |
161 | out->print_cr("== GC Tasks Stats, GC %3d" , |
162 | ParallelScavengeHeap::heap()->total_collections()); |
163 | |
164 | TaskQueueStats totals; |
165 | out->print("thr " ); TaskQueueStats::print_header(1, out); out->cr(); |
166 | out->print("--- " ); TaskQueueStats::print_header(2, out); out->cr(); |
167 | for (uint i = 0; i < ParallelGCThreads + 1; ++i) { |
168 | TaskQueueStats& next = manager_array(i)->_claimed_stack_depth.stats; |
169 | out->print("%3d " , i); next.print(out); out->cr(); |
170 | totals += next; |
171 | } |
172 | out->print("tot " ); totals.print(out); out->cr(); |
173 | |
174 | const uint hlines = sizeof(pm_stats_hdr) / sizeof(pm_stats_hdr[0]); |
175 | for (uint i = 0; i < hlines; ++i) out->print_cr("%s" , pm_stats_hdr[i]); |
176 | for (uint i = 0; i < ParallelGCThreads + 1; ++i) { |
177 | manager_array(i)->print_local_stats(out, i); |
178 | } |
179 | } |
180 | |
181 | void |
182 | PSPromotionManager::reset_stats() { |
183 | claimed_stack_depth()->stats.reset(); |
184 | _masked_pushes = _masked_steals = 0; |
185 | _arrays_chunked = _array_chunks_processed = 0; |
186 | } |
187 | #endif // TASKQUEUE_STATS |
188 | |
189 | PSPromotionManager::PSPromotionManager() { |
190 | ParallelScavengeHeap* heap = ParallelScavengeHeap::heap(); |
191 | |
192 | // We set the old lab's start array. |
193 | _old_lab.set_start_array(old_gen()->start_array()); |
194 | |
195 | uint queue_size; |
196 | claimed_stack_depth()->initialize(); |
197 | queue_size = claimed_stack_depth()->max_elems(); |
198 | |
199 | _totally_drain = (ParallelGCThreads == 1) || (GCDrainStackTargetSize == 0); |
200 | if (_totally_drain) { |
201 | _target_stack_size = 0; |
202 | } else { |
203 | // don't let the target stack size to be more than 1/4 of the entries |
204 | _target_stack_size = (uint) MIN2((uint) GCDrainStackTargetSize, |
205 | (uint) (queue_size / 4)); |
206 | } |
207 | |
208 | _array_chunk_size = ParGCArrayScanChunk; |
209 | // let's choose 1.5x the chunk size |
210 | _min_array_size_for_chunking = 3 * _array_chunk_size / 2; |
211 | |
212 | _preserved_marks = NULL; |
213 | |
214 | reset(); |
215 | } |
216 | |
217 | void PSPromotionManager::reset() { |
218 | assert(stacks_empty(), "reset of non-empty stack" ); |
219 | |
220 | // We need to get an assert in here to make sure the labs are always flushed. |
221 | |
222 | ParallelScavengeHeap* heap = ParallelScavengeHeap::heap(); |
223 | |
224 | // Do not prefill the LAB's, save heap wastage! |
225 | HeapWord* lab_base = young_space()->top(); |
226 | _young_lab.initialize(MemRegion(lab_base, (size_t)0)); |
227 | _young_gen_is_full = false; |
228 | |
229 | lab_base = old_gen()->object_space()->top(); |
230 | _old_lab.initialize(MemRegion(lab_base, (size_t)0)); |
231 | _old_gen_is_full = false; |
232 | |
233 | _promotion_failed_info.reset(); |
234 | |
235 | TASKQUEUE_STATS_ONLY(reset_stats()); |
236 | } |
237 | |
238 | void PSPromotionManager::register_preserved_marks(PreservedMarks* preserved_marks) { |
239 | assert(_preserved_marks == NULL, "do not set it twice" ); |
240 | _preserved_marks = preserved_marks; |
241 | } |
242 | |
243 | class ParRestoreGCTask : public GCTask { |
244 | private: |
245 | const uint _id; |
246 | PreservedMarksSet* const _preserved_marks_set; |
247 | volatile size_t* const _total_size_addr; |
248 | |
249 | public: |
250 | virtual char* name() { |
251 | return (char*) "preserved mark restoration task" ; |
252 | } |
253 | |
254 | virtual void do_it(GCTaskManager* manager, uint which){ |
255 | _preserved_marks_set->get(_id)->restore_and_increment(_total_size_addr); |
256 | } |
257 | |
258 | ParRestoreGCTask(uint id, |
259 | PreservedMarksSet* preserved_marks_set, |
260 | volatile size_t* total_size_addr) |
261 | : _id(id), |
262 | _preserved_marks_set(preserved_marks_set), |
263 | _total_size_addr(total_size_addr) { } |
264 | }; |
265 | |
266 | class PSRestorePreservedMarksTaskExecutor : public RestorePreservedMarksTaskExecutor { |
267 | private: |
268 | GCTaskManager* _gc_task_manager; |
269 | |
270 | public: |
271 | PSRestorePreservedMarksTaskExecutor(GCTaskManager* gc_task_manager) |
272 | : _gc_task_manager(gc_task_manager) { } |
273 | |
274 | void restore(PreservedMarksSet* preserved_marks_set, |
275 | volatile size_t* total_size_addr) { |
276 | // GCTask / GCTaskQueue are ResourceObjs |
277 | ResourceMark rm; |
278 | |
279 | GCTaskQueue* q = GCTaskQueue::create(); |
280 | for (uint i = 0; i < preserved_marks_set->num(); i += 1) { |
281 | q->enqueue(new ParRestoreGCTask(i, preserved_marks_set, total_size_addr)); |
282 | } |
283 | _gc_task_manager->execute_and_wait(q); |
284 | } |
285 | }; |
286 | |
287 | void PSPromotionManager::restore_preserved_marks() { |
288 | PSRestorePreservedMarksTaskExecutor task_executor(PSScavenge::gc_task_manager()); |
289 | _preserved_marks_set->restore(&task_executor); |
290 | } |
291 | |
292 | void PSPromotionManager::drain_stacks_depth(bool totally_drain) { |
293 | totally_drain = totally_drain || _totally_drain; |
294 | |
295 | #ifdef ASSERT |
296 | ParallelScavengeHeap* heap = ParallelScavengeHeap::heap(); |
297 | MutableSpace* to_space = heap->young_gen()->to_space(); |
298 | MutableSpace* old_space = heap->old_gen()->object_space(); |
299 | #endif /* ASSERT */ |
300 | |
301 | OopStarTaskQueue* const tq = claimed_stack_depth(); |
302 | do { |
303 | StarTask p; |
304 | |
305 | // Drain overflow stack first, so other threads can steal from |
306 | // claimed stack while we work. |
307 | while (tq->pop_overflow(p)) { |
308 | process_popped_location_depth(p); |
309 | } |
310 | |
311 | if (totally_drain) { |
312 | while (tq->pop_local(p)) { |
313 | process_popped_location_depth(p); |
314 | } |
315 | } else { |
316 | while (tq->size() > _target_stack_size && tq->pop_local(p)) { |
317 | process_popped_location_depth(p); |
318 | } |
319 | } |
320 | } while ((totally_drain && !tq->taskqueue_empty()) || !tq->overflow_empty()); |
321 | |
322 | assert(!totally_drain || tq->taskqueue_empty(), "Sanity" ); |
323 | assert(totally_drain || tq->size() <= _target_stack_size, "Sanity" ); |
324 | assert(tq->overflow_empty(), "Sanity" ); |
325 | } |
326 | |
327 | void PSPromotionManager::flush_labs() { |
328 | assert(stacks_empty(), "Attempt to flush lab with live stack" ); |
329 | |
330 | // If either promotion lab fills up, we can flush the |
331 | // lab but not refill it, so check first. |
332 | assert(!_young_lab.is_flushed() || _young_gen_is_full, "Sanity" ); |
333 | if (!_young_lab.is_flushed()) |
334 | _young_lab.flush(); |
335 | |
336 | assert(!_old_lab.is_flushed() || _old_gen_is_full, "Sanity" ); |
337 | if (!_old_lab.is_flushed()) |
338 | _old_lab.flush(); |
339 | |
340 | // Let PSScavenge know if we overflowed |
341 | if (_young_gen_is_full) { |
342 | PSScavenge::set_survivor_overflow(true); |
343 | } |
344 | } |
345 | |
346 | template <class T> void PSPromotionManager::process_array_chunk_work( |
347 | oop obj, |
348 | int start, int end) { |
349 | assert(start <= end, "invariant" ); |
350 | T* const base = (T*)objArrayOop(obj)->base(); |
351 | T* p = base + start; |
352 | T* const chunk_end = base + end; |
353 | while (p < chunk_end) { |
354 | if (PSScavenge::should_scavenge(p)) { |
355 | claim_or_forward_depth(p); |
356 | } |
357 | ++p; |
358 | } |
359 | } |
360 | |
361 | void PSPromotionManager::process_array_chunk(oop old) { |
362 | assert(PSChunkLargeArrays, "invariant" ); |
363 | assert(old->is_objArray(), "invariant" ); |
364 | assert(old->is_forwarded(), "invariant" ); |
365 | |
366 | TASKQUEUE_STATS_ONLY(++_array_chunks_processed); |
367 | |
368 | oop const obj = old->forwardee(); |
369 | |
370 | int start; |
371 | int const end = arrayOop(old)->length(); |
372 | if (end > (int) _min_array_size_for_chunking) { |
373 | // we'll chunk more |
374 | start = end - _array_chunk_size; |
375 | assert(start > 0, "invariant" ); |
376 | arrayOop(old)->set_length(start); |
377 | push_depth(mask_chunked_array_oop(old)); |
378 | TASKQUEUE_STATS_ONLY(++_masked_pushes); |
379 | } else { |
380 | // this is the final chunk for this array |
381 | start = 0; |
382 | int const actual_length = arrayOop(obj)->length(); |
383 | arrayOop(old)->set_length(actual_length); |
384 | } |
385 | |
386 | if (UseCompressedOops) { |
387 | process_array_chunk_work<narrowOop>(obj, start, end); |
388 | } else { |
389 | process_array_chunk_work<oop>(obj, start, end); |
390 | } |
391 | } |
392 | |
393 | oop PSPromotionManager::oop_promotion_failed(oop obj, markOop obj_mark) { |
394 | assert(_old_gen_is_full || PromotionFailureALot, "Sanity" ); |
395 | |
396 | // Attempt to CAS in the header. |
397 | // This tests if the header is still the same as when |
398 | // this started. If it is the same (i.e., no forwarding |
399 | // pointer has been installed), then this thread owns |
400 | // it. |
401 | if (obj->cas_forward_to(obj, obj_mark)) { |
402 | // We won any races, we "own" this object. |
403 | assert(obj == obj->forwardee(), "Sanity" ); |
404 | |
405 | _promotion_failed_info.register_copy_failure(obj->size()); |
406 | |
407 | push_contents(obj); |
408 | |
409 | _preserved_marks->push_if_necessary(obj, obj_mark); |
410 | } else { |
411 | // We lost, someone else "owns" this object |
412 | guarantee(obj->is_forwarded(), "Object must be forwarded if the cas failed." ); |
413 | |
414 | // No unallocation to worry about. |
415 | obj = obj->forwardee(); |
416 | } |
417 | |
418 | log_develop_trace(gc, scavenge)("{promotion-failure %s " PTR_FORMAT " (%d)}" , obj->klass()->internal_name(), p2i(obj), obj->size()); |
419 | |
420 | return obj; |
421 | } |
422 | |