src/share/vm/memory/metaspaceShared.cpp

changeset 0
f90c822e73f8
child 6876
710a3c8b516e
equal deleted inserted replaced
-1:000000000000 0:f90c822e73f8
1 /*
2 * Copyright (c) 2012, 2014, 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 #include "classfile/dictionary.hpp"
27 #include "classfile/loaderConstraints.hpp"
28 #include "classfile/placeholders.hpp"
29 #include "classfile/symbolTable.hpp"
30 #include "classfile/systemDictionary.hpp"
31 #include "code/codeCache.hpp"
32 #include "memory/filemap.hpp"
33 #include "memory/gcLocker.hpp"
34 #include "memory/metaspace.hpp"
35 #include "memory/metaspaceShared.hpp"
36 #include "oops/objArrayOop.hpp"
37 #include "oops/oop.inline.hpp"
38 #include "runtime/signature.hpp"
39 #include "runtime/vm_operations.hpp"
40 #include "runtime/vmThread.hpp"
41 #include "utilities/hashtable.inline.hpp"
42
43 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
44
45 int MetaspaceShared::_max_alignment = 0;
46
47 ReservedSpace* MetaspaceShared::_shared_rs = NULL;
48
49 // Read/write a data stream for restoring/preserving metadata pointers and
50 // miscellaneous data from/to the shared archive file.
51
52 void MetaspaceShared::serialize(SerializeClosure* soc) {
53 int tag = 0;
54 soc->do_tag(--tag);
55
56 // Verify the sizes of various metadata in the system.
57 soc->do_tag(sizeof(Method));
58 soc->do_tag(sizeof(ConstMethod));
59 soc->do_tag(arrayOopDesc::base_offset_in_bytes(T_BYTE));
60 soc->do_tag(sizeof(ConstantPool));
61 soc->do_tag(sizeof(ConstantPoolCache));
62 soc->do_tag(objArrayOopDesc::base_offset_in_bytes());
63 soc->do_tag(typeArrayOopDesc::base_offset_in_bytes(T_BYTE));
64 soc->do_tag(sizeof(Symbol));
65
66 // Dump/restore miscellaneous metadata.
67 Universe::serialize(soc, true);
68 soc->do_tag(--tag);
69
70 // Dump/restore references to commonly used names and signatures.
71 vmSymbols::serialize(soc);
72 soc->do_tag(--tag);
73
74 soc->do_tag(666);
75 }
76
77
78 // CDS code for dumping shared archive.
79
80 // Global object for holding classes that have been loaded. Since this
81 // is run at a safepoint just before exit, this is the entire set of classes.
82 static GrowableArray<Klass*>* _global_klass_objects;
83 static void collect_classes(Klass* k) {
84 _global_klass_objects->append_if_missing(k);
85 if (k->oop_is_instance()) {
86 // Add in the array classes too
87 InstanceKlass* ik = InstanceKlass::cast(k);
88 ik->array_klasses_do(collect_classes);
89 }
90 }
91
92 static void remove_unshareable_in_classes() {
93 for (int i = 0; i < _global_klass_objects->length(); i++) {
94 Klass* k = _global_klass_objects->at(i);
95 k->remove_unshareable_info();
96 }
97 }
98
99 // Walk all methods in the class list and assign a fingerprint.
100 // so that this part of the ConstMethod* is read only.
101 static void calculate_fingerprints() {
102 for (int i = 0; i < _global_klass_objects->length(); i++) {
103 Klass* k = _global_klass_objects->at(i);
104 if (k->oop_is_instance()) {
105 InstanceKlass* ik = InstanceKlass::cast(k);
106 for (int i = 0; i < ik->methods()->length(); i++) {
107 Method* m = ik->methods()->at(i);
108 Fingerprinter fp(m);
109 // The side effect of this call sets method's fingerprint field.
110 fp.fingerprint();
111 }
112 }
113 }
114 }
115
116 // Patch C++ vtable pointer in metadata.
117
118 // Klass and other metadata objects contain references to c++ vtables in the
119 // JVM library.
120 // Fix them to point to our constructed vtables. However, don't iterate
121 // across the space while doing this, as that causes the vtables to be
122 // patched, undoing our useful work. Instead, iterate to make a list,
123 // then use the list to do the fixing.
124 //
125 // Our constructed vtables:
126 // Dump time:
127 // 1. init_self_patching_vtbl_list: table of pointers to current virtual method addrs
128 // 2. generate_vtable_methods: create jump table, appended to above vtbl_list
129 // 3. patch_klass_vtables: for Klass list, patch the vtable entry in klass and
130 // associated metadata to point to jump table rather than to current vtbl
131 // Table layout: NOTE FIXED SIZE
132 // 1. vtbl pointers
133 // 2. #Klass X #virtual methods per Klass
134 // 1 entry for each, in the order:
135 // Klass1:method1 entry, Klass1:method2 entry, ... Klass1:method<num_virtuals> entry
136 // Klass2:method1 entry, Klass2:method2 entry, ... Klass2:method<num_virtuals> entry
137 // ...
138 // Klass<vtbl_list_size>:method1 entry, Klass<vtbl_list_size>:method2 entry,
139 // ... Klass<vtbl_list_size>:method<num_virtuals> entry
140 // Sample entry: (Sparc):
141 // save(sp, -256, sp)
142 // ba,pt common_code
143 // mov XXX, %L0 %L0 gets: Klass index <<8 + method index (note: max method index 255)
144 //
145 // Restore time:
146 // 1. initialize_shared_space: reserve space for table
147 // 2. init_self_patching_vtbl_list: update pointers to NEW virtual method addrs in text
148 //
149 // Execution time:
150 // First virtual method call for any object of these metadata types:
151 // 1. object->klass
152 // 2. vtable entry for that klass points to the jump table entries
153 // 3. branches to common_code with %O0/klass, %L0: Klass index <<8 + method index
154 // 4. common_code:
155 // Get address of new vtbl pointer for this Klass from updated table
156 // Update new vtbl pointer in the Klass: future virtual calls go direct
157 // Jump to method, using new vtbl pointer and method index
158
159
160 static void* find_matching_vtbl_ptr(void** vtbl_list, void* new_vtable_start, void* obj) {
161 void* old_vtbl_ptr = *(void**)obj;
162 for (int i = 0; i < MetaspaceShared::vtbl_list_size; i++) {
163 if (vtbl_list[i] == old_vtbl_ptr) {
164 return (void**)new_vtable_start + i * MetaspaceShared::num_virtuals;
165 }
166 }
167 ShouldNotReachHere();
168 return NULL;
169 }
170
171 // Assumes the vtable is in first slot in object.
172 static void patch_klass_vtables(void** vtbl_list, void* new_vtable_start) {
173 int n = _global_klass_objects->length();
174 for (int i = 0; i < n; i++) {
175 Klass* obj = _global_klass_objects->at(i);
176 // Note oop_is_instance() is a virtual call. After patching vtables
177 // all virtual calls on the dummy vtables will restore the original!
178 if (obj->oop_is_instance()) {
179 InstanceKlass* ik = InstanceKlass::cast(obj);
180 *(void**)ik = find_matching_vtbl_ptr(vtbl_list, new_vtable_start, ik);
181 ConstantPool* cp = ik->constants();
182 *(void**)cp = find_matching_vtbl_ptr(vtbl_list, new_vtable_start, cp);
183 for (int j = 0; j < ik->methods()->length(); j++) {
184 Method* m = ik->methods()->at(j);
185 *(void**)m = find_matching_vtbl_ptr(vtbl_list, new_vtable_start, m);
186 }
187 } else {
188 // Array klasses
189 Klass* k = obj;
190 *(void**)k = find_matching_vtbl_ptr(vtbl_list, new_vtable_start, k);
191 }
192 }
193 }
194
195 // Closure for serializing initialization data out to a data area to be
196 // written to the shared file.
197
198 class WriteClosure : public SerializeClosure {
199 private:
200 intptr_t* top;
201 char* end;
202
203 inline void check_space() {
204 if ((char*)top + sizeof(intptr_t) > end) {
205 report_out_of_shared_space(SharedMiscData);
206 }
207 }
208
209 public:
210 WriteClosure(char* md_top, char* md_end) {
211 top = (intptr_t*)md_top;
212 end = md_end;
213 }
214
215 char* get_top() { return (char*)top; }
216
217 void do_ptr(void** p) {
218 check_space();
219 *top = (intptr_t)*p;
220 ++top;
221 }
222
223 void do_tag(int tag) {
224 check_space();
225 *top = (intptr_t)tag;
226 ++top;
227 }
228
229 void do_region(u_char* start, size_t size) {
230 if ((char*)top + size > end) {
231 report_out_of_shared_space(SharedMiscData);
232 }
233 assert((intptr_t)start % sizeof(intptr_t) == 0, "bad alignment");
234 assert(size % sizeof(intptr_t) == 0, "bad size");
235 do_tag((int)size);
236 while (size > 0) {
237 *top = *(intptr_t*)start;
238 ++top;
239 start += sizeof(intptr_t);
240 size -= sizeof(intptr_t);
241 }
242 }
243
244 bool reading() const { return false; }
245 };
246
247 // This is for dumping detailed statistics for the allocations
248 // in the shared spaces.
249 class DumpAllocClosure : public Metaspace::AllocRecordClosure {
250 public:
251
252 // Here's poor man's enum inheritance
253 #define SHAREDSPACE_OBJ_TYPES_DO(f) \
254 METASPACE_OBJ_TYPES_DO(f) \
255 f(SymbolHashentry) \
256 f(SymbolBuckets) \
257 f(Other)
258
259 #define SHAREDSPACE_OBJ_TYPE_DECLARE(name) name ## Type,
260 #define SHAREDSPACE_OBJ_TYPE_NAME_CASE(name) case name ## Type: return #name;
261
262 enum Type {
263 // Types are MetaspaceObj::ClassType, MetaspaceObj::SymbolType, etc
264 SHAREDSPACE_OBJ_TYPES_DO(SHAREDSPACE_OBJ_TYPE_DECLARE)
265 _number_of_types
266 };
267
268 static const char * type_name(Type type) {
269 switch(type) {
270 SHAREDSPACE_OBJ_TYPES_DO(SHAREDSPACE_OBJ_TYPE_NAME_CASE)
271 default:
272 ShouldNotReachHere();
273 return NULL;
274 }
275 }
276
277 public:
278 enum {
279 RO = 0,
280 RW = 1
281 };
282
283 int _counts[2][_number_of_types];
284 int _bytes [2][_number_of_types];
285 int _which;
286
287 DumpAllocClosure() {
288 memset(_counts, 0, sizeof(_counts));
289 memset(_bytes, 0, sizeof(_bytes));
290 };
291
292 void iterate_metaspace(Metaspace* space, int which) {
293 assert(which == RO || which == RW, "sanity");
294 _which = which;
295 space->iterate(this);
296 }
297
298 virtual void doit(address ptr, MetaspaceObj::Type type, int byte_size) {
299 assert(int(type) >= 0 && type < MetaspaceObj::_number_of_types, "sanity");
300 _counts[_which][type] ++;
301 _bytes [_which][type] += byte_size;
302 }
303
304 void dump_stats(int ro_all, int rw_all, int md_all, int mc_all);
305 };
306
307 void DumpAllocClosure::dump_stats(int ro_all, int rw_all, int md_all, int mc_all) {
308 rw_all += (md_all + mc_all); // md and mc are all mapped Read/Write
309 int other_bytes = md_all + mc_all;
310
311 // Calculate size of data that was not allocated by Metaspace::allocate()
312 int symbol_count = _counts[RO][MetaspaceObj::SymbolType];
313 int symhash_bytes = symbol_count * sizeof (HashtableEntry<Symbol*, mtSymbol>);
314 int symbuck_count = SymbolTable::the_table()->table_size();
315 int symbuck_bytes = symbuck_count * sizeof(HashtableBucket<mtSymbol>);
316
317 _counts[RW][SymbolHashentryType] = symbol_count;
318 _bytes [RW][SymbolHashentryType] = symhash_bytes;
319 other_bytes -= symhash_bytes;
320
321 _counts[RW][SymbolBucketsType] = symbuck_count;
322 _bytes [RW][SymbolBucketsType] = symbuck_bytes;
323 other_bytes -= symbuck_bytes;
324
325 // TODO: count things like dictionary, vtable, etc
326 _bytes[RW][OtherType] = other_bytes;
327
328 // prevent divide-by-zero
329 if (ro_all < 1) {
330 ro_all = 1;
331 }
332 if (rw_all < 1) {
333 rw_all = 1;
334 }
335
336 int all_ro_count = 0;
337 int all_ro_bytes = 0;
338 int all_rw_count = 0;
339 int all_rw_bytes = 0;
340
341 // To make fmt_stats be a syntactic constant (for format warnings), use #define.
342 #define fmt_stats "%-20s: %8d %10d %5.1f | %8d %10d %5.1f | %8d %10d %5.1f"
343 const char *sep = "--------------------+---------------------------+---------------------------+--------------------------";
344 const char *hdr = " ro_cnt ro_bytes % | rw_cnt rw_bytes % | all_cnt all_bytes %";
345
346 tty->print_cr("Detailed metadata info (rw includes md and mc):");
347 tty->print_cr("%s", hdr);
348 tty->print_cr("%s", sep);
349 for (int type = 0; type < int(_number_of_types); type ++) {
350 const char *name = type_name((Type)type);
351 int ro_count = _counts[RO][type];
352 int ro_bytes = _bytes [RO][type];
353 int rw_count = _counts[RW][type];
354 int rw_bytes = _bytes [RW][type];
355 int count = ro_count + rw_count;
356 int bytes = ro_bytes + rw_bytes;
357
358 double ro_perc = 100.0 * double(ro_bytes) / double(ro_all);
359 double rw_perc = 100.0 * double(rw_bytes) / double(rw_all);
360 double perc = 100.0 * double(bytes) / double(ro_all + rw_all);
361
362 tty->print_cr(fmt_stats, name,
363 ro_count, ro_bytes, ro_perc,
364 rw_count, rw_bytes, rw_perc,
365 count, bytes, perc);
366
367 all_ro_count += ro_count;
368 all_ro_bytes += ro_bytes;
369 all_rw_count += rw_count;
370 all_rw_bytes += rw_bytes;
371 }
372
373 int all_count = all_ro_count + all_rw_count;
374 int all_bytes = all_ro_bytes + all_rw_bytes;
375
376 double all_ro_perc = 100.0 * double(all_ro_bytes) / double(ro_all);
377 double all_rw_perc = 100.0 * double(all_rw_bytes) / double(rw_all);
378 double all_perc = 100.0 * double(all_bytes) / double(ro_all + rw_all);
379
380 tty->print_cr("%s", sep);
381 tty->print_cr(fmt_stats, "Total",
382 all_ro_count, all_ro_bytes, all_ro_perc,
383 all_rw_count, all_rw_bytes, all_rw_perc,
384 all_count, all_bytes, all_perc);
385
386 assert(all_ro_bytes == ro_all, "everything should have been counted");
387 assert(all_rw_bytes == rw_all, "everything should have been counted");
388 #undef fmt_stats
389 }
390
391 // Populate the shared space.
392
393 class VM_PopulateDumpSharedSpace: public VM_Operation {
394 private:
395 ClassLoaderData* _loader_data;
396 GrowableArray<Klass*> *_class_promote_order;
397 VirtualSpace _md_vs;
398 VirtualSpace _mc_vs;
399
400 public:
401 VM_PopulateDumpSharedSpace(ClassLoaderData* loader_data,
402 GrowableArray<Klass*> *class_promote_order) :
403 _loader_data(loader_data) {
404
405 // Split up and initialize the misc code and data spaces
406 ReservedSpace* shared_rs = MetaspaceShared::shared_rs();
407 int metadata_size = SharedReadOnlySize+SharedReadWriteSize;
408 ReservedSpace shared_ro_rw = shared_rs->first_part(metadata_size);
409 ReservedSpace misc_section = shared_rs->last_part(metadata_size);
410
411 // Now split into misc sections.
412 ReservedSpace md_rs = misc_section.first_part(SharedMiscDataSize);
413 ReservedSpace mc_rs = misc_section.last_part(SharedMiscDataSize);
414 _md_vs.initialize(md_rs, SharedMiscDataSize);
415 _mc_vs.initialize(mc_rs, SharedMiscCodeSize);
416 _class_promote_order = class_promote_order;
417 }
418
419 VMOp_Type type() const { return VMOp_PopulateDumpSharedSpace; }
420 void doit(); // outline because gdb sucks
421 }; // class VM_PopulateDumpSharedSpace
422
423
424 void VM_PopulateDumpSharedSpace::doit() {
425 Thread* THREAD = VMThread::vm_thread();
426 NOT_PRODUCT(SystemDictionary::verify();)
427 // The following guarantee is meant to ensure that no loader constraints
428 // exist yet, since the constraints table is not shared. This becomes
429 // more important now that we don't re-initialize vtables/itables for
430 // shared classes at runtime, where constraints were previously created.
431 guarantee(SystemDictionary::constraints()->number_of_entries() == 0,
432 "loader constraints are not saved");
433 guarantee(SystemDictionary::placeholders()->number_of_entries() == 0,
434 "placeholders are not saved");
435 // Revisit and implement this if we prelink method handle call sites:
436 guarantee(SystemDictionary::invoke_method_table() == NULL ||
437 SystemDictionary::invoke_method_table()->number_of_entries() == 0,
438 "invoke method table is not saved");
439
440 // At this point, many classes have been loaded.
441 // Gather systemDictionary classes in a global array and do everything to
442 // that so we don't have to walk the SystemDictionary again.
443 _global_klass_objects = new GrowableArray<Klass*>(1000);
444 Universe::basic_type_classes_do(collect_classes);
445 SystemDictionary::classes_do(collect_classes);
446
447 tty->print_cr("Number of classes %d", _global_klass_objects->length());
448
449 // Update all the fingerprints in the shared methods.
450 tty->print("Calculating fingerprints ... ");
451 calculate_fingerprints();
452 tty->print_cr("done. ");
453
454 // Remove all references outside the metadata
455 tty->print("Removing unshareable information ... ");
456 remove_unshareable_in_classes();
457 tty->print_cr("done. ");
458
459 // Set up the share data and shared code segments.
460 char* md_low = _md_vs.low();
461 char* md_top = md_low;
462 char* md_end = _md_vs.high();
463 char* mc_low = _mc_vs.low();
464 char* mc_top = mc_low;
465 char* mc_end = _mc_vs.high();
466
467 // Reserve space for the list of Klass*s whose vtables are used
468 // for patching others as needed.
469
470 void** vtbl_list = (void**)md_top;
471 int vtbl_list_size = MetaspaceShared::vtbl_list_size;
472 Universe::init_self_patching_vtbl_list(vtbl_list, vtbl_list_size);
473
474 md_top += vtbl_list_size * sizeof(void*);
475 void* vtable = md_top;
476
477 // Reserve space for a new dummy vtable for klass objects in the
478 // heap. Generate self-patching vtable entries.
479
480 MetaspaceShared::generate_vtable_methods(vtbl_list, &vtable,
481 &md_top, md_end,
482 &mc_top, mc_end);
483
484 // Reorder the system dictionary. (Moving the symbols affects
485 // how the hash table indices are calculated.)
486 // Not doing this either.
487
488 SystemDictionary::reorder_dictionary();
489
490 NOT_PRODUCT(SystemDictionary::verify();)
491
492 // Copy the the symbol table, and the system dictionary to the shared
493 // space in usable form. Copy the hastable
494 // buckets first [read-write], then copy the linked lists of entries
495 // [read-only].
496
497 SymbolTable::reverse(md_top);
498 NOT_PRODUCT(SymbolTable::verify());
499 SymbolTable::copy_buckets(&md_top, md_end);
500
501 SystemDictionary::reverse();
502 SystemDictionary::copy_buckets(&md_top, md_end);
503
504 ClassLoader::verify();
505 ClassLoader::copy_package_info_buckets(&md_top, md_end);
506 ClassLoader::verify();
507
508 SymbolTable::copy_table(&md_top, md_end);
509 SystemDictionary::copy_table(&md_top, md_end);
510 ClassLoader::verify();
511 ClassLoader::copy_package_info_table(&md_top, md_end);
512 ClassLoader::verify();
513
514 // Write the other data to the output array.
515 WriteClosure wc(md_top, md_end);
516 MetaspaceShared::serialize(&wc);
517 md_top = wc.get_top();
518
519 // Print shared spaces all the time
520 // To make fmt_space be a syntactic constant (for format warnings), use #define.
521 #define fmt_space "%s space: %9d [ %4.1f%% of total] out of %9d bytes [%4.1f%% used] at " PTR_FORMAT
522 Metaspace* ro_space = _loader_data->ro_metaspace();
523 Metaspace* rw_space = _loader_data->rw_metaspace();
524
525 // Allocated size of each space (may not be all occupied)
526 const size_t ro_alloced = ro_space->capacity_bytes_slow(Metaspace::NonClassType);
527 const size_t rw_alloced = rw_space->capacity_bytes_slow(Metaspace::NonClassType);
528 const size_t md_alloced = md_end-md_low;
529 const size_t mc_alloced = mc_end-mc_low;
530 const size_t total_alloced = ro_alloced + rw_alloced + md_alloced + mc_alloced;
531
532 // Occupied size of each space.
533 const size_t ro_bytes = ro_space->used_bytes_slow(Metaspace::NonClassType);
534 const size_t rw_bytes = rw_space->used_bytes_slow(Metaspace::NonClassType);
535 const size_t md_bytes = size_t(md_top - md_low);
536 const size_t mc_bytes = size_t(mc_top - mc_low);
537
538 // Percent of total size
539 const size_t total_bytes = ro_bytes + rw_bytes + md_bytes + mc_bytes;
540 const double ro_t_perc = ro_bytes / double(total_bytes) * 100.0;
541 const double rw_t_perc = rw_bytes / double(total_bytes) * 100.0;
542 const double md_t_perc = md_bytes / double(total_bytes) * 100.0;
543 const double mc_t_perc = mc_bytes / double(total_bytes) * 100.0;
544
545 // Percent of fullness of each space
546 const double ro_u_perc = ro_bytes / double(ro_alloced) * 100.0;
547 const double rw_u_perc = rw_bytes / double(rw_alloced) * 100.0;
548 const double md_u_perc = md_bytes / double(md_alloced) * 100.0;
549 const double mc_u_perc = mc_bytes / double(mc_alloced) * 100.0;
550 const double total_u_perc = total_bytes / double(total_alloced) * 100.0;
551
552 tty->print_cr(fmt_space, "ro", ro_bytes, ro_t_perc, ro_alloced, ro_u_perc, ro_space->bottom());
553 tty->print_cr(fmt_space, "rw", rw_bytes, rw_t_perc, rw_alloced, rw_u_perc, rw_space->bottom());
554 tty->print_cr(fmt_space, "md", md_bytes, md_t_perc, md_alloced, md_u_perc, md_low);
555 tty->print_cr(fmt_space, "mc", mc_bytes, mc_t_perc, mc_alloced, mc_u_perc, mc_low);
556 tty->print_cr("total : %9d [100.0%% of total] out of %9d bytes [%4.1f%% used]",
557 total_bytes, total_alloced, total_u_perc);
558
559 // Update the vtable pointers in all of the Klass objects in the
560 // heap. They should point to newly generated vtable.
561 patch_klass_vtables(vtbl_list, vtable);
562
563 // dunno what this is for.
564 char* saved_vtbl = (char*)os::malloc(vtbl_list_size * sizeof(void*), mtClass);
565 memmove(saved_vtbl, vtbl_list, vtbl_list_size * sizeof(void*));
566 memset(vtbl_list, 0, vtbl_list_size * sizeof(void*));
567
568 // Create and write the archive file that maps the shared spaces.
569
570 FileMapInfo* mapinfo = new FileMapInfo();
571 mapinfo->populate_header(MetaspaceShared::max_alignment());
572
573 // Pass 1 - update file offsets in header.
574 mapinfo->write_header();
575 mapinfo->write_space(MetaspaceShared::ro, _loader_data->ro_metaspace(), true);
576 mapinfo->write_space(MetaspaceShared::rw, _loader_data->rw_metaspace(), false);
577 mapinfo->write_region(MetaspaceShared::md, _md_vs.low(),
578 pointer_delta(md_top, _md_vs.low(), sizeof(char)),
579 SharedMiscDataSize,
580 false, false);
581 mapinfo->write_region(MetaspaceShared::mc, _mc_vs.low(),
582 pointer_delta(mc_top, _mc_vs.low(), sizeof(char)),
583 SharedMiscCodeSize,
584 true, true);
585
586 // Pass 2 - write data.
587 mapinfo->open_for_write();
588 mapinfo->set_header_crc(mapinfo->compute_header_crc());
589 mapinfo->write_header();
590 mapinfo->write_space(MetaspaceShared::ro, _loader_data->ro_metaspace(), true);
591 mapinfo->write_space(MetaspaceShared::rw, _loader_data->rw_metaspace(), false);
592 mapinfo->write_region(MetaspaceShared::md, _md_vs.low(),
593 pointer_delta(md_top, _md_vs.low(), sizeof(char)),
594 SharedMiscDataSize,
595 false, false);
596 mapinfo->write_region(MetaspaceShared::mc, _mc_vs.low(),
597 pointer_delta(mc_top, _mc_vs.low(), sizeof(char)),
598 SharedMiscCodeSize,
599 true, true);
600 mapinfo->close();
601
602 memmove(vtbl_list, saved_vtbl, vtbl_list_size * sizeof(void*));
603
604 if (PrintSharedSpaces) {
605 DumpAllocClosure dac;
606 dac.iterate_metaspace(_loader_data->ro_metaspace(), DumpAllocClosure::RO);
607 dac.iterate_metaspace(_loader_data->rw_metaspace(), DumpAllocClosure::RW);
608
609 dac.dump_stats(int(ro_bytes), int(rw_bytes), int(md_bytes), int(mc_bytes));
610 }
611 #undef fmt_space
612 }
613
614 static void link_shared_classes(Klass* obj, TRAPS) {
615 Klass* k = obj;
616 if (k->oop_is_instance()) {
617 InstanceKlass* ik = (InstanceKlass*) k;
618 // Link the class to cause the bytecodes to be rewritten and the
619 // cpcache to be created.
620 if (ik->init_state() < InstanceKlass::linked) {
621 ik->link_class(THREAD);
622 guarantee(!HAS_PENDING_EXCEPTION, "exception in class rewriting");
623 }
624 }
625 }
626
627
628 // Support for a simple checksum of the contents of the class list
629 // file to prevent trivial tampering. The algorithm matches that in
630 // the MakeClassList program used by the J2SE build process.
631 #define JSUM_SEED ((jlong)CONST64(0xcafebabebabecafe))
632 static jlong
633 jsum(jlong start, const char *buf, const int len)
634 {
635 jlong h = start;
636 char *p = (char *)buf, *e = p + len;
637 while (p < e) {
638 char c = *p++;
639 if (c <= ' ') {
640 /* Skip spaces and control characters */
641 continue;
642 }
643 h = 31 * h + c;
644 }
645 return h;
646 }
647
648 // Preload classes from a list, populate the shared spaces and dump to a
649 // file.
650 void MetaspaceShared::preload_and_dump(TRAPS) {
651 TraceTime timer("Dump Shared Spaces", TraceStartupTime);
652 ResourceMark rm;
653
654 // Preload classes to be shared.
655 // Should use some os:: method rather than fopen() here. aB.
656 // Construct the path to the class list (in jre/lib)
657 // Walk up two directories from the location of the VM and
658 // optionally tack on "lib" (depending on platform)
659 char class_list_path[JVM_MAXPATHLEN];
660 os::jvm_path(class_list_path, sizeof(class_list_path));
661 for (int i = 0; i < 3; i++) {
662 char *end = strrchr(class_list_path, *os::file_separator());
663 if (end != NULL) *end = '\0';
664 }
665 int class_list_path_len = (int)strlen(class_list_path);
666 if (class_list_path_len >= 3) {
667 if (strcmp(class_list_path + class_list_path_len - 3, "lib") != 0) {
668 strcat(class_list_path, os::file_separator());
669 strcat(class_list_path, "lib");
670 }
671 }
672 strcat(class_list_path, os::file_separator());
673 strcat(class_list_path, "classlist");
674
675 FILE* file = fopen(class_list_path, "r");
676 if (file != NULL) {
677 jlong computed_jsum = JSUM_SEED;
678 jlong file_jsum = 0;
679
680 char class_name[256];
681 int class_count = 0;
682 GrowableArray<Klass*>* class_promote_order = new GrowableArray<Klass*>();
683
684 // sun.io.Converters
685 static const char obj_array_sig[] = "[[Ljava/lang/Object;";
686 SymbolTable::new_permanent_symbol(obj_array_sig, THREAD);
687
688 // java.util.HashMap
689 static const char map_entry_array_sig[] = "[Ljava/util/Map$Entry;";
690 SymbolTable::new_permanent_symbol(map_entry_array_sig, THREAD);
691
692 tty->print("Loading classes to share ... ");
693 while ((fgets(class_name, sizeof class_name, file)) != NULL) {
694 if (*class_name == '#') {
695 jint fsh, fsl;
696 if (sscanf(class_name, "# %8x%8x\n", &fsh, &fsl) == 2) {
697 file_jsum = ((jlong)(fsh) << 32) | (fsl & 0xffffffff);
698 }
699
700 continue;
701 }
702 // Remove trailing newline
703 size_t name_len = strlen(class_name);
704 class_name[name_len-1] = '\0';
705
706 computed_jsum = jsum(computed_jsum, class_name, (const int)name_len - 1);
707
708 // Got a class name - load it.
709 TempNewSymbol class_name_symbol = SymbolTable::new_permanent_symbol(class_name, THREAD);
710 guarantee(!HAS_PENDING_EXCEPTION, "Exception creating a symbol.");
711 Klass* klass = SystemDictionary::resolve_or_null(class_name_symbol,
712 THREAD);
713 guarantee(!HAS_PENDING_EXCEPTION, "Exception resolving a class.");
714 if (klass != NULL) {
715 if (PrintSharedSpaces && Verbose && WizardMode) {
716 tty->print_cr("Shared spaces preloaded: %s", class_name);
717 }
718
719
720 InstanceKlass* ik = InstanceKlass::cast(klass);
721
722 // Should be class load order as per -XX:+TraceClassLoadingPreorder
723 class_promote_order->append(ik);
724
725 // Link the class to cause the bytecodes to be rewritten and the
726 // cpcache to be created. The linking is done as soon as classes
727 // are loaded in order that the related data structures (klass and
728 // cpCache) are located together.
729
730 if (ik->init_state() < InstanceKlass::linked) {
731 ik->link_class(THREAD);
732 guarantee(!(HAS_PENDING_EXCEPTION), "exception in class rewriting");
733 }
734
735 // TODO: Resolve klasses in constant pool
736 ik->constants()->resolve_class_constants(THREAD);
737
738 class_count++;
739 } else {
740 if (PrintSharedSpaces && Verbose && WizardMode) {
741 tty->cr();
742 tty->print_cr(" Preload failed: %s", class_name);
743 }
744 }
745 file_jsum = 0; // Checksum must be on last line of file
746 }
747 if (computed_jsum != file_jsum) {
748 tty->cr();
749 tty->print_cr("Preload failed: checksum of class list was incorrect.");
750 exit(1);
751 }
752
753 tty->print_cr("done. ");
754
755 if (PrintSharedSpaces) {
756 tty->print_cr("Shared spaces: preloaded %d classes", class_count);
757 }
758
759 // Rewrite and unlink classes.
760 tty->print("Rewriting and linking classes ... ");
761
762 // Link any classes which got missed. (It's not quite clear why
763 // they got missed.) This iteration would be unsafe if we weren't
764 // single-threaded at this point; however we can't do it on the VM
765 // thread because it requires object allocation.
766 SystemDictionary::classes_do(link_shared_classes, CATCH);
767 tty->print_cr("done. ");
768
769 // Create and dump the shared spaces. Everything so far is loaded
770 // with the null class loader.
771 ClassLoaderData* loader_data = ClassLoaderData::the_null_class_loader_data();
772 VM_PopulateDumpSharedSpace op(loader_data, class_promote_order);
773 VMThread::execute(&op);
774
775 } else {
776 char errmsg[JVM_MAXPATHLEN];
777 os::lasterror(errmsg, JVM_MAXPATHLEN);
778 tty->print_cr("Loading classlist failed: %s", errmsg);
779 exit(1);
780 }
781
782 // Since various initialization steps have been undone by this process,
783 // it is not reasonable to continue running a java process.
784 exit(0);
785 }
786
787
788 // Closure for serializing initialization data in from a data area
789 // (ptr_array) read from the shared file.
790
791 class ReadClosure : public SerializeClosure {
792 private:
793 intptr_t** _ptr_array;
794
795 inline intptr_t nextPtr() {
796 return *(*_ptr_array)++;
797 }
798
799 public:
800 ReadClosure(intptr_t** ptr_array) { _ptr_array = ptr_array; }
801
802 void do_ptr(void** p) {
803 assert(*p == NULL, "initializing previous initialized pointer.");
804 intptr_t obj = nextPtr();
805 assert((intptr_t)obj >= 0 || (intptr_t)obj < -100,
806 "hit tag while initializing ptrs.");
807 *p = (void*)obj;
808 }
809
810 void do_tag(int tag) {
811 int old_tag;
812 old_tag = (int)(intptr_t)nextPtr();
813 // do_int(&old_tag);
814 assert(tag == old_tag, "old tag doesn't match");
815 FileMapInfo::assert_mark(tag == old_tag);
816 }
817
818 void do_region(u_char* start, size_t size) {
819 assert((intptr_t)start % sizeof(intptr_t) == 0, "bad alignment");
820 assert(size % sizeof(intptr_t) == 0, "bad size");
821 do_tag((int)size);
822 while (size > 0) {
823 *(intptr_t*)start = nextPtr();
824 start += sizeof(intptr_t);
825 size -= sizeof(intptr_t);
826 }
827 }
828
829 bool reading() const { return true; }
830 };
831
832 // Return true if given address is in the mapped shared space.
833 bool MetaspaceShared::is_in_shared_space(const void* p) {
834 return UseSharedSpaces && FileMapInfo::current_info()->is_in_shared_space(p);
835 }
836
837 void MetaspaceShared::print_shared_spaces() {
838 if (UseSharedSpaces) {
839 FileMapInfo::current_info()->print_shared_spaces();
840 }
841 }
842
843
844 // Map shared spaces at requested addresses and return if succeeded.
845 // Need to keep the bounds of the ro and rw space for the Metaspace::contains
846 // call, or is_in_shared_space.
847 bool MetaspaceShared::map_shared_spaces(FileMapInfo* mapinfo) {
848 size_t image_alignment = mapinfo->alignment();
849
850 #ifndef _WINDOWS
851 // Map in the shared memory and then map the regions on top of it.
852 // On Windows, don't map the memory here because it will cause the
853 // mappings of the regions to fail.
854 ReservedSpace shared_rs = mapinfo->reserve_shared_memory();
855 if (!shared_rs.is_reserved()) return false;
856 #endif
857
858 assert(!DumpSharedSpaces, "Should not be called with DumpSharedSpaces");
859
860 char* _ro_base = NULL;
861 char* _rw_base = NULL;
862 char* _md_base = NULL;
863 char* _mc_base = NULL;
864
865 // Map each shared region
866 if ((_ro_base = mapinfo->map_region(ro)) != NULL &&
867 mapinfo->verify_region_checksum(ro) &&
868 (_rw_base = mapinfo->map_region(rw)) != NULL &&
869 mapinfo->verify_region_checksum(rw) &&
870 (_md_base = mapinfo->map_region(md)) != NULL &&
871 mapinfo->verify_region_checksum(md) &&
872 (_mc_base = mapinfo->map_region(mc)) != NULL &&
873 mapinfo->verify_region_checksum(mc) &&
874 (image_alignment == (size_t)max_alignment())) {
875 // Success (no need to do anything)
876 return true;
877 } else {
878 // If there was a failure in mapping any of the spaces, unmap the ones
879 // that succeeded
880 if (_ro_base != NULL) mapinfo->unmap_region(ro);
881 if (_rw_base != NULL) mapinfo->unmap_region(rw);
882 if (_md_base != NULL) mapinfo->unmap_region(md);
883 if (_mc_base != NULL) mapinfo->unmap_region(mc);
884 #ifndef _WINDOWS
885 // Release the entire mapped region
886 shared_rs.release();
887 #endif
888 // If -Xshare:on is specified, print out the error message and exit VM,
889 // otherwise, set UseSharedSpaces to false and continue.
890 if (RequireSharedSpaces) {
891 vm_exit_during_initialization("Unable to use shared archive.", NULL);
892 } else {
893 FLAG_SET_DEFAULT(UseSharedSpaces, false);
894 }
895 return false;
896 }
897 }
898
899 // Read the miscellaneous data from the shared file, and
900 // serialize it out to its various destinations.
901
902 void MetaspaceShared::initialize_shared_spaces() {
903 FileMapInfo *mapinfo = FileMapInfo::current_info();
904
905 char* buffer = mapinfo->region_base(md);
906
907 // Skip over (reserve space for) a list of addresses of C++ vtables
908 // for Klass objects. They get filled in later.
909
910 void** vtbl_list = (void**)buffer;
911 buffer += MetaspaceShared::vtbl_list_size * sizeof(void*);
912 Universe::init_self_patching_vtbl_list(vtbl_list, vtbl_list_size);
913
914 // Skip over (reserve space for) dummy C++ vtables Klass objects.
915 // They are used as is.
916
917 intptr_t vtable_size = *(intptr_t*)buffer;
918 buffer += sizeof(intptr_t);
919 buffer += vtable_size;
920
921 // Create the symbol table using the bucket array at this spot in the
922 // misc data space. Since the symbol table is often modified, this
923 // region (of mapped pages) will be copy-on-write.
924
925 int symbolTableLen = *(intptr_t*)buffer;
926 buffer += sizeof(intptr_t);
927 int number_of_entries = *(intptr_t*)buffer;
928 buffer += sizeof(intptr_t);
929 SymbolTable::create_table((HashtableBucket<mtSymbol>*)buffer, symbolTableLen,
930 number_of_entries);
931 buffer += symbolTableLen;
932
933 // Create the shared dictionary using the bucket array at this spot in
934 // the misc data space. Since the shared dictionary table is never
935 // modified, this region (of mapped pages) will be (effectively, if
936 // not explicitly) read-only.
937
938 int sharedDictionaryLen = *(intptr_t*)buffer;
939 buffer += sizeof(intptr_t);
940 number_of_entries = *(intptr_t*)buffer;
941 buffer += sizeof(intptr_t);
942 SystemDictionary::set_shared_dictionary((HashtableBucket<mtClass>*)buffer,
943 sharedDictionaryLen,
944 number_of_entries);
945 buffer += sharedDictionaryLen;
946
947 // Create the package info table using the bucket array at this spot in
948 // the misc data space. Since the package info table is never
949 // modified, this region (of mapped pages) will be (effectively, if
950 // not explicitly) read-only.
951
952 int pkgInfoLen = *(intptr_t*)buffer;
953 buffer += sizeof(intptr_t);
954 number_of_entries = *(intptr_t*)buffer;
955 buffer += sizeof(intptr_t);
956 ClassLoader::create_package_info_table((HashtableBucket<mtClass>*)buffer, pkgInfoLen,
957 number_of_entries);
958 buffer += pkgInfoLen;
959 ClassLoader::verify();
960
961 // The following data in the shared misc data region are the linked
962 // list elements (HashtableEntry objects) for the symbol table, string
963 // table, and shared dictionary. The heap objects refered to by the
964 // symbol table, string table, and shared dictionary are permanent and
965 // unmovable. Since new entries added to the string and symbol tables
966 // are always added at the beginning of the linked lists, THESE LINKED
967 // LIST ELEMENTS ARE READ-ONLY.
968
969 int len = *(intptr_t*)buffer; // skip over symbol table entries
970 buffer += sizeof(intptr_t);
971 buffer += len;
972
973 len = *(intptr_t*)buffer; // skip over shared dictionary entries
974 buffer += sizeof(intptr_t);
975 buffer += len;
976
977 len = *(intptr_t*)buffer; // skip over package info table entries
978 buffer += sizeof(intptr_t);
979 buffer += len;
980
981 len = *(intptr_t*)buffer; // skip over package info table char[] arrays.
982 buffer += sizeof(intptr_t);
983 buffer += len;
984
985 intptr_t* array = (intptr_t*)buffer;
986 ReadClosure rc(&array);
987 serialize(&rc);
988
989 // Close the mapinfo file
990 mapinfo->close();
991 }
992
993 // JVM/TI RedefineClasses() support:
994 bool MetaspaceShared::remap_shared_readonly_as_readwrite() {
995 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
996
997 if (UseSharedSpaces) {
998 // remap the shared readonly space to shared readwrite, private
999 FileMapInfo* mapinfo = FileMapInfo::current_info();
1000 if (!mapinfo->remap_shared_readonly_as_readwrite()) {
1001 return false;
1002 }
1003 }
1004 return true;
1005 }

mercurial