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