1 /*
   2  * Copyright (c) 2017, Red Hat, Inc. and/or its affiliates.
   3  *
   4  * This code is free software; you can redistribute it and/or modify it
   5  * under the terms of the GNU General Public License version 2 only, as
   6  * published by the Free Software Foundation.
   7  *
   8  * This code is distributed in the hope that it will be useful, but WITHOUT
   9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  11  * version 2 for more details (a copy is included in the LICENSE file that
  12  * accompanied this code).
  13  *
  14  * You should have received a copy of the GNU General Public License version
  15  * 2 along with this work; if not, write to the Free Software Foundation,
  16  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  17  *
  18  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  19  * or visit www.oracle.com if you need additional information or have any
  20  * questions.
  21  *
  22  */
  23 
  24 #include "gc/shared/collectedHeap.hpp"
  25 #include "gc/epsilon/epsilonCollectorPolicy.hpp"
  26 #include "gc/epsilon/epsilonBarrierSet.hpp"
  27 
  28 class EpsilonCollectedHeap : public CollectedHeap {
  29 private:
  30   EpsilonCollectorPolicy* _policy;
  31   HeapWord* _start;
  32   HeapWord* _current;
  33   HeapWord* _end;
  34 public:
  35   EpsilonCollectedHeap(EpsilonCollectorPolicy* p) : _policy(p) {};
  36 
  37   virtual Name kind() const {
  38     return CollectedHeap::EpsilonCollectedHeap;
  39   }
  40 
  41   virtual const char *name() const {
  42     return "Epsilon GC";
  43   }
  44 
  45   virtual jint initialize();
  46 
  47   virtual void post_initialize() {
  48     // do nothing
  49   }
  50 
  51   virtual size_t capacity() const {
  52     return pointer_delta(_end, _start);
  53   }
  54 
  55   virtual size_t used() const {
  56     return pointer_delta(_current, _start);
  57   }
  58 
  59   virtual bool is_maximal_no_gc() const {
  60     return false;
  61   }
  62 
  63   virtual size_t max_capacity() const {
  64     return pointer_delta(_end, _start);
  65   }
  66 
  67   virtual bool is_in(const void *p) const {
  68     return (_start <= p) && (p < _end);
  69   }
  70 
  71   virtual bool is_scavengable(const void *p) {
  72     return true; // TODO: Why?
  73   }
  74 
  75   virtual HeapWord* mem_allocate(size_t size, bool *gc_overhead_limit_was_exceeded);
  76 
  77   virtual bool supports_tlab_allocation() const {
  78     return true;
  79   }
  80 
  81   HeapWord* allocate_new_tlab(size_t size);
  82 
  83   virtual size_t tlab_capacity(Thread *thr) const {
  84     return capacity();
  85   }
  86 
  87   virtual size_t tlab_used(Thread *thr) const {
  88     return used();
  89   }
  90 
  91   virtual size_t unsafe_max_tlab_alloc(Thread *thr) const {
  92     return HeapWordSize * 16 * K; // TODO: hook up in TLAB policy better. pointer_delta(_current, _end);
  93   }
  94 
  95   virtual bool can_elide_tlab_store_barriers() const {
  96     return true; // TODO: Really?
  97   }
  98 
  99   virtual bool can_elide_initializing_store_barrier(oop new_obj) {
 100     return true; // TODO: Really?
 101   }
 102 
 103   virtual bool card_mark_must_follow_store() const {
 104     return true; // TODO: Really?
 105   }
 106 
 107   virtual void collect(GCCause::Cause cause) {
 108     log_warning(gc)("GC was triggered with cause \"%s\", unable to handle. Goodbye.", GCCause::to_string(cause));
 109     vm_abort(false);
 110   }
 111 
 112   virtual void do_full_collection(bool clear_all_soft_refs) {
 113     log_warning(gc)("Full GC was triggered, unable to handle. Goodbye.");
 114     vm_abort(false);
 115   }
 116 
 117   virtual AdaptiveSizePolicy *size_policy() {
 118     // No such thing for Epsilon
 119     return NULL;
 120   }
 121 
 122   virtual CollectorPolicy *collector_policy() const {
 123     return _policy;
 124   }
 125 
 126   virtual void object_iterate(ObjectClosure *cl) {
 127     Unimplemented();
 128   }
 129 
 130   virtual void safe_object_iterate(ObjectClosure *cl) {
 131     // TODO: Enable heap dumps by implementing this.
 132     Unimplemented();
 133   }
 134 
 135   virtual HeapWord *block_start(const void *addr) const {
 136     Unimplemented();
 137     return NULL;
 138   }
 139 
 140   virtual size_t block_size(const HeapWord *addr) const {
 141     Unimplemented();
 142     return 0;
 143   }
 144 
 145   virtual bool block_is_obj(const HeapWord *addr) const {
 146     Unimplemented();
 147     return false;
 148   }
 149 
 150   virtual jlong millis_since_last_gc() {
 151     Unimplemented();
 152     return 0;
 153   }
 154 
 155   virtual void prepare_for_verify() {
 156     // No heap verification.
 157   }
 158 
 159   virtual void print_on(outputStream *st) const {
 160     // TODO: Print something here.
 161   }
 162 
 163   virtual void print_gc_threads_on(outputStream *st) const {
 164     // No GC threads.
 165   }
 166 
 167   virtual void gc_threads_do(ThreadClosure *tc) const {
 168     // No GC threads.
 169   }
 170 
 171   virtual void print_tracing_info() const {
 172     Log(gc) log;
 173     log.info("Epsilon: yup, we are still alive.");
 174     log.info("Allocated " SIZE_FORMAT "K in this session.",
 175              pointer_delta(_current, _start) * HeapWordSize / K);
 176   }
 177 
 178   virtual void verify(VerifyOption option) {
 179     // No heap verification for Epsilon.
 180   }
 181 
 182 };