< prev index next >

src/share/vm/memory/specialized_oop_closures.hpp

Print this page
rev 7183 : autospecialized oop_iterate using SFINAE and templates
rev 7184 : cross platform compiler friendly enum declaration


   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 #if INCLUDE_ALL_GCS
  30 #include "gc_implementation/g1/g1_specialized_oop_closures.hpp"
  31 #endif // INCLUDE_ALL_GCS
  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


  52 class Par_PushAndMarkClosure;
  53 class PushOrMarkClosure;
  54 class Par_PushOrMarkClosure;
  55 class CMSKeepAliveClosure;
  56 class CMSInnerParMarkAndPushClosure;
  57 // Misc
  58 class NoHeaderExtendedOopClosure;
  59 
  60 // This macro applies an argument macro to all OopClosures for which we
  61 // want specialized bodies of "oop_oop_iterate".  The arguments to "f" are:
  62 //   "f(closureType, non_virtual)"
  63 // where "closureType" is the name of the particular subclass of OopClosure,
  64 // and "non_virtual" will be the string "_nv" if the closure type should
  65 // have its "do_oop" method invoked non-virtually, or else the
  66 // string "_v".  ("OopClosure" itself will be the only class in the latter
  67 // category.)
  68 
  69 // This is split into several because of a Visual C++ 6.0 compiler bug
  70 // where very long macros cause the compiler to crash
  71 



















  72 // Some other heap might define further specialized closures.
  73 #ifndef FURTHER_SPECIALIZED_OOP_OOP_ITERATE_CLOSURES
  74 #define FURTHER_SPECIALIZED_OOP_OOP_ITERATE_CLOSURES(f) \
  75         /* None */
  76 #endif
  77 
  78 #define SPECIALIZED_OOP_OOP_ITERATE_CLOSURES_S(f)       \
  79   f(ScanClosure,_nv)                                    \
  80   f(FastScanClosure,_nv)                                \
  81   f(FilteringClosure,_nv)
  82 
  83 #if INCLUDE_ALL_GCS
  84 #define SPECIALIZED_OOP_OOP_ITERATE_CLOSURES_P(f)       \
  85   f(ParScanWithBarrierClosure,_nv)                      \
  86   f(ParScanWithoutBarrierClosure,_nv)
  87 #else  // INCLUDE_ALL_GCS
  88 #define SPECIALIZED_OOP_OOP_ITERATE_CLOSURES_P(f)
  89 #endif // INCLUDE_ALL_GCS
  90 
  91 #define SPECIALIZED_OOP_OOP_ITERATE_CLOSURES_1(f)       \


 177 // NOTE:   One of the valid criticisms of this
 178 // specialize-oop_oop_iterate-for-specific-closures idiom is that it is
 179 // easy to have a silent performance bug: if you fail to de-virtualize,
 180 // things still work, just slower.  The "SpecializationStats" mode is
 181 // intended to at least make such a failure easy to detect.
 182 // *Not* using the ALL_SINCE_SAVE_MARKS_CLOSURES(f) macro defined
 183 // below means that *only* closures for which oop_oop_iterate specializations
 184 // exist above may be applied to "oops_since_save_marks".  That is,
 185 // this form of the performance bug is caught statically.  When you add
 186 // a definition for the general type, this property goes away.
 187 // Make sure you test with SpecializationStats to find such bugs
 188 // when introducing a new closure where you don't want virtual dispatch.
 189 
 190 #define ALL_SINCE_SAVE_MARKS_CLOSURES(f)                \
 191   f(OopsInGenClosure,_v)                                \
 192   SPECIALIZED_SINCE_SAVE_MARKS_CLOSURES(f)
 193 
 194 // For keeping stats on effectiveness.
 195 #define ENABLE_SPECIALIZATION_STATS 0
 196 


















































































































 197 
 198 class SpecializationStats {
 199 public:
 200   enum Kind {
 201     ik,             // InstanceKlass
 202     irk,            // InstanceRefKlass
 203     oa,             // ObjArrayKlass
 204     NUM_Kinds
 205   };
 206 
 207 #if ENABLE_SPECIALIZATION_STATS
 208 private:
 209   static bool _init;
 210   static bool _wrapped;
 211   static jint _numCallsAll;
 212 
 213   static jint _numCallsTotal[NUM_Kinds];
 214   static jint _numCalls_nv[NUM_Kinds];
 215 
 216   static jint _numDoOopCallsTotal[NUM_Kinds];




   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


  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)       \


 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];


< prev index next >