92 // (auxiliary stuff goes into MetaspaceUtils) |
87 // (auxiliary stuff goes into MetaspaceUtils) |
93 class Metaspace : public AllStatic { |
88 class Metaspace : public AllStatic { |
94 |
89 |
95 friend class MetaspaceShared; |
90 friend class MetaspaceShared; |
96 |
91 |
97 public: |
92 // Base and size of the compressed class space. |
98 enum MetadataType { |
93 static MetaWord* _compressed_class_space_base; |
99 ClassType, |
|
100 NonClassType, |
|
101 MetadataTypeCount |
|
102 }; |
|
103 enum MetaspaceType { |
|
104 ZeroMetaspaceType = 0, |
|
105 StandardMetaspaceType = ZeroMetaspaceType, |
|
106 BootMetaspaceType = StandardMetaspaceType + 1, |
|
107 UnsafeAnonymousMetaspaceType = BootMetaspaceType + 1, |
|
108 ReflectionMetaspaceType = UnsafeAnonymousMetaspaceType + 1, |
|
109 MetaspaceTypeCount |
|
110 }; |
|
111 |
|
112 private: |
|
113 |
|
114 // Align up the word size to the allocation word size |
|
115 static size_t align_word_size_up(size_t); |
|
116 |
|
117 // Aligned size of the metaspace. |
|
118 static size_t _compressed_class_space_size; |
94 static size_t _compressed_class_space_size; |
119 |
|
120 static size_t compressed_class_space_size() { |
|
121 return _compressed_class_space_size; |
|
122 } |
|
123 |
|
124 static void set_compressed_class_space_size(size_t size) { |
|
125 _compressed_class_space_size = size; |
|
126 } |
|
127 |
|
128 static size_t _first_chunk_word_size; |
|
129 static size_t _first_class_chunk_word_size; |
|
130 |
95 |
131 static size_t _commit_alignment; |
96 static size_t _commit_alignment; |
132 static size_t _reserve_alignment; |
97 static size_t _reserve_alignment; |
133 DEBUG_ONLY(static bool _frozen;) |
98 DEBUG_ONLY(static bool _frozen;) |
134 |
99 |
135 // Virtual Space lists for both classes and other metadata |
|
136 static metaspace::VirtualSpaceList* _space_list; |
|
137 static metaspace::VirtualSpaceList* _class_space_list; |
|
138 |
|
139 static metaspace::ChunkManager* _chunk_manager_metadata; |
|
140 static metaspace::ChunkManager* _chunk_manager_class; |
|
141 |
|
142 static const MetaspaceTracer* _tracer; |
100 static const MetaspaceTracer* _tracer; |
143 |
101 |
144 static bool _initialized; |
102 static bool _initialized; |
145 |
103 |
146 public: |
104 static MetaWord* compressed_class_space_base() { return _compressed_class_space_base; } |
147 static metaspace::VirtualSpaceList* space_list() { return _space_list; } |
105 static size_t compressed_class_space_size() { return _compressed_class_space_size; } |
148 static metaspace::VirtualSpaceList* class_space_list() { return _class_space_list; } |
106 |
149 static metaspace::VirtualSpaceList* get_space_list(MetadataType mdtype) { |
107 public: |
150 assert(mdtype != MetadataTypeCount, "MetadaTypeCount can't be used as mdtype"); |
|
151 return mdtype == ClassType ? class_space_list() : space_list(); |
|
152 } |
|
153 |
|
154 static metaspace::ChunkManager* chunk_manager_metadata() { return _chunk_manager_metadata; } |
|
155 static metaspace::ChunkManager* chunk_manager_class() { return _chunk_manager_class; } |
|
156 static metaspace::ChunkManager* get_chunk_manager(MetadataType mdtype) { |
|
157 assert(mdtype != MetadataTypeCount, "MetadaTypeCount can't be used as mdtype"); |
|
158 return mdtype == ClassType ? chunk_manager_class() : chunk_manager_metadata(); |
|
159 } |
|
160 |
|
161 // convenience function |
|
162 static metaspace::ChunkManager* get_chunk_manager(bool is_class) { |
|
163 return is_class ? chunk_manager_class() : chunk_manager_metadata(); |
|
164 } |
|
165 |
108 |
166 static const MetaspaceTracer* tracer() { return _tracer; } |
109 static const MetaspaceTracer* tracer() { return _tracer; } |
167 static void freeze() { |
110 static void freeze() { |
168 assert(DumpSharedSpaces, "sanity"); |
111 assert(DumpSharedSpaces, "sanity"); |
169 DEBUG_ONLY(_frozen = true;) |
112 DEBUG_ONLY(_frozen = true;) |
190 |
133 |
191 static void ergo_initialize(); |
134 static void ergo_initialize(); |
192 static void global_initialize(); |
135 static void global_initialize(); |
193 static void post_initialize(); |
136 static void post_initialize(); |
194 |
137 |
195 static void verify_global_initialization(); |
138 // The alignment at which Metaspace mappings are reserved. |
196 |
|
197 static size_t first_chunk_word_size() { return _first_chunk_word_size; } |
|
198 static size_t first_class_chunk_word_size() { return _first_class_chunk_word_size; } |
|
199 |
|
200 static size_t reserve_alignment() { return _reserve_alignment; } |
139 static size_t reserve_alignment() { return _reserve_alignment; } |
201 static size_t reserve_alignment_words() { return _reserve_alignment / BytesPerWord; } |
140 static size_t reserve_alignment_words() { return _reserve_alignment / BytesPerWord; } |
|
141 |
|
142 // The granularity at which Metaspace is committed and uncommitted. |
202 static size_t commit_alignment() { return _commit_alignment; } |
143 static size_t commit_alignment() { return _commit_alignment; } |
203 static size_t commit_alignment_words() { return _commit_alignment / BytesPerWord; } |
144 static size_t commit_words() { return _commit_alignment / BytesPerWord; } |
204 |
145 |
205 static MetaWord* allocate(ClassLoaderData* loader_data, size_t word_size, |
146 static MetaWord* allocate(ClassLoaderData* loader_data, size_t word_size, |
206 MetaspaceObj::Type type, TRAPS); |
147 MetaspaceObj::Type type, TRAPS); |
207 |
148 |
208 static bool contains(const void* ptr); |
149 static bool contains(const void* ptr); |
209 static bool contains_non_shared(const void* ptr); |
150 static bool contains_non_shared(const void* ptr); |
210 |
151 |
211 // Free empty virtualspaces |
152 // Free empty virtualspaces |
212 static void purge(MetadataType mdtype); |
|
213 static void purge(); |
153 static void purge(); |
214 |
154 |
215 static void report_metadata_oome(ClassLoaderData* loader_data, size_t word_size, |
155 static void report_metadata_oome(ClassLoaderData* loader_data, size_t word_size, |
216 MetaspaceObj::Type type, MetadataType mdtype, TRAPS); |
156 MetaspaceObj::Type type, metaspace::MetadataType mdtype, TRAPS); |
217 |
|
218 static const char* metadata_type_name(Metaspace::MetadataType mdtype); |
|
219 |
157 |
220 static void print_compressed_class_space(outputStream* st, const char* requested_addr = 0) NOT_LP64({}); |
158 static void print_compressed_class_space(outputStream* st, const char* requested_addr = 0) NOT_LP64({}); |
221 |
159 |
222 // Return TRUE only if UseCompressedClassPointers is True. |
160 // Return TRUE only if UseCompressedClassPointers is True. |
223 static bool using_class_space() { |
161 static bool using_class_space() { |
224 return NOT_LP64(false) LP64_ONLY(UseCompressedClassPointers); |
162 return NOT_LP64(false) LP64_ONLY(UseCompressedClassPointers); |
225 } |
163 } |
226 |
164 |
227 static bool is_class_space_allocation(MetadataType mdType) { |
|
228 return mdType == ClassType && using_class_space(); |
|
229 } |
|
230 |
|
231 static bool initialized() { return _initialized; } |
165 static bool initialized() { return _initialized; } |
232 |
166 |
233 }; |
167 }; |
234 |
168 |
235 // Manages the metaspace portion belonging to a class loader |
169 ////////////////// MetaspaceGC /////////////////////// |
236 class ClassLoaderMetaspace : public CHeapObj<mtClass> { |
|
237 friend class CollectedHeap; // For expand_and_allocate() |
|
238 friend class ZCollectedHeap; // For expand_and_allocate() |
|
239 friend class ShenandoahHeap; // For expand_and_allocate() |
|
240 friend class Metaspace; |
|
241 friend class MetaspaceUtils; |
|
242 friend class metaspace::PrintCLDMetaspaceInfoClosure; |
|
243 friend class VM_CollectForMetadataAllocation; // For expand_and_allocate() |
|
244 |
|
245 private: |
|
246 |
|
247 void initialize(Mutex* lock, Metaspace::MetaspaceType type); |
|
248 |
|
249 // Initialize the first chunk for a Metaspace. Used for |
|
250 // special cases such as the boot class loader, reflection |
|
251 // class loader and anonymous class loader. |
|
252 void initialize_first_chunk(Metaspace::MetaspaceType type, Metaspace::MetadataType mdtype); |
|
253 metaspace::Metachunk* get_initialization_chunk(Metaspace::MetaspaceType type, Metaspace::MetadataType mdtype); |
|
254 |
|
255 const Metaspace::MetaspaceType _space_type; |
|
256 Mutex* const _lock; |
|
257 metaspace::SpaceManager* _vsm; |
|
258 metaspace::SpaceManager* _class_vsm; |
|
259 |
|
260 metaspace::SpaceManager* vsm() const { return _vsm; } |
|
261 metaspace::SpaceManager* class_vsm() const { return _class_vsm; } |
|
262 metaspace::SpaceManager* get_space_manager(Metaspace::MetadataType mdtype) { |
|
263 assert(mdtype != Metaspace::MetadataTypeCount, "MetadaTypeCount can't be used as mdtype"); |
|
264 return mdtype == Metaspace::ClassType ? class_vsm() : vsm(); |
|
265 } |
|
266 |
|
267 Mutex* lock() const { return _lock; } |
|
268 |
|
269 MetaWord* expand_and_allocate(size_t size, Metaspace::MetadataType mdtype); |
|
270 |
|
271 size_t class_chunk_size(size_t word_size); |
|
272 |
|
273 // Adds to the given statistic object. Must be locked with CLD metaspace lock. |
|
274 void add_to_statistics_locked(metaspace::ClassLoaderMetaspaceStatistics* out) const; |
|
275 |
|
276 Metaspace::MetaspaceType space_type() const { return _space_type; } |
|
277 |
|
278 public: |
|
279 |
|
280 ClassLoaderMetaspace(Mutex* lock, Metaspace::MetaspaceType type); |
|
281 ~ClassLoaderMetaspace(); |
|
282 |
|
283 // Allocate space for metadata of type mdtype. This is space |
|
284 // within a Metachunk and is used by |
|
285 // allocate(ClassLoaderData*, size_t, bool, MetadataType, TRAPS) |
|
286 MetaWord* allocate(size_t word_size, Metaspace::MetadataType mdtype); |
|
287 |
|
288 size_t allocated_blocks_bytes() const; |
|
289 size_t allocated_chunks_bytes() const; |
|
290 |
|
291 void deallocate(MetaWord* ptr, size_t byte_size, bool is_class); |
|
292 |
|
293 void print_on(outputStream* st) const; |
|
294 // Debugging support |
|
295 void verify(); |
|
296 |
|
297 // Adds to the given statistic object. Will lock with CLD metaspace lock. |
|
298 void add_to_statistics(metaspace::ClassLoaderMetaspaceStatistics* out) const; |
|
299 |
|
300 }; // ClassLoaderMetaspace |
|
301 |
|
302 class MetaspaceUtils : AllStatic { |
|
303 |
|
304 // Spacemanager updates running counters. |
|
305 friend class metaspace::SpaceManager; |
|
306 |
|
307 // Special access for error reporting (checks without locks). |
|
308 friend class oopDesc; |
|
309 friend class Klass; |
|
310 |
|
311 // Running counters for statistics concerning in-use chunks. |
|
312 // Note: capacity = used + free + waste + overhead. Note that we do not |
|
313 // count free and waste. Their sum can be deduces from the three other values. |
|
314 // For more details, one should call print_report() from within a safe point. |
|
315 static size_t _capacity_words [Metaspace:: MetadataTypeCount]; |
|
316 static size_t _overhead_words [Metaspace:: MetadataTypeCount]; |
|
317 static volatile size_t _used_words [Metaspace:: MetadataTypeCount]; |
|
318 |
|
319 // Atomically decrement or increment in-use statistic counters |
|
320 static void dec_capacity(Metaspace::MetadataType mdtype, size_t words); |
|
321 static void inc_capacity(Metaspace::MetadataType mdtype, size_t words); |
|
322 static void dec_used(Metaspace::MetadataType mdtype, size_t words); |
|
323 static void inc_used(Metaspace::MetadataType mdtype, size_t words); |
|
324 static void dec_overhead(Metaspace::MetadataType mdtype, size_t words); |
|
325 static void inc_overhead(Metaspace::MetadataType mdtype, size_t words); |
|
326 |
|
327 |
|
328 // Getters for the in-use counters. |
|
329 static size_t capacity_words(Metaspace::MetadataType mdtype) { return _capacity_words[mdtype]; } |
|
330 static size_t used_words(Metaspace::MetadataType mdtype) { return _used_words[mdtype]; } |
|
331 static size_t overhead_words(Metaspace::MetadataType mdtype) { return _overhead_words[mdtype]; } |
|
332 |
|
333 static size_t free_chunks_total_words(Metaspace::MetadataType mdtype); |
|
334 |
|
335 // Helper for print_xx_report. |
|
336 static void print_vs(outputStream* out, size_t scale); |
|
337 |
|
338 public: |
|
339 |
|
340 // Collect used metaspace statistics. This involves walking the CLDG. The resulting |
|
341 // output will be the accumulated values for all live metaspaces. |
|
342 // Note: method does not do any locking. |
|
343 static void collect_statistics(metaspace::ClassLoaderMetaspaceStatistics* out); |
|
344 |
|
345 // Used by MetaspaceCounters |
|
346 static size_t free_chunks_total_words(); |
|
347 static size_t free_chunks_total_bytes(); |
|
348 static size_t free_chunks_total_bytes(Metaspace::MetadataType mdtype); |
|
349 |
|
350 static size_t capacity_words() { |
|
351 return capacity_words(Metaspace::NonClassType) + |
|
352 capacity_words(Metaspace::ClassType); |
|
353 } |
|
354 static size_t capacity_bytes(Metaspace::MetadataType mdtype) { |
|
355 return capacity_words(mdtype) * BytesPerWord; |
|
356 } |
|
357 static size_t capacity_bytes() { |
|
358 return capacity_words() * BytesPerWord; |
|
359 } |
|
360 |
|
361 static size_t used_words() { |
|
362 return used_words(Metaspace::NonClassType) + |
|
363 used_words(Metaspace::ClassType); |
|
364 } |
|
365 static size_t used_bytes(Metaspace::MetadataType mdtype) { |
|
366 return used_words(mdtype) * BytesPerWord; |
|
367 } |
|
368 static size_t used_bytes() { |
|
369 return used_words() * BytesPerWord; |
|
370 } |
|
371 |
|
372 // Space committed but yet unclaimed by any class loader. |
|
373 static size_t free_in_vs_bytes(); |
|
374 static size_t free_in_vs_bytes(Metaspace::MetadataType mdtype); |
|
375 |
|
376 static size_t reserved_bytes(Metaspace::MetadataType mdtype); |
|
377 static size_t reserved_bytes() { |
|
378 return reserved_bytes(Metaspace::ClassType) + |
|
379 reserved_bytes(Metaspace::NonClassType); |
|
380 } |
|
381 |
|
382 static size_t committed_bytes(Metaspace::MetadataType mdtype); |
|
383 static size_t committed_bytes() { |
|
384 return committed_bytes(Metaspace::ClassType) + |
|
385 committed_bytes(Metaspace::NonClassType); |
|
386 } |
|
387 |
|
388 static size_t min_chunk_size_words(); |
|
389 |
|
390 // Flags for print_report(). |
|
391 enum ReportFlag { |
|
392 // Show usage by class loader. |
|
393 rf_show_loaders = (1 << 0), |
|
394 // Breaks report down by chunk type (small, medium, ...). |
|
395 rf_break_down_by_chunktype = (1 << 1), |
|
396 // Breaks report down by space type (anonymous, reflection, ...). |
|
397 rf_break_down_by_spacetype = (1 << 2), |
|
398 // Print details about the underlying virtual spaces. |
|
399 rf_show_vslist = (1 << 3), |
|
400 // Print metaspace map. |
|
401 rf_show_vsmap = (1 << 4), |
|
402 // If show_loaders: show loaded classes for each loader. |
|
403 rf_show_classes = (1 << 5) |
|
404 }; |
|
405 |
|
406 // This will print out a basic metaspace usage report but |
|
407 // unlike print_report() is guaranteed not to lock or to walk the CLDG. |
|
408 static void print_basic_report(outputStream* st, size_t scale); |
|
409 |
|
410 // Prints a report about the current metaspace state. |
|
411 // Optional parts can be enabled via flags. |
|
412 // Function will walk the CLDG and will lock the expand lock; if that is not |
|
413 // convenient, use print_basic_report() instead. |
|
414 static void print_report(outputStream* out, size_t scale = 0, int flags = 0); |
|
415 |
|
416 static bool has_chunk_free_list(Metaspace::MetadataType mdtype); |
|
417 static MetaspaceChunkFreeListSummary chunk_free_list_summary(Metaspace::MetadataType mdtype); |
|
418 |
|
419 // Log change in used metadata. |
|
420 static void print_metaspace_change(const metaspace::MetaspaceSizesSnapshot& pre_meta_values); |
|
421 static void print_on(outputStream * out); |
|
422 |
|
423 // Prints an ASCII representation of the given space. |
|
424 static void print_metaspace_map(outputStream* out, Metaspace::MetadataType mdtype); |
|
425 |
|
426 static void dump(outputStream* out); |
|
427 static void verify_free_chunks(); |
|
428 // Check internal counters (capacity, used). |
|
429 static void verify_metrics(); |
|
430 }; |
|
431 |
170 |
432 // Metaspace are deallocated when their class loader are GC'ed. |
171 // Metaspace are deallocated when their class loader are GC'ed. |
433 // This class implements a policy for inducing GC's to recover |
172 // This class implements a policy for inducing GC's to recover |
434 // Metaspaces. |
173 // Metaspaces. |
435 |
174 |
436 class MetaspaceGC : AllStatic { |
175 class MetaspaceGCThresholdUpdater : public AllStatic { |
|
176 public: |
|
177 enum Type { |
|
178 ComputeNewSize, |
|
179 ExpandAndAllocate, |
|
180 Last |
|
181 }; |
|
182 |
|
183 static const char* to_string(MetaspaceGCThresholdUpdater::Type updater) { |
|
184 switch (updater) { |
|
185 case ComputeNewSize: |
|
186 return "compute_new_size"; |
|
187 case ExpandAndAllocate: |
|
188 return "expand_and_allocate"; |
|
189 default: |
|
190 assert(false, "Got bad updater: %d", (int) updater); |
|
191 return NULL; |
|
192 }; |
|
193 } |
|
194 }; |
|
195 |
|
196 class MetaspaceGC : public AllStatic { |
437 |
197 |
438 // The current high-water-mark for inducing a GC. |
198 // The current high-water-mark for inducing a GC. |
439 // When committed memory of all metaspaces reaches this value, |
199 // When committed memory of all metaspaces reaches this value, |
440 // a GC is induced and the value is increased. Size is in bytes. |
200 // a GC is induced and the value is increased. Size is in bytes. |
441 static volatile size_t _capacity_until_GC; |
201 static volatile size_t _capacity_until_GC; |
479 // Calculate the new high-water mark at which to induce |
239 // Calculate the new high-water mark at which to induce |
480 // a GC. |
240 // a GC. |
481 static void compute_new_size(); |
241 static void compute_new_size(); |
482 }; |
242 }; |
483 |
243 |
|
244 |
|
245 |
|
246 |
|
247 class MetaspaceUtils : AllStatic { |
|
248 public: |
|
249 |
|
250 // Committed space actually in use by Metadata |
|
251 static size_t used_words(); |
|
252 static size_t used_words(metaspace::MetadataType mdtype); |
|
253 |
|
254 // Space committed for Metaspace |
|
255 static size_t committed_words(); |
|
256 static size_t committed_words(metaspace::MetadataType mdtype); |
|
257 |
|
258 // Space reserved for Metaspace |
|
259 static size_t reserved_words(); |
|
260 static size_t reserved_words(metaspace::MetadataType mdtype); |
|
261 |
|
262 // _bytes() variants for convenience... |
|
263 static size_t used_bytes() { return used_words() * BytesPerWord; } |
|
264 static size_t used_bytes(metaspace::MetadataType mdtype) { return used_words(mdtype) * BytesPerWord; } |
|
265 static size_t committed_bytes() { return committed_words() * BytesPerWord; } |
|
266 static size_t committed_bytes(metaspace::MetadataType mdtype) { return committed_words(mdtype) * BytesPerWord; } |
|
267 static size_t reserved_bytes() { return reserved_words() * BytesPerWord; } |
|
268 static size_t reserved_bytes(metaspace::MetadataType mdtype) { return reserved_words(mdtype) * BytesPerWord; } |
|
269 |
|
270 // TODO. Do we need this really? This number is kind of uninformative. |
|
271 static size_t capacity_bytes() { return 0; } |
|
272 static size_t capacity_bytes(metaspace::MetadataType mdtype) { return 0; } |
|
273 |
|
274 // Todo. Consolidate. |
|
275 // Committed space in freelists |
|
276 static size_t free_chunks_total_words(metaspace::MetadataType mdtype); |
|
277 |
|
278 // Todo. Implement or Consolidate. |
|
279 static MetaspaceChunkFreeListSummary chunk_free_list_summary(metaspace::MetadataType mdtype) { |
|
280 return MetaspaceChunkFreeListSummary(0,0,0,0,0,0,0,0); |
|
281 } |
|
282 |
|
283 // Log change in used metadata. |
|
284 static void print_metaspace_change(const metaspace::MetaspaceSizesSnapshot& pre_meta_values); |
|
285 |
|
286 // Prints an ASCII representation of the given space. |
|
287 static void print_metaspace_map(outputStream* out, metaspace::MetadataType mdtype); |
|
288 |
|
289 // This will print out a basic metaspace usage report but |
|
290 // unlike print_report() is guaranteed not to lock or to walk the CLDG. |
|
291 static void print_basic_report(outputStream* st, size_t scale = 0); |
|
292 |
|
293 // Prints a report about the current metaspace state. |
|
294 // Function will walk the CLDG and will lock the expand lock; if that is not |
|
295 // convenient, use print_basic_report() instead. |
|
296 static void print_full_report(outputStream* out, size_t scale = 0); |
|
297 |
|
298 static void print_on(outputStream * out); |
|
299 |
|
300 DEBUG_ONLY(static void verify(bool slow);) |
|
301 |
|
302 }; |
|
303 |
484 #endif // SHARE_MEMORY_METASPACE_HPP |
304 #endif // SHARE_MEMORY_METASPACE_HPP |