1 /*
   2  * Copyright (c) 2016, 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 #ifndef SHARE_VM_GC_G1_G1HEAPVERIFIER_HPP
  26 #define SHARE_VM_GC_G1_G1HEAPVERIFIER_HPP
  27 
  28 #include "gc/g1/heapRegionSet.hpp"
  29 #include "memory/allocation.hpp"
  30 #include "memory/universe.hpp"
  31 
  32 class G1CollectedHeap;
  33 
  34 class G1HeapVerifier : public CHeapObj<mtGC> {
  35 private:
  36   G1CollectedHeap* _g1h;
  37   int _enabled_verification_types;
  38 
  39   // verify_region_sets() performs verification over the region
  40   // lists. It will be compiled in the product code to be used when
  41   // necessary (i.e., during heap verification).
  42   void verify_region_sets();
  43 
  44 public:
  45   enum G1VerifyType {
  46     G1VerifyYoungOnly   =  1, // -XX:VerifyGCType=young-only
  47     G1VerifyInitialMark =  2, // -XX:VerifyGCType=initial-mark
  48     G1VerifyMixed       =  4, // -XX:VerifyGCType=mixed
  49     G1VerifyRemark      =  8, // -XX:VerifyGCType=remark
  50     G1VerifyCleanup     = 16, // -XX:VerifyGCType=cleanup
  51     G1VerifyFull        = 32, // -XX:VerifyGCType=full
  52     G1VerifyAll         = -1
  53   };
  54 
  55   G1HeapVerifier(G1CollectedHeap* heap) : _g1h(heap), _enabled_verification_types(G1VerifyAll) { }
  56 
  57   void parse_verification_type(const char* type);
  58   void enable_verification_type(G1VerifyType type);
  59   bool should_verify(G1VerifyType type);
  60 
  61   // Perform verification.
  62 
  63   // vo == UsePrevMarking -> use "prev" marking information,
  64   // vo == UseNextMarking -> use "next" marking information
  65   // vo == UseFullMarking -> use "next" marking bitmap but no TAMS
  66   //
  67   // NOTE: Only the "prev" marking information is guaranteed to be
  68   // consistent most of the time, so most calls to this should use
  69   // vo == UsePrevMarking.
  70   // Currently, there is only one case where this is called with
  71   // vo == UseNextMarking, which is to verify the "next" marking
  72   // information at the end of remark.
  73   // Currently there is only one place where this is called with
  74   // vo == UseFullMarking, which is to verify the marking during a
  75   // full GC.
  76   void verify(VerifyOption vo);
  77 
  78   // verify_region_sets_optional() is planted in the code for
  79   // list verification in non-product builds (and it can be enabled in
  80   // product builds by defining HEAP_REGION_SET_FORCE_VERIFY to be 1).
  81 #if HEAP_REGION_SET_FORCE_VERIFY
  82   void verify_region_sets_optional() {
  83     verify_region_sets();
  84   }
  85 #else // HEAP_REGION_SET_FORCE_VERIFY
  86   void verify_region_sets_optional() { }
  87 #endif // HEAP_REGION_SET_FORCE_VERIFY
  88 
  89   void prepare_for_verify();
  90   double verify(G1VerifyType type, VerifyOption vo, const char* msg);
  91   void verify_before_gc(G1VerifyType type);
  92   void verify_after_gc(G1VerifyType type);
  93 
  94 #ifndef PRODUCT
  95   // Make sure that the given bitmap has no marked objects in the
  96   // range [from,limit). If it does, print an error message and return
  97   // false. Otherwise, just return true. bitmap_name should be "prev"
  98   // or "next".
  99   bool verify_no_bits_over_tams(const char* bitmap_name, const G1CMBitMap* const bitmap,
 100                                 HeapWord* from, HeapWord* limit);
 101 
 102   // Verify that the prev / next bitmap range [tams,end) for the given
 103   // region has no marks. Return true if all is well, false if errors
 104   // are detected.
 105   bool verify_bitmaps(const char* caller, HeapRegion* hr);
 106 #endif // PRODUCT
 107 
 108   // If G1VerifyBitmaps is set, verify that the marking bitmaps for
 109   // the given region do not have any spurious marks. If errors are
 110   // detected, print appropriate error messages and crash.
 111   void check_bitmaps(const char* caller, HeapRegion* hr) PRODUCT_RETURN;
 112 
 113   // If G1VerifyBitmaps is set, verify that the marking bitmaps do not
 114   // have any spurious marks. If errors are detected, print
 115   // appropriate error messages and crash.
 116   void check_bitmaps(const char* caller) PRODUCT_RETURN;
 117 
 118   // Do sanity check on the contents of the in-cset fast test table.
 119   bool check_cset_fast_test() PRODUCT_RETURN_( return true; );
 120 
 121   void verify_card_table_cleanup() PRODUCT_RETURN;
 122 
 123   void verify_not_dirty_region(HeapRegion* hr) PRODUCT_RETURN;
 124   void verify_dirty_region(HeapRegion* hr) PRODUCT_RETURN;
 125   void verify_dirty_young_regions() PRODUCT_RETURN;
 126 
 127   static void verify_archive_regions();
 128 };
 129 
 130 #endif // SHARE_VM_GC_G1_G1HEAPVERIFIER_HPP