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 const bool scan_concurrently = CMSPrecleaningEnabled; 96 return new CMSCardTable(reserved_region, scan_concurrently); 97 } 98 99 void CMSHeap::initialize_serviceability() { 100 _young_manager = new GCMemoryManager("ParNew", "end of minor GC"); 101 _old_manager = new GCMemoryManager("ConcurrentMarkSweep", "end of major GC"); 102 103 ParNewGeneration* young = (ParNewGeneration*) young_gen(); 104 _eden_pool = new ContiguousSpacePool(young->eden(), 105 "Par Eden Space", 106 young->max_eden_size(), 107 false); 108 109 _survivor_pool = new SurvivorContiguousSpacePool(young, 110 "Par Survivor Space", 111 young->max_survivor_size(), 112 false); 113 114 ConcurrentMarkSweepGeneration* old = (ConcurrentMarkSweepGeneration*) old_gen(); 115 _old_pool = new CompactibleFreeListSpacePool(old->cmsSpace(), 116 "CMS Old Gen", 117 old->reserved().byte_size(), 118 true); 119 120 _young_manager->add_pool(_eden_pool); 121 _young_manager->add_pool(_survivor_pool); 122 young->set_gc_manager(_young_manager); 123 124 _old_manager->add_pool(_eden_pool); 125 _old_manager->add_pool(_survivor_pool); 126 _old_manager->add_pool(_old_pool); 127 old ->set_gc_manager(_old_manager); 128 129 } 130 131 void CMSHeap::check_gen_kinds() { 132 assert(young_gen()->kind() == Generation::ParNew, 133 "Wrong youngest generation type"); 134 assert(old_gen()->kind() == Generation::ConcurrentMarkSweep, 135 "Wrong generation kind"); 136 } 137 138 CMSHeap* CMSHeap::heap() { 139 CollectedHeap* heap = Universe::heap(); 140 assert(heap != NULL, "Uninitialized access to CMSHeap::heap()"); 141 assert(heap->kind() == CollectedHeap::CMS, "Invalid name"); 142 return (CMSHeap*) heap; 143 } 144 145 void CMSHeap::gc_threads_do(ThreadClosure* tc) const { 146 assert(workers() != NULL, "should have workers here"); 147 workers()->threads_do(tc); 148 ConcurrentMarkSweepThread::threads_do(tc); 149 } 150 151 void CMSHeap::print_gc_threads_on(outputStream* st) const { 152 assert(workers() != NULL, "should have workers here"); 153 workers()->print_worker_threads_on(st); 154 ConcurrentMarkSweepThread::print_all_on(st); 155 } 156 157 void CMSHeap::print_on_error(outputStream* st) const { 158 GenCollectedHeap::print_on_error(st); 159 st->cr(); 160 CMSCollector::print_on_error(st); 161 } 162 163 bool CMSHeap::create_cms_collector() { 164 assert(old_gen()->kind() == Generation::ConcurrentMarkSweep, 165 "Unexpected generation kinds"); 166 CMSCollector* collector = 167 new CMSCollector((ConcurrentMarkSweepGeneration*) old_gen(), 168 rem_set(), 169 (ConcurrentMarkSweepPolicy*) gen_policy()); 170 171 if (collector == NULL || !collector->completed_initialization()) { 172 if (collector) { 173 delete collector; // Be nice in embedded situation 174 } 175 vm_shutdown_during_initialization("Could not create CMS collector"); 176 return false; 177 } 178 return true; // success 179 } 180 181 void CMSHeap::collect(GCCause::Cause cause) { 182 if (should_do_concurrent_full_gc(cause)) { 183 // Mostly concurrent full collection. 184 collect_mostly_concurrent(cause); 185 } else { 186 GenCollectedHeap::collect(cause); 187 } 188 } 189 190 bool CMSHeap::should_do_concurrent_full_gc(GCCause::Cause cause) { 191 switch (cause) { 192 case GCCause::_gc_locker: return GCLockerInvokesConcurrent; 193 case GCCause::_java_lang_system_gc: 194 case GCCause::_dcmd_gc_run: return ExplicitGCInvokesConcurrent; 195 default: return false; 196 } 197 } 198 199 void CMSHeap::collect_mostly_concurrent(GCCause::Cause cause) { 200 assert(!Heap_lock->owned_by_self(), "Should not own Heap_lock"); 201 202 MutexLocker ml(Heap_lock); 203 // Read the GC counts while holding the Heap_lock 204 unsigned int full_gc_count_before = total_full_collections(); 205 unsigned int gc_count_before = total_collections(); 206 { 207 MutexUnlocker mu(Heap_lock); 208 VM_GenCollectFullConcurrent op(gc_count_before, full_gc_count_before, cause); 209 VMThread::execute(&op); 210 } 211 } 212 213 void CMSHeap::stop() { 214 ConcurrentMarkSweepThread::cmst()->stop(); 215 } 216 217 void CMSHeap::safepoint_synchronize_begin() { 218 ConcurrentMarkSweepThread::synchronize(false); 219 } 220 221 void CMSHeap::safepoint_synchronize_end() { 222 ConcurrentMarkSweepThread::desynchronize(false); 223 } 224 225 void CMSHeap::cms_process_roots(StrongRootsScope* scope, 226 bool young_gen_as_roots, 227 ScanningOption so, 228 bool only_strong_roots, 229 OopsInGenClosure* root_closure, 230 CLDClosure* cld_closure) { 231 MarkingCodeBlobClosure mark_code_closure(root_closure, !CodeBlobToOopClosure::FixRelocations); 232 OopsInGenClosure* weak_roots = only_strong_roots ? NULL : root_closure; 233 CLDClosure* weak_cld_closure = only_strong_roots ? NULL : cld_closure; 234 235 process_roots(scope, so, root_closure, weak_roots, cld_closure, weak_cld_closure, &mark_code_closure); 236 if (!only_strong_roots) { 237 process_string_table_roots(scope, root_closure); 238 } 239 240 if (young_gen_as_roots && 241 !_process_strong_tasks->is_task_claimed(GCH_PS_younger_gens)) { 242 root_closure->set_generation(young_gen()); 243 young_gen()->oop_iterate(root_closure); 244 root_closure->reset_generation(); 245 } 246 247 _process_strong_tasks->all_tasks_completed(scope->n_threads()); 248 } 249 250 void CMSHeap::gc_prologue(bool full) { 251 always_do_update_barrier = false; 252 GenCollectedHeap::gc_prologue(full); 253 }; 254 255 void CMSHeap::gc_epilogue(bool full) { 256 GenCollectedHeap::gc_epilogue(full); 257 always_do_update_barrier = true; 258 }; 259 260 GrowableArray<GCMemoryManager*> CMSHeap::memory_managers() { 261 GrowableArray<GCMemoryManager*> memory_managers(2); 262 memory_managers.append(_young_manager); 263 memory_managers.append(_old_manager); 264 return memory_managers; 265 } 266 267 GrowableArray<MemoryPool*> CMSHeap::memory_pools() { 268 GrowableArray<MemoryPool*> memory_pools(3); 269 memory_pools.append(_eden_pool); 270 memory_pools.append(_survivor_pool); 271 memory_pools.append(_old_pool); 272 return memory_pools; 273 }