1 /*
   2  * Copyright (c) 2012, 2019, 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/g1CollectedHeap.inline.hpp"
  27 #include "gc/g1/g1CollectorState.hpp"
  28 #include "gc/g1/g1ConcurrentMark.inline.hpp"
  29 #include "gc/g1/g1DirtyCardQueue.hpp"
  30 #include "gc/g1/g1EvacFailure.hpp"
  31 #include "gc/g1/g1HeapVerifier.hpp"
  32 #include "gc/g1/g1OopClosures.inline.hpp"
  33 #include "gc/g1/g1_globals.hpp"
  34 #include "gc/g1/heapRegion.hpp"
  35 #include "gc/g1/heapRegionRemSet.hpp"
  36 #include "gc/shared/preservedMarks.inline.hpp"
  37 #include "oops/access.inline.hpp"
  38 #include "oops/compressedOops.inline.hpp"
  39 #include "oops/oop.inline.hpp"
  40 
  41 class UpdateRSetDeferred : public BasicOopIterateClosure {
  42 private:
  43   G1CollectedHeap* _g1h;
  44   G1DirtyCardQueue* _dcq;
  45   G1CardTable*    _ct;
  46 
  47 public:
  48   UpdateRSetDeferred(G1DirtyCardQueue* dcq) :
  49     _g1h(G1CollectedHeap::heap()), _dcq(dcq), _ct(_g1h->card_table()) {}
  50 
  51   virtual void do_oop(narrowOop* p) { do_oop_work(p); }
  52   virtual void do_oop(      oop* p) { do_oop_work(p); }
  53   template <class T> void do_oop_work(T* p) {
  54     assert(_g1h->heap_region_containing(p)->is_in_reserved(p), "paranoia");
  55     assert(!_g1h->heap_region_containing(p)->is_survivor(), "Unexpected evac failure in survivor region");
  56 
  57     T const o = RawAccess<>::oop_load(p);
  58     if (CompressedOops::is_null(o)) {
  59       return;
  60     }
  61 
  62     if (HeapRegion::is_in_same_region(p, CompressedOops::decode(o))) {
  63       return;
  64     }
  65     size_t card_index = _ct->index_for(p);
  66     if (_ct->mark_card_deferred(card_index)) {
  67       _dcq->enqueue(_ct->byte_for_index(card_index));
  68     }
  69   }
  70 };
  71 
  72 class RemoveSelfForwardPtrObjClosure: public ObjectClosure {
  73   G1CollectedHeap* _g1h;
  74   G1ConcurrentMark* _cm;
  75   HeapRegion* _hr;
  76   size_t _marked_bytes;
  77   UpdateRSetDeferred* _update_rset_cl;
  78   bool _during_initial_mark;
  79   uint _worker_id;
  80   HeapWord* _last_forwarded_object_end;
  81 
  82 public:
  83   RemoveSelfForwardPtrObjClosure(HeapRegion* hr,
  84                                  UpdateRSetDeferred* update_rset_cl,
  85                                  bool during_initial_mark,
  86                                  uint worker_id) :
  87     _g1h(G1CollectedHeap::heap()),
  88     _cm(_g1h->concurrent_mark()),
  89     _hr(hr),
  90     _marked_bytes(0),
  91     _update_rset_cl(update_rset_cl),
  92     _during_initial_mark(during_initial_mark),
  93     _worker_id(worker_id),
  94     _last_forwarded_object_end(hr->bottom()) { }
  95 
  96   size_t marked_bytes() { return _marked_bytes; }
  97 
  98   // Iterate over the live objects in the region to find self-forwarded objects
  99   // that need to be kept live. We need to update the remembered sets of these
 100   // objects. Further update the BOT and marks.
 101   // We can coalesce and overwrite the remaining heap contents with dummy objects
 102   // as they have either been dead or evacuated (which are unreferenced now, i.e.
 103   // dead too) already.
 104   void do_object(oop obj) {
 105     HeapWord* obj_addr = (HeapWord*) obj;
 106     assert(_hr->is_in(obj_addr), "sanity");
 107 
 108     if (obj->is_forwarded() && obj->forwardee() == obj) {
 109       // The object failed to move.
 110 
 111       zap_dead_objects(_last_forwarded_object_end, obj_addr);
 112       // We consider all objects that we find self-forwarded to be
 113       // live. What we'll do is that we'll update the prev marking
 114       // info so that they are all under PTAMS and explicitly marked.
 115       if (!_cm->is_marked_in_prev_bitmap(obj)) {
 116         _cm->mark_in_prev_bitmap(obj);
 117       }
 118       if (_during_initial_mark) {
 119         // For the next marking info we'll only mark the
 120         // self-forwarded objects explicitly if we are during
 121         // initial-mark (since, normally, we only mark objects pointed
 122         // to by roots if we succeed in copying them). By marking all
 123         // self-forwarded objects we ensure that we mark any that are
 124         // still pointed to be roots. During concurrent marking, and
 125         // after initial-mark, we don't need to mark any objects
 126         // explicitly and all objects in the CSet are considered
 127         // (implicitly) live. So, we won't mark them explicitly and
 128         // we'll leave them over NTAMS.
 129         _cm->mark_in_next_bitmap(_worker_id, _hr, obj);
 130       }
 131       size_t obj_size = obj->size();
 132 
 133       _marked_bytes += (obj_size * HeapWordSize);
 134       PreservedMarks::init_forwarded_mark(obj);
 135 
 136       // While we were processing RSet buffers during the collection,
 137       // we actually didn't scan any cards on the collection set,
 138       // since we didn't want to update remembered sets with entries
 139       // that point into the collection set, given that live objects
 140       // from the collection set are about to move and such entries
 141       // will be stale very soon.
 142       // This change also dealt with a reliability issue which
 143       // involved scanning a card in the collection set and coming
 144       // across an array that was being chunked and looking malformed.
 145       // The problem is that, if evacuation fails, we might have
 146       // remembered set entries missing given that we skipped cards on
 147       // the collection set. So, we'll recreate such entries now.
 148       obj->oop_iterate(_update_rset_cl);
 149 
 150       HeapWord* obj_end = obj_addr + obj_size;
 151       _last_forwarded_object_end = obj_end;
 152       _hr->cross_threshold(obj_addr, obj_end);
 153     }
 154   }
 155 
 156   // Fill the memory area from start to end with filler objects, and update the BOT
 157   // and the mark bitmap accordingly.
 158   void zap_dead_objects(HeapWord* start, HeapWord* end) {
 159     if (start == end) {
 160       return;
 161     }
 162 
 163     size_t gap_size = pointer_delta(end, start);
 164     MemRegion mr(start, gap_size);
 165     if (gap_size >= CollectedHeap::min_fill_size()) {
 166       CollectedHeap::fill_with_objects(start, gap_size);
 167 
 168       HeapWord* end_first_obj = start + ((oop)start)->size();
 169       _hr->cross_threshold(start, end_first_obj);
 170       // Fill_with_objects() may have created multiple (i.e. two)
 171       // objects, as the max_fill_size() is half a region.
 172       // After updating the BOT for the first object, also update the
 173       // BOT for the second object to make the BOT complete.
 174       if (end_first_obj != end) {
 175         _hr->cross_threshold(end_first_obj, end);
 176 #ifdef ASSERT
 177         size_t size_second_obj = ((oop)end_first_obj)->size();
 178         HeapWord* end_of_second_obj = end_first_obj + size_second_obj;
 179         assert(end == end_of_second_obj,
 180                "More than two objects were used to fill the area from " PTR_FORMAT " to " PTR_FORMAT ", "
 181                "second objects size " SIZE_FORMAT " ends at " PTR_FORMAT,
 182                p2i(start), p2i(end), size_second_obj, p2i(end_of_second_obj));
 183 #endif
 184       }
 185     }
 186     _cm->clear_range_in_prev_bitmap(mr);
 187   }
 188 
 189   void zap_remainder() {
 190     zap_dead_objects(_last_forwarded_object_end, _hr->top());
 191   }
 192 };
 193 
 194 class RemoveSelfForwardPtrHRClosure: public HeapRegionClosure {
 195   G1CollectedHeap* _g1h;
 196   uint _worker_id;
 197   HeapRegionClaimer* _hrclaimer;
 198 
 199   G1DirtyCardQueue _dcq;
 200   UpdateRSetDeferred _update_rset_cl;
 201 
 202 public:
 203   RemoveSelfForwardPtrHRClosure(uint worker_id,
 204                                 HeapRegionClaimer* hrclaimer) :
 205     _g1h(G1CollectedHeap::heap()),
 206     _worker_id(worker_id),
 207     _hrclaimer(hrclaimer),
 208     _dcq(&_g1h->dirty_card_queue_set()),
 209     _update_rset_cl(&_dcq){
 210   }
 211 
 212   size_t remove_self_forward_ptr_by_walking_hr(HeapRegion* hr,
 213                                                bool during_initial_mark) {
 214     RemoveSelfForwardPtrObjClosure rspc(hr,
 215                                         &_update_rset_cl,
 216                                         during_initial_mark,
 217                                         _worker_id);
 218     hr->object_iterate(&rspc);
 219     // Need to zap the remainder area of the processed region.
 220     rspc.zap_remainder();
 221 
 222     return rspc.marked_bytes();
 223   }
 224 
 225   bool do_heap_region(HeapRegion *hr) {
 226     assert(!hr->is_pinned(), "Unexpected pinned region at index %u", hr->hrm_index());
 227     assert(hr->in_collection_set(), "bad CS");
 228 
 229     if (_hrclaimer->claim_region(hr->hrm_index())) {
 230       if (hr->evacuation_failed()) {
 231         bool during_initial_mark = _g1h->collector_state()->in_initial_mark_gc();
 232         bool during_conc_mark = _g1h->collector_state()->mark_or_rebuild_in_progress();
 233 
 234         hr->note_self_forwarding_removal_start(during_initial_mark,
 235                                                during_conc_mark);
 236         _g1h->verifier()->check_bitmaps("Self-Forwarding Ptr Removal", hr);
 237 
 238         hr->reset_bot();
 239 
 240         size_t live_bytes = remove_self_forward_ptr_by_walking_hr(hr, during_initial_mark);
 241 
 242         hr->rem_set()->clean_strong_code_roots(hr);
 243         hr->rem_set()->clear_locked(true);
 244 
 245         hr->note_self_forwarding_removal_end(live_bytes);
 246       }
 247     }
 248     return false;
 249   }
 250 };
 251 
 252 G1ParRemoveSelfForwardPtrsTask::G1ParRemoveSelfForwardPtrsTask() :
 253   AbstractGangTask("G1 Remove Self-forwarding Pointers"),
 254   _g1h(G1CollectedHeap::heap()),
 255   _hrclaimer(_g1h->workers()->active_workers()) { }
 256 
 257 void G1ParRemoveSelfForwardPtrsTask::work(uint worker_id) {
 258   RemoveSelfForwardPtrHRClosure rsfp_cl(worker_id, &_hrclaimer);
 259 
 260   _g1h->collection_set_iterate_from(&rsfp_cl, worker_id);
 261 }