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 "precompiled.hpp"
  25 #include "oops/oop.hpp"
  26 #include "oops/oop.inline.hpp"
  27 #include "gc_implementation/epsilon/epsilonCollectedHeap.hpp"
  28 
  29 jint EpsilonCollectedHeap::initialize() {
  30   CollectedHeap::pre_initialize();
  31 
  32   size_t init_byte_size = _policy->initial_heap_byte_size();
  33   size_t max_byte_size = _policy->max_heap_byte_size();
  34   size_t align = _policy->heap_alignment();
  35 
  36   ReservedSpace heap_rs = Universe::reserve_heap(max_byte_size,  align);
  37   _virtual_space.initialize(heap_rs, init_byte_size);
  38 
  39   MemRegion committed_region((HeapWord*)_virtual_space.low(), (HeapWord*)_virtual_space.high());
  40   MemRegion reserved_region((HeapWord*)_virtual_space.low_boundary(), (HeapWord*)_virtual_space.high_boundary());
  41 
  42   _reserved.set_word_size(0);
  43   _reserved.set_start((HeapWord*)heap_rs.base());
  44   _reserved.set_end((HeapWord*)(heap_rs.base() + heap_rs.size()));
  45 
  46   _space = new ContiguousSpace();
  47   _space->initialize(committed_region, true, true);
  48 
  49   EpsilonBarrierSet* bs = new EpsilonBarrierSet();
  50   set_barrier_set(bs);
  51 
  52   _max_tlab_size = MIN2(CollectedHeap::max_tlab_size(), EpsilonMaxTLABSize / HeapWordSize);
  53 
  54   _monitoring_support = new EpsilonMonitoringSupport(this);
  55   _last_counter_update = 0;
  56 
  57   if (init_byte_size != max_byte_size) {
  58     gclog_or_tty->print_cr("Initialized with " SIZE_FORMAT "M heap, resizeable to up to " SIZE_FORMAT "M heap with " SIZE_FORMAT "M steps",
  59                  init_byte_size / M, max_byte_size / M, EpsilonMinHeapExpand / M);
  60   } else {
  61     gclog_or_tty->print_cr("Initialized with " SIZE_FORMAT "M non-resizeable heap", init_byte_size / M);
  62   }
  63   if (UseTLAB) {
  64     gclog_or_tty->print_cr("Using TLAB allocation; min: " SIZE_FORMAT "K, max: " SIZE_FORMAT "K",
  65                                 ThreadLocalAllocBuffer::min_size()*HeapWordSize / K,
  66                                 _max_tlab_size*HeapWordSize / K);
  67   } else {
  68     gclog_or_tty->print_cr("Not using TLAB allocation");
  69   }
  70 
  71   return JNI_OK;
  72 }
  73 
  74 size_t EpsilonCollectedHeap::unsafe_max_tlab_alloc(Thread *thr) const {
  75   // This is the only way we can control TLAB sizes without having safepoints.
  76   // Implement exponential expansion within [MinTLABSize; _max_tlab_size], based
  77   // on previously "used" TLAB size.
  78 
  79   size_t size = MIN2(_max_tlab_size * HeapWordSize, MAX2(MinTLABSize, thr->tlab().used() * HeapWordSize * 2));
  80 
  81 //  if (log_is_enabled(Trace, gc)) {
  82 //    ResourceMark rm;
  83 //    log_trace(gc)(
  84 //            "Selecting TLAB size for \"%s\" (Desired: " SIZE_FORMAT "K, Used: " SIZE_FORMAT "K) -> " SIZE_FORMAT "K",
  85 //            Thread::current()->name(),
  86 //            thr->tlab().desired_size() * HeapWordSize / K,
  87 //            thr->tlab().used() * HeapWordSize / K,
  88 //            size / K);
  89 //  }
  90 
  91   return size;
  92 }
  93 
  94 EpsilonCollectedHeap* EpsilonCollectedHeap::heap() {
  95   CollectedHeap* heap = Universe::heap();
  96   assert(heap != NULL, "Uninitialized access to EpsilonCollectedHeap::heap()");
  97   assert(heap->kind() == CollectedHeap::EpsilonCollectedHeap, "Not a EpsilonCollectedHeap");
  98   return (EpsilonCollectedHeap*)heap;
  99 }
 100 
 101 HeapWord* EpsilonCollectedHeap::allocate_work(size_t size) {
 102   HeapWord* res = _space->par_allocate(size);
 103 
 104   while (res == NULL) {
 105     // Allocation failed, attempt expansion, and retry:
 106     MutexLockerEx ml(Heap_lock);
 107     if (!_virtual_space.expand_by(MAX2(size, EpsilonMinHeapExpand))) {
 108       return NULL;
 109     }
 110     _space->set_end((HeapWord *) _virtual_space.high());
 111     res = _space->par_allocate(size);
 112   }
 113 
 114   size_t used = _space->used();
 115   if (used - _last_counter_update >= 1024 * 1024) {
 116     _last_counter_update = used;
 117     _monitoring_support->update_counters();
 118   }
 119   return res;
 120 }
 121 
 122 HeapWord* EpsilonCollectedHeap::allocate_new_tlab(size_t size) {
 123   return allocate_work(size);
 124 }
 125 
 126 HeapWord* EpsilonCollectedHeap::mem_allocate(size_t size, bool *gc_overhead_limit_was_exceeded) {
 127   *gc_overhead_limit_was_exceeded = false;
 128   return allocate_work(size);
 129 }
 130 
 131 void EpsilonCollectedHeap::collect(GCCause::Cause cause) {
 132   gclog_or_tty->print_cr("GC was triggered with cause \"%s\". Ignoring.", GCCause::to_string(cause));
 133   _monitoring_support->update_counters();
 134 }
 135 
 136 void EpsilonCollectedHeap::do_full_collection(bool clear_all_soft_refs) {
 137   gclog_or_tty->print_cr("Full GC was triggered with cause \"%s\". Ignoring.", GCCause::to_string(gc_cause()));
 138   _monitoring_support->update_counters();
 139 }
 140 
 141 void EpsilonCollectedHeap::safe_object_iterate(ObjectClosure *cl) {
 142   _space->safe_object_iterate(cl);
 143 }
 144 
 145 void EpsilonCollectedHeap::print_on(outputStream *st) const {
 146   st->print_cr("Epsilon Heap");
 147 
 148   // Cast away constness:
 149   ((VirtualSpace)_virtual_space).print_on(st);
 150 
 151   st->print_cr("Allocation space:");
 152   _space->print_on(st);
 153 }
 154 
 155 void EpsilonCollectedHeap::print_tracing_info() const {
 156   size_t allocated_kb = used() / K;
 157   gclog_or_tty->print_cr("Total allocated: " SIZE_FORMAT " KB",
 158            allocated_kb);
 159   gclog_or_tty->print_cr("Average allocation rate: " SIZE_FORMAT " KB/sec",
 160            allocated_kb * NANOSECS_PER_SEC / os::elapsed_counter());
 161 }