1 /*
   2  * Copyright (c) 2001, 2013, 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_MEMORY_SPECIALIZED_OOP_CLOSURES_HPP
  26 #define SHARE_VM_MEMORY_SPECIALIZED_OOP_CLOSURES_HPP
  27 
  28 #include "runtime/atomic.hpp"
  29 #include "utilities/macros.hpp"
  30 #if INCLUDE_ALL_GCS
  31 #include "gc_implementation/g1/g1_specialized_oop_closures.hpp"
  32 #include "gc_implementation/shenandoah/shenandoah_specialized_oop_closures.hpp"
  33 #endif // INCLUDE_ALL_GCS
  34 
  35 // The following OopClosure types get specialized versions of
  36 // "oop_oop_iterate" that invoke the closures' do_oop methods
  37 // non-virtually, using a mechanism defined in this file.  Extend these
  38 // macros in the obvious way to add specializations for new closures.
  39 
  40 // Forward declarations.
  41 class OopClosure;
  42 class OopsInGenClosure;
  43 // DefNew
  44 class ScanClosure;
  45 class FastScanClosure;
  46 class FilteringClosure;
  47 // ParNew
  48 class ParScanWithBarrierClosure;
  49 class ParScanWithoutBarrierClosure;
  50 // CMS
  51 class MarkRefsIntoAndScanClosure;
  52 class Par_MarkRefsIntoAndScanClosure;
  53 class PushAndMarkClosure;
  54 class Par_PushAndMarkClosure;
  55 class PushOrMarkClosure;
  56 class Par_PushOrMarkClosure;
  57 class CMSKeepAliveClosure;
  58 class CMSInnerParMarkAndPushClosure;
  59 // Misc
  60 class NoHeaderExtendedOopClosure;
  61 
  62 // This macro applies an argument macro to all OopClosures for which we
  63 // want specialized bodies of "oop_oop_iterate".  The arguments to "f" are:
  64 //   "f(closureType, non_virtual)"
  65 // where "closureType" is the name of the particular subclass of OopClosure,
  66 // and "non_virtual" will be the string "_nv" if the closure type should
  67 // have its "do_oop" method invoked non-virtually, or else the
  68 // string "_v".  ("OopClosure" itself will be the only class in the latter
  69 // category.)
  70 
  71 // This is split into several because of a Visual C++ 6.0 compiler bug
  72 // where very long macros cause the compiler to crash
  73 
  74 // Some other heap might define further specialized closures.
  75 #ifndef FURTHER_SPECIALIZED_OOP_OOP_ITERATE_CLOSURES
  76 #define FURTHER_SPECIALIZED_OOP_OOP_ITERATE_CLOSURES(f) \
  77         /* None */
  78 #endif
  79 
  80 #define SPECIALIZED_OOP_OOP_ITERATE_CLOSURES_S(f)       \
  81   f(ScanClosure,_nv)                                    \
  82   f(FastScanClosure,_nv)                                \
  83   f(FilteringClosure,_nv)
  84 
  85 #if INCLUDE_ALL_GCS
  86 #define SPECIALIZED_OOP_OOP_ITERATE_CLOSURES_P(f)       \
  87   f(ParScanWithBarrierClosure,_nv)                      \
  88   f(ParScanWithoutBarrierClosure,_nv)
  89 #else  // INCLUDE_ALL_GCS
  90 #define SPECIALIZED_OOP_OOP_ITERATE_CLOSURES_P(f)
  91 #endif // INCLUDE_ALL_GCS
  92 
  93 #define SPECIALIZED_OOP_OOP_ITERATE_CLOSURES_1(f)       \
  94   f(NoHeaderExtendedOopClosure,_nv)                     \
  95   SPECIALIZED_OOP_OOP_ITERATE_CLOSURES_S(f)             \
  96   SPECIALIZED_OOP_OOP_ITERATE_CLOSURES_P(f)
  97 
  98 #if INCLUDE_ALL_GCS
  99 #define SPECIALIZED_OOP_OOP_ITERATE_CLOSURES_2(f)       \
 100   f(MarkRefsIntoAndScanClosure,_nv)                     \
 101   f(Par_MarkRefsIntoAndScanClosure,_nv)                 \
 102   f(PushAndMarkClosure,_nv)                             \
 103   f(Par_PushAndMarkClosure,_nv)                         \
 104   f(PushOrMarkClosure,_nv)                              \
 105   f(Par_PushOrMarkClosure,_nv)                          \
 106   f(CMSKeepAliveClosure,_nv)                            \
 107   f(CMSInnerParMarkAndPushClosure,_nv)                  \
 108   FURTHER_SPECIALIZED_OOP_OOP_ITERATE_CLOSURES(f)       \
 109   SPECIALIZED_OOP_OOP_ITERATE_CLOSURES_SHENANDOAH(f)
 110 #else  // INCLUDE_ALL_GCS
 111 #define SPECIALIZED_OOP_OOP_ITERATE_CLOSURES_2(f)
 112 #endif // INCLUDE_ALL_GCS
 113 
 114 
 115 // We separate these out, because sometime the general one has
 116 // a different definition from the specialized ones, and sometimes it
 117 // doesn't.
 118 
 119 #define ALL_OOP_OOP_ITERATE_CLOSURES_1(f)               \
 120   f(ExtendedOopClosure,_v)                              \
 121   SPECIALIZED_OOP_OOP_ITERATE_CLOSURES_1(f)
 122 
 123 #define ALL_OOP_OOP_ITERATE_CLOSURES_2(f)               \
 124   SPECIALIZED_OOP_OOP_ITERATE_CLOSURES_2(f)
 125 
 126 #if INCLUDE_ALL_GCS
 127 // This macro applies an argument macro to all OopClosures for which we
 128 // want specialized bodies of a family of methods related to
 129 // "par_oop_iterate".  The arguments to f are the same as above.
 130 // The "root_class" is the most general class to define; this may be
 131 // "OopClosure" in some applications and "OopsInGenClosure" in others.
 132 
 133 #define SPECIALIZED_PAR_OOP_ITERATE_CLOSURES(f)        \
 134   f(MarkRefsIntoAndScanClosure,_nv)                    \
 135   f(PushAndMarkClosure,_nv)                            \
 136   f(Par_MarkRefsIntoAndScanClosure,_nv)                \
 137   f(Par_PushAndMarkClosure,_nv)
 138 
 139 #define ALL_PAR_OOP_ITERATE_CLOSURES(f)                \
 140   f(ExtendedOopClosure,_v)                             \
 141   SPECIALIZED_PAR_OOP_ITERATE_CLOSURES(f)
 142 #endif // INCLUDE_ALL_GCS
 143 
 144 // This macro applies an argument macro to all OopClosures for which we
 145 // want specialized bodies of a family of methods related to
 146 // "oops_since_save_marks_do".  The arguments to f are the same as above.
 147 // The "root_class" is the most general class to define; this may be
 148 // "OopClosure" in some applications and "OopsInGenClosure" in others.
 149 
 150 
 151 // Some other heap might define further specialized closures.
 152 #ifndef FURTHER_SPECIALIZED_SINCE_SAVE_MARKS_CLOSURES
 153 #define FURTHER_SPECIALIZED_SINCE_SAVE_MARKS_CLOSURES(f) \
 154         /* None */
 155 #endif
 156 
 157 #define SPECIALIZED_SINCE_SAVE_MARKS_CLOSURES_YOUNG_S(f) \
 158   f(ScanClosure,_nv)                                     \
 159   f(FastScanClosure,_nv)
 160 
 161 #if INCLUDE_ALL_GCS
 162 #define SPECIALIZED_SINCE_SAVE_MARKS_CLOSURES_YOUNG_P(f) \
 163   f(ParScanWithBarrierClosure,_nv)                       \
 164   f(ParScanWithoutBarrierClosure,_nv)                    \
 165   FURTHER_SPECIALIZED_SINCE_SAVE_MARKS_CLOSURES(f)
 166 #else  // INCLUDE_ALL_GCS
 167 #define SPECIALIZED_SINCE_SAVE_MARKS_CLOSURES_YOUNG_P(f)
 168 #endif // INCLUDE_ALL_GCS
 169 
 170 #define SPECIALIZED_SINCE_SAVE_MARKS_CLOSURES_YOUNG(f)  \
 171   SPECIALIZED_SINCE_SAVE_MARKS_CLOSURES_YOUNG_S(f)      \
 172   SPECIALIZED_SINCE_SAVE_MARKS_CLOSURES_YOUNG_P(f)
 173 
 174 #define SPECIALIZED_SINCE_SAVE_MARKS_CLOSURES(f)        \
 175   SPECIALIZED_SINCE_SAVE_MARKS_CLOSURES_YOUNG(f)
 176 
 177 // We separate these out, because sometime the general one has
 178 // a different definition from the specialized ones, and sometimes it
 179 // doesn't.
 180 // NOTE:   One of the valid criticisms of this
 181 // specialize-oop_oop_iterate-for-specific-closures idiom is that it is
 182 // easy to have a silent performance bug: if you fail to de-virtualize,
 183 // things still work, just slower.  The "SpecializationStats" mode is
 184 // intended to at least make such a failure easy to detect.
 185 // *Not* using the ALL_SINCE_SAVE_MARKS_CLOSURES(f) macro defined
 186 // below means that *only* closures for which oop_oop_iterate specializations
 187 // exist above may be applied to "oops_since_save_marks".  That is,
 188 // this form of the performance bug is caught statically.  When you add
 189 // a definition for the general type, this property goes away.
 190 // Make sure you test with SpecializationStats to find such bugs
 191 // when introducing a new closure where you don't want virtual dispatch.
 192 
 193 #define ALL_SINCE_SAVE_MARKS_CLOSURES(f)                \
 194   f(OopsInGenClosure,_v)                                \
 195   SPECIALIZED_SINCE_SAVE_MARKS_CLOSURES(f)
 196 
 197 // For keeping stats on effectiveness.
 198 #define ENABLE_SPECIALIZATION_STATS 0
 199 
 200 
 201 class SpecializationStats {
 202 public:
 203   enum Kind {
 204     ik,             // InstanceKlass
 205     irk,            // InstanceRefKlass
 206     oa,             // ObjArrayKlass
 207     NUM_Kinds
 208   };
 209 
 210 #if ENABLE_SPECIALIZATION_STATS
 211 private:
 212   static bool _init;
 213   static bool _wrapped;
 214   static jint _numCallsAll;
 215 
 216   static jint _numCallsTotal[NUM_Kinds];
 217   static jint _numCalls_nv[NUM_Kinds];
 218 
 219   static jint _numDoOopCallsTotal[NUM_Kinds];
 220   static jint _numDoOopCalls_nv[NUM_Kinds];
 221 public:
 222 #endif
 223   static void clear()  PRODUCT_RETURN;
 224 
 225   static inline void record_call()  PRODUCT_RETURN;
 226   static inline void record_iterate_call_v(Kind k)  PRODUCT_RETURN;
 227   static inline void record_iterate_call_nv(Kind k)  PRODUCT_RETURN;
 228   static inline void record_do_oop_call_v(Kind k)  PRODUCT_RETURN;
 229   static inline void record_do_oop_call_nv(Kind k)  PRODUCT_RETURN;
 230 
 231   static void print() PRODUCT_RETURN;
 232 };
 233 
 234 #ifndef PRODUCT
 235 #if ENABLE_SPECIALIZATION_STATS
 236 
 237 inline void SpecializationStats::record_call() {
 238   Atomic::inc(&_numCallsAll);
 239 }
 240 inline void SpecializationStats::record_iterate_call_v(Kind k) {
 241   Atomic::inc(&_numCallsTotal[k]);
 242 }
 243 inline void SpecializationStats::record_iterate_call_nv(Kind k) {
 244   Atomic::inc(&_numCallsTotal[k]);
 245   Atomic::inc(&_numCalls_nv[k]);
 246 }
 247 
 248 inline void SpecializationStats::record_do_oop_call_v(Kind k) {
 249   Atomic::inc(&_numDoOopCallsTotal[k]);
 250 }
 251 inline void SpecializationStats::record_do_oop_call_nv(Kind k) {
 252   Atomic::inc(&_numDoOopCallsTotal[k]);
 253   Atomic::inc(&_numDoOopCalls_nv[k]);
 254 }
 255 
 256 #else   // !ENABLE_SPECIALIZATION_STATS
 257 
 258 inline void SpecializationStats::record_call() {}
 259 inline void SpecializationStats::record_iterate_call_v(Kind k) {}
 260 inline void SpecializationStats::record_iterate_call_nv(Kind k) {}
 261 inline void SpecializationStats::record_do_oop_call_v(Kind k) {}
 262 inline void SpecializationStats::record_do_oop_call_nv(Kind k) {}
 263 inline void SpecializationStats::clear() {}
 264 inline void SpecializationStats::print() {}
 265 
 266 #endif  // ENABLE_SPECIALIZATION_STATS
 267 #endif  // !PRODUCT
 268 
 269 #endif // SHARE_VM_MEMORY_SPECIALIZED_OOP_CLOSURES_HPP