1 /*
  2  * Copyright (c) 2023, 2025, Oracle and/or its affiliates. All rights reserved.
  3  * Copyright (c) 2017, 2022, Red Hat, Inc. All rights reserved.
  4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  5  *
  6  * This code is free software; you can redistribute it and/or modify it
  7  * under the terms of the GNU General Public License version 2 only, as
  8  * published by the Free Software Foundation.
  9  *
 10  * This code is distributed in the hope that it will be useful, but WITHOUT
 11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 13  * version 2 for more details (a copy is included in the LICENSE file that
 14  * accompanied this code).
 15  *
 16  * You should have received a copy of the GNU General Public License version
 17  * 2 along with this work; if not, write to the Free Software Foundation,
 18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 19  *
 20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 21  * or visit www.oracle.com if you need additional information or have any
 22  * questions.
 23  *
 24  */
 25 
 26 #ifndef SHARE_GC_EPSILON_EPSILONHEAP_HPP
 27 #define SHARE_GC_EPSILON_EPSILONHEAP_HPP
 28 
 29 #include "gc/epsilon/epsilonBarrierSet.hpp"
 30 #include "gc/epsilon/epsilonMonitoringSupport.hpp"
 31 #include "gc/shared/collectedHeap.hpp"
 32 #include "gc/shared/markBitMap.hpp"
 33 #include "gc/shared/space.hpp"
 34 #include "memory/virtualspace.hpp"
 35 #include "services/memoryManager.hpp"
 36 
 37 class EpsilonHeap : public CollectedHeap {
 38   friend class VMStructs;
 39 private:
 40   EpsilonMonitoringSupport* _monitoring_support;
 41   MemoryPool* _pool;
 42   GCMemoryManager _memory_manager;
 43   ContiguousSpace* _space;
 44   VirtualSpace _virtual_space;
 45   size_t _max_tlab_size;
 46   size_t _step_counter_update;
 47   size_t _step_heap_print;
 48   int64_t _decay_time_ns;
 49   volatile size_t _last_counter_update;
 50   volatile size_t _last_heap_print;
 51   MemRegion  _bitmap_region;
 52   MarkBitMap _bitmap;
 53 
 54   void print_tracing_info() const override;
 55   void stop() override {};
 56 
 57 public:
 58   static EpsilonHeap* heap();
 59 
 60   EpsilonHeap() :
 61           _memory_manager("Epsilon Heap"),
 62           _space(nullptr) {};
 63 
 64   Name kind() const override {
 65     return CollectedHeap::Epsilon;
 66   }
 67 
 68   const char* name() const override {
 69     return "Epsilon";
 70   }
 71 
 72   jint initialize() override;
 73   void initialize_serviceability() override;
 74 
 75   GrowableArray<GCMemoryManager*> memory_managers() override;
 76   GrowableArray<MemoryPool*> memory_pools() override;
 77 
 78   size_t max_capacity() const override { return _virtual_space.reserved_size();  }
 79   size_t capacity()     const override { return _virtual_space.committed_size(); }
 80   size_t used()         const override { return _space->used(); }
 81 
 82   bool is_in(const void* p) const override {
 83     return _space->is_in(p);
 84   }
 85 
 86   bool requires_barriers(stackChunkOop obj) const override { return false; }
 87 
 88   // Allocation
 89   HeapWord* allocate_work(size_t size, bool verbose = true);
 90   HeapWord* allocate_or_collect_work(size_t size, bool verbose = true);
 91   HeapWord* mem_allocate(size_t size) override;
 92   HeapWord* allocate_new_tlab(size_t min_size,
 93                               size_t requested_size,
 94                               size_t* actual_size) override;
 95 
 96   // TLAB allocation
 97   size_t tlab_capacity(Thread* thr)         const override { return capacity();     }
 98   size_t tlab_used(Thread* thr)             const override { return used();         }
 99   size_t max_tlab_size()                    const override { return _max_tlab_size; }
100   size_t unsafe_max_tlab_alloc(Thread* thr) const override;
101 
102   void collect(GCCause::Cause cause) override;
103   void do_full_collection(bool clear_all_soft_refs) override;
104 
105   // Heap walking support
106   void object_iterate(ObjectClosure* cl) override;
107 
108   // Object pinning support: every object is implicitly pinned
109   void pin_object(JavaThread* thread, oop obj) override;
110   void unpin_object(JavaThread* thread, oop obj) override;
111 
112   // No support for block parsing.
113   HeapWord* block_start(const void* addr) const { return nullptr;  }
114   bool block_is_obj(const HeapWord* addr) const { return false; }
115 
116   // No GC threads
117   void gc_threads_do(ThreadClosure* tc) const override {}
118 
119   // No nmethod handling
120   void register_nmethod(nmethod* nm) override {}
121   void unregister_nmethod(nmethod* nm) override {}
122   void verify_nmethod(nmethod* nm) override {}
123 
124   // No heap verification
125   void prepare_for_verify() override {}
126   void verify(VerifyOption option) override {}
127 
128   MemRegion reserved_region() const { return _reserved; }
129   bool is_in_reserved(const void* addr) const { return _reserved.contains(addr); }
130 
131   // Support for loading objects from CDS archive into the heap
132   bool can_load_archived_objects() const override { return true; }
133   HeapWord* allocate_loaded_archive_space(size_t size) override;
134 
135   void print_heap_on(outputStream* st) const override;
136   void print_gc_on(outputStream* st) const override {}
137   bool print_location(outputStream* st, void* addr) const override;
138 
139   void entry_collect(GCCause::Cause cause);
140 
141 private:
142   void print_heap_info(size_t used) const;
143   void print_metaspace_info() const;
144 
145   void vmentry_collect(GCCause::Cause cause);
146 
147   void process_roots(OopClosure* cl, bool update);
148   void walk_bitmap(ObjectClosure* cl);
149 
150 };
151 
152 #endif // SHARE_GC_EPSILON_EPSILONHEAP_HPP