1 /* 2 * Copyright (c) 2017, 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 "gc/cms/cmsCardTable.hpp" 27 #include "gc/cms/compactibleFreeListSpace.hpp" 28 #include "gc/cms/concurrentMarkSweepGeneration.hpp" 29 #include "gc/cms/concurrentMarkSweepThread.hpp" 30 #include "gc/cms/cmsHeap.hpp" 31 #include "gc/cms/parNewGeneration.hpp" 32 #include "gc/cms/vmCMSOperations.hpp" 33 #include "gc/shared/genMemoryPools.hpp" 34 #include "gc/shared/genOopClosures.inline.hpp" 35 #include "gc/shared/strongRootsScope.hpp" 36 #include "gc/shared/workgroup.hpp" 37 #include "oops/oop.inline.hpp" 38 #include "runtime/vmThread.hpp" 39 #include "services/memoryManager.hpp" 40 #include "utilities/stack.inline.hpp" 41 42 class CompactibleFreeListSpacePool : public CollectedMemoryPool { 43 private: 44 CompactibleFreeListSpace* _space; 45 public: 46 CompactibleFreeListSpacePool(CompactibleFreeListSpace* space, 47 const char* name, 48 size_t max_size, 49 bool support_usage_threshold) : 50 CollectedMemoryPool(name, space->capacity(), max_size, support_usage_threshold), 51 _space(space) { 52 } 53 54 MemoryUsage get_memory_usage() { 55 size_t max_heap_size = (available_for_allocation() ? max_size() : 0); 56 size_t used = used_in_bytes(); 57 size_t committed = _space->capacity(); 58 59 return MemoryUsage(initial_size(), used, committed, max_heap_size); 60 } 61 62 size_t used_in_bytes() { 63 return _space->used(); 64 } 65 }; 66 67 CMSHeap::CMSHeap(GenCollectorPolicy *policy) : 68 GenCollectedHeap(policy, 69 Generation::ParNew, 70 Generation::ConcurrentMarkSweep, 71 "ParNew::CMS"), 72 _eden_pool(NULL), 73 _survivor_pool(NULL), 74 _old_pool(NULL) { 75 _workers = new WorkGang("GC Thread", ParallelGCThreads, 76 /* are_GC_task_threads */true, 77 /* are_ConcurrentGC_threads */false); 78 _workers->initialize_workers(); 79 } 80 81 jint CMSHeap::initialize() { 82 jint status = GenCollectedHeap::initialize(); 83 if (status != JNI_OK) return status; 84 85 // If we are running CMS, create the collector responsible 86 // for collecting the CMS generations. 87 if (!create_cms_collector()) { 88 return JNI_ENOMEM; 89 } 90 91 return JNI_OK; 92 } 93 94 CardTableRS* CMSHeap::create_rem_set(const MemRegion& reserved_region) { 95 return new CMSCardTable(reserved_region); 96 } 97 98 void CMSHeap::initialize_serviceability() { 99 _young_manager = new GCMemoryManager("ParNew", "end of minor GC"); 100 _old_manager = new GCMemoryManager("ConcurrentMarkSweep", "end of major GC"); 101 102 ParNewGeneration* young = (ParNewGeneration*) young_gen(); 103 _eden_pool = new ContiguousSpacePool(young->eden(), 104 "Par Eden Space", 105 young->max_eden_size(), 106 false); 107 108 _survivor_pool = new SurvivorContiguousSpacePool(young, 109 "Par Survivor Space", 110 young->max_survivor_size(), 111 false); 112 113 ConcurrentMarkSweepGeneration* old = (ConcurrentMarkSweepGeneration*) old_gen(); 114 _old_pool = new CompactibleFreeListSpacePool(old->cmsSpace(), 115 "CMS Old Gen", 116 old->reserved().byte_size(), 117 true); 118 119 _young_manager->add_pool(_eden_pool); 120 _young_manager->add_pool(_survivor_pool); 121 young->set_gc_manager(_young_manager); 122 123 _old_manager->add_pool(_eden_pool); 124 _old_manager->add_pool(_survivor_pool); 125 _old_manager->add_pool(_old_pool); 126 old ->set_gc_manager(_old_manager); 127 128 } 129 130 void CMSHeap::check_gen_kinds() { 131 assert(young_gen()->kind() == Generation::ParNew, 132 "Wrong youngest generation type"); 133 assert(old_gen()->kind() == Generation::ConcurrentMarkSweep, 134 "Wrong generation kind"); 135 } 136 137 CMSHeap* CMSHeap::heap() { 138 CollectedHeap* heap = Universe::heap(); 139 assert(heap != NULL, "Uninitialized access to CMSHeap::heap()"); 140 assert(heap->kind() == CollectedHeap::CMS, "Invalid name"); 141 return (CMSHeap*) heap; 142 } 143 144 void CMSHeap::gc_threads_do(ThreadClosure* tc) const { 145 assert(workers() != NULL, "should have workers here"); 146 workers()->threads_do(tc); 147 ConcurrentMarkSweepThread::threads_do(tc); 148 } 149 150 void CMSHeap::print_gc_threads_on(outputStream* st) const { 151 assert(workers() != NULL, "should have workers here"); 152 workers()->print_worker_threads_on(st); 153 ConcurrentMarkSweepThread::print_all_on(st); 154 } 155 156 void CMSHeap::print_on_error(outputStream* st) const { 157 GenCollectedHeap::print_on_error(st); 158 st->cr(); 159 CMSCollector::print_on_error(st); 160 } 161 162 bool CMSHeap::create_cms_collector() { 163 assert(old_gen()->kind() == Generation::ConcurrentMarkSweep, 164 "Unexpected generation kinds"); 165 CMSCollector* collector = 166 new CMSCollector((ConcurrentMarkSweepGeneration*) old_gen(), 167 rem_set(), 168 (ConcurrentMarkSweepPolicy*) gen_policy()); 169 170 if (collector == NULL || !collector->completed_initialization()) { 171 if (collector) { 172 delete collector; // Be nice in embedded situation 173 } 174 vm_shutdown_during_initialization("Could not create CMS collector"); 175 return false; 176 } 177 return true; // success 178 } 179 180 void CMSHeap::collect(GCCause::Cause cause) { 181 if (should_do_concurrent_full_gc(cause)) { 182 // Mostly concurrent full collection. 183 collect_mostly_concurrent(cause); 184 } else { 185 GenCollectedHeap::collect(cause); 186 } 187 } 188 189 bool CMSHeap::should_do_concurrent_full_gc(GCCause::Cause cause) { 190 switch (cause) { 191 case GCCause::_gc_locker: return GCLockerInvokesConcurrent; 192 case GCCause::_java_lang_system_gc: 193 case GCCause::_dcmd_gc_run: return ExplicitGCInvokesConcurrent; 194 default: return false; 195 } 196 } 197 198 void CMSHeap::collect_mostly_concurrent(GCCause::Cause cause) { 199 assert(!Heap_lock->owned_by_self(), "Should not own Heap_lock"); 200 201 MutexLocker ml(Heap_lock); 202 // Read the GC counts while holding the Heap_lock 203 unsigned int full_gc_count_before = total_full_collections(); 204 unsigned int gc_count_before = total_collections(); 205 { 206 MutexUnlocker mu(Heap_lock); 207 VM_GenCollectFullConcurrent op(gc_count_before, full_gc_count_before, cause); 208 VMThread::execute(&op); 209 } 210 } 211 212 void CMSHeap::stop() { 213 ConcurrentMarkSweepThread::cmst()->stop(); 214 } 215 216 void CMSHeap::safepoint_synchronize_begin() { 217 ConcurrentMarkSweepThread::synchronize(false); 218 } 219 220 void CMSHeap::safepoint_synchronize_end() { 221 ConcurrentMarkSweepThread::desynchronize(false); 222 } 223 224 void CMSHeap::cms_process_roots(StrongRootsScope* scope, 225 bool young_gen_as_roots, 226 ScanningOption so, 227 bool only_strong_roots, 228 OopsInGenClosure* root_closure, 229 CLDClosure* cld_closure) { 230 MarkingCodeBlobClosure mark_code_closure(root_closure, !CodeBlobToOopClosure::FixRelocations); 231 OopsInGenClosure* weak_roots = only_strong_roots ? NULL : root_closure; 232 CLDClosure* weak_cld_closure = only_strong_roots ? NULL : cld_closure; 233 234 process_roots(scope, so, root_closure, weak_roots, cld_closure, weak_cld_closure, &mark_code_closure); 235 if (!only_strong_roots) { 236 process_string_table_roots(scope, root_closure); 237 } 238 239 if (young_gen_as_roots && 240 !_process_strong_tasks->is_task_claimed(GCH_PS_younger_gens)) { 241 root_closure->set_generation(young_gen()); 242 young_gen()->oop_iterate(root_closure); 243 root_closure->reset_generation(); 244 } 245 246 _process_strong_tasks->all_tasks_completed(scope->n_threads()); 247 } 248 249 void CMSHeap::gc_prologue(bool full) { 250 always_do_update_barrier = false; 251 GenCollectedHeap::gc_prologue(full); 252 }; 253 254 void CMSHeap::gc_epilogue(bool full) { 255 GenCollectedHeap::gc_epilogue(full); 256 always_do_update_barrier = true; 257 }; 258 259 GrowableArray<GCMemoryManager*> CMSHeap::memory_managers() { 260 GrowableArray<GCMemoryManager*> memory_managers(2); 261 memory_managers.append(_young_manager); 262 memory_managers.append(_old_manager); 263 return memory_managers; 264 } 265 266 GrowableArray<MemoryPool*> CMSHeap::memory_pools() { 267 GrowableArray<MemoryPool*> memory_pools(3); 268 memory_pools.append(_eden_pool); 269 memory_pools.append(_survivor_pool); 270 memory_pools.append(_old_pool); 271 return memory_pools; 272 }