/
metaspace.cpp
4145 lines (3470 loc) · 146 KB
/
metaspace.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2011, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*
*/
#include "precompiled.hpp"
#include "gc_interface/collectedHeap.hpp"
#include "memory/allocation.hpp"
#include "memory/binaryTreeDictionary.hpp"
#include "memory/freeList.hpp"
#include "memory/collectorPolicy.hpp"
#include "memory/filemap.hpp"
#include "memory/freeList.hpp"
#include "memory/gcLocker.hpp"
#include "memory/metachunk.hpp"
#include "memory/metaspace.hpp"
#include "memory/metaspaceGCThresholdUpdater.hpp"
#include "memory/metaspaceShared.hpp"
#include "memory/metaspaceTracer.hpp"
#include "memory/resourceArea.hpp"
#include "memory/universe.hpp"
#include "runtime/atomic.inline.hpp"
#include "runtime/globals.hpp"
#include "runtime/init.hpp"
#include "runtime/java.hpp"
#include "runtime/mutex.hpp"
#include "runtime/orderAccess.inline.hpp"
#include "services/memTracker.hpp"
#include "services/memoryService.hpp"
#include "utilities/copy.hpp"
#include "utilities/debug.hpp"
PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
typedef BinaryTreeDictionary<Metablock, FreeList<Metablock> > BlockTreeDictionary;
typedef BinaryTreeDictionary<Metachunk, FreeList<Metachunk> > ChunkTreeDictionary;
// Set this constant to enable slow integrity checking of the free chunk lists
const bool metaspace_slow_verify = false;
size_t const allocation_from_dictionary_limit = 4 * K;
MetaWord* last_allocated = 0;
size_t Metaspace::_compressed_class_space_size;
const MetaspaceTracer* Metaspace::_tracer = NULL;
// Used in declarations in SpaceManager and ChunkManager
enum ChunkIndex {
ZeroIndex = 0,
SpecializedIndex = ZeroIndex,
SmallIndex = SpecializedIndex + 1,
MediumIndex = SmallIndex + 1,
HumongousIndex = MediumIndex + 1,
NumberOfFreeLists = 3,
NumberOfInUseLists = 4
};
enum ChunkSizes { // in words.
ClassSpecializedChunk = 128,
SpecializedChunk = 128,
ClassSmallChunk = 256,
SmallChunk = 512,
ClassMediumChunk = 4 * K,
MediumChunk = 8 * K
};
static ChunkIndex next_chunk_index(ChunkIndex i) {
assert(i < NumberOfInUseLists, "Out of bound");
return (ChunkIndex) (i+1);
}
volatile intptr_t MetaspaceGC::_capacity_until_GC = 0;
uint MetaspaceGC::_shrink_factor = 0;
bool MetaspaceGC::_should_concurrent_collect = false;
typedef class FreeList<Metachunk> ChunkList;
// Manages the global free lists of chunks.
class ChunkManager : public CHeapObj<mtInternal> {
friend class TestVirtualSpaceNodeTest;
// Free list of chunks of different sizes.
// SpecializedChunk
// SmallChunk
// MediumChunk
// HumongousChunk
ChunkList _free_chunks[NumberOfFreeLists];
// HumongousChunk
ChunkTreeDictionary _humongous_dictionary;
// ChunkManager in all lists of this type
size_t _free_chunks_total;
size_t _free_chunks_count;
void dec_free_chunks_total(size_t v) {
assert(_free_chunks_count > 0 &&
_free_chunks_total > 0,
"About to go negative");
Atomic::add_ptr(-1, &_free_chunks_count);
jlong minus_v = (jlong) - (jlong) v;
Atomic::add_ptr(minus_v, &_free_chunks_total);
}
// Debug support
size_t sum_free_chunks();
size_t sum_free_chunks_count();
void locked_verify_free_chunks_total();
void slow_locked_verify_free_chunks_total() {
if (metaspace_slow_verify) {
locked_verify_free_chunks_total();
}
}
void locked_verify_free_chunks_count();
void slow_locked_verify_free_chunks_count() {
if (metaspace_slow_verify) {
locked_verify_free_chunks_count();
}
}
void verify_free_chunks_count();
public:
ChunkManager(size_t specialized_size, size_t small_size, size_t medium_size)
: _free_chunks_total(0), _free_chunks_count(0) {
_free_chunks[SpecializedIndex].set_size(specialized_size);
_free_chunks[SmallIndex].set_size(small_size);
_free_chunks[MediumIndex].set_size(medium_size);
}
// add or delete (return) a chunk to the global freelist.
Metachunk* chunk_freelist_allocate(size_t word_size);
// Map a size to a list index assuming that there are lists
// for special, small, medium, and humongous chunks.
ChunkIndex list_index(size_t size);
// Remove the chunk from its freelist. It is
// expected to be on one of the _free_chunks[] lists.
void remove_chunk(Metachunk* chunk);
// Add the simple linked list of chunks to the freelist of chunks
// of type index.
void return_chunks(ChunkIndex index, Metachunk* chunks);
// Total of the space in the free chunks list
size_t free_chunks_total_words();
size_t free_chunks_total_bytes();
// Number of chunks in the free chunks list
size_t free_chunks_count();
void inc_free_chunks_total(size_t v, size_t count = 1) {
Atomic::add_ptr(count, &_free_chunks_count);
Atomic::add_ptr(v, &_free_chunks_total);
}
ChunkTreeDictionary* humongous_dictionary() {
return &_humongous_dictionary;
}
ChunkList* free_chunks(ChunkIndex index);
// Returns the list for the given chunk word size.
ChunkList* find_free_chunks_list(size_t word_size);
// Remove from a list by size. Selects list based on size of chunk.
Metachunk* free_chunks_get(size_t chunk_word_size);
#define index_bounds_check(index) \
assert(index == SpecializedIndex || \
index == SmallIndex || \
index == MediumIndex || \
index == HumongousIndex, err_msg("Bad index: %d", (int) index))
size_t num_free_chunks(ChunkIndex index) const {
index_bounds_check(index);
if (index == HumongousIndex) {
return _humongous_dictionary.total_free_blocks();
}
ssize_t count = _free_chunks[index].count();
return count == -1 ? 0 : (size_t) count;
}
size_t size_free_chunks_in_bytes(ChunkIndex index) const {
index_bounds_check(index);
size_t word_size = 0;
if (index == HumongousIndex) {
word_size = _humongous_dictionary.total_size();
} else {
const size_t size_per_chunk_in_words = _free_chunks[index].size();
word_size = size_per_chunk_in_words * num_free_chunks(index);
}
return word_size * BytesPerWord;
}
MetaspaceChunkFreeListSummary chunk_free_list_summary() const {
return MetaspaceChunkFreeListSummary(num_free_chunks(SpecializedIndex),
num_free_chunks(SmallIndex),
num_free_chunks(MediumIndex),
num_free_chunks(HumongousIndex),
size_free_chunks_in_bytes(SpecializedIndex),
size_free_chunks_in_bytes(SmallIndex),
size_free_chunks_in_bytes(MediumIndex),
size_free_chunks_in_bytes(HumongousIndex));
}
// Debug support
void verify();
void slow_verify() {
if (metaspace_slow_verify) {
verify();
}
}
void locked_verify();
void slow_locked_verify() {
if (metaspace_slow_verify) {
locked_verify();
}
}
void verify_free_chunks_total();
void locked_print_free_chunks(outputStream* st);
void locked_print_sum_free_chunks(outputStream* st);
void print_on(outputStream* st) const;
};
// Used to manage the free list of Metablocks (a block corresponds
// to the allocation of a quantum of metadata).
class BlockFreelist VALUE_OBJ_CLASS_SPEC {
BlockTreeDictionary* _dictionary;
// Only allocate and split from freelist if the size of the allocation
// is at least 1/4th the size of the available block.
const static int WasteMultiplier = 4;
// Accessors
BlockTreeDictionary* dictionary() const { return _dictionary; }
public:
BlockFreelist();
~BlockFreelist();
// Get and return a block to the free list
MetaWord* get_block(size_t word_size);
void return_block(MetaWord* p, size_t word_size);
size_t total_size() {
if (dictionary() == NULL) {
return 0;
} else {
return dictionary()->total_size();
}
}
void print_on(outputStream* st) const;
};
// A VirtualSpaceList node.
class VirtualSpaceNode : public CHeapObj<mtClass> {
friend class VirtualSpaceList;
// Link to next VirtualSpaceNode
VirtualSpaceNode* _next;
// total in the VirtualSpace
MemRegion _reserved;
ReservedSpace _rs;
VirtualSpace _virtual_space;
MetaWord* _top;
// count of chunks contained in this VirtualSpace
uintx _container_count;
// Convenience functions to access the _virtual_space
char* low() const { return virtual_space()->low(); }
char* high() const { return virtual_space()->high(); }
// The first Metachunk will be allocated at the bottom of the
// VirtualSpace
Metachunk* first_chunk() { return (Metachunk*) bottom(); }
// Committed but unused space in the virtual space
size_t free_words_in_vs() const;
public:
VirtualSpaceNode(size_t byte_size);
VirtualSpaceNode(ReservedSpace rs) : _top(NULL), _next(NULL), _rs(rs), _container_count(0) {}
~VirtualSpaceNode();
// Convenience functions for logical bottom and end
MetaWord* bottom() const { return (MetaWord*) _virtual_space.low(); }
MetaWord* end() const { return (MetaWord*) _virtual_space.high(); }
bool contains(const void* ptr) { return ptr >= low() && ptr < high(); }
size_t reserved_words() const { return _virtual_space.reserved_size() / BytesPerWord; }
size_t committed_words() const { return _virtual_space.actual_committed_size() / BytesPerWord; }
bool is_pre_committed() const { return _virtual_space.special(); }
// address of next available space in _virtual_space;
// Accessors
VirtualSpaceNode* next() { return _next; }
void set_next(VirtualSpaceNode* v) { _next = v; }
void set_reserved(MemRegion const v) { _reserved = v; }
void set_top(MetaWord* v) { _top = v; }
// Accessors
MemRegion* reserved() { return &_reserved; }
VirtualSpace* virtual_space() const { return (VirtualSpace*) &_virtual_space; }
// Returns true if "word_size" is available in the VirtualSpace
bool is_available(size_t word_size) { return word_size <= pointer_delta(end(), _top, sizeof(MetaWord)); }
MetaWord* top() const { return _top; }
void inc_top(size_t word_size) { _top += word_size; }
uintx container_count() { return _container_count; }
void inc_container_count();
void dec_container_count();
#ifdef ASSERT
uint container_count_slow();
void verify_container_count();
#endif
// used and capacity in this single entry in the list
size_t used_words_in_vs() const;
size_t capacity_words_in_vs() const;
bool initialize();
// get space from the virtual space
Metachunk* take_from_committed(size_t chunk_word_size);
// Allocate a chunk from the virtual space and return it.
Metachunk* get_chunk_vs(size_t chunk_word_size);
// Expands/shrinks the committed space in a virtual space. Delegates
// to Virtualspace
bool expand_by(size_t min_words, size_t preferred_words);
// In preparation for deleting this node, remove all the chunks
// in the node from any freelist.
void purge(ChunkManager* chunk_manager);
// If an allocation doesn't fit in the current node a new node is created.
// Allocate chunks out of the remaining committed space in this node
// to avoid wasting that memory.
// This always adds up because all the chunk sizes are multiples of
// the smallest chunk size.
void retire(ChunkManager* chunk_manager);
#ifdef ASSERT
// Debug support
void mangle();
#endif
void print_on(outputStream* st) const;
};
#define assert_is_ptr_aligned(ptr, alignment) \
assert(is_ptr_aligned(ptr, alignment), \
err_msg(PTR_FORMAT " is not aligned to " \
SIZE_FORMAT, ptr, alignment))
#define assert_is_size_aligned(size, alignment) \
assert(is_size_aligned(size, alignment), \
err_msg(SIZE_FORMAT " is not aligned to " \
SIZE_FORMAT, size, alignment))
// Decide if large pages should be committed when the memory is reserved.
static bool should_commit_large_pages_when_reserving(size_t bytes) {
if (UseLargePages && UseLargePagesInMetaspace && !os::can_commit_large_page_memory()) {
size_t words = bytes / BytesPerWord;
bool is_class = false; // We never reserve large pages for the class space.
if (MetaspaceGC::can_expand(words, is_class) &&
MetaspaceGC::allowed_expansion() >= words) {
return true;
}
}
return false;
}
// byte_size is the size of the associated virtualspace.
VirtualSpaceNode::VirtualSpaceNode(size_t bytes) : _top(NULL), _next(NULL), _rs(), _container_count(0) {
assert_is_size_aligned(bytes, Metaspace::reserve_alignment());
#if INCLUDE_CDS
// This allocates memory with mmap. For DumpSharedspaces, try to reserve
// configurable address, generally at the top of the Java heap so other
// memory addresses don't conflict.
if (DumpSharedSpaces) {
bool large_pages = false; // No large pages when dumping the CDS archive.
char* shared_base = (char*)align_ptr_up((char*)SharedBaseAddress, Metaspace::reserve_alignment());
_rs = ReservedSpace(bytes, Metaspace::reserve_alignment(), large_pages, shared_base, 0);
if (_rs.is_reserved()) {
assert(shared_base == 0 || _rs.base() == shared_base, "should match");
} else {
// Get a mmap region anywhere if the SharedBaseAddress fails.
_rs = ReservedSpace(bytes, Metaspace::reserve_alignment(), large_pages);
}
MetaspaceShared::set_shared_rs(&_rs);
} else
#endif
{
bool large_pages = should_commit_large_pages_when_reserving(bytes);
_rs = ReservedSpace(bytes, Metaspace::reserve_alignment(), large_pages);
}
if (_rs.is_reserved()) {
assert(_rs.base() != NULL, "Catch if we get a NULL address");
assert(_rs.size() != 0, "Catch if we get a 0 size");
assert_is_ptr_aligned(_rs.base(), Metaspace::reserve_alignment());
assert_is_size_aligned(_rs.size(), Metaspace::reserve_alignment());
MemTracker::record_virtual_memory_type((address)_rs.base(), mtClass);
}
}
void VirtualSpaceNode::purge(ChunkManager* chunk_manager) {
Metachunk* chunk = first_chunk();
Metachunk* invalid_chunk = (Metachunk*) top();
while (chunk < invalid_chunk ) {
assert(chunk->is_tagged_free(), "Should be tagged free");
MetaWord* next = ((MetaWord*)chunk) + chunk->word_size();
chunk_manager->remove_chunk(chunk);
assert(chunk->next() == NULL &&
chunk->prev() == NULL,
"Was not removed from its list");
chunk = (Metachunk*) next;
}
}
#ifdef ASSERT
uint VirtualSpaceNode::container_count_slow() {
uint count = 0;
Metachunk* chunk = first_chunk();
Metachunk* invalid_chunk = (Metachunk*) top();
while (chunk < invalid_chunk ) {
MetaWord* next = ((MetaWord*)chunk) + chunk->word_size();
// Don't count the chunks on the free lists. Those are
// still part of the VirtualSpaceNode but not currently
// counted.
if (!chunk->is_tagged_free()) {
count++;
}
chunk = (Metachunk*) next;
}
return count;
}
#endif
// List of VirtualSpaces for metadata allocation.
class VirtualSpaceList : public CHeapObj<mtClass> {
friend class VirtualSpaceNode;
enum VirtualSpaceSizes {
VirtualSpaceSize = 256 * K
};
// Head of the list
VirtualSpaceNode* _virtual_space_list;
// virtual space currently being used for allocations
VirtualSpaceNode* _current_virtual_space;
// Is this VirtualSpaceList used for the compressed class space
bool _is_class;
// Sum of reserved and committed memory in the virtual spaces
size_t _reserved_words;
size_t _committed_words;
// Number of virtual spaces
size_t _virtual_space_count;
~VirtualSpaceList();
VirtualSpaceNode* virtual_space_list() const { return _virtual_space_list; }
void set_virtual_space_list(VirtualSpaceNode* v) {
_virtual_space_list = v;
}
void set_current_virtual_space(VirtualSpaceNode* v) {
_current_virtual_space = v;
}
void link_vs(VirtualSpaceNode* new_entry);
// Get another virtual space and add it to the list. This
// is typically prompted by a failed attempt to allocate a chunk
// and is typically followed by the allocation of a chunk.
bool create_new_virtual_space(size_t vs_word_size);
// Chunk up the unused committed space in the current
// virtual space and add the chunks to the free list.
void retire_current_virtual_space();
public:
VirtualSpaceList(size_t word_size);
VirtualSpaceList(ReservedSpace rs);
size_t free_bytes();
Metachunk* get_new_chunk(size_t chunk_word_size,
size_t suggested_commit_granularity);
bool expand_node_by(VirtualSpaceNode* node,
size_t min_words,
size_t preferred_words);
bool expand_by(size_t min_words,
size_t preferred_words);
VirtualSpaceNode* current_virtual_space() {
return _current_virtual_space;
}
bool is_class() const { return _is_class; }
bool initialization_succeeded() { return _virtual_space_list != NULL; }
size_t reserved_words() { return _reserved_words; }
size_t reserved_bytes() { return reserved_words() * BytesPerWord; }
size_t committed_words() { return _committed_words; }
size_t committed_bytes() { return committed_words() * BytesPerWord; }
void inc_reserved_words(size_t v);
void dec_reserved_words(size_t v);
void inc_committed_words(size_t v);
void dec_committed_words(size_t v);
void inc_virtual_space_count();
void dec_virtual_space_count();
bool contains(const void* ptr);
// Unlink empty VirtualSpaceNodes and free it.
void purge(ChunkManager* chunk_manager);
void print_on(outputStream* st) const;
class VirtualSpaceListIterator : public StackObj {
VirtualSpaceNode* _virtual_spaces;
public:
VirtualSpaceListIterator(VirtualSpaceNode* virtual_spaces) :
_virtual_spaces(virtual_spaces) {}
bool repeat() {
return _virtual_spaces != NULL;
}
VirtualSpaceNode* get_next() {
VirtualSpaceNode* result = _virtual_spaces;
if (_virtual_spaces != NULL) {
_virtual_spaces = _virtual_spaces->next();
}
return result;
}
};
};
class Metadebug : AllStatic {
// Debugging support for Metaspaces
static int _allocation_fail_alot_count;
public:
static void init_allocation_fail_alot_count();
#ifdef ASSERT
static bool test_metadata_failure();
#endif
};
int Metadebug::_allocation_fail_alot_count = 0;
// SpaceManager - used by Metaspace to handle allocations
class SpaceManager : public CHeapObj<mtClass> {
friend class Metaspace;
friend class Metadebug;
private:
// protects allocations
Mutex* const _lock;
// Type of metadata allocated.
Metaspace::MetadataType _mdtype;
// List of chunks in use by this SpaceManager. Allocations
// are done from the current chunk. The list is used for deallocating
// chunks when the SpaceManager is freed.
Metachunk* _chunks_in_use[NumberOfInUseLists];
Metachunk* _current_chunk;
// Number of small chunks to allocate to a manager
// If class space manager, small chunks are unlimited
static uint const _small_chunk_limit;
// Sum of all space in allocated chunks
size_t _allocated_blocks_words;
// Sum of all allocated chunks
size_t _allocated_chunks_words;
size_t _allocated_chunks_count;
// Free lists of blocks are per SpaceManager since they
// are assumed to be in chunks in use by the SpaceManager
// and all chunks in use by a SpaceManager are freed when
// the class loader using the SpaceManager is collected.
BlockFreelist _block_freelists;
// protects virtualspace and chunk expansions
static const char* _expand_lock_name;
static const int _expand_lock_rank;
static Mutex* const _expand_lock;
private:
// Accessors
Metachunk* chunks_in_use(ChunkIndex index) const { return _chunks_in_use[index]; }
void set_chunks_in_use(ChunkIndex index, Metachunk* v) {
_chunks_in_use[index] = v;
}
BlockFreelist* block_freelists() const {
return (BlockFreelist*) &_block_freelists;
}
Metaspace::MetadataType mdtype() { return _mdtype; }
VirtualSpaceList* vs_list() const { return Metaspace::get_space_list(_mdtype); }
ChunkManager* chunk_manager() const { return Metaspace::get_chunk_manager(_mdtype); }
Metachunk* current_chunk() const { return _current_chunk; }
void set_current_chunk(Metachunk* v) {
_current_chunk = v;
}
Metachunk* find_current_chunk(size_t word_size);
// Add chunk to the list of chunks in use
void add_chunk(Metachunk* v, bool make_current);
void retire_current_chunk();
Mutex* lock() const { return _lock; }
const char* chunk_size_name(ChunkIndex index) const;
protected:
void initialize();
public:
SpaceManager(Metaspace::MetadataType mdtype,
Mutex* lock);
~SpaceManager();
enum ChunkMultiples {
MediumChunkMultiple = 4
};
static size_t specialized_chunk_size(bool is_class) { return is_class ? ClassSpecializedChunk : SpecializedChunk; }
static size_t small_chunk_size(bool is_class) { return is_class ? ClassSmallChunk : SmallChunk; }
static size_t medium_chunk_size(bool is_class) { return is_class ? ClassMediumChunk : MediumChunk; }
static size_t smallest_chunk_size(bool is_class) { return specialized_chunk_size(is_class); }
// Accessors
bool is_class() const { return _mdtype == Metaspace::ClassType; }
size_t specialized_chunk_size() const { return specialized_chunk_size(is_class()); }
size_t small_chunk_size() const { return small_chunk_size(is_class()); }
size_t medium_chunk_size() const { return medium_chunk_size(is_class()); }
size_t smallest_chunk_size() const { return smallest_chunk_size(is_class()); }
size_t medium_chunk_bunch() const { return medium_chunk_size() * MediumChunkMultiple; }
size_t allocated_blocks_words() const { return _allocated_blocks_words; }
size_t allocated_blocks_bytes() const { return _allocated_blocks_words * BytesPerWord; }
size_t allocated_chunks_words() const { return _allocated_chunks_words; }
size_t allocated_chunks_bytes() const { return _allocated_chunks_words * BytesPerWord; }
size_t allocated_chunks_count() const { return _allocated_chunks_count; }
bool is_humongous(size_t word_size) { return word_size > medium_chunk_size(); }
static Mutex* expand_lock() { return _expand_lock; }
// Increment the per Metaspace and global running sums for Metachunks
// by the given size. This is used when a Metachunk to added to
// the in-use list.
void inc_size_metrics(size_t words);
// Increment the per Metaspace and global running sums Metablocks by the given
// size. This is used when a Metablock is allocated.
void inc_used_metrics(size_t words);
// Delete the portion of the running sums for this SpaceManager. That is,
// the globals running sums for the Metachunks and Metablocks are
// decremented for all the Metachunks in-use by this SpaceManager.
void dec_total_from_size_metrics();
// Adjust the initial chunk size to match one of the fixed chunk list sizes,
// or return the unadjusted size if the requested size is humongous.
static size_t adjust_initial_chunk_size(size_t requested, bool is_class_space);
size_t adjust_initial_chunk_size(size_t requested) const;
// Get the initial chunks size for this metaspace type.
size_t get_initial_chunk_size(Metaspace::MetaspaceType type) const;
size_t sum_capacity_in_chunks_in_use() const;
size_t sum_used_in_chunks_in_use() const;
size_t sum_free_in_chunks_in_use() const;
size_t sum_waste_in_chunks_in_use() const;
size_t sum_waste_in_chunks_in_use(ChunkIndex index ) const;
size_t sum_count_in_chunks_in_use();
size_t sum_count_in_chunks_in_use(ChunkIndex i);
Metachunk* get_new_chunk(size_t chunk_word_size);
// Block allocation and deallocation.
// Allocates a block from the current chunk
MetaWord* allocate(size_t word_size);
// Helper for allocations
MetaWord* allocate_work(size_t word_size);
// Returns a block to the per manager freelist
void deallocate(MetaWord* p, size_t word_size);
// Based on the allocation size and a minimum chunk size,
// returned chunk size (for expanding space for chunk allocation).
size_t calc_chunk_size(size_t allocation_word_size);
// Called when an allocation from the current chunk fails.
// Gets a new chunk (may require getting a new virtual space),
// and allocates from that chunk.
MetaWord* grow_and_allocate(size_t word_size);
// Notify memory usage to MemoryService.
void track_metaspace_memory_usage();
// debugging support.
void dump(outputStream* const out) const;
void print_on(outputStream* st) const;
void locked_print_chunks_in_use_on(outputStream* st) const;
void verify();
void verify_chunk_size(Metachunk* chunk);
NOT_PRODUCT(void mangle_freed_chunks();)
#ifdef ASSERT
void verify_allocated_blocks_words();
#endif
size_t get_raw_word_size(size_t word_size) {
size_t byte_size = word_size * BytesPerWord;
size_t raw_bytes_size = MAX2(byte_size, sizeof(Metablock));
raw_bytes_size = align_size_up(raw_bytes_size, Metachunk::object_alignment());
size_t raw_word_size = raw_bytes_size / BytesPerWord;
assert(raw_word_size * BytesPerWord == raw_bytes_size, "Size problem");
return raw_word_size;
}
};
uint const SpaceManager::_small_chunk_limit = 4;
const char* SpaceManager::_expand_lock_name =
"SpaceManager chunk allocation lock";
const int SpaceManager::_expand_lock_rank = Monitor::leaf - 1;
Mutex* const SpaceManager::_expand_lock =
new Mutex(SpaceManager::_expand_lock_rank,
SpaceManager::_expand_lock_name,
Mutex::_allow_vm_block_flag);
void VirtualSpaceNode::inc_container_count() {
assert_lock_strong(SpaceManager::expand_lock());
_container_count++;
assert(_container_count == container_count_slow(),
err_msg("Inconsistency in countainer_count _container_count " SIZE_FORMAT
" container_count_slow() " SIZE_FORMAT,
_container_count, container_count_slow()));
}
void VirtualSpaceNode::dec_container_count() {
assert_lock_strong(SpaceManager::expand_lock());
_container_count--;
}
#ifdef ASSERT
void VirtualSpaceNode::verify_container_count() {
assert(_container_count == container_count_slow(),
err_msg("Inconsistency in countainer_count _container_count " SIZE_FORMAT
" container_count_slow() " SIZE_FORMAT, _container_count, container_count_slow()));
}
#endif
// BlockFreelist methods
BlockFreelist::BlockFreelist() : _dictionary(NULL) {}
BlockFreelist::~BlockFreelist() {
if (_dictionary != NULL) {
if (Verbose && TraceMetadataChunkAllocation) {
_dictionary->print_free_lists(gclog_or_tty);
}
delete _dictionary;
}
}
void BlockFreelist::return_block(MetaWord* p, size_t word_size) {
Metablock* free_chunk = ::new (p) Metablock(word_size);
if (dictionary() == NULL) {
_dictionary = new BlockTreeDictionary();
}
dictionary()->return_chunk(free_chunk);
}
MetaWord* BlockFreelist::get_block(size_t word_size) {
if (dictionary() == NULL) {
return NULL;
}
if (word_size < TreeChunk<Metablock, FreeList<Metablock> >::min_size()) {
// Dark matter. Too small for dictionary.
return NULL;
}
Metablock* free_block =
dictionary()->get_chunk(word_size, FreeBlockDictionary<Metablock>::atLeast);
if (free_block == NULL) {
return NULL;
}
const size_t block_size = free_block->size();
if (block_size > WasteMultiplier * word_size) {
return_block((MetaWord*)free_block, block_size);
return NULL;
}
MetaWord* new_block = (MetaWord*)free_block;
assert(block_size >= word_size, "Incorrect size of block from freelist");
const size_t unused = block_size - word_size;
if (unused >= TreeChunk<Metablock, FreeList<Metablock> >::min_size()) {
return_block(new_block + word_size, unused);
}
return new_block;
}
void BlockFreelist::print_on(outputStream* st) const {
if (dictionary() == NULL) {
return;
}
dictionary()->print_free_lists(st);
}
// VirtualSpaceNode methods
VirtualSpaceNode::~VirtualSpaceNode() {
_rs.release();
#ifdef ASSERT
size_t word_size = sizeof(*this) / BytesPerWord;
Copy::fill_to_words((HeapWord*) this, word_size, 0xf1f1f1f1);
#endif
}
size_t VirtualSpaceNode::used_words_in_vs() const {
return pointer_delta(top(), bottom(), sizeof(MetaWord));
}
// Space committed in the VirtualSpace
size_t VirtualSpaceNode::capacity_words_in_vs() const {
return pointer_delta(end(), bottom(), sizeof(MetaWord));
}
size_t VirtualSpaceNode::free_words_in_vs() const {
return pointer_delta(end(), top(), sizeof(MetaWord));
}
// Allocates the chunk from the virtual space only.
// This interface is also used internally for debugging. Not all
// chunks removed here are necessarily used for allocation.
Metachunk* VirtualSpaceNode::take_from_committed(size_t chunk_word_size) {
// Bottom of the new chunk
MetaWord* chunk_limit = top();
assert(chunk_limit != NULL, "Not safe to call this method");
// The virtual spaces are always expanded by the
// commit granularity to enforce the following condition.
// Without this the is_available check will not work correctly.
assert(_virtual_space.committed_size() == _virtual_space.actual_committed_size(),
"The committed memory doesn't match the expanded memory.");
if (!is_available(chunk_word_size)) {
if (TraceMetadataChunkAllocation) {
gclog_or_tty->print("VirtualSpaceNode::take_from_committed() not available %d words ", chunk_word_size);
// Dump some information about the virtual space that is nearly full
print_on(gclog_or_tty);
}
return NULL;
}
// Take the space (bump top on the current virtual space).
inc_top(chunk_word_size);
// Initialize the chunk
Metachunk* result = ::new (chunk_limit) Metachunk(chunk_word_size, this);
return result;
}
// Expand the virtual space (commit more of the reserved space)
bool VirtualSpaceNode::expand_by(size_t min_words, size_t preferred_words) {
size_t min_bytes = min_words * BytesPerWord;
size_t preferred_bytes = preferred_words * BytesPerWord;
size_t uncommitted = virtual_space()->reserved_size() - virtual_space()->actual_committed_size();
if (uncommitted < min_bytes) {
return false;
}
size_t commit = MIN2(preferred_bytes, uncommitted);
bool result = virtual_space()->expand_by(commit, false);
assert(result, "Failed to commit memory");
return result;
}
Metachunk* VirtualSpaceNode::get_chunk_vs(size_t chunk_word_size) {
assert_lock_strong(SpaceManager::expand_lock());
Metachunk* result = take_from_committed(chunk_word_size);
if (result != NULL) {
inc_container_count();
}
return result;
}
bool VirtualSpaceNode::initialize() {
if (!_rs.is_reserved()) {
return false;
}
// These are necessary restriction to make sure that the virtual space always
// grows in steps of Metaspace::commit_alignment(). If both base and size are
// aligned only the middle alignment of the VirtualSpace is used.
assert_is_ptr_aligned(_rs.base(), Metaspace::commit_alignment());
assert_is_size_aligned(_rs.size(), Metaspace::commit_alignment());
// ReservedSpaces marked as special will have the entire memory
// pre-committed. Setting a committed size will make sure that
// committed_size and actual_committed_size agrees.
size_t pre_committed_size = _rs.special() ? _rs.size() : 0;
bool result = virtual_space()->initialize_with_granularity(_rs, pre_committed_size,
Metaspace::commit_alignment());
if (result) {
assert(virtual_space()->committed_size() == virtual_space()->actual_committed_size(),
"Checking that the pre-committed memory was registered by the VirtualSpace");
set_top((MetaWord*)virtual_space()->low());
set_reserved(MemRegion((HeapWord*)_rs.base(),
(HeapWord*)(_rs.base() + _rs.size())));
assert(reserved()->start() == (HeapWord*) _rs.base(),
err_msg("Reserved start was not set properly " PTR_FORMAT
" != " PTR_FORMAT, reserved()->start(), _rs.base()));
assert(reserved()->word_size() == _rs.size() / BytesPerWord,