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