Mercurial > hg > graal-compiler
comparison src/share/vm/memory/metaspace.cpp @ 10183:868d87ed63c8
8008966: NPG: Inefficient Metaspace counter functions cause large young GC regressions
Reviewed-by: mgerdin, coleenp
author | jmasa |
---|---|
date | Tue, 12 Feb 2013 14:15:45 -0800 |
parents | c23dbf0e8ab7 |
children | 601183f604b2 |
comparison
equal
deleted
inserted
replaced
10182:5c93c1f61226 | 10183:868d87ed63c8 |
---|---|
45 typedef BinaryTreeDictionary<Metachunk, FreeList> ChunkTreeDictionary; | 45 typedef BinaryTreeDictionary<Metachunk, FreeList> ChunkTreeDictionary; |
46 // Define this macro to enable slow integrity checking of | 46 // Define this macro to enable slow integrity checking of |
47 // the free chunk lists | 47 // the free chunk lists |
48 const bool metaspace_slow_verify = false; | 48 const bool metaspace_slow_verify = false; |
49 | 49 |
50 | |
51 // Parameters for stress mode testing | 50 // Parameters for stress mode testing |
52 const uint metadata_deallocate_a_lot_block = 10; | 51 const uint metadata_deallocate_a_lot_block = 10; |
53 const uint metadata_deallocate_a_lock_chunk = 3; | 52 const uint metadata_deallocate_a_lock_chunk = 3; |
54 size_t const allocation_from_dictionary_limit = 64 * K; | 53 size_t const allocation_from_dictionary_limit = 64 * K; |
55 | 54 |
218 void locked_print_sum_free_chunks(outputStream* st); | 217 void locked_print_sum_free_chunks(outputStream* st); |
219 | 218 |
220 void print_on(outputStream* st); | 219 void print_on(outputStream* st); |
221 }; | 220 }; |
222 | 221 |
223 | |
224 // Used to manage the free list of Metablocks (a block corresponds | 222 // Used to manage the free list of Metablocks (a block corresponds |
225 // to the allocation of a quantum of metadata). | 223 // to the allocation of a quantum of metadata). |
226 class BlockFreelist VALUE_OBJ_CLASS_SPEC { | 224 class BlockFreelist VALUE_OBJ_CLASS_SPEC { |
227 BlockTreeDictionary* _dictionary; | 225 BlockTreeDictionary* _dictionary; |
228 static Metablock* initialize_free_chunk(MetaWord* p, size_t word_size); | 226 static Metablock* initialize_free_chunk(MetaWord* p, size_t word_size); |
296 | 294 |
297 // Accessors | 295 // Accessors |
298 MemRegion* reserved() { return &_reserved; } | 296 MemRegion* reserved() { return &_reserved; } |
299 VirtualSpace* virtual_space() const { return (VirtualSpace*) &_virtual_space; } | 297 VirtualSpace* virtual_space() const { return (VirtualSpace*) &_virtual_space; } |
300 | 298 |
301 // Returns true if "word_size" is available in the virtual space | 299 // Returns true if "word_size" is available in the VirtualSpace |
302 bool is_available(size_t word_size) { return _top + word_size <= end(); } | 300 bool is_available(size_t word_size) { return _top + word_size <= end(); } |
303 | 301 |
304 MetaWord* top() const { return _top; } | 302 MetaWord* top() const { return _top; } |
305 void inc_top(size_t word_size) { _top += word_size; } | 303 void inc_top(size_t word_size) { _top += word_size; } |
306 | 304 |
311 #endif | 309 #endif |
312 | 310 |
313 // used and capacity in this single entry in the list | 311 // used and capacity in this single entry in the list |
314 size_t used_words_in_vs() const; | 312 size_t used_words_in_vs() const; |
315 size_t capacity_words_in_vs() const; | 313 size_t capacity_words_in_vs() const; |
314 size_t free_words_in_vs() const; | |
316 | 315 |
317 bool initialize(); | 316 bool initialize(); |
318 | 317 |
319 // get space from the virtual space | 318 // get space from the virtual space |
320 Metachunk* take_from_committed(size_t chunk_word_size); | 319 Metachunk* take_from_committed(size_t chunk_word_size); |
447 | 446 |
448 public: | 447 public: |
449 VirtualSpaceList(size_t word_size); | 448 VirtualSpaceList(size_t word_size); |
450 VirtualSpaceList(ReservedSpace rs); | 449 VirtualSpaceList(ReservedSpace rs); |
451 | 450 |
451 size_t free_bytes(); | |
452 | |
452 Metachunk* get_new_chunk(size_t word_size, | 453 Metachunk* get_new_chunk(size_t word_size, |
453 size_t grow_chunks_by_words, | 454 size_t grow_chunks_by_words, |
454 size_t medium_chunk_bunch); | 455 size_t medium_chunk_bunch); |
455 | 456 |
456 // Get the first chunk for a Metaspace. Used for | 457 // Get the first chunk for a Metaspace. Used for |
577 // If class space manager, small chunks are unlimited | 578 // If class space manager, small chunks are unlimited |
578 static uint const _small_chunk_limit; | 579 static uint const _small_chunk_limit; |
579 bool has_small_chunk_limit() { return !vs_list()->is_class(); } | 580 bool has_small_chunk_limit() { return !vs_list()->is_class(); } |
580 | 581 |
581 // Sum of all space in allocated chunks | 582 // Sum of all space in allocated chunks |
582 size_t _allocation_total; | 583 size_t _allocated_blocks_words; |
584 | |
585 // Sum of all allocated chunks | |
586 size_t _allocated_chunks_words; | |
587 size_t _allocated_chunks_count; | |
583 | 588 |
584 // Free lists of blocks are per SpaceManager since they | 589 // Free lists of blocks are per SpaceManager since they |
585 // are assumed to be in chunks in use by the SpaceManager | 590 // are assumed to be in chunks in use by the SpaceManager |
586 // and all chunks in use by a SpaceManager are freed when | 591 // and all chunks in use by a SpaceManager are freed when |
587 // the class loader using the SpaceManager is collected. | 592 // the class loader using the SpaceManager is collected. |
633 size_t specialized_chunk_size() { return SpecializedChunk; } | 638 size_t specialized_chunk_size() { return SpecializedChunk; } |
634 size_t small_chunk_size() { return (size_t) vs_list()->is_class() ? ClassSmallChunk : SmallChunk; } | 639 size_t small_chunk_size() { return (size_t) vs_list()->is_class() ? ClassSmallChunk : SmallChunk; } |
635 size_t medium_chunk_size() { return (size_t) vs_list()->is_class() ? ClassMediumChunk : MediumChunk; } | 640 size_t medium_chunk_size() { return (size_t) vs_list()->is_class() ? ClassMediumChunk : MediumChunk; } |
636 size_t medium_chunk_bunch() { return medium_chunk_size() * MediumChunkMultiple; } | 641 size_t medium_chunk_bunch() { return medium_chunk_size() * MediumChunkMultiple; } |
637 | 642 |
638 size_t allocation_total() const { return _allocation_total; } | 643 size_t allocated_blocks_words() const { return _allocated_blocks_words; } |
639 void inc_allocation_total(size_t v) { Atomic::add_ptr(v, &_allocation_total); } | 644 size_t allocated_blocks_bytes() const { return _allocated_blocks_words * BytesPerWord; } |
645 size_t allocated_chunks_words() const { return _allocated_chunks_words; } | |
646 size_t allocated_chunks_count() const { return _allocated_chunks_count; } | |
647 | |
640 bool is_humongous(size_t word_size) { return word_size > medium_chunk_size(); } | 648 bool is_humongous(size_t word_size) { return word_size > medium_chunk_size(); } |
641 | 649 |
642 static Mutex* expand_lock() { return _expand_lock; } | 650 static Mutex* expand_lock() { return _expand_lock; } |
651 | |
652 // Increment the per Metaspace and global running sums for Metachunks | |
653 // by the given size. This is used when a Metachunk to added to | |
654 // the in-use list. | |
655 void inc_size_metrics(size_t words); | |
656 // Increment the per Metaspace and global running sums Metablocks by the given | |
657 // size. This is used when a Metablock is allocated. | |
658 void inc_used_metrics(size_t words); | |
659 // Delete the portion of the running sums for this SpaceManager. That is, | |
660 // the globals running sums for the Metachunks and Metablocks are | |
661 // decremented for all the Metachunks in-use by this SpaceManager. | |
662 void dec_total_from_size_metrics(); | |
643 | 663 |
644 // Set the sizes for the initial chunks. | 664 // Set the sizes for the initial chunks. |
645 void get_initial_chunk_sizes(Metaspace::MetaspaceType type, | 665 void get_initial_chunk_sizes(Metaspace::MetaspaceType type, |
646 size_t* chunk_word_size, | 666 size_t* chunk_word_size, |
647 size_t* class_chunk_word_size); | 667 size_t* class_chunk_word_size); |
684 | 704 |
685 void verify(); | 705 void verify(); |
686 void verify_chunk_size(Metachunk* chunk); | 706 void verify_chunk_size(Metachunk* chunk); |
687 NOT_PRODUCT(void mangle_freed_chunks();) | 707 NOT_PRODUCT(void mangle_freed_chunks();) |
688 #ifdef ASSERT | 708 #ifdef ASSERT |
689 void verify_allocation_total(); | 709 void verify_allocated_blocks_words(); |
690 #endif | 710 #endif |
691 }; | 711 }; |
692 | 712 |
693 uint const SpaceManager::_small_chunk_limit = 4; | 713 uint const SpaceManager::_small_chunk_limit = 4; |
694 | 714 |
795 // Space committed in the VirtualSpace | 815 // Space committed in the VirtualSpace |
796 size_t VirtualSpaceNode::capacity_words_in_vs() const { | 816 size_t VirtualSpaceNode::capacity_words_in_vs() const { |
797 return pointer_delta(end(), bottom(), sizeof(MetaWord)); | 817 return pointer_delta(end(), bottom(), sizeof(MetaWord)); |
798 } | 818 } |
799 | 819 |
820 size_t VirtualSpaceNode::free_words_in_vs() const { | |
821 return pointer_delta(end(), top(), sizeof(MetaWord)); | |
822 } | |
800 | 823 |
801 // Allocates the chunk from the virtual space only. | 824 // Allocates the chunk from the virtual space only. |
802 // This interface is also used internally for debugging. Not all | 825 // This interface is also used internally for debugging. Not all |
803 // chunks removed here are necessarily used for allocation. | 826 // chunks removed here are necessarily used for allocation. |
804 Metachunk* VirtualSpaceNode::take_from_committed(size_t chunk_word_size) { | 827 Metachunk* VirtualSpaceNode::take_from_committed(size_t chunk_word_size) { |
1067 _chunk_manager.free_chunks(SpecializedIndex)->set_size(SpecializedChunk); | 1090 _chunk_manager.free_chunks(SpecializedIndex)->set_size(SpecializedChunk); |
1068 _chunk_manager.free_chunks(SmallIndex)->set_size(ClassSmallChunk); | 1091 _chunk_manager.free_chunks(SmallIndex)->set_size(ClassSmallChunk); |
1069 _chunk_manager.free_chunks(MediumIndex)->set_size(ClassMediumChunk); | 1092 _chunk_manager.free_chunks(MediumIndex)->set_size(ClassMediumChunk); |
1070 assert(succeeded, " VirtualSpaceList initialization should not fail"); | 1093 assert(succeeded, " VirtualSpaceList initialization should not fail"); |
1071 link_vs(class_entry, rs.size()/BytesPerWord); | 1094 link_vs(class_entry, rs.size()/BytesPerWord); |
1095 } | |
1096 | |
1097 size_t VirtualSpaceList::free_bytes() { | |
1098 return virtual_space_list()->free_words_in_vs() * BytesPerWord; | |
1072 } | 1099 } |
1073 | 1100 |
1074 // Allocate another meta virtual space and add it to the list. | 1101 // Allocate another meta virtual space and add it to the list. |
1075 bool VirtualSpaceList::grow_vs(size_t vs_word_size) { | 1102 bool VirtualSpaceList::grow_vs(size_t vs_word_size) { |
1076 assert_lock_strong(SpaceManager::expand_lock()); | 1103 assert_lock_strong(SpaceManager::expand_lock()); |
1209 // gen had bounds, MinMetaspaceExpansion and MaxMetaspaceExpansion. The | 1236 // gen had bounds, MinMetaspaceExpansion and MaxMetaspaceExpansion. The |
1210 // metaspace policy uses those as the small and large steps for the HWM. | 1237 // metaspace policy uses those as the small and large steps for the HWM. |
1211 // | 1238 // |
1212 // After the GC the compute_new_size() for MetaspaceGC is called to | 1239 // After the GC the compute_new_size() for MetaspaceGC is called to |
1213 // resize the capacity of the metaspaces. The current implementation | 1240 // resize the capacity of the metaspaces. The current implementation |
1214 // is based on the flags MinMetaspaceFreeRatio and MaxHeapFreeRatio used | 1241 // is based on the flags MinMetaspaceFreeRatio and MaxMetaspaceFreeRatio used |
1215 // to resize the Java heap by some GC's. New flags can be implemented | 1242 // to resize the Java heap by some GC's. New flags can be implemented |
1216 // if really needed. MinHeapFreeRatio is used to calculate how much | 1243 // if really needed. MinMetaspaceFreeRatio is used to calculate how much |
1217 // free space is desirable in the metaspace capacity to decide how much | 1244 // free space is desirable in the metaspace capacity to decide how much |
1218 // to increase the HWM. MaxMetaspaceFreeRatio is used to decide how much | 1245 // to increase the HWM. MaxMetaspaceFreeRatio is used to decide how much |
1219 // free space is desirable in the metaspace capacity before decreasing | 1246 // free space is desirable in the metaspace capacity before decreasing |
1220 // the HWM. | 1247 // the HWM. |
1221 | 1248 |
1246 } | 1273 } |
1247 return delta_words; | 1274 return delta_words; |
1248 } | 1275 } |
1249 | 1276 |
1250 bool MetaspaceGC::should_expand(VirtualSpaceList* vsl, size_t word_size) { | 1277 bool MetaspaceGC::should_expand(VirtualSpaceList* vsl, size_t word_size) { |
1278 | |
1279 size_t committed_capacity_bytes = MetaspaceAux::allocated_capacity_bytes(); | |
1251 // If the user wants a limit, impose one. | 1280 // If the user wants a limit, impose one. |
1281 size_t max_metaspace_size_bytes = MaxMetaspaceSize; | |
1282 size_t metaspace_size_bytes = MetaspaceSize; | |
1252 if (!FLAG_IS_DEFAULT(MaxMetaspaceSize) && | 1283 if (!FLAG_IS_DEFAULT(MaxMetaspaceSize) && |
1253 MetaspaceAux::reserved_in_bytes() >= MaxMetaspaceSize) { | 1284 MetaspaceAux::reserved_in_bytes() >= MaxMetaspaceSize) { |
1254 return false; | 1285 return false; |
1255 } | 1286 } |
1256 | 1287 |
1258 // metadata virtual space. | 1289 // metadata virtual space. |
1259 if (vsl == Metaspace::class_space_list()) return true; | 1290 if (vsl == Metaspace::class_space_list()) return true; |
1260 | 1291 |
1261 // If this is part of an allocation after a GC, expand | 1292 // If this is part of an allocation after a GC, expand |
1262 // unconditionally. | 1293 // unconditionally. |
1263 if(MetaspaceGC::expand_after_GC()) { | 1294 if (MetaspaceGC::expand_after_GC()) { |
1264 return true; | 1295 return true; |
1265 } | 1296 } |
1266 | 1297 |
1267 size_t metaspace_size_words = MetaspaceSize / BytesPerWord; | 1298 |
1268 | 1299 |
1269 // If the capacity is below the minimum capacity, allow the | 1300 // If the capacity is below the minimum capacity, allow the |
1270 // expansion. Also set the high-water-mark (capacity_until_GC) | 1301 // expansion. Also set the high-water-mark (capacity_until_GC) |
1271 // to that minimum capacity so that a GC will not be induced | 1302 // to that minimum capacity so that a GC will not be induced |
1272 // until that minimum capacity is exceeded. | 1303 // until that minimum capacity is exceeded. |
1273 if (vsl->capacity_words_sum() < metaspace_size_words || | 1304 if (committed_capacity_bytes < metaspace_size_bytes || |
1274 capacity_until_GC() == 0) { | 1305 capacity_until_GC() == 0) { |
1275 set_capacity_until_GC(metaspace_size_words); | 1306 set_capacity_until_GC(metaspace_size_bytes); |
1276 return true; | 1307 return true; |
1277 } else { | 1308 } else { |
1278 if (vsl->capacity_words_sum() < capacity_until_GC()) { | 1309 if (committed_capacity_bytes < capacity_until_GC()) { |
1279 return true; | 1310 return true; |
1280 } else { | 1311 } else { |
1281 if (TraceMetadataChunkAllocation && Verbose) { | 1312 if (TraceMetadataChunkAllocation && Verbose) { |
1282 gclog_or_tty->print_cr(" allocation request size " SIZE_FORMAT | 1313 gclog_or_tty->print_cr(" allocation request size " SIZE_FORMAT |
1283 " capacity_until_GC " SIZE_FORMAT | 1314 " capacity_until_GC " SIZE_FORMAT |
1284 " capacity_words_sum " SIZE_FORMAT | 1315 " allocated_capacity_bytes " SIZE_FORMAT, |
1285 " used_words_sum " SIZE_FORMAT | |
1286 " free chunks " SIZE_FORMAT | |
1287 " free chunks count %d", | |
1288 word_size, | 1316 word_size, |
1289 capacity_until_GC(), | 1317 capacity_until_GC(), |
1290 vsl->capacity_words_sum(), | 1318 MetaspaceAux::allocated_capacity_bytes()); |
1291 vsl->used_words_sum(), | |
1292 vsl->chunk_manager()->free_chunks_total(), | |
1293 vsl->chunk_manager()->free_chunks_count()); | |
1294 } | 1319 } |
1295 return false; | 1320 return false; |
1296 } | 1321 } |
1297 } | 1322 } |
1298 } | 1323 } |
1299 | 1324 |
1300 // Variables are in bytes | 1325 |
1301 | 1326 |
1302 void MetaspaceGC::compute_new_size() { | 1327 void MetaspaceGC::compute_new_size() { |
1303 assert(_shrink_factor <= 100, "invalid shrink factor"); | 1328 assert(_shrink_factor <= 100, "invalid shrink factor"); |
1304 uint current_shrink_factor = _shrink_factor; | 1329 uint current_shrink_factor = _shrink_factor; |
1305 _shrink_factor = 0; | 1330 _shrink_factor = 0; |
1306 | 1331 |
1307 VirtualSpaceList *vsl = Metaspace::space_list(); | 1332 // Until a faster way of calculating the "used" quantity is implemented, |
1308 | 1333 // use "capacity". |
1309 size_t capacity_after_gc = vsl->capacity_bytes_sum(); | 1334 const size_t used_after_gc = MetaspaceAux::allocated_capacity_bytes(); |
1310 // Check to see if these two can be calculated without walking the CLDG | 1335 const size_t capacity_until_GC = MetaspaceGC::capacity_until_GC(); |
1311 size_t used_after_gc = vsl->used_bytes_sum(); | |
1312 size_t capacity_until_GC = vsl->capacity_bytes_sum(); | |
1313 size_t free_after_gc = capacity_until_GC - used_after_gc; | |
1314 | 1336 |
1315 const double minimum_free_percentage = MinMetaspaceFreeRatio / 100.0; | 1337 const double minimum_free_percentage = MinMetaspaceFreeRatio / 100.0; |
1316 const double maximum_used_percentage = 1.0 - minimum_free_percentage; | 1338 const double maximum_used_percentage = 1.0 - minimum_free_percentage; |
1317 | 1339 |
1318 const double min_tmp = used_after_gc / maximum_used_percentage; | 1340 const double min_tmp = used_after_gc / maximum_used_percentage; |
1321 // Don't shrink less than the initial generation size | 1343 // Don't shrink less than the initial generation size |
1322 minimum_desired_capacity = MAX2(minimum_desired_capacity, | 1344 minimum_desired_capacity = MAX2(minimum_desired_capacity, |
1323 MetaspaceSize); | 1345 MetaspaceSize); |
1324 | 1346 |
1325 if (PrintGCDetails && Verbose) { | 1347 if (PrintGCDetails && Verbose) { |
1326 const double free_percentage = ((double)free_after_gc) / capacity_until_GC; | |
1327 gclog_or_tty->print_cr("\nMetaspaceGC::compute_new_size: "); | 1348 gclog_or_tty->print_cr("\nMetaspaceGC::compute_new_size: "); |
1328 gclog_or_tty->print_cr(" " | 1349 gclog_or_tty->print_cr(" " |
1329 " minimum_free_percentage: %6.2f" | 1350 " minimum_free_percentage: %6.2f" |
1330 " maximum_used_percentage: %6.2f", | 1351 " maximum_used_percentage: %6.2f", |
1331 minimum_free_percentage, | 1352 minimum_free_percentage, |
1332 maximum_used_percentage); | 1353 maximum_used_percentage); |
1333 double d_free_after_gc = free_after_gc / (double) K; | |
1334 gclog_or_tty->print_cr(" " | 1354 gclog_or_tty->print_cr(" " |
1335 " free_after_gc : %6.1fK" | 1355 " used_after_gc : %6.1fKB", |
1336 " used_after_gc : %6.1fK" | 1356 used_after_gc / (double) K); |
1337 " capacity_after_gc : %6.1fK" | 1357 } |
1338 " metaspace HWM : %6.1fK", | 1358 |
1339 free_after_gc / (double) K, | 1359 |
1340 used_after_gc / (double) K, | 1360 size_t shrink_bytes = 0; |
1341 capacity_after_gc / (double) K, | |
1342 capacity_until_GC / (double) K); | |
1343 gclog_or_tty->print_cr(" " | |
1344 " free_percentage: %6.2f", | |
1345 free_percentage); | |
1346 } | |
1347 | |
1348 | |
1349 if (capacity_until_GC < minimum_desired_capacity) { | 1361 if (capacity_until_GC < minimum_desired_capacity) { |
1350 // If we have less capacity below the metaspace HWM, then | 1362 // If we have less capacity below the metaspace HWM, then |
1351 // increment the HWM. | 1363 // increment the HWM. |
1352 size_t expand_bytes = minimum_desired_capacity - capacity_until_GC; | 1364 size_t expand_bytes = minimum_desired_capacity - capacity_until_GC; |
1353 // Don't expand unless it's significant | 1365 // Don't expand unless it's significant |
1354 if (expand_bytes >= MinMetaspaceExpansion) { | 1366 if (expand_bytes >= MinMetaspaceExpansion) { |
1355 size_t expand_words = expand_bytes / BytesPerWord; | 1367 MetaspaceGC::set_capacity_until_GC(capacity_until_GC + expand_bytes); |
1356 MetaspaceGC::inc_capacity_until_GC(expand_words); | |
1357 } | 1368 } |
1358 if (PrintGCDetails && Verbose) { | 1369 if (PrintGCDetails && Verbose) { |
1359 size_t new_capacity_until_GC = MetaspaceGC::capacity_until_GC_in_bytes(); | 1370 size_t new_capacity_until_GC = capacity_until_GC; |
1360 gclog_or_tty->print_cr(" expanding:" | 1371 gclog_or_tty->print_cr(" expanding:" |
1361 " minimum_desired_capacity: %6.1fK" | 1372 " minimum_desired_capacity: %6.1fKB" |
1362 " expand_words: %6.1fK" | 1373 " expand_bytes: %6.1fKB" |
1363 " MinMetaspaceExpansion: %6.1fK" | 1374 " MinMetaspaceExpansion: %6.1fKB" |
1364 " new metaspace HWM: %6.1fK", | 1375 " new metaspace HWM: %6.1fKB", |
1365 minimum_desired_capacity / (double) K, | 1376 minimum_desired_capacity / (double) K, |
1366 expand_bytes / (double) K, | 1377 expand_bytes / (double) K, |
1367 MinMetaspaceExpansion / (double) K, | 1378 MinMetaspaceExpansion / (double) K, |
1368 new_capacity_until_GC / (double) K); | 1379 new_capacity_until_GC / (double) K); |
1369 } | 1380 } |
1370 return; | 1381 return; |
1371 } | 1382 } |
1372 | 1383 |
1373 // No expansion, now see if we want to shrink | 1384 // No expansion, now see if we want to shrink |
1374 size_t shrink_words = 0; | |
1375 // We would never want to shrink more than this | 1385 // We would never want to shrink more than this |
1376 size_t max_shrink_words = capacity_until_GC - minimum_desired_capacity; | 1386 size_t max_shrink_bytes = capacity_until_GC - minimum_desired_capacity; |
1377 assert(max_shrink_words >= 0, err_msg("max_shrink_words " SIZE_FORMAT, | 1387 assert(max_shrink_bytes >= 0, err_msg("max_shrink_bytes " SIZE_FORMAT, |
1378 max_shrink_words)); | 1388 max_shrink_bytes)); |
1379 | 1389 |
1380 // Should shrinking be considered? | 1390 // Should shrinking be considered? |
1381 if (MaxMetaspaceFreeRatio < 100) { | 1391 if (MaxMetaspaceFreeRatio < 100) { |
1382 const double maximum_free_percentage = MaxMetaspaceFreeRatio / 100.0; | 1392 const double maximum_free_percentage = MaxMetaspaceFreeRatio / 100.0; |
1383 const double minimum_used_percentage = 1.0 - maximum_free_percentage; | 1393 const double minimum_used_percentage = 1.0 - maximum_free_percentage; |
1384 const double max_tmp = used_after_gc / minimum_used_percentage; | 1394 const double max_tmp = used_after_gc / minimum_used_percentage; |
1385 size_t maximum_desired_capacity = (size_t)MIN2(max_tmp, double(max_uintx)); | 1395 size_t maximum_desired_capacity = (size_t)MIN2(max_tmp, double(max_uintx)); |
1386 maximum_desired_capacity = MAX2(maximum_desired_capacity, | 1396 maximum_desired_capacity = MAX2(maximum_desired_capacity, |
1387 MetaspaceSize); | 1397 MetaspaceSize); |
1388 if (PrintGC && Verbose) { | 1398 if (PrintGCDetails && Verbose) { |
1389 gclog_or_tty->print_cr(" " | 1399 gclog_or_tty->print_cr(" " |
1390 " maximum_free_percentage: %6.2f" | 1400 " maximum_free_percentage: %6.2f" |
1391 " minimum_used_percentage: %6.2f", | 1401 " minimum_used_percentage: %6.2f", |
1392 maximum_free_percentage, | 1402 maximum_free_percentage, |
1393 minimum_used_percentage); | 1403 minimum_used_percentage); |
1394 gclog_or_tty->print_cr(" " | 1404 gclog_or_tty->print_cr(" " |
1395 " capacity_until_GC: %6.1fK" | 1405 " minimum_desired_capacity: %6.1fKB" |
1396 " minimum_desired_capacity: %6.1fK" | 1406 " maximum_desired_capacity: %6.1fKB", |
1397 " maximum_desired_capacity: %6.1fK", | |
1398 capacity_until_GC / (double) K, | |
1399 minimum_desired_capacity / (double) K, | 1407 minimum_desired_capacity / (double) K, |
1400 maximum_desired_capacity / (double) K); | 1408 maximum_desired_capacity / (double) K); |
1401 } | 1409 } |
1402 | 1410 |
1403 assert(minimum_desired_capacity <= maximum_desired_capacity, | 1411 assert(minimum_desired_capacity <= maximum_desired_capacity, |
1404 "sanity check"); | 1412 "sanity check"); |
1405 | 1413 |
1406 if (capacity_until_GC > maximum_desired_capacity) { | 1414 if (capacity_until_GC > maximum_desired_capacity) { |
1407 // Capacity too large, compute shrinking size | 1415 // Capacity too large, compute shrinking size |
1408 shrink_words = capacity_until_GC - maximum_desired_capacity; | 1416 shrink_bytes = capacity_until_GC - maximum_desired_capacity; |
1409 // We don't want shrink all the way back to initSize if people call | 1417 // We don't want shrink all the way back to initSize if people call |
1410 // System.gc(), because some programs do that between "phases" and then | 1418 // System.gc(), because some programs do that between "phases" and then |
1411 // we'd just have to grow the heap up again for the next phase. So we | 1419 // we'd just have to grow the heap up again for the next phase. So we |
1412 // damp the shrinking: 0% on the first call, 10% on the second call, 40% | 1420 // damp the shrinking: 0% on the first call, 10% on the second call, 40% |
1413 // on the third call, and 100% by the fourth call. But if we recompute | 1421 // on the third call, and 100% by the fourth call. But if we recompute |
1414 // size without shrinking, it goes back to 0%. | 1422 // size without shrinking, it goes back to 0%. |
1415 shrink_words = shrink_words / 100 * current_shrink_factor; | 1423 shrink_bytes = shrink_bytes / 100 * current_shrink_factor; |
1416 assert(shrink_words <= max_shrink_words, | 1424 assert(shrink_bytes <= max_shrink_bytes, |
1417 err_msg("invalid shrink size " SIZE_FORMAT " not <= " SIZE_FORMAT, | 1425 err_msg("invalid shrink size " SIZE_FORMAT " not <= " SIZE_FORMAT, |
1418 shrink_words, max_shrink_words)); | 1426 shrink_bytes, max_shrink_bytes)); |
1419 if (current_shrink_factor == 0) { | 1427 if (current_shrink_factor == 0) { |
1420 _shrink_factor = 10; | 1428 _shrink_factor = 10; |
1421 } else { | 1429 } else { |
1422 _shrink_factor = MIN2(current_shrink_factor * 4, (uint) 100); | 1430 _shrink_factor = MIN2(current_shrink_factor * 4, (uint) 100); |
1423 } | 1431 } |
1427 " initSize: %.1fK" | 1435 " initSize: %.1fK" |
1428 " maximum_desired_capacity: %.1fK", | 1436 " maximum_desired_capacity: %.1fK", |
1429 MetaspaceSize / (double) K, | 1437 MetaspaceSize / (double) K, |
1430 maximum_desired_capacity / (double) K); | 1438 maximum_desired_capacity / (double) K); |
1431 gclog_or_tty->print_cr(" " | 1439 gclog_or_tty->print_cr(" " |
1432 " shrink_words: %.1fK" | 1440 " shrink_bytes: %.1fK" |
1433 " current_shrink_factor: %d" | 1441 " current_shrink_factor: %d" |
1434 " new shrink factor: %d" | 1442 " new shrink factor: %d" |
1435 " MinMetaspaceExpansion: %.1fK", | 1443 " MinMetaspaceExpansion: %.1fK", |
1436 shrink_words / (double) K, | 1444 shrink_bytes / (double) K, |
1437 current_shrink_factor, | 1445 current_shrink_factor, |
1438 _shrink_factor, | 1446 _shrink_factor, |
1439 MinMetaspaceExpansion / (double) K); | 1447 MinMetaspaceExpansion / (double) K); |
1440 } | 1448 } |
1441 } | 1449 } |
1442 } | 1450 } |
1443 | 1451 |
1444 | |
1445 // Don't shrink unless it's significant | 1452 // Don't shrink unless it's significant |
1446 if (shrink_words >= MinMetaspaceExpansion) { | 1453 if (shrink_bytes >= MinMetaspaceExpansion && |
1447 VirtualSpaceNode* csp = vsl->current_virtual_space(); | 1454 ((capacity_until_GC - shrink_bytes) >= MetaspaceSize)) { |
1448 size_t available_to_shrink = csp->capacity_words_in_vs() - | 1455 MetaspaceGC::set_capacity_until_GC(capacity_until_GC - shrink_bytes); |
1449 csp->used_words_in_vs(); | 1456 } |
1450 shrink_words = MIN2(shrink_words, available_to_shrink); | |
1451 csp->shrink_by(shrink_words); | |
1452 MetaspaceGC::dec_capacity_until_GC(shrink_words); | |
1453 if (PrintGCDetails && Verbose) { | |
1454 size_t new_capacity_until_GC = MetaspaceGC::capacity_until_GC_in_bytes(); | |
1455 gclog_or_tty->print_cr(" metaspace HWM: %.1fK", new_capacity_until_GC / (double) K); | |
1456 } | |
1457 } | |
1458 assert(used_after_gc <= vsl->capacity_bytes_sum(), | |
1459 "sanity check"); | |
1460 | |
1461 } | 1457 } |
1462 | 1458 |
1463 // Metadebug methods | 1459 // Metadebug methods |
1464 | 1460 |
1465 void Metadebug::deallocate_chunk_a_lot(SpaceManager* sm, | 1461 void Metadebug::deallocate_chunk_a_lot(SpaceManager* sm, |
1858 } | 1854 } |
1859 return result; | 1855 return result; |
1860 } | 1856 } |
1861 | 1857 |
1862 size_t SpaceManager::sum_capacity_in_chunks_in_use() const { | 1858 size_t SpaceManager::sum_capacity_in_chunks_in_use() const { |
1863 MutexLockerEx cl(lock(), Mutex::_no_safepoint_check_flag); | 1859 // For CMS use "allocated_chunks_words()" which does not need the |
1864 size_t sum = 0; | 1860 // Metaspace lock. For the other collectors sum over the |
1865 for (ChunkIndex i = ZeroIndex; i < NumberOfInUseLists; i = next_chunk_index(i)) { | 1861 // lists. Use both methods as a check that "allocated_chunks_words()" |
1866 Metachunk* chunk = chunks_in_use(i); | 1862 // is correct. That is, sum_capacity_in_chunks() is too expensive |
1867 while (chunk != NULL) { | 1863 // to use in the product and allocated_chunks_words() should be used |
1868 // Just changed this sum += chunk->capacity_word_size(); | 1864 // but allow for checking that allocated_chunks_words() returns the same |
1869 // sum += chunk->word_size() - Metachunk::overhead(); | 1865 // value as sum_capacity_in_chunks_in_use() which is the definitive |
1870 sum += chunk->capacity_word_size(); | 1866 // answer. |
1871 chunk = chunk->next(); | 1867 if (UseConcMarkSweepGC) { |
1872 } | 1868 return allocated_chunks_words(); |
1873 } | 1869 } else { |
1870 MutexLockerEx cl(lock(), Mutex::_no_safepoint_check_flag); | |
1871 size_t sum = 0; | |
1872 for (ChunkIndex i = ZeroIndex; i < NumberOfInUseLists; i = next_chunk_index(i)) { | |
1873 Metachunk* chunk = chunks_in_use(i); | |
1874 while (chunk != NULL) { | |
1875 sum += chunk->capacity_word_size(); | |
1876 chunk = chunk->next(); | |
1877 } | |
1878 } | |
1874 return sum; | 1879 return sum; |
1880 } | |
1875 } | 1881 } |
1876 | 1882 |
1877 size_t SpaceManager::sum_count_in_chunks_in_use() { | 1883 size_t SpaceManager::sum_count_in_chunks_in_use() { |
1878 size_t count = 0; | 1884 size_t count = 0; |
1879 for (ChunkIndex i = ZeroIndex; i < NumberOfInUseLists; i = next_chunk_index(i)) { | 1885 for (ChunkIndex i = ZeroIndex; i < NumberOfInUseLists; i = next_chunk_index(i)) { |
2027 } | 2033 } |
2028 | 2034 |
2029 SpaceManager::SpaceManager(Mutex* lock, | 2035 SpaceManager::SpaceManager(Mutex* lock, |
2030 VirtualSpaceList* vs_list) : | 2036 VirtualSpaceList* vs_list) : |
2031 _vs_list(vs_list), | 2037 _vs_list(vs_list), |
2032 _allocation_total(0), | 2038 _allocated_blocks_words(0), |
2039 _allocated_chunks_words(0), | |
2040 _allocated_chunks_count(0), | |
2033 _lock(lock) | 2041 _lock(lock) |
2034 { | 2042 { |
2035 initialize(); | 2043 initialize(); |
2044 } | |
2045 | |
2046 void SpaceManager::inc_size_metrics(size_t words) { | |
2047 assert_lock_strong(SpaceManager::expand_lock()); | |
2048 // Total of allocated Metachunks and allocated Metachunks count | |
2049 // for each SpaceManager | |
2050 _allocated_chunks_words = _allocated_chunks_words + words; | |
2051 _allocated_chunks_count++; | |
2052 // Global total of capacity in allocated Metachunks | |
2053 MetaspaceAux::inc_capacity(words); | |
2054 // Global total of allocated Metablocks. | |
2055 // used_words_slow() includes the overhead in each | |
2056 // Metachunk so include it in the used when the | |
2057 // Metachunk is first added (so only added once per | |
2058 // Metachunk). | |
2059 MetaspaceAux::inc_used(Metachunk::overhead()); | |
2060 } | |
2061 | |
2062 void SpaceManager::inc_used_metrics(size_t words) { | |
2063 // Add to the per SpaceManager total | |
2064 Atomic::add_ptr(words, &_allocated_blocks_words); | |
2065 // Add to the global total | |
2066 MetaspaceAux::inc_used(words); | |
2067 } | |
2068 | |
2069 void SpaceManager::dec_total_from_size_metrics() { | |
2070 MetaspaceAux::dec_capacity(allocated_chunks_words()); | |
2071 MetaspaceAux::dec_used(allocated_blocks_words()); | |
2072 // Also deduct the overhead per Metachunk | |
2073 MetaspaceAux::dec_used(allocated_chunks_count() * Metachunk::overhead()); | |
2036 } | 2074 } |
2037 | 2075 |
2038 void SpaceManager::initialize() { | 2076 void SpaceManager::initialize() { |
2039 Metadebug::init_allocation_fail_alot_count(); | 2077 Metadebug::init_allocation_fail_alot_count(); |
2040 for (ChunkIndex i = ZeroIndex; i < NumberOfInUseLists; i = next_chunk_index(i)) { | 2078 for (ChunkIndex i = ZeroIndex; i < NumberOfInUseLists; i = next_chunk_index(i)) { |
2071 } | 2109 } |
2072 } | 2110 } |
2073 | 2111 |
2074 SpaceManager::~SpaceManager() { | 2112 SpaceManager::~SpaceManager() { |
2075 // This call this->_lock which can't be done while holding expand_lock() | 2113 // This call this->_lock which can't be done while holding expand_lock() |
2076 const size_t in_use_before = sum_capacity_in_chunks_in_use(); | 2114 assert(sum_capacity_in_chunks_in_use() == allocated_chunks_words(), |
2115 err_msg("sum_capacity_in_chunks_in_use() " SIZE_FORMAT | |
2116 " allocated_chunks_words() " SIZE_FORMAT, | |
2117 sum_capacity_in_chunks_in_use(), allocated_chunks_words())); | |
2077 | 2118 |
2078 MutexLockerEx fcl(SpaceManager::expand_lock(), | 2119 MutexLockerEx fcl(SpaceManager::expand_lock(), |
2079 Mutex::_no_safepoint_check_flag); | 2120 Mutex::_no_safepoint_check_flag); |
2080 | 2121 |
2081 ChunkManager* chunk_manager = vs_list()->chunk_manager(); | 2122 ChunkManager* chunk_manager = vs_list()->chunk_manager(); |
2082 | 2123 |
2083 chunk_manager->slow_locked_verify(); | 2124 chunk_manager->slow_locked_verify(); |
2125 | |
2126 dec_total_from_size_metrics(); | |
2084 | 2127 |
2085 if (TraceMetadataChunkAllocation && Verbose) { | 2128 if (TraceMetadataChunkAllocation && Verbose) { |
2086 gclog_or_tty->print_cr("~SpaceManager(): " PTR_FORMAT, this); | 2129 gclog_or_tty->print_cr("~SpaceManager(): " PTR_FORMAT, this); |
2087 locked_print_chunks_in_use_on(gclog_or_tty); | 2130 locked_print_chunks_in_use_on(gclog_or_tty); |
2088 } | 2131 } |
2090 // Do not mangle freed Metachunks. The chunk size inside Metachunks | 2133 // Do not mangle freed Metachunks. The chunk size inside Metachunks |
2091 // is during the freeing of a VirtualSpaceNodes. | 2134 // is during the freeing of a VirtualSpaceNodes. |
2092 | 2135 |
2093 // Have to update before the chunks_in_use lists are emptied | 2136 // Have to update before the chunks_in_use lists are emptied |
2094 // below. | 2137 // below. |
2095 chunk_manager->inc_free_chunks_total(in_use_before, | 2138 chunk_manager->inc_free_chunks_total(allocated_chunks_words(), |
2096 sum_count_in_chunks_in_use()); | 2139 sum_count_in_chunks_in_use()); |
2097 | 2140 |
2098 // Add all the chunks in use by this space manager | 2141 // Add all the chunks in use by this space manager |
2099 // to the global list of free chunks. | 2142 // to the global list of free chunks. |
2100 | 2143 |
2156 gclog_or_tty->print_cr(""); | 2199 gclog_or_tty->print_cr(""); |
2157 gclog_or_tty->print_cr("updated dictionary count %d %s", | 2200 gclog_or_tty->print_cr("updated dictionary count %d %s", |
2158 chunk_manager->humongous_dictionary()->total_count(), | 2201 chunk_manager->humongous_dictionary()->total_count(), |
2159 chunk_size_name(HumongousIndex)); | 2202 chunk_size_name(HumongousIndex)); |
2160 } | 2203 } |
2161 set_chunks_in_use(HumongousIndex, NULL); | |
2162 chunk_manager->slow_locked_verify(); | 2204 chunk_manager->slow_locked_verify(); |
2163 } | 2205 } |
2164 | 2206 |
2165 const char* SpaceManager::chunk_size_name(ChunkIndex index) const { | 2207 const char* SpaceManager::chunk_size_name(ChunkIndex index) const { |
2166 switch (index) { | 2208 switch (index) { |
2236 set_chunks_in_use(HumongousIndex, new_chunk); | 2278 set_chunks_in_use(HumongousIndex, new_chunk); |
2237 | 2279 |
2238 assert(new_chunk->word_size() > medium_chunk_size(), "List inconsistency"); | 2280 assert(new_chunk->word_size() > medium_chunk_size(), "List inconsistency"); |
2239 } | 2281 } |
2240 | 2282 |
2283 // Add to the running sum of capacity | |
2284 inc_size_metrics(new_chunk->word_size()); | |
2285 | |
2241 assert(new_chunk->is_empty(), "Not ready for reuse"); | 2286 assert(new_chunk->is_empty(), "Not ready for reuse"); |
2242 if (TraceMetadataChunkAllocation && Verbose) { | 2287 if (TraceMetadataChunkAllocation && Verbose) { |
2243 gclog_or_tty->print("SpaceManager::add_chunk: %d) ", | 2288 gclog_or_tty->print("SpaceManager::add_chunk: %d) ", |
2244 sum_count_in_chunks_in_use()); | 2289 sum_count_in_chunks_in_use()); |
2245 new_chunk->print_on(gclog_or_tty); | 2290 new_chunk->print_on(gclog_or_tty); |
2246 vs_list()->chunk_manager()->locked_print_free_chunks(tty); | 2291 if (vs_list() != NULL) { |
2292 vs_list()->chunk_manager()->locked_print_free_chunks(tty); | |
2293 } | |
2247 } | 2294 } |
2248 } | 2295 } |
2249 | 2296 |
2250 Metachunk* SpaceManager::get_new_chunk(size_t word_size, | 2297 Metachunk* SpaceManager::get_new_chunk(size_t word_size, |
2251 size_t grow_chunks_by_words) { | 2298 size_t grow_chunks_by_words) { |
2312 // For DumpSharedSpaces, only allocate out of the current chunk which is | 2359 // For DumpSharedSpaces, only allocate out of the current chunk which is |
2313 // never null because we gave it the size we wanted. Caller reports out | 2360 // never null because we gave it the size we wanted. Caller reports out |
2314 // of memory if this returns null. | 2361 // of memory if this returns null. |
2315 if (DumpSharedSpaces) { | 2362 if (DumpSharedSpaces) { |
2316 assert(current_chunk() != NULL, "should never happen"); | 2363 assert(current_chunk() != NULL, "should never happen"); |
2317 inc_allocation_total(word_size); | 2364 inc_used_metrics(word_size); |
2318 return current_chunk()->allocate(word_size); // caller handles null result | 2365 return current_chunk()->allocate(word_size); // caller handles null result |
2319 } | 2366 } |
2320 if (current_chunk() != NULL) { | 2367 if (current_chunk() != NULL) { |
2321 result = current_chunk()->allocate(word_size); | 2368 result = current_chunk()->allocate(word_size); |
2322 } | 2369 } |
2323 | 2370 |
2324 if (result == NULL) { | 2371 if (result == NULL) { |
2325 result = grow_and_allocate(word_size); | 2372 result = grow_and_allocate(word_size); |
2326 } | 2373 } |
2327 if (result > 0) { | 2374 if (result > 0) { |
2328 inc_allocation_total(word_size); | 2375 inc_used_metrics(word_size); |
2329 assert(result != (MetaWord*) chunks_in_use(MediumIndex), | 2376 assert(result != (MetaWord*) chunks_in_use(MediumIndex), |
2330 "Head of the list is being allocated"); | 2377 "Head of the list is being allocated"); |
2331 } | 2378 } |
2332 | 2379 |
2333 return result; | 2380 return result; |
2357 "Chunk size is wrong"); | 2404 "Chunk size is wrong"); |
2358 return; | 2405 return; |
2359 } | 2406 } |
2360 | 2407 |
2361 #ifdef ASSERT | 2408 #ifdef ASSERT |
2362 void SpaceManager::verify_allocation_total() { | 2409 void SpaceManager::verify_allocated_blocks_words() { |
2363 // Verification is only guaranteed at a safepoint. | 2410 // Verification is only guaranteed at a safepoint. |
2364 if (SafepointSynchronize::is_at_safepoint()) { | 2411 assert(SafepointSynchronize::is_at_safepoint() || !Universe::is_fully_initialized(), |
2365 gclog_or_tty->print_cr("Chunk " PTR_FORMAT " allocation_total " SIZE_FORMAT | 2412 "Verification can fail if the applications is running"); |
2366 " sum_used_in_chunks_in_use " SIZE_FORMAT, | 2413 assert(allocated_blocks_words() == sum_used_in_chunks_in_use(), |
2367 this, | |
2368 allocation_total(), | |
2369 sum_used_in_chunks_in_use()); | |
2370 } | |
2371 MutexLockerEx cl(lock(), Mutex::_no_safepoint_check_flag); | |
2372 assert(allocation_total() == sum_used_in_chunks_in_use(), | |
2373 err_msg("allocation total is not consistent " SIZE_FORMAT | 2414 err_msg("allocation total is not consistent " SIZE_FORMAT |
2374 " vs " SIZE_FORMAT, | 2415 " vs " SIZE_FORMAT, |
2375 allocation_total(), sum_used_in_chunks_in_use())); | 2416 allocated_blocks_words(), sum_used_in_chunks_in_use())); |
2376 } | 2417 } |
2377 | 2418 |
2378 #endif | 2419 #endif |
2379 | 2420 |
2380 void SpaceManager::dump(outputStream* const out) const { | 2421 void SpaceManager::dump(outputStream* const out) const { |
2426 } | 2467 } |
2427 #endif // PRODUCT | 2468 #endif // PRODUCT |
2428 | 2469 |
2429 // MetaspaceAux | 2470 // MetaspaceAux |
2430 | 2471 |
2431 size_t MetaspaceAux::used_in_bytes(Metaspace::MetadataType mdtype) { | 2472 |
2473 size_t MetaspaceAux::_allocated_capacity_words = 0; | |
2474 size_t MetaspaceAux::_allocated_used_words = 0; | |
2475 | |
2476 size_t MetaspaceAux::free_bytes() { | |
2477 size_t result = 0; | |
2478 if (Metaspace::class_space_list() != NULL) { | |
2479 result = result + Metaspace::class_space_list()->free_bytes(); | |
2480 } | |
2481 if (Metaspace::space_list() != NULL) { | |
2482 result = result + Metaspace::space_list()->free_bytes(); | |
2483 } | |
2484 return result; | |
2485 } | |
2486 | |
2487 void MetaspaceAux::dec_capacity(size_t words) { | |
2488 assert_lock_strong(SpaceManager::expand_lock()); | |
2489 assert(words <= _allocated_capacity_words, | |
2490 err_msg("About to decrement below 0: words " SIZE_FORMAT | |
2491 " is greater than _allocated_capacity_words " SIZE_FORMAT, | |
2492 words, _allocated_capacity_words)); | |
2493 _allocated_capacity_words = _allocated_capacity_words - words; | |
2494 } | |
2495 | |
2496 void MetaspaceAux::inc_capacity(size_t words) { | |
2497 assert_lock_strong(SpaceManager::expand_lock()); | |
2498 // Needs to be atomic | |
2499 _allocated_capacity_words = _allocated_capacity_words + words; | |
2500 } | |
2501 | |
2502 void MetaspaceAux::dec_used(size_t words) { | |
2503 assert(words <= _allocated_used_words, | |
2504 err_msg("About to decrement below 0: words " SIZE_FORMAT | |
2505 " is greater than _allocated_used_words " SIZE_FORMAT, | |
2506 words, _allocated_used_words)); | |
2507 // For CMS deallocation of the Metaspaces occurs during the | |
2508 // sweep which is a concurrent phase. Protection by the expand_lock() | |
2509 // is not enough since allocation is on a per Metaspace basis | |
2510 // and protected by the Metaspace lock. | |
2511 jlong minus_words = (jlong) - (jlong) words; | |
2512 Atomic::add_ptr(minus_words, &_allocated_used_words); | |
2513 } | |
2514 | |
2515 void MetaspaceAux::inc_used(size_t words) { | |
2516 // _allocated_used_words tracks allocations for | |
2517 // each piece of metadata. Those allocations are | |
2518 // generally done concurrently by different application | |
2519 // threads so must be done atomically. | |
2520 Atomic::add_ptr(words, &_allocated_used_words); | |
2521 } | |
2522 | |
2523 size_t MetaspaceAux::used_bytes_slow(Metaspace::MetadataType mdtype) { | |
2432 size_t used = 0; | 2524 size_t used = 0; |
2433 ClassLoaderDataGraphMetaspaceIterator iter; | 2525 ClassLoaderDataGraphMetaspaceIterator iter; |
2434 while (iter.repeat()) { | 2526 while (iter.repeat()) { |
2435 Metaspace* msp = iter.get_next(); | 2527 Metaspace* msp = iter.get_next(); |
2436 // Sum allocation_total for each metaspace | 2528 // Sum allocated_blocks_words for each metaspace |
2437 if (msp != NULL) { | 2529 if (msp != NULL) { |
2438 used += msp->used_words(mdtype); | 2530 used += msp->used_words_slow(mdtype); |
2439 } | 2531 } |
2440 } | 2532 } |
2441 return used * BytesPerWord; | 2533 return used * BytesPerWord; |
2442 } | 2534 } |
2443 | 2535 |
2451 } | 2543 } |
2452 } | 2544 } |
2453 return free * BytesPerWord; | 2545 return free * BytesPerWord; |
2454 } | 2546 } |
2455 | 2547 |
2456 size_t MetaspaceAux::capacity_in_bytes(Metaspace::MetadataType mdtype) { | 2548 size_t MetaspaceAux::capacity_bytes_slow(Metaspace::MetadataType mdtype) { |
2457 size_t capacity = free_chunks_total(mdtype); | 2549 // Don't count the space in the freelists. That space will be |
2550 // added to the capacity calculation as needed. | |
2551 size_t capacity = 0; | |
2458 ClassLoaderDataGraphMetaspaceIterator iter; | 2552 ClassLoaderDataGraphMetaspaceIterator iter; |
2459 while (iter.repeat()) { | 2553 while (iter.repeat()) { |
2460 Metaspace* msp = iter.get_next(); | 2554 Metaspace* msp = iter.get_next(); |
2461 if (msp != NULL) { | 2555 if (msp != NULL) { |
2462 capacity += msp->capacity_words(mdtype); | 2556 capacity += msp->capacity_words_slow(mdtype); |
2463 } | 2557 } |
2464 } | 2558 } |
2465 return capacity * BytesPerWord; | 2559 return capacity * BytesPerWord; |
2466 } | 2560 } |
2467 | 2561 |
2484 | 2578 |
2485 size_t MetaspaceAux::free_chunks_total_in_bytes(Metaspace::MetadataType mdtype) { | 2579 size_t MetaspaceAux::free_chunks_total_in_bytes(Metaspace::MetadataType mdtype) { |
2486 return free_chunks_total(mdtype) * BytesPerWord; | 2580 return free_chunks_total(mdtype) * BytesPerWord; |
2487 } | 2581 } |
2488 | 2582 |
2583 size_t MetaspaceAux::free_chunks_total() { | |
2584 return free_chunks_total(Metaspace::ClassType) + | |
2585 free_chunks_total(Metaspace::NonClassType); | |
2586 } | |
2587 | |
2588 size_t MetaspaceAux::free_chunks_total_in_bytes() { | |
2589 return free_chunks_total() * BytesPerWord; | |
2590 } | |
2591 | |
2489 void MetaspaceAux::print_metaspace_change(size_t prev_metadata_used) { | 2592 void MetaspaceAux::print_metaspace_change(size_t prev_metadata_used) { |
2490 gclog_or_tty->print(", [Metaspace:"); | 2593 gclog_or_tty->print(", [Metaspace:"); |
2491 if (PrintGCDetails && Verbose) { | 2594 if (PrintGCDetails && Verbose) { |
2492 gclog_or_tty->print(" " SIZE_FORMAT | 2595 gclog_or_tty->print(" " SIZE_FORMAT |
2493 "->" SIZE_FORMAT | 2596 "->" SIZE_FORMAT |
2494 "(" SIZE_FORMAT "/" SIZE_FORMAT ")", | 2597 "(" SIZE_FORMAT ")", |
2495 prev_metadata_used, | 2598 prev_metadata_used, |
2496 used_in_bytes(), | 2599 allocated_capacity_bytes(), |
2497 capacity_in_bytes(), | |
2498 reserved_in_bytes()); | 2600 reserved_in_bytes()); |
2499 } else { | 2601 } else { |
2500 gclog_or_tty->print(" " SIZE_FORMAT "K" | 2602 gclog_or_tty->print(" " SIZE_FORMAT "K" |
2501 "->" SIZE_FORMAT "K" | 2603 "->" SIZE_FORMAT "K" |
2502 "(" SIZE_FORMAT "K/" SIZE_FORMAT "K)", | 2604 "(" SIZE_FORMAT "K)", |
2503 prev_metadata_used / K, | 2605 prev_metadata_used / K, |
2504 used_in_bytes()/ K, | 2606 allocated_capacity_bytes() / K, |
2505 capacity_in_bytes()/K, | |
2506 reserved_in_bytes()/ K); | 2607 reserved_in_bytes()/ K); |
2507 } | 2608 } |
2508 | 2609 |
2509 gclog_or_tty->print("]"); | 2610 gclog_or_tty->print("]"); |
2510 } | 2611 } |
2515 Metaspace::MetadataType nct = Metaspace::NonClassType; | 2616 Metaspace::MetadataType nct = Metaspace::NonClassType; |
2516 | 2617 |
2517 out->print_cr(" Metaspace total " | 2618 out->print_cr(" Metaspace total " |
2518 SIZE_FORMAT "K, used " SIZE_FORMAT "K," | 2619 SIZE_FORMAT "K, used " SIZE_FORMAT "K," |
2519 " reserved " SIZE_FORMAT "K", | 2620 " reserved " SIZE_FORMAT "K", |
2520 capacity_in_bytes()/K, used_in_bytes()/K, reserved_in_bytes()/K); | 2621 allocated_capacity_bytes()/K, allocated_used_bytes()/K, reserved_in_bytes()/K); |
2521 out->print_cr(" data space " | 2622 #if 0 |
2522 SIZE_FORMAT "K, used " SIZE_FORMAT "K," | 2623 // The calls to capacity_bytes_slow() and used_bytes_slow() cause |
2523 " reserved " SIZE_FORMAT "K", | 2624 // lock ordering assertion failures with some collectors. Do |
2524 capacity_in_bytes(nct)/K, used_in_bytes(nct)/K, reserved_in_bytes(nct)/K); | 2625 // not include this code until the lock ordering is fixed. |
2525 out->print_cr(" class space " | 2626 if (PrintGCDetails && Verbose) { |
2526 SIZE_FORMAT "K, used " SIZE_FORMAT "K," | 2627 out->print_cr(" data space " |
2527 " reserved " SIZE_FORMAT "K", | 2628 SIZE_FORMAT "K, used " SIZE_FORMAT "K," |
2528 capacity_in_bytes(ct)/K, used_in_bytes(ct)/K, reserved_in_bytes(ct)/K); | 2629 " reserved " SIZE_FORMAT "K", |
2630 capacity_bytes_slow(nct)/K, used_bytes_slow(nct)/K, reserved_in_bytes(nct)/K); | |
2631 out->print_cr(" class space " | |
2632 SIZE_FORMAT "K, used " SIZE_FORMAT "K," | |
2633 " reserved " SIZE_FORMAT "K", | |
2634 capacity_bytes_slow(ct)/K, used_bytes_slow(ct)/K, reserved_in_bytes(ct)/K); | |
2635 } | |
2636 #endif | |
2529 } | 2637 } |
2530 | 2638 |
2531 // Print information for class space and data space separately. | 2639 // Print information for class space and data space separately. |
2532 // This is almost the same as above. | 2640 // This is almost the same as above. |
2533 void MetaspaceAux::print_on(outputStream* out, Metaspace::MetadataType mdtype) { | 2641 void MetaspaceAux::print_on(outputStream* out, Metaspace::MetadataType mdtype) { |
2534 size_t free_chunks_capacity_bytes = free_chunks_total_in_bytes(mdtype); | 2642 size_t free_chunks_capacity_bytes = free_chunks_total_in_bytes(mdtype); |
2535 size_t capacity_bytes = capacity_in_bytes(mdtype); | 2643 size_t capacity_bytes = capacity_bytes_slow(mdtype); |
2536 size_t used_bytes = used_in_bytes(mdtype); | 2644 size_t used_bytes = used_bytes_slow(mdtype); |
2537 size_t free_bytes = free_in_bytes(mdtype); | 2645 size_t free_bytes = free_in_bytes(mdtype); |
2538 size_t used_and_free = used_bytes + free_bytes + | 2646 size_t used_and_free = used_bytes + free_bytes + |
2539 free_chunks_capacity_bytes; | 2647 free_chunks_capacity_bytes; |
2540 out->print_cr(" Chunk accounting: used in chunks " SIZE_FORMAT | 2648 out->print_cr(" Chunk accounting: used in chunks " SIZE_FORMAT |
2541 "K + unused in chunks " SIZE_FORMAT "K + " | 2649 "K + unused in chunks " SIZE_FORMAT "K + " |
2603 | 2711 |
2604 void MetaspaceAux::verify_free_chunks() { | 2712 void MetaspaceAux::verify_free_chunks() { |
2605 Metaspace::space_list()->chunk_manager()->verify(); | 2713 Metaspace::space_list()->chunk_manager()->verify(); |
2606 Metaspace::class_space_list()->chunk_manager()->verify(); | 2714 Metaspace::class_space_list()->chunk_manager()->verify(); |
2607 } | 2715 } |
2716 | |
2717 void MetaspaceAux::verify_capacity() { | |
2718 #ifdef ASSERT | |
2719 size_t running_sum_capacity_bytes = allocated_capacity_bytes(); | |
2720 // For purposes of the running sum of used, verify against capacity | |
2721 size_t capacity_in_use_bytes = capacity_bytes_slow(); | |
2722 assert(running_sum_capacity_bytes == capacity_in_use_bytes, | |
2723 err_msg("allocated_capacity_words() * BytesPerWord " SIZE_FORMAT | |
2724 " capacity_bytes_slow()" SIZE_FORMAT, | |
2725 running_sum_capacity_bytes, capacity_in_use_bytes)); | |
2726 #endif | |
2727 } | |
2728 | |
2729 void MetaspaceAux::verify_used() { | |
2730 #ifdef ASSERT | |
2731 size_t running_sum_used_bytes = allocated_used_bytes(); | |
2732 // For purposes of the running sum of used, verify against capacity | |
2733 size_t used_in_use_bytes = used_bytes_slow(); | |
2734 assert(allocated_used_bytes() == used_in_use_bytes, | |
2735 err_msg("allocated_used_bytes() " SIZE_FORMAT | |
2736 " used_bytes_slow()()" SIZE_FORMAT, | |
2737 allocated_used_bytes(), used_in_use_bytes)); | |
2738 #endif | |
2739 } | |
2740 | |
2741 void MetaspaceAux::verify_metrics() { | |
2742 verify_capacity(); | |
2743 verify_used(); | |
2744 } | |
2745 | |
2608 | 2746 |
2609 // Metaspace methods | 2747 // Metaspace methods |
2610 | 2748 |
2611 size_t Metaspace::_first_chunk_word_size = 0; | 2749 size_t Metaspace::_first_chunk_word_size = 0; |
2612 size_t Metaspace::_first_class_chunk_word_size = 0; | 2750 size_t Metaspace::_first_class_chunk_word_size = 0; |
2753 | 2891 |
2754 MetaWord* Metaspace::expand_and_allocate(size_t word_size, MetadataType mdtype) { | 2892 MetaWord* Metaspace::expand_and_allocate(size_t word_size, MetadataType mdtype) { |
2755 MetaWord* result; | 2893 MetaWord* result; |
2756 MetaspaceGC::set_expand_after_GC(true); | 2894 MetaspaceGC::set_expand_after_GC(true); |
2757 size_t before_inc = MetaspaceGC::capacity_until_GC(); | 2895 size_t before_inc = MetaspaceGC::capacity_until_GC(); |
2758 size_t delta_words = MetaspaceGC::delta_capacity_until_GC(word_size); | 2896 size_t delta_bytes = MetaspaceGC::delta_capacity_until_GC(word_size) * BytesPerWord; |
2759 MetaspaceGC::inc_capacity_until_GC(delta_words); | 2897 MetaspaceGC::inc_capacity_until_GC(delta_bytes); |
2760 if (PrintGCDetails && Verbose) { | 2898 if (PrintGCDetails && Verbose) { |
2761 gclog_or_tty->print_cr("Increase capacity to GC from " SIZE_FORMAT | 2899 gclog_or_tty->print_cr("Increase capacity to GC from " SIZE_FORMAT |
2762 " to " SIZE_FORMAT, before_inc, MetaspaceGC::capacity_until_GC()); | 2900 " to " SIZE_FORMAT, before_inc, MetaspaceGC::capacity_until_GC()); |
2763 } | 2901 } |
2764 | 2902 |
2772 char* Metaspace::bottom() const { | 2910 char* Metaspace::bottom() const { |
2773 assert(DumpSharedSpaces, "only useful and valid for dumping shared spaces"); | 2911 assert(DumpSharedSpaces, "only useful and valid for dumping shared spaces"); |
2774 return (char*)vsm()->current_chunk()->bottom(); | 2912 return (char*)vsm()->current_chunk()->bottom(); |
2775 } | 2913 } |
2776 | 2914 |
2777 size_t Metaspace::used_words(MetadataType mdtype) const { | 2915 size_t Metaspace::used_words_slow(MetadataType mdtype) const { |
2778 // return vsm()->allocation_total(); | 2916 // return vsm()->allocated_used_words(); |
2779 return mdtype == ClassType ? class_vsm()->sum_used_in_chunks_in_use() : | 2917 return mdtype == ClassType ? class_vsm()->sum_used_in_chunks_in_use() : |
2780 vsm()->sum_used_in_chunks_in_use(); // includes overhead! | 2918 vsm()->sum_used_in_chunks_in_use(); // includes overhead! |
2781 } | 2919 } |
2782 | 2920 |
2783 size_t Metaspace::free_words(MetadataType mdtype) const { | 2921 size_t Metaspace::free_words(MetadataType mdtype) const { |
2788 // Space capacity in the Metaspace. It includes | 2926 // Space capacity in the Metaspace. It includes |
2789 // space in the list of chunks from which allocations | 2927 // space in the list of chunks from which allocations |
2790 // have been made. Don't include space in the global freelist and | 2928 // have been made. Don't include space in the global freelist and |
2791 // in the space available in the dictionary which | 2929 // in the space available in the dictionary which |
2792 // is already counted in some chunk. | 2930 // is already counted in some chunk. |
2793 size_t Metaspace::capacity_words(MetadataType mdtype) const { | 2931 size_t Metaspace::capacity_words_slow(MetadataType mdtype) const { |
2794 return mdtype == ClassType ? class_vsm()->sum_capacity_in_chunks_in_use() : | 2932 return mdtype == ClassType ? class_vsm()->sum_capacity_in_chunks_in_use() : |
2795 vsm()->sum_capacity_in_chunks_in_use(); | 2933 vsm()->sum_capacity_in_chunks_in_use(); |
2934 } | |
2935 | |
2936 size_t Metaspace::used_bytes_slow(MetadataType mdtype) const { | |
2937 return used_words_slow(mdtype) * BytesPerWord; | |
2938 } | |
2939 | |
2940 size_t Metaspace::capacity_bytes_slow(MetadataType mdtype) const { | |
2941 return capacity_words_slow(mdtype) * BytesPerWord; | |
2796 } | 2942 } |
2797 | 2943 |
2798 void Metaspace::deallocate(MetaWord* ptr, size_t word_size, bool is_class) { | 2944 void Metaspace::deallocate(MetaWord* ptr, size_t word_size, bool is_class) { |
2799 if (SafepointSynchronize::is_at_safepoint()) { | 2945 if (SafepointSynchronize::is_at_safepoint()) { |
2800 assert(Thread::current()->is_VM_thread(), "should be the VM thread"); | 2946 assert(Thread::current()->is_VM_thread(), "should be the VM thread"); |
2919 vsm()->verify(); | 3065 vsm()->verify(); |
2920 class_vsm()->verify(); | 3066 class_vsm()->verify(); |
2921 } | 3067 } |
2922 | 3068 |
2923 void Metaspace::dump(outputStream* const out) const { | 3069 void Metaspace::dump(outputStream* const out) const { |
2924 if (UseMallocOnly) { | |
2925 // Just print usage for now | |
2926 out->print_cr("usage %d", used_words(Metaspace::NonClassType)); | |
2927 } | |
2928 out->print_cr("\nVirtual space manager: " INTPTR_FORMAT, vsm()); | 3070 out->print_cr("\nVirtual space manager: " INTPTR_FORMAT, vsm()); |
2929 vsm()->dump(out); | 3071 vsm()->dump(out); |
2930 out->print_cr("\nClass space manager: " INTPTR_FORMAT, class_vsm()); | 3072 out->print_cr("\nClass space manager: " INTPTR_FORMAT, class_vsm()); |
2931 class_vsm()->dump(out); | 3073 class_vsm()->dump(out); |
2932 } | 3074 } |