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