Mon, 27 May 2013 15:22:59 +0200
8015268: NPG: 2.5% regression in young GC times on CRM Sales Opty
Summary: Split SystemDictionary and ClassLoaderDataGraph root processing to help load balancing.
Reviewed-by: tschatzl, johnc
1 /*
2 * Copyright (c) 2005, 2013, 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 */
25 #include "precompiled.hpp"
26 #include "classfile/systemDictionary.hpp"
27 #include "code/codeCache.hpp"
28 #include "gc_implementation/parallelScavenge/pcTasks.hpp"
29 #include "gc_implementation/parallelScavenge/psParallelCompact.hpp"
30 #include "gc_interface/collectedHeap.hpp"
31 #include "memory/universe.hpp"
32 #include "oops/objArrayKlass.inline.hpp"
33 #include "oops/oop.inline.hpp"
34 #include "oops/oop.pcgc.inline.hpp"
35 #include "prims/jvmtiExport.hpp"
36 #include "runtime/fprofiler.hpp"
37 #include "runtime/jniHandles.hpp"
38 #include "runtime/thread.hpp"
39 #include "runtime/vmThread.hpp"
40 #include "services/management.hpp"
42 //
43 // ThreadRootsMarkingTask
44 //
46 void ThreadRootsMarkingTask::do_it(GCTaskManager* manager, uint which) {
47 assert(Universe::heap()->is_gc_active(), "called outside gc");
49 ResourceMark rm;
51 NOT_PRODUCT(TraceTime tm("ThreadRootsMarkingTask",
52 PrintGCDetails && TraceParallelOldGCTasks, true, gclog_or_tty));
53 ParCompactionManager* cm =
54 ParCompactionManager::gc_thread_compaction_manager(which);
56 PSParallelCompact::MarkAndPushClosure mark_and_push_closure(cm);
57 CLDToOopClosure mark_and_push_from_clds(&mark_and_push_closure, true);
58 CodeBlobToOopClosure mark_and_push_in_blobs(&mark_and_push_closure, /*do_marking=*/ true);
60 if (_java_thread != NULL)
61 _java_thread->oops_do(
62 &mark_and_push_closure,
63 &mark_and_push_from_clds,
64 &mark_and_push_in_blobs);
66 if (_vm_thread != NULL)
67 _vm_thread->oops_do(
68 &mark_and_push_closure,
69 &mark_and_push_from_clds,
70 &mark_and_push_in_blobs);
72 // Do the real work
73 cm->follow_marking_stacks();
74 }
77 void MarkFromRootsTask::do_it(GCTaskManager* manager, uint which) {
78 assert(Universe::heap()->is_gc_active(), "called outside gc");
80 NOT_PRODUCT(TraceTime tm("MarkFromRootsTask",
81 PrintGCDetails && TraceParallelOldGCTasks, true, gclog_or_tty));
82 ParCompactionManager* cm =
83 ParCompactionManager::gc_thread_compaction_manager(which);
84 PSParallelCompact::MarkAndPushClosure mark_and_push_closure(cm);
85 PSParallelCompact::FollowKlassClosure follow_klass_closure(&mark_and_push_closure);
87 switch (_root_type) {
88 case universe:
89 Universe::oops_do(&mark_and_push_closure);
90 break;
92 case jni_handles:
93 JNIHandles::oops_do(&mark_and_push_closure);
94 break;
96 case threads:
97 {
98 ResourceMark rm;
99 CodeBlobToOopClosure each_active_code_blob(&mark_and_push_closure, /*do_marking=*/ true);
100 CLDToOopClosure mark_and_push_from_cld(&mark_and_push_closure);
101 Threads::oops_do(&mark_and_push_closure, &mark_and_push_from_cld, &each_active_code_blob);
102 }
103 break;
105 case object_synchronizer:
106 ObjectSynchronizer::oops_do(&mark_and_push_closure);
107 break;
109 case flat_profiler:
110 FlatProfiler::oops_do(&mark_and_push_closure);
111 break;
113 case management:
114 Management::oops_do(&mark_and_push_closure);
115 break;
117 case jvmti:
118 JvmtiExport::oops_do(&mark_and_push_closure);
119 break;
121 case system_dictionary:
122 SystemDictionary::always_strong_oops_do(&mark_and_push_closure);
123 break;
125 case class_loader_data:
126 ClassLoaderDataGraph::always_strong_oops_do(&mark_and_push_closure, &follow_klass_closure, true);
127 break;
129 case code_cache:
130 // Do not treat nmethods as strong roots for mark/sweep, since we can unload them.
131 //CodeCache::scavenge_root_nmethods_do(CodeBlobToOopClosure(&mark_and_push_closure));
132 break;
134 default:
135 fatal("Unknown root type");
136 }
138 // Do the real work
139 cm->follow_marking_stacks();
140 }
143 //
144 // RefProcTaskProxy
145 //
147 void RefProcTaskProxy::do_it(GCTaskManager* manager, uint which)
148 {
149 assert(Universe::heap()->is_gc_active(), "called outside gc");
151 NOT_PRODUCT(TraceTime tm("RefProcTask",
152 PrintGCDetails && TraceParallelOldGCTasks, true, gclog_or_tty));
153 ParCompactionManager* cm =
154 ParCompactionManager::gc_thread_compaction_manager(which);
155 PSParallelCompact::MarkAndPushClosure mark_and_push_closure(cm);
156 PSParallelCompact::FollowStackClosure follow_stack_closure(cm);
157 _rp_task.work(_work_id, *PSParallelCompact::is_alive_closure(),
158 mark_and_push_closure, follow_stack_closure);
159 }
161 //
162 // RefProcTaskExecutor
163 //
165 void RefProcTaskExecutor::execute(ProcessTask& task)
166 {
167 ParallelScavengeHeap* heap = PSParallelCompact::gc_heap();
168 uint parallel_gc_threads = heap->gc_task_manager()->workers();
169 uint active_gc_threads = heap->gc_task_manager()->active_workers();
170 RegionTaskQueueSet* qset = ParCompactionManager::region_array();
171 ParallelTaskTerminator terminator(active_gc_threads, qset);
172 GCTaskQueue* q = GCTaskQueue::create();
173 for(uint i=0; i<parallel_gc_threads; i++) {
174 q->enqueue(new RefProcTaskProxy(task, i));
175 }
176 if (task.marks_oops_alive()) {
177 if (parallel_gc_threads>1) {
178 for (uint j=0; j<active_gc_threads; j++) {
179 q->enqueue(new StealMarkingTask(&terminator));
180 }
181 }
182 }
183 PSParallelCompact::gc_task_manager()->execute_and_wait(q);
184 }
186 void RefProcTaskExecutor::execute(EnqueueTask& task)
187 {
188 ParallelScavengeHeap* heap = PSParallelCompact::gc_heap();
189 uint parallel_gc_threads = heap->gc_task_manager()->workers();
190 GCTaskQueue* q = GCTaskQueue::create();
191 for(uint i=0; i<parallel_gc_threads; i++) {
192 q->enqueue(new RefEnqueueTaskProxy(task, i));
193 }
194 PSParallelCompact::gc_task_manager()->execute_and_wait(q);
195 }
197 //
198 // StealMarkingTask
199 //
201 StealMarkingTask::StealMarkingTask(ParallelTaskTerminator* t) :
202 _terminator(t) {}
204 void StealMarkingTask::do_it(GCTaskManager* manager, uint which) {
205 assert(Universe::heap()->is_gc_active(), "called outside gc");
207 NOT_PRODUCT(TraceTime tm("StealMarkingTask",
208 PrintGCDetails && TraceParallelOldGCTasks, true, gclog_or_tty));
210 ParCompactionManager* cm =
211 ParCompactionManager::gc_thread_compaction_manager(which);
212 PSParallelCompact::MarkAndPushClosure mark_and_push_closure(cm);
214 oop obj = NULL;
215 ObjArrayTask task;
216 int random_seed = 17;
217 do {
218 while (ParCompactionManager::steal_objarray(which, &random_seed, task)) {
219 ObjArrayKlass* k = (ObjArrayKlass*)task.obj()->klass();
220 k->oop_follow_contents(cm, task.obj(), task.index());
221 cm->follow_marking_stacks();
222 }
223 while (ParCompactionManager::steal(which, &random_seed, obj)) {
224 obj->follow_contents(cm);
225 cm->follow_marking_stacks();
226 }
227 } while (!terminator()->offer_termination());
228 }
230 //
231 // StealRegionCompactionTask
232 //
234 StealRegionCompactionTask::StealRegionCompactionTask(ParallelTaskTerminator* t):
235 _terminator(t) {}
237 void StealRegionCompactionTask::do_it(GCTaskManager* manager, uint which) {
238 assert(Universe::heap()->is_gc_active(), "called outside gc");
240 NOT_PRODUCT(TraceTime tm("StealRegionCompactionTask",
241 PrintGCDetails && TraceParallelOldGCTasks, true, gclog_or_tty));
243 ParCompactionManager* cm =
244 ParCompactionManager::gc_thread_compaction_manager(which);
247 // If not all threads are active, get a draining stack
248 // from the list. Else, just use this threads draining stack.
249 uint which_stack_index;
250 bool use_all_workers = manager->all_workers_active();
251 if (use_all_workers) {
252 which_stack_index = which;
253 assert(manager->active_workers() == ParallelGCThreads,
254 err_msg("all_workers_active has been incorrectly set: "
255 " active %d ParallelGCThreads %d", manager->active_workers(),
256 ParallelGCThreads));
257 } else {
258 which_stack_index = ParCompactionManager::pop_recycled_stack_index();
259 }
261 cm->set_region_stack_index(which_stack_index);
262 cm->set_region_stack(ParCompactionManager::region_list(which_stack_index));
263 if (TraceDynamicGCThreads) {
264 gclog_or_tty->print_cr("StealRegionCompactionTask::do_it "
265 "region_stack_index %d region_stack = 0x%x "
266 " empty (%d) use all workers %d",
267 which_stack_index, ParCompactionManager::region_list(which_stack_index),
268 cm->region_stack()->is_empty(),
269 use_all_workers);
270 }
272 // Has to drain stacks first because there may be regions on
273 // preloaded onto the stack and this thread may never have
274 // done a draining task. Are the draining tasks needed?
276 cm->drain_region_stacks();
278 size_t region_index = 0;
279 int random_seed = 17;
281 // If we're the termination task, try 10 rounds of stealing before
282 // setting the termination flag
284 while(true) {
285 if (ParCompactionManager::steal(which, &random_seed, region_index)) {
286 PSParallelCompact::fill_and_update_region(cm, region_index);
287 cm->drain_region_stacks();
288 } else {
289 if (terminator()->offer_termination()) {
290 break;
291 }
292 // Go around again.
293 }
294 }
295 return;
296 }
298 UpdateDensePrefixTask::UpdateDensePrefixTask(
299 PSParallelCompact::SpaceId space_id,
300 size_t region_index_start,
301 size_t region_index_end) :
302 _space_id(space_id), _region_index_start(region_index_start),
303 _region_index_end(region_index_end) {}
305 void UpdateDensePrefixTask::do_it(GCTaskManager* manager, uint which) {
307 NOT_PRODUCT(TraceTime tm("UpdateDensePrefixTask",
308 PrintGCDetails && TraceParallelOldGCTasks, true, gclog_or_tty));
310 ParCompactionManager* cm =
311 ParCompactionManager::gc_thread_compaction_manager(which);
313 PSParallelCompact::update_and_deadwood_in_dense_prefix(cm,
314 _space_id,
315 _region_index_start,
316 _region_index_end);
317 }
319 void DrainStacksCompactionTask::do_it(GCTaskManager* manager, uint which) {
320 assert(Universe::heap()->is_gc_active(), "called outside gc");
322 NOT_PRODUCT(TraceTime tm("DrainStacksCompactionTask",
323 PrintGCDetails && TraceParallelOldGCTasks, true, gclog_or_tty));
325 ParCompactionManager* cm =
326 ParCompactionManager::gc_thread_compaction_manager(which);
328 uint which_stack_index;
329 bool use_all_workers = manager->all_workers_active();
330 if (use_all_workers) {
331 which_stack_index = which;
332 assert(manager->active_workers() == ParallelGCThreads,
333 err_msg("all_workers_active has been incorrectly set: "
334 " active %d ParallelGCThreads %d", manager->active_workers(),
335 ParallelGCThreads));
336 } else {
337 which_stack_index = stack_index();
338 }
340 cm->set_region_stack(ParCompactionManager::region_list(which_stack_index));
341 if (TraceDynamicGCThreads) {
342 gclog_or_tty->print_cr("DrainStacksCompactionTask::do_it which = %d "
343 "which_stack_index = %d/empty(%d) "
344 "use all workers %d",
345 which, which_stack_index,
346 cm->region_stack()->is_empty(),
347 use_all_workers);
348 }
350 cm->set_region_stack_index(which_stack_index);
352 // Process any regions already in the compaction managers stacks.
353 cm->drain_region_stacks();
355 assert(cm->region_stack()->is_empty(), "Not empty");
357 if (!use_all_workers) {
358 // Always give up the region stack.
359 assert(cm->region_stack() ==
360 ParCompactionManager::region_list(cm->region_stack_index()),
361 "region_stack and region_stack_index are inconsistent");
362 ParCompactionManager::push_recycled_stack_index(cm->region_stack_index());
364 if (TraceDynamicGCThreads) {
365 void* old_region_stack = (void*) cm->region_stack();
366 int old_region_stack_index = cm->region_stack_index();
367 gclog_or_tty->print_cr("Pushing region stack 0x%x/%d",
368 old_region_stack, old_region_stack_index);
369 }
371 cm->set_region_stack(NULL);
372 cm->set_region_stack_index((uint)max_uintx);
373 }
374 }