1 /*
2 * Copyright (c) 2018, 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 #include "precompiled.hpp"
26
27 #include "memory/metaspace/metaspaceCommon.hpp"
28 #include "memory/metaspace/virtualSpaceNode.hpp"
29 #include "utilities/debug.hpp"
30 #include "utilities/globalDefinitions.hpp"
31 #include "utilities/ostream.hpp"
32
33 namespace metaspace {
34
35 DEBUG_ONLY(internal_statistics_t g_internal_statistics;)
36
37 // Print a size, in words, scaled.
38 void print_scaled_words(outputStream* st, size_t word_size, size_t scale, int width) {
39 print_human_readable_size(st, word_size * sizeof(MetaWord), scale, width);
40 }
41
42 // Convenience helper: prints a size value and a percentage.
43 void print_scaled_words_and_percentage(outputStream* st, size_t word_size, size_t compare_word_size, size_t scale, int width) {
44 print_scaled_words(st, word_size, scale, width);
45 st->print(" (");
46 print_percentage(st, compare_word_size, word_size);
47 st->print(")");
48 }
49
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, int width) {
57 if (scale == 0) {
58 // Dynamic mode. Choose scale for this value.
59 if (byte_size == 0) {
60 // Zero values are printed as bytes.
61 scale = 1;
62 } else {
63 if (byte_size >= G) {
64 scale = G;
65 } else if (byte_size >= M) {
66 scale = M;
67 } else if (byte_size >= K) {
68 scale = K;
69 } else {
70 scale = 1;
71 }
72 }
73 return print_human_readable_size(st, byte_size, scale, width);
74 }
75
76 #ifdef ASSERT
77 assert(scale == 1 || scale == BytesPerWord || scale == K || scale == M || scale == G, "Invalid scale");
78 // Special case: printing wordsize should only be done with word-sized values
79 if (scale == BytesPerWord) {
80 assert(byte_size % BytesPerWord == 0, "not word sized");
81 }
82 #endif
83
84 if (scale == 1) {
85 st->print("%*" PRIuPTR " bytes", width, byte_size);
86 } else if (scale == BytesPerWord) {
87 st->print("%*" PRIuPTR " words", width, byte_size / BytesPerWord);
88 } else {
89 const char* display_unit = "";
90 switch(scale) {
91 case 1: display_unit = "bytes"; break;
92 case BytesPerWord: display_unit = "words"; break;
93 case K: display_unit = "KB"; break;
94 case M: display_unit = "MB"; break;
95 case G: display_unit = "GB"; break;
96 default:
97 ShouldNotReachHere();
98 }
99 float display_value = (float) byte_size / scale;
100 // Since we use width to display a number with two trailing digits, increase it a bit.
101 width += 3;
102 // Prevent very small but non-null values showing up as 0.00.
103 if (byte_size > 0 && display_value < 0.01f) {
104 st->print("%*s %s", width, "<0.01", display_unit);
105 } else {
106 st->print("%*.2f %s", width, display_value, display_unit);
107 }
108 }
109 }
110
111 // Prints a percentage value. Values smaller than 1% but not 0 are displayed as "<1%", values
112 // larger than 99% but not 100% are displayed as ">100%".
113 void print_percentage(outputStream* st, size_t total, size_t part) {
114 if (total == 0) {
115 st->print(" ?%%");
116 } else if (part == 0) {
117 st->print(" 0%%");
118 } else if (part == total) {
119 st->print("100%%");
120 } else {
121 // Note: clearly print very-small-but-not-0% and very-large-but-not-100% percentages.
122 float p = ((float)part / total) * 100.0f;
123 if (p < 1.0f) {
124 st->print(" <1%%");
125 } else if (p > 99.0f){
126 st->print(">99%%");
127 } else {
128 st->print("%3.0f%%", p);
129 }
130 }
131 }
132
133 // Returns size of this chunk type.
134 size_t get_size_for_nonhumongous_chunktype(ChunkIndex chunktype, bool is_class) {
135 assert(is_valid_nonhumongous_chunktype(chunktype), "invalid chunk type.");
136 size_t size = 0;
137 if (is_class) {
138 switch(chunktype) {
139 case SpecializedIndex: size = ClassSpecializedChunk; break;
140 case SmallIndex: size = ClassSmallChunk; break;
141 case MediumIndex: size = ClassMediumChunk; break;
142 default:
143 ShouldNotReachHere();
144 }
145 } else {
146 switch(chunktype) {
147 case SpecializedIndex: size = SpecializedChunk; break;
148 case SmallIndex: size = SmallChunk; break;
149 case MediumIndex: size = MediumChunk; break;
150 default:
151 ShouldNotReachHere();
152 }
153 }
154 return size;
155 }
156
157 ChunkIndex get_chunk_type_by_size(size_t size, bool is_class) {
158 if (is_class) {
159 if (size == ClassSpecializedChunk) {
160 return SpecializedIndex;
161 } else if (size == ClassSmallChunk) {
162 return SmallIndex;
163 } else if (size == ClassMediumChunk) {
164 return MediumIndex;
165 } else if (size > ClassMediumChunk) {
166 // A valid humongous chunk size is a multiple of the smallest chunk size.
167 assert(is_aligned(size, ClassSpecializedChunk), "Invalid chunk size");
168 return HumongousIndex;
169 }
170 } else {
171 if (size == SpecializedChunk) {
172 return SpecializedIndex;
173 } else if (size == SmallChunk) {
174 return SmallIndex;
175 } else if (size == MediumChunk) {
176 return MediumIndex;
177 } else if (size > MediumChunk) {
178 // A valid humongous chunk size is a multiple of the smallest chunk size.
179 assert(is_aligned(size, SpecializedChunk), "Invalid chunk size");
180 return HumongousIndex;
181 }
182 }
183 ShouldNotReachHere();
184 return (ChunkIndex)-1;
185 }
186
187 ChunkIndex next_chunk_index(ChunkIndex i) {
188 assert(i < NumberOfInUseLists, "Out of bound");
189 return (ChunkIndex) (i+1);
190 }
191
192 ChunkIndex prev_chunk_index(ChunkIndex i) {
193 assert(i > ZeroIndex, "Out of bound");
194 return (ChunkIndex) (i-1);
195 }
196
197 const char* loaders_plural(uintx num) {
198 return num == 1 ? "loader" : "loaders";
199 }
200
201 const char* classes_plural(uintx num) {
202 return num == 1 ? "class" : "classes";
203 }
204
205 void print_number_of_classes(outputStream* out, uintx classes, uintx classes_shared) {
206 out->print(UINTX_FORMAT " %s", classes, classes_plural(classes));
207 if (classes_shared > 0) {
208 out->print(" (" UINTX_FORMAT " shared)", classes_shared);
209 }
210 }
211
212 } // namespace metaspace
213
|
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 #include "precompiled.hpp"
27
28 #include "memory/metaspace/allocationGuard.hpp"
29 #include "memory/metaspace/freeBlocks.hpp"
30 #include "memory/metaspace/metaspaceCommon.hpp"
31 #include "memory/metaspace/virtualSpaceNode.hpp"
32
33 #include "utilities/align.hpp"
34 #include "utilities/debug.hpp"
35 #include "utilities/globalDefinitions.hpp"
36 #include "utilities/ostream.hpp"
37
38 namespace metaspace {
39
40
41 // Print a size, in words, scaled.
42 void print_scaled_words(outputStream* st, size_t word_size, size_t scale, int width) {
43 print_human_readable_size(st, word_size * sizeof(MetaWord), scale, width);
44 }
45
46 // Convenience helper: prints a size value and a percentage.
47 void print_scaled_words_and_percentage(outputStream* st, size_t word_size, size_t compare_word_size, size_t scale, int width) {
48 print_scaled_words(st, word_size, scale, width);
49 st->print(" (");
50 print_percentage(st, compare_word_size, word_size);
51 st->print(")");
52 }
53
54 static const char* display_unit_for_scale(size_t scale) {
55 const char* s = NULL;
56 switch(scale) {
57 case 1: s = "bytes"; break;
58 case BytesPerWord: s = "words"; break;
59 case K: s = "KB"; break;
60 case M: s = "MB"; break;
61 case G: s = "GB"; break;
62 default:
63 ShouldNotReachHere();
64 }
65 return s;
66 }
67
68 // Print a human readable size.
69 // byte_size: size, in bytes, to be printed.
70 // scale: one of 1 (byte-wise printing), sizeof(word) (word-size printing), K, M, G (scaled by KB, MB, GB respectively,
71 // or 0, which means the best scale is choosen dynamically.
72 // width: printing width.
73 void print_human_readable_size(outputStream* st, size_t byte_size, size_t scale, int width) {
74 if (scale == 0) {
75 // Dynamic mode. Choose scale for this value.
76 if (byte_size == 0) {
77 // Zero values are printed as bytes.
78 scale = 1;
79 } else {
80 if (byte_size >= G) {
81 scale = G;
82 } else if (byte_size >= M) {
83 scale = M;
84 } else if (byte_size >= K) {
85 scale = K;
86 } else {
87 scale = 1;
88 }
89 }
90 return print_human_readable_size(st, byte_size, scale, width);
91 }
92
93 #ifdef ASSERT
94 assert(scale == 1 || scale == BytesPerWord ||
95 scale == K || scale == M || scale == G, "Invalid scale");
96 // Special case: printing wordsize should only be done with word-sized values
97 if (scale == BytesPerWord) {
98 assert(byte_size % BytesPerWord == 0, "not word sized");
99 }
100 #endif
101
102 if (width == -1) {
103 if (scale == 1) {
104 st->print(SIZE_FORMAT " bytes", byte_size);
105 } else if (scale == BytesPerWord) {
106 st->print(SIZE_FORMAT " words", byte_size / BytesPerWord);
107 } else {
108 const char* display_unit = display_unit_for_scale(scale);
109 float display_value = (float) byte_size / scale;
110 // Prevent very small but non-null values showing up as 0.00.
111 if (byte_size > 0 && display_value < 0.01f) {
112 st->print("<0.01 %s", display_unit);
113 } else {
114 st->print("%.2f %s", display_value, display_unit);
115 }
116 }
117 } else {
118 if (scale == 1) {
119 st->print("%*" PRIuPTR " bytes", width, byte_size);
120 } else if (scale == BytesPerWord) {
121 st->print("%*" PRIuPTR " words", width, byte_size / BytesPerWord);
122 } else {
123 const char* display_unit = display_unit_for_scale(scale);
124 float display_value = (float) byte_size / scale;
125 // Since we use width to display a number with two trailing digits, increase it a bit.
126 width += 3;
127 // Prevent very small but non-null values showing up as 0.00.
128 if (byte_size > 0 && display_value < 0.01f) {
129 st->print("%*s %s", width, "<0.01", display_unit);
130 } else {
131 st->print("%*.2f %s", width, display_value, display_unit);
132 }
133 }
134 }
135 }
136
137 // Prints a percentage value. Values smaller than 1% but not 0 are displayed as "<1%", values
138 // larger than 99% but not 100% are displayed as ">100%".
139 void print_percentage(outputStream* st, size_t total, size_t part) {
140 if (total == 0) {
141 st->print(" ?%%");
142 } else if (part == 0) {
143 st->print(" 0%%");
144 } else if (part == total) {
145 st->print("100%%");
146 } else {
147 // Note: clearly print very-small-but-not-0% and very-large-but-not-100% percentages.
148 float p = ((float)part / total) * 100.0f;
149 if (p < 1.0f) {
150 st->print(" <1%%");
151 } else if (p > 99.0f){
152 st->print(">99%%");
153 } else {
154 st->print("%3.0f%%", p);
155 }
156 }
157 }
158
159 const char* loaders_plural(uintx num) {
160 return num == 1 ? "loader" : "loaders";
161 }
162
163 const char* classes_plural(uintx num) {
164 return num == 1 ? "class" : "classes";
165 }
166
167 void print_number_of_classes(outputStream* out, uintx classes, uintx classes_shared) {
168 out->print(UINTX_FORMAT " %s", classes, classes_plural(classes));
169 if (classes_shared > 0) {
170 out->print(" (" UINTX_FORMAT " shared)", classes_shared);
171 }
172 }
173
174 // Given a net allocation word size, return the raw word size we actually allocate.
175 // Note: externally visible for gtests.
176 //static
177 size_t get_raw_word_size_for_requested_word_size(size_t word_size) {
178
179 size_t byte_size = word_size * BytesPerWord;
180
181 // Deallocated metablocks are kept in a binlist which limits their minimal
182 // size to at least the size of a binlist item (2 words).
183 byte_size = MAX2(byte_size, FreeBlocks::minimal_word_size * BytesPerWord);
184
185 // Metaspace allocations are aligned to word size.
186 byte_size = align_up(byte_size, allocation_alignment_bytes);
187
188 // If we guard allocations, we need additional space for a prefix.
189 #ifdef ASSERT
190 if (Settings::use_allocation_guard()) {
191 byte_size += align_up(prefix_size(), allocation_alignment_bytes);
192 }
193 #endif
194
195 size_t raw_word_size = byte_size / BytesPerWord;
196
197 assert(raw_word_size * BytesPerWord == byte_size, "Sanity");
198
199 return raw_word_size;
200
201 }
202
203 } // namespace metaspace
204
|