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