40 // | | Virtualspace |
41 // | | |
42 // | | |
43 // | |-------------------|
44 // | || Chunk |
45 // | || |
46 // | ||---------- |
47 // +------>||| block 0 | |
48 // ||---------- |
49 // ||| block 1 | |
50 // ||---------- |
51 // || |
52 // |-------------------|
53 // | |
54 // | |
55 // +-------------------+
56 //
57
58 class ChunkManager;
59 class ClassLoaderData;
60 class Metablock;
61 class Metachunk;
62 class MetaspaceTracer;
63 class MetaWord;
64 class Mutex;
65 class outputStream;
66 class PrintCLDMetaspaceInfoClosure;
67 class SpaceManager;
68 class VirtualSpaceList;
69 class CollectedHeap;
70
71 // Metaspaces each have a SpaceManager and allocations
72 // are done by the SpaceManager. Allocations are done
73 // out of the current Metachunk. When the current Metachunk
74 // is exhausted, the SpaceManager gets a new one from
75 // the current VirtualSpace. When the VirtualSpace is exhausted
76 // the SpaceManager gets a new one. The SpaceManager
77 // also manages freelists of available Chunks.
78 //
79 // Currently the space manager maintains the list of
80 // virtual spaces and the list of chunks in use. Its
81 // allocate() method returns a block for use as a
82 // quantum of metadata.
83
84 // Namespace for important central static functions
85 // (auxiliary stuff goes into MetaspaceUtils)
86 class Metaspace : public AllStatic {
87
88 friend class MetaspaceShared;
89
90 public:
91 enum MetadataType {
92 ClassType,
93 NonClassType,
94 MetadataTypeCount
95 };
96 enum MetaspaceType {
97 StandardMetaspaceType,
98 BootMetaspaceType,
99 AnonymousMetaspaceType,
100 ReflectionMetaspaceType
101 };
102
103 private:
104
105 // Align up the word size to the allocation word size
106 static size_t align_word_size_up(size_t);
107
108 // Aligned size of the metaspace.
109 static size_t _compressed_class_space_size;
110
111 static size_t compressed_class_space_size() {
112 return _compressed_class_space_size;
113 }
114
115 static void set_compressed_class_space_size(size_t size) {
116 _compressed_class_space_size = size;
117 }
118
119 static size_t _first_chunk_word_size;
120 static size_t _first_class_chunk_word_size;
221 };
222
223 // Manages the metaspace portion belonging to a class loader
224 class ClassLoaderMetaspace : public CHeapObj<mtClass> {
225 friend class CollectedHeap; // For expand_and_allocate()
226 friend class Metaspace;
227 friend class MetaspaceUtils;
228 friend class PrintCLDMetaspaceInfoClosure;
229 friend class VM_CollectForMetadataAllocation; // For expand_and_allocate()
230
231 private:
232
233 void initialize(Mutex* lock, Metaspace::MetaspaceType type);
234
235 // Initialize the first chunk for a Metaspace. Used for
236 // special cases such as the boot class loader, reflection
237 // class loader and anonymous class loader.
238 void initialize_first_chunk(Metaspace::MetaspaceType type, Metaspace::MetadataType mdtype);
239 Metachunk* get_initialization_chunk(Metaspace::MetaspaceType type, Metaspace::MetadataType mdtype);
240
241 SpaceManager* _vsm;
242 SpaceManager* vsm() const { return _vsm; }
243
244 SpaceManager* _class_vsm;
245 SpaceManager* class_vsm() const { return _class_vsm; }
246 SpaceManager* get_space_manager(Metaspace::MetadataType mdtype) {
247 assert(mdtype != Metaspace::MetadataTypeCount, "MetadaTypeCount can't be used as mdtype");
248 return mdtype == Metaspace::ClassType ? class_vsm() : vsm();
249 }
250
251 MetaWord* expand_and_allocate(size_t size, Metaspace::MetadataType mdtype);
252
253 size_t class_chunk_size(size_t word_size);
254
255 public:
256
257 ClassLoaderMetaspace(Mutex* lock, Metaspace::MetaspaceType type);
258 ~ClassLoaderMetaspace();
259
260 // Allocate space for metadata of type mdtype. This is space
261 // within a Metachunk and is used by
262 // allocate(ClassLoaderData*, size_t, bool, MetadataType, TRAPS)
263 MetaWord* allocate(size_t word_size, Metaspace::MetadataType mdtype);
264
265 size_t used_words_slow(Metaspace::MetadataType mdtype) const;
266 size_t free_words_slow(Metaspace::MetadataType mdtype) const;
267 size_t capacity_words_slow(Metaspace::MetadataType mdtype) const;
268
269 size_t used_bytes_slow(Metaspace::MetadataType mdtype) const;
270 size_t capacity_bytes_slow(Metaspace::MetadataType mdtype) const;
271
272 size_t allocated_blocks_bytes() const;
273 size_t allocated_chunks_bytes() const;
274
275 void deallocate(MetaWord* ptr, size_t byte_size, bool is_class);
276
277 void dump(outputStream* const out) const;
278
279 void print_on(outputStream* st) const;
280 // Debugging support
281 void verify();
282
283 }; // ClassLoaderMetaspace
284
285
286 class MetaspaceUtils : AllStatic {
287 static size_t free_chunks_total_words(Metaspace::MetadataType mdtype);
288
289 // These methods iterate over the classloader data graph
290 // for the given Metaspace type. These are slow.
291 static size_t used_bytes_slow(Metaspace::MetadataType mdtype);
292 static size_t free_bytes_slow(Metaspace::MetadataType mdtype);
293 static size_t capacity_bytes_slow(Metaspace::MetadataType mdtype);
294 static size_t capacity_bytes_slow();
295
296 // Running sum of space in all Metachunks that has been
297 // allocated to a Metaspace. This is used instead of
298 // iterating over all the classloaders. One for each
299 // type of Metadata
300 static size_t _capacity_words[Metaspace:: MetadataTypeCount];
301 // Running sum of space in all Metachunks that
302 // are being used for metadata. One for each
303 // type of Metadata.
304 static volatile size_t _used_words[Metaspace:: MetadataTypeCount];
305
306 public:
307 // Decrement and increment _allocated_capacity_words
308 static void dec_capacity(Metaspace::MetadataType type, size_t words);
309 static void inc_capacity(Metaspace::MetadataType type, size_t words);
310
311 // Decrement and increment _allocated_used_words
312 static void dec_used(Metaspace::MetadataType type, size_t words);
313 static void inc_used(Metaspace::MetadataType type, size_t words);
314
315 // Total of space allocated to metadata in all Metaspaces.
316 // This sums the space used in each Metachunk by
317 // iterating over the classloader data graph
318 static size_t used_bytes_slow() {
319 return used_bytes_slow(Metaspace::ClassType) +
320 used_bytes_slow(Metaspace::NonClassType);
321 }
322
323 // Used by MetaspaceCounters
324 static size_t free_chunks_total_words();
325 static size_t free_chunks_total_bytes();
326 static size_t free_chunks_total_bytes(Metaspace::MetadataType mdtype);
327
328 static size_t capacity_words(Metaspace::MetadataType mdtype) {
329 return _capacity_words[mdtype];
330 }
331 static size_t capacity_words() {
332 return capacity_words(Metaspace::NonClassType) +
333 capacity_words(Metaspace::ClassType);
334 }
335 static size_t capacity_bytes(Metaspace::MetadataType mdtype) {
336 return capacity_words(mdtype) * BytesPerWord;
337 }
338 static size_t capacity_bytes() {
339 return capacity_words() * BytesPerWord;
340 }
341
342 static size_t used_words(Metaspace::MetadataType mdtype) {
343 return _used_words[mdtype];
344 }
345 static size_t used_words() {
346 return used_words(Metaspace::NonClassType) +
347 used_words(Metaspace::ClassType);
348 }
349 static size_t used_bytes(Metaspace::MetadataType mdtype) {
350 return used_words(mdtype) * BytesPerWord;
351 }
352 static size_t used_bytes() {
353 return used_words() * BytesPerWord;
354 }
355
356 static size_t free_bytes();
357 static size_t free_bytes(Metaspace::MetadataType mdtype);
358
359 static size_t reserved_bytes(Metaspace::MetadataType mdtype);
360 static size_t reserved_bytes() {
361 return reserved_bytes(Metaspace::ClassType) +
362 reserved_bytes(Metaspace::NonClassType);
363 }
364
365 static size_t committed_bytes(Metaspace::MetadataType mdtype);
366 static size_t committed_bytes() {
367 return committed_bytes(Metaspace::ClassType) +
368 committed_bytes(Metaspace::NonClassType);
369 }
370
371 static size_t min_chunk_size_words();
372 static size_t min_chunk_size_bytes() {
373 return min_chunk_size_words() * BytesPerWord;
374 }
375
376 static void print_metadata_for_nmt(outputStream* out, size_t scale = K);
377
378 static bool has_chunk_free_list(Metaspace::MetadataType mdtype);
379 static MetaspaceChunkFreeListSummary chunk_free_list_summary(Metaspace::MetadataType mdtype);
380
381 // Print change in used metadata.
382 static void print_metaspace_change(size_t prev_metadata_used);
383 static void print_on(outputStream * out);
384 static void print_on(outputStream * out, Metaspace::MetadataType mdtype);
385
386 static void print_class_waste(outputStream* out);
387 static void print_waste(outputStream* out);
388
389 // Prints an ASCII representation of the given space.
390 static void print_metaspace_map(outputStream* out, Metaspace::MetadataType mdtype);
391
392 static void dump(outputStream* out);
393 static void verify_free_chunks();
394 // Checks that the values returned by allocated_capacity_bytes() and
395 // capacity_bytes_slow() are the same.
396 static void verify_capacity();
397 static void verify_used();
398 static void verify_metrics();
399 };
400
401 // Metaspace are deallocated when their class loader are GC'ed.
402 // This class implements a policy for inducing GC's to recover
403 // Metaspaces.
404
405 class MetaspaceGC : AllStatic {
406
407 // The current high-water-mark for inducing a GC.
408 // When committed memory of all metaspaces reaches this value,
409 // a GC is induced and the value is increased. Size is in bytes.
410 static volatile intptr_t _capacity_until_GC;
411
412 // For a CMS collection, signal that a concurrent collection should
413 // be started.
414 static bool _should_concurrent_collect;
415
416 static uint _shrink_factor;
417
|
40 // | | Virtualspace |
41 // | | |
42 // | | |
43 // | |-------------------|
44 // | || Chunk |
45 // | || |
46 // | ||---------- |
47 // +------>||| block 0 | |
48 // ||---------- |
49 // ||| block 1 | |
50 // ||---------- |
51 // || |
52 // |-------------------|
53 // | |
54 // | |
55 // +-------------------+
56 //
57
58 class ChunkManager;
59 class ClassLoaderData;
60 class ClassLoaderMetaspaceStatistics;
61 class Metablock;
62 class Metachunk;
63 class MetaspaceTracer;
64 class MetaWord;
65 class Mutex;
66 class outputStream;
67 class PrintCLDMetaspaceInfoClosure;
68 class SpaceManager;
69 class VirtualSpaceList;
70 class CollectedHeap;
71
72 // Metaspaces each have a SpaceManager and allocations
73 // are done by the SpaceManager. Allocations are done
74 // out of the current Metachunk. When the current Metachunk
75 // is exhausted, the SpaceManager gets a new one from
76 // the current VirtualSpace. When the VirtualSpace is exhausted
77 // the SpaceManager gets a new one. The SpaceManager
78 // also manages freelists of available Chunks.
79 //
80 // Currently the space manager maintains the list of
81 // virtual spaces and the list of chunks in use. Its
82 // allocate() method returns a block for use as a
83 // quantum of metadata.
84
85 // Namespace for important central static functions
86 // (auxiliary stuff goes into MetaspaceUtils)
87 class Metaspace : public AllStatic {
88
89 friend class MetaspaceShared;
90
91 public:
92 enum MetadataType {
93 ClassType,
94 NonClassType,
95 MetadataTypeCount
96 };
97 enum MetaspaceType {
98 ZeroMetaspaceType = 0,
99 //...
100 StandardMetaspaceType = ZeroMetaspaceType,
101 BootMetaspaceType = StandardMetaspaceType + 1,
102 AnonymousMetaspaceType = BootMetaspaceType + 1,
103 ReflectionMetaspaceType = AnonymousMetaspaceType + 1,
104 //..
105 MetaspaceTypeCount
106 };
107
108 private:
109
110 // Align up the word size to the allocation word size
111 static size_t align_word_size_up(size_t);
112
113 // Aligned size of the metaspace.
114 static size_t _compressed_class_space_size;
115
116 static size_t compressed_class_space_size() {
117 return _compressed_class_space_size;
118 }
119
120 static void set_compressed_class_space_size(size_t size) {
121 _compressed_class_space_size = size;
122 }
123
124 static size_t _first_chunk_word_size;
125 static size_t _first_class_chunk_word_size;
226 };
227
228 // Manages the metaspace portion belonging to a class loader
229 class ClassLoaderMetaspace : public CHeapObj<mtClass> {
230 friend class CollectedHeap; // For expand_and_allocate()
231 friend class Metaspace;
232 friend class MetaspaceUtils;
233 friend class PrintCLDMetaspaceInfoClosure;
234 friend class VM_CollectForMetadataAllocation; // For expand_and_allocate()
235
236 private:
237
238 void initialize(Mutex* lock, Metaspace::MetaspaceType type);
239
240 // Initialize the first chunk for a Metaspace. Used for
241 // special cases such as the boot class loader, reflection
242 // class loader and anonymous class loader.
243 void initialize_first_chunk(Metaspace::MetaspaceType type, Metaspace::MetadataType mdtype);
244 Metachunk* get_initialization_chunk(Metaspace::MetaspaceType type, Metaspace::MetadataType mdtype);
245
246 const Metaspace::MetaspaceType _space_type;
247 Mutex* const _lock;
248 SpaceManager* _vsm;
249 SpaceManager* _class_vsm;
250
251 SpaceManager* vsm() const { return _vsm; }
252 SpaceManager* class_vsm() const { return _class_vsm; }
253 SpaceManager* get_space_manager(Metaspace::MetadataType mdtype) {
254 assert(mdtype != Metaspace::MetadataTypeCount, "MetadaTypeCount can't be used as mdtype");
255 return mdtype == Metaspace::ClassType ? class_vsm() : vsm();
256 }
257
258 Mutex* lock() const { return _lock; }
259
260 MetaWord* expand_and_allocate(size_t size, Metaspace::MetadataType mdtype);
261
262 size_t class_chunk_size(size_t word_size);
263
264 // Adds to the given statistic object. Must be locked with CLD metaspace lock.
265 void add_to_statistics_locked(ClassLoaderMetaspaceStatistics* out) const;
266
267 public:
268
269 ClassLoaderMetaspace(Mutex* lock, Metaspace::MetaspaceType type);
270 ~ClassLoaderMetaspace();
271
272 Metaspace::MetaspaceType space_type() const { return _space_type; }
273
274 // Allocate space for metadata of type mdtype. This is space
275 // within a Metachunk and is used by
276 // allocate(ClassLoaderData*, size_t, bool, MetadataType, TRAPS)
277 MetaWord* allocate(size_t word_size, Metaspace::MetadataType mdtype);
278
279 size_t allocated_blocks_bytes() const;
280 size_t allocated_chunks_bytes() const;
281
282 void deallocate(MetaWord* ptr, size_t byte_size, bool is_class);
283
284 void print_on(outputStream* st) const;
285 // Debugging support
286 void verify();
287
288 // Adds to the given statistic object. Will lock with CLD metaspace lock.
289 void add_to_statistics(ClassLoaderMetaspaceStatistics* out) const;
290
291 }; // ClassLoaderMetaspace
292
293 class MetaspaceUtils : AllStatic {
294 static size_t free_chunks_total_words(Metaspace::MetadataType mdtype);
295
296 // Running counters for statistics concerning in-use chunks.
297 enum StatType {
298 stat_overhead = 0,
299 stat_capacity,
300 stat_used,
301 stat_free,
302 stat_waste,
303 stat_num_values
304 };
305 volatile size_t _in_use_stats[stat_num_values][Metaspace::MetadataTypeCount];
306
307 template <StatType e> static void inc_stat_value(Metaspace::MetadataType mdtype, size_t words);
308 template <StatType e> static void dec_stat_value(Metaspace::MetadataType mdtype, size_t words);
309 template <StatType e> static size_t get_stat_value(Metaspace::MetadataType mdtype) const;
310
311 public:
312
313 // Collect used metaspace statistics. This involves walking the CLDG. The resulting
314 // output will be the accumulated values for all live metaspaces.
315 // Note: method does not do any locking.
316 static void collect_statistics(ClassLoaderMetaspaceStatistics* out);
317
318 // Atomically decrement or increment in-use statistic counters.
319 static void dec_capacity(Metaspace::MetadataType mdtype, size_t words) { dec_stat_value<stat_capacity>(mdtype, words); }
320 static void inc_capacity(Metaspace::MetadataType mdtype, size_t words) { inc_stat_value<stat_capacity>(mdtype, words); }
321 static size_t capacity_words(Metaspace::MetadataType mdtype) const { return get_stat_value<stat_capacity>(mdtype); }
322
323 static void dec_used(Metaspace::MetadataType mdtype, size_t words) { dec_stat_value<stat_used>(mdtype, words); }
324 static void inc_used(Metaspace::MetadataType mdtype, size_t words) { inc_stat_value<stat_used>(mdtype, words); }
325 static size_t used_words(Metaspace::MetadataType mdtype) const { return get_stat_value<stat_used>(mdtype); }
326
327 static void dec_overhead(Metaspace::MetadataType mdtype, size_t words) { dec_stat_value<stat_overhead>(mdtype, words); }
328 static void inc_overhead(Metaspace::MetadataType mdtype, size_t words) { inc_stat_value<stat_overhead>(mdtype, words); }
329 static size_t overhead_words(Metaspace::MetadataType mdtype) const { return get_stat_value<stat_overhead>(mdtype); }
330
331 static void dec_free(Metaspace::MetadataType mdtype, size_t words) { dec_stat_value<stat_free>(mdtype, words); }
332 static void inc_free(Metaspace::MetadataType mdtype, size_t words) { inc_stat_value<stat_free>(mdtype, words); }
333 static size_t free_words(Metaspace::MetadataType mdtype) const { return get_stat_value<stat_free>(mdtype); }
334
335 static void dec_waste(Metaspace::MetadataType mdtype, size_t words) { dec_stat_value<stat_waste>(mdtype, words); }
336 static void inc_waste(Metaspace::MetadataType mdtype, size_t words) { inc_stat_value<stat_waste>(mdtype, words); }
337 static size_t waste_words(Metaspace::MetadataType mdtype) const { return get_stat_value<stat_waste>(mdtype); }
338
339 // Used by MetaspaceCounters
340 static size_t free_chunks_total_words();
341 static size_t free_chunks_total_bytes();
342 static size_t free_chunks_total_bytes(Metaspace::MetadataType mdtype);
343
344 static size_t capacity_words() {
345 return capacity_words(Metaspace::NonClassType) +
346 capacity_words(Metaspace::ClassType);
347 }
348 static size_t capacity_bytes(Metaspace::MetadataType mdtype) {
349 return capacity_words(mdtype) * BytesPerWord;
350 }
351 static size_t capacity_bytes() {
352 return capacity_words() * BytesPerWord;
353 }
354
355 static size_t used_words() {
356 return used_words(Metaspace::NonClassType) +
357 used_words(Metaspace::ClassType);
358 }
359 static size_t used_bytes(Metaspace::MetadataType mdtype) {
360 return used_words(mdtype) * BytesPerWord;
361 }
362 static size_t used_bytes() {
363 return used_words() * BytesPerWord;
364 }
365
366 static size_t free_bytes();
367 static size_t free_bytes(Metaspace::MetadataType mdtype);
368
369 static size_t reserved_bytes(Metaspace::MetadataType mdtype);
370 static size_t reserved_bytes() {
371 return reserved_bytes(Metaspace::ClassType) +
372 reserved_bytes(Metaspace::NonClassType);
373 }
374
375 static size_t committed_bytes(Metaspace::MetadataType mdtype);
376 static size_t committed_bytes() {
377 return committed_bytes(Metaspace::ClassType) +
378 committed_bytes(Metaspace::NonClassType);
379 }
380
381 static size_t min_chunk_size_words();
382 static size_t min_chunk_size_bytes() {
383 return min_chunk_size_words() * BytesPerWord;
384 }
385
386 // Flags for print_report().
387 enum ReportFlag {
388 // Breaks report down by class loader.
389 rf_show_loaders = (1 << 0),
390 // Breaks report down by chunk type (small, medium, ...).
391 rf_break_down_by_chunktype = (1 << 1),
392 // Breaks report down by space type (anonymous, reflection, ...).
393 rf_break_down_by_spacetype = (1 << 2),
394 // Print details about the underlying virtual spaces.
395 rf_show_vslist = (1 << 3),
396 // Print metaspace map.
397 rf_show_vsmap = (1 << 4),
398 // Print details about the free chunk list.
399 rf_show_chunk_freelist = (1 << 5),
400 };
401
402 // Prints a report about the current metaspace state. Form of report is defined
403 // by flags (see ReportFlags). In its most basic form (flags == 0), a short
404 // overview is printed.
405 // Scale is one of 1, K, M, G - in which case numbers are printed as bytes,
406 // KB, MB, GB, respectively - or 0, in which case the most suitable scale
407 // is chosen dynamically.
408 static void print_report(outputStream* out, size_t scale = 0, int flags = 0);
409
410 static bool has_chunk_free_list(Metaspace::MetadataType mdtype);
411 static MetaspaceChunkFreeListSummary chunk_free_list_summary(Metaspace::MetadataType mdtype);
412
413 // Print change in used metadata.
414 static void print_metaspace_change(size_t prev_metadata_used);
415 static void print_on(outputStream * out);
416
417 // Prints an ASCII representation of the given space.
418 static void print_metaspace_map(outputStream* out, Metaspace::MetadataType mdtype);
419
420 static void dump(outputStream* out);
421 static void verify_free_chunks();
422 // Check internal counters (capacity, used).
423 static void verify_metrics();
424 };
425
426 // Metaspace are deallocated when their class loader are GC'ed.
427 // This class implements a policy for inducing GC's to recover
428 // Metaspaces.
429
430 class MetaspaceGC : AllStatic {
431
432 // The current high-water-mark for inducing a GC.
433 // When committed memory of all metaspaces reaches this value,
434 // a GC is induced and the value is increased. Size is in bytes.
435 static volatile intptr_t _capacity_until_GC;
436
437 // For a CMS collection, signal that a concurrent collection should
438 // be started.
439 static bool _should_concurrent_collect;
440
441 static uint _shrink_factor;
442
|