1 /*
   2  * Copyright (c) 2017, 2018 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/g1FullGCMarker.inline.hpp"
  28 #include "gc/g1/g1FullGCOopClosures.inline.hpp"
  29 #include "gc/g1/g1_specialized_oop_closures.hpp"
  30 #include "logging/logStream.hpp"
  31 #include "oops/oop.inline.hpp"
  32 
  33 void G1MarkAndPushClosure::do_oop(oop* p) {
  34   do_oop_nv(p);
  35 }
  36 
  37 void G1MarkAndPushClosure::do_oop(narrowOop* p) {
  38   do_oop_nv(p);
  39 }
  40 
  41 bool G1MarkAndPushClosure::do_metadata() {
  42   return do_metadata_nv();
  43 }
  44 
  45 void G1MarkAndPushClosure::do_klass(Klass* k) {
  46   do_klass_nv(k);
  47 }
  48 
  49 void G1MarkAndPushClosure::do_cld(ClassLoaderData* cld) {
  50   do_cld_nv(cld);
  51 }
  52 
  53 G1AdjustAndRebuildClosure::G1AdjustAndRebuildClosure(uint worker_id) :
  54   _worker_id(worker_id),
  55   _compaction_delta(0),
  56   _g1h(G1CollectedHeap::heap()) { }
  57 
  58 void G1AdjustAndRebuildClosure::update_compaction_delta(oop obj) {
  59   if (G1ArchiveAllocator::is_open_archive_object(obj)) {
  60     _compaction_delta = 0;
  61     return;
  62   }
  63   oop forwardee = obj->forwardee();
  64   if (forwardee == NULL) {
  65     // Object not moved.
  66     _compaction_delta = 0;
  67   } else {
  68     // Object moved to forwardee, calculate delta.
  69     _compaction_delta = calculate_compaction_delta(obj, forwardee);
  70   }
  71 }
  72 
  73 void G1AdjustClosure::do_oop(oop* p)       { adjust_pointer(p); }
  74 void G1AdjustClosure::do_oop(narrowOop* p) { adjust_pointer(p); }
  75 
  76 void G1AdjustAndRebuildClosure::do_oop(oop* p)       { do_oop_nv(p); }
  77 void G1AdjustAndRebuildClosure::do_oop(narrowOop* p) { do_oop_nv(p); }
  78 
  79 void G1FollowStackClosure::do_void() { _marker->drain_stack(); }
  80 
  81 void G1FullKeepAliveClosure::do_oop(oop* p) { do_oop_work(p); }
  82 void G1FullKeepAliveClosure::do_oop(narrowOop* p) { do_oop_work(p); }
  83 
  84 G1VerifyOopClosure::G1VerifyOopClosure(VerifyOption option) :
  85    _g1h(G1CollectedHeap::heap()),
  86    _containing_obj(NULL),
  87    _verify_option(option),
  88    _cc(0),
  89    _failures(false) {
  90 }
  91 
  92 void G1VerifyOopClosure::print_object(outputStream* out, oop obj) {
  93 #ifdef PRODUCT
  94   Klass* k = obj->klass();
  95   const char* class_name = InstanceKlass::cast(k)->external_name();
  96   out->print_cr("class name %s", class_name);
  97 #else // PRODUCT
  98   obj->print_on(out);
  99 #endif // PRODUCT
 100 }
 101 
 102 template <class T> void G1VerifyOopClosure::do_oop_nv(T* p) {
 103   T heap_oop = oopDesc::load_heap_oop(p);
 104   if (!oopDesc::is_null(heap_oop)) {
 105     _cc++;
 106     oop obj = oopDesc::decode_heap_oop_not_null(heap_oop);
 107     bool failed = false;
 108     if (!_g1h->is_in_closed_subset(obj) || _g1h->is_obj_dead_cond(obj, _verify_option)) {
 109       MutexLockerEx x(ParGCRareEvent_lock,
 110           Mutex::_no_safepoint_check_flag);
 111       LogStreamHandle(Error, gc, verify) yy;
 112       if (!_failures) {
 113         yy.cr();
 114         yy.print_cr("----------");
 115       }
 116       if (!_g1h->is_in_closed_subset(obj)) {
 117         HeapRegion* from = _g1h->heap_region_containing((HeapWord*)p);
 118         yy.print_cr("Field " PTR_FORMAT
 119             " of live obj " PTR_FORMAT " in region "
 120             "[" PTR_FORMAT ", " PTR_FORMAT ")",
 121             p2i(p), p2i(_containing_obj),
 122             p2i(from->bottom()), p2i(from->end()));
 123         print_object(&yy, _containing_obj);
 124         yy.print_cr("points to obj " PTR_FORMAT " not in the heap",
 125             p2i(obj));
 126       } else {
 127         HeapRegion* from = _g1h->heap_region_containing((HeapWord*)p);
 128         HeapRegion* to   = _g1h->heap_region_containing((HeapWord*)obj);
 129         yy.print_cr("Field " PTR_FORMAT
 130             " of live obj " PTR_FORMAT " in region "
 131             "[" PTR_FORMAT ", " PTR_FORMAT ")",
 132             p2i(p), p2i(_containing_obj),
 133             p2i(from->bottom()), p2i(from->end()));
 134         print_object(&yy, _containing_obj);
 135         yy.print_cr("points to dead obj " PTR_FORMAT " in region "
 136             "[" PTR_FORMAT ", " PTR_FORMAT ")",
 137             p2i(obj), p2i(to->bottom()), p2i(to->end()));
 138         print_object(&yy, obj);
 139       }
 140       yy.print_cr("----------");
 141       yy.flush();
 142       _failures = true;
 143       failed = true;
 144     }
 145   }
 146 }
 147 
 148 template void G1VerifyOopClosure::do_oop_nv(oop*);
 149 template void G1VerifyOopClosure::do_oop_nv(narrowOop*);
 150 
 151 // Generate G1 full GC specialized oop_oop_iterate functions.
 152 SPECIALIZED_OOP_OOP_ITERATE_CLOSURES_G1FULL(ALL_KLASS_OOP_OOP_ITERATE_DEFN)