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/space.hpp"
 33 #include "memory/virtualspace.hpp"
 34 #include "services/memoryManager.hpp"
 35 
 36 class EpsilonHeap : public CollectedHeap {
 37   friend class VMStructs;
 38 private:
 39   EpsilonMonitoringSupport* _monitoring_support;
 40   MemoryPool* _pool;
 41   GCMemoryManager _memory_manager;
 42   ContiguousSpace* _space;
 43   VirtualSpace _virtual_space;
 44   size_t _max_tlab_size;
 45   size_t _step_counter_update;
 46   size_t _step_heap_print;
 47   int64_t _decay_time_ns;
 48   volatile size_t _last_counter_update;
 49   volatile size_t _last_heap_print;
 50 
 51   void print_tracing_info() const override;
 52   void stop() override {};
 53 
 54 public:
 55   static EpsilonHeap* heap();
 56 
 57   EpsilonHeap() :
 58           _memory_manager("Epsilon Heap"),
 59           _space(nullptr) {};
 60 
 61   Name kind() const override {
 62     return CollectedHeap::Epsilon;
 63   }
 64 
 65   const char* name() const override {
 66     return "Epsilon";
 67   }
 68 
 69   jint initialize() override;
 70   void initialize_serviceability() override;
 71 
 72   GrowableArray<GCMemoryManager*> memory_managers() override;
 73   GrowableArray<MemoryPool*> memory_pools() override;
 74 
 75   size_t max_capacity() const override { return _virtual_space.reserved_size();  }
 76   size_t capacity()     const override { return _virtual_space.committed_size(); }
 77   size_t used()         const override { return _space->used(); }
 78 
 79   bool is_in(const void* p) const override {
 80     return _space->is_in(p);
 81   }
 82 
 83   bool requires_barriers(stackChunkOop obj) const override { return false; }
 84 
 85   // Allocation
 86   HeapWord* allocate_work(size_t size, bool verbose = true);
 87   HeapWord* mem_allocate(size_t size) override;
 88   HeapWord* allocate_new_tlab(size_t min_size,
 89                               size_t requested_size,
 90                               size_t* actual_size) override;
 91 
 92   // TLAB allocation
 93   size_t tlab_capacity(Thread* thr)         const override { return capacity();     }
 94   size_t tlab_used(Thread* thr)             const override { return used();         }
 95   size_t max_tlab_size()                    const override { return _max_tlab_size; }
 96   size_t unsafe_max_tlab_alloc(Thread* thr) const override;
 97 
 98   void collect(GCCause::Cause cause) override;
 99   void do_full_collection(bool clear_all_soft_refs) override;
100 
101   // Heap walking support
102   void object_iterate(ObjectClosure* cl) override;
103 
104   // Object pinning support: every object is implicitly pinned
105   void pin_object(JavaThread* thread, oop obj) override { }
106   void unpin_object(JavaThread* thread, oop obj) override { }
107 
108   // No support for block parsing.
109   HeapWord* block_start(const void* addr) const { return nullptr;  }
110   bool block_is_obj(const HeapWord* addr) const { return false; }
111 
112   // No GC threads
113   void gc_threads_do(ThreadClosure* tc) const override {}
114 
115   // No nmethod handling
116   void register_nmethod(nmethod* nm) override {}
117   void unregister_nmethod(nmethod* nm) override {}
118   void verify_nmethod(nmethod* nm) override {}
119 
120   // No heap verification
121   void prepare_for_verify() override {}
122   void verify(VerifyOption option) override {}
123 
124   MemRegion reserved_region() const { return _reserved; }
125   bool is_in_reserved(const void* addr) const { return _reserved.contains(addr); }
126 
127   // Support for loading objects from CDS archive into the heap
128   bool can_load_archived_objects() const override { return true; }
129   HeapWord* allocate_loaded_archive_space(size_t size) override;
130 
131   void print_heap_on(outputStream* st) const override;
132   void print_gc_on(outputStream* st) const override {}
133   bool print_location(outputStream* st, void* addr) const override;
134 
135 private:
136   void print_heap_info(size_t used) const;
137   void print_metaspace_info() const;
138 
139 };
140 
141 #endif // SHARE_GC_EPSILON_EPSILONHEAP_HPP