6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24 #include "precompiled.hpp"
25 #include "gc_interface/collectedHeap.hpp"
26 #include "memory/binaryTreeDictionary.hpp"
27 #include "memory/freeList.hpp"
28 #include "memory/collectorPolicy.hpp"
29 #include "memory/filemap.hpp"
30 #include "memory/freeList.hpp"
31 #include "memory/metablock.hpp"
32 #include "memory/metachunk.hpp"
33 #include "memory/metaspace.hpp"
34 #include "memory/metaspaceShared.hpp"
35 #include "memory/resourceArea.hpp"
36 #include "memory/universe.hpp"
37 #include "runtime/globals.hpp"
38 #include "runtime/java.hpp"
39 #include "runtime/mutex.hpp"
40 #include "runtime/orderAccess.hpp"
41 #include "services/memTracker.hpp"
42 #include "utilities/copy.hpp"
43 #include "utilities/debug.hpp"
44
45 typedef BinaryTreeDictionary<Metablock, FreeList> BlockTreeDictionary;
94 bool MetaspaceGC::_should_concurrent_collect = false;
95
96 // Blocks of space for metadata are allocated out of Metachunks.
97 //
98 // Metachunk are allocated out of MetadataVirtualspaces and once
99 // allocated there is no explicit link between a Metachunk and
100 // the MetadataVirtualspaces from which it was allocated.
101 //
102 // Each SpaceManager maintains a
103 // list of the chunks it is using and the current chunk. The current
104 // chunk is the chunk from which allocations are done. Space freed in
105 // a chunk is placed on the free list of blocks (BlockFreelist) and
106 // reused from there.
107
108 typedef class FreeList<Metachunk> ChunkList;
109
110 // Manages the global free lists of chunks.
111 // Has three lists of free chunks, and a total size and
112 // count that includes all three
113
114 class ChunkManager VALUE_OBJ_CLASS_SPEC {
115
116 // Free list of chunks of different sizes.
117 // SpecializedChunk
118 // SmallChunk
119 // MediumChunk
120 // HumongousChunk
121 ChunkList _free_chunks[NumberOfFreeLists];
122
123
124 // HumongousChunk
125 ChunkTreeDictionary _humongous_dictionary;
126
127 // ChunkManager in all lists of this type
128 size_t _free_chunks_total;
129 size_t _free_chunks_count;
130
131 void dec_free_chunks_total(size_t v) {
132 assert(_free_chunks_count > 0 &&
133 _free_chunks_total > 0,
134 "About to go negative");
141
142 size_t sum_free_chunks();
143 size_t sum_free_chunks_count();
144
145 void locked_verify_free_chunks_total();
146 void slow_locked_verify_free_chunks_total() {
147 if (metaspace_slow_verify) {
148 locked_verify_free_chunks_total();
149 }
150 }
151 void locked_verify_free_chunks_count();
152 void slow_locked_verify_free_chunks_count() {
153 if (metaspace_slow_verify) {
154 locked_verify_free_chunks_count();
155 }
156 }
157 void verify_free_chunks_count();
158
159 public:
160
161 ChunkManager() : _free_chunks_total(0), _free_chunks_count(0) {}
162
163 // add or delete (return) a chunk to the global freelist.
164 Metachunk* chunk_freelist_allocate(size_t word_size);
165 void chunk_freelist_deallocate(Metachunk* chunk);
166
167 // Map a size to a list index assuming that there are lists
168 // for special, small, medium, and humongous chunks.
169 static ChunkIndex list_index(size_t size);
170
171 // Remove the chunk from its freelist. It is
172 // expected to be on one of the _free_chunks[] lists.
173 void remove_chunk(Metachunk* chunk);
174
175 // Add the simple linked list of chunks to the freelist of chunks
176 // of type index.
177 void return_chunks(ChunkIndex index, Metachunk* chunks);
178
179 // Total of the space in the free chunks list
180 size_t free_chunks_total_words();
181 size_t free_chunks_total_bytes();
259
260 // Link to next VirtualSpaceNode
261 VirtualSpaceNode* _next;
262
263 // total in the VirtualSpace
264 MemRegion _reserved;
265 ReservedSpace _rs;
266 VirtualSpace _virtual_space;
267 MetaWord* _top;
268 // count of chunks contained in this VirtualSpace
269 uintx _container_count;
270
271 // Convenience functions to access the _virtual_space
272 char* low() const { return virtual_space()->low(); }
273 char* high() const { return virtual_space()->high(); }
274
275 // The first Metachunk will be allocated at the bottom of the
276 // VirtualSpace
277 Metachunk* first_chunk() { return (Metachunk*) bottom(); }
278
279 void inc_container_count();
280 #ifdef ASSERT
281 uint container_count_slow();
282 #endif
283
284 public:
285
286 VirtualSpaceNode(size_t byte_size);
287 VirtualSpaceNode(ReservedSpace rs) : _top(NULL), _next(NULL), _rs(rs), _container_count(0) {}
288 ~VirtualSpaceNode();
289
290 // Convenience functions for logical bottom and end
291 MetaWord* bottom() const { return (MetaWord*) _virtual_space.low(); }
292 MetaWord* end() const { return (MetaWord*) _virtual_space.high(); }
293
294 size_t reserved_words() const { return _virtual_space.reserved_size() / BytesPerWord; }
295 size_t expanded_words() const { return _virtual_space.committed_size() / BytesPerWord; }
296 size_t committed_words() const { return _virtual_space.actual_committed_size() / BytesPerWord; }
297
298 // address of next available space in _virtual_space;
299 // Accessors
300 VirtualSpaceNode* next() { return _next; }
301 void set_next(VirtualSpaceNode* v) { _next = v; }
302
303 void set_reserved(MemRegion const v) { _reserved = v; }
304 void set_top(MetaWord* v) { _top = v; }
305
306 // Accessors
307 MemRegion* reserved() { return &_reserved; }
308 VirtualSpace* virtual_space() const { return (VirtualSpace*) &_virtual_space; }
309
310 // Returns true if "word_size" is available in the VirtualSpace
311 bool is_available(size_t word_size) { return _top + word_size <= end(); }
312
313 MetaWord* top() const { return _top; }
314 void inc_top(size_t word_size) { _top += word_size; }
315
316 uintx container_count() { return _container_count; }
317 void dec_container_count();
318 #ifdef ASSERT
319 void verify_container_count();
320 #endif
321
322 // used and capacity in this single entry in the list
323 size_t used_words_in_vs() const;
324 size_t capacity_words_in_vs() const;
325 size_t free_words_in_vs() const;
326
327 bool initialize();
328
329 // get space from the virtual space
330 Metachunk* take_from_committed(size_t chunk_word_size);
331
332 // Allocate a chunk from the virtual space and return it.
333 Metachunk* get_chunk_vs(size_t chunk_word_size);
334
335 // Expands/shrinks the committed space in a virtual space. Delegates
336 // to Virtualspace
337 bool expand_by(size_t words, bool pre_touch = false);
338
404 }
405 return count;
406 }
407 #endif
408
409 // List of VirtualSpaces for metadata allocation.
410 // It has a _next link for singly linked list and a MemRegion
411 // for total space in the VirtualSpace.
412 class VirtualSpaceList : public CHeapObj<mtClass> {
413 friend class VirtualSpaceNode;
414
415 enum VirtualSpaceSizes {
416 VirtualSpaceSize = 256 * K
417 };
418
419 // Global list of virtual spaces
420 // Head of the list
421 VirtualSpaceNode* _virtual_space_list;
422 // virtual space currently being used for allocations
423 VirtualSpaceNode* _current_virtual_space;
424 // Free chunk list for all other metadata
425 ChunkManager _chunk_manager;
426
427 // Can this virtual list allocate >1 spaces? Also, used to determine
428 // whether to allocate unlimited small chunks in this virtual space
429 bool _is_class;
430 bool can_grow() const { return !is_class() || !UseCompressedClassPointers; }
431
432 // Sum of reserved and committed memory in the virtual spaces
433 size_t _reserved_words;
434 size_t _committed_words;
435
436 // Number of virtual spaces
437 size_t _virtual_space_count;
438
439 ~VirtualSpaceList();
440
441 VirtualSpaceNode* virtual_space_list() const { return _virtual_space_list; }
442
443 void set_virtual_space_list(VirtualSpaceNode* v) {
444 _virtual_space_list = v;
445 }
458 VirtualSpaceList(size_t word_size);
459 VirtualSpaceList(ReservedSpace rs);
460
461 size_t free_bytes();
462
463 Metachunk* get_new_chunk(size_t word_size,
464 size_t grow_chunks_by_words,
465 size_t medium_chunk_bunch);
466
467 bool expand_by(VirtualSpaceNode* node, size_t word_size, bool pre_touch = false);
468
469 // Get the first chunk for a Metaspace. Used for
470 // special cases such as the boot class loader, reflection
471 // class loader and anonymous class loader.
472 Metachunk* get_initialization_chunk(size_t word_size, size_t chunk_bunch);
473
474 VirtualSpaceNode* current_virtual_space() {
475 return _current_virtual_space;
476 }
477
478 ChunkManager* chunk_manager() { return &_chunk_manager; }
479 bool is_class() const { return _is_class; }
480
481 // Allocate the first virtualspace.
482 void initialize(size_t word_size);
483
484 size_t reserved_words() { return _reserved_words; }
485 size_t reserved_bytes() { return reserved_words() * BytesPerWord; }
486 size_t committed_words() { return _committed_words; }
487 size_t committed_bytes() { return committed_words() * BytesPerWord; }
488
489 void inc_reserved_words(size_t v);
490 void dec_reserved_words(size_t v);
491 void inc_committed_words(size_t v);
492 void dec_committed_words(size_t v);
493 void inc_virtual_space_count();
494 void dec_virtual_space_count();
495
496 // Unlink empty VirtualSpaceNodes and free it.
497 void purge();
498
499 // Used and capacity in the entire list of virtual spaces.
500 // These are global values shared by all Metaspaces
501 size_t capacity_words_sum();
502 size_t capacity_bytes_sum() { return capacity_words_sum() * BytesPerWord; }
503 size_t used_words_sum();
504 size_t used_bytes_sum() { return used_words_sum() * BytesPerWord; }
505
506 bool contains(const void *ptr);
507
508 void print_on(outputStream* st) const;
509
510 class VirtualSpaceListIterator : public StackObj {
511 VirtualSpaceNode* _virtual_spaces;
512 public:
513 VirtualSpaceListIterator(VirtualSpaceNode* virtual_spaces) :
514 _virtual_spaces(virtual_spaces) {}
515
516 bool repeat() {
517 return _virtual_spaces != NULL;
518 }
519
520 VirtualSpaceNode* get_next() {
521 VirtualSpaceNode* result = _virtual_spaces;
522 if (_virtual_spaces != NULL) {
523 _virtual_spaces = _virtual_spaces->next();
524 }
577 private:
578
579 // protects allocations and contains.
580 Mutex* const _lock;
581
582 // Type of metadata allocated.
583 Metaspace::MetadataType _mdtype;
584
585 // Chunk related size
586 size_t _medium_chunk_bunch;
587
588 // List of chunks in use by this SpaceManager. Allocations
589 // are done from the current chunk. The list is used for deallocating
590 // chunks when the SpaceManager is freed.
591 Metachunk* _chunks_in_use[NumberOfInUseLists];
592 Metachunk* _current_chunk;
593
594 // Virtual space where allocation comes from.
595 VirtualSpaceList* _vs_list;
596
597 // Number of small chunks to allocate to a manager
598 // If class space manager, small chunks are unlimited
599 static uint const _small_chunk_limit;
600
601 // Sum of all space in allocated chunks
602 size_t _allocated_blocks_words;
603
604 // Sum of all allocated chunks
605 size_t _allocated_chunks_words;
606 size_t _allocated_chunks_count;
607
608 // Free lists of blocks are per SpaceManager since they
609 // are assumed to be in chunks in use by the SpaceManager
610 // and all chunks in use by a SpaceManager are freed when
611 // the class loader using the SpaceManager is collected.
612 BlockFreelist _block_freelists;
613
614 // protects virtualspace and chunk expansions
615 static const char* _expand_lock_name;
616 static const int _expand_lock_rank;
617 static Mutex* const _expand_lock;
618
619 private:
620 // Accessors
621 Metachunk* chunks_in_use(ChunkIndex index) const { return _chunks_in_use[index]; }
622 void set_chunks_in_use(ChunkIndex index, Metachunk* v) { _chunks_in_use[index] = v; }
623
624 BlockFreelist* block_freelists() const {
625 return (BlockFreelist*) &_block_freelists;
626 }
627
628 Metaspace::MetadataType mdtype() { return _mdtype; }
629 VirtualSpaceList* vs_list() const { return _vs_list; }
630
631 Metachunk* current_chunk() const { return _current_chunk; }
632 void set_current_chunk(Metachunk* v) {
633 _current_chunk = v;
634 }
635
636 Metachunk* find_current_chunk(size_t word_size);
637
638 // Add chunk to the list of chunks in use
639 void add_chunk(Metachunk* v, bool make_current);
640 void retire_current_chunk();
641
642 Mutex* lock() const { return _lock; }
643
644 const char* chunk_size_name(ChunkIndex index) const;
645
646 protected:
647 void initialize();
648
649 public:
650 SpaceManager(Metaspace::MetadataType mdtype,
651 Mutex* lock,
652 VirtualSpaceList* vs_list);
653 ~SpaceManager();
654
655 enum ChunkMultiples {
656 MediumChunkMultiple = 4
657 };
658
659 // Accessors
660 size_t specialized_chunk_size() { return SpecializedChunk; }
661 size_t small_chunk_size() { return (size_t) vs_list()->is_class() ? ClassSmallChunk : SmallChunk; }
662 size_t medium_chunk_size() { return (size_t) vs_list()->is_class() ? ClassMediumChunk : MediumChunk; }
663 size_t medium_chunk_bunch() { return medium_chunk_size() * MediumChunkMultiple; }
664
665 size_t allocated_blocks_words() const { return _allocated_blocks_words; }
666 size_t allocated_blocks_bytes() const { return _allocated_blocks_words * BytesPerWord; }
667 size_t allocated_chunks_words() const { return _allocated_chunks_words; }
668 size_t allocated_chunks_count() const { return _allocated_chunks_count; }
669
670 bool is_humongous(size_t word_size) { return word_size > medium_chunk_size(); }
671
672 static Mutex* expand_lock() { return _expand_lock; }
673
674 // Increment the per Metaspace and global running sums for Metachunks
675 // by the given size. This is used when a Metachunk to added to
676 // the in-use list.
677 void inc_size_metrics(size_t words);
678 // Increment the per Metaspace and global running sums Metablocks by the given
679 // size. This is used when a Metablock is allocated.
680 void inc_used_metrics(size_t words);
681 // Delete the portion of the running sums for this SpaceManager. That is,
682 // the globals running sums for the Metachunks and Metablocks are
745
746 return raw_word_size;
747 }
748 };
749
750 uint const SpaceManager::_small_chunk_limit = 4;
751
752 const char* SpaceManager::_expand_lock_name =
753 "SpaceManager chunk allocation lock";
754 const int SpaceManager::_expand_lock_rank = Monitor::leaf - 1;
755 Mutex* const SpaceManager::_expand_lock =
756 new Mutex(SpaceManager::_expand_lock_rank,
757 SpaceManager::_expand_lock_name,
758 Mutex::_allow_vm_block_flag);
759
760 void VirtualSpaceNode::inc_container_count() {
761 assert_lock_strong(SpaceManager::expand_lock());
762 _container_count++;
763 assert(_container_count == container_count_slow(),
764 err_msg("Inconsistency in countainer_count _container_count " SIZE_FORMAT
765 "container_count_slow() " SIZE_FORMAT,
766 _container_count, container_count_slow()));
767 }
768
769 void VirtualSpaceNode::dec_container_count() {
770 assert_lock_strong(SpaceManager::expand_lock());
771 _container_count--;
772 }
773
774 #ifdef ASSERT
775 void VirtualSpaceNode::verify_container_count() {
776 assert(_container_count == container_count_slow(),
777 err_msg("Inconsistency in countainer_count _container_count " SIZE_FORMAT
778 "container_count_slow() " SIZE_FORMAT, _container_count, container_count_slow()));
779 }
780 #endif
781
782 // BlockFreelist methods
783
784 BlockFreelist::BlockFreelist() : _dictionary(NULL) {}
785
786 BlockFreelist::~BlockFreelist() {
787 if (_dictionary != NULL) {
788 if (Verbose && TraceMetadataChunkAllocation) {
789 _dictionary->print_free_lists(gclog_or_tty);
790 }
791 delete _dictionary;
792 }
793 }
794
795 Metablock* BlockFreelist::initialize_free_chunk(MetaWord* p, size_t word_size) {
796 Metablock* block = (Metablock*) p;
797 block->set_word_size(word_size);
798 block->set_prev(NULL);
1003 assert_lock_strong(SpaceManager::expand_lock());
1004 _virtual_space_count--;
1005 }
1006
1007 void ChunkManager::remove_chunk(Metachunk* chunk) {
1008 size_t word_size = chunk->word_size();
1009 ChunkIndex index = list_index(word_size);
1010 if (index != HumongousIndex) {
1011 free_chunks(index)->remove_chunk(chunk);
1012 } else {
1013 humongous_dictionary()->remove_chunk(chunk);
1014 }
1015
1016 // Chunk is being removed from the chunks free list.
1017 dec_free_chunks_total(chunk->capacity_word_size());
1018 }
1019
1020 // Walk the list of VirtualSpaceNodes and delete
1021 // nodes with a 0 container_count. Remove Metachunks in
1022 // the node from their respective freelists.
1023 void VirtualSpaceList::purge() {
1024 assert_lock_strong(SpaceManager::expand_lock());
1025 // Don't use a VirtualSpaceListIterator because this
1026 // list is being changed and a straightforward use of an iterator is not safe.
1027 VirtualSpaceNode* purged_vsl = NULL;
1028 VirtualSpaceNode* prev_vsl = virtual_space_list();
1029 VirtualSpaceNode* next_vsl = prev_vsl;
1030 while (next_vsl != NULL) {
1031 VirtualSpaceNode* vsl = next_vsl;
1032 next_vsl = vsl->next();
1033 // Don't free the current virtual space since it will likely
1034 // be needed soon.
1035 if (vsl->container_count() == 0 && vsl != current_virtual_space()) {
1036 // Unlink it from the list
1037 if (prev_vsl == vsl) {
1038 // This is the case of the current note being the first note.
1039 assert(vsl == virtual_space_list(), "Expected to be the first note");
1040 set_virtual_space_list(vsl->next());
1041 } else {
1042 prev_vsl->set_next(vsl->next());
1043 }
1044
1045 vsl->purge(chunk_manager());
1046 dec_reserved_words(vsl->reserved_words());
1047 dec_committed_words(vsl->committed_words());
1048 dec_virtual_space_count();
1049 purged_vsl = vsl;
1050 delete vsl;
1051 } else {
1052 prev_vsl = vsl;
1053 }
1054 }
1055 #ifdef ASSERT
1056 if (purged_vsl != NULL) {
1057 // List should be stable enough to use an iterator here.
1058 VirtualSpaceListIterator iter(virtual_space_list());
1059 while (iter.repeat()) {
1060 VirtualSpaceNode* vsl = iter.get_next();
1061 assert(vsl != purged_vsl, "Purge of vsl failed");
1062 }
1063 }
1064 #endif
1065 }
1066
1067 size_t VirtualSpaceList::used_words_sum() {
1068 size_t allocated_by_vs = 0;
1069 VirtualSpaceListIterator iter(virtual_space_list());
1070 while (iter.repeat()) {
1071 VirtualSpaceNode* vsl = iter.get_next();
1072 // Sum used region [bottom, top) in each virtualspace
1073 allocated_by_vs += vsl->used_words_in_vs();
1074 }
1075 assert(allocated_by_vs >= chunk_manager()->free_chunks_total_words(),
1076 err_msg("Total in free chunks " SIZE_FORMAT
1077 " greater than total from virtual_spaces " SIZE_FORMAT,
1078 allocated_by_vs, chunk_manager()->free_chunks_total_words()));
1079 size_t used =
1080 allocated_by_vs - chunk_manager()->free_chunks_total_words();
1081 return used;
1082 }
1083
1084 // Space available in all MetadataVirtualspaces allocated
1085 // for metadata. This is the upper limit on the capacity
1086 // of chunks allocated out of all the MetadataVirtualspaces.
1087 size_t VirtualSpaceList::capacity_words_sum() {
1088 size_t capacity = 0;
1089 VirtualSpaceListIterator iter(virtual_space_list());
1090 while (iter.repeat()) {
1091 VirtualSpaceNode* vsl = iter.get_next();
1092 capacity += vsl->capacity_words_in_vs();
1093 }
1094 return capacity;
1095 }
1096
1097 VirtualSpaceList::VirtualSpaceList(size_t word_size ) :
1098 _is_class(false),
1099 _virtual_space_list(NULL),
1100 _current_virtual_space(NULL),
1101 _reserved_words(0),
1102 _committed_words(0),
1103 _virtual_space_count(0) {
1104 MutexLockerEx cl(SpaceManager::expand_lock(),
1105 Mutex::_no_safepoint_check_flag);
1106 bool initialization_succeeded = grow_vs(word_size);
1107
1108 _chunk_manager.free_chunks(SpecializedIndex)->set_size(SpecializedChunk);
1109 _chunk_manager.free_chunks(SmallIndex)->set_size(SmallChunk);
1110 _chunk_manager.free_chunks(MediumIndex)->set_size(MediumChunk);
1111 assert(initialization_succeeded,
1112 " VirtualSpaceList initialization should not fail");
1113 }
1114
1115 VirtualSpaceList::VirtualSpaceList(ReservedSpace rs) :
1116 _is_class(true),
1117 _virtual_space_list(NULL),
1118 _current_virtual_space(NULL),
1119 _reserved_words(0),
1120 _committed_words(0),
1121 _virtual_space_count(0) {
1122 MutexLockerEx cl(SpaceManager::expand_lock(),
1123 Mutex::_no_safepoint_check_flag);
1124 VirtualSpaceNode* class_entry = new VirtualSpaceNode(rs);
1125 bool succeeded = class_entry->initialize();
1126 _chunk_manager.free_chunks(SpecializedIndex)->set_size(SpecializedChunk);
1127 _chunk_manager.free_chunks(SmallIndex)->set_size(ClassSmallChunk);
1128 _chunk_manager.free_chunks(MediumIndex)->set_size(ClassMediumChunk);
1129 assert(succeeded, " VirtualSpaceList initialization should not fail");
1130 link_vs(class_entry);
1131 }
1132
1133 size_t VirtualSpaceList::free_bytes() {
1134 return virtual_space_list()->free_words_in_vs() * BytesPerWord;
1135 }
1136
1137 // Allocate another meta virtual space and add it to the list.
1138 bool VirtualSpaceList::grow_vs(size_t vs_word_size) {
1139 assert_lock_strong(SpaceManager::expand_lock());
1140 if (vs_word_size == 0) {
1141 return false;
1142 }
1143 // Reserve the space
1144 size_t vs_byte_size = vs_word_size * BytesPerWord;
1145 assert(vs_byte_size % os::vm_page_size() == 0, "Not aligned");
1146
1147 // Allocate the meta virtual space and initialize it.
1148 VirtualSpaceNode* new_entry = new VirtualSpaceNode(vs_byte_size);
1178 }
1179
1180 bool VirtualSpaceList::expand_by(VirtualSpaceNode* node, size_t word_size, bool pre_touch) {
1181 size_t before = node->committed_words();
1182
1183 bool result = node->expand_by(word_size, pre_touch);
1184
1185 size_t after = node->committed_words();
1186
1187 // after and before can be the same if the memory was pre-committed.
1188 assert(after >= before, "Must be");
1189 inc_committed_words(after - before);
1190
1191 return result;
1192 }
1193
1194 Metachunk* VirtualSpaceList::get_new_chunk(size_t word_size,
1195 size_t grow_chunks_by_words,
1196 size_t medium_chunk_bunch) {
1197
1198 // Get a chunk from the chunk freelist
1199 Metachunk* next = chunk_manager()->chunk_freelist_allocate(grow_chunks_by_words);
1200
1201 if (next != NULL) {
1202 next->container()->inc_container_count();
1203 } else {
1204 // Allocate a chunk out of the current virtual space.
1205 next = current_virtual_space()->get_chunk_vs(grow_chunks_by_words);
1206 }
1207
1208 if (next == NULL) {
1209 // Not enough room in current virtual space. Try to commit
1210 // more space.
1211 size_t expand_vs_by_words = MAX2(medium_chunk_bunch,
1212 grow_chunks_by_words);
1213 size_t page_size_words = os::vm_page_size() / BytesPerWord;
1214 size_t aligned_expand_vs_by_words = align_size_up(expand_vs_by_words,
1215 page_size_words);
1216 bool vs_expanded =
1217 expand_by(current_virtual_space(), aligned_expand_vs_by_words);
1218 if (!vs_expanded) {
1219 // Should the capacity of the metaspaces be expanded for
1220 // this allocation? If it's the virtual space for classes and is
1221 // being used for CompressedHeaders, don't allocate a new virtualspace.
1222 if (can_grow() && MetaspaceGC::should_expand(this, word_size)) {
1223 // Get another virtual space.
1224 size_t grow_vs_words =
1225 MAX2((size_t)VirtualSpaceSize, aligned_expand_vs_by_words);
1226 if (grow_vs(grow_vs_words)) {
1519 if (shrink_bytes >= MinMetaspaceExpansion &&
1520 ((capacity_until_GC - shrink_bytes) >= MetaspaceSize)) {
1521 MetaspaceGC::set_capacity_until_GC(capacity_until_GC - shrink_bytes);
1522 }
1523 }
1524
1525 // Metadebug methods
1526
1527 void Metadebug::deallocate_chunk_a_lot(SpaceManager* sm,
1528 size_t chunk_word_size){
1529 #ifdef ASSERT
1530 VirtualSpaceList* vsl = sm->vs_list();
1531 if (MetaDataDeallocateALot &&
1532 Metadebug::deallocate_chunk_a_lot_count() % MetaDataDeallocateALotInterval == 0 ) {
1533 Metadebug::reset_deallocate_chunk_a_lot_count();
1534 for (uint i = 0; i < metadata_deallocate_a_lock_chunk; i++) {
1535 Metachunk* dummy_chunk = vsl->current_virtual_space()->take_from_committed(chunk_word_size);
1536 if (dummy_chunk == NULL) {
1537 break;
1538 }
1539 vsl->chunk_manager()->chunk_freelist_deallocate(dummy_chunk);
1540
1541 if (TraceMetadataChunkAllocation && Verbose) {
1542 gclog_or_tty->print("Metadebug::deallocate_chunk_a_lot: %d) ",
1543 sm->sum_count_in_chunks_in_use());
1544 dummy_chunk->print_on(gclog_or_tty);
1545 gclog_or_tty->print_cr(" Free chunks total %d count %d",
1546 vsl->chunk_manager()->free_chunks_total_words(),
1547 vsl->chunk_manager()->free_chunks_count());
1548 }
1549 }
1550 } else {
1551 Metadebug::inc_deallocate_chunk_a_lot_count();
1552 }
1553 #endif
1554 }
1555
1556 void Metadebug::deallocate_block_a_lot(SpaceManager* sm,
1557 size_t raw_word_size){
1558 #ifdef ASSERT
1559 if (MetaDataDeallocateALot &&
1560 Metadebug::deallocate_block_a_lot_count() % MetaDataDeallocateALotInterval == 0 ) {
1561 Metadebug::set_deallocate_block_a_lot_count(0);
1562 for (uint i = 0; i < metadata_deallocate_a_lot_block; i++) {
1563 MetaWord* dummy_block = sm->allocate_work(raw_word_size);
1564 if (dummy_block == 0) {
1565 break;
1566 }
1567 sm->deallocate(dummy_block, raw_word_size);
1779 gclog_or_tty->print_cr("Free list allocate humongous chunk size "
1780 SIZE_FORMAT " for requested size " SIZE_FORMAT
1781 " waste " SIZE_FORMAT,
1782 chunk->word_size(), word_size, waste);
1783 }
1784 // Chunk is being removed from the chunks free list.
1785 dec_free_chunks_total(chunk->capacity_word_size());
1786 } else {
1787 return NULL;
1788 }
1789 }
1790
1791 // Remove it from the links to this freelist
1792 chunk->set_next(NULL);
1793 chunk->set_prev(NULL);
1794 #ifdef ASSERT
1795 // Chunk is no longer on any freelist. Setting to false make container_count_slow()
1796 // work.
1797 chunk->set_is_free(false);
1798 #endif
1799 slow_locked_verify();
1800 return chunk;
1801 }
1802
1803 Metachunk* ChunkManager::chunk_freelist_allocate(size_t word_size) {
1804 assert_lock_strong(SpaceManager::expand_lock());
1805 slow_locked_verify();
1806
1807 // Take from the beginning of the list
1808 Metachunk* chunk = free_chunks_get(word_size);
1809 if (chunk == NULL) {
1810 return NULL;
1811 }
1812
1813 assert((word_size <= chunk->word_size()) ||
1814 list_index(chunk->word_size() == HumongousIndex),
1815 "Non-humongous variable sized chunk");
1816 if (TraceMetadataChunkAllocation) {
1817 size_t list_count;
1818 if (list_index(word_size) < HumongousIndex) {
1962 chunk = chunk->next();
1963 }
1964 }
1965 return used;
1966 }
1967
1968 void SpaceManager::locked_print_chunks_in_use_on(outputStream* st) const {
1969
1970 for (ChunkIndex i = ZeroIndex; i < NumberOfInUseLists; i = next_chunk_index(i)) {
1971 Metachunk* chunk = chunks_in_use(i);
1972 st->print("SpaceManager: %s " PTR_FORMAT,
1973 chunk_size_name(i), chunk);
1974 if (chunk != NULL) {
1975 st->print_cr(" free " SIZE_FORMAT,
1976 chunk->free_word_size());
1977 } else {
1978 st->print_cr("");
1979 }
1980 }
1981
1982 vs_list()->chunk_manager()->locked_print_free_chunks(st);
1983 vs_list()->chunk_manager()->locked_print_sum_free_chunks(st);
1984 }
1985
1986 size_t SpaceManager::calc_chunk_size(size_t word_size) {
1987
1988 // Decide between a small chunk and a medium chunk. Up to
1989 // _small_chunk_limit small chunks can be allocated but
1990 // once a medium chunk has been allocated, no more small
1991 // chunks will be allocated.
1992 size_t chunk_word_size;
1993 if (chunks_in_use(MediumIndex) == NULL &&
1994 sum_count_in_chunks_in_use(SmallIndex) < _small_chunk_limit) {
1995 chunk_word_size = (size_t) small_chunk_size();
1996 if (word_size + Metachunk::overhead() > small_chunk_size()) {
1997 chunk_word_size = medium_chunk_size();
1998 }
1999 } else {
2000 chunk_word_size = medium_chunk_size();
2001 }
2002
2003 // Might still need a humongous chunk. Enforce an
2068 i < NumberOfInUseLists ;
2069 i = next_chunk_index(i) ) {
2070 st->print_cr(" chunks_in_use " PTR_FORMAT " chunk size " PTR_FORMAT,
2071 chunks_in_use(i),
2072 chunks_in_use(i) == NULL ? 0 : chunks_in_use(i)->word_size());
2073 }
2074 st->print_cr(" waste: Small " SIZE_FORMAT " Medium " SIZE_FORMAT
2075 " Humongous " SIZE_FORMAT,
2076 sum_waste_in_chunks_in_use(SmallIndex),
2077 sum_waste_in_chunks_in_use(MediumIndex),
2078 sum_waste_in_chunks_in_use(HumongousIndex));
2079 // block free lists
2080 if (block_freelists() != NULL) {
2081 st->print_cr("total in block free lists " SIZE_FORMAT,
2082 block_freelists()->total_size());
2083 }
2084 }
2085
2086 SpaceManager::SpaceManager(Metaspace::MetadataType mdtype,
2087 Mutex* lock,
2088 VirtualSpaceList* vs_list) :
2089 _vs_list(vs_list),
2090 _mdtype(mdtype),
2091 _allocated_blocks_words(0),
2092 _allocated_chunks_words(0),
2093 _allocated_chunks_count(0),
2094 _lock(lock)
2095 {
2096 initialize();
2097 }
2098
2099 void SpaceManager::inc_size_metrics(size_t words) {
2100 assert_lock_strong(SpaceManager::expand_lock());
2101 // Total of allocated Metachunks and allocated Metachunks count
2102 // for each SpaceManager
2103 _allocated_chunks_words = _allocated_chunks_words + words;
2104 _allocated_chunks_count++;
2105 // Global total of capacity in allocated Metachunks
2106 MetaspaceAux::inc_capacity(mdtype(), words);
2107 // Global total of allocated Metablocks.
2108 // used_words_slow() includes the overhead in each
2109 // Metachunk so include it in the used when the
2155 cur->container()->dec_container_count();
2156 // Capture the next link before it is changed
2157 // by the call to return_chunk_at_head();
2158 Metachunk* next = cur->next();
2159 cur->set_is_free(true);
2160 list->return_chunk_at_head(cur);
2161 cur = next;
2162 }
2163 }
2164
2165 SpaceManager::~SpaceManager() {
2166 // This call this->_lock which can't be done while holding expand_lock()
2167 assert(sum_capacity_in_chunks_in_use() == allocated_chunks_words(),
2168 err_msg("sum_capacity_in_chunks_in_use() " SIZE_FORMAT
2169 " allocated_chunks_words() " SIZE_FORMAT,
2170 sum_capacity_in_chunks_in_use(), allocated_chunks_words()));
2171
2172 MutexLockerEx fcl(SpaceManager::expand_lock(),
2173 Mutex::_no_safepoint_check_flag);
2174
2175 ChunkManager* chunk_manager = vs_list()->chunk_manager();
2176
2177 chunk_manager->slow_locked_verify();
2178
2179 dec_total_from_size_metrics();
2180
2181 if (TraceMetadataChunkAllocation && Verbose) {
2182 gclog_or_tty->print_cr("~SpaceManager(): " PTR_FORMAT, this);
2183 locked_print_chunks_in_use_on(gclog_or_tty);
2184 }
2185
2186 // Do not mangle freed Metachunks. The chunk size inside Metachunks
2187 // is during the freeing of a VirtualSpaceNodes.
2188
2189 // Have to update before the chunks_in_use lists are emptied
2190 // below.
2191 chunk_manager->inc_free_chunks_total(allocated_chunks_words(),
2192 sum_count_in_chunks_in_use());
2193
2194 // Add all the chunks in use by this space manager
2195 // to the global list of free chunks.
2196
2197 // Follow each list of chunks-in-use and add them to the
2198 // free lists. Each list is NULL terminated.
2199
2200 for (ChunkIndex i = ZeroIndex; i < HumongousIndex; i = next_chunk_index(i)) {
2201 if (TraceMetadataChunkAllocation && Verbose) {
2202 gclog_or_tty->print_cr("returned %d %s chunks to freelist",
2203 sum_count_in_chunks_in_use(i),
2204 chunk_size_name(i));
2205 }
2206 Metachunk* chunks = chunks_in_use(i);
2207 chunk_manager->return_chunks(i, chunks);
2208 set_chunks_in_use(i, NULL);
2209 if (TraceMetadataChunkAllocation && Verbose) {
2210 gclog_or_tty->print_cr("updated freelist count %d %s",
2211 chunk_manager->free_chunks(i)->count(),
2212 chunk_size_name(i));
2213 }
2214 assert(i != HumongousIndex, "Humongous chunks are handled explicitly later");
2215 }
2216
2217 // The medium chunk case may be optimized by passing the head and
2218 // tail of the medium chunk list to add_at_head(). The tail is often
2219 // the current chunk but there are probably exceptions.
2220
2221 // Humongous chunks
2222 if (TraceMetadataChunkAllocation && Verbose) {
2223 gclog_or_tty->print_cr("returned %d %s humongous chunks to dictionary",
2224 sum_count_in_chunks_in_use(HumongousIndex),
2225 chunk_size_name(HumongousIndex));
2226 gclog_or_tty->print("Humongous chunk dictionary: ");
2227 }
2228 // Humongous chunks are never the current chunk.
2229 Metachunk* humongous_chunks = chunks_in_use(HumongousIndex);
2230
2231 while (humongous_chunks != NULL) {
2232 #ifdef ASSERT
2233 humongous_chunks->set_is_free(true);
2234 #endif
2235 if (TraceMetadataChunkAllocation && Verbose) {
2236 gclog_or_tty->print(PTR_FORMAT " (" SIZE_FORMAT ") ",
2237 humongous_chunks,
2238 humongous_chunks->word_size());
2239 }
2240 assert(humongous_chunks->word_size() == (size_t)
2241 align_size_up(humongous_chunks->word_size(),
2242 HumongousChunkGranularity),
2243 err_msg("Humongous chunk size is wrong: word size " SIZE_FORMAT
2244 " granularity %d",
2245 humongous_chunks->word_size(), HumongousChunkGranularity));
2246 Metachunk* next_humongous_chunks = humongous_chunks->next();
2247 humongous_chunks->container()->dec_container_count();
2248 chunk_manager->humongous_dictionary()->return_chunk(humongous_chunks);
2249 humongous_chunks = next_humongous_chunks;
2250 }
2251 if (TraceMetadataChunkAllocation && Verbose) {
2252 gclog_or_tty->print_cr("");
2253 gclog_or_tty->print_cr("updated dictionary count %d %s",
2254 chunk_manager->humongous_dictionary()->total_count(),
2255 chunk_size_name(HumongousIndex));
2256 }
2257 chunk_manager->slow_locked_verify();
2258 }
2259
2260 const char* SpaceManager::chunk_size_name(ChunkIndex index) const {
2261 switch (index) {
2262 case SpecializedIndex:
2263 return "Specialized";
2264 case SmallIndex:
2265 return "Small";
2266 case MediumIndex:
2267 return "Medium";
2268 case HumongousIndex:
2269 return "Humongous";
2270 default:
2271 return NULL;
2272 }
2273 }
2274
2275 ChunkIndex ChunkManager::list_index(size_t size) {
2276 switch (size) {
2277 case SpecializedChunk:
2326 set_current_chunk(new_chunk);
2327 }
2328 // Link at head. The _current_chunk only points to a humongous chunk for
2329 // the null class loader metaspace (class and data virtual space managers)
2330 // any humongous chunks so will not point to the tail
2331 // of the humongous chunks list.
2332 new_chunk->set_next(chunks_in_use(HumongousIndex));
2333 set_chunks_in_use(HumongousIndex, new_chunk);
2334
2335 assert(new_chunk->word_size() > medium_chunk_size(), "List inconsistency");
2336 }
2337
2338 // Add to the running sum of capacity
2339 inc_size_metrics(new_chunk->word_size());
2340
2341 assert(new_chunk->is_empty(), "Not ready for reuse");
2342 if (TraceMetadataChunkAllocation && Verbose) {
2343 gclog_or_tty->print("SpaceManager::add_chunk: %d) ",
2344 sum_count_in_chunks_in_use());
2345 new_chunk->print_on(gclog_or_tty);
2346 if (vs_list() != NULL) {
2347 vs_list()->chunk_manager()->locked_print_free_chunks(gclog_or_tty);
2348 }
2349 }
2350 }
2351
2352 void SpaceManager::retire_current_chunk() {
2353 if (current_chunk() != NULL) {
2354 size_t remaining_words = current_chunk()->free_word_size();
2355 if (remaining_words >= TreeChunk<Metablock, FreeList>::min_size()) {
2356 block_freelists()->return_block(current_chunk()->allocate(remaining_words), remaining_words);
2357 inc_used_metrics(remaining_words);
2358 }
2359 }
2360 }
2361
2362 Metachunk* SpaceManager::get_new_chunk(size_t word_size,
2363 size_t grow_chunks_by_words) {
2364
2365 Metachunk* next = vs_list()->get_new_chunk(word_size,
2366 grow_chunks_by_words,
2367 medium_chunk_bunch());
2368
2369 if (TraceMetadataHumongousAllocation && next != NULL &&
2370 SpaceManager::is_humongous(next->word_size())) {
2371 gclog_or_tty->print_cr(" new humongous chunk word size "
2372 PTR_FORMAT, next->word_size());
2373 }
2374
2375 return next;
2376 }
2377
2378 MetaWord* SpaceManager::allocate(size_t word_size) {
2379 MutexLockerEx cl(lock(), Mutex::_no_safepoint_check_flag);
2380
2381 size_t raw_word_size = get_raw_word_size(word_size);
2382 BlockFreelist* fl = block_freelists();
2383 MetaWord* p = NULL;
2384 // Allocation from the dictionary is expensive in the sense that
2385 // the dictionary has to be searched for a size. Don't allocate
2386 // from the dictionary until it starts to get fat. Is this
2387 // a reasonable policy? Maybe an skinny dictionary is fast enough
2627 " class_capacity " SIZE_FORMAT " non_class_capacity " SIZE_FORMAT,
2628 allocated_capacity_bytes(), class_capacity + non_class_capacity,
2629 class_capacity, non_class_capacity));
2630
2631 return class_capacity + non_class_capacity;
2632 }
2633
2634 size_t MetaspaceAux::reserved_bytes(Metaspace::MetadataType mdtype) {
2635 VirtualSpaceList* list = Metaspace::get_space_list(mdtype);
2636 return list == NULL ? 0 : list->reserved_bytes();
2637 }
2638
2639 size_t MetaspaceAux::committed_bytes(Metaspace::MetadataType mdtype) {
2640 VirtualSpaceList* list = Metaspace::get_space_list(mdtype);
2641 return list == NULL ? 0 : list->committed_bytes();
2642 }
2643
2644 size_t MetaspaceAux::min_chunk_size_words() { return Metaspace::first_chunk_word_size(); }
2645
2646 size_t MetaspaceAux::free_chunks_total_words(Metaspace::MetadataType mdtype) {
2647 VirtualSpaceList* list = Metaspace::get_space_list(mdtype);
2648 if (list == NULL) {
2649 return 0;
2650 }
2651 ChunkManager* chunk = list->chunk_manager();
2652 chunk->slow_verify();
2653 return chunk->free_chunks_total_words();
2654 }
2655
2656 size_t MetaspaceAux::free_chunks_total_bytes(Metaspace::MetadataType mdtype) {
2657 return free_chunks_total_words(mdtype) * BytesPerWord;
2658 }
2659
2660 size_t MetaspaceAux::free_chunks_total_words() {
2661 return free_chunks_total_words(Metaspace::ClassType) +
2662 free_chunks_total_words(Metaspace::NonClassType);
2663 }
2664
2665 size_t MetaspaceAux::free_chunks_total_bytes() {
2666 return free_chunks_total_words() * BytesPerWord;
2667 }
2668
2669 void MetaspaceAux::print_metaspace_change(size_t prev_metadata_used) {
2670 gclog_or_tty->print(", [Metaspace:");
2671 if (PrintGCDetails && Verbose) {
2672 gclog_or_tty->print(" " SIZE_FORMAT
2673 "->" SIZE_FORMAT
2784 out->print_cr(" data: " SIZE_FORMAT " specialized(s) " SIZE_FORMAT ", "
2785 SIZE_FORMAT " small(s) " SIZE_FORMAT ", "
2786 SIZE_FORMAT " medium(s) " SIZE_FORMAT ", "
2787 "large count " SIZE_FORMAT,
2788 specialized_count, specialized_waste, small_count,
2789 small_waste, medium_count, medium_waste, humongous_count);
2790 if (Metaspace::using_class_space()) {
2791 print_class_waste(out);
2792 }
2793 }
2794
2795 // Dump global metaspace things from the end of ClassLoaderDataGraph
2796 void MetaspaceAux::dump(outputStream* out) {
2797 out->print_cr("All Metaspace:");
2798 out->print("data space: "); print_on(out, Metaspace::NonClassType);
2799 out->print("class space: "); print_on(out, Metaspace::ClassType);
2800 print_waste(out);
2801 }
2802
2803 void MetaspaceAux::verify_free_chunks() {
2804 Metaspace::space_list()->chunk_manager()->verify();
2805 if (Metaspace::using_class_space()) {
2806 Metaspace::class_space_list()->chunk_manager()->verify();
2807 }
2808 }
2809
2810 void MetaspaceAux::verify_capacity() {
2811 #ifdef ASSERT
2812 size_t running_sum_capacity_bytes = allocated_capacity_bytes();
2813 // For purposes of the running sum of capacity, verify against capacity
2814 size_t capacity_in_use_bytes = capacity_bytes_slow();
2815 assert(running_sum_capacity_bytes == capacity_in_use_bytes,
2816 err_msg("allocated_capacity_words() * BytesPerWord " SIZE_FORMAT
2817 " capacity_bytes_slow()" SIZE_FORMAT,
2818 running_sum_capacity_bytes, capacity_in_use_bytes));
2819 for (Metaspace::MetadataType i = Metaspace::ClassType;
2820 i < Metaspace:: MetadataTypeCount;
2821 i = (Metaspace::MetadataType)(i + 1)) {
2822 size_t capacity_in_use_bytes = capacity_bytes_slow(i);
2823 assert(allocated_capacity_bytes(i) == capacity_in_use_bytes,
2824 err_msg("allocated_capacity_bytes(%u) " SIZE_FORMAT
2825 " capacity_bytes_slow(%u)" SIZE_FORMAT,
2826 i, allocated_capacity_bytes(i), i, capacity_in_use_bytes));
2857
2858 // Metaspace methods
2859
2860 size_t Metaspace::_first_chunk_word_size = 0;
2861 size_t Metaspace::_first_class_chunk_word_size = 0;
2862
2863 Metaspace::Metaspace(Mutex* lock, MetaspaceType type) {
2864 initialize(lock, type);
2865 }
2866
2867 Metaspace::~Metaspace() {
2868 delete _vsm;
2869 if (using_class_space()) {
2870 delete _class_vsm;
2871 }
2872 }
2873
2874 VirtualSpaceList* Metaspace::_space_list = NULL;
2875 VirtualSpaceList* Metaspace::_class_space_list = NULL;
2876
2877 #define VIRTUALSPACEMULTIPLIER 2
2878
2879 #ifdef _LP64
2880 void Metaspace::set_narrow_klass_base_and_shift(address metaspace_base, address cds_base) {
2881 // Figure out the narrow_klass_base and the narrow_klass_shift. The
2882 // narrow_klass_base is the lower of the metaspace base and the cds base
2883 // (if cds is enabled). The narrow_klass_shift depends on the distance
2884 // between the lower base and higher address.
2885 address lower_base;
2886 address higher_address;
2887 if (UseSharedSpaces) {
2888 higher_address = MAX2((address)(cds_base + FileMapInfo::shared_spaces_size()),
2889 (address)(metaspace_base + class_metaspace_size()));
2890 lower_base = MIN2(metaspace_base, cds_base);
2891 } else {
2892 higher_address = metaspace_base + class_metaspace_size();
2893 lower_base = metaspace_base;
2894 }
2895 Universe::set_narrow_klass_base(lower_base);
2896 if ((uint64_t)(higher_address - lower_base) < (uint64_t)max_juint) {
2964 UseSharedSpaces ? (address)cds_base : 0);
2965
2966 initialize_class_space(metaspace_rs);
2967
2968 if (PrintCompressedOopsMode || (PrintMiscellaneous && Verbose)) {
2969 gclog_or_tty->print_cr("Narrow klass base: " PTR_FORMAT ", Narrow klass shift: " SIZE_FORMAT,
2970 Universe::narrow_klass_base(), Universe::narrow_klass_shift());
2971 gclog_or_tty->print_cr("Metaspace Size: " SIZE_FORMAT " Address: " PTR_FORMAT " Req Addr: " PTR_FORMAT,
2972 class_metaspace_size(), metaspace_rs.base(), requested_addr);
2973 }
2974 }
2975
2976 // For UseCompressedClassPointers the class space is reserved above the top of
2977 // the Java heap. The argument passed in is at the base of the compressed space.
2978 void Metaspace::initialize_class_space(ReservedSpace rs) {
2979 // The reserved space size may be bigger because of alignment, esp with UseLargePages
2980 assert(rs.size() >= CompressedClassSpaceSize,
2981 err_msg(SIZE_FORMAT " != " UINTX_FORMAT, rs.size(), CompressedClassSpaceSize));
2982 assert(using_class_space(), "Must be using class space");
2983 _class_space_list = new VirtualSpaceList(rs);
2984 }
2985
2986 #endif
2987
2988 void Metaspace::global_initialize() {
2989 // Initialize the alignment for shared spaces.
2990 int max_alignment = os::vm_page_size();
2991 size_t cds_total = 0;
2992
2993 set_class_metaspace_size(align_size_up(CompressedClassSpaceSize,
2994 os::vm_allocation_granularity()));
2995
2996 MetaspaceShared::set_max_alignment(max_alignment);
2997
2998 if (DumpSharedSpaces) {
2999 SharedReadOnlySize = align_size_up(SharedReadOnlySize, max_alignment);
3000 SharedReadWriteSize = align_size_up(SharedReadWriteSize, max_alignment);
3001 SharedMiscDataSize = align_size_up(SharedMiscDataSize, max_alignment);
3002 SharedMiscCodeSize = align_size_up(SharedMiscCodeSize, max_alignment);
3003
3004 // Initialize with the sum of the shared space sizes. The read-only
3005 // and read write metaspace chunks will be allocated out of this and the
3006 // remainder is the misc code and data chunks.
3007 cds_total = FileMapInfo::shared_spaces_size();
3008 _space_list = new VirtualSpaceList(cds_total/wordSize);
3009
3010 #ifdef _LP64
3011 // Set the compressed klass pointer base so that decoding of these pointers works
3012 // properly when creating the shared archive.
3013 assert(UseCompressedOops && UseCompressedClassPointers,
3014 "UseCompressedOops and UseCompressedClassPointers must be set");
3015 Universe::set_narrow_klass_base((address)_space_list->current_virtual_space()->bottom());
3016 if (TraceMetavirtualspaceAllocation && Verbose) {
3017 gclog_or_tty->print_cr("Setting_narrow_klass_base to Address: " PTR_FORMAT,
3018 _space_list->current_virtual_space()->bottom());
3019 }
3020
3021 // Set the shift to zero.
3022 assert(class_metaspace_size() < (uint64_t)(max_juint) - cds_total,
3023 "CDS region is too large");
3024 Universe::set_narrow_klass_shift(0);
3025 #endif
3026
3027 } else {
3028 // If using shared space, open the file that contains the shared space
3056 } else {
3057 allocate_metaspace_compressed_klass_ptrs((char *)CompressedKlassPointersBase, 0);
3058 }
3059 }
3060 #endif
3061
3062 // Initialize these before initializing the VirtualSpaceList
3063 _first_chunk_word_size = InitialBootClassLoaderMetaspaceSize / BytesPerWord;
3064 _first_chunk_word_size = align_word_size_up(_first_chunk_word_size);
3065 // Make the first class chunk bigger than a medium chunk so it's not put
3066 // on the medium chunk list. The next chunk will be small and progress
3067 // from there. This size calculated by -version.
3068 _first_class_chunk_word_size = MIN2((size_t)MediumChunk*6,
3069 (CompressedClassSpaceSize/BytesPerWord)*2);
3070 _first_class_chunk_word_size = align_word_size_up(_first_class_chunk_word_size);
3071 // Arbitrarily set the initial virtual space to a multiple
3072 // of the boot class loader size.
3073 size_t word_size = VIRTUALSPACEMULTIPLIER * first_chunk_word_size();
3074 // Initialize the list of virtual spaces.
3075 _space_list = new VirtualSpaceList(word_size);
3076 }
3077 }
3078
3079 void Metaspace::initialize(Mutex* lock, MetaspaceType type) {
3080
3081 assert(space_list() != NULL,
3082 "Metadata VirtualSpaceList has not been initialized");
3083
3084 _vsm = new SpaceManager(NonClassType, lock, space_list());
3085 if (_vsm == NULL) {
3086 return;
3087 }
3088 size_t word_size;
3089 size_t class_word_size;
3090 vsm()->get_initial_chunk_sizes(type, &word_size, &class_word_size);
3091
3092 if (using_class_space()) {
3093 assert(class_space_list() != NULL,
3094 "Class VirtualSpaceList has not been initialized");
3095
3096 // Allocate SpaceManager for classes.
3097 _class_vsm = new SpaceManager(ClassType, lock, class_space_list());
3098 if (_class_vsm == NULL) {
3099 return;
3100 }
3101 }
3102
3103 MutexLockerEx cl(SpaceManager::expand_lock(), Mutex::_no_safepoint_check_flag);
3104
3105 // Allocate chunk for metadata objects
3106 Metachunk* new_chunk =
3107 space_list()->get_initialization_chunk(word_size,
3108 vsm()->medium_chunk_bunch());
3109 assert(!DumpSharedSpaces || new_chunk != NULL, "should have enough space for both chunks");
3110 if (new_chunk != NULL) {
3111 // Add to this manager's list of chunks in use and current_chunk().
3112 vsm()->add_chunk(new_chunk, true);
3113 }
3114
3115 // Allocate chunk for class metadata objects
3116 if (using_class_space()) {
3117 Metachunk* class_chunk =
3118 class_space_list()->get_initialization_chunk(class_word_size,
3119 class_vsm()->medium_chunk_bunch());
3120 if (class_chunk != NULL) {
3121 class_vsm()->add_chunk(class_chunk, true);
3122 }
3123 }
3124
3125 _alloc_record_head = NULL;
3126 _alloc_record_tail = NULL;
3127 }
3128
3129 size_t Metaspace::align_word_size_up(size_t word_size) {
3130 size_t byte_size = word_size * wordSize;
3131 return ReservedSpace::allocation_align_size_up(byte_size) / wordSize;
3132 }
3133
3134 MetaWord* Metaspace::allocate(size_t word_size, MetadataType mdtype) {
3135 // DumpSharedSpaces doesn't use class metadata area (yet)
3136 // Also, don't use class_vsm() unless UseCompressedClassPointers is true.
3137 if (mdtype == ClassType && using_class_space()) {
3138 return class_vsm()->allocate(word_size);
3316 void Metaspace::iterate(Metaspace::AllocRecordClosure *closure) {
3317 assert(DumpSharedSpaces, "unimplemented for !DumpSharedSpaces");
3318
3319 address last_addr = (address)bottom();
3320
3321 for (AllocRecord *rec = _alloc_record_head; rec; rec = rec->_next) {
3322 address ptr = rec->_ptr;
3323 if (last_addr < ptr) {
3324 closure->doit(last_addr, MetaspaceObj::UnknownType, ptr - last_addr);
3325 }
3326 closure->doit(ptr, rec->_type, rec->_byte_size);
3327 last_addr = ptr + rec->_byte_size;
3328 }
3329
3330 address top = ((address)bottom()) + used_bytes_slow(Metaspace::NonClassType);
3331 if (last_addr < top) {
3332 closure->doit(last_addr, MetaspaceObj::UnknownType, top - last_addr);
3333 }
3334 }
3335
3336 void Metaspace::purge() {
3337 MutexLockerEx cl(SpaceManager::expand_lock(),
3338 Mutex::_no_safepoint_check_flag);
3339 space_list()->purge();
3340 if (using_class_space()) {
3341 class_space_list()->purge();
3342 }
3343 }
3344
3345 void Metaspace::print_on(outputStream* out) const {
3346 // Print both class virtual space counts and metaspace.
3347 if (Verbose) {
3348 vsm()->print_on(out);
3349 if (using_class_space()) {
3350 class_vsm()->print_on(out);
3351 }
3352 }
3353 }
3354
3355 bool Metaspace::contains(const void * ptr) {
3356 if (MetaspaceShared::is_in_shared_space(ptr)) {
3357 return true;
3358 }
3359 // This is checked while unlocked. As long as the virtualspaces are added
3360 // at the end, the pointer will be in one of them. The virtual spaces
3361 // aren't deleted presently. When they are, some sort of locking might
|
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24 #include "precompiled.hpp"
25 #include "gc_interface/collectedHeap.hpp"
26 #include "memory/allocation.hpp"
27 #include "memory/binaryTreeDictionary.hpp"
28 #include "memory/freeList.hpp"
29 #include "memory/collectorPolicy.hpp"
30 #include "memory/filemap.hpp"
31 #include "memory/freeList.hpp"
32 #include "memory/metablock.hpp"
33 #include "memory/metachunk.hpp"
34 #include "memory/metaspace.hpp"
35 #include "memory/metaspaceShared.hpp"
36 #include "memory/resourceArea.hpp"
37 #include "memory/universe.hpp"
38 #include "runtime/globals.hpp"
39 #include "runtime/java.hpp"
40 #include "runtime/mutex.hpp"
41 #include "runtime/orderAccess.hpp"
42 #include "services/memTracker.hpp"
43 #include "utilities/copy.hpp"
44 #include "utilities/debug.hpp"
45
46 typedef BinaryTreeDictionary<Metablock, FreeList> BlockTreeDictionary;
95 bool MetaspaceGC::_should_concurrent_collect = false;
96
97 // Blocks of space for metadata are allocated out of Metachunks.
98 //
99 // Metachunk are allocated out of MetadataVirtualspaces and once
100 // allocated there is no explicit link between a Metachunk and
101 // the MetadataVirtualspaces from which it was allocated.
102 //
103 // Each SpaceManager maintains a
104 // list of the chunks it is using and the current chunk. The current
105 // chunk is the chunk from which allocations are done. Space freed in
106 // a chunk is placed on the free list of blocks (BlockFreelist) and
107 // reused from there.
108
109 typedef class FreeList<Metachunk> ChunkList;
110
111 // Manages the global free lists of chunks.
112 // Has three lists of free chunks, and a total size and
113 // count that includes all three
114
115 class ChunkManager : public CHeapObj<mtInternal> {
116
117 // Free list of chunks of different sizes.
118 // SpecializedChunk
119 // SmallChunk
120 // MediumChunk
121 // HumongousChunk
122 ChunkList _free_chunks[NumberOfFreeLists];
123
124
125 // HumongousChunk
126 ChunkTreeDictionary _humongous_dictionary;
127
128 // ChunkManager in all lists of this type
129 size_t _free_chunks_total;
130 size_t _free_chunks_count;
131
132 void dec_free_chunks_total(size_t v) {
133 assert(_free_chunks_count > 0 &&
134 _free_chunks_total > 0,
135 "About to go negative");
142
143 size_t sum_free_chunks();
144 size_t sum_free_chunks_count();
145
146 void locked_verify_free_chunks_total();
147 void slow_locked_verify_free_chunks_total() {
148 if (metaspace_slow_verify) {
149 locked_verify_free_chunks_total();
150 }
151 }
152 void locked_verify_free_chunks_count();
153 void slow_locked_verify_free_chunks_count() {
154 if (metaspace_slow_verify) {
155 locked_verify_free_chunks_count();
156 }
157 }
158 void verify_free_chunks_count();
159
160 public:
161
162 ChunkManager(size_t specialized, size_t small, size_t medium)
163 : _free_chunks_total(0), _free_chunks_count(0) {
164 free_chunks(SpecializedIndex)->set_size(specialized);
165 free_chunks(SmallIndex)->set_size(small);
166 free_chunks(MediumIndex)->set_size(medium);
167 }
168
169 // add or delete (return) a chunk to the global freelist.
170 Metachunk* chunk_freelist_allocate(size_t word_size);
171 void chunk_freelist_deallocate(Metachunk* chunk);
172
173 // Map a size to a list index assuming that there are lists
174 // for special, small, medium, and humongous chunks.
175 static ChunkIndex list_index(size_t size);
176
177 // Remove the chunk from its freelist. It is
178 // expected to be on one of the _free_chunks[] lists.
179 void remove_chunk(Metachunk* chunk);
180
181 // Add the simple linked list of chunks to the freelist of chunks
182 // of type index.
183 void return_chunks(ChunkIndex index, Metachunk* chunks);
184
185 // Total of the space in the free chunks list
186 size_t free_chunks_total_words();
187 size_t free_chunks_total_bytes();
265
266 // Link to next VirtualSpaceNode
267 VirtualSpaceNode* _next;
268
269 // total in the VirtualSpace
270 MemRegion _reserved;
271 ReservedSpace _rs;
272 VirtualSpace _virtual_space;
273 MetaWord* _top;
274 // count of chunks contained in this VirtualSpace
275 uintx _container_count;
276
277 // Convenience functions to access the _virtual_space
278 char* low() const { return virtual_space()->low(); }
279 char* high() const { return virtual_space()->high(); }
280
281 // The first Metachunk will be allocated at the bottom of the
282 // VirtualSpace
283 Metachunk* first_chunk() { return (Metachunk*) bottom(); }
284
285 public:
286
287 VirtualSpaceNode(size_t byte_size);
288 VirtualSpaceNode(ReservedSpace rs) : _top(NULL), _next(NULL), _rs(rs), _container_count(0) {}
289 ~VirtualSpaceNode();
290
291 // Convenience functions for logical bottom and end
292 MetaWord* bottom() const { return (MetaWord*) _virtual_space.low(); }
293 MetaWord* end() const { return (MetaWord*) _virtual_space.high(); }
294
295 size_t reserved_words() const { return _virtual_space.reserved_size() / BytesPerWord; }
296 size_t expanded_words() const { return _virtual_space.committed_size() / BytesPerWord; }
297 size_t committed_words() const { return _virtual_space.actual_committed_size() / BytesPerWord; }
298
299 // address of next available space in _virtual_space;
300 // Accessors
301 VirtualSpaceNode* next() { return _next; }
302 void set_next(VirtualSpaceNode* v) { _next = v; }
303
304 void set_reserved(MemRegion const v) { _reserved = v; }
305 void set_top(MetaWord* v) { _top = v; }
306
307 // Accessors
308 MemRegion* reserved() { return &_reserved; }
309 VirtualSpace* virtual_space() const { return (VirtualSpace*) &_virtual_space; }
310
311 // Returns true if "word_size" is available in the VirtualSpace
312 bool is_available(size_t word_size) { return _top + word_size <= end(); }
313
314 MetaWord* top() const { return _top; }
315 void inc_top(size_t word_size) { _top += word_size; }
316
317 uintx container_count() { return _container_count; }
318 void inc_container_count();
319 void dec_container_count();
320 #ifdef ASSERT
321 uint container_count_slow();
322 void verify_container_count();
323 #endif
324
325 // used and capacity in this single entry in the list
326 size_t used_words_in_vs() const;
327 size_t capacity_words_in_vs() const;
328 size_t free_words_in_vs() const;
329
330 bool initialize();
331
332 // get space from the virtual space
333 Metachunk* take_from_committed(size_t chunk_word_size);
334
335 // Allocate a chunk from the virtual space and return it.
336 Metachunk* get_chunk_vs(size_t chunk_word_size);
337
338 // Expands/shrinks the committed space in a virtual space. Delegates
339 // to Virtualspace
340 bool expand_by(size_t words, bool pre_touch = false);
341
407 }
408 return count;
409 }
410 #endif
411
412 // List of VirtualSpaces for metadata allocation.
413 // It has a _next link for singly linked list and a MemRegion
414 // for total space in the VirtualSpace.
415 class VirtualSpaceList : public CHeapObj<mtClass> {
416 friend class VirtualSpaceNode;
417
418 enum VirtualSpaceSizes {
419 VirtualSpaceSize = 256 * K
420 };
421
422 // Global list of virtual spaces
423 // Head of the list
424 VirtualSpaceNode* _virtual_space_list;
425 // virtual space currently being used for allocations
426 VirtualSpaceNode* _current_virtual_space;
427
428 // Can this virtual list allocate >1 spaces? Also, used to determine
429 // whether to allocate unlimited small chunks in this virtual space
430 bool _is_class;
431 bool can_grow() const { return !is_class() || !UseCompressedClassPointers; }
432
433 // Sum of reserved and committed memory in the virtual spaces
434 size_t _reserved_words;
435 size_t _committed_words;
436
437 // Number of virtual spaces
438 size_t _virtual_space_count;
439
440 ~VirtualSpaceList();
441
442 VirtualSpaceNode* virtual_space_list() const { return _virtual_space_list; }
443
444 void set_virtual_space_list(VirtualSpaceNode* v) {
445 _virtual_space_list = v;
446 }
459 VirtualSpaceList(size_t word_size);
460 VirtualSpaceList(ReservedSpace rs);
461
462 size_t free_bytes();
463
464 Metachunk* get_new_chunk(size_t word_size,
465 size_t grow_chunks_by_words,
466 size_t medium_chunk_bunch);
467
468 bool expand_by(VirtualSpaceNode* node, size_t word_size, bool pre_touch = false);
469
470 // Get the first chunk for a Metaspace. Used for
471 // special cases such as the boot class loader, reflection
472 // class loader and anonymous class loader.
473 Metachunk* get_initialization_chunk(size_t word_size, size_t chunk_bunch);
474
475 VirtualSpaceNode* current_virtual_space() {
476 return _current_virtual_space;
477 }
478
479 bool is_class() const { return _is_class; }
480
481 // Allocate the first virtualspace.
482 void initialize(size_t word_size);
483
484 size_t reserved_words() { return _reserved_words; }
485 size_t reserved_bytes() { return reserved_words() * BytesPerWord; }
486 size_t committed_words() { return _committed_words; }
487 size_t committed_bytes() { return committed_words() * BytesPerWord; }
488
489 void inc_reserved_words(size_t v);
490 void dec_reserved_words(size_t v);
491 void inc_committed_words(size_t v);
492 void dec_committed_words(size_t v);
493 void inc_virtual_space_count();
494 void dec_virtual_space_count();
495
496 // Unlink empty VirtualSpaceNodes and free it.
497 void purge(ChunkManager* chunk_manager);
498
499 bool contains(const void *ptr);
500
501 void print_on(outputStream* st) const;
502
503 class VirtualSpaceListIterator : public StackObj {
504 VirtualSpaceNode* _virtual_spaces;
505 public:
506 VirtualSpaceListIterator(VirtualSpaceNode* virtual_spaces) :
507 _virtual_spaces(virtual_spaces) {}
508
509 bool repeat() {
510 return _virtual_spaces != NULL;
511 }
512
513 VirtualSpaceNode* get_next() {
514 VirtualSpaceNode* result = _virtual_spaces;
515 if (_virtual_spaces != NULL) {
516 _virtual_spaces = _virtual_spaces->next();
517 }
570 private:
571
572 // protects allocations and contains.
573 Mutex* const _lock;
574
575 // Type of metadata allocated.
576 Metaspace::MetadataType _mdtype;
577
578 // Chunk related size
579 size_t _medium_chunk_bunch;
580
581 // List of chunks in use by this SpaceManager. Allocations
582 // are done from the current chunk. The list is used for deallocating
583 // chunks when the SpaceManager is freed.
584 Metachunk* _chunks_in_use[NumberOfInUseLists];
585 Metachunk* _current_chunk;
586
587 // Virtual space where allocation comes from.
588 VirtualSpaceList* _vs_list;
589
590 ChunkManager* _chunk_manager;
591
592 // Number of small chunks to allocate to a manager
593 // If class space manager, small chunks are unlimited
594 static uint const _small_chunk_limit;
595
596 // Sum of all space in allocated chunks
597 size_t _allocated_blocks_words;
598
599 // Sum of all allocated chunks
600 size_t _allocated_chunks_words;
601 size_t _allocated_chunks_count;
602
603 // Free lists of blocks are per SpaceManager since they
604 // are assumed to be in chunks in use by the SpaceManager
605 // and all chunks in use by a SpaceManager are freed when
606 // the class loader using the SpaceManager is collected.
607 BlockFreelist _block_freelists;
608
609 // protects virtualspace and chunk expansions
610 static const char* _expand_lock_name;
611 static const int _expand_lock_rank;
612 static Mutex* const _expand_lock;
613
614 private:
615 // Accessors
616 Metachunk* chunks_in_use(ChunkIndex index) const { return _chunks_in_use[index]; }
617 void set_chunks_in_use(ChunkIndex index, Metachunk* v) { _chunks_in_use[index] = v; }
618
619 BlockFreelist* block_freelists() const {
620 return (BlockFreelist*) &_block_freelists;
621 }
622
623 Metaspace::MetadataType mdtype() { return _mdtype; }
624 VirtualSpaceList* vs_list() const { return _vs_list; }
625
626 ChunkManager* chunk_manager() const { return _chunk_manager; }
627
628 Metachunk* current_chunk() const { return _current_chunk; }
629 void set_current_chunk(Metachunk* v) {
630 _current_chunk = v;
631 }
632
633 Metachunk* find_current_chunk(size_t word_size);
634
635 // Add chunk to the list of chunks in use
636 void add_chunk(Metachunk* v, bool make_current);
637 void retire_current_chunk();
638
639 Mutex* lock() const { return _lock; }
640
641 const char* chunk_size_name(ChunkIndex index) const;
642
643 protected:
644 void initialize();
645
646 public:
647 SpaceManager(Metaspace::MetadataType mdtype,
648 Mutex* lock,
649 VirtualSpaceList* vs_list,
650 ChunkManager* chunk_manager);
651 ~SpaceManager();
652
653 enum ChunkMultiples {
654 MediumChunkMultiple = 4
655 };
656
657 bool is_class() { return _mdtype == Metaspace::ClassType; }
658
659 // Accessors
660 size_t specialized_chunk_size() { return SpecializedChunk; }
661 size_t small_chunk_size() { return (size_t) is_class() ? ClassSmallChunk : SmallChunk; }
662 size_t medium_chunk_size() { return (size_t) is_class() ? ClassMediumChunk : MediumChunk; }
663 size_t medium_chunk_bunch() { return medium_chunk_size() * MediumChunkMultiple; }
664
665 size_t allocated_blocks_words() const { return _allocated_blocks_words; }
666 size_t allocated_blocks_bytes() const { return _allocated_blocks_words * BytesPerWord; }
667 size_t allocated_chunks_words() const { return _allocated_chunks_words; }
668 size_t allocated_chunks_count() const { return _allocated_chunks_count; }
669
670 bool is_humongous(size_t word_size) { return word_size > medium_chunk_size(); }
671
672 static Mutex* expand_lock() { return _expand_lock; }
673
674 // Increment the per Metaspace and global running sums for Metachunks
675 // by the given size. This is used when a Metachunk to added to
676 // the in-use list.
677 void inc_size_metrics(size_t words);
678 // Increment the per Metaspace and global running sums Metablocks by the given
679 // size. This is used when a Metablock is allocated.
680 void inc_used_metrics(size_t words);
681 // Delete the portion of the running sums for this SpaceManager. That is,
682 // the globals running sums for the Metachunks and Metablocks are
745
746 return raw_word_size;
747 }
748 };
749
750 uint const SpaceManager::_small_chunk_limit = 4;
751
752 const char* SpaceManager::_expand_lock_name =
753 "SpaceManager chunk allocation lock";
754 const int SpaceManager::_expand_lock_rank = Monitor::leaf - 1;
755 Mutex* const SpaceManager::_expand_lock =
756 new Mutex(SpaceManager::_expand_lock_rank,
757 SpaceManager::_expand_lock_name,
758 Mutex::_allow_vm_block_flag);
759
760 void VirtualSpaceNode::inc_container_count() {
761 assert_lock_strong(SpaceManager::expand_lock());
762 _container_count++;
763 assert(_container_count == container_count_slow(),
764 err_msg("Inconsistency in countainer_count _container_count " SIZE_FORMAT
765 " container_count_slow() " SIZE_FORMAT,
766 _container_count, container_count_slow()));
767 }
768
769 void VirtualSpaceNode::dec_container_count() {
770 assert_lock_strong(SpaceManager::expand_lock());
771 _container_count--;
772 }
773
774 #ifdef ASSERT
775 void VirtualSpaceNode::verify_container_count() {
776 assert(_container_count == container_count_slow(),
777 err_msg("Inconsistency in countainer_count _container_count " SIZE_FORMAT
778 " container_count_slow() " SIZE_FORMAT, _container_count, container_count_slow()));
779 }
780 #endif
781
782 // BlockFreelist methods
783
784 BlockFreelist::BlockFreelist() : _dictionary(NULL) {}
785
786 BlockFreelist::~BlockFreelist() {
787 if (_dictionary != NULL) {
788 if (Verbose && TraceMetadataChunkAllocation) {
789 _dictionary->print_free_lists(gclog_or_tty);
790 }
791 delete _dictionary;
792 }
793 }
794
795 Metablock* BlockFreelist::initialize_free_chunk(MetaWord* p, size_t word_size) {
796 Metablock* block = (Metablock*) p;
797 block->set_word_size(word_size);
798 block->set_prev(NULL);
1003 assert_lock_strong(SpaceManager::expand_lock());
1004 _virtual_space_count--;
1005 }
1006
1007 void ChunkManager::remove_chunk(Metachunk* chunk) {
1008 size_t word_size = chunk->word_size();
1009 ChunkIndex index = list_index(word_size);
1010 if (index != HumongousIndex) {
1011 free_chunks(index)->remove_chunk(chunk);
1012 } else {
1013 humongous_dictionary()->remove_chunk(chunk);
1014 }
1015
1016 // Chunk is being removed from the chunks free list.
1017 dec_free_chunks_total(chunk->capacity_word_size());
1018 }
1019
1020 // Walk the list of VirtualSpaceNodes and delete
1021 // nodes with a 0 container_count. Remove Metachunks in
1022 // the node from their respective freelists.
1023 void VirtualSpaceList::purge(ChunkManager* chunk_manager) {
1024 assert_lock_strong(SpaceManager::expand_lock());
1025 // Don't use a VirtualSpaceListIterator because this
1026 // list is being changed and a straightforward use of an iterator is not safe.
1027 VirtualSpaceNode* purged_vsl = NULL;
1028 VirtualSpaceNode* prev_vsl = virtual_space_list();
1029 VirtualSpaceNode* next_vsl = prev_vsl;
1030 while (next_vsl != NULL) {
1031 VirtualSpaceNode* vsl = next_vsl;
1032 next_vsl = vsl->next();
1033 // Don't free the current virtual space since it will likely
1034 // be needed soon.
1035 if (vsl->container_count() == 0 && vsl != current_virtual_space()) {
1036 // Unlink it from the list
1037 if (prev_vsl == vsl) {
1038 // This is the case of the current note being the first note.
1039 assert(vsl == virtual_space_list(), "Expected to be the first note");
1040 set_virtual_space_list(vsl->next());
1041 } else {
1042 prev_vsl->set_next(vsl->next());
1043 }
1044
1045 vsl->purge(chunk_manager);
1046 dec_reserved_words(vsl->reserved_words());
1047 dec_committed_words(vsl->committed_words());
1048 dec_virtual_space_count();
1049 purged_vsl = vsl;
1050 delete vsl;
1051 } else {
1052 prev_vsl = vsl;
1053 }
1054 }
1055 #ifdef ASSERT
1056 if (purged_vsl != NULL) {
1057 // List should be stable enough to use an iterator here.
1058 VirtualSpaceListIterator iter(virtual_space_list());
1059 while (iter.repeat()) {
1060 VirtualSpaceNode* vsl = iter.get_next();
1061 assert(vsl != purged_vsl, "Purge of vsl failed");
1062 }
1063 }
1064 #endif
1065 }
1066
1067 VirtualSpaceList::VirtualSpaceList(size_t word_size ) :
1068 _is_class(false),
1069 _virtual_space_list(NULL),
1070 _current_virtual_space(NULL),
1071 _reserved_words(0),
1072 _committed_words(0),
1073 _virtual_space_count(0) {
1074 MutexLockerEx cl(SpaceManager::expand_lock(),
1075 Mutex::_no_safepoint_check_flag);
1076 bool initialization_succeeded = grow_vs(word_size);
1077 assert(initialization_succeeded,
1078 " VirtualSpaceList initialization should not fail");
1079 }
1080
1081 VirtualSpaceList::VirtualSpaceList(ReservedSpace rs) :
1082 _is_class(true),
1083 _virtual_space_list(NULL),
1084 _current_virtual_space(NULL),
1085 _reserved_words(0),
1086 _committed_words(0),
1087 _virtual_space_count(0) {
1088 MutexLockerEx cl(SpaceManager::expand_lock(),
1089 Mutex::_no_safepoint_check_flag);
1090 VirtualSpaceNode* class_entry = new VirtualSpaceNode(rs);
1091 bool succeeded = class_entry->initialize();
1092 assert(succeeded, " VirtualSpaceList initialization should not fail");
1093 link_vs(class_entry);
1094 }
1095
1096 size_t VirtualSpaceList::free_bytes() {
1097 return virtual_space_list()->free_words_in_vs() * BytesPerWord;
1098 }
1099
1100 // Allocate another meta virtual space and add it to the list.
1101 bool VirtualSpaceList::grow_vs(size_t vs_word_size) {
1102 assert_lock_strong(SpaceManager::expand_lock());
1103 if (vs_word_size == 0) {
1104 return false;
1105 }
1106 // Reserve the space
1107 size_t vs_byte_size = vs_word_size * BytesPerWord;
1108 assert(vs_byte_size % os::vm_page_size() == 0, "Not aligned");
1109
1110 // Allocate the meta virtual space and initialize it.
1111 VirtualSpaceNode* new_entry = new VirtualSpaceNode(vs_byte_size);
1141 }
1142
1143 bool VirtualSpaceList::expand_by(VirtualSpaceNode* node, size_t word_size, bool pre_touch) {
1144 size_t before = node->committed_words();
1145
1146 bool result = node->expand_by(word_size, pre_touch);
1147
1148 size_t after = node->committed_words();
1149
1150 // after and before can be the same if the memory was pre-committed.
1151 assert(after >= before, "Must be");
1152 inc_committed_words(after - before);
1153
1154 return result;
1155 }
1156
1157 Metachunk* VirtualSpaceList::get_new_chunk(size_t word_size,
1158 size_t grow_chunks_by_words,
1159 size_t medium_chunk_bunch) {
1160
1161 // Allocate a chunk out of the current virtual space.
1162 Metachunk* next = current_virtual_space()->get_chunk_vs(grow_chunks_by_words);
1163
1164 if (next == NULL) {
1165 // Not enough room in current virtual space. Try to commit
1166 // more space.
1167 size_t expand_vs_by_words = MAX2(medium_chunk_bunch,
1168 grow_chunks_by_words);
1169 size_t page_size_words = os::vm_page_size() / BytesPerWord;
1170 size_t aligned_expand_vs_by_words = align_size_up(expand_vs_by_words,
1171 page_size_words);
1172 bool vs_expanded =
1173 expand_by(current_virtual_space(), aligned_expand_vs_by_words);
1174 if (!vs_expanded) {
1175 // Should the capacity of the metaspaces be expanded for
1176 // this allocation? If it's the virtual space for classes and is
1177 // being used for CompressedHeaders, don't allocate a new virtualspace.
1178 if (can_grow() && MetaspaceGC::should_expand(this, word_size)) {
1179 // Get another virtual space.
1180 size_t grow_vs_words =
1181 MAX2((size_t)VirtualSpaceSize, aligned_expand_vs_by_words);
1182 if (grow_vs(grow_vs_words)) {
1475 if (shrink_bytes >= MinMetaspaceExpansion &&
1476 ((capacity_until_GC - shrink_bytes) >= MetaspaceSize)) {
1477 MetaspaceGC::set_capacity_until_GC(capacity_until_GC - shrink_bytes);
1478 }
1479 }
1480
1481 // Metadebug methods
1482
1483 void Metadebug::deallocate_chunk_a_lot(SpaceManager* sm,
1484 size_t chunk_word_size){
1485 #ifdef ASSERT
1486 VirtualSpaceList* vsl = sm->vs_list();
1487 if (MetaDataDeallocateALot &&
1488 Metadebug::deallocate_chunk_a_lot_count() % MetaDataDeallocateALotInterval == 0 ) {
1489 Metadebug::reset_deallocate_chunk_a_lot_count();
1490 for (uint i = 0; i < metadata_deallocate_a_lock_chunk; i++) {
1491 Metachunk* dummy_chunk = vsl->current_virtual_space()->take_from_committed(chunk_word_size);
1492 if (dummy_chunk == NULL) {
1493 break;
1494 }
1495 sm->chunk_manager()->chunk_freelist_deallocate(dummy_chunk);
1496
1497 if (TraceMetadataChunkAllocation && Verbose) {
1498 gclog_or_tty->print("Metadebug::deallocate_chunk_a_lot: %d) ",
1499 sm->sum_count_in_chunks_in_use());
1500 dummy_chunk->print_on(gclog_or_tty);
1501 gclog_or_tty->print_cr(" Free chunks total %d count %d",
1502 sm->chunk_manager()->free_chunks_total_words(),
1503 sm->chunk_manager()->free_chunks_count());
1504 }
1505 }
1506 } else {
1507 Metadebug::inc_deallocate_chunk_a_lot_count();
1508 }
1509 #endif
1510 }
1511
1512 void Metadebug::deallocate_block_a_lot(SpaceManager* sm,
1513 size_t raw_word_size){
1514 #ifdef ASSERT
1515 if (MetaDataDeallocateALot &&
1516 Metadebug::deallocate_block_a_lot_count() % MetaDataDeallocateALotInterval == 0 ) {
1517 Metadebug::set_deallocate_block_a_lot_count(0);
1518 for (uint i = 0; i < metadata_deallocate_a_lot_block; i++) {
1519 MetaWord* dummy_block = sm->allocate_work(raw_word_size);
1520 if (dummy_block == 0) {
1521 break;
1522 }
1523 sm->deallocate(dummy_block, raw_word_size);
1735 gclog_or_tty->print_cr("Free list allocate humongous chunk size "
1736 SIZE_FORMAT " for requested size " SIZE_FORMAT
1737 " waste " SIZE_FORMAT,
1738 chunk->word_size(), word_size, waste);
1739 }
1740 // Chunk is being removed from the chunks free list.
1741 dec_free_chunks_total(chunk->capacity_word_size());
1742 } else {
1743 return NULL;
1744 }
1745 }
1746
1747 // Remove it from the links to this freelist
1748 chunk->set_next(NULL);
1749 chunk->set_prev(NULL);
1750 #ifdef ASSERT
1751 // Chunk is no longer on any freelist. Setting to false make container_count_slow()
1752 // work.
1753 chunk->set_is_free(false);
1754 #endif
1755 chunk->container()->inc_container_count();
1756
1757 slow_locked_verify();
1758 return chunk;
1759 }
1760
1761 Metachunk* ChunkManager::chunk_freelist_allocate(size_t word_size) {
1762 assert_lock_strong(SpaceManager::expand_lock());
1763 slow_locked_verify();
1764
1765 // Take from the beginning of the list
1766 Metachunk* chunk = free_chunks_get(word_size);
1767 if (chunk == NULL) {
1768 return NULL;
1769 }
1770
1771 assert((word_size <= chunk->word_size()) ||
1772 list_index(chunk->word_size() == HumongousIndex),
1773 "Non-humongous variable sized chunk");
1774 if (TraceMetadataChunkAllocation) {
1775 size_t list_count;
1776 if (list_index(word_size) < HumongousIndex) {
1920 chunk = chunk->next();
1921 }
1922 }
1923 return used;
1924 }
1925
1926 void SpaceManager::locked_print_chunks_in_use_on(outputStream* st) const {
1927
1928 for (ChunkIndex i = ZeroIndex; i < NumberOfInUseLists; i = next_chunk_index(i)) {
1929 Metachunk* chunk = chunks_in_use(i);
1930 st->print("SpaceManager: %s " PTR_FORMAT,
1931 chunk_size_name(i), chunk);
1932 if (chunk != NULL) {
1933 st->print_cr(" free " SIZE_FORMAT,
1934 chunk->free_word_size());
1935 } else {
1936 st->print_cr("");
1937 }
1938 }
1939
1940 chunk_manager()->locked_print_free_chunks(st);
1941 chunk_manager()->locked_print_sum_free_chunks(st);
1942 }
1943
1944 size_t SpaceManager::calc_chunk_size(size_t word_size) {
1945
1946 // Decide between a small chunk and a medium chunk. Up to
1947 // _small_chunk_limit small chunks can be allocated but
1948 // once a medium chunk has been allocated, no more small
1949 // chunks will be allocated.
1950 size_t chunk_word_size;
1951 if (chunks_in_use(MediumIndex) == NULL &&
1952 sum_count_in_chunks_in_use(SmallIndex) < _small_chunk_limit) {
1953 chunk_word_size = (size_t) small_chunk_size();
1954 if (word_size + Metachunk::overhead() > small_chunk_size()) {
1955 chunk_word_size = medium_chunk_size();
1956 }
1957 } else {
1958 chunk_word_size = medium_chunk_size();
1959 }
1960
1961 // Might still need a humongous chunk. Enforce an
2026 i < NumberOfInUseLists ;
2027 i = next_chunk_index(i) ) {
2028 st->print_cr(" chunks_in_use " PTR_FORMAT " chunk size " PTR_FORMAT,
2029 chunks_in_use(i),
2030 chunks_in_use(i) == NULL ? 0 : chunks_in_use(i)->word_size());
2031 }
2032 st->print_cr(" waste: Small " SIZE_FORMAT " Medium " SIZE_FORMAT
2033 " Humongous " SIZE_FORMAT,
2034 sum_waste_in_chunks_in_use(SmallIndex),
2035 sum_waste_in_chunks_in_use(MediumIndex),
2036 sum_waste_in_chunks_in_use(HumongousIndex));
2037 // block free lists
2038 if (block_freelists() != NULL) {
2039 st->print_cr("total in block free lists " SIZE_FORMAT,
2040 block_freelists()->total_size());
2041 }
2042 }
2043
2044 SpaceManager::SpaceManager(Metaspace::MetadataType mdtype,
2045 Mutex* lock,
2046 VirtualSpaceList* vs_list,
2047 ChunkManager* chunk_manager) :
2048 _vs_list(vs_list),
2049 _chunk_manager(chunk_manager),
2050 _mdtype(mdtype),
2051 _allocated_blocks_words(0),
2052 _allocated_chunks_words(0),
2053 _allocated_chunks_count(0),
2054 _lock(lock)
2055 {
2056 initialize();
2057 }
2058
2059 void SpaceManager::inc_size_metrics(size_t words) {
2060 assert_lock_strong(SpaceManager::expand_lock());
2061 // Total of allocated Metachunks and allocated Metachunks count
2062 // for each SpaceManager
2063 _allocated_chunks_words = _allocated_chunks_words + words;
2064 _allocated_chunks_count++;
2065 // Global total of capacity in allocated Metachunks
2066 MetaspaceAux::inc_capacity(mdtype(), words);
2067 // Global total of allocated Metablocks.
2068 // used_words_slow() includes the overhead in each
2069 // Metachunk so include it in the used when the
2115 cur->container()->dec_container_count();
2116 // Capture the next link before it is changed
2117 // by the call to return_chunk_at_head();
2118 Metachunk* next = cur->next();
2119 cur->set_is_free(true);
2120 list->return_chunk_at_head(cur);
2121 cur = next;
2122 }
2123 }
2124
2125 SpaceManager::~SpaceManager() {
2126 // This call this->_lock which can't be done while holding expand_lock()
2127 assert(sum_capacity_in_chunks_in_use() == allocated_chunks_words(),
2128 err_msg("sum_capacity_in_chunks_in_use() " SIZE_FORMAT
2129 " allocated_chunks_words() " SIZE_FORMAT,
2130 sum_capacity_in_chunks_in_use(), allocated_chunks_words()));
2131
2132 MutexLockerEx fcl(SpaceManager::expand_lock(),
2133 Mutex::_no_safepoint_check_flag);
2134
2135 chunk_manager()->slow_locked_verify();
2136
2137 dec_total_from_size_metrics();
2138
2139 if (TraceMetadataChunkAllocation && Verbose) {
2140 gclog_or_tty->print_cr("~SpaceManager(): " PTR_FORMAT, this);
2141 locked_print_chunks_in_use_on(gclog_or_tty);
2142 }
2143
2144 // Do not mangle freed Metachunks. The chunk size inside Metachunks
2145 // is during the freeing of a VirtualSpaceNodes.
2146
2147 // Have to update before the chunks_in_use lists are emptied
2148 // below.
2149 chunk_manager()->inc_free_chunks_total(allocated_chunks_words(),
2150 sum_count_in_chunks_in_use());
2151
2152 // Add all the chunks in use by this space manager
2153 // to the global list of free chunks.
2154
2155 // Follow each list of chunks-in-use and add them to the
2156 // free lists. Each list is NULL terminated.
2157
2158 for (ChunkIndex i = ZeroIndex; i < HumongousIndex; i = next_chunk_index(i)) {
2159 if (TraceMetadataChunkAllocation && Verbose) {
2160 gclog_or_tty->print_cr("returned %d %s chunks to freelist",
2161 sum_count_in_chunks_in_use(i),
2162 chunk_size_name(i));
2163 }
2164 Metachunk* chunks = chunks_in_use(i);
2165 chunk_manager()->return_chunks(i, chunks);
2166 set_chunks_in_use(i, NULL);
2167 if (TraceMetadataChunkAllocation && Verbose) {
2168 gclog_or_tty->print_cr("updated freelist count %d %s",
2169 chunk_manager()->free_chunks(i)->count(),
2170 chunk_size_name(i));
2171 }
2172 assert(i != HumongousIndex, "Humongous chunks are handled explicitly later");
2173 }
2174
2175 // The medium chunk case may be optimized by passing the head and
2176 // tail of the medium chunk list to add_at_head(). The tail is often
2177 // the current chunk but there are probably exceptions.
2178
2179 // Humongous chunks
2180 if (TraceMetadataChunkAllocation && Verbose) {
2181 gclog_or_tty->print_cr("returned %d %s humongous chunks to dictionary",
2182 sum_count_in_chunks_in_use(HumongousIndex),
2183 chunk_size_name(HumongousIndex));
2184 gclog_or_tty->print("Humongous chunk dictionary: ");
2185 }
2186 // Humongous chunks are never the current chunk.
2187 Metachunk* humongous_chunks = chunks_in_use(HumongousIndex);
2188
2189 while (humongous_chunks != NULL) {
2190 #ifdef ASSERT
2191 humongous_chunks->set_is_free(true);
2192 #endif
2193 if (TraceMetadataChunkAllocation && Verbose) {
2194 gclog_or_tty->print(PTR_FORMAT " (" SIZE_FORMAT ") ",
2195 humongous_chunks,
2196 humongous_chunks->word_size());
2197 }
2198 assert(humongous_chunks->word_size() == (size_t)
2199 align_size_up(humongous_chunks->word_size(),
2200 HumongousChunkGranularity),
2201 err_msg("Humongous chunk size is wrong: word size " SIZE_FORMAT
2202 " granularity %d",
2203 humongous_chunks->word_size(), HumongousChunkGranularity));
2204 Metachunk* next_humongous_chunks = humongous_chunks->next();
2205 humongous_chunks->container()->dec_container_count();
2206 chunk_manager()->humongous_dictionary()->return_chunk(humongous_chunks);
2207 humongous_chunks = next_humongous_chunks;
2208 }
2209 if (TraceMetadataChunkAllocation && Verbose) {
2210 gclog_or_tty->print_cr("");
2211 gclog_or_tty->print_cr("updated dictionary count %d %s",
2212 chunk_manager()->humongous_dictionary()->total_count(),
2213 chunk_size_name(HumongousIndex));
2214 }
2215 chunk_manager()->slow_locked_verify();
2216 }
2217
2218 const char* SpaceManager::chunk_size_name(ChunkIndex index) const {
2219 switch (index) {
2220 case SpecializedIndex:
2221 return "Specialized";
2222 case SmallIndex:
2223 return "Small";
2224 case MediumIndex:
2225 return "Medium";
2226 case HumongousIndex:
2227 return "Humongous";
2228 default:
2229 return NULL;
2230 }
2231 }
2232
2233 ChunkIndex ChunkManager::list_index(size_t size) {
2234 switch (size) {
2235 case SpecializedChunk:
2284 set_current_chunk(new_chunk);
2285 }
2286 // Link at head. The _current_chunk only points to a humongous chunk for
2287 // the null class loader metaspace (class and data virtual space managers)
2288 // any humongous chunks so will not point to the tail
2289 // of the humongous chunks list.
2290 new_chunk->set_next(chunks_in_use(HumongousIndex));
2291 set_chunks_in_use(HumongousIndex, new_chunk);
2292
2293 assert(new_chunk->word_size() > medium_chunk_size(), "List inconsistency");
2294 }
2295
2296 // Add to the running sum of capacity
2297 inc_size_metrics(new_chunk->word_size());
2298
2299 assert(new_chunk->is_empty(), "Not ready for reuse");
2300 if (TraceMetadataChunkAllocation && Verbose) {
2301 gclog_or_tty->print("SpaceManager::add_chunk: %d) ",
2302 sum_count_in_chunks_in_use());
2303 new_chunk->print_on(gclog_or_tty);
2304 chunk_manager()->locked_print_free_chunks(gclog_or_tty);
2305 }
2306 }
2307
2308 void SpaceManager::retire_current_chunk() {
2309 if (current_chunk() != NULL) {
2310 size_t remaining_words = current_chunk()->free_word_size();
2311 if (remaining_words >= TreeChunk<Metablock, FreeList>::min_size()) {
2312 block_freelists()->return_block(current_chunk()->allocate(remaining_words), remaining_words);
2313 inc_used_metrics(remaining_words);
2314 }
2315 }
2316 }
2317
2318 Metachunk* SpaceManager::get_new_chunk(size_t word_size,
2319 size_t grow_chunks_by_words) {
2320 // Get a chunk from the chunk freelist
2321 Metachunk* next = chunk_manager()->chunk_freelist_allocate(grow_chunks_by_words);
2322
2323 if (next == NULL) {
2324 next = vs_list()->get_new_chunk(word_size,
2325 grow_chunks_by_words,
2326 medium_chunk_bunch());
2327 }
2328
2329 if (TraceMetadataHumongousAllocation && next != NULL &&
2330 SpaceManager::is_humongous(next->word_size())) {
2331 gclog_or_tty->print_cr(" new humongous chunk word size "
2332 PTR_FORMAT, next->word_size());
2333 }
2334
2335 return next;
2336 }
2337
2338 MetaWord* SpaceManager::allocate(size_t word_size) {
2339 MutexLockerEx cl(lock(), Mutex::_no_safepoint_check_flag);
2340
2341 size_t raw_word_size = get_raw_word_size(word_size);
2342 BlockFreelist* fl = block_freelists();
2343 MetaWord* p = NULL;
2344 // Allocation from the dictionary is expensive in the sense that
2345 // the dictionary has to be searched for a size. Don't allocate
2346 // from the dictionary until it starts to get fat. Is this
2347 // a reasonable policy? Maybe an skinny dictionary is fast enough
2587 " class_capacity " SIZE_FORMAT " non_class_capacity " SIZE_FORMAT,
2588 allocated_capacity_bytes(), class_capacity + non_class_capacity,
2589 class_capacity, non_class_capacity));
2590
2591 return class_capacity + non_class_capacity;
2592 }
2593
2594 size_t MetaspaceAux::reserved_bytes(Metaspace::MetadataType mdtype) {
2595 VirtualSpaceList* list = Metaspace::get_space_list(mdtype);
2596 return list == NULL ? 0 : list->reserved_bytes();
2597 }
2598
2599 size_t MetaspaceAux::committed_bytes(Metaspace::MetadataType mdtype) {
2600 VirtualSpaceList* list = Metaspace::get_space_list(mdtype);
2601 return list == NULL ? 0 : list->committed_bytes();
2602 }
2603
2604 size_t MetaspaceAux::min_chunk_size_words() { return Metaspace::first_chunk_word_size(); }
2605
2606 size_t MetaspaceAux::free_chunks_total_words(Metaspace::MetadataType mdtype) {
2607 ChunkManager* chunk_manager = Metaspace::get_chunk_manager(mdtype);
2608 if (chunk_manager == NULL) {
2609 return 0;
2610 }
2611 chunk_manager->slow_verify();
2612 return chunk_manager->free_chunks_total_words();
2613 }
2614
2615 size_t MetaspaceAux::free_chunks_total_bytes(Metaspace::MetadataType mdtype) {
2616 return free_chunks_total_words(mdtype) * BytesPerWord;
2617 }
2618
2619 size_t MetaspaceAux::free_chunks_total_words() {
2620 return free_chunks_total_words(Metaspace::ClassType) +
2621 free_chunks_total_words(Metaspace::NonClassType);
2622 }
2623
2624 size_t MetaspaceAux::free_chunks_total_bytes() {
2625 return free_chunks_total_words() * BytesPerWord;
2626 }
2627
2628 void MetaspaceAux::print_metaspace_change(size_t prev_metadata_used) {
2629 gclog_or_tty->print(", [Metaspace:");
2630 if (PrintGCDetails && Verbose) {
2631 gclog_or_tty->print(" " SIZE_FORMAT
2632 "->" SIZE_FORMAT
2743 out->print_cr(" data: " SIZE_FORMAT " specialized(s) " SIZE_FORMAT ", "
2744 SIZE_FORMAT " small(s) " SIZE_FORMAT ", "
2745 SIZE_FORMAT " medium(s) " SIZE_FORMAT ", "
2746 "large count " SIZE_FORMAT,
2747 specialized_count, specialized_waste, small_count,
2748 small_waste, medium_count, medium_waste, humongous_count);
2749 if (Metaspace::using_class_space()) {
2750 print_class_waste(out);
2751 }
2752 }
2753
2754 // Dump global metaspace things from the end of ClassLoaderDataGraph
2755 void MetaspaceAux::dump(outputStream* out) {
2756 out->print_cr("All Metaspace:");
2757 out->print("data space: "); print_on(out, Metaspace::NonClassType);
2758 out->print("class space: "); print_on(out, Metaspace::ClassType);
2759 print_waste(out);
2760 }
2761
2762 void MetaspaceAux::verify_free_chunks() {
2763 Metaspace::chunk_manager_metadata()->verify();
2764 if (Metaspace::using_class_space()) {
2765 Metaspace::chunk_manager_class()->verify();
2766 }
2767 }
2768
2769 void MetaspaceAux::verify_capacity() {
2770 #ifdef ASSERT
2771 size_t running_sum_capacity_bytes = allocated_capacity_bytes();
2772 // For purposes of the running sum of capacity, verify against capacity
2773 size_t capacity_in_use_bytes = capacity_bytes_slow();
2774 assert(running_sum_capacity_bytes == capacity_in_use_bytes,
2775 err_msg("allocated_capacity_words() * BytesPerWord " SIZE_FORMAT
2776 " capacity_bytes_slow()" SIZE_FORMAT,
2777 running_sum_capacity_bytes, capacity_in_use_bytes));
2778 for (Metaspace::MetadataType i = Metaspace::ClassType;
2779 i < Metaspace:: MetadataTypeCount;
2780 i = (Metaspace::MetadataType)(i + 1)) {
2781 size_t capacity_in_use_bytes = capacity_bytes_slow(i);
2782 assert(allocated_capacity_bytes(i) == capacity_in_use_bytes,
2783 err_msg("allocated_capacity_bytes(%u) " SIZE_FORMAT
2784 " capacity_bytes_slow(%u)" SIZE_FORMAT,
2785 i, allocated_capacity_bytes(i), i, capacity_in_use_bytes));
2816
2817 // Metaspace methods
2818
2819 size_t Metaspace::_first_chunk_word_size = 0;
2820 size_t Metaspace::_first_class_chunk_word_size = 0;
2821
2822 Metaspace::Metaspace(Mutex* lock, MetaspaceType type) {
2823 initialize(lock, type);
2824 }
2825
2826 Metaspace::~Metaspace() {
2827 delete _vsm;
2828 if (using_class_space()) {
2829 delete _class_vsm;
2830 }
2831 }
2832
2833 VirtualSpaceList* Metaspace::_space_list = NULL;
2834 VirtualSpaceList* Metaspace::_class_space_list = NULL;
2835
2836 ChunkManager* Metaspace::_chunk_manager_metadata = NULL;
2837 ChunkManager* Metaspace::_chunk_manager_class = NULL;
2838
2839 #define VIRTUALSPACEMULTIPLIER 2
2840
2841 #ifdef _LP64
2842 void Metaspace::set_narrow_klass_base_and_shift(address metaspace_base, address cds_base) {
2843 // Figure out the narrow_klass_base and the narrow_klass_shift. The
2844 // narrow_klass_base is the lower of the metaspace base and the cds base
2845 // (if cds is enabled). The narrow_klass_shift depends on the distance
2846 // between the lower base and higher address.
2847 address lower_base;
2848 address higher_address;
2849 if (UseSharedSpaces) {
2850 higher_address = MAX2((address)(cds_base + FileMapInfo::shared_spaces_size()),
2851 (address)(metaspace_base + class_metaspace_size()));
2852 lower_base = MIN2(metaspace_base, cds_base);
2853 } else {
2854 higher_address = metaspace_base + class_metaspace_size();
2855 lower_base = metaspace_base;
2856 }
2857 Universe::set_narrow_klass_base(lower_base);
2858 if ((uint64_t)(higher_address - lower_base) < (uint64_t)max_juint) {
2926 UseSharedSpaces ? (address)cds_base : 0);
2927
2928 initialize_class_space(metaspace_rs);
2929
2930 if (PrintCompressedOopsMode || (PrintMiscellaneous && Verbose)) {
2931 gclog_or_tty->print_cr("Narrow klass base: " PTR_FORMAT ", Narrow klass shift: " SIZE_FORMAT,
2932 Universe::narrow_klass_base(), Universe::narrow_klass_shift());
2933 gclog_or_tty->print_cr("Metaspace Size: " SIZE_FORMAT " Address: " PTR_FORMAT " Req Addr: " PTR_FORMAT,
2934 class_metaspace_size(), metaspace_rs.base(), requested_addr);
2935 }
2936 }
2937
2938 // For UseCompressedClassPointers the class space is reserved above the top of
2939 // the Java heap. The argument passed in is at the base of the compressed space.
2940 void Metaspace::initialize_class_space(ReservedSpace rs) {
2941 // The reserved space size may be bigger because of alignment, esp with UseLargePages
2942 assert(rs.size() >= CompressedClassSpaceSize,
2943 err_msg(SIZE_FORMAT " != " UINTX_FORMAT, rs.size(), CompressedClassSpaceSize));
2944 assert(using_class_space(), "Must be using class space");
2945 _class_space_list = new VirtualSpaceList(rs);
2946 _chunk_manager_class = new ChunkManager(SpecializedChunk, ClassSmallChunk, ClassMediumChunk);
2947 }
2948
2949 #endif
2950
2951 void Metaspace::global_initialize() {
2952 // Initialize the alignment for shared spaces.
2953 int max_alignment = os::vm_page_size();
2954 size_t cds_total = 0;
2955
2956 set_class_metaspace_size(align_size_up(CompressedClassSpaceSize,
2957 os::vm_allocation_granularity()));
2958
2959 MetaspaceShared::set_max_alignment(max_alignment);
2960
2961 if (DumpSharedSpaces) {
2962 SharedReadOnlySize = align_size_up(SharedReadOnlySize, max_alignment);
2963 SharedReadWriteSize = align_size_up(SharedReadWriteSize, max_alignment);
2964 SharedMiscDataSize = align_size_up(SharedMiscDataSize, max_alignment);
2965 SharedMiscCodeSize = align_size_up(SharedMiscCodeSize, max_alignment);
2966
2967 // Initialize with the sum of the shared space sizes. The read-only
2968 // and read write metaspace chunks will be allocated out of this and the
2969 // remainder is the misc code and data chunks.
2970 cds_total = FileMapInfo::shared_spaces_size();
2971 _space_list = new VirtualSpaceList(cds_total/wordSize);
2972 _chunk_manager_metadata = new ChunkManager(SpecializedChunk, SmallChunk, MediumChunk);
2973
2974 #ifdef _LP64
2975 // Set the compressed klass pointer base so that decoding of these pointers works
2976 // properly when creating the shared archive.
2977 assert(UseCompressedOops && UseCompressedClassPointers,
2978 "UseCompressedOops and UseCompressedClassPointers must be set");
2979 Universe::set_narrow_klass_base((address)_space_list->current_virtual_space()->bottom());
2980 if (TraceMetavirtualspaceAllocation && Verbose) {
2981 gclog_or_tty->print_cr("Setting_narrow_klass_base to Address: " PTR_FORMAT,
2982 _space_list->current_virtual_space()->bottom());
2983 }
2984
2985 // Set the shift to zero.
2986 assert(class_metaspace_size() < (uint64_t)(max_juint) - cds_total,
2987 "CDS region is too large");
2988 Universe::set_narrow_klass_shift(0);
2989 #endif
2990
2991 } else {
2992 // If using shared space, open the file that contains the shared space
3020 } else {
3021 allocate_metaspace_compressed_klass_ptrs((char *)CompressedKlassPointersBase, 0);
3022 }
3023 }
3024 #endif
3025
3026 // Initialize these before initializing the VirtualSpaceList
3027 _first_chunk_word_size = InitialBootClassLoaderMetaspaceSize / BytesPerWord;
3028 _first_chunk_word_size = align_word_size_up(_first_chunk_word_size);
3029 // Make the first class chunk bigger than a medium chunk so it's not put
3030 // on the medium chunk list. The next chunk will be small and progress
3031 // from there. This size calculated by -version.
3032 _first_class_chunk_word_size = MIN2((size_t)MediumChunk*6,
3033 (CompressedClassSpaceSize/BytesPerWord)*2);
3034 _first_class_chunk_word_size = align_word_size_up(_first_class_chunk_word_size);
3035 // Arbitrarily set the initial virtual space to a multiple
3036 // of the boot class loader size.
3037 size_t word_size = VIRTUALSPACEMULTIPLIER * first_chunk_word_size();
3038 // Initialize the list of virtual spaces.
3039 _space_list = new VirtualSpaceList(word_size);
3040 _chunk_manager_metadata = new ChunkManager(SpecializedChunk, SmallChunk, MediumChunk);
3041 }
3042 }
3043
3044 Metachunk* Metaspace::get_initialization_chunk(MetadataType mdtype,
3045 size_t chunk_word_size,
3046 size_t chunk_bunch) {
3047 // Get a chunk from the chunk freelist
3048 Metachunk* chunk = get_chunk_manager(mdtype)->chunk_freelist_allocate(chunk_word_size);
3049 if (chunk != NULL) {
3050 return chunk;
3051 }
3052
3053 return get_space_list(mdtype)->get_initialization_chunk(chunk_word_size, chunk_bunch);
3054 }
3055
3056 void Metaspace::initialize(Mutex* lock, MetaspaceType type) {
3057
3058 assert(space_list() != NULL,
3059 "Metadata VirtualSpaceList has not been initialized");
3060 assert(chunk_manager_metadata() != NULL,
3061 "Metadata ChunkManager has not been initialized");
3062
3063 _vsm = new SpaceManager(NonClassType, lock, space_list(), chunk_manager_metadata());
3064 if (_vsm == NULL) {
3065 return;
3066 }
3067 size_t word_size;
3068 size_t class_word_size;
3069 vsm()->get_initial_chunk_sizes(type, &word_size, &class_word_size);
3070
3071 if (using_class_space()) {
3072 assert(class_space_list() != NULL,
3073 "Class VirtualSpaceList has not been initialized");
3074 assert(chunk_manager_class() != NULL,
3075 "Class ChunkManager has not been initialized");
3076
3077 // Allocate SpaceManager for classes.
3078 _class_vsm = new SpaceManager(ClassType, lock, class_space_list(), chunk_manager_class());
3079 if (_class_vsm == NULL) {
3080 return;
3081 }
3082 }
3083
3084 MutexLockerEx cl(SpaceManager::expand_lock(), Mutex::_no_safepoint_check_flag);
3085
3086 // Allocate chunk for metadata objects
3087 Metachunk* new_chunk = get_initialization_chunk(NonClassType,
3088 word_size,
3089 vsm()->medium_chunk_bunch());
3090 assert(!DumpSharedSpaces || new_chunk != NULL, "should have enough space for both chunks");
3091 if (new_chunk != NULL) {
3092 // Add to this manager's list of chunks in use and current_chunk().
3093 vsm()->add_chunk(new_chunk, true);
3094 }
3095
3096 // Allocate chunk for class metadata objects
3097 if (using_class_space()) {
3098 Metachunk* class_chunk = get_initialization_chunk(ClassType,
3099 class_word_size,
3100 class_vsm()->medium_chunk_bunch());
3101 if (class_chunk != NULL) {
3102 class_vsm()->add_chunk(class_chunk, true);
3103 }
3104 }
3105
3106 _alloc_record_head = NULL;
3107 _alloc_record_tail = NULL;
3108 }
3109
3110 size_t Metaspace::align_word_size_up(size_t word_size) {
3111 size_t byte_size = word_size * wordSize;
3112 return ReservedSpace::allocation_align_size_up(byte_size) / wordSize;
3113 }
3114
3115 MetaWord* Metaspace::allocate(size_t word_size, MetadataType mdtype) {
3116 // DumpSharedSpaces doesn't use class metadata area (yet)
3117 // Also, don't use class_vsm() unless UseCompressedClassPointers is true.
3118 if (mdtype == ClassType && using_class_space()) {
3119 return class_vsm()->allocate(word_size);
3297 void Metaspace::iterate(Metaspace::AllocRecordClosure *closure) {
3298 assert(DumpSharedSpaces, "unimplemented for !DumpSharedSpaces");
3299
3300 address last_addr = (address)bottom();
3301
3302 for (AllocRecord *rec = _alloc_record_head; rec; rec = rec->_next) {
3303 address ptr = rec->_ptr;
3304 if (last_addr < ptr) {
3305 closure->doit(last_addr, MetaspaceObj::UnknownType, ptr - last_addr);
3306 }
3307 closure->doit(ptr, rec->_type, rec->_byte_size);
3308 last_addr = ptr + rec->_byte_size;
3309 }
3310
3311 address top = ((address)bottom()) + used_bytes_slow(Metaspace::NonClassType);
3312 if (last_addr < top) {
3313 closure->doit(last_addr, MetaspaceObj::UnknownType, top - last_addr);
3314 }
3315 }
3316
3317 void Metaspace::purge(MetadataType mdtype) {
3318 get_space_list(mdtype)->purge(get_chunk_manager(mdtype));
3319 }
3320
3321 void Metaspace::purge() {
3322 MutexLockerEx cl(SpaceManager::expand_lock(),
3323 Mutex::_no_safepoint_check_flag);
3324 purge(NonClassType);
3325 if (using_class_space()) {
3326 purge(ClassType);
3327 }
3328 }
3329
3330 void Metaspace::print_on(outputStream* out) const {
3331 // Print both class virtual space counts and metaspace.
3332 if (Verbose) {
3333 vsm()->print_on(out);
3334 if (using_class_space()) {
3335 class_vsm()->print_on(out);
3336 }
3337 }
3338 }
3339
3340 bool Metaspace::contains(const void * ptr) {
3341 if (MetaspaceShared::is_in_shared_space(ptr)) {
3342 return true;
3343 }
3344 // This is checked while unlocked. As long as the virtualspaces are added
3345 // at the end, the pointer will be in one of them. The virtual spaces
3346 // aren't deleted presently. When they are, some sort of locking might
|