1 /*
   2  * Copyright (c) 2017, 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 "classfile/classLoaderDataGraph.hpp"
  27 #include "gc/g1/g1CollectedHeap.hpp"
  28 #include "gc/g1/g1ConcurrentMarkBitMap.inline.hpp"
  29 #include "gc/g1/g1FullCollector.hpp"
  30 #include "gc/g1/g1FullGCAdjustTask.hpp"
  31 #include "gc/g1/g1FullGCCompactionPoint.hpp"
  32 #include "gc/g1/g1FullGCMarker.hpp"
  33 #include "gc/g1/g1FullGCOopClosures.inline.hpp"
  34 #include "gc/g1/heapRegion.inline.hpp"
  35 #include "gc/shared/gcTraceTime.inline.hpp"
  36 #include "gc/shared/referenceProcessor.hpp"
  37 #include "gc/shared/weakProcessor.inline.hpp"
  38 #include "logging/log.hpp"
  39 #include "memory/iterator.inline.hpp"
  40 #include "runtime/atomic.hpp"
  41 
  42 class G1AdjustLiveClosure : public StackObj {
  43   G1AdjustClosure* _adjust_closure;
  44 public:
  45   G1AdjustLiveClosure(G1AdjustClosure* cl) :
  46     _adjust_closure(cl) { }
  47 
  48   size_t apply(oop object) {
  49     return object->oop_iterate_size(_adjust_closure);
  50   }
  51 };
  52 
  53 class G1AdjustRegionClosure : public HeapRegionClosure {
  54   G1CMBitMap* _bitmap;
  55   uint _worker_id;
  56  public:
  57   G1AdjustRegionClosure(G1CMBitMap* bitmap, uint worker_id) :
  58     _bitmap(bitmap),
  59     _worker_id(worker_id) { }
  60 
  61   bool do_heap_region(HeapRegion* r) {
  62     G1AdjustClosure cl;
  63     if (r->is_humongous()) {
  64       oop obj = oop(r->humongous_start_region()->bottom());
  65       obj->oop_iterate(&cl, MemRegion(r->bottom(), r->top()));
  66     } else if (r->is_open_archive()) {
  67       // Only adjust the open archive regions, the closed ones
  68       // never change.
  69       G1AdjustLiveClosure adjust(&cl);
  70       r->apply_to_marked_objects(_bitmap, &adjust);
  71       // Open archive regions will not be compacted and the marking information is
  72       // no longer needed. Clear it here to avoid having to do it later.
  73       _bitmap->clear_region(r);
  74     } else {
  75       G1AdjustLiveClosure adjust(&cl);
  76       r->apply_to_marked_objects(_bitmap, &adjust);
  77     }
  78     return false;
  79   }
  80 };
  81 
  82 G1FullGCAdjustTask::G1FullGCAdjustTask(G1FullCollector* collector) :
  83     G1FullGCTask("G1 Adjust", collector),
  84     _root_processor(G1CollectedHeap::heap(), collector->workers()),
  85     _references_done(0),
  86     _weak_proc_task(collector->workers()),
  87     _hrclaimer(collector->workers()),
  88     _adjust(),
  89     _string_dedup_cleaning_task(NULL, &_adjust, false) {
  90   // Need cleared claim bits for the roots processing
  91   ClassLoaderDataGraph::clear_claimed_marks();
  92 }
  93 
  94 void G1FullGCAdjustTask::work(uint worker_id) {
  95   Ticks start = Ticks::now();
  96   ResourceMark rm;
  97 
  98   // Adjust preserved marks first since they are not balanced.
  99   G1FullGCMarker* marker = collector()->marker(worker_id);
 100   marker->preserved_stack()->adjust_during_full_gc();
 101 
 102   // Adjust the weak roots.
 103 
 104   if (Atomic::add(&_references_done, 1u) == 1u) { // First incr claims task.
 105     G1CollectedHeap::heap()->ref_processor_stw()->weak_oops_do(&_adjust);
 106   }
 107 
 108   AlwaysTrueClosure always_alive;
 109   _weak_proc_task.work(worker_id, &always_alive, &_adjust);
 110 
 111   CLDToOopClosure adjust_cld(&_adjust, ClassLoaderData::_claim_strong);
 112   CodeBlobToOopClosure adjust_code(&_adjust, CodeBlobToOopClosure::FixRelocations);
 113   _root_processor.process_all_roots(&_adjust, &adjust_cld, &adjust_code);
 114 
 115   // Adjust string dedup data structures.
 116   _string_dedup_cleaning_task.work(worker_id);
 117 
 118   // Now adjust pointers region by region
 119   G1AdjustRegionClosure blk(collector()->mark_bitmap(), worker_id);
 120   G1CollectedHeap::heap()->heap_region_par_iterate_from_worker_offset(&blk, &_hrclaimer, worker_id);
 121   log_task("Adjust task", worker_id, start);
 122 }