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