< prev index next >

src/hotspot/share/memory/metaspace.cpp

Print this page
rev 49017 : [mq]: metaspace-stat


  22  *
  23  */
  24 #include "precompiled.hpp"
  25 #include "aot/aotLoader.hpp"
  26 #include "gc/shared/collectedHeap.hpp"
  27 #include "gc/shared/collectorPolicy.hpp"
  28 #include "gc/shared/gcLocker.hpp"
  29 #include "logging/log.hpp"
  30 #include "logging/logStream.hpp"
  31 #include "memory/allocation.hpp"
  32 #include "memory/binaryTreeDictionary.hpp"
  33 #include "memory/filemap.hpp"
  34 #include "memory/freeList.hpp"
  35 #include "memory/metachunk.hpp"
  36 #include "memory/metaspace.hpp"
  37 #include "memory/metaspaceGCThresholdUpdater.hpp"
  38 #include "memory/metaspaceShared.hpp"
  39 #include "memory/metaspaceTracer.hpp"
  40 #include "memory/resourceArea.hpp"
  41 #include "memory/universe.hpp"

  42 #include "runtime/atomic.hpp"
  43 #include "runtime/globals.hpp"
  44 #include "runtime/init.hpp"
  45 #include "runtime/java.hpp"
  46 #include "runtime/mutex.hpp"
  47 #include "runtime/orderAccess.inline.hpp"
  48 #include "services/memTracker.hpp"
  49 #include "services/memoryService.hpp"
  50 #include "utilities/align.hpp"
  51 #include "utilities/copy.hpp"
  52 #include "utilities/debug.hpp"
  53 #include "utilities/macros.hpp"

  54 
  55 typedef BinaryTreeDictionary<Metablock, FreeList<Metablock> > BlockTreeDictionary;
  56 typedef BinaryTreeDictionary<Metachunk, FreeList<Metachunk> > ChunkTreeDictionary;
  57 
  58 // Set this constant to enable slow integrity checking of the free chunk lists
  59 const bool metaspace_slow_verify = false;
  60 
  61 size_t const allocation_from_dictionary_limit = 4 * K;
  62 
  63 MetaWord* last_allocated = 0;
  64 
  65 size_t Metaspace::_compressed_class_space_size;
  66 const MetaspaceTracer* Metaspace::_tracer = NULL;
  67 
  68 DEBUG_ONLY(bool Metaspace::_frozen = false;)
  69 
  70 // Used in declarations in SpaceManager and ChunkManager
  71 enum ChunkIndex {
  72   ZeroIndex = 0,
  73   SpecializedIndex = ZeroIndex,


 134   //   SpecializedChunk
 135   //   SmallChunk
 136   //   MediumChunk
 137   ChunkList _free_chunks[NumberOfFreeLists];
 138 
 139   // Return non-humongous chunk list by its index.
 140   ChunkList* free_chunks(ChunkIndex index);
 141 
 142   // Returns non-humongous chunk list for the given chunk word size.
 143   ChunkList* find_free_chunks_list(size_t word_size);
 144 
 145   //   HumongousChunk
 146   ChunkTreeDictionary _humongous_dictionary;
 147 
 148   // Returns the humongous chunk dictionary.
 149   ChunkTreeDictionary* humongous_dictionary() {
 150     return &_humongous_dictionary;
 151   }
 152 
 153   // Size, in metaspace words, of all chunks managed by this ChunkManager
 154   size_t _free_chunks_total;
 155   // Number of chunks in this ChunkManager
 156   size_t _free_chunks_count;
 157 
 158   // Update counters after a chunk was added or removed removed.
 159   void account_for_added_chunk(const Metachunk* c);
 160   void account_for_removed_chunk(const Metachunk* c);
 161 
 162   // Debug support
 163 
 164   size_t sum_free_chunks();
 165   size_t sum_free_chunks_count();
 166 
 167   void locked_verify_free_chunks_total();
 168   void slow_locked_verify_free_chunks_total() {
 169     if (metaspace_slow_verify) {
 170       locked_verify_free_chunks_total();
 171     }
 172   }
 173   void locked_verify_free_chunks_count();
 174   void slow_locked_verify_free_chunks_count() {
 175     if (metaspace_slow_verify) {
 176       locked_verify_free_chunks_count();
 177     }
 178   }
 179   void verify_free_chunks_count();
 180 
 181   struct ChunkManagerStatistics {
 182     size_t num_by_type[NumberOfFreeLists];
 183     size_t single_size_by_type[NumberOfFreeLists];
 184     size_t total_size_by_type[NumberOfFreeLists];
 185     size_t num_humongous_chunks;
 186     size_t total_size_humongous_chunks;
 187   };
 188 
 189   void locked_get_statistics(ChunkManagerStatistics* stat) const;
 190   void get_statistics(ChunkManagerStatistics* stat) const;
 191   static void print_statistics(const ChunkManagerStatistics* stat, outputStream* out, size_t scale);
 192 
 193  public:
 194 
 195   ChunkManager(size_t specialized_size, size_t small_size, size_t medium_size)
 196       : _free_chunks_total(0), _free_chunks_count(0) {
 197     _free_chunks[SpecializedIndex].set_size(specialized_size);
 198     _free_chunks[SmallIndex].set_size(small_size);
 199     _free_chunks[MediumIndex].set_size(medium_size);
 200   }
 201 
 202   // add or delete (return) a chunk to the global freelist.
 203   Metachunk* chunk_freelist_allocate(size_t word_size);
 204 
 205   // Map a size to a list index assuming that there are lists
 206   // for special, small, medium, and humongous chunks.
 207   ChunkIndex list_index(size_t size);
 208 
 209   // Map a given index to the chunk size.
 210   size_t size_by_index(ChunkIndex index) const;
 211 
 212   // Take a chunk from the ChunkManager. The chunk is expected to be in
 213   // the chunk manager (the freelist if non-humongous, the dictionary if
 214   // humongous).
 215   void remove_chunk(Metachunk* chunk);
 216 


 269                                          num_free_chunks(HumongousIndex),
 270                                          size_free_chunks_in_bytes(SpecializedIndex),
 271                                          size_free_chunks_in_bytes(SmallIndex),
 272                                          size_free_chunks_in_bytes(MediumIndex),
 273                                          size_free_chunks_in_bytes(HumongousIndex));
 274   }
 275 
 276   // Debug support
 277   void verify();
 278   void slow_verify() {
 279     if (metaspace_slow_verify) {
 280       verify();
 281     }
 282   }
 283   void locked_verify();
 284   void slow_locked_verify() {
 285     if (metaspace_slow_verify) {
 286       locked_verify();
 287     }
 288   }
 289   void verify_free_chunks_total();
 290 
 291   void locked_print_free_chunks(outputStream* st);
 292   void locked_print_sum_free_chunks(outputStream* st);





 293 
 294   void print_on(outputStream* st) const;
 295 
 296   // Prints composition for both non-class and (if available)
 297   // class chunk manager.
 298   static void print_all_chunkmanagers(outputStream* out, size_t scale = 1);
 299 };
 300 
 301 class SmallBlocks : public CHeapObj<mtClass> {
 302   const static uint _small_block_max_size = sizeof(TreeChunk<Metablock,  FreeList<Metablock> >)/HeapWordSize;
 303   const static uint _small_block_min_size = sizeof(Metablock)/HeapWordSize;
 304 
 305  private:
 306   FreeList<Metablock> _small_lists[_small_block_max_size - _small_block_min_size];
 307 
 308   FreeList<Metablock>& list_at(size_t word_size) {
 309     assert(word_size >= _small_block_min_size, "There are no metaspace objects less than %u words", _small_block_min_size);
 310     return _small_lists[word_size - _small_block_min_size];
 311   }
 312 
 313  public:
 314   SmallBlocks() {
 315     for (uint i = _small_block_min_size; i < _small_block_max_size; i++) {
 316       uint k = i - _small_block_min_size;
 317       _small_lists[k].set_size(i);
 318     }


1831 }
1832 
1833 #ifdef ASSERT
1834 bool Metadebug::test_metadata_failure() {
1835   if (MetadataAllocationFailALot &&
1836       Threads::is_vm_complete()) {
1837     if (_allocation_fail_alot_count > 0) {
1838       _allocation_fail_alot_count--;
1839     } else {
1840       log_trace(gc, metaspace, freelist)("Metadata allocation failing for MetadataAllocationFailALot");
1841       init_allocation_fail_alot_count();
1842       return true;
1843     }
1844   }
1845   return false;
1846 }
1847 #endif
1848 
1849 // ChunkManager methods
1850 size_t ChunkManager::free_chunks_total_words() {
1851   return _free_chunks_total;
1852 }
1853 
1854 size_t ChunkManager::free_chunks_total_bytes() {
1855   return free_chunks_total_words() * BytesPerWord;
1856 }
1857 
1858 // Update internal accounting after a chunk was added
1859 void ChunkManager::account_for_added_chunk(const Metachunk* c) {
1860   assert_lock_strong(SpaceManager::expand_lock());
1861   _free_chunks_count ++;
1862   _free_chunks_total += c->word_size();
1863 }
1864 
1865 // Update internal accounting after a chunk was removed
1866 void ChunkManager::account_for_removed_chunk(const Metachunk* c) {
1867   assert_lock_strong(SpaceManager::expand_lock());
1868   assert(_free_chunks_count >= 1,
1869     "ChunkManager::_free_chunks_count: about to go negative (" SIZE_FORMAT ").", _free_chunks_count);
1870   assert(_free_chunks_total >= c->word_size(),
1871     "ChunkManager::_free_chunks_total: about to go negative"
1872      "(now: " SIZE_FORMAT ", decrement value: " SIZE_FORMAT ").", _free_chunks_total, c->word_size());
1873   _free_chunks_count --;
1874   _free_chunks_total -= c->word_size();
1875 }
1876 
1877 size_t ChunkManager::free_chunks_count() {
1878 #ifdef ASSERT
1879   if (!UseConcMarkSweepGC && !SpaceManager::expand_lock()->is_locked()) {
1880     MutexLockerEx cl(SpaceManager::expand_lock(),
1881                      Mutex::_no_safepoint_check_flag);
1882     // This lock is only needed in debug because the verification
1883     // of the _free_chunks_totals walks the list of free chunks
1884     slow_locked_verify_free_chunks_count();
1885   }
1886 #endif
1887   return _free_chunks_count;
1888 }
1889 
1890 ChunkIndex ChunkManager::list_index(size_t size) {
1891   if (size_by_index(SpecializedIndex) == size) {
1892     return SpecializedIndex;
1893   }
1894   if (size_by_index(SmallIndex) == size) {
1895     return SmallIndex;
1896   }
1897   const size_t med_size = size_by_index(MediumIndex);
1898   if (med_size == size) {
1899     return MediumIndex;
1900   }
1901 
1902   assert(size > med_size, "Not a humongous chunk");
1903   return HumongousIndex;
1904 }
1905 
1906 size_t ChunkManager::size_by_index(ChunkIndex index) const {
1907   index_bounds_check(index);
1908   assert(index != HumongousIndex, "Do not call for humongous chunks.");
1909   return _free_chunks[index].size();
1910 }
1911 
1912 void ChunkManager::locked_verify_free_chunks_total() {
1913   assert_lock_strong(SpaceManager::expand_lock());
1914   assert(sum_free_chunks() == _free_chunks_total,
1915          "_free_chunks_total " SIZE_FORMAT " is not the"
1916          " same as sum " SIZE_FORMAT, _free_chunks_total,
1917          sum_free_chunks());
1918 }
1919 
1920 void ChunkManager::verify_free_chunks_total() {
1921   MutexLockerEx cl(SpaceManager::expand_lock(),
1922                      Mutex::_no_safepoint_check_flag);
1923   locked_verify_free_chunks_total();
1924 }
1925 
1926 void ChunkManager::locked_verify_free_chunks_count() {
1927   assert_lock_strong(SpaceManager::expand_lock());
1928   assert(sum_free_chunks_count() == _free_chunks_count,
1929          "_free_chunks_count " SIZE_FORMAT " is not the"
1930          " same as sum " SIZE_FORMAT, _free_chunks_count,
1931          sum_free_chunks_count());
1932 }
1933 
1934 void ChunkManager::verify_free_chunks_count() {
1935 #ifdef ASSERT
1936   MutexLockerEx cl(SpaceManager::expand_lock(),
1937                      Mutex::_no_safepoint_check_flag);
1938   locked_verify_free_chunks_count();
1939 #endif
1940 }
1941 
1942 void ChunkManager::verify() {
1943   MutexLockerEx cl(SpaceManager::expand_lock(),
1944                      Mutex::_no_safepoint_check_flag);
1945   locked_verify();
1946 }
1947 
1948 void ChunkManager::locked_verify() {
1949   locked_verify_free_chunks_count();
1950   locked_verify_free_chunks_total();
1951 }
1952 
1953 void ChunkManager::locked_print_free_chunks(outputStream* st) {
1954   assert_lock_strong(SpaceManager::expand_lock());
1955   st->print_cr("Free chunk total " SIZE_FORMAT "  count " SIZE_FORMAT,
1956                 _free_chunks_total, _free_chunks_count);
1957 }
1958 
1959 void ChunkManager::locked_print_sum_free_chunks(outputStream* st) {
1960   assert_lock_strong(SpaceManager::expand_lock());
1961   st->print_cr("Sum free chunk total " SIZE_FORMAT "  count " SIZE_FORMAT,
1962                 sum_free_chunks(), sum_free_chunks_count());
1963 }
1964 
1965 ChunkList* ChunkManager::free_chunks(ChunkIndex index) {
1966   assert(index == SpecializedIndex || index == SmallIndex || index == MediumIndex,
1967          "Bad index: %d", (int)index);
1968 
1969   return &_free_chunks[index];
1970 }
1971 
1972 // These methods that sum the free chunk lists are used in printing
1973 // methods that are used in product builds.
1974 size_t ChunkManager::sum_free_chunks() {
1975   assert_lock_strong(SpaceManager::expand_lock());
1976   size_t result = 0;
1977   for (ChunkIndex i = ZeroIndex; i < NumberOfFreeLists; i = next_chunk_index(i)) {
1978     ChunkList* list = free_chunks(i);
1979 
1980     if (list == NULL) {
1981       continue;
1982     }
1983 
1984     result = result + list->count() * list->size();


2140     // by the call to return_chunk_at_head();
2141     Metachunk* next = cur->next();
2142     if (log.is_enabled()) { // tracing
2143       num_chunks_returned ++;
2144       size_chunks_returned += cur->word_size();
2145     }
2146     return_single_chunk(index, cur);
2147     cur = next;
2148   }
2149   if (log.is_enabled()) { // tracing
2150     log.print("returned %u %s chunks to freelist, total word size " SIZE_FORMAT ".",
2151         num_chunks_returned, chunk_size_name(index), size_chunks_returned);
2152     if (index != HumongousIndex) {
2153       log.print("updated freelist count: " SIZE_FORMAT ".", free_chunks(index)->size());
2154     } else {
2155       log.print("updated dictionary count " SIZE_FORMAT ".", _humongous_dictionary.total_count());
2156     }
2157   }
2158 }
2159 
2160 void ChunkManager::print_on(outputStream* out) const {
2161   _humongous_dictionary.report_statistics(out);
2162 }
2163 
2164 void ChunkManager::locked_get_statistics(ChunkManagerStatistics* stat) const {
2165   assert_lock_strong(SpaceManager::expand_lock());
2166   for (ChunkIndex i = ZeroIndex; i < NumberOfFreeLists; i = next_chunk_index(i)) {
2167     stat->num_by_type[i] = num_free_chunks(i);
2168     stat->single_size_by_type[i] = size_by_index(i);
2169     stat->total_size_by_type[i] = size_free_chunks_in_bytes(i);
2170   }
2171   stat->num_humongous_chunks = num_free_chunks(HumongousIndex);
2172   stat->total_size_humongous_chunks = size_free_chunks_in_bytes(HumongousIndex);
2173 }
2174 
2175 void ChunkManager::get_statistics(ChunkManagerStatistics* stat) const {
2176   MutexLockerEx cl(SpaceManager::expand_lock(),
2177                    Mutex::_no_safepoint_check_flag);
2178   locked_get_statistics(stat);
2179 }
2180 
2181 void ChunkManager::print_statistics(const ChunkManagerStatistics* stat, outputStream* out, size_t scale) {
2182   size_t total = 0;
2183   assert(scale == 1 || scale == K || scale == M || scale == G, "Invalid scale");


2197   }
2198 
2199 
2200   total += stat->total_size_humongous_chunks;
2201 
2202   if (scale == 1) {
2203     out->print_cr("  " SIZE_FORMAT " humongous chunks, total " SIZE_FORMAT " bytes",
2204     stat->num_humongous_chunks, stat->total_size_humongous_chunks);
2205 
2206     out->print_cr("  total size: " SIZE_FORMAT " bytes.", total);
2207   } else {
2208     out->print_cr("  " SIZE_FORMAT " humongous chunks, total %.2f%s",
2209     stat->num_humongous_chunks,
2210     (float)stat->total_size_humongous_chunks / scale, unit);
2211 
2212     out->print_cr("  total size: %.2f%s.", (float)total / scale, unit);
2213   }
2214 
2215 }
2216 
2217 void ChunkManager::print_all_chunkmanagers(outputStream* out, size_t scale) {
2218   assert(scale == 1 || scale == K || scale == M || scale == G, "Invalid scale");




2219 
2220   // Note: keep lock protection only to retrieving statistics; keep printing
2221   // out of lock protection
2222   ChunkManagerStatistics stat;












2223   out->print_cr("Chunkmanager (non-class):");
2224   const ChunkManager* const non_class_cm = Metaspace::chunk_manager_metadata();
2225   if (non_class_cm != NULL) {
2226     non_class_cm->get_statistics(&stat);
2227     ChunkManager::print_statistics(&stat, out, scale);
2228   } else {
2229     out->print_cr("unavailable.");
2230   }
2231   out->print_cr("Chunkmanager (class):");
2232   const ChunkManager* const class_cm = Metaspace::chunk_manager_class();
2233   if (class_cm != NULL) {
2234     class_cm->get_statistics(&stat);
2235     ChunkManager::print_statistics(&stat, out, scale);
2236   } else {
2237     out->print_cr("unavailable.");
2238   }
2239 }
2240 
2241 // SpaceManager methods
2242 
2243 size_t SpaceManager::adjust_initial_chunk_size(size_t requested, bool is_class_space) {
2244   size_t chunk_sizes[] = {
2245       specialized_chunk_size(is_class_space),
2246       small_chunk_size(is_class_space),
2247       medium_chunk_size(is_class_space)
2248   };
2249 
2250   // Adjust up to one of the fixed chunk sizes ...
2251   for (size_t i = 0; i < ARRAY_SIZE(chunk_sizes); i++) {
2252     if (requested <= chunk_sizes[i]) {
2253       return chunk_sizes[i];
2254     }
2255   }


2382       chunk = chunk->next();
2383     }
2384   }
2385   return used;
2386 }
2387 
2388 void SpaceManager::locked_print_chunks_in_use_on(outputStream* st) const {
2389 
2390   for (ChunkIndex i = ZeroIndex; i < NumberOfInUseLists; i = next_chunk_index(i)) {
2391     Metachunk* chunk = chunks_in_use(i);
2392     st->print("SpaceManager: %s " PTR_FORMAT,
2393                  chunk_size_name(i), p2i(chunk));
2394     if (chunk != NULL) {
2395       st->print_cr(" free " SIZE_FORMAT,
2396                    chunk->free_word_size());
2397     } else {
2398       st->cr();
2399     }
2400   }
2401 
2402   chunk_manager()->locked_print_free_chunks(st);
2403   chunk_manager()->locked_print_sum_free_chunks(st);
2404 }
2405 
2406 size_t SpaceManager::calc_chunk_size(size_t word_size) {
2407 
2408   // Decide between a small chunk and a medium chunk.  Up to
2409   // _small_chunk_limit small chunks can be allocated.
2410   // After that a medium chunk is preferred.
2411   size_t chunk_word_size;
2412 
2413   // Special case for anonymous metadata space.
2414   // Anonymous metadata space is usually small, with majority within 1K - 2K range and
2415   // rarely about 4K (64-bits JVM).
2416   // Instead of jumping to SmallChunk after initial chunk exhausted, keeping allocation
2417   // from SpecializeChunk up to _anon_metadata_specialize_chunk_limit (4) reduces space waste
2418   // from 60+% to around 30%.
2419   if (_space_type == Metaspace::AnonymousMetaspaceType &&
2420       _mdtype == Metaspace::NonClassType &&
2421       sum_count_in_chunks_in_use(SpecializedIndex) < _anon_metadata_specialize_chunk_limit &&
2422       word_size + Metachunk::overhead() <= SpecializedChunk) {
2423     return SpecializedChunk;


3301 
3302   if (Metaspace::using_class_space()) {
3303     _out->print("                  Class data ");
3304     _total_anon_class.print_on(_out, _scale);
3305   }
3306 }
3307 
3308 void MetaspaceAux::print_metadata_for_nmt(outputStream* out, size_t scale) {
3309   const char* unit = scale_unit(scale);
3310   out->print_cr("Metaspaces:");
3311   out->print_cr("  Metadata space: reserved=" SIZE_FORMAT_W(10) "%s committed=" SIZE_FORMAT_W(10) "%s",
3312     reserved_bytes(Metaspace::NonClassType) / scale, unit,
3313     committed_bytes(Metaspace::NonClassType) / scale, unit);
3314   if (Metaspace::using_class_space()) {
3315     out->print_cr("  Class    space: reserved=" SIZE_FORMAT_W(10) "%s committed=" SIZE_FORMAT_W(10) "%s",
3316     reserved_bytes(Metaspace::ClassType) / scale, unit,
3317     committed_bytes(Metaspace::ClassType) / scale, unit);
3318   }
3319 
3320   out->cr();
3321   ChunkManager::print_all_chunkmanagers(out, scale);
3322 
3323   out->cr();
3324   out->print_cr("Per-classloader metadata:");
3325   out->cr();
3326 
3327   PrintCLDMetaspaceInfoClosure cl(out, scale);
3328   ClassLoaderDataGraph::cld_do(&cl);
3329 }
3330 
3331 
3332 // Dump global metaspace things from the end of ClassLoaderDataGraph
3333 void MetaspaceAux::dump(outputStream* out) {
3334   out->print_cr("All Metaspace:");
3335   out->print("data space: "); print_on(out, Metaspace::NonClassType);
3336   out->print("class space: "); print_on(out, Metaspace::ClassType);
3337   print_waste(out);
3338 }
3339 
3340 // Prints an ASCII representation of the given space.
3341 void MetaspaceAux::print_metaspace_map(outputStream* out, Metaspace::MetadataType mdtype) {


3983 }
3984 
3985 void Metaspace::report_metadata_oome(ClassLoaderData* loader_data, size_t word_size, MetaspaceObj::Type type, MetadataType mdtype, TRAPS) {
3986   tracer()->report_metadata_oom(loader_data, word_size, type, mdtype);
3987 
3988   // If result is still null, we are out of memory.
3989   Log(gc, metaspace, freelist) log;
3990   if (log.is_info()) {
3991     log.info("Metaspace (%s) allocation failed for size " SIZE_FORMAT,
3992              is_class_space_allocation(mdtype) ? "class" : "data", word_size);
3993     ResourceMark rm;
3994     if (log.is_debug()) {
3995       if (loader_data->metaspace_or_null() != NULL) {
3996         LogStream ls(log.debug());
3997         loader_data->dump(&ls);
3998       }
3999     }
4000     LogStream ls(log.info());
4001     MetaspaceAux::dump(&ls);
4002     MetaspaceAux::print_metaspace_map(&ls, mdtype);
4003     ChunkManager::print_all_chunkmanagers(&ls);
4004   }
4005 
4006   bool out_of_compressed_class_space = false;
4007   if (is_class_space_allocation(mdtype)) {
4008     Metaspace* metaspace = loader_data->metaspace_non_null();
4009     out_of_compressed_class_space =
4010       MetaspaceAux::committed_bytes(Metaspace::ClassType) +
4011       (metaspace->class_chunk_size(word_size) * BytesPerWord) >
4012       CompressedClassSpaceSize;
4013   }
4014 
4015   // -XX:+HeapDumpOnOutOfMemoryError and -XX:OnOutOfMemoryError support
4016   const char* space_string = out_of_compressed_class_space ?
4017     "Compressed class space" : "Metaspace";
4018 
4019   report_java_out_of_memory(space_string);
4020 
4021   if (JvmtiExport::should_post_resource_exhausted()) {
4022     JvmtiExport::post_resource_exhausted(
4023         JVMTI_RESOURCE_EXHAUSTED_OOM_ERROR,




  22  *
  23  */
  24 #include "precompiled.hpp"
  25 #include "aot/aotLoader.hpp"
  26 #include "gc/shared/collectedHeap.hpp"
  27 #include "gc/shared/collectorPolicy.hpp"
  28 #include "gc/shared/gcLocker.hpp"
  29 #include "logging/log.hpp"
  30 #include "logging/logStream.hpp"
  31 #include "memory/allocation.hpp"
  32 #include "memory/binaryTreeDictionary.hpp"
  33 #include "memory/filemap.hpp"
  34 #include "memory/freeList.hpp"
  35 #include "memory/metachunk.hpp"
  36 #include "memory/metaspace.hpp"
  37 #include "memory/metaspaceGCThresholdUpdater.hpp"
  38 #include "memory/metaspaceShared.hpp"
  39 #include "memory/metaspaceTracer.hpp"
  40 #include "memory/resourceArea.hpp"
  41 #include "memory/universe.hpp"
  42 #include "memory/virtualspace.hpp"
  43 #include "runtime/atomic.hpp"
  44 #include "runtime/globals.hpp"
  45 #include "runtime/init.hpp"
  46 #include "runtime/java.hpp"
  47 #include "runtime/mutex.hpp"
  48 #include "runtime/orderAccess.inline.hpp"
  49 #include "services/memTracker.hpp"
  50 #include "services/memoryService.hpp"
  51 #include "utilities/align.hpp"
  52 #include "utilities/copy.hpp"
  53 #include "utilities/debug.hpp"
  54 #include "utilities/macros.hpp"
  55 #include "utilities/ostream.hpp"
  56 
  57 typedef BinaryTreeDictionary<Metablock, FreeList<Metablock> > BlockTreeDictionary;
  58 typedef BinaryTreeDictionary<Metachunk, FreeList<Metachunk> > ChunkTreeDictionary;
  59 
  60 // Set this constant to enable slow integrity checking of the free chunk lists
  61 const bool metaspace_slow_verify = false;
  62 
  63 size_t const allocation_from_dictionary_limit = 4 * K;
  64 
  65 MetaWord* last_allocated = 0;
  66 
  67 size_t Metaspace::_compressed_class_space_size;
  68 const MetaspaceTracer* Metaspace::_tracer = NULL;
  69 
  70 DEBUG_ONLY(bool Metaspace::_frozen = false;)
  71 
  72 // Used in declarations in SpaceManager and ChunkManager
  73 enum ChunkIndex {
  74   ZeroIndex = 0,
  75   SpecializedIndex = ZeroIndex,


 136   //   SpecializedChunk
 137   //   SmallChunk
 138   //   MediumChunk
 139   ChunkList _free_chunks[NumberOfFreeLists];
 140 
 141   // Return non-humongous chunk list by its index.
 142   ChunkList* free_chunks(ChunkIndex index);
 143 
 144   // Returns non-humongous chunk list for the given chunk word size.
 145   ChunkList* find_free_chunks_list(size_t word_size);
 146 
 147   //   HumongousChunk
 148   ChunkTreeDictionary _humongous_dictionary;
 149 
 150   // Returns the humongous chunk dictionary.
 151   ChunkTreeDictionary* humongous_dictionary() {
 152     return &_humongous_dictionary;
 153   }
 154 
 155   // Size, in metaspace words, of all chunks managed by this ChunkManager
 156   size_t _free_chunks_total_words;
 157   // Number of chunks in this ChunkManager
 158   size_t _free_chunks_count;
 159 
 160   // Update counters after a chunk was added or removed removed.
 161   void account_for_added_chunk(const Metachunk* c);
 162   void account_for_removed_chunk(const Metachunk* c);
 163 
 164   // Debug support
 165 
 166   size_t sum_free_chunks();
 167   size_t sum_free_chunks_count();
 168 
 169   void locked_verify_free_chunks_total();
 170   void slow_locked_verify_free_chunks_total() {
 171     if (metaspace_slow_verify) {
 172       locked_verify_free_chunks_total();
 173     }
 174   }
 175   void locked_verify_free_chunks_count();
 176   void slow_locked_verify_free_chunks_count() {
 177     if (metaspace_slow_verify) {
 178       locked_verify_free_chunks_count();
 179     }
 180   }

 181 
 182   struct ChunkManagerStatistics {
 183     size_t num_by_type[NumberOfFreeLists];
 184     size_t single_size_by_type[NumberOfFreeLists];
 185     size_t total_size_by_type[NumberOfFreeLists];
 186     size_t num_humongous_chunks;
 187     size_t total_size_humongous_chunks;
 188   };
 189 
 190   void locked_get_statistics(ChunkManagerStatistics* stat) const;
 191   void get_statistics(ChunkManagerStatistics* stat) const;
 192   static void print_statistics(const ChunkManagerStatistics* stat, outputStream* out, size_t scale);
 193 
 194  public:
 195 
 196   ChunkManager(size_t specialized_size, size_t small_size, size_t medium_size)
 197       : _free_chunks_total_words(0), _free_chunks_count(0) {
 198     _free_chunks[SpecializedIndex].set_size(specialized_size);
 199     _free_chunks[SmallIndex].set_size(small_size);
 200     _free_chunks[MediumIndex].set_size(medium_size);
 201   }
 202 
 203   // add or delete (return) a chunk to the global freelist.
 204   Metachunk* chunk_freelist_allocate(size_t word_size);
 205 
 206   // Map a size to a list index assuming that there are lists
 207   // for special, small, medium, and humongous chunks.
 208   ChunkIndex list_index(size_t size);
 209 
 210   // Map a given index to the chunk size.
 211   size_t size_by_index(ChunkIndex index) const;
 212 
 213   // Take a chunk from the ChunkManager. The chunk is expected to be in
 214   // the chunk manager (the freelist if non-humongous, the dictionary if
 215   // humongous).
 216   void remove_chunk(Metachunk* chunk);
 217 


 270                                          num_free_chunks(HumongousIndex),
 271                                          size_free_chunks_in_bytes(SpecializedIndex),
 272                                          size_free_chunks_in_bytes(SmallIndex),
 273                                          size_free_chunks_in_bytes(MediumIndex),
 274                                          size_free_chunks_in_bytes(HumongousIndex));
 275   }
 276 
 277   // Debug support
 278   void verify();
 279   void slow_verify() {
 280     if (metaspace_slow_verify) {
 281       verify();
 282     }
 283   }
 284   void locked_verify();
 285   void slow_locked_verify() {
 286     if (metaspace_slow_verify) {
 287       locked_verify();
 288     }
 289   }

 290 
 291   // Prints composition (number and total size of chunks per chunk size).
 292   void locked_print_on(outputStream* out, size_t scale = 1) const;
 293   void print_on(outputStream* out, size_t scale = 1) const;
 294 
 295   // Prints composition (number and total size of chunks per chunk size) for all
 296   //  (class and non-class) chunk managers.
 297   static void print_all_chunkmanagers_on(outputStream* out, size_t scale = 1);
 298 





 299 };
 300 
 301 class SmallBlocks : public CHeapObj<mtClass> {
 302   const static uint _small_block_max_size = sizeof(TreeChunk<Metablock,  FreeList<Metablock> >)/HeapWordSize;
 303   const static uint _small_block_min_size = sizeof(Metablock)/HeapWordSize;
 304 
 305  private:
 306   FreeList<Metablock> _small_lists[_small_block_max_size - _small_block_min_size];
 307 
 308   FreeList<Metablock>& list_at(size_t word_size) {
 309     assert(word_size >= _small_block_min_size, "There are no metaspace objects less than %u words", _small_block_min_size);
 310     return _small_lists[word_size - _small_block_min_size];
 311   }
 312 
 313  public:
 314   SmallBlocks() {
 315     for (uint i = _small_block_min_size; i < _small_block_max_size; i++) {
 316       uint k = i - _small_block_min_size;
 317       _small_lists[k].set_size(i);
 318     }


1831 }
1832 
1833 #ifdef ASSERT
1834 bool Metadebug::test_metadata_failure() {
1835   if (MetadataAllocationFailALot &&
1836       Threads::is_vm_complete()) {
1837     if (_allocation_fail_alot_count > 0) {
1838       _allocation_fail_alot_count--;
1839     } else {
1840       log_trace(gc, metaspace, freelist)("Metadata allocation failing for MetadataAllocationFailALot");
1841       init_allocation_fail_alot_count();
1842       return true;
1843     }
1844   }
1845   return false;
1846 }
1847 #endif
1848 
1849 // ChunkManager methods
1850 size_t ChunkManager::free_chunks_total_words() {
1851   return _free_chunks_total_words;
1852 }
1853 
1854 size_t ChunkManager::free_chunks_total_bytes() {
1855   return free_chunks_total_words() * BytesPerWord;
1856 }
1857 
1858 // Update internal accounting after a chunk was added
1859 void ChunkManager::account_for_added_chunk(const Metachunk* c) {
1860   assert_lock_strong(SpaceManager::expand_lock());
1861   _free_chunks_count ++;
1862   _free_chunks_total_words += c->word_size();
1863 }
1864 
1865 // Update internal accounting after a chunk was removed
1866 void ChunkManager::account_for_removed_chunk(const Metachunk* c) {
1867   assert_lock_strong(SpaceManager::expand_lock());
1868   assert(_free_chunks_count >= 1,
1869     "ChunkManager::_free_chunks_count: about to go negative (" SIZE_FORMAT ").", _free_chunks_count);
1870   assert(_free_chunks_total_words >= c->word_size(),
1871     "ChunkManager::_free_chunks_total: about to go negative"
1872      "(now: " SIZE_FORMAT ", decrement value: " SIZE_FORMAT ").", _free_chunks_total_words, c->word_size());
1873   _free_chunks_count --;
1874   _free_chunks_total_words -= c->word_size();
1875 }
1876 
1877 size_t ChunkManager::free_chunks_count() {
1878 #ifdef ASSERT
1879   if (!UseConcMarkSweepGC && !SpaceManager::expand_lock()->is_locked()) {
1880     MutexLockerEx cl(SpaceManager::expand_lock(),
1881                      Mutex::_no_safepoint_check_flag);
1882     // This lock is only needed in debug because the verification
1883     // of the _free_chunks_totals walks the list of free chunks
1884     slow_locked_verify_free_chunks_count();
1885   }
1886 #endif
1887   return _free_chunks_count;
1888 }
1889 
1890 ChunkIndex ChunkManager::list_index(size_t size) {
1891   if (size_by_index(SpecializedIndex) == size) {
1892     return SpecializedIndex;
1893   }
1894   if (size_by_index(SmallIndex) == size) {
1895     return SmallIndex;
1896   }
1897   const size_t med_size = size_by_index(MediumIndex);
1898   if (med_size == size) {
1899     return MediumIndex;
1900   }
1901 
1902   assert(size > med_size, "Not a humongous chunk");
1903   return HumongousIndex;
1904 }
1905 
1906 size_t ChunkManager::size_by_index(ChunkIndex index) const {
1907   index_bounds_check(index);
1908   assert(index != HumongousIndex, "Do not call for humongous chunks.");
1909   return _free_chunks[index].size();
1910 }
1911 
1912 void ChunkManager::locked_verify_free_chunks_total() {
1913   assert_lock_strong(SpaceManager::expand_lock());
1914   assert(sum_free_chunks() == _free_chunks_total_words,
1915          "_free_chunks_total " SIZE_FORMAT " is not the"
1916          " same as sum " SIZE_FORMAT, _free_chunks_total_words,
1917          sum_free_chunks());
1918 }
1919 






1920 void ChunkManager::locked_verify_free_chunks_count() {
1921   assert_lock_strong(SpaceManager::expand_lock());
1922   assert(sum_free_chunks_count() == _free_chunks_count,
1923          "_free_chunks_count " SIZE_FORMAT " is not the"
1924          " same as sum " SIZE_FORMAT, _free_chunks_count,
1925          sum_free_chunks_count());
1926 }
1927 








1928 void ChunkManager::verify() {
1929   MutexLockerEx cl(SpaceManager::expand_lock(),
1930                      Mutex::_no_safepoint_check_flag);
1931   locked_verify();
1932 }
1933 
1934 void ChunkManager::locked_verify() {
1935   locked_verify_free_chunks_count();
1936   locked_verify_free_chunks_total();
1937 }
1938 












1939 ChunkList* ChunkManager::free_chunks(ChunkIndex index) {
1940   assert(index == SpecializedIndex || index == SmallIndex || index == MediumIndex,
1941          "Bad index: %d", (int)index);
1942 
1943   return &_free_chunks[index];
1944 }
1945 
1946 // These methods that sum the free chunk lists are used in printing
1947 // methods that are used in product builds.
1948 size_t ChunkManager::sum_free_chunks() {
1949   assert_lock_strong(SpaceManager::expand_lock());
1950   size_t result = 0;
1951   for (ChunkIndex i = ZeroIndex; i < NumberOfFreeLists; i = next_chunk_index(i)) {
1952     ChunkList* list = free_chunks(i);
1953 
1954     if (list == NULL) {
1955       continue;
1956     }
1957 
1958     result = result + list->count() * list->size();


2114     // by the call to return_chunk_at_head();
2115     Metachunk* next = cur->next();
2116     if (log.is_enabled()) { // tracing
2117       num_chunks_returned ++;
2118       size_chunks_returned += cur->word_size();
2119     }
2120     return_single_chunk(index, cur);
2121     cur = next;
2122   }
2123   if (log.is_enabled()) { // tracing
2124     log.print("returned %u %s chunks to freelist, total word size " SIZE_FORMAT ".",
2125         num_chunks_returned, chunk_size_name(index), size_chunks_returned);
2126     if (index != HumongousIndex) {
2127       log.print("updated freelist count: " SIZE_FORMAT ".", free_chunks(index)->size());
2128     } else {
2129       log.print("updated dictionary count " SIZE_FORMAT ".", _humongous_dictionary.total_count());
2130     }
2131   }
2132 }
2133 




2134 void ChunkManager::locked_get_statistics(ChunkManagerStatistics* stat) const {
2135   assert_lock_strong(SpaceManager::expand_lock());
2136   for (ChunkIndex i = ZeroIndex; i < NumberOfFreeLists; i = next_chunk_index(i)) {
2137     stat->num_by_type[i] = num_free_chunks(i);
2138     stat->single_size_by_type[i] = size_by_index(i);
2139     stat->total_size_by_type[i] = size_free_chunks_in_bytes(i);
2140   }
2141   stat->num_humongous_chunks = num_free_chunks(HumongousIndex);
2142   stat->total_size_humongous_chunks = size_free_chunks_in_bytes(HumongousIndex);
2143 }
2144 
2145 void ChunkManager::get_statistics(ChunkManagerStatistics* stat) const {
2146   MutexLockerEx cl(SpaceManager::expand_lock(),
2147                    Mutex::_no_safepoint_check_flag);
2148   locked_get_statistics(stat);
2149 }
2150 
2151 void ChunkManager::print_statistics(const ChunkManagerStatistics* stat, outputStream* out, size_t scale) {
2152   size_t total = 0;
2153   assert(scale == 1 || scale == K || scale == M || scale == G, "Invalid scale");


2167   }
2168 
2169 
2170   total += stat->total_size_humongous_chunks;
2171 
2172   if (scale == 1) {
2173     out->print_cr("  " SIZE_FORMAT " humongous chunks, total " SIZE_FORMAT " bytes",
2174     stat->num_humongous_chunks, stat->total_size_humongous_chunks);
2175 
2176     out->print_cr("  total size: " SIZE_FORMAT " bytes.", total);
2177   } else {
2178     out->print_cr("  " SIZE_FORMAT " humongous chunks, total %.2f%s",
2179     stat->num_humongous_chunks,
2180     (float)stat->total_size_humongous_chunks / scale, unit);
2181 
2182     out->print_cr("  total size: %.2f%s.", (float)total / scale, unit);
2183   }
2184 
2185 }
2186 
2187 void ChunkManager::locked_print_on(outputStream* out, size_t scale = 1) const {
2188   assert_lock_strong(SpaceManager::expand_lock());
2189   ChunkManagerStatistics stat;
2190   locked_get_statistics(&stat);
2191   print_statistics(&stat, out, scale);
2192 }
2193 
2194 void ChunkManager::print_on(outputStream* out, size_t scale = 1) const {

2195   ChunkManagerStatistics stat;
2196   {
2197     // Print out of lock protection
2198     MutexLockerEx cl(SpaceManager::expand_lock(),
2199                      Mutex::_no_safepoint_check_flag);
2200     locked_get_statistics(&stat);
2201   }
2202   print_statistics(&stat, out, scale);
2203 }
2204 
2205 void ChunkManager::print_all_chunkmanagers_on(outputStream* out, size_t scale) {
2206   assert(scale == 1 || scale == K || scale == M || scale == G, "Invalid scale");
2207 
2208   out->print_cr("Chunkmanager (non-class):");
2209   const ChunkManager* const non_class_cm = Metaspace::chunk_manager_metadata();
2210   if (non_class_cm != NULL) {
2211     non_class_cm->print_on(out, scale);

2212   } else {
2213     out->print_cr("unavailable.");
2214   }
2215   out->print_cr("Chunkmanager (class):");
2216   const ChunkManager* const class_cm = Metaspace::chunk_manager_class();
2217   if (class_cm != NULL) {
2218     class_cm->print_on(out, scale);

2219   } else {
2220     out->print_cr("unavailable.");
2221   }
2222 }
2223 
2224 // SpaceManager methods
2225 
2226 size_t SpaceManager::adjust_initial_chunk_size(size_t requested, bool is_class_space) {
2227   size_t chunk_sizes[] = {
2228       specialized_chunk_size(is_class_space),
2229       small_chunk_size(is_class_space),
2230       medium_chunk_size(is_class_space)
2231   };
2232 
2233   // Adjust up to one of the fixed chunk sizes ...
2234   for (size_t i = 0; i < ARRAY_SIZE(chunk_sizes); i++) {
2235     if (requested <= chunk_sizes[i]) {
2236       return chunk_sizes[i];
2237     }
2238   }


2365       chunk = chunk->next();
2366     }
2367   }
2368   return used;
2369 }
2370 
2371 void SpaceManager::locked_print_chunks_in_use_on(outputStream* st) const {
2372 
2373   for (ChunkIndex i = ZeroIndex; i < NumberOfInUseLists; i = next_chunk_index(i)) {
2374     Metachunk* chunk = chunks_in_use(i);
2375     st->print("SpaceManager: %s " PTR_FORMAT,
2376                  chunk_size_name(i), p2i(chunk));
2377     if (chunk != NULL) {
2378       st->print_cr(" free " SIZE_FORMAT,
2379                    chunk->free_word_size());
2380     } else {
2381       st->cr();
2382     }
2383   }
2384 
2385   chunk_manager()->print_on(st, 1024);

2386 }
2387 
2388 size_t SpaceManager::calc_chunk_size(size_t word_size) {
2389 
2390   // Decide between a small chunk and a medium chunk.  Up to
2391   // _small_chunk_limit small chunks can be allocated.
2392   // After that a medium chunk is preferred.
2393   size_t chunk_word_size;
2394 
2395   // Special case for anonymous metadata space.
2396   // Anonymous metadata space is usually small, with majority within 1K - 2K range and
2397   // rarely about 4K (64-bits JVM).
2398   // Instead of jumping to SmallChunk after initial chunk exhausted, keeping allocation
2399   // from SpecializeChunk up to _anon_metadata_specialize_chunk_limit (4) reduces space waste
2400   // from 60+% to around 30%.
2401   if (_space_type == Metaspace::AnonymousMetaspaceType &&
2402       _mdtype == Metaspace::NonClassType &&
2403       sum_count_in_chunks_in_use(SpecializedIndex) < _anon_metadata_specialize_chunk_limit &&
2404       word_size + Metachunk::overhead() <= SpecializedChunk) {
2405     return SpecializedChunk;


3283 
3284   if (Metaspace::using_class_space()) {
3285     _out->print("                  Class data ");
3286     _total_anon_class.print_on(_out, _scale);
3287   }
3288 }
3289 
3290 void MetaspaceAux::print_metadata_for_nmt(outputStream* out, size_t scale) {
3291   const char* unit = scale_unit(scale);
3292   out->print_cr("Metaspaces:");
3293   out->print_cr("  Metadata space: reserved=" SIZE_FORMAT_W(10) "%s committed=" SIZE_FORMAT_W(10) "%s",
3294     reserved_bytes(Metaspace::NonClassType) / scale, unit,
3295     committed_bytes(Metaspace::NonClassType) / scale, unit);
3296   if (Metaspace::using_class_space()) {
3297     out->print_cr("  Class    space: reserved=" SIZE_FORMAT_W(10) "%s committed=" SIZE_FORMAT_W(10) "%s",
3298     reserved_bytes(Metaspace::ClassType) / scale, unit,
3299     committed_bytes(Metaspace::ClassType) / scale, unit);
3300   }
3301 
3302   out->cr();
3303   ChunkManager::print_all_chunkmanagers_on(out, scale);
3304 
3305   out->cr();
3306   out->print_cr("Per-classloader metadata:");
3307   out->cr();
3308 
3309   PrintCLDMetaspaceInfoClosure cl(out, scale);
3310   ClassLoaderDataGraph::cld_do(&cl);
3311 }
3312 
3313 
3314 // Dump global metaspace things from the end of ClassLoaderDataGraph
3315 void MetaspaceAux::dump(outputStream* out) {
3316   out->print_cr("All Metaspace:");
3317   out->print("data space: "); print_on(out, Metaspace::NonClassType);
3318   out->print("class space: "); print_on(out, Metaspace::ClassType);
3319   print_waste(out);
3320 }
3321 
3322 // Prints an ASCII representation of the given space.
3323 void MetaspaceAux::print_metaspace_map(outputStream* out, Metaspace::MetadataType mdtype) {


3965 }
3966 
3967 void Metaspace::report_metadata_oome(ClassLoaderData* loader_data, size_t word_size, MetaspaceObj::Type type, MetadataType mdtype, TRAPS) {
3968   tracer()->report_metadata_oom(loader_data, word_size, type, mdtype);
3969 
3970   // If result is still null, we are out of memory.
3971   Log(gc, metaspace, freelist) log;
3972   if (log.is_info()) {
3973     log.info("Metaspace (%s) allocation failed for size " SIZE_FORMAT,
3974              is_class_space_allocation(mdtype) ? "class" : "data", word_size);
3975     ResourceMark rm;
3976     if (log.is_debug()) {
3977       if (loader_data->metaspace_or_null() != NULL) {
3978         LogStream ls(log.debug());
3979         loader_data->dump(&ls);
3980       }
3981     }
3982     LogStream ls(log.info());
3983     MetaspaceAux::dump(&ls);
3984     MetaspaceAux::print_metaspace_map(&ls, mdtype);
3985     ChunkManager::print_all_chunkmanagers_on(&ls);
3986   }
3987 
3988   bool out_of_compressed_class_space = false;
3989   if (is_class_space_allocation(mdtype)) {
3990     Metaspace* metaspace = loader_data->metaspace_non_null();
3991     out_of_compressed_class_space =
3992       MetaspaceAux::committed_bytes(Metaspace::ClassType) +
3993       (metaspace->class_chunk_size(word_size) * BytesPerWord) >
3994       CompressedClassSpaceSize;
3995   }
3996 
3997   // -XX:+HeapDumpOnOutOfMemoryError and -XX:OnOutOfMemoryError support
3998   const char* space_string = out_of_compressed_class_space ?
3999     "Compressed class space" : "Metaspace";
4000 
4001   report_java_out_of_memory(space_string);
4002 
4003   if (JvmtiExport::should_post_resource_exhausted()) {
4004     JvmtiExport::post_resource_exhausted(
4005         JVMTI_RESOURCE_EXHAUSTED_OOM_ERROR,


< prev index next >