src/hotspot/share/memory/metaspace.hpp
changeset 49980 57dd7b4ba338
parent 49449 ef5d5d343e2a
child 50119 bf9177eac58d
equal deleted inserted replaced
49979:b70281f5146e 49980:57dd7b4ba338
    66 class PrintCLDMetaspaceInfoClosure;
    66 class PrintCLDMetaspaceInfoClosure;
    67 class SpaceManager;
    67 class SpaceManager;
    68 class VirtualSpaceList;
    68 class VirtualSpaceList;
    69 class CollectedHeap;
    69 class CollectedHeap;
    70 
    70 
       
    71 namespace metaspace {
       
    72 namespace internals {
       
    73   class ClassLoaderMetaspaceStatistics;
       
    74 }}
       
    75 
    71 // Metaspaces each have a  SpaceManager and allocations
    76 // Metaspaces each have a  SpaceManager and allocations
    72 // are done by the SpaceManager.  Allocations are done
    77 // are done by the SpaceManager.  Allocations are done
    73 // out of the current Metachunk.  When the current Metachunk
    78 // out of the current Metachunk.  When the current Metachunk
    74 // is exhausted, the SpaceManager gets a new one from
    79 // is exhausted, the SpaceManager gets a new one from
    75 // the current VirtualSpace.  When the VirtualSpace is exhausted
    80 // the current VirtualSpace.  When the VirtualSpace is exhausted
    92     ClassType,
    97     ClassType,
    93     NonClassType,
    98     NonClassType,
    94     MetadataTypeCount
    99     MetadataTypeCount
    95   };
   100   };
    96   enum MetaspaceType {
   101   enum MetaspaceType {
    97     StandardMetaspaceType,
   102     ZeroMetaspaceType = 0,
    98     BootMetaspaceType,
   103     StandardMetaspaceType = ZeroMetaspaceType,
    99     AnonymousMetaspaceType,
   104     BootMetaspaceType = StandardMetaspaceType + 1,
   100     ReflectionMetaspaceType
   105     AnonymousMetaspaceType = BootMetaspaceType + 1,
       
   106     ReflectionMetaspaceType = AnonymousMetaspaceType + 1,
       
   107     MetaspaceTypeCount
   101   };
   108   };
   102 
   109 
   103  private:
   110  private:
   104 
   111 
   105   // Align up the word size to the allocation word size
   112   // Align up the word size to the allocation word size
   191   static size_t commit_alignment()        { return _commit_alignment; }
   198   static size_t commit_alignment()        { return _commit_alignment; }
   192   static size_t commit_alignment_words()  { return _commit_alignment / BytesPerWord; }
   199   static size_t commit_alignment_words()  { return _commit_alignment / BytesPerWord; }
   193 
   200 
   194   static MetaWord* allocate(ClassLoaderData* loader_data, size_t word_size,
   201   static MetaWord* allocate(ClassLoaderData* loader_data, size_t word_size,
   195                             MetaspaceObj::Type type, TRAPS);
   202                             MetaspaceObj::Type type, TRAPS);
   196   void deallocate(MetaWord* ptr, size_t byte_size, bool is_class);
       
   197 
   203 
   198   static bool contains(const void* ptr);
   204   static bool contains(const void* ptr);
   199   static bool contains_non_shared(const void* ptr);
   205   static bool contains_non_shared(const void* ptr);
   200 
   206 
   201   // Free empty virtualspaces
   207   // Free empty virtualspaces
   236   // special cases such as the boot class loader, reflection
   242   // special cases such as the boot class loader, reflection
   237   // class loader and anonymous class loader.
   243   // class loader and anonymous class loader.
   238   void initialize_first_chunk(Metaspace::MetaspaceType type, Metaspace::MetadataType mdtype);
   244   void initialize_first_chunk(Metaspace::MetaspaceType type, Metaspace::MetadataType mdtype);
   239   Metachunk* get_initialization_chunk(Metaspace::MetaspaceType type, Metaspace::MetadataType mdtype);
   245   Metachunk* get_initialization_chunk(Metaspace::MetaspaceType type, Metaspace::MetadataType mdtype);
   240 
   246 
       
   247   const Metaspace::MetaspaceType _space_type;
       
   248   Mutex* const  _lock;
   241   SpaceManager* _vsm;
   249   SpaceManager* _vsm;
       
   250   SpaceManager* _class_vsm;
       
   251 
   242   SpaceManager* vsm() const { return _vsm; }
   252   SpaceManager* vsm() const { return _vsm; }
   243 
       
   244   SpaceManager* _class_vsm;
       
   245   SpaceManager* class_vsm() const { return _class_vsm; }
   253   SpaceManager* class_vsm() const { return _class_vsm; }
   246   SpaceManager* get_space_manager(Metaspace::MetadataType mdtype) {
   254   SpaceManager* get_space_manager(Metaspace::MetadataType mdtype) {
   247     assert(mdtype != Metaspace::MetadataTypeCount, "MetadaTypeCount can't be used as mdtype");
   255     assert(mdtype != Metaspace::MetadataTypeCount, "MetadaTypeCount can't be used as mdtype");
   248     return mdtype == Metaspace::ClassType ? class_vsm() : vsm();
   256     return mdtype == Metaspace::ClassType ? class_vsm() : vsm();
   249   }
   257   }
   250 
   258 
       
   259   Mutex* lock() const { return _lock; }
       
   260 
   251   MetaWord* expand_and_allocate(size_t size, Metaspace::MetadataType mdtype);
   261   MetaWord* expand_and_allocate(size_t size, Metaspace::MetadataType mdtype);
   252 
   262 
   253   size_t class_chunk_size(size_t word_size);
   263   size_t class_chunk_size(size_t word_size);
       
   264 
       
   265   // Adds to the given statistic object. Must be locked with CLD metaspace lock.
       
   266   void add_to_statistics_locked(metaspace::internals::ClassLoaderMetaspaceStatistics* out) const;
   254 
   267 
   255  public:
   268  public:
   256 
   269 
   257   ClassLoaderMetaspace(Mutex* lock, Metaspace::MetaspaceType type);
   270   ClassLoaderMetaspace(Mutex* lock, Metaspace::MetaspaceType type);
   258   ~ClassLoaderMetaspace();
   271   ~ClassLoaderMetaspace();
       
   272 
       
   273   Metaspace::MetaspaceType space_type() const { return _space_type; }
   259 
   274 
   260   // Allocate space for metadata of type mdtype. This is space
   275   // Allocate space for metadata of type mdtype. This is space
   261   // within a Metachunk and is used by
   276   // within a Metachunk and is used by
   262   //   allocate(ClassLoaderData*, size_t, bool, MetadataType, TRAPS)
   277   //   allocate(ClassLoaderData*, size_t, bool, MetadataType, TRAPS)
   263   MetaWord* allocate(size_t word_size, Metaspace::MetadataType mdtype);
   278   MetaWord* allocate(size_t word_size, Metaspace::MetadataType mdtype);
   264 
   279 
   265   size_t used_words_slow(Metaspace::MetadataType mdtype) const;
       
   266   size_t free_words_slow(Metaspace::MetadataType mdtype) const;
       
   267   size_t capacity_words_slow(Metaspace::MetadataType mdtype) const;
       
   268 
       
   269   size_t used_bytes_slow(Metaspace::MetadataType mdtype) const;
       
   270   size_t capacity_bytes_slow(Metaspace::MetadataType mdtype) const;
       
   271 
       
   272   size_t allocated_blocks_bytes() const;
   280   size_t allocated_blocks_bytes() const;
   273   size_t allocated_chunks_bytes() const;
   281   size_t allocated_chunks_bytes() const;
   274 
   282 
   275   void deallocate(MetaWord* ptr, size_t byte_size, bool is_class);
   283   void deallocate(MetaWord* ptr, size_t byte_size, bool is_class);
   276 
       
   277   void dump(outputStream* const out) const;
       
   278 
   284 
   279   void print_on(outputStream* st) const;
   285   void print_on(outputStream* st) const;
   280   // Debugging support
   286   // Debugging support
   281   void verify();
   287   void verify();
   282 
   288 
       
   289   // Adds to the given statistic object. Will lock with CLD metaspace lock.
       
   290   void add_to_statistics(metaspace::internals::ClassLoaderMetaspaceStatistics* out) const;
       
   291 
   283 }; // ClassLoaderMetaspace
   292 }; // ClassLoaderMetaspace
   284 
   293 
   285 
       
   286 class MetaspaceUtils : AllStatic {
   294 class MetaspaceUtils : AllStatic {
       
   295 
       
   296   // Spacemanager updates running counters.
       
   297   friend class SpaceManager;
       
   298 
       
   299   // Running counters for statistics concerning in-use chunks.
       
   300   // Note: capacity = used + free + waste + overhead. Note that we do not
       
   301   // count free and waste. Their sum can be deduces from the three other values.
       
   302   // For more details, one should call print_report() from within a safe point.
       
   303   static size_t _capacity_words [Metaspace:: MetadataTypeCount];
       
   304   static size_t _overhead_words [Metaspace:: MetadataTypeCount];
       
   305   static volatile size_t _used_words [Metaspace:: MetadataTypeCount];
       
   306 
       
   307   // Atomically decrement or increment in-use statistic counters
       
   308   static void dec_capacity(Metaspace::MetadataType mdtype, size_t words);
       
   309   static void inc_capacity(Metaspace::MetadataType mdtype, size_t words);
       
   310   static void dec_used(Metaspace::MetadataType mdtype, size_t words);
       
   311   static void inc_used(Metaspace::MetadataType mdtype, size_t words);
       
   312   static void dec_overhead(Metaspace::MetadataType mdtype, size_t words);
       
   313   static void inc_overhead(Metaspace::MetadataType mdtype, size_t words);
       
   314 
       
   315 
       
   316   // Getters for the in-use counters.
       
   317   static size_t capacity_words(Metaspace::MetadataType mdtype)        { return _capacity_words[mdtype]; }
       
   318   static size_t used_words(Metaspace::MetadataType mdtype)            { return _used_words[mdtype]; }
       
   319   static size_t overhead_words(Metaspace::MetadataType mdtype)        { return _overhead_words[mdtype]; }
       
   320 
   287   static size_t free_chunks_total_words(Metaspace::MetadataType mdtype);
   321   static size_t free_chunks_total_words(Metaspace::MetadataType mdtype);
   288 
   322 
   289   // These methods iterate over the classloader data graph
   323   // Helper for print_xx_report.
   290   // for the given Metaspace type.  These are slow.
   324   static void print_vs(outputStream* out, size_t scale);
   291   static size_t used_bytes_slow(Metaspace::MetadataType mdtype);
   325 
   292   static size_t free_bytes_slow(Metaspace::MetadataType mdtype);
   326 public:
   293   static size_t capacity_bytes_slow(Metaspace::MetadataType mdtype);
   327 
   294   static size_t capacity_bytes_slow();
   328   // Collect used metaspace statistics. This involves walking the CLDG. The resulting
   295 
   329   // output will be the accumulated values for all live metaspaces.
   296   // Running sum of space in all Metachunks that has been
   330   // Note: method does not do any locking.
   297   // allocated to a Metaspace.  This is used instead of
   331   static void collect_statistics(metaspace::internals::ClassLoaderMetaspaceStatistics* out);
   298   // iterating over all the classloaders. One for each
       
   299   // type of Metadata
       
   300   static size_t _capacity_words[Metaspace:: MetadataTypeCount];
       
   301   // Running sum of space in all Metachunks that
       
   302   // are being used for metadata. One for each
       
   303   // type of Metadata.
       
   304   static volatile size_t _used_words[Metaspace:: MetadataTypeCount];
       
   305 
       
   306  public:
       
   307   // Decrement and increment _allocated_capacity_words
       
   308   static void dec_capacity(Metaspace::MetadataType type, size_t words);
       
   309   static void inc_capacity(Metaspace::MetadataType type, size_t words);
       
   310 
       
   311   // Decrement and increment _allocated_used_words
       
   312   static void dec_used(Metaspace::MetadataType type, size_t words);
       
   313   static void inc_used(Metaspace::MetadataType type, size_t words);
       
   314 
       
   315   // Total of space allocated to metadata in all Metaspaces.
       
   316   // This sums the space used in each Metachunk by
       
   317   // iterating over the classloader data graph
       
   318   static size_t used_bytes_slow() {
       
   319     return used_bytes_slow(Metaspace::ClassType) +
       
   320            used_bytes_slow(Metaspace::NonClassType);
       
   321   }
       
   322 
   332 
   323   // Used by MetaspaceCounters
   333   // Used by MetaspaceCounters
   324   static size_t free_chunks_total_words();
   334   static size_t free_chunks_total_words();
   325   static size_t free_chunks_total_bytes();
   335   static size_t free_chunks_total_bytes();
   326   static size_t free_chunks_total_bytes(Metaspace::MetadataType mdtype);
   336   static size_t free_chunks_total_bytes(Metaspace::MetadataType mdtype);
   327 
   337 
   328   static size_t capacity_words(Metaspace::MetadataType mdtype) {
       
   329     return _capacity_words[mdtype];
       
   330   }
       
   331   static size_t capacity_words() {
   338   static size_t capacity_words() {
   332     return capacity_words(Metaspace::NonClassType) +
   339     return capacity_words(Metaspace::NonClassType) +
   333            capacity_words(Metaspace::ClassType);
   340            capacity_words(Metaspace::ClassType);
   334   }
   341   }
   335   static size_t capacity_bytes(Metaspace::MetadataType mdtype) {
   342   static size_t capacity_bytes(Metaspace::MetadataType mdtype) {
   337   }
   344   }
   338   static size_t capacity_bytes() {
   345   static size_t capacity_bytes() {
   339     return capacity_words() * BytesPerWord;
   346     return capacity_words() * BytesPerWord;
   340   }
   347   }
   341 
   348 
   342   static size_t used_words(Metaspace::MetadataType mdtype) {
       
   343     return _used_words[mdtype];
       
   344   }
       
   345   static size_t used_words() {
   349   static size_t used_words() {
   346     return used_words(Metaspace::NonClassType) +
   350     return used_words(Metaspace::NonClassType) +
   347            used_words(Metaspace::ClassType);
   351            used_words(Metaspace::ClassType);
   348   }
   352   }
   349   static size_t used_bytes(Metaspace::MetadataType mdtype) {
   353   static size_t used_bytes(Metaspace::MetadataType mdtype) {
   351   }
   355   }
   352   static size_t used_bytes() {
   356   static size_t used_bytes() {
   353     return used_words() * BytesPerWord;
   357     return used_words() * BytesPerWord;
   354   }
   358   }
   355 
   359 
   356   static size_t free_bytes();
   360   // Space committed but yet unclaimed by any class loader.
   357   static size_t free_bytes(Metaspace::MetadataType mdtype);
   361   static size_t free_in_vs_bytes();
       
   362   static size_t free_in_vs_bytes(Metaspace::MetadataType mdtype);
   358 
   363 
   359   static size_t reserved_bytes(Metaspace::MetadataType mdtype);
   364   static size_t reserved_bytes(Metaspace::MetadataType mdtype);
   360   static size_t reserved_bytes() {
   365   static size_t reserved_bytes() {
   361     return reserved_bytes(Metaspace::ClassType) +
   366     return reserved_bytes(Metaspace::ClassType) +
   362            reserved_bytes(Metaspace::NonClassType);
   367            reserved_bytes(Metaspace::NonClassType);
   371   static size_t min_chunk_size_words();
   376   static size_t min_chunk_size_words();
   372   static size_t min_chunk_size_bytes() {
   377   static size_t min_chunk_size_bytes() {
   373     return min_chunk_size_words() * BytesPerWord;
   378     return min_chunk_size_words() * BytesPerWord;
   374   }
   379   }
   375 
   380 
   376   static void print_metadata_for_nmt(outputStream* out, size_t scale = K);
   381   // Flags for print_report().
       
   382   enum ReportFlag {
       
   383     // Show usage by class loader.
       
   384     rf_show_loaders                 = (1 << 0),
       
   385     // Breaks report down by chunk type (small, medium, ...).
       
   386     rf_break_down_by_chunktype      = (1 << 1),
       
   387     // Breaks report down by space type (anonymous, reflection, ...).
       
   388     rf_break_down_by_spacetype      = (1 << 2),
       
   389     // Print details about the underlying virtual spaces.
       
   390     rf_show_vslist                  = (1 << 3),
       
   391     // Print metaspace map.
       
   392     rf_show_vsmap                   = (1 << 4)
       
   393   };
       
   394 
       
   395   // This will print out a basic metaspace usage report but
       
   396   // unlike print_report() is guaranteed not to lock or to walk the CLDG.
       
   397   static void print_basic_report(outputStream* st, size_t scale);
       
   398 
       
   399   // Prints a report about the current metaspace state.
       
   400   // Optional parts can be enabled via flags.
       
   401   // Function will walk the CLDG and will lock the expand lock; if that is not
       
   402   // convenient, use print_basic_report() instead.
       
   403   static void print_report(outputStream* out, size_t scale = 0, int flags = 0);
   377 
   404 
   378   static bool has_chunk_free_list(Metaspace::MetadataType mdtype);
   405   static bool has_chunk_free_list(Metaspace::MetadataType mdtype);
   379   static MetaspaceChunkFreeListSummary chunk_free_list_summary(Metaspace::MetadataType mdtype);
   406   static MetaspaceChunkFreeListSummary chunk_free_list_summary(Metaspace::MetadataType mdtype);
   380 
   407 
   381   // Print change in used metadata.
   408   // Print change in used metadata.
   382   static void print_metaspace_change(size_t prev_metadata_used);
   409   static void print_metaspace_change(size_t prev_metadata_used);
   383   static void print_on(outputStream * out);
   410   static void print_on(outputStream * out);
   384   static void print_on(outputStream * out, Metaspace::MetadataType mdtype);
       
   385 
       
   386   static void print_class_waste(outputStream* out);
       
   387   static void print_waste(outputStream* out);
       
   388 
   411 
   389   // Prints an ASCII representation of the given space.
   412   // Prints an ASCII representation of the given space.
   390   static void print_metaspace_map(outputStream* out, Metaspace::MetadataType mdtype);
   413   static void print_metaspace_map(outputStream* out, Metaspace::MetadataType mdtype);
   391 
   414 
   392   static void dump(outputStream* out);
   415   static void dump(outputStream* out);
   393   static void verify_free_chunks();
   416   static void verify_free_chunks();
   394   // Checks that the values returned by allocated_capacity_bytes() and
   417   // Check internal counters (capacity, used).
   395   // capacity_bytes_slow() are the same.
       
   396   static void verify_capacity();
       
   397   static void verify_used();
       
   398   static void verify_metrics();
   418   static void verify_metrics();
   399 };
   419 };
   400 
   420 
   401 // Metaspace are deallocated when their class loader are GC'ed.
   421 // Metaspace are deallocated when their class loader are GC'ed.
   402 // This class implements a policy for inducing GC's to recover
   422 // This class implements a policy for inducing GC's to recover