1 /*
  2  * Copyright (c) 2017, 2021, 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/serial/defNewGeneration.inline.hpp"
 27 #include "gc/serial/serialHeap.hpp"
 28 #include "gc/serial/tenuredGeneration.inline.hpp"
 29 #include "gc/shared/genMemoryPools.hpp"
 30 #include "gc/shared/strongRootsScope.hpp"
 31 #include "gc/shared/suspendibleThreadSet.hpp"
 32 #include "memory/universe.hpp"
 33 #include "runtime/mutexLocker.hpp"
 34 #include "services/memoryManager.hpp"
 35 
 36 SerialHeap* SerialHeap::heap() {
 37   return named_heap<SerialHeap>(CollectedHeap::Serial);
 38 }
 39 
 40 SerialHeap::SerialHeap() :
 41     GenCollectedHeap(Generation::DefNew,
 42                      Generation::MarkSweepCompact,
 43                      "Copy:MSC"),
 44     _eden_pool(NULL),
 45     _survivor_pool(NULL),
 46     _old_pool(NULL) {
 47   _young_manager = new GCMemoryManager("Copy", "end of minor GC");
 48   _old_manager = new GCMemoryManager("MarkSweepCompact", "end of major GC");
 49 }
 50 
 51 void SerialHeap::initialize_serviceability() {
 52 
 53   DefNewGeneration* young = young_gen();
 54 
 55   // Add a memory pool for each space and young gen doesn't
 56   // support low memory detection as it is expected to get filled up.
 57   _eden_pool = new ContiguousSpacePool(young->eden(),
 58                                        "Eden Space",
 59                                        young->max_eden_size(),
 60                                        false /* support_usage_threshold */);
 61   _survivor_pool = new SurvivorContiguousSpacePool(young,
 62                                                    "Survivor Space",
 63                                                    young->max_survivor_size(),
 64                                                    false /* support_usage_threshold */);
 65   TenuredGeneration* old = old_gen();
 66   _old_pool = new GenerationPool(old, "Tenured Gen", true);
 67 
 68   _young_manager->add_pool(_eden_pool);
 69   _young_manager->add_pool(_survivor_pool);
 70   young->set_gc_manager(_young_manager);
 71 
 72   _old_manager->add_pool(_eden_pool);
 73   _old_manager->add_pool(_survivor_pool);
 74   _old_manager->add_pool(_old_pool);
 75   old->set_gc_manager(_old_manager);
 76 
 77 }
 78 
 79 GrowableArray<GCMemoryManager*> SerialHeap::memory_managers() {
 80   GrowableArray<GCMemoryManager*> memory_managers(2);
 81   memory_managers.append(_young_manager);
 82   memory_managers.append(_old_manager);
 83   return memory_managers;
 84 }
 85 
 86 GrowableArray<MemoryPool*> SerialHeap::memory_pools() {
 87   GrowableArray<MemoryPool*> memory_pools(3);
 88   memory_pools.append(_eden_pool);
 89   memory_pools.append(_survivor_pool);
 90   memory_pools.append(_old_pool);
 91   return memory_pools;
 92 }
 93 
 94 void SerialHeap::young_process_roots(OopIterateClosure* root_closure,
 95                                      OopIterateClosure* old_gen_closure,
 96                                      CLDClosure* cld_closure) {
 97   MarkingCodeBlobClosure mark_code_closure(root_closure, CodeBlobToOopClosure::FixRelocations);
 98 
 99   process_roots(SO_ScavengeCodeCache, root_closure,
100                 cld_closure, cld_closure, &mark_code_closure);
101 
102   old_gen()->younger_refs_iterate(old_gen_closure);
103 }
104 
105 void SerialHeap::safepoint_synchronize_begin() {
106   if (UseStringDeduplication) {
107     SuspendibleThreadSet::synchronize();
108   }
109 }
110 
111 void SerialHeap::safepoint_synchronize_end() {
112   if (UseStringDeduplication) {
113     SuspendibleThreadSet::desynchronize();
114   }
115 }
116 
117 HeapWord* SerialHeap::allocate_loaded_archive_space(size_t word_size) {
118   MutexLocker ml(Heap_lock);
119   return old_gen()->allocate(word_size, false /* is_tlab */);
120 }
121 
122 void SerialHeap::complete_loaded_archive_space(MemRegion archive_space) {
123   assert(old_gen()->used_region().contains(archive_space), "Archive space not contained in old gen");
124   old_gen()->complete_loaded_archive_space(archive_space);
125 }