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 "utilities/copy.hpp"
  25 #include "gc/shared/collectedHeap.hpp"
  26 #include "epsilonCollectedHeap.hpp"
  27 #include "epsilonBarrierSet.hpp"
  28 
  29 jint EpsilonCollectedHeap::initialize() {
  30   size_t max_byte_size = _policy->max_heap_byte_size();
  31   size_t align = _policy->heap_alignment();
  32 
  33   ReservedSpace heap_rs = Universe::reserve_heap(max_byte_size,
  34                                                  align);
  35 
  36   _start = (HeapWord *) heap_rs.base();
  37   _current = _start;
  38   _end = (HeapWord *) (heap_rs.base() + heap_rs.size());
  39 
  40   size_t size = pointer_delta(_end, _start);
  41 
  42   initialize_reserved_region(_start, _end);
  43   os::commit_memory((char*)_start, size*HeapWordSize, align, false);
  44 
  45   log_info(gc)("Heap space: [" PTR_FORMAT ", " PTR_FORMAT "] (" SIZE_FORMAT "M)",
  46                p2i(_start), p2i(_end), size*HeapWordSize / M);
  47 
  48   EpsilonBarrierSet* bs = new EpsilonBarrierSet();
  49   set_barrier_set(bs);
  50 
  51   log_info(gc)("Ready to go. See you at your next OutOfMemoryError.");
  52 
  53   return JNI_OK;
  54 }
  55 
  56 HeapWord* EpsilonCollectedHeap::allocate_new_tlab(size_t size) {
  57   bool trap;
  58   return mem_allocate(size, &trap);
  59 }
  60 
  61 HeapWord* EpsilonCollectedHeap::mem_allocate(size_t size, bool *gc_overhead_limit_was_exceeded) {
  62   *gc_overhead_limit_was_exceeded = false;
  63   do {
  64     HeapWord* obj = (HeapWord *) _current;
  65     if (pointer_delta(_end, obj) >= size) {
  66       HeapWord* new_current = obj + size;
  67       HeapWord* result = (HeapWord*)Atomic::cmpxchg_ptr(new_current, &_current, obj);
  68       if (result != obj) {
  69         continue;
  70       }
  71       assert(is_object_aligned((intptr_t)obj), "object is aligned");
  72       assert(is_object_aligned((intptr_t)new_current), "current is aligned");
  73       return obj;
  74     } else {
  75       log_warning(gc)("Heap is exhausted, goodbye.");
  76       vm_abort(false); // TODO: Throw the actual OOME;
  77     }
  78   } while (true);
  79 }
  80 
  81 void EpsilonCollectedHeap::collect_as_vm_thread(GCCause::Cause cause) {
  82   log_warning(gc)("GC was triggered for VM thread with cause \"%s\", unable to handle. Goodbye.", GCCause::to_string(cause));
  83   vm_abort(false);
  84 }
  85 
  86 void EpsilonCollectedHeap::collect(GCCause::Cause cause) {
  87   log_warning(gc)("GC was triggered with cause \"%s\", unable to handle. Goodbye.", GCCause::to_string(cause));
  88   vm_abort(false);
  89 }
  90 
  91 void EpsilonCollectedHeap::do_full_collection(bool clear_all_soft_refs) {
  92   log_warning(gc)("Full GC was triggered, unable to handle. Goodbye.");
  93   vm_abort(false);
  94 }