< prev index next >

src/hotspot/share/memory/metaspaceClosure.hpp

Print this page
   1 /*
   2  * Copyright (c) 2017, 2019, 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  *


  91   //
  92   // Calling these methods would be trivial if these two were virtual methods.
  93   // However, to save space, MetaspaceObj has NO vtable. The vtable is introduced
  94   // only in the Metadata class.
  95   //
  96   // To work around the lack of a vtable, we use Ref class with templates
  97   // (see ObjectRef, PrimitiveArrayRef and PointerArrayRef)
  98   // so that we can statically discover the type of a object. The use of Ref
  99   // depends on the fact that:
 100   //
 101   // [1] We don't use polymorphic pointers for MetaspaceObj's that are not subclasses
 102   //     of Metadata. I.e., we don't do this:
 103   //     class Klass {
 104   //         MetaspaceObj *_obj;
 105   //         Array<int>* foo() { return (Array<int>*)_obj; }
 106   //         Symbol*     bar() { return (Symbol*)    _obj; }
 107   //
 108   // [2] All Array<T> dimensions are statically declared.
 109   class Ref : public CHeapObj<mtInternal> {
 110     Writability _writability;

 111     Ref* _next;

 112     NONCOPYABLE(Ref);
 113 
 114   protected:
 115     virtual void** mpp() const = 0;
 116     Ref(Writability w) : _writability(w), _next(NULL) {}
 117   public:
 118     virtual bool not_null() const = 0;
 119     virtual int size() const = 0;
 120     virtual void metaspace_pointers_do(MetaspaceClosure *it) const = 0;
 121     virtual void metaspace_pointers_do_at(MetaspaceClosure *it, address new_loc) const = 0;
 122     virtual MetaspaceObj::Type msotype() const = 0;
 123     virtual bool is_read_only_by_default() const = 0;
 124     virtual ~Ref() {}
 125 
 126     address obj() const {
 127       // In some rare cases (see CPSlot in constantPool.hpp) we store some flags in the lowest
 128       // 2 bits of a MetaspaceObj pointer. Unmask these when manipulating the pointer.
 129       uintx p = (uintx)*mpp();
 130       return (address)(p & (~FLAG_MASK));
 131     }
 132 
 133     address* addr() const {
 134       return (address*)mpp();
 135     }
 136 
 137     void update(address new_loc) const;
 138 
 139     Writability writability() const { return _writability; };




 140     void set_next(Ref* n)           { _next = n; }
 141     Ref* next() const               { return _next; }
 142 
 143   private:
 144     static const uintx FLAG_MASK = 0x03;
 145 
 146     int flag_bits() const {
 147       uintx p = (uintx)*mpp();
 148       return (int)(p & FLAG_MASK);
 149     }
 150   };
 151 
 152 private:
 153   // -------------------------------------------------- ObjectRef
 154   template <class T> class ObjectRef : public Ref {
 155     T** _mpp;
 156     T* dereference() const {
 157       return *_mpp;
 158     }
 159   protected:


 226     virtual bool not_null()                const { return dereference() != NULL; }
 227     virtual int size()                     const { return dereference()->size(); }
 228     virtual MetaspaceObj::Type msotype()   const { return MetaspaceObj::array_type(sizeof(T*)); }
 229 
 230     virtual void metaspace_pointers_do(MetaspaceClosure *it) const {
 231       metaspace_pointers_do_at_impl(it, dereference());
 232     }
 233     virtual void metaspace_pointers_do_at(MetaspaceClosure *it, address new_loc) const {
 234       metaspace_pointers_do_at_impl(it, (Array<T*>*)new_loc);
 235     }
 236   private:
 237     void metaspace_pointers_do_at_impl(MetaspaceClosure *it, Array<T*>* array) const {
 238       log_trace(cds)("Iter(ObjectArray): %p [%d]", array, array->length());
 239       for (int i = 0; i < array->length(); i++) {
 240         T** mpp = array->adr_at(i);
 241         it->push(mpp);
 242       }
 243     }
 244   };
 245 
 246   // If recursion is too deep, save the Refs in _pending_refs, and push them later using
 247   // MetaspaceClosure::finish()

 248   static const int MAX_NEST_LEVEL = 5;
 249   Ref* _pending_refs;
 250   int _nest_level;

 251 
 252   void push_impl(Ref* ref);
 253   void do_push(Ref* ref);
 254 
 255 public:
 256   MetaspaceClosure(): _pending_refs(NULL), _nest_level(0) {}
 257   ~MetaspaceClosure();
 258 
 259   void finish();
 260 




















 261   // returns true if we want to keep iterating the pointers embedded inside <ref>
 262   virtual bool do_ref(Ref* ref, bool read_only) = 0;
 263 
 264   // When you do:
 265   //     void MyType::metaspace_pointers_do(MetaspaceClosure* it) {
 266   //       it->push(_my_field)
 267   //     }
 268   //
 269   // C++ will try to match the "most specific" template function. This one will
 270   // will be matched if possible (if mpp is an Array<> of any pointer type).
 271   template <typename T> void push(Array<T*>** mpp, Writability w = _default) {
 272     push_impl(new PointerArrayRef<T>(mpp, w));
 273   }
 274 
 275   // If the above function doesn't match (mpp is an Array<>, but T is not a pointer type), then
 276   // this is the second choice.
 277   template <typename T> void push(Array<T>** mpp, Writability w = _default) {
 278     push_impl(new PrimitiveArrayRef<T>(mpp, w));
 279   }
 280 
 281   // If the above function doesn't match (mpp is not an Array<> type), then
 282   // this will be matched by default.
 283   template <class T> void push(T** mpp, Writability w = _default) {
 284     push_impl(new ObjectRef<T>(mpp, w));
 285   }
 286 
 287   template <class T> void push_method_entry(T** mpp, intptr_t* p) {
 288     push_special(_method_entry_ref, new ObjectRef<T>(mpp, _default), (intptr_t*)p);




 289   }
 290 
 291   // This is for tagging special pointers that are not a reference to MetaspaceObj. It's currently
 292   // used to mark the method entry points in Method/ConstMethod.
 293   virtual void push_special(SpecialRef type, Ref* obj, intptr_t* p) {
 294     assert(type == _method_entry_ref, "only special type allowed for now");
 295   }
 296 };
 297 
 298 // This is a special MetaspaceClosure that visits each unique MetaspaceObj once.
 299 class UniqueMetaspaceClosure : public MetaspaceClosure {
 300   static const int INITIAL_TABLE_SIZE = 15889;
 301   static const int MAX_TABLE_SIZE     = 1000000;
 302 
 303   // Do not override. Returns true if we are discovering ref->obj() for the first time.
 304   virtual bool do_ref(Ref* ref, bool read_only);
 305 
 306 public:
 307   // Gets called the first time we discover an object.
 308   virtual bool do_unique_ref(Ref* ref, bool read_only) = 0;
   1 /*
   2  * Copyright (c) 2017, 2020, 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  *


  91   //
  92   // Calling these methods would be trivial if these two were virtual methods.
  93   // However, to save space, MetaspaceObj has NO vtable. The vtable is introduced
  94   // only in the Metadata class.
  95   //
  96   // To work around the lack of a vtable, we use Ref class with templates
  97   // (see ObjectRef, PrimitiveArrayRef and PointerArrayRef)
  98   // so that we can statically discover the type of a object. The use of Ref
  99   // depends on the fact that:
 100   //
 101   // [1] We don't use polymorphic pointers for MetaspaceObj's that are not subclasses
 102   //     of Metadata. I.e., we don't do this:
 103   //     class Klass {
 104   //         MetaspaceObj *_obj;
 105   //         Array<int>* foo() { return (Array<int>*)_obj; }
 106   //         Symbol*     bar() { return (Symbol*)    _obj; }
 107   //
 108   // [2] All Array<T> dimensions are statically declared.
 109   class Ref : public CHeapObj<mtInternal> {
 110     Writability _writability;
 111     bool _keep_after_pushing;
 112     Ref* _next;
 113     void* _user_data;
 114     NONCOPYABLE(Ref);
 115 
 116   protected:
 117     virtual void** mpp() const = 0;
 118     Ref(Writability w) : _writability(w), _keep_after_pushing(false), _next(NULL), _user_data(NULL) {}
 119   public:
 120     virtual bool not_null() const = 0;
 121     virtual int size() const = 0;
 122     virtual void metaspace_pointers_do(MetaspaceClosure *it) const = 0;
 123     virtual void metaspace_pointers_do_at(MetaspaceClosure *it, address new_loc) const = 0;
 124     virtual MetaspaceObj::Type msotype() const = 0;
 125     virtual bool is_read_only_by_default() const = 0;
 126     virtual ~Ref() {}
 127 
 128     address obj() const {
 129       // In some rare cases (see CPSlot in constantPool.hpp) we store some flags in the lowest
 130       // 2 bits of a MetaspaceObj pointer. Unmask these when manipulating the pointer.
 131       uintx p = (uintx)*mpp();
 132       return (address)(p & (~FLAG_MASK));
 133     }
 134 
 135     address* addr() const {
 136       return (address*)mpp();
 137     }
 138 
 139     void update(address new_loc) const;
 140 
 141     Writability writability() const { return _writability; };
 142     void set_keep_after_pushing()   { _keep_after_pushing = true; }
 143     bool keep_after_pushing()       { return _keep_after_pushing; }
 144     void set_user_data(void* data)  { _user_data = data; }
 145     void* user_data()               { return _user_data; }
 146     void set_next(Ref* n)           { _next = n; }
 147     Ref* next() const               { return _next; }
 148 
 149   private:
 150     static const uintx FLAG_MASK = 0x03;
 151 
 152     int flag_bits() const {
 153       uintx p = (uintx)*mpp();
 154       return (int)(p & FLAG_MASK);
 155     }
 156   };
 157 
 158 private:
 159   // -------------------------------------------------- ObjectRef
 160   template <class T> class ObjectRef : public Ref {
 161     T** _mpp;
 162     T* dereference() const {
 163       return *_mpp;
 164     }
 165   protected:


 232     virtual bool not_null()                const { return dereference() != NULL; }
 233     virtual int size()                     const { return dereference()->size(); }
 234     virtual MetaspaceObj::Type msotype()   const { return MetaspaceObj::array_type(sizeof(T*)); }
 235 
 236     virtual void metaspace_pointers_do(MetaspaceClosure *it) const {
 237       metaspace_pointers_do_at_impl(it, dereference());
 238     }
 239     virtual void metaspace_pointers_do_at(MetaspaceClosure *it, address new_loc) const {
 240       metaspace_pointers_do_at_impl(it, (Array<T*>*)new_loc);
 241     }
 242   private:
 243     void metaspace_pointers_do_at_impl(MetaspaceClosure *it, Array<T*>* array) const {
 244       log_trace(cds)("Iter(ObjectArray): %p [%d]", array, array->length());
 245       for (int i = 0; i < array->length(); i++) {
 246         T** mpp = array->adr_at(i);
 247         it->push(mpp);
 248       }
 249     }
 250   };
 251 
 252   // Normally, chains of references like a->b->c->d are iterated recursively. However,
 253   // if recursion is too deep, we save the Refs in _pending_refs, and push them later in
 254   // MetaspaceClosure::finish(). This avoids overflowing the C stack.
 255   static const int MAX_NEST_LEVEL = 5;
 256   Ref* _pending_refs;
 257   int _nest_level;
 258   Ref* _enclosing_ref;
 259 
 260   void push_impl(Ref* ref);
 261   void do_push(Ref* ref);
 262 
 263 public:
 264   MetaspaceClosure(): _pending_refs(NULL), _nest_level(0), _enclosing_ref(NULL) {}
 265   ~MetaspaceClosure();
 266 
 267   void finish();
 268 
 269   // enclosing_ref() is used to compute the offset of a field in a C++ class. For example
 270   // class Foo { intx scala; Bar* ptr; }
 271   //    Foo *f = 0x100;
 272   // when the f->ptr field is iterated with do_ref() on 64-bit platforms, we will have
 273   //    do_ref(Ref* r) {
 274   //       r->addr() == 0x108;                // == &f->ptr;
 275   //       enclosing_ref()->obj() == 0x100;   // == foo
 276   // So we know that we are iterating upon a field at offset 8 of the object at 0x100.
 277   //
 278   // Note that if we have stack overflow, do_pending_ref(r) will be called first and
 279   // do_ref(r) will be called later, for the same r. In this case, enclosing_ref() is valid only
 280   // when do_pending_ref(r) is called, and will return NULL when do_ref(r) is called.
 281   Ref* enclosing_ref() const {
 282     return _enclosing_ref;
 283   }
 284 
 285   // This is called when a reference is placed in _pending_refs. Override this
 286   // function if you're using enclosing_ref(). See notes above.
 287   virtual void do_pending_ref(Ref* ref) {}
 288 
 289   // returns true if we want to keep iterating the pointers embedded inside <ref>
 290   virtual bool do_ref(Ref* ref, bool read_only) = 0;
 291 
 292   // When you do:
 293   //     void MyType::metaspace_pointers_do(MetaspaceClosure* it) {
 294   //       it->push(_my_field)
 295   //     }
 296   //
 297   // C++ will try to match the "most specific" template function. This one will
 298   // will be matched if possible (if mpp is an Array<> of any pointer type).
 299   template <typename T> void push(Array<T*>** mpp, Writability w = _default) {
 300     push_impl(new PointerArrayRef<T>(mpp, w));
 301   }
 302 
 303   // If the above function doesn't match (mpp is an Array<>, but T is not a pointer type), then
 304   // this is the second choice.
 305   template <typename T> void push(Array<T>** mpp, Writability w = _default) {
 306     push_impl(new PrimitiveArrayRef<T>(mpp, w));
 307   }
 308 
 309   // If the above function doesn't match (mpp is not an Array<> type), then
 310   // this will be matched by default.
 311   template <class T> void push(T** mpp, Writability w = _default) {
 312     push_impl(new ObjectRef<T>(mpp, w));
 313   }
 314 
 315   template <class T> void push_method_entry(T** mpp, intptr_t* p) {
 316     Ref* ref = new ObjectRef<T>(mpp, _default);
 317     push_special(_method_entry_ref, ref, (intptr_t*)p);
 318     if (!ref->keep_after_pushing()) {
 319       delete ref;
 320     }
 321   }
 322 
 323   // This is for tagging special pointers that are not a reference to MetaspaceObj. It's currently
 324   // used to mark the method entry points in Method/ConstMethod.
 325   virtual void push_special(SpecialRef type, Ref* obj, intptr_t* p) {
 326     assert(type == _method_entry_ref, "only special type allowed for now");
 327   }
 328 };
 329 
 330 // This is a special MetaspaceClosure that visits each unique MetaspaceObj once.
 331 class UniqueMetaspaceClosure : public MetaspaceClosure {
 332   static const int INITIAL_TABLE_SIZE = 15889;
 333   static const int MAX_TABLE_SIZE     = 1000000;
 334 
 335   // Do not override. Returns true if we are discovering ref->obj() for the first time.
 336   virtual bool do_ref(Ref* ref, bool read_only);
 337 
 338 public:
 339   // Gets called the first time we discover an object.
 340   virtual bool do_unique_ref(Ref* ref, bool read_only) = 0;
< prev index next >