63 class MetaWord;
64 class Mutex;
65 class outputStream;
66 class PrintCLDMetaspaceInfoClosure;
67 class SpaceManager;
68 class VirtualSpaceList;
69
70 // Metaspaces each have a SpaceManager and allocations
71 // are done by the SpaceManager. Allocations are done
72 // out of the current Metachunk. When the current Metachunk
73 // is exhausted, the SpaceManager gets a new one from
74 // the current VirtualSpace. When the VirtualSpace is exhausted
75 // the SpaceManager gets a new one. The SpaceManager
76 // also manages freelists of available Chunks.
77 //
78 // Currently the space manager maintains the list of
79 // virtual spaces and the list of chunks in use. Its
80 // allocate() method returns a block for use as a
81 // quantum of metadata.
82
83 class Metaspace : public CHeapObj<mtClass> {
84 friend class VMStructs;
85 friend class SpaceManager;
86 friend class VM_CollectForMetadataAllocation;
87 friend class MetaspaceGC;
88 friend class MetaspaceUtils;
89 friend class MetaspaceShared;
90 friend class CollectedHeap;
91 friend class PrintCLDMetaspaceInfoClosure;
92 friend class MetaspaceAllocationTest;
93
94 public:
95 enum MetadataType {
96 ClassType,
97 NonClassType,
98 MetadataTypeCount
99 };
100 enum MetaspaceType {
101 StandardMetaspaceType,
102 BootMetaspaceType,
103 AnonymousMetaspaceType,
104 ReflectionMetaspaceType
105 };
106
107 private:
108 static void verify_global_initialization();
109
110 void initialize(Mutex* lock, MetaspaceType type);
111
112 // Initialize the first chunk for a Metaspace. Used for
113 // special cases such as the boot class loader, reflection
114 // class loader and anonymous class loader.
115 void initialize_first_chunk(MetaspaceType type, MetadataType mdtype);
116 Metachunk* get_initialization_chunk(MetaspaceType type, MetadataType mdtype);
117
118 // Align up the word size to the allocation word size
119 static size_t align_word_size_up(size_t);
120
121 // Aligned size of the metaspace.
122 static size_t _compressed_class_space_size;
123
124 static size_t compressed_class_space_size() {
125 return _compressed_class_space_size;
126 }
127
128 static void set_compressed_class_space_size(size_t size) {
129 _compressed_class_space_size = size;
130 }
131
132 static size_t _first_chunk_word_size;
133 static size_t _first_class_chunk_word_size;
134
135 static size_t _commit_alignment;
136 static size_t _reserve_alignment;
137 DEBUG_ONLY(static bool _frozen;)
138
139 SpaceManager* _vsm;
140 SpaceManager* vsm() const { return _vsm; }
141
142 SpaceManager* _class_vsm;
143 SpaceManager* class_vsm() const { return _class_vsm; }
144 SpaceManager* get_space_manager(MetadataType mdtype) {
145 assert(mdtype != MetadataTypeCount, "MetadaTypeCount can't be used as mdtype");
146 return mdtype == ClassType ? class_vsm() : vsm();
147 }
148
149 // Allocate space for metadata of type mdtype. This is space
150 // within a Metachunk and is used by
151 // allocate(ClassLoaderData*, size_t, bool, MetadataType, TRAPS)
152 MetaWord* allocate(size_t word_size, MetadataType mdtype);
153
154 MetaWord* expand_and_allocate(size_t size, MetadataType mdtype);
155
156 // Virtual Space lists for both classes and other metadata
157 static VirtualSpaceList* _space_list;
158 static VirtualSpaceList* _class_space_list;
159
160 static ChunkManager* _chunk_manager_metadata;
161 static ChunkManager* _chunk_manager_class;
162
163 static const MetaspaceTracer* _tracer;
164
165 public:
166 static VirtualSpaceList* space_list() { return _space_list; }
167 static VirtualSpaceList* class_space_list() { return _class_space_list; }
168 static VirtualSpaceList* get_space_list(MetadataType mdtype) {
169 assert(mdtype != MetadataTypeCount, "MetadaTypeCount can't be used as mdtype");
170 return mdtype == ClassType ? class_space_list() : space_list();
171 }
172
173 static ChunkManager* chunk_manager_metadata() { return _chunk_manager_metadata; }
174 static ChunkManager* chunk_manager_class() { return _chunk_manager_class; }
175 static ChunkManager* get_chunk_manager(MetadataType mdtype) {
176 assert(mdtype != MetadataTypeCount, "MetadaTypeCount can't be used as mdtype");
177 return mdtype == ClassType ? chunk_manager_class() : chunk_manager_metadata();
178 }
179
180 // convenience function
181 static ChunkManager* get_chunk_manager(bool is_class) {
182 return is_class ? chunk_manager_class() : chunk_manager_metadata();
183 }
184
185 static const MetaspaceTracer* tracer() { return _tracer; }
186 static void freeze() {
187 assert(DumpSharedSpaces, "sanity");
188 DEBUG_ONLY(_frozen = true;)
189 }
190 #ifdef _LP64
191 static void allocate_metaspace_compressed_klass_ptrs(char* requested_addr, address cds_base);
192 #endif
193
194 private:
195
196 #ifdef _LP64
197 static void set_narrow_klass_base_and_shift(address metaspace_base, address cds_base);
198
199 // Returns true if can use CDS with metaspace allocated as specified address.
200 static bool can_use_cds_with_metaspace_addr(char* metaspace_base, address cds_base);
201
202 static void initialize_class_space(ReservedSpace rs);
203 #endif
204 size_t class_chunk_size(size_t word_size);
205
206 public:
207
208 Metaspace(Mutex* lock, MetaspaceType type);
209 ~Metaspace();
210
211 static void ergo_initialize();
212 static void global_initialize();
213 static void post_initialize();
214
215 static size_t first_chunk_word_size() { return _first_chunk_word_size; }
216 static size_t first_class_chunk_word_size() { return _first_class_chunk_word_size; }
217
218 static size_t reserve_alignment() { return _reserve_alignment; }
219 static size_t reserve_alignment_words() { return _reserve_alignment / BytesPerWord; }
220 static size_t commit_alignment() { return _commit_alignment; }
221 static size_t commit_alignment_words() { return _commit_alignment / BytesPerWord; }
222
223 size_t used_words_slow(MetadataType mdtype) const;
224 size_t free_words_slow(MetadataType mdtype) const;
225 size_t capacity_words_slow(MetadataType mdtype) const;
226
227 size_t used_bytes_slow(MetadataType mdtype) const;
228 size_t capacity_bytes_slow(MetadataType mdtype) const;
229
230 size_t allocated_blocks_bytes() const;
231 size_t allocated_chunks_bytes() const;
232
233 static MetaWord* allocate(ClassLoaderData* loader_data, size_t word_size,
234 MetaspaceObj::Type type, TRAPS);
235 void deallocate(MetaWord* ptr, size_t byte_size, bool is_class);
236
237 static bool contains(const void* ptr);
238 static bool contains_non_shared(const void* ptr);
239
240 void dump(outputStream* const out) const;
241
242 // Free empty virtualspaces
243 static void purge(MetadataType mdtype);
244 static void purge();
245
246 static void report_metadata_oome(ClassLoaderData* loader_data, size_t word_size,
247 MetaspaceObj::Type type, MetadataType mdtype, TRAPS);
248
249 static const char* metadata_type_name(Metaspace::MetadataType mdtype);
250
251 void print_on(outputStream* st) const;
252 // Debugging support
253 void verify();
254
255 static void print_compressed_class_space(outputStream* st, const char* requested_addr = 0) NOT_LP64({});
256
257 // Return TRUE only if UseCompressedClassPointers is True.
258 static bool using_class_space() {
259 return NOT_LP64(false) LP64_ONLY(UseCompressedClassPointers);
260 }
261
262 static bool is_class_space_allocation(MetadataType mdType) {
263 return mdType == ClassType && using_class_space();
264 }
265
266 };
267
268 class MetaspaceUtils : AllStatic {
269 static size_t free_chunks_total_words(Metaspace::MetadataType mdtype);
270
271 // These methods iterate over the classloader data graph
272 // for the given Metaspace type. These are slow.
273 static size_t used_bytes_slow(Metaspace::MetadataType mdtype);
274 static size_t free_bytes_slow(Metaspace::MetadataType mdtype);
275 static size_t capacity_bytes_slow(Metaspace::MetadataType mdtype);
276 static size_t capacity_bytes_slow();
277
278 // Running sum of space in all Metachunks that has been
279 // allocated to a Metaspace. This is used instead of
280 // iterating over all the classloaders. One for each
281 // type of Metadata
282 static size_t _capacity_words[Metaspace:: MetadataTypeCount];
283 // Running sum of space in all Metachunks that
284 // are being used for metadata. One for each
285 // type of Metadata.
286 static volatile size_t _used_words[Metaspace:: MetadataTypeCount];
|
63 class MetaWord;
64 class Mutex;
65 class outputStream;
66 class PrintCLDMetaspaceInfoClosure;
67 class SpaceManager;
68 class VirtualSpaceList;
69
70 // Metaspaces each have a SpaceManager and allocations
71 // are done by the SpaceManager. Allocations are done
72 // out of the current Metachunk. When the current Metachunk
73 // is exhausted, the SpaceManager gets a new one from
74 // the current VirtualSpace. When the VirtualSpace is exhausted
75 // the SpaceManager gets a new one. The SpaceManager
76 // also manages freelists of available Chunks.
77 //
78 // Currently the space manager maintains the list of
79 // virtual spaces and the list of chunks in use. Its
80 // allocate() method returns a block for use as a
81 // quantum of metadata.
82
83 // Namespace for important central static functions
84 // (auxiliary stuff goes into MetaspaceUtils)
85 class Metaspace : public AllStatic {
86
87 friend class MetaspaceShared;
88
89 public:
90 enum MetadataType {
91 ClassType,
92 NonClassType,
93 MetadataTypeCount
94 };
95 enum MetaspaceType {
96 StandardMetaspaceType,
97 BootMetaspaceType,
98 AnonymousMetaspaceType,
99 ReflectionMetaspaceType
100 };
101
102 private:
103
104 // Align up the word size to the allocation word size
105 static size_t align_word_size_up(size_t);
106
107 // Aligned size of the metaspace.
108 static size_t _compressed_class_space_size;
109
110 static size_t compressed_class_space_size() {
111 return _compressed_class_space_size;
112 }
113
114 static void set_compressed_class_space_size(size_t size) {
115 _compressed_class_space_size = size;
116 }
117
118 static size_t _first_chunk_word_size;
119 static size_t _first_class_chunk_word_size;
120
121 static size_t _commit_alignment;
122 static size_t _reserve_alignment;
123 DEBUG_ONLY(static bool _frozen;)
124
125 // Virtual Space lists for both classes and other metadata
126 static VirtualSpaceList* _space_list;
127 static VirtualSpaceList* _class_space_list;
128
129 static ChunkManager* _chunk_manager_metadata;
130 static ChunkManager* _chunk_manager_class;
131
132 static const MetaspaceTracer* _tracer;
133
134 public:
135 static VirtualSpaceList* space_list() { return _space_list; }
136 static VirtualSpaceList* class_space_list() { return _class_space_list; }
137 static VirtualSpaceList* get_space_list(MetadataType mdtype) {
138 assert(mdtype != MetadataTypeCount, "MetadaTypeCount can't be used as mdtype");
139 return mdtype == ClassType ? class_space_list() : space_list();
140 }
141
142 static ChunkManager* chunk_manager_metadata() { return _chunk_manager_metadata; }
143 static ChunkManager* chunk_manager_class() { return _chunk_manager_class; }
144 static ChunkManager* get_chunk_manager(MetadataType mdtype) {
145 assert(mdtype != MetadataTypeCount, "MetadaTypeCount can't be used as mdtype");
146 return mdtype == ClassType ? chunk_manager_class() : chunk_manager_metadata();
147 }
148
149 // convenience function
150 static ChunkManager* get_chunk_manager(bool is_class) {
151 return is_class ? chunk_manager_class() : chunk_manager_metadata();
152 }
153
154 static const MetaspaceTracer* tracer() { return _tracer; }
155 static void freeze() {
156 assert(DumpSharedSpaces, "sanity");
157 DEBUG_ONLY(_frozen = true;)
158 }
159 static void assert_not_frozen() {
160 assert(!_frozen, "sanity");
161 }
162 #ifdef _LP64
163 static void allocate_metaspace_compressed_klass_ptrs(char* requested_addr, address cds_base);
164 #endif
165
166 private:
167
168 #ifdef _LP64
169 static void set_narrow_klass_base_and_shift(address metaspace_base, address cds_base);
170
171 // Returns true if can use CDS with metaspace allocated as specified address.
172 static bool can_use_cds_with_metaspace_addr(char* metaspace_base, address cds_base);
173
174 static void initialize_class_space(ReservedSpace rs);
175 #endif
176
177 public:
178
179 static void ergo_initialize();
180 static void global_initialize();
181 static void post_initialize();
182
183 static void verify_global_initialization();
184
185 static size_t first_chunk_word_size() { return _first_chunk_word_size; }
186 static size_t first_class_chunk_word_size() { return _first_class_chunk_word_size; }
187
188 static size_t reserve_alignment() { return _reserve_alignment; }
189 static size_t reserve_alignment_words() { return _reserve_alignment / BytesPerWord; }
190 static size_t commit_alignment() { return _commit_alignment; }
191 static size_t commit_alignment_words() { return _commit_alignment / BytesPerWord; }
192
193 static MetaWord* allocate(ClassLoaderData* loader_data, size_t word_size,
194 MetaspaceObj::Type type, TRAPS);
195 void deallocate(MetaWord* ptr, size_t byte_size, bool is_class);
196
197 static bool contains(const void* ptr);
198 static bool contains_non_shared(const void* ptr);
199
200 // Free empty virtualspaces
201 static void purge(MetadataType mdtype);
202 static void purge();
203
204 static void report_metadata_oome(ClassLoaderData* loader_data, size_t word_size,
205 MetaspaceObj::Type type, MetadataType mdtype, TRAPS);
206
207 static const char* metadata_type_name(Metaspace::MetadataType mdtype);
208
209 static void print_compressed_class_space(outputStream* st, const char* requested_addr = 0) NOT_LP64({});
210
211 // Return TRUE only if UseCompressedClassPointers is True.
212 static bool using_class_space() {
213 return NOT_LP64(false) LP64_ONLY(UseCompressedClassPointers);
214 }
215
216 static bool is_class_space_allocation(MetadataType mdType) {
217 return mdType == ClassType && using_class_space();
218 }
219
220 };
221
222 // Manages the metaspace portion belonging to a class loader
223 class ClassLoaderMetaspace : public CHeapObj<mtClass> {
224 friend class CollectedHeap; // For expand_and_allocate()
225 friend class Metaspace;
226 friend class MetaspaceUtils;
227 friend class PrintCLDMetaspaceInfoClosure;
228 friend class VM_CollectForMetadataAllocation; // For expand_and_allocate()
229
230 private:
231
232 void initialize(Mutex* lock, Metaspace::MetaspaceType type);
233
234 // Initialize the first chunk for a Metaspace. Used for
235 // special cases such as the boot class loader, reflection
236 // class loader and anonymous class loader.
237 void initialize_first_chunk(Metaspace::MetaspaceType type, Metaspace::MetadataType mdtype);
238 Metachunk* get_initialization_chunk(Metaspace::MetaspaceType type, Metaspace::MetadataType mdtype);
239
240 SpaceManager* _vsm;
241 SpaceManager* vsm() const { return _vsm; }
242
243 SpaceManager* _class_vsm;
244 SpaceManager* class_vsm() const { return _class_vsm; }
245 SpaceManager* get_space_manager(Metaspace::MetadataType mdtype) {
246 assert(mdtype != Metaspace::MetadataTypeCount, "MetadaTypeCount can't be used as mdtype");
247 return mdtype == Metaspace::ClassType ? class_vsm() : vsm();
248 }
249
250 MetaWord* expand_and_allocate(size_t size, Metaspace::MetadataType mdtype);
251
252 size_t class_chunk_size(size_t word_size);
253
254 public:
255
256 ClassLoaderMetaspace(Mutex* lock, Metaspace::MetaspaceType type);
257 ~ClassLoaderMetaspace();
258
259 // Allocate space for metadata of type mdtype. This is space
260 // within a Metachunk and is used by
261 // allocate(ClassLoaderData*, size_t, bool, MetadataType, TRAPS)
262 MetaWord* allocate(size_t word_size, Metaspace::MetadataType mdtype);
263
264 size_t used_words_slow(Metaspace::MetadataType mdtype) const;
265 size_t free_words_slow(Metaspace::MetadataType mdtype) const;
266 size_t capacity_words_slow(Metaspace::MetadataType mdtype) const;
267
268 size_t used_bytes_slow(Metaspace::MetadataType mdtype) const;
269 size_t capacity_bytes_slow(Metaspace::MetadataType mdtype) const;
270
271 size_t allocated_blocks_bytes() const;
272 size_t allocated_chunks_bytes() const;
273
274 void deallocate(MetaWord* ptr, size_t byte_size, bool is_class);
275
276 void dump(outputStream* const out) const;
277
278 void print_on(outputStream* st) const;
279 // Debugging support
280 void verify();
281
282 }; // ClassLoaderMetaspace
283
284
285 class MetaspaceUtils : AllStatic {
286 static size_t free_chunks_total_words(Metaspace::MetadataType mdtype);
287
288 // These methods iterate over the classloader data graph
289 // for the given Metaspace type. These are slow.
290 static size_t used_bytes_slow(Metaspace::MetadataType mdtype);
291 static size_t free_bytes_slow(Metaspace::MetadataType mdtype);
292 static size_t capacity_bytes_slow(Metaspace::MetadataType mdtype);
293 static size_t capacity_bytes_slow();
294
295 // Running sum of space in all Metachunks that has been
296 // allocated to a Metaspace. This is used instead of
297 // iterating over all the classloaders. One for each
298 // type of Metadata
299 static size_t _capacity_words[Metaspace:: MetadataTypeCount];
300 // Running sum of space in all Metachunks that
301 // are being used for metadata. One for each
302 // type of Metadata.
303 static volatile size_t _used_words[Metaspace:: MetadataTypeCount];
|