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