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/g1FullGCCompactionPoint.hpp"
  30 #include "gc/g1/g1FullGCCompactTask.hpp"
  31 #include "gc/g1/heapRegion.inline.hpp"
  32 #include "gc/shared/gcTraceTime.inline.hpp"
  33 #include "logging/log.hpp"
  34 #include "utilities/ticks.inline.hpp"
  35 
  36 class G1ResetHumongousClosure : public HeapRegionClosure {
  37   G1CMBitMap* _bitmap;
  38 
  39 public:
  40   G1ResetHumongousClosure(G1CMBitMap* bitmap) :
  41       _bitmap(bitmap) { }
  42 
  43   bool doHeapRegion(HeapRegion* current) {
  44     if (current->is_humongous()) {
  45       if (current->is_starts_humongous()) {
  46         oop obj = oop(current->bottom());
  47         if (_bitmap->is_marked(obj)) {
  48           // Clear bitmap and fix mark word.
  49           _bitmap->clear(obj);
  50           obj->init_mark();
  51         } else {
  52           assert(current->is_empty(), "Should have been cleared in phase 2.");
  53         }
  54       }
  55       current->reset_during_compaction();
  56     }
  57     return false;
  58   }
  59 };
  60 
  61 size_t G1FullGCCompactTask::G1CompactRegionClosure::apply(oop obj) {
  62   size_t size = obj->size();
  63   HeapWord* destination = (HeapWord*)obj->forwardee();
  64   if (destination == NULL) {
  65     // Object not moving
  66     return size;
  67   }
  68 
  69   // copy object and reinit its mark
  70   HeapWord* obj_addr = (HeapWord*) obj;
  71   assert(obj_addr != destination, "everything in this pass should be moving");
  72   Copy::aligned_conjoint_words(obj_addr, destination, size);
  73   oop(destination)->init_mark();
  74   assert(oop(destination)->klass() != NULL, "should have a class");
  75 
  76   return size;
  77 }
  78 
  79 void G1FullGCCompactTask::compact_region(HeapRegion* hr) {
  80   assert(!hr->is_humongous(), "Should be no humongous regions in compaction queue");
  81   G1CompactRegionClosure compact(collector()->mark_bitmap());
  82   hr->apply_to_marked_objects(collector()->mark_bitmap(), &compact);
  83   // Once all objects have been moved the liveness information
  84   // needs be cleared.
  85   collector()->mark_bitmap()->clear_region(hr);
  86   hr->complete_compaction();
  87 }
  88 
  89 void G1FullGCCompactTask::work(uint worker_id) {
  90   Ticks start = Ticks::now();
  91   GrowableArray<HeapRegion*>* compaction_queue = collector()->compaction_point(worker_id)->regions();
  92   for (GrowableArrayIterator<HeapRegion*> it = compaction_queue->begin();
  93        it != compaction_queue->end();
  94        ++it) {
  95     compact_region(*it);
  96   }
  97 
  98   G1ResetHumongousClosure hc(collector()->mark_bitmap());
  99   G1CollectedHeap::heap()->heap_region_par_iterate_from_worker_offset(&hc, &_claimer, worker_id);
 100   log_task("Compaction task", worker_id, start);
 101 }
 102 
 103 void G1FullGCCompactTask::serial_compaction() {
 104   GCTraceTime(Debug, gc, phases) tm("Phase 4: Serial Compaction", collector()->scope()->timer());
 105   GrowableArray<HeapRegion*>* compaction_queue = collector()->serial_compaction_point()->regions();
 106   for (GrowableArrayIterator<HeapRegion*> it = compaction_queue->begin();
 107        it != compaction_queue->end();
 108        ++it) {
 109     compact_region(*it);
 110   }
 111 }