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 "utilities/macros.hpp"
  29 #include "utilities/templateIdioms.hpp"
  30 #if INCLUDE_ALL_GCS
  31 #include "gc_implementation/g1/g1_specialized_oop_closures.hpp"
  32 #endif // INCLUDE_ALL_GCS
  33 
  34 // The following OopClosure types get specialized versions of
  35 // "oop_oop_iterate" that invoke the closures' do_oop methods
  36 // non-virtually, using a mechanism defined in this file.  Extend these
  37 // macros in the obvious way to add specializations for new closures.
  38 
  39 // Forward declarations.
  40 class OopClosure;
  41 class OopsInGenClosure;
  42 // DefNew
  43 class ScanClosure;
  44 class FastScanClosure;
  45 class FilteringClosure;
  46 // ParNew
  47 class ParScanWithBarrierClosure;
  48 class ParScanWithoutBarrierClosure;
  49 // CMS
  50 class MarkRefsIntoAndScanClosure;
  51 class Par_MarkRefsIntoAndScanClosure;
  52 class PushAndMarkClosure;
  53 class Par_PushAndMarkClosure;
  54 class PushOrMarkClosure;
  55 class Par_PushOrMarkClosure;
  56 class CMSKeepAliveClosure;
  57 class CMSInnerParMarkAndPushClosure;
  58 // Misc
  59 class NoHeaderExtendedOopClosure;
  60 
  61 // This macro applies an argument macro to all OopClosures for which we
  62 // want specialized bodies of "oop_oop_iterate".  The arguments to "f" are:
  63 //   "f(closureType, non_virtual)"
  64 // where "closureType" is the name of the particular subclass of OopClosure,
  65 // and "non_virtual" will be the string "_nv" if the closure type should
  66 // have its "do_oop" method invoked non-virtually, or else the
  67 // string "_v".  ("OopClosure" itself will be the only class in the latter
  68 // category.)
  69 
  70 // This is split into several because of a Visual C++ 6.0 compiler bug
  71 // where very long macros cause the compiler to crash
  72 
  73 // These two macros are for explicitly declaring base classes for closures that
  74 // have no do_oop implementation and hence need virtual calls.
  75 // It should never be necessary to add more closure types except the base
  76 // classes here as auto specialization mechanism automatically
  77 // checks that the declared closure type also has its own declaration
  78 // of the specialized calls.
  79 #define UNSPECIALIZED_OOP_OOP_ITERATE_CLOSURES(f)       \
  80     f(ExtendedOopClosure)                               \
  81     f(OopClosure)
  82 
  83 #define UNSPECIALIZED_DO_METADATA_CLOSURES(f)           \
  84     f(ExtendedOopClosure)
  85 
  86 #define FORWARD_DECLARE_CLOSURE(OopClosureType)         \
  87 class OopClosureType;
  88 
  89 UNSPECIALIZED_OOP_OOP_ITERATE_CLOSURES(FORWARD_DECLARE_CLOSURE)
  90 UNSPECIALIZED_DO_METADATA_CLOSURES(FORWARD_DECLARE_CLOSURE)
  91 
  92 // Some other heap might define further specialized closures.
  93 #ifndef FURTHER_SPECIALIZED_OOP_OOP_ITERATE_CLOSURES
  94 #define FURTHER_SPECIALIZED_OOP_OOP_ITERATE_CLOSURES(f) \
  95         /* None */
  96 #endif
  97 
  98 #define SPECIALIZED_OOP_OOP_ITERATE_CLOSURES_S(f)       \
  99   f(ScanClosure,_nv)                                    \
 100   f(FastScanClosure,_nv)                                \
 101   f(FilteringClosure,_nv)
 102 
 103 #if INCLUDE_ALL_GCS
 104 #define SPECIALIZED_OOP_OOP_ITERATE_CLOSURES_P(f)       \
 105   f(ParScanWithBarrierClosure,_nv)                      \
 106   f(ParScanWithoutBarrierClosure,_nv)
 107 #else  // INCLUDE_ALL_GCS
 108 #define SPECIALIZED_OOP_OOP_ITERATE_CLOSURES_P(f)
 109 #endif // INCLUDE_ALL_GCS
 110 
 111 #define SPECIALIZED_OOP_OOP_ITERATE_CLOSURES_1(f)       \
 112   f(NoHeaderExtendedOopClosure,_nv)                     \
 113   SPECIALIZED_OOP_OOP_ITERATE_CLOSURES_S(f)             \
 114   SPECIALIZED_OOP_OOP_ITERATE_CLOSURES_P(f)
 115 
 116 #if INCLUDE_ALL_GCS
 117 #define SPECIALIZED_OOP_OOP_ITERATE_CLOSURES_2(f)       \
 118   f(MarkRefsIntoAndScanClosure,_nv)                     \
 119   f(Par_MarkRefsIntoAndScanClosure,_nv)                 \
 120   f(PushAndMarkClosure,_nv)                             \
 121   f(Par_PushAndMarkClosure,_nv)                         \
 122   f(PushOrMarkClosure,_nv)                              \
 123   f(Par_PushOrMarkClosure,_nv)                          \
 124   f(CMSKeepAliveClosure,_nv)                            \
 125   f(CMSInnerParMarkAndPushClosure,_nv)                  \
 126   FURTHER_SPECIALIZED_OOP_OOP_ITERATE_CLOSURES(f)
 127 #else  // INCLUDE_ALL_GCS
 128 #define SPECIALIZED_OOP_OOP_ITERATE_CLOSURES_2(f)
 129 #endif // INCLUDE_ALL_GCS
 130 
 131 
 132 // We separate these out, because sometime the general one has
 133 // a different definition from the specialized ones, and sometimes it
 134 // doesn't.
 135 
 136 #define ALL_OOP_OOP_ITERATE_CLOSURES_1(f)               \
 137   f(ExtendedOopClosure,_v)                              \
 138   SPECIALIZED_OOP_OOP_ITERATE_CLOSURES_1(f)
 139 
 140 #define ALL_OOP_OOP_ITERATE_CLOSURES_2(f)               \
 141   SPECIALIZED_OOP_OOP_ITERATE_CLOSURES_2(f)
 142 
 143 #if INCLUDE_ALL_GCS
 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 // "par_oop_iterate".  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 #define SPECIALIZED_PAR_OOP_ITERATE_CLOSURES(f)        \
 151   f(MarkRefsIntoAndScanClosure,_nv)                    \
 152   f(PushAndMarkClosure,_nv)                            \
 153   f(Par_MarkRefsIntoAndScanClosure,_nv)                \
 154   f(Par_PushAndMarkClosure,_nv)
 155 
 156 #define ALL_PAR_OOP_ITERATE_CLOSURES(f)                \
 157   f(ExtendedOopClosure,_v)                             \
 158   SPECIALIZED_PAR_OOP_ITERATE_CLOSURES(f)
 159 #endif // INCLUDE_ALL_GCS
 160 
 161 // This macro applies an argument macro to all OopClosures for which we
 162 // want specialized bodies of a family of methods related to
 163 // "oops_since_save_marks_do".  The arguments to f are the same as above.
 164 // The "root_class" is the most general class to define; this may be
 165 // "OopClosure" in some applications and "OopsInGenClosure" in others.
 166 
 167 
 168 // Some other heap might define further specialized closures.
 169 #ifndef FURTHER_SPECIALIZED_SINCE_SAVE_MARKS_CLOSURES
 170 #define FURTHER_SPECIALIZED_SINCE_SAVE_MARKS_CLOSURES(f) \
 171         /* None */
 172 #endif
 173 
 174 #define SPECIALIZED_SINCE_SAVE_MARKS_CLOSURES_YOUNG_S(f) \
 175   f(ScanClosure,_nv)                                     \
 176   f(FastScanClosure,_nv)
 177 
 178 #if INCLUDE_ALL_GCS
 179 #define SPECIALIZED_SINCE_SAVE_MARKS_CLOSURES_YOUNG_P(f) \
 180   f(ParScanWithBarrierClosure,_nv)                       \
 181   f(ParScanWithoutBarrierClosure,_nv)                    \
 182   FURTHER_SPECIALIZED_SINCE_SAVE_MARKS_CLOSURES(f)
 183 #else  // INCLUDE_ALL_GCS
 184 #define SPECIALIZED_SINCE_SAVE_MARKS_CLOSURES_YOUNG_P(f)
 185 #endif // INCLUDE_ALL_GCS
 186 
 187 #define SPECIALIZED_SINCE_SAVE_MARKS_CLOSURES_YOUNG(f)  \
 188   SPECIALIZED_SINCE_SAVE_MARKS_CLOSURES_YOUNG_S(f)      \
 189   SPECIALIZED_SINCE_SAVE_MARKS_CLOSURES_YOUNG_P(f)
 190 
 191 #define SPECIALIZED_SINCE_SAVE_MARKS_CLOSURES(f)        \
 192   SPECIALIZED_SINCE_SAVE_MARKS_CLOSURES_YOUNG(f)
 193 
 194 // We separate these out, because sometime the general one has
 195 // a different definition from the specialized ones, and sometimes it
 196 // doesn't.
 197 // NOTE:   One of the valid criticisms of this
 198 // specialize-oop_oop_iterate-for-specific-closures idiom is that it is
 199 // easy to have a silent performance bug: if you fail to de-virtualize,
 200 // things still work, just slower.  The "SpecializationStats" mode is
 201 // intended to at least make such a failure easy to detect.
 202 // *Not* using the ALL_SINCE_SAVE_MARKS_CLOSURES(f) macro defined
 203 // below means that *only* closures for which oop_oop_iterate specializations
 204 // exist above may be applied to "oops_since_save_marks".  That is,
 205 // this form of the performance bug is caught statically.  When you add
 206 // a definition for the general type, this property goes away.
 207 // Make sure you test with SpecializationStats to find such bugs
 208 // when introducing a new closure where you don't want virtual dispatch.
 209 
 210 #define ALL_SINCE_SAVE_MARKS_CLOSURES(f)                \
 211   f(OopsInGenClosure,_v)                                \
 212   SPECIALIZED_SINCE_SAVE_MARKS_CLOSURES(f)
 213 
 214 // For keeping stats on effectiveness.
 215 #define ENABLE_SPECIALIZATION_STATS 0
 216 
 217 typedef enum DispatchTag {
 218   _unknown_klass = 1,
 219   _instance_mirror_klass,
 220   _instance_class_loader_klass,
 221   _instance_ref_klass
 222 } DispatchTag;
 223 
 224 /**
 225  * The OopClosureDispatcher is a proxy class that automatically figures out
 226  * which OopClosure member function to call. It first checks for overridden
 227  * specializations using macros (only needed for OopClosure and
 228  * ExtendedOopClosure. These will result in virtual calls.
 229  * Otherwise if it's a subclass of these two, it will first try to call
 230  * the corresponding _nv member function for backward compatibility.
 231  * Otherwise, it will check for a a normal non nv declaration, in the derived
 232  * class (not the super class). If it exists, it will be called, otherwise
 233  * it resorts to a normal virtual call.
 234  */
 235 class OopClosureDispatcher : AllStatic {
 236   template<class OopClosureType, class OopType>
 237   static typename enable_if<!has_member_function_do_oop_nv<OopClosureType, void (OopClosureType::*)(OopType *obj)>::value, void>::type
 238   do_oop_internal_try_nv(OopClosureType *cl, OopType *obj);
 239 
 240   template<class OopClosureType, class OopType>
 241   static typename enable_if<has_member_function_do_oop_nv<OopClosureType, void (OopClosureType::*)(OopType *obj)>::value, void>::type
 242   do_oop_internal_try_nv(OopClosureType *cl, OopType *obj);
 243   
 244   template<class OopClosureType, class OopType>
 245   static typename enable_if<!has_member_function_do_oop<OopClosureType, void (OopClosureType::*)(OopType *obj)>::value, void>::type
 246   do_oop_internal_try_v(OopClosureType *cl, OopType *obj);
 247 
 248   template<class OopClosureType, class OopType>
 249   static typename enable_if<has_member_function_do_oop<OopClosureType, void (OopClosureType::*)(OopType *obj)>::value, void>::type
 250   do_oop_internal_try_v(OopClosureType *cl, OopType *obj);
 251 
 252   template<class OopClosureType>
 253   static typename enable_if<!has_member_function_do_metadata_nv<OopClosureType, bool (OopClosureType::*)()>::value, bool>::type
 254   do_metadata_internal_try_nv(OopClosureType *cl);
 255 
 256   template<class OopClosureType>
 257   static typename enable_if<has_member_function_do_metadata_nv<OopClosureType, bool (OopClosureType::*)()>::value, bool>::type
 258   do_metadata_internal_try_nv(OopClosureType *cl);
 259 
 260   template<class OopClosureType>
 261   static typename enable_if<!has_member_function_do_metadata<OopClosureType, bool (OopClosureType::*)()>::value, bool>::type
 262   do_metadata_internal_try_v(OopClosureType *cl);
 263 
 264   template<class OopClosureType>
 265   static typename enable_if<has_member_function_do_metadata<OopClosureType, bool (OopClosureType::*)()>::value, bool>::type
 266   do_metadata_internal_try_v(OopClosureType *cl);
 267 
 268   template<class OopClosureType>
 269   static typename enable_if<!has_member_function_do_klass_nv<OopClosureType, void (OopClosureType::*)(Klass*)>::value, void>::type
 270   do_klass_internal_try_nv(OopClosureType *cl, Klass *klass);
 271 
 272   template<class OopClosureType>
 273   static typename enable_if<has_member_function_do_klass_nv<OopClosureType, void (OopClosureType::*)(Klass*)>::value, void>::type
 274   do_klass_internal_try_nv(OopClosureType *cl, Klass *klass);
 275 
 276   template<class OopClosureType>
 277   static typename enable_if<!has_member_function_do_klass<OopClosureType, void (OopClosureType::*)(Klass*)>::value, void>::type
 278   do_klass_internal_try_v(OopClosureType *cl, Klass *klass);
 279 
 280   template<class OopClosureType>
 281   static typename enable_if<has_member_function_do_klass<OopClosureType, void (OopClosureType::*)(Klass*)>::value, void>::type
 282   do_klass_internal_try_v(OopClosureType *cl, Klass *klass);
 283   
 284   template<class OopClosureType>
 285   static typename enable_if<!has_member_function_do_class_loader_data_nv<OopClosureType, void (OopClosureType::*)(ClassLoaderData*)>::value, void>::type
 286   do_class_loader_data_internal_try_nv(OopClosureType *cl, ClassLoaderData *cld);
 287 
 288   template<class OopClosureType>
 289   static typename enable_if<has_member_function_do_class_loader_data_nv<OopClosureType, void (OopClosureType::*)(ClassLoaderData*)>::value, void>::type
 290   do_class_loader_data_internal_try_nv(OopClosureType *cl, ClassLoaderData *cld);
 291   
 292   template<class OopClosureType>
 293   static typename enable_if<!has_member_function_do_class_loader_data<OopClosureType, void (OopClosureType::*)(ClassLoaderData*)>::value, void>::type
 294   do_class_loader_data_internal_try_v(OopClosureType *cl, ClassLoaderData *cld);
 295 
 296   template<class OopClosureType>
 297   static typename enable_if<has_member_function_do_class_loader_data<OopClosureType, void (OopClosureType::*)(ClassLoaderData*)>::value, void>::type
 298   do_class_loader_data_internal_try_v(OopClosureType *cl, ClassLoaderData *cld);
 299 
 300   template<class OopClosureType, class OopType>
 301   static void do_oop_internal(OopClosureType *cl, OopType *obj);
 302 
 303   template<class OopClosureType>
 304   static bool do_metadata_internal(OopClosureType *cl);
 305 
 306   template<class OopClosureType>
 307   static void do_klass_internal(OopClosureType *cl, Klass *klass);
 308 
 309   template<class OopClosureType>
 310   static void do_class_loader_data_internal(OopClosureType *cl, ClassLoaderData *cld);
 311 
 312 public:
 313   // Make sure we only dispatch to OopClosure subtypes, otherwise compiler error
 314   template<class OopClosureType, class OopType>
 315   static typename enable_if<is_kind_of<OopClosure, OopClosureType>::value, void>::type 
 316   do_oop(OopClosureType *cl, OopType *obj);
 317 
 318   // Only do metadata stuff on ExtendedOopClosure, otherwise compiler error
 319   template<class OopClosureType>
 320   static typename enable_if<is_kind_of<ExtendedOopClosure, OopClosureType>::value, bool>::type
 321   do_metadata(OopClosureType *cl);
 322 
 323   template<class OopClosureType>
 324   static typename enable_if<is_kind_of<ExtendedOopClosure, OopClosureType>::value, void>::type
 325   do_klass(OopClosureType *cl, Klass *klass);
 326 
 327   template<class OopClosureType>
 328   static typename enable_if<is_kind_of<ExtendedOopClosure, OopClosureType>::value, void>::type
 329   do_class_loader_data(OopClosureType *cl, ClassLoaderData* cld);
 330 };
 331 
 332 class SpecializationStats {
 333 public:
 334   enum Kind {
 335     ik,             // InstanceKlass
 336     irk,            // InstanceRefKlass
 337     oa,             // ObjArrayKlass
 338     NUM_Kinds
 339   };
 340 
 341 #if ENABLE_SPECIALIZATION_STATS
 342 private:
 343   static bool _init;
 344   static bool _wrapped;
 345   static jint _numCallsAll;
 346 
 347   static jint _numCallsTotal[NUM_Kinds];
 348   static jint _numCalls_nv[NUM_Kinds];
 349 
 350   static jint _numDoOopCallsTotal[NUM_Kinds];
 351   static jint _numDoOopCalls_nv[NUM_Kinds];
 352 public:
 353 #endif
 354   static void clear()  PRODUCT_RETURN;
 355 
 356   static inline void record_call()  PRODUCT_RETURN;
 357   static inline void record_iterate_call_v(Kind k)  PRODUCT_RETURN;
 358   static inline void record_iterate_call_nv(Kind k)  PRODUCT_RETURN;
 359   static inline void record_do_oop_call_v(Kind k)  PRODUCT_RETURN;
 360   static inline void record_do_oop_call_nv(Kind k)  PRODUCT_RETURN;
 361 
 362   static void print() PRODUCT_RETURN;
 363 };
 364 
 365 #ifndef PRODUCT
 366 #if ENABLE_SPECIALIZATION_STATS
 367 
 368 inline void SpecializationStats::record_call() {
 369   Atomic::inc(&_numCallsAll);
 370 }
 371 inline void SpecializationStats::record_iterate_call_v(Kind k) {
 372   Atomic::inc(&_numCallsTotal[k]);
 373 }
 374 inline void SpecializationStats::record_iterate_call_nv(Kind k) {
 375   Atomic::inc(&_numCallsTotal[k]);
 376   Atomic::inc(&_numCalls_nv[k]);
 377 }
 378 
 379 inline void SpecializationStats::record_do_oop_call_v(Kind k) {
 380   Atomic::inc(&_numDoOopCallsTotal[k]);
 381 }
 382 inline void SpecializationStats::record_do_oop_call_nv(Kind k) {
 383   Atomic::inc(&_numDoOopCallsTotal[k]);
 384   Atomic::inc(&_numDoOopCalls_nv[k]);
 385 }
 386 
 387 #else   // !ENABLE_SPECIALIZATION_STATS
 388 
 389 inline void SpecializationStats::record_call() {}
 390 inline void SpecializationStats::record_iterate_call_v(Kind k) {}
 391 inline void SpecializationStats::record_iterate_call_nv(Kind k) {}
 392 inline void SpecializationStats::record_do_oop_call_v(Kind k) {}
 393 inline void SpecializationStats::record_do_oop_call_nv(Kind k) {}
 394 inline void SpecializationStats::clear() {}
 395 inline void SpecializationStats::print() {}
 396 
 397 #endif  // ENABLE_SPECIALIZATION_STATS
 398 #endif  // !PRODUCT
 399 
 400 #endif // SHARE_VM_MEMORY_SPECIALIZED_OOP_CLOSURES_HPP