1 /* 2 * Copyright (c) 2005, 2015, 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/systemDictionary.hpp" 27 #include "code/codeCache.hpp" 28 #include "gc/parallel/parallelScavengeHeap.hpp" 29 #include "gc/parallel/pcTasks.hpp" 30 #include "gc/parallel/psCompactionManager.inline.hpp" 31 #include "gc/parallel/psParallelCompact.hpp" 32 #include "gc/shared/collectedHeap.hpp" 33 #include "gc/shared/gcTimer.hpp" 34 #include "gc/shared/gcTraceTime.hpp" 35 #include "logging/log.hpp" 36 #include "memory/universe.hpp" 37 #include "oops/objArrayKlass.inline.hpp" 38 #include "oops/oop.inline.hpp" 39 #include "prims/jvmtiExport.hpp" 40 #include "runtime/fprofiler.hpp" 41 #include "runtime/jniHandles.hpp" 42 #include "runtime/thread.hpp" 43 #include "runtime/vmThread.hpp" 44 #include "services/management.hpp" 45 #include "utilities/stack.inline.hpp" 46 47 // 48 // ThreadRootsMarkingTask 49 // 50 51 void ThreadRootsMarkingTask::do_it(GCTaskManager* manager, uint which) { 52 assert(ParallelScavengeHeap::heap()->is_gc_active(), "called outside gc"); 53 54 ResourceMark rm; 55 56 ParCompactionManager* cm = 57 ParCompactionManager::gc_thread_compaction_manager(which); 58 59 ParCompactionManager::MarkAndPushClosure mark_and_push_closure(cm); 60 CLDToOopClosure mark_and_push_from_clds(&mark_and_push_closure, true); 61 MarkingCodeBlobClosure mark_and_push_in_blobs(&mark_and_push_closure, !CodeBlobToOopClosure::FixRelocations); 62 63 if (_java_thread != NULL) 64 _java_thread->oops_do( 65 &mark_and_push_closure, 66 &mark_and_push_from_clds, 67 &mark_and_push_in_blobs); 68 69 if (_vm_thread != NULL) 70 _vm_thread->oops_do( 71 &mark_and_push_closure, 72 &mark_and_push_from_clds, 73 &mark_and_push_in_blobs); 74 75 // Do the real work 76 cm->follow_marking_stacks(); 77 } 78 79 80 void MarkFromRootsTask::do_it(GCTaskManager* manager, uint which) { 81 assert(ParallelScavengeHeap::heap()->is_gc_active(), "called outside gc"); 82 ParCompactionManager* cm = 83 ParCompactionManager::gc_thread_compaction_manager(which); 84 ParCompactionManager::MarkAndPushClosure mark_and_push_closure(cm); 85 ParCompactionManager::FollowKlassClosure follow_klass_closure(&mark_and_push_closure); 86 87 switch (_root_type) { 88 case universe: 89 Universe::oops_do(&mark_and_push_closure); 90 break; 91 92 case jni_handles: 93 JNIHandles::oops_do(&mark_and_push_closure); 94 break; 95 96 case threads: 97 { 98 ResourceMark rm; 99 MarkingCodeBlobClosure each_active_code_blob(&mark_and_push_closure, !CodeBlobToOopClosure::FixRelocations); 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; 104 105 case object_synchronizer: 106 ObjectSynchronizer::oops_do(&mark_and_push_closure); 107 break; 108 109 case flat_profiler: 110 FlatProfiler::oops_do(&mark_and_push_closure); 111 break; 112 113 case management: 114 Management::oops_do(&mark_and_push_closure); 115 break; 116 117 case jvmti: 118 JvmtiExport::oops_do(&mark_and_push_closure); 119 break; 120 121 case system_dictionary: 122 SystemDictionary::always_strong_oops_do(&mark_and_push_closure); 123 break; 124 125 case class_loader_data: 126 ClassLoaderDataGraph::always_strong_oops_do(&mark_and_push_closure, &follow_klass_closure, true); 127 break; 128 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; 133 134 default: 135 fatal("Unknown root type"); 136 } 137 138 // Do the real work 139 cm->follow_marking_stacks(); 140 } 141 142 143 // 144 // RefProcTaskProxy 145 // 146 147 void RefProcTaskProxy::do_it(GCTaskManager* manager, uint which) 148 { 149 assert(ParallelScavengeHeap::heap()->is_gc_active(), "called outside gc"); 150 ParCompactionManager* cm = ParCompactionManager::gc_thread_compaction_manager(which); 151 ParCompactionManager::MarkAndPushClosure mark_and_push_closure(cm); 152 ParCompactionManager::FollowStackClosure follow_stack_closure(cm); 153 _rp_task.work(_work_id, *PSParallelCompact::is_alive_closure(), 154 mark_and_push_closure, follow_stack_closure); 155 } 156 157 // 158 // RefProcTaskExecutor 159 // 160 161 void RefProcTaskExecutor::execute(ProcessTask& task) 162 { 163 ParallelScavengeHeap* heap = ParallelScavengeHeap::heap(); 164 uint parallel_gc_threads = heap->gc_task_manager()->workers(); 165 uint active_gc_threads = heap->gc_task_manager()->active_workers(); 166 RegionTaskQueueSet* qset = ParCompactionManager::region_array(); 167 ParallelTaskTerminator terminator(active_gc_threads, qset); 168 GCTaskQueue* q = GCTaskQueue::create(); 169 for(uint i=0; i<parallel_gc_threads; i++) { 170 q->enqueue(new RefProcTaskProxy(task, i)); 171 } 172 if (task.marks_oops_alive()) { 173 if (parallel_gc_threads>1) { 174 for (uint j=0; j<active_gc_threads; j++) { 175 q->enqueue(new StealMarkingTask(&terminator)); 176 } 177 } 178 } 179 PSParallelCompact::gc_task_manager()->execute_and_wait(q); 180 } 181 182 void RefProcTaskExecutor::execute(EnqueueTask& task) 183 { 184 ParallelScavengeHeap* heap = ParallelScavengeHeap::heap(); 185 uint parallel_gc_threads = heap->gc_task_manager()->workers(); 186 GCTaskQueue* q = GCTaskQueue::create(); 187 for(uint i=0; i<parallel_gc_threads; i++) { 188 q->enqueue(new RefEnqueueTaskProxy(task, i)); 189 } 190 PSParallelCompact::gc_task_manager()->execute_and_wait(q); 191 } 192 193 // 194 // StealMarkingTask 195 // 196 197 StealMarkingTask::StealMarkingTask(ParallelTaskTerminator* t) : 198 _terminator(t) {} 199 200 void StealMarkingTask::do_it(GCTaskManager* manager, uint which) { 201 assert(ParallelScavengeHeap::heap()->is_gc_active(), "called outside gc"); 202 203 ParCompactionManager* cm = ParCompactionManager::gc_thread_compaction_manager(which); 204 ParCompactionManager::MarkAndPushClosure mark_and_push_closure(cm); 205 206 oop obj = NULL; 207 ObjArrayTask task; 208 int random_seed = 17; 209 do { 210 while (ParCompactionManager::steal_objarray(which, &random_seed, task)) { 211 cm->follow_contents((objArrayOop)task.obj(), task.index()); 212 cm->follow_marking_stacks(); 213 } 214 while (ParCompactionManager::steal(which, &random_seed, obj)) { 215 cm->follow_contents(obj); 216 cm->follow_marking_stacks(); 217 } 218 } while (!terminator()->offer_termination()); 219 } 220 221 // 222 // StealRegionCompactionTask 223 // 224 225 StealRegionCompactionTask::StealRegionCompactionTask(ParallelTaskTerminator* t): 226 _terminator(t) {} 227 228 void StealRegionCompactionTask::do_it(GCTaskManager* manager, uint which) { 229 assert(ParallelScavengeHeap::heap()->is_gc_active(), "called outside gc"); 230 231 ParCompactionManager* cm = ParCompactionManager::gc_thread_compaction_manager(which); 232 233 // If not all threads are active, get a draining stack 234 // from the list. Else, just use this threads draining stack. 235 uint which_stack_index; 236 bool use_all_workers = manager->all_workers_active(); 237 if (use_all_workers) { 238 which_stack_index = which; 239 assert(manager->active_workers() == ParallelGCThreads, 240 "all_workers_active has been incorrectly set: " 241 " active %d ParallelGCThreads %u", manager->active_workers(), 242 ParallelGCThreads); 243 } else { 244 which_stack_index = ParCompactionManager::pop_recycled_stack_index(); 245 } 246 247 cm->set_region_stack_index(which_stack_index); 248 cm->set_region_stack(ParCompactionManager::region_list(which_stack_index)); 249 250 // Has to drain stacks first because there may be regions on 251 // preloaded onto the stack and this thread may never have 252 // done a draining task. Are the draining tasks needed? 253 254 cm->drain_region_stacks(); 255 256 size_t region_index = 0; 257 int random_seed = 17; 258 259 // If we're the termination task, try 10 rounds of stealing before 260 // setting the termination flag 261 262 while(true) { 263 if (ParCompactionManager::steal(which, &random_seed, region_index)) { 264 PSParallelCompact::fill_and_update_region(cm, region_index); 265 cm->drain_region_stacks(); 266 } else { 267 if (terminator()->offer_termination()) { 268 break; 269 } 270 // Go around again. 271 } 272 } 273 return; 274 } 275 276 UpdateDensePrefixTask::UpdateDensePrefixTask( 277 PSParallelCompact::SpaceId space_id, 278 size_t region_index_start, 279 size_t region_index_end) : 280 _space_id(space_id), _region_index_start(region_index_start), 281 _region_index_end(region_index_end) {} 282 283 void UpdateDensePrefixTask::do_it(GCTaskManager* manager, uint which) { 284 ParCompactionManager* cm = ParCompactionManager::gc_thread_compaction_manager(which); 285 286 PSParallelCompact::update_and_deadwood_in_dense_prefix(cm, 287 _space_id, 288 _region_index_start, 289 _region_index_end); 290 } 291 292 void DrainStacksCompactionTask::do_it(GCTaskManager* manager, uint which) { 293 assert(ParallelScavengeHeap::heap()->is_gc_active(), "called outside gc"); 294 ParCompactionManager* cm = ParCompactionManager::gc_thread_compaction_manager(which); 295 296 uint which_stack_index; 297 bool use_all_workers = manager->all_workers_active(); 298 if (use_all_workers) { 299 which_stack_index = which; 300 assert(manager->active_workers() == ParallelGCThreads, 301 "all_workers_active has been incorrectly set: " 302 " active %d ParallelGCThreads %u", manager->active_workers(), 303 ParallelGCThreads); 304 } else { 305 which_stack_index = stack_index(); 306 } 307 308 cm->set_region_stack(ParCompactionManager::region_list(which_stack_index)); 309 310 cm->set_region_stack_index(which_stack_index); 311 312 // Process any regions already in the compaction managers stacks. 313 cm->drain_region_stacks(); 314 315 assert(cm->region_stack()->is_empty(), "Not empty"); 316 317 if (!use_all_workers) { 318 // Always give up the region stack. 319 assert(cm->region_stack() == 320 ParCompactionManager::region_list(cm->region_stack_index()), 321 "region_stack and region_stack_index are inconsistent"); 322 ParCompactionManager::push_recycled_stack_index(cm->region_stack_index()); 323 324 cm->set_region_stack(NULL); 325 cm->set_region_stack_index((uint)max_uintx); 326 } 327 }