1 /*
  2  * Copyright (c) 2017, 2018, Oracle and/or its affiliates. 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 #include "precompiled.hpp"
 26 #include "gc/g1/g1FullGCCompactionPoint.hpp"
 27 #include "gc/g1/heapRegion.hpp"
 28 #include "gc/shared/slidingForwarding.inline.hpp"
 29 #include "oops/oop.inline.hpp"
 30 #include "utilities/debug.hpp"
 31 
 32 G1FullGCCompactionPoint::G1FullGCCompactionPoint() :
 33     _current_region(NULL),
 34     _threshold(NULL),
 35     _compaction_top(NULL) {
 36   _compaction_regions = new (ResourceObj::C_HEAP, mtGC) GrowableArray<HeapRegion*>(32, mtGC);
 37   _compaction_region_iterator = _compaction_regions->begin();
 38 }
 39 
 40 G1FullGCCompactionPoint::~G1FullGCCompactionPoint() {
 41   delete _compaction_regions;
 42 }
 43 
 44 void G1FullGCCompactionPoint::update() {
 45   if (is_initialized()) {
 46     _current_region->set_compaction_top(_compaction_top);
 47   }
 48 }
 49 
 50 void G1FullGCCompactionPoint::initialize_values(bool init_threshold) {
 51   _compaction_top = _current_region->compaction_top();
 52   if (init_threshold) {
 53     _threshold = _current_region->initialize_threshold();
 54   }
 55 }
 56 
 57 bool G1FullGCCompactionPoint::has_regions() {
 58   return !_compaction_regions->is_empty();
 59 }
 60 
 61 bool G1FullGCCompactionPoint::is_initialized() {
 62   return _current_region != NULL;
 63 }
 64 
 65 void G1FullGCCompactionPoint::initialize(HeapRegion* hr, bool init_threshold) {
 66   _current_region = hr;
 67   initialize_values(init_threshold);
 68 }
 69 
 70 HeapRegion* G1FullGCCompactionPoint::current_region() {
 71   return *_compaction_region_iterator;
 72 }
 73 
 74 HeapRegion* G1FullGCCompactionPoint::next_region() {
 75   HeapRegion* next = *(++_compaction_region_iterator);
 76   assert(next != NULL, "Must return valid region");
 77   return next;
 78 }
 79 
 80 GrowableArray<HeapRegion*>* G1FullGCCompactionPoint::regions() {
 81   return _compaction_regions;
 82 }
 83 
 84 bool G1FullGCCompactionPoint::object_will_fit(size_t size) {
 85   size_t space_left = pointer_delta(_current_region->end(), _compaction_top);
 86   return size <= space_left;
 87 }
 88 
 89 void G1FullGCCompactionPoint::switch_region() {
 90   // Save compaction top in the region.
 91   _current_region->set_compaction_top(_compaction_top);
 92   // Get the next region and re-initialize the values.
 93   _current_region = next_region();
 94   initialize_values(true);
 95 }
 96 
 97 template <bool ALT_FWD>
 98 void G1FullGCCompactionPoint::forward(oop object, size_t size) {
 99   assert(_current_region != NULL, "Must have been initialized");
100 
101   // Ensure the object fit in the current region.
102   while (!object_will_fit(size)) {
103     switch_region();
104   }
105 
106   // Store a forwarding pointer if the object should be moved.
107   if (cast_from_oop<HeapWord*>(object) != _compaction_top) {
108     SlidingForwarding::forward_to<ALT_FWD>(object, cast_to_oop(_compaction_top));
109   } else {
110     assert(!SlidingForwarding::is_forwarded(object), "should not be forwarded");
111     /*
112     if (object->forwardee() != NULL) {
113       // Object should not move but mark-word is used so it looks like the
114       // object is forwarded. Need to clear the mark and it's no problem
115       // since it will be restored by preserved marks. There is an exception
116       // with BiasedLocking, in this case forwardee() will return NULL
117       // even if the mark-word is used. This is no problem since
118       // forwardee() will return NULL in the compaction phase as well.
119       object->init_mark();
120     } else {
121       // Make sure object has the correct mark-word set or that it will be
122       // fixed when restoring the preserved marks.
123       assert(object->mark() == markWord::prototype_for_klass(object->klass()) || // Correct mark
124              object->mark_must_be_preserved() || // Will be restored by PreservedMarksSet
125              (UseBiasedLocking && object->has_bias_pattern()), // Will be restored by BiasedLocking
126              "should have correct prototype obj: " PTR_FORMAT " mark: " PTR_FORMAT " prototype: " PTR_FORMAT,
127              p2i(object), object->mark().value(), markWord::prototype_for_klass(object->klass()).value());
128     }
129     assert(object->forwardee() == NULL, "should be forwarded to NULL");
130     */
131   }
132 
133   // Update compaction values.
134   _compaction_top += size;
135   if (_compaction_top > _threshold) {
136     _threshold = _current_region->cross_threshold(_compaction_top - size, _compaction_top);
137   }
138 }
139 
140 template void G1FullGCCompactionPoint::forward<true>(oop object, size_t size);
141 template void G1FullGCCompactionPoint::forward<false>(oop object, size_t size);
142 
143 void G1FullGCCompactionPoint::add(HeapRegion* hr) {
144   _compaction_regions->append(hr);
145 }
146 
147 HeapRegion* G1FullGCCompactionPoint::remove_last() {
148   return _compaction_regions->pop();
149 }