1 /*
   2  * Copyright (c) 2012, 2015, 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_implementation/g1/concurrentMark.inline.hpp"
  27 #include "gc_implementation/g1/dirtyCardQueue.hpp"
  28 #include "gc_implementation/g1/g1CollectedHeap.inline.hpp"
  29 #include "gc_implementation/g1/g1EvacFailure.hpp"
  30 #include "gc_implementation/g1/g1_globals.hpp"
  31 #include "gc_implementation/g1/g1OopClosures.inline.hpp"
  32 #include "gc_implementation/g1/heapRegion.hpp"
  33 #include "gc_implementation/g1/heapRegionRemSet.hpp"
  34 
  35 // Closures and tasks associated with any self-forwarding pointers
  36 // installed as a result of an evacuation failure.
  37 
  38 class UpdateRSetDeferred : public OopsInHeapRegionClosure {
  39 private:
  40   G1CollectedHeap* _g1;
  41   DirtyCardQueue *_dcq;
  42   G1SATBCardTableModRefBS* _ct_bs;
  43 
  44 public:
  45   UpdateRSetDeferred(G1CollectedHeap* g1, DirtyCardQueue* dcq) :
  46     _g1(g1), _ct_bs(_g1->g1_barrier_set()), _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(_from->is_in_reserved(p), "paranoia");
  52     if (!_from->is_in_reserved(oopDesc::load_decode_heap_oop(p)) &&
  53         !_from->is_survivor()) {
  54       size_t card_index = _ct_bs->index_for(p);
  55       if (_ct_bs->mark_card_deferred(card_index)) {
  56         _dcq->enqueue((jbyte*)_ct_bs->byte_for_index(card_index));
  57       }
  58     }
  59   }
  60 };
  61 
  62 class RemoveSelfForwardPtrObjClosure: public ObjectClosure {
  63 private:
  64   G1CollectedHeap* _g1;
  65   ConcurrentMark* _cm;
  66   HeapRegion* _hr;
  67   size_t _marked_bytes;
  68   OopsInHeapRegionClosure *_update_rset_cl;
  69   bool _during_initial_mark;
  70   bool _during_conc_mark;
  71   uint _worker_id;
  72   HeapWord* _end_of_last_gap;
  73   HeapWord* _last_gap_threshold;
  74   HeapWord* _last_obj_threshold;
  75 
  76 public:
  77   RemoveSelfForwardPtrObjClosure(G1CollectedHeap* g1, ConcurrentMark* cm,
  78                                  HeapRegion* hr,
  79                                  OopsInHeapRegionClosure* update_rset_cl,
  80                                  bool during_initial_mark,
  81                                  bool during_conc_mark,
  82                                  uint worker_id) :
  83     _g1(g1), _cm(cm), _hr(hr), _marked_bytes(0),
  84     _update_rset_cl(update_rset_cl),
  85     _during_initial_mark(during_initial_mark),
  86     _during_conc_mark(during_conc_mark),
  87     _worker_id(worker_id),
  88     _end_of_last_gap(hr->bottom()),
  89     _last_gap_threshold(hr->bottom()),
  90     _last_obj_threshold(hr->bottom()) { }
  91 
  92   size_t marked_bytes() { return _marked_bytes; }
  93 
  94   // <original comment>
  95   // The original idea here was to coalesce evacuated and dead objects.
  96   // However that caused complications with the block offset table (BOT).
  97   // In particular if there were two TLABs, one of them partially refined.
  98   // |----- TLAB_1--------|----TLAB_2-~~~(partially refined part)~~~|
  99   // The BOT entries of the unrefined part of TLAB_2 point to the start
 100   // of TLAB_2. If the last object of the TLAB_1 and the first object
 101   // of TLAB_2 are coalesced, then the cards of the unrefined part
 102   // would point into middle of the filler object.
 103   // The current approach is to not coalesce and leave the BOT contents intact.
 104   // </original comment>
 105   //
 106   // We now reset the BOT when we start the object iteration over the
 107   // region and refine its entries for every object we come across. So
 108   // the above comment is not really relevant and we should be able
 109   // to coalesce dead objects if we want to.
 110   void do_object(oop obj) {
 111     HeapWord* obj_addr = (HeapWord*) obj;
 112     assert(_hr->is_in(obj_addr), "sanity");
 113     size_t obj_size = obj->size();
 114     HeapWord* obj_end = obj_addr + obj_size;
 115 
 116     if (_end_of_last_gap != obj_addr) {
 117       // there was a gap before obj_addr
 118       _last_gap_threshold = _hr->cross_threshold(_end_of_last_gap, obj_addr);
 119     }
 120 
 121     if (obj->is_forwarded() && obj->forwardee() == obj) {
 122       // The object failed to move.
 123 
 124       // We consider all objects that we find self-forwarded to be
 125       // live. What we'll do is that we'll update the prev marking
 126       // info so that they are all under PTAMS and explicitly marked.
 127       if (!_cm->isPrevMarked(obj)) {
 128         _cm->markPrev(obj);
 129       }
 130       if (_during_initial_mark) {
 131         // For the next marking info we'll only mark the
 132         // self-forwarded objects explicitly if we are during
 133         // initial-mark (since, normally, we only mark objects pointed
 134         // to by roots if we succeed in copying them). By marking all
 135         // self-forwarded objects we ensure that we mark any that are
 136         // still pointed to be roots. During concurrent marking, and
 137         // after initial-mark, we don't need to mark any objects
 138         // explicitly and all objects in the CSet are considered
 139         // (implicitly) live. So, we won't mark them explicitly and
 140         // we'll leave them over NTAMS.
 141         _cm->grayRoot(obj, obj_size, _worker_id, _hr);
 142       }
 143       _marked_bytes += (obj_size * HeapWordSize);
 144       obj->set_mark(markOopDesc::prototype());
 145 
 146       // While we were processing RSet buffers during the collection,
 147       // we actually didn't scan any cards on the collection set,
 148       // since we didn't want to update remembered sets with entries
 149       // that point into the collection set, given that live objects
 150       // from the collection set are about to move and such entries
 151       // will be stale very soon.
 152       // This change also dealt with a reliability issue which
 153       // involved scanning a card in the collection set and coming
 154       // across an array that was being chunked and looking malformed.
 155       // The problem is that, if evacuation fails, we might have
 156       // remembered set entries missing given that we skipped cards on
 157       // the collection set. So, we'll recreate such entries now.
 158       obj->oop_iterate(_update_rset_cl);
 159     } else {
 160 
 161       // The object has been either evacuated or is dead. Fill it with a
 162       // dummy object.
 163       MemRegion mr(obj_addr, obj_size);
 164       CollectedHeap::fill_with_object(mr);
 165 
 166       // must nuke all dead objects which we skipped when iterating over the region
 167       _cm->clearRangePrevBitmap(MemRegion(_end_of_last_gap, obj_end));
 168     }
 169     _end_of_last_gap = obj_end;
 170     _last_obj_threshold = _hr->cross_threshold(obj_addr, obj_end);
 171   }
 172 };
 173 
 174 class RemoveSelfForwardPtrHRClosure: public HeapRegionClosure {
 175   G1CollectedHeap* _g1h;
 176   ConcurrentMark* _cm;
 177   uint _worker_id;
 178   HeapRegionClaimer* _hrclaimer;
 179 
 180   DirtyCardQueue _dcq;
 181   UpdateRSetDeferred _update_rset_cl;
 182 
 183 public:
 184   RemoveSelfForwardPtrHRClosure(G1CollectedHeap* g1h,
 185                                 uint worker_id,
 186                                 HeapRegionClaimer* hrclaimer) :
 187       _g1h(g1h), _dcq(&g1h->dirty_card_queue_set()), _update_rset_cl(g1h, &_dcq),
 188       _worker_id(worker_id), _cm(_g1h->concurrent_mark()), _hrclaimer(hrclaimer) {
 189   }
 190 
 191   bool doHeapRegion(HeapRegion *hr) {
 192     bool during_initial_mark = _g1h->g1_policy()->during_initial_mark_pause();
 193     bool during_conc_mark = _g1h->mark_in_progress();
 194 
 195     assert(!hr->is_humongous(), "sanity");
 196     assert(hr->in_collection_set(), "bad CS");
 197 
 198     if (_hrclaimer->claim_region(hr->hrm_index())) {
 199       if (hr->evacuation_failed()) {
 200         RemoveSelfForwardPtrObjClosure rspc(_g1h, _cm, hr, &_update_rset_cl,
 201                                             during_initial_mark,
 202                                             during_conc_mark,
 203                                             _worker_id);
 204 
 205         hr->note_self_forwarding_removal_start(during_initial_mark,
 206                                                during_conc_mark);
 207         _g1h->check_bitmaps("Self-Forwarding Ptr Removal", hr);
 208 
 209         // In the common case (i.e. when there is no evacuation
 210         // failure) we make sure that the following is done when
 211         // the region is freed so that it is "ready-to-go" when it's
 212         // re-allocated. However, when evacuation failure happens, a
 213         // region will remain in the heap and might ultimately be added
 214         // to a CSet in the future. So we have to be careful here and
 215         // make sure the region's RSet is ready for parallel iteration
 216         // whenever this might be required in the future.
 217         hr->rem_set()->reset_for_par_iteration();
 218         hr->reset_bot();
 219         _update_rset_cl.set_region(hr);
 220         hr->object_iterate(&rspc);
 221 
 222         hr->rem_set()->clean_strong_code_roots(hr);
 223 
 224         hr->note_self_forwarding_removal_end(during_initial_mark,
 225                                              during_conc_mark,
 226                                              rspc.marked_bytes());
 227       }
 228     }
 229     return false;
 230   }
 231 };
 232 
 233 G1ParRemoveSelfForwardPtrsTask::G1ParRemoveSelfForwardPtrsTask(G1CollectedHeap* g1h) :
 234     AbstractGangTask("G1 Remove Self-forwarding Pointers"), _g1h(g1h),
 235     _hrclaimer(g1h->workers()->active_workers()) {}
 236 
 237 void G1ParRemoveSelfForwardPtrsTask::work(uint worker_id) {
 238   RemoveSelfForwardPtrHRClosure rsfp_cl(_g1h, worker_id, &_hrclaimer);
 239 
 240   HeapRegion* hr = _g1h->start_cset_region_for_worker(worker_id);
 241   _g1h->collection_set_iterate_from(hr, &rsfp_cl);
 242 }