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