diff src/share/vm/memory/metaspace.hpp @ 14422:2b8e28fdf503

Merge
author kvn
date Tue, 05 Nov 2013 17:38:04 -0800
parents 209aa13ab8c0
children ce86c36b8921 15120a36272d
line wrap: on
line diff
--- a/src/share/vm/memory/metaspace.hpp	Wed Oct 16 10:52:41 2013 +0200
+++ b/src/share/vm/memory/metaspace.hpp	Tue Nov 05 17:38:04 2013 -0800
@@ -56,12 +56,15 @@
 //                       +-------------------+
 //
 
+class ChunkManager;
 class ClassLoaderData;
 class Metablock;
+class Metachunk;
 class MetaWord;
 class Mutex;
 class outputStream;
 class SpaceManager;
+class VirtualSpaceList;
 
 // Metaspaces each have a  SpaceManager and allocations
 // are done by the SpaceManager.  Allocations are done
@@ -76,8 +79,6 @@
 // allocate() method returns a block for use as a
 // quantum of metadata.
 
-class VirtualSpaceList;
-
 class Metaspace : public CHeapObj<mtClass> {
   friend class VMStructs;
   friend class SpaceManager;
@@ -86,9 +87,10 @@
   friend class MetaspaceAux;
 
  public:
-  enum MetadataType {ClassType = 0,
-                     NonClassType = ClassType + 1,
-                     MetadataTypeCount = ClassType + 2
+  enum MetadataType {
+    ClassType,
+    NonClassType,
+    MetadataTypeCount
   };
   enum MetaspaceType {
     StandardMetaspaceType,
@@ -102,22 +104,32 @@
  private:
   void initialize(Mutex* lock, MetaspaceType type);
 
+  // Get the first chunk for a Metaspace.  Used for
+  // special cases such as the boot class loader, reflection
+  // class loader and anonymous class loader.
+  Metachunk* get_initialization_chunk(MetadataType mdtype,
+                                      size_t chunk_word_size,
+                                      size_t chunk_bunch);
+
   // Align up the word size to the allocation word size
   static size_t align_word_size_up(size_t);
 
   // Aligned size of the metaspace.
-  static size_t _class_metaspace_size;
+  static size_t _compressed_class_space_size;
 
-  static size_t class_metaspace_size() {
-    return _class_metaspace_size;
+  static size_t compressed_class_space_size() {
+    return _compressed_class_space_size;
   }
-  static void set_class_metaspace_size(size_t metaspace_size) {
-    _class_metaspace_size = metaspace_size;
+  static void set_compressed_class_space_size(size_t size) {
+    _compressed_class_space_size = size;
   }
 
   static size_t _first_chunk_word_size;
   static size_t _first_class_chunk_word_size;
 
+  static size_t _commit_alignment;
+  static size_t _reserve_alignment;
+
   SpaceManager* _vsm;
   SpaceManager* vsm() const { return _vsm; }
 
@@ -127,13 +139,16 @@
   // Allocate space for metadata of type mdtype. This is space
   // within a Metachunk and is used by
   //   allocate(ClassLoaderData*, size_t, bool, MetadataType, TRAPS)
-  // which returns a Metablock.
   MetaWord* allocate(size_t word_size, MetadataType mdtype);
 
   // Virtual Space lists for both classes and other metadata
   static VirtualSpaceList* _space_list;
   static VirtualSpaceList* _class_space_list;
 
+  static ChunkManager* _chunk_manager_metadata;
+  static ChunkManager* _chunk_manager_class;
+
+ public:
   static VirtualSpaceList* space_list()       { return _space_list; }
   static VirtualSpaceList* class_space_list() { return _class_space_list; }
   static VirtualSpaceList* get_space_list(MetadataType mdtype) {
@@ -141,6 +156,14 @@
     return mdtype == ClassType ? class_space_list() : space_list();
   }
 
+  static ChunkManager* chunk_manager_metadata() { return _chunk_manager_metadata; }
+  static ChunkManager* chunk_manager_class()    { return _chunk_manager_class; }
+  static ChunkManager* get_chunk_manager(MetadataType mdtype) {
+    assert(mdtype != MetadataTypeCount, "MetadaTypeCount can't be used as mdtype");
+    return mdtype == ClassType ? chunk_manager_class() : chunk_manager_metadata();
+  }
+
+ private:
   // This is used by DumpSharedSpaces only, where only _vsm is used. So we will
   // maintain a single list for now.
   void record_allocation(void* ptr, MetaspaceObj::Type type, size_t word_size);
@@ -169,28 +192,34 @@
   AllocRecord * _alloc_record_head;
   AllocRecord * _alloc_record_tail;
 
+  size_t class_chunk_size(size_t word_size);
+
  public:
 
   Metaspace(Mutex* lock, MetaspaceType type);
   ~Metaspace();
 
-  // Initialize globals for Metaspace
+  static void ergo_initialize();
   static void global_initialize();
 
   static size_t first_chunk_word_size() { return _first_chunk_word_size; }
   static size_t first_class_chunk_word_size() { return _first_class_chunk_word_size; }
 
+  static size_t reserve_alignment()       { return _reserve_alignment; }
+  static size_t reserve_alignment_words() { return _reserve_alignment / BytesPerWord; }
+  static size_t commit_alignment()        { return _commit_alignment; }
+  static size_t commit_alignment_words()  { return _commit_alignment / BytesPerWord; }
+
   char*  bottom() const;
   size_t used_words_slow(MetadataType mdtype) const;
-  size_t free_words(MetadataType mdtype) const;
+  size_t free_words_slow(MetadataType mdtype) const;
   size_t capacity_words_slow(MetadataType mdtype) const;
-  size_t waste_words(MetadataType mdtype) const;
 
   size_t used_bytes_slow(MetadataType mdtype) const;
   size_t capacity_bytes_slow(MetadataType mdtype) const;
 
-  static Metablock* allocate(ClassLoaderData* loader_data, size_t word_size,
-                             bool read_only, MetaspaceObj::Type type, TRAPS);
+  static MetaWord* allocate(ClassLoaderData* loader_data, size_t word_size,
+                            bool read_only, MetaspaceObj::Type type, TRAPS);
   void deallocate(MetaWord* ptr, size_t byte_size, bool is_class);
 
   MetaWord* expand_and_allocate(size_t size,
@@ -200,8 +229,12 @@
   void dump(outputStream* const out) const;
 
   // Free empty virtualspaces
+  static void purge(MetadataType mdtype);
   static void purge();
 
+  static void report_metadata_oome(ClassLoaderData* loader_data, size_t word_size,
+                                   MetadataType mdtype, TRAPS);
+
   void print_on(outputStream* st) const;
   // Debugging support
   void verify();
@@ -213,27 +246,26 @@
 
   void iterate(AllocRecordClosure *closure);
 
-  // Return TRUE only if UseCompressedKlassPointers is True and DumpSharedSpaces is False.
+  // Return TRUE only if UseCompressedClassPointers is True and DumpSharedSpaces is False.
   static bool using_class_space() {
-    return NOT_LP64(false) LP64_ONLY(UseCompressedKlassPointers && !DumpSharedSpaces);
+    return NOT_LP64(false) LP64_ONLY(UseCompressedClassPointers && !DumpSharedSpaces);
+  }
+
+  static bool is_class_space_allocation(MetadataType mdType) {
+    return mdType == ClassType && using_class_space();
   }
 
 };
 
 class MetaspaceAux : AllStatic {
-  static size_t free_chunks_total(Metaspace::MetadataType mdtype);
-
- public:
-  // Statistics for class space and data space in metaspace.
+  static size_t free_chunks_total_words(Metaspace::MetadataType mdtype);
 
   // These methods iterate over the classloader data graph
   // for the given Metaspace type.  These are slow.
   static size_t used_bytes_slow(Metaspace::MetadataType mdtype);
-  static size_t free_in_bytes(Metaspace::MetadataType mdtype);
+  static size_t free_bytes_slow(Metaspace::MetadataType mdtype);
   static size_t capacity_bytes_slow(Metaspace::MetadataType mdtype);
-
-  // Iterates over the virtual space list.
-  static size_t reserved_in_bytes(Metaspace::MetadataType mdtype);
+  static size_t capacity_bytes_slow();
 
   // Running sum of space in all Metachunks that has been
   // allocated to a Metaspace.  This is used instead of
@@ -263,17 +295,16 @@
   }
 
   // Used by MetaspaceCounters
-  static size_t free_chunks_total();
-  static size_t free_chunks_total_in_bytes();
-  static size_t free_chunks_total_in_bytes(Metaspace::MetadataType mdtype);
+  static size_t free_chunks_total_words();
+  static size_t free_chunks_total_bytes();
+  static size_t free_chunks_total_bytes(Metaspace::MetadataType mdtype);
 
   static size_t allocated_capacity_words(Metaspace::MetadataType mdtype) {
     return _allocated_capacity_words[mdtype];
   }
   static size_t allocated_capacity_words() {
-    return _allocated_capacity_words[Metaspace::NonClassType] +
-           (Metaspace::using_class_space() ?
-           _allocated_capacity_words[Metaspace::ClassType] : 0);
+    return allocated_capacity_words(Metaspace::NonClassType) +
+           allocated_capacity_words(Metaspace::ClassType);
   }
   static size_t allocated_capacity_bytes(Metaspace::MetadataType mdtype) {
     return allocated_capacity_words(mdtype) * BytesPerWord;
@@ -286,9 +317,8 @@
     return _allocated_used_words[mdtype];
   }
   static size_t allocated_used_words() {
-    return _allocated_used_words[Metaspace::NonClassType] +
-           (Metaspace::using_class_space() ?
-           _allocated_used_words[Metaspace::ClassType] : 0);
+    return allocated_used_words(Metaspace::NonClassType) +
+           allocated_used_words(Metaspace::ClassType);
   }
   static size_t allocated_used_bytes(Metaspace::MetadataType mdtype) {
     return allocated_used_words(mdtype) * BytesPerWord;
@@ -300,31 +330,22 @@
   static size_t free_bytes();
   static size_t free_bytes(Metaspace::MetadataType mdtype);
 
-  // Total capacity in all Metaspaces
-  static size_t capacity_bytes_slow() {
-#ifdef PRODUCT
-    // Use allocated_capacity_bytes() in PRODUCT instead of this function.
-    guarantee(false, "Should not call capacity_bytes_slow() in the PRODUCT");
-#endif
-    size_t class_capacity = capacity_bytes_slow(Metaspace::ClassType);
-    size_t non_class_capacity = capacity_bytes_slow(Metaspace::NonClassType);
-    assert(allocated_capacity_bytes() == class_capacity + non_class_capacity,
-           err_msg("bad accounting: allocated_capacity_bytes() " SIZE_FORMAT
-             " class_capacity + non_class_capacity " SIZE_FORMAT
-             " class_capacity " SIZE_FORMAT " non_class_capacity " SIZE_FORMAT,
-             allocated_capacity_bytes(), class_capacity + non_class_capacity,
-             class_capacity, non_class_capacity));
-
-    return class_capacity + non_class_capacity;
+  static size_t reserved_bytes(Metaspace::MetadataType mdtype);
+  static size_t reserved_bytes() {
+    return reserved_bytes(Metaspace::ClassType) +
+           reserved_bytes(Metaspace::NonClassType);
   }
 
-  // Total space reserved in all Metaspaces
-  static size_t reserved_in_bytes() {
-    return reserved_in_bytes(Metaspace::ClassType) +
-           reserved_in_bytes(Metaspace::NonClassType);
+  static size_t committed_bytes(Metaspace::MetadataType mdtype);
+  static size_t committed_bytes() {
+    return committed_bytes(Metaspace::ClassType) +
+           committed_bytes(Metaspace::NonClassType);
   }
 
-  static size_t min_chunk_size();
+  static size_t min_chunk_size_words();
+  static size_t min_chunk_size_bytes() {
+    return min_chunk_size_words() * BytesPerWord;
+  }
 
   // Print change in used metadata.
   static void print_metaspace_change(size_t prev_metadata_used);
@@ -348,17 +369,10 @@
 
 class MetaspaceGC : AllStatic {
 
-  // The current high-water-mark for inducing a GC.  When
-  // the capacity of all space in the virtual lists reaches this value,
-  // a GC is induced and the value is increased.  This should be changed
-  // to the space actually used for allocations to avoid affects of
-  // fragmentation losses to partially used chunks.  Size is in words.
-  static size_t _capacity_until_GC;
-
-  // After a GC is done any allocation that fails should try to expand
-  // the capacity of the Metaspaces.  This flag is set during attempts
-  // to allocate in the VMGCOperation that does the GC.
-  static bool _expand_after_GC;
+  // The current high-water-mark for inducing a GC.
+  // When committed memory of all metaspaces reaches this value,
+  // a GC is induced and the value is increased. Size is in bytes.
+  static volatile intptr_t _capacity_until_GC;
 
   // For a CMS collection, signal that a concurrent collection should
   // be started.
@@ -366,20 +380,16 @@
 
   static uint _shrink_factor;
 
-  static void set_capacity_until_GC(size_t v) { _capacity_until_GC = v; }
-
   static size_t shrink_factor() { return _shrink_factor; }
   void set_shrink_factor(uint v) { _shrink_factor = v; }
 
  public:
 
-  static size_t capacity_until_GC() { return _capacity_until_GC; }
-  static void inc_capacity_until_GC(size_t v) { _capacity_until_GC += v; }
-  static void dec_capacity_until_GC(size_t v) {
-    _capacity_until_GC = _capacity_until_GC > v ? _capacity_until_GC - v : 0;
-  }
-  static bool expand_after_GC()           { return _expand_after_GC; }
-  static void set_expand_after_GC(bool v) { _expand_after_GC = v; }
+  static void initialize() { _capacity_until_GC = MetaspaceSize; }
+
+  static size_t capacity_until_GC();
+  static size_t inc_capacity_until_GC(size_t v);
+  static size_t dec_capacity_until_GC(size_t v);
 
   static bool should_concurrent_collect() { return _should_concurrent_collect; }
   static void set_should_concurrent_collect(bool v) {
@@ -387,11 +397,14 @@
   }
 
   // The amount to increase the high-water-mark (_capacity_until_GC)
-  static size_t delta_capacity_until_GC(size_t word_size);
+  static size_t delta_capacity_until_GC(size_t bytes);
 
-  // It is expected that this will be called when the current capacity
-  // has been used and a GC should be considered.
-  static bool should_expand(VirtualSpaceList* vsl, size_t word_size);
+  // Tells if we have can expand metaspace without hitting set limits.
+  static bool can_expand(size_t words, bool is_class);
+
+  // Returns amount that we can expand without hitting a GC,
+  // measured in words.
+  static size_t allowed_expansion();
 
   // Calculate the new high-water mark at which to induce
   // a GC.