1 /*
2 * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
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
25 #ifndef SHARE_MEMORY_METASPACE_METASPACECOMMON_HPP
26 #define SHARE_MEMORY_METASPACE_METASPACECOMMON_HPP
27
28 #include "utilities/align.hpp"
29 #include "utilities/debug.hpp"
30 #include "utilities/globalDefinitions.hpp"
31
32 class outputStream;
33
34 namespace metaspace {
35
36 enum ChunkSizes { // in words.
37 ClassSpecializedChunk = 128,
38 SpecializedChunk = 128,
39 ClassSmallChunk = 256,
40 SmallChunk = 512,
41 ClassMediumChunk = 4 * K,
42 MediumChunk = 8 * K
43 };
44
45 // Print a size, in words, scaled.
46 void print_scaled_words(outputStream* st, size_t word_size, size_t scale = 0, int width = -1);
47
48 // Convenience helper: prints a size value and a percentage.
49 void print_scaled_words_and_percentage(outputStream* st, size_t word_size, size_t compare_word_size, size_t scale = 0, int width = -1);
50
51 // Print a human readable size.
52 // byte_size: size, in bytes, to be printed.
53 // scale: one of 1 (byte-wise printing), sizeof(word) (word-size printing), K, M, G (scaled by KB, MB, GB respectively,
54 // or 0, which means the best scale is choosen dynamically.
55 // width: printing width.
56 void print_human_readable_size(outputStream* st, size_t byte_size, size_t scale = 0, int width = -1);
57
58 // Prints a percentage value. Values smaller than 1% but not 0 are displayed as "<1%", values
59 // larger than 99% but not 100% are displayed as ">100%".
60 void print_percentage(outputStream* st, size_t total, size_t part);
61
62
63 #define assert_is_aligned(value, alignment) \
64 assert(is_aligned((value), (alignment)), \
65 SIZE_FORMAT_HEX " is not aligned to " \
66 SIZE_FORMAT, (size_t)(uintptr_t)value, (alignment))
67
68 // Internal statistics.
69 #ifdef ASSERT
70 struct internal_statistics_t {
71 // Number of allocations.
72 uintx num_allocs;
73 // Number of times a ClassLoaderMetaspace was born...
74 uintx num_metaspace_births;
75 // ... and died.
76 uintx num_metaspace_deaths;
77 // Number of times VirtualSpaceListNodes were created...
78 uintx num_vsnodes_created;
79 // ... and purged.
80 uintx num_vsnodes_purged;
81 // Number of times we expanded the committed section of the space.
82 uintx num_committed_space_expanded;
83 // Number of deallocations
84 uintx num_deallocs;
85 // Number of deallocations triggered from outside ("real" deallocations).
86 uintx num_external_deallocs;
87 // Number of times an allocation was satisfied from deallocated blocks.
88 uintx num_allocs_from_deallocated_blocks;
89 // Number of times a chunk was added to the freelist
90 uintx num_chunks_added_to_freelist;
91 // Number of times a chunk was removed from the freelist
92 uintx num_chunks_removed_from_freelist;
93 // Number of chunk merges
94 uintx num_chunk_merges;
95 // Number of chunk splits
96 uintx num_chunk_splits;
97 };
98 extern internal_statistics_t g_internal_statistics;
99 #endif
100
101 // ChunkIndex defines the type of chunk.
102 // Chunk types differ by size: specialized < small < medium, chunks
103 // larger than medium are humongous chunks of varying size.
104 enum ChunkIndex {
105 ZeroIndex = 0,
106 SpecializedIndex = ZeroIndex,
107 SmallIndex = SpecializedIndex + 1,
108 MediumIndex = SmallIndex + 1,
109 HumongousIndex = MediumIndex + 1,
110 NumberOfFreeLists = 3,
111 NumberOfInUseLists = 4
112 };
113
114 // Utility functions.
115 size_t get_size_for_nonhumongous_chunktype(ChunkIndex chunk_type, bool is_class);
116 ChunkIndex get_chunk_type_by_size(size_t size, bool is_class);
117
118 ChunkIndex next_chunk_index(ChunkIndex i);
119 ChunkIndex prev_chunk_index(ChunkIndex i);
120 // Returns a descriptive name for a chunk type.
121 const char* chunk_size_name(ChunkIndex index);
122
123 // Verify chunk sizes.
124 inline bool is_valid_chunksize(bool is_class, size_t size) {
125 const size_t reasonable_maximum_humongous_chunk_size = 1 * G;
126 return is_aligned(size, sizeof(MetaWord)) &&
127 size < reasonable_maximum_humongous_chunk_size &&
128 is_class ?
129 (size == ClassSpecializedChunk || size == ClassSmallChunk || size >= ClassMediumChunk) :
130 (size == SpecializedChunk || size == SmallChunk || size >= MediumChunk);
131 }
132
133 // Verify chunk type.
134 inline bool is_valid_chunktype(ChunkIndex index) {
135 return index == SpecializedIndex || index == SmallIndex ||
136 index == MediumIndex || index == HumongousIndex;
137 }
138
139 inline bool is_valid_nonhumongous_chunktype(ChunkIndex index) {
140 return is_valid_chunktype(index) && index != HumongousIndex;
141 }
142
143 // Pretty printing helpers
144 const char* classes_plural(uintx num);
145 const char* loaders_plural(uintx num);
146 void print_number_of_classes(outputStream* out, uintx classes, uintx classes_shared);
147
148 } // namespace metaspace
149
150 #endif // SHARE_MEMORY_METASPACE_METASPACECOMMON_HPP
|
1 /*
2 * Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved.
3 * Copyright (c) 2018, 2020 SAP SE. All rights reserved.
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 *
6 * This code is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 only, as
8 * published by the Free Software Foundation.
9 *
10 * This code is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * version 2 for more details (a copy is included in the LICENSE file that
14 * accompanied this code).
15 *
16 * You should have received a copy of the GNU General Public License version
17 * 2 along with this work; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21 * or visit www.oracle.com if you need additional information or have any
22 * questions.
23 *
24 */
25
26 #ifndef SHARE_MEMORY_METASPACE_METASPACECOMMON_HPP
27 #define SHARE_MEMORY_METASPACE_METASPACECOMMON_HPP
28
29 #include "runtime/globals.hpp"
30 #include "utilities/align.hpp"
31 #include "utilities/debug.hpp"
32 #include "utilities/globalDefinitions.hpp"
33
34 class outputStream;
35
36 namespace metaspace {
37
38
39 // Metaspace allocation alignment:
40
41 // 1) Metaspace allocations have to be aligned such that 64bit values are aligned
42 // correctly.
43 //
44 // 2) Klass* structures allocated from Metaspace have to be aligned to KlassAlignmentInBytes.
45 //
46 // At the moment LogKlassAlignmentInBytes is 3, so KlassAlignmentInBytes == 8,
47 // so (1) and (2) can both be fulfilled with an alignment of 8. Should we increase
48 // KlassAlignmentInBytes at any time this will increase the necessary alignment as well. In
49 // that case we may think about introducing a separate alignment just for the class space
50 // since that alignment would only be needed for Klass structures.
51
52 static const size_t allocation_alignment_bytes = 8;
53 STATIC_ASSERT(allocation_alignment_bytes == (size_t)KlassAlignmentInBytes);
54
55 static const size_t allocation_alignment_words = allocation_alignment_bytes / BytesPerWord;
56
57 // Returns the raw word size allocated for a given net allocation
58 size_t get_raw_word_size_for_requested_word_size(size_t word_size);
59
60
61 // Utility functions
62
63 // Print a size, in words, scaled.
64 void print_scaled_words(outputStream* st, size_t word_size, size_t scale = 0, int width = -1);
65
66 // Convenience helper: prints a size value and a percentage.
67 void print_scaled_words_and_percentage(outputStream* st, size_t word_size, size_t compare_word_size, size_t scale = 0, int width = -1);
68
69 // Print a human readable size.
70 // byte_size: size, in bytes, to be printed.
71 // scale: one of 1 (byte-wise printing), sizeof(word) (word-size printing), K, M, G (scaled by KB, MB, GB respectively,
72 // or 0, which means the best scale is choosen dynamically.
73 // width: printing width.
74 void print_human_readable_size(outputStream* st, size_t byte_size, size_t scale = 0, int width = -1);
75
76 // Prints a percentage value. Values smaller than 1% but not 0 are displayed as "<1%", values
77 // larger than 99% but not 100% are displayed as ">100%".
78 void print_percentage(outputStream* st, size_t total, size_t part);
79
80
81 #ifdef ASSERT
82 #define assert_is_aligned(value, alignment) \
83 assert(is_aligned((value), (alignment)), \
84 SIZE_FORMAT_HEX " is not aligned to " \
85 SIZE_FORMAT_HEX, (size_t)(uintptr_t)value, (size_t)(alignment))
86 #else
87 #define assert_is_aligned(value, alignment)
88 #endif
89
90
91 // Pretty printing helpers
92 const char* classes_plural(uintx num);
93 const char* loaders_plural(uintx num);
94 void print_number_of_classes(outputStream* out, uintx classes, uintx classes_shared);
95
96
97 // Since Metaspace verifications are expensive, we want to do them at a reduced rate,
98 // but not completely avoiding them.
99 // For that we introduce the macros SOMETIMES() and ASSERT_SOMETIMES() which will
100 // execute code or assert at intervals controlled via VerifyMetaspaceInterval.
101 #ifdef ASSERT
102
103 #define EVERY_NTH(n) \
104 { static int counter_ = 0; \
105 if (n > 0) { \
106 counter_ ++; \
107 if (counter_ > n) { \
108 counter_ = 0; \
109
110 #define END_EVERY_NTH } } }
111
112 #define SOMETIMES(code) \
113 EVERY_NTH(VerifyMetaspaceInterval) \
114 { code } \
115 END_EVERY_NTH
116
117 #define ASSERT_SOMETIMES(condition, ...) \
118 EVERY_NTH(VerifyMetaspaceInterval) \
119 assert( (condition), __VA_ARGS__); \
120 END_EVERY_NTH
121
122 #else
123
124 #define SOMETIMES(code)
125 #define ASSERT_SOMETIMES(condition, ...)
126
127 #endif // ASSERT
128
129 ///////// Logging //////////////
130
131 // What we log at which levels:
132
133 // "info" : metaspace failed allocation, commit failure, reserve failure, metaspace oom, metaspace gc threshold changed, Arena created, destroyed, metaspace purged
134
135 // "debug" : "info" + vslist extended, memory committed/uncommitted, chunk created/split/merged/enlarged, chunk returned
136
137 // "trace" : "debug" + every single allocation and deallocation, internals
138
139 #define HAVE_UL
140
141 #ifdef HAVE_UL
142 #define UL(level, message) log_##level(metaspace)(LOGFMT ": " message, LOGFMT_ARGS);
143 #define UL2(level, message, ...) log_##level(metaspace)(LOGFMT ": " message, LOGFMT_ARGS, __VA_ARGS__);
144 #else
145 #define UL(level, ...)
146 #define UL2(level, ...)
147 #endif
148
149 } // namespace metaspace
150
151 #endif // SHARE_MEMORY_METASPACE_METASPACECOMMON_HPP
|