1 /*
  2  * Copyright (c) 2019, 2021, Red Hat, Inc. All rights reserved.
  3  * Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
  4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  5  *
  6  * This code is free software; you can redistribute it and/or modify it
  7  * under the terms of the GNU General Public License version 2 only, as
  8  * published by the Free Software Foundation.
  9  *
 10  * This code is distributed in the hope that it will be useful, but WITHOUT
 11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 13  * version 2 for more details (a copy is included in the LICENSE file that
 14  * accompanied this code).
 15  *
 16  * You should have received a copy of the GNU General Public License version
 17  * 2 along with this work; if not, write to the Free Software Foundation,
 18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 19  *
 20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 21  * or visit www.oracle.com if you need additional information or have any
 22  * questions.
 23  *
 24  */
 25 
 26 
 27 #include "precompiled.hpp"
 28 
 29 
 30 #include "classfile/classLoaderDataGraph.hpp"
 31 #include "code/codeCache.hpp"
 32 #include "gc/shenandoah/shenandoahAsserts.hpp"
 33 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
 34 #include "gc/shenandoah/shenandoahGeneration.hpp"
 35 #include "gc/shenandoah/shenandoahPhaseTimings.hpp"
 36 #include "gc/shenandoah/shenandoahRootVerifier.hpp"
 37 #include "gc/shenandoah/shenandoahScanRemembered.inline.hpp"
 38 #include "gc/shenandoah/shenandoahStringDedup.hpp"
 39 #include "gc/shenandoah/shenandoahUtils.hpp"
 40 #include "gc/shared/oopStorage.inline.hpp"
 41 #include "gc/shared/oopStorageSet.hpp"
 42 #include "runtime/javaThread.hpp"
 43 #include "runtime/jniHandles.hpp"
 44 #include "runtime/threads.hpp"
 45 #include "utilities/debug.hpp"
 46 #include "utilities/enumIterator.hpp"
 47 
 48 ShenandoahGCStateResetter::ShenandoahGCStateResetter() :
 49   _heap(ShenandoahHeap::heap()),
 50   _gc_state(_heap->gc_state()) {
 51   _heap->_gc_state.clear();
 52 }
 53 
 54 ShenandoahGCStateResetter::~ShenandoahGCStateResetter() {
 55   _heap->_gc_state.set(_gc_state);
 56   assert(_heap->gc_state() == _gc_state, "Should be restored");
 57 }
 58 
 59 void ShenandoahRootVerifier::roots_do(OopIterateClosure* oops) {
 60   ShenandoahGCStateResetter resetter;
 61   shenandoah_assert_safepoint();
 62 
 63   CodeBlobToOopClosure blobs(oops, !CodeBlobToOopClosure::FixRelocations);
 64   CodeCache::blobs_do(&blobs);
 65 
 66   CLDToOopClosure clds(oops, ClassLoaderData::_claim_none);
 67   ClassLoaderDataGraph::cld_do(&clds);
 68 
 69   for (auto id : EnumRange<OopStorageSet::StrongId>()) {
 70     OopStorageSet::storage(id)->oops_do(oops);
 71   }
 72 
 73   ShenandoahHeap* heap = ShenandoahHeap::heap();
 74   if (heap->mode()->is_generational() && heap->active_generation()->is_young()) {
 75     shenandoah_assert_safepoint();
 76     ShenandoahGenerationalHeap::heap()->old_generation()->card_scan()->roots_do(oops);
 77   }
 78 
 79   // Do thread roots the last. This allows verification code to find
 80   // any broken objects from those special roots first, not the accidental
 81   // dangling reference from the thread root.
 82   Threads::possibly_parallel_oops_do(true, oops, nullptr);
 83 }
 84 
 85 void ShenandoahRootVerifier::strong_roots_do(OopIterateClosure* oops) {
 86   ShenandoahGCStateResetter resetter;
 87   shenandoah_assert_safepoint();
 88 
 89   CLDToOopClosure clds(oops, ClassLoaderData::_claim_none);
 90   ClassLoaderDataGraph::always_strong_cld_do(&clds);
 91 
 92   for (auto id : EnumRange<OopStorageSet::StrongId>()) {
 93     OopStorageSet::storage(id)->oops_do(oops);
 94   }
 95 
 96   ShenandoahHeap* heap = ShenandoahHeap::heap();
 97   if (heap->mode()->is_generational() && heap->active_generation()->is_young()) {
 98     ShenandoahGenerationalHeap::heap()->old_generation()->card_scan()->roots_do(oops);
 99   }
100 
101   // Do thread roots the last. This allows verification code to find
102   // any broken objects from those special roots first, not the accidental
103   // dangling reference from the thread root.
104   CodeBlobToOopClosure blobs(oops, !CodeBlobToOopClosure::FixRelocations);
105   Threads::possibly_parallel_oops_do(true, oops, &blobs);
106 }