1 /* 2 * Copyright (c) 2005, 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/systemDictionary.hpp" 27 #include "gc/parallel/gcTaskManager.hpp" 28 #include "gc/parallel/objectStartArray.hpp" 29 #include "gc/parallel/parMarkBitMap.inline.hpp" 30 #include "gc/parallel/parallelScavengeHeap.hpp" 31 #include "gc/parallel/psCompactionManager.inline.hpp" 32 #include "gc/parallel/psOldGen.hpp" 33 #include "gc/parallel/psParallelCompact.inline.hpp" 34 #include "gc/shared/taskqueue.inline.hpp" 35 #include "logging/log.hpp" 36 #include "memory/iterator.inline.hpp" 37 #include "oops/instanceKlass.inline.hpp" 38 #include "oops/instanceMirrorKlass.inline.hpp" 39 #include "oops/objArrayKlass.inline.hpp" 40 #include "oops/oop.inline.hpp" 41 #include "runtime/atomic.hpp" 42 43 PSOldGen* ParCompactionManager::_old_gen = NULL; 44 ParCompactionManager** ParCompactionManager::_manager_array = NULL; 45 46 OopTaskQueueSet* ParCompactionManager::_stack_array = NULL; 47 ParCompactionManager::ObjArrayTaskQueueSet* 48 ParCompactionManager::_objarray_queues = NULL; 49 ObjectStartArray* ParCompactionManager::_start_array = NULL; 50 ParMarkBitMap* ParCompactionManager::_mark_bitmap = NULL; 51 RegionTaskQueueSet* ParCompactionManager::_region_array = NULL; 52 53 ParCompactionManager::ParCompactionManager() : 54 _action(CopyAndUpdate) { 55 56 ParallelScavengeHeap* heap = ParallelScavengeHeap::heap(); 57 58 _old_gen = heap->old_gen(); 59 _start_array = old_gen()->start_array(); 60 61 marking_stack()->initialize(); 62 _objarray_stack.initialize(); 63 _region_stack.initialize(); 64 65 reset_bitmap_query_cache(); 66 } 67 68 void ParCompactionManager::initialize(ParMarkBitMap* mbm) { 69 assert(PSParallelCompact::gc_task_manager() != NULL, 70 "Needed for initialization"); 71 72 _mark_bitmap = mbm; 73 74 uint parallel_gc_threads = PSParallelCompact::gc_task_manager()->workers(); 75 76 assert(_manager_array == NULL, "Attempt to initialize twice"); 77 _manager_array = NEW_C_HEAP_ARRAY(ParCompactionManager*, parallel_gc_threads+1, mtGC); 78 guarantee(_manager_array != NULL, "Could not allocate manager_array"); 79 80 _stack_array = new OopTaskQueueSet(parallel_gc_threads); 81 guarantee(_stack_array != NULL, "Could not allocate stack_array"); 82 _objarray_queues = new ObjArrayTaskQueueSet(parallel_gc_threads); 83 guarantee(_objarray_queues != NULL, "Could not allocate objarray_queues"); 84 _region_array = new RegionTaskQueueSet(parallel_gc_threads); 85 guarantee(_region_array != NULL, "Could not allocate region_array"); 86 87 // Create and register the ParCompactionManager(s) for the worker threads. 88 for(uint i=0; i<parallel_gc_threads; i++) { 89 _manager_array[i] = new ParCompactionManager(); 90 guarantee(_manager_array[i] != NULL, "Could not create ParCompactionManager"); 91 stack_array()->register_queue(i, _manager_array[i]->marking_stack()); 92 _objarray_queues->register_queue(i, &_manager_array[i]->_objarray_stack); 93 region_array()->register_queue(i, _manager_array[i]->region_stack()); 94 } 95 96 // The VMThread gets its own ParCompactionManager, which is not available 97 // for work stealing. 98 _manager_array[parallel_gc_threads] = new ParCompactionManager(); 99 guarantee(_manager_array[parallel_gc_threads] != NULL, 100 "Could not create ParCompactionManager"); 101 assert(PSParallelCompact::gc_task_manager()->workers() != 0, 102 "Not initialized?"); 103 } 104 105 void ParCompactionManager::reset_all_bitmap_query_caches() { 106 uint parallel_gc_threads = PSParallelCompact::gc_task_manager()->workers(); 107 for (uint i=0; i<=parallel_gc_threads; i++) { 108 _manager_array[i]->reset_bitmap_query_cache(); 109 } 110 } 111 112 bool ParCompactionManager::should_update() { 113 assert(action() != NotValid, "Action is not set"); 114 return (action() == ParCompactionManager::Update) || 115 (action() == ParCompactionManager::CopyAndUpdate) || 116 (action() == ParCompactionManager::UpdateAndCopy); 117 } 118 119 bool ParCompactionManager::should_copy() { 120 assert(action() != NotValid, "Action is not set"); 121 return (action() == ParCompactionManager::Copy) || 122 (action() == ParCompactionManager::CopyAndUpdate) || 123 (action() == ParCompactionManager::UpdateAndCopy); 124 } 125 126 ParCompactionManager* 127 ParCompactionManager::gc_thread_compaction_manager(uint index) { 128 assert(index < ParallelGCThreads, "index out of range"); 129 assert(_manager_array != NULL, "Sanity"); 130 return _manager_array[index]; 131 } 132 133 void InstanceKlass::oop_pc_follow_contents(oop obj, ParCompactionManager* cm) { 134 assert(obj != NULL, "can't follow the content of NULL object"); 135 136 cm->follow_klass(this); 137 // Only mark the header and let the scan of the meta-data mark 138 // everything else. 139 140 ParCompactionManager::MarkAndPushClosure cl(cm); 141 InstanceKlass::oop_oop_iterate_oop_maps<true>(obj, &cl); 142 } 143 144 void InstanceMirrorKlass::oop_pc_follow_contents(oop obj, ParCompactionManager* cm) { 145 InstanceKlass::oop_pc_follow_contents(obj, cm); 146 147 // Follow the klass field in the mirror. 148 Klass* klass = java_lang_Class::as_Klass(obj); 149 if (klass != NULL) { 150 // An anonymous class doesn't have its own class loader, so the call 151 // to follow_klass will mark and push its java mirror instead of the 152 // class loader. When handling the java mirror for an anonymous class 153 // we need to make sure its class loader data is claimed, this is done 154 // by calling follow_class_loader explicitly. For non-anonymous classes 155 // the call to follow_class_loader is made when the class loader itself 156 // is handled. 157 if (klass->is_instance_klass() && InstanceKlass::cast(klass)->is_anonymous()) { 158 cm->follow_class_loader(klass->class_loader_data()); 159 } else { 160 cm->follow_klass(klass); 161 } 162 } else { 163 // If klass is NULL then this a mirror for a primitive type. 164 // We don't have to follow them, since they are handled as strong 165 // roots in Universe::oops_do. 166 assert(java_lang_Class::is_primitive(obj), "Sanity check"); 167 } 168 169 ParCompactionManager::MarkAndPushClosure cl(cm); 170 oop_oop_iterate_statics<true>(obj, &cl); 171 } 172 173 void InstanceClassLoaderKlass::oop_pc_follow_contents(oop obj, ParCompactionManager* cm) { 174 InstanceKlass::oop_pc_follow_contents(obj, cm); 175 176 ClassLoaderData * const loader_data = java_lang_ClassLoader::loader_data(obj); 177 if (loader_data != NULL) { 178 cm->follow_class_loader(loader_data); 179 } 180 } 181 182 template <class T> 183 static void oop_pc_follow_contents_specialized(InstanceRefKlass* klass, oop obj, ParCompactionManager* cm) { 184 T* referent_addr = (T*)java_lang_ref_Reference::referent_addr_raw(obj); 185 T heap_oop = oopDesc::load_heap_oop(referent_addr); 186 log_develop_trace(gc, ref)("InstanceRefKlass::oop_pc_follow_contents " PTR_FORMAT, p2i(obj)); 187 if (!oopDesc::is_null(heap_oop)) { 188 oop referent = oopDesc::decode_heap_oop_not_null(heap_oop); 189 if (PSParallelCompact::mark_bitmap()->is_unmarked(referent) && 190 PSParallelCompact::ref_processor()->discover_reference(obj, klass->reference_type())) { 191 // reference already enqueued, referent will be traversed later 192 klass->InstanceKlass::oop_pc_follow_contents(obj, cm); 193 log_develop_trace(gc, ref)(" Non NULL enqueued " PTR_FORMAT, p2i(obj)); 194 return; 195 } else { 196 // treat referent as normal oop 197 log_develop_trace(gc, ref)(" Non NULL normal " PTR_FORMAT, p2i(obj)); 198 cm->mark_and_push(referent_addr); 199 } 200 } 201 T* next_addr = (T*)java_lang_ref_Reference::next_addr_raw(obj); 202 // Treat discovered as normal oop, if ref is not "active", 203 // i.e. if next is non-NULL. 204 T next_oop = oopDesc::load_heap_oop(next_addr); 205 if (!oopDesc::is_null(next_oop)) { // i.e. ref is not "active" 206 T* discovered_addr = (T*)java_lang_ref_Reference::discovered_addr_raw(obj); 207 log_develop_trace(gc, ref)(" Process discovered as normal " PTR_FORMAT, p2i(discovered_addr)); 208 cm->mark_and_push(discovered_addr); 209 } 210 cm->mark_and_push(next_addr); 211 klass->InstanceKlass::oop_pc_follow_contents(obj, cm); 212 } 213 214 215 void InstanceRefKlass::oop_pc_follow_contents(oop obj, ParCompactionManager* cm) { 216 if (UseCompressedOops) { 217 oop_pc_follow_contents_specialized<narrowOop>(this, obj, cm); 218 } else { 219 oop_pc_follow_contents_specialized<oop>(this, obj, cm); 220 } 221 } 222 223 void ObjArrayKlass::oop_pc_follow_contents(oop obj, ParCompactionManager* cm) { 224 cm->follow_klass(this); 225 226 if (UseCompressedOops) { 227 oop_pc_follow_contents_specialized<narrowOop>(objArrayOop(obj), 0, cm); 228 } else { 229 oop_pc_follow_contents_specialized<oop>(objArrayOop(obj), 0, cm); 230 } 231 } 232 233 void TypeArrayKlass::oop_pc_follow_contents(oop obj, ParCompactionManager* cm) { 234 assert(obj->is_typeArray(),"must be a type array"); 235 // Performance tweak: We skip iterating over the klass pointer since we 236 // know that Universe::TypeArrayKlass never moves. 237 } 238 239 void ParCompactionManager::follow_marking_stacks() { 240 do { 241 // Drain the overflow stack first, to allow stealing from the marking stack. 242 oop obj; 243 while (marking_stack()->pop_overflow(obj)) { 244 follow_contents(obj); 245 } 246 while (marking_stack()->pop_local(obj)) { 247 follow_contents(obj); 248 } 249 250 // Process ObjArrays one at a time to avoid marking stack bloat. 251 ObjArrayTask task; 252 if (_objarray_stack.pop_overflow(task) || _objarray_stack.pop_local(task)) { 253 follow_contents((objArrayOop)task.obj(), task.index()); 254 } 255 } while (!marking_stacks_empty()); 256 257 assert(marking_stacks_empty(), "Sanity"); 258 } 259 260 void ParCompactionManager::drain_region_stacks() { 261 do { 262 // Drain overflow stack first so other threads can steal. 263 size_t region_index; 264 while (region_stack()->pop_overflow(region_index)) { 265 PSParallelCompact::fill_and_update_region(this, region_index); 266 } 267 268 while (region_stack()->pop_local(region_index)) { 269 PSParallelCompact::fill_and_update_region(this, region_index); 270 } 271 } while (!region_stack()->is_empty()); 272 }