1 /*
  2  * Copyright (c) 2017, 2021, Red Hat, Inc. 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 #ifndef SHARE_GC_EPSILON_EPSILONHEAP_HPP
 26 #define SHARE_GC_EPSILON_EPSILONHEAP_HPP
 27 
 28 #include "gc/shared/collectedHeap.hpp"
 29 #include "gc/shared/softRefPolicy.hpp"
 30 #include "gc/shared/space.hpp"
 31 #include "gc/epsilon/epsilonMonitoringSupport.hpp"
 32 #include "gc/epsilon/epsilonBarrierSet.hpp"
 33 #include "services/memoryManager.hpp"
 34 
 35 class EpsilonHeap : public CollectedHeap {
 36   friend class VMStructs;
 37 private:
 38   SoftRefPolicy _soft_ref_policy;
 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 public:
 52   static EpsilonHeap* heap();
 53 
 54   EpsilonHeap() :
 55           _memory_manager("Epsilon Heap", ""),
 56           _space(NULL) {};
 57 
 58   virtual Name kind() const {
 59     return CollectedHeap::Epsilon;
 60   }
 61 
 62   virtual const char* name() const {
 63     return "Epsilon";
 64   }
 65 
 66   virtual SoftRefPolicy* soft_ref_policy() {
 67     return &_soft_ref_policy;
 68   }
 69 
 70   virtual jint initialize();
 71   virtual void post_initialize();
 72   virtual void initialize_serviceability();
 73 
 74   virtual GrowableArray<GCMemoryManager*> memory_managers();
 75   virtual GrowableArray<MemoryPool*> memory_pools();
 76 
 77   virtual size_t max_capacity() const { return _virtual_space.reserved_size();  }
 78   virtual size_t capacity()     const { return _virtual_space.committed_size(); }
 79   virtual size_t used()         const { return _space->used(); }
 80 
 81   virtual bool is_in(const void* p) const {
 82     return _space->is_in(p);
 83   }
 84 
 85   virtual bool requires_barriers(oop obj) const { return false; }
 86 
 87   virtual bool is_maximal_no_gc() const {
 88     // No GC is going to happen. Return "we are at max", when we are about to fail.
 89     return used() == capacity();
 90   }
 91 
 92   // Allocation
 93   HeapWord* allocate_work(size_t size, bool verbose = true);
 94   virtual HeapWord* mem_allocate(size_t size, bool* gc_overhead_limit_was_exceeded);
 95   virtual HeapWord* allocate_new_tlab(size_t min_size,
 96                                       size_t requested_size,
 97                                       size_t* actual_size);
 98 
 99   // TLAB allocation
100   virtual size_t tlab_capacity(Thread* thr)         const { return capacity();     }
101   virtual size_t tlab_used(Thread* thr)             const { return used();         }
102   virtual size_t max_tlab_size()                    const { return _max_tlab_size; }
103   virtual size_t unsafe_max_tlab_alloc(Thread* thr) const;
104 
105   virtual void collect(GCCause::Cause cause);
106   virtual void do_full_collection(bool clear_all_soft_refs);
107 
108   // Heap walking support
109   virtual void object_iterate(ObjectClosure* cl);
110 
111   // Object pinning support: every object is implicitly pinned
112   virtual bool supports_object_pinning() const           { return true; }
113   virtual oop pin_object(JavaThread* thread, oop obj)    { return obj; }
114   virtual void unpin_object(JavaThread* thread, oop obj) { }
115 
116   // No support for block parsing.
117   HeapWord* block_start(const void* addr) const { return NULL;  }
118   bool block_is_obj(const HeapWord* addr) const { return false; }
119 
120   // No GC threads
121   virtual void gc_threads_do(ThreadClosure* tc) const {}
122 
123   // No nmethod handling
124   virtual void register_nmethod(nmethod* nm) {}
125   virtual void unregister_nmethod(nmethod* nm) {}
126   virtual void flush_nmethod(nmethod* nm) {}
127   virtual void verify_nmethod(nmethod* nm) {}
128 
129   // No heap verification
130   virtual void prepare_for_verify() {}
131   virtual void verify(VerifyOption option) {}
132 
133   MemRegion reserved_region() const { return _reserved; }
134   bool is_in_reserved(const void* addr) const { return _reserved.contains(addr); }
135 
136   // Support for loading objects from CDS archive into the heap
137   virtual bool can_load_archived_objects() const { return true; }
138   virtual HeapWord* allocate_loaded_archive_space(size_t size);
139 
140   virtual void print_on(outputStream* st) const;
141   virtual void print_tracing_info() const;
142   virtual bool print_location(outputStream* st, void* addr) const;
143 
144 private:
145   void print_heap_info(size_t used) const;
146   void print_metaspace_info() const;
147 
148 };
149 
150 #endif // SHARE_GC_EPSILON_EPSILONHEAP_HPP