40 #include "memory/metaspaceShared.hpp"
41 #include "oops/objArrayOop.hpp"
42 #include "oops/oop.inline.hpp"
43 #include "runtime/os.hpp"
44 #include "runtime/signature.hpp"
45 #include "runtime/vmThread.hpp"
46 #include "runtime/vm_operations.hpp"
47 #include "utilities/defaultStream.hpp"
48 #include "utilities/hashtable.inline.hpp"
49
50 int MetaspaceShared::_max_alignment = 0;
51
52 ReservedSpace* MetaspaceShared::_shared_rs = NULL;
53
54 MetaspaceSharedStats MetaspaceShared::_stats;
55
56 bool MetaspaceShared::_link_classes_made_progress;
57 bool MetaspaceShared::_check_classes_made_progress;
58 bool MetaspaceShared::_has_error_classes;
59 bool MetaspaceShared::_archive_loading_failed = false;
60 SharedMiscRegion MetaspaceShared::_mc;
61 SharedMiscRegion MetaspaceShared::_md;
62
63 void SharedMiscRegion::initialize(ReservedSpace rs, size_t committed_byte_size, SharedSpaceType space_type) {
64 _vs.initialize(rs, committed_byte_size);
65 _alloc_top = _vs.low();
66 _space_type = space_type;
67 }
68
69 // NOT thread-safe, but this is called during dump time in single-threaded mode.
70 char* SharedMiscRegion::alloc(size_t num_bytes) {
71 assert(DumpSharedSpaces, "dump time only");
72 size_t alignment = sizeof(char*);
73 num_bytes = align_size_up(num_bytes, alignment);
74 _alloc_top = (char*)align_ptr_up(_alloc_top, alignment);
75 if (_alloc_top + num_bytes > _vs.high()) {
76 report_out_of_shared_space(_space_type);
77 }
78
79 char* p = _alloc_top;
110 soc->do_tag(sizeof(Method));
111 soc->do_tag(sizeof(ConstMethod));
112 soc->do_tag(arrayOopDesc::base_offset_in_bytes(T_BYTE));
113 soc->do_tag(sizeof(ConstantPool));
114 soc->do_tag(sizeof(ConstantPoolCache));
115 soc->do_tag(objArrayOopDesc::base_offset_in_bytes());
116 soc->do_tag(typeArrayOopDesc::base_offset_in_bytes(T_BYTE));
117 soc->do_tag(sizeof(Symbol));
118
119 // Dump/restore miscellaneous metadata.
120 Universe::serialize(soc, true);
121 soc->do_tag(--tag);
122
123 // Dump/restore references to commonly used names and signatures.
124 vmSymbols::serialize(soc);
125 soc->do_tag(--tag);
126
127 soc->do_tag(666);
128 }
129
130
131 // CDS code for dumping shared archive.
132
133 // Global object for holding classes that have been loaded. Since this
134 // is run at a safepoint just before exit, this is the entire set of classes.
135 static GrowableArray<Klass*>* _global_klass_objects;
136 static void collect_classes(Klass* k) {
137 _global_klass_objects->append_if_missing(k);
138 if (k->is_instance_klass()) {
139 // Add in the array classes too
140 InstanceKlass* ik = InstanceKlass::cast(k);
141 ik->array_klasses_do(collect_classes);
142 }
143 }
144
145 static void collect_classes2(Klass* k, ClassLoaderData* class_data) {
146 collect_classes(k);
147 }
148
149 static void remove_unshareable_in_classes() {
656 tty->print_cr(fmt_space, "md", md_bytes, md_t_perc, md_alloced, md_u_perc, p2i(md_low));
657 tty->print_cr(fmt_space, "mc", mc_bytes, mc_t_perc, mc_alloced, mc_u_perc, p2i(mc_low));
658 tty->print_cr(fmt_space, "st", ss_bytes, ss_t_perc, ss_bytes, 100.0, p2i(ss_low));
659 tty->print_cr("total : " SIZE_FORMAT_W(9) " [100.0%% of total] out of " SIZE_FORMAT_W(9) " bytes [%4.1f%% used]",
660 total_bytes, total_alloced, total_u_perc);
661
662 // Update the vtable pointers in all of the Klass objects in the
663 // heap. They should point to newly generated vtable.
664 patch_klass_vtables(vtbl_list, vtable);
665
666 // dunno what this is for.
667 char* saved_vtbl = (char*)os::malloc(vtbl_list_size * sizeof(void*), mtClass);
668 memmove(saved_vtbl, vtbl_list, vtbl_list_size * sizeof(void*));
669 memset(vtbl_list, 0, vtbl_list_size * sizeof(void*));
670
671 // Create and write the archive file that maps the shared spaces.
672
673 FileMapInfo* mapinfo = new FileMapInfo();
674 mapinfo->populate_header(MetaspaceShared::max_alignment());
675 mapinfo->set_misc_data_patching_start((char*)vtbl_list);
676
677 for (int pass=1; pass<=2; pass++) {
678 if (pass == 1) {
679 // The first pass doesn't actually write the data to disk. All it
680 // does is to update the fields in the mapinfo->_header.
681 } else {
682 // After the first pass, the contents of mapinfo->_header are finalized,
683 // so we can compute the header's CRC, and write the contents of the header
684 // and the regions into disk.
685 mapinfo->open_for_write();
686 mapinfo->set_header_crc(mapinfo->compute_header_crc());
687 }
688 mapinfo->write_header();
689 mapinfo->write_space(MetaspaceShared::ro, _loader_data->ro_metaspace(), true);
690 mapinfo->write_space(MetaspaceShared::rw, _loader_data->rw_metaspace(), false);
691 mapinfo->write_region(MetaspaceShared::md, _md_vs.low(),
692 pointer_delta(md_top, _md_vs.low(), sizeof(char)),
693 SharedMiscDataSize,
694 false, false);
695 mapinfo->write_region(MetaspaceShared::mc, _mc_vs.low(),
696 pointer_delta(mc_top, _mc_vs.low(), sizeof(char)),
697 SharedMiscCodeSize,
698 true, true);
699 mapinfo->write_string_regions(_string_regions);
700 }
701
702 mapinfo->close();
703
704 memmove(vtbl_list, saved_vtbl, vtbl_list_size * sizeof(void*));
705 os::free(saved_vtbl);
706
707 if (PrintSharedSpaces) {
708 DumpAllocClosure dac;
709 dac.iterate_metaspace(_loader_data->ro_metaspace(), DumpAllocClosure::RO);
710 dac.iterate_metaspace(_loader_data->rw_metaspace(), DumpAllocClosure::RW);
711
712 dac.dump_stats(int(ro_bytes), int(rw_bytes), int(md_bytes), int(mc_bytes));
713 }
714 #undef fmt_space
715 }
716
717
718 void MetaspaceShared::link_one_shared_class(Klass* obj, TRAPS) {
1030 #ifndef _WINDOWS
1031 // Release the entire mapped region
1032 shared_rs.release();
1033 #endif
1034 // If -Xshare:on is specified, print out the error message and exit VM,
1035 // otherwise, set UseSharedSpaces to false and continue.
1036 if (RequireSharedSpaces || PrintSharedArchiveAndExit) {
1037 vm_exit_during_initialization("Unable to use shared archive.", "Failed map_region for using -Xshare:on.");
1038 } else {
1039 FLAG_SET_DEFAULT(UseSharedSpaces, false);
1040 }
1041 return false;
1042 }
1043 }
1044
1045 // Read the miscellaneous data from the shared file, and
1046 // serialize it out to its various destinations.
1047
1048 void MetaspaceShared::initialize_shared_spaces() {
1049 FileMapInfo *mapinfo = FileMapInfo::current_info();
1050 char* buffer = mapinfo->misc_data_patching_start();
1051
1052 // Skip over (reserve space for) a list of addresses of C++ vtables
1053 // for Klass objects. They get filled in later.
1054
1055 void** vtbl_list = (void**)buffer;
1056 buffer += MetaspaceShared::vtbl_list_size * sizeof(void*);
1057 Universe::init_self_patching_vtbl_list(vtbl_list, vtbl_list_size);
1058
1059 // Skip over (reserve space for) dummy C++ vtables Klass objects.
1060 // They are used as is.
1061
1062 intptr_t vtable_size = *(intptr_t*)buffer;
1063 buffer += sizeof(intptr_t);
1064 buffer += vtable_size;
1065
1066 // Create the shared symbol table using the compact table at this spot in the
1067 // misc data space. (Todo: move this to read-only space. Currently
1068 // this is mapped copy-on-write but will never be written into).
1069
|
40 #include "memory/metaspaceShared.hpp"
41 #include "oops/objArrayOop.hpp"
42 #include "oops/oop.inline.hpp"
43 #include "runtime/os.hpp"
44 #include "runtime/signature.hpp"
45 #include "runtime/vmThread.hpp"
46 #include "runtime/vm_operations.hpp"
47 #include "utilities/defaultStream.hpp"
48 #include "utilities/hashtable.inline.hpp"
49
50 int MetaspaceShared::_max_alignment = 0;
51
52 ReservedSpace* MetaspaceShared::_shared_rs = NULL;
53
54 MetaspaceSharedStats MetaspaceShared::_stats;
55
56 bool MetaspaceShared::_link_classes_made_progress;
57 bool MetaspaceShared::_check_classes_made_progress;
58 bool MetaspaceShared::_has_error_classes;
59 bool MetaspaceShared::_archive_loading_failed = false;
60 address MetaspaceShared::_cds_i2i_entry_code_buffers = NULL;
61 size_t MetaspaceShared::_cds_i2i_entry_code_buffers_size = 0;
62 SharedMiscRegion MetaspaceShared::_mc;
63 SharedMiscRegion MetaspaceShared::_md;
64
65 void SharedMiscRegion::initialize(ReservedSpace rs, size_t committed_byte_size, SharedSpaceType space_type) {
66 _vs.initialize(rs, committed_byte_size);
67 _alloc_top = _vs.low();
68 _space_type = space_type;
69 }
70
71 // NOT thread-safe, but this is called during dump time in single-threaded mode.
72 char* SharedMiscRegion::alloc(size_t num_bytes) {
73 assert(DumpSharedSpaces, "dump time only");
74 size_t alignment = sizeof(char*);
75 num_bytes = align_size_up(num_bytes, alignment);
76 _alloc_top = (char*)align_ptr_up(_alloc_top, alignment);
77 if (_alloc_top + num_bytes > _vs.high()) {
78 report_out_of_shared_space(_space_type);
79 }
80
81 char* p = _alloc_top;
112 soc->do_tag(sizeof(Method));
113 soc->do_tag(sizeof(ConstMethod));
114 soc->do_tag(arrayOopDesc::base_offset_in_bytes(T_BYTE));
115 soc->do_tag(sizeof(ConstantPool));
116 soc->do_tag(sizeof(ConstantPoolCache));
117 soc->do_tag(objArrayOopDesc::base_offset_in_bytes());
118 soc->do_tag(typeArrayOopDesc::base_offset_in_bytes(T_BYTE));
119 soc->do_tag(sizeof(Symbol));
120
121 // Dump/restore miscellaneous metadata.
122 Universe::serialize(soc, true);
123 soc->do_tag(--tag);
124
125 // Dump/restore references to commonly used names and signatures.
126 vmSymbols::serialize(soc);
127 soc->do_tag(--tag);
128
129 soc->do_tag(666);
130 }
131
132 address MetaspaceShared::cds_i2i_entry_code_buffers(size_t total_size) {
133 if (DumpSharedSpaces) {
134 if (_cds_i2i_entry_code_buffers == NULL) {
135 _cds_i2i_entry_code_buffers = (address)misc_code_space_alloc(total_size);
136 _cds_i2i_entry_code_buffers_size = total_size;
137 }
138 } else if (UseSharedSpaces) {
139 assert(_cds_i2i_entry_code_buffers != NULL, "must already been initialized");
140 } else {
141 return NULL;
142 }
143
144 assert(_cds_i2i_entry_code_buffers_size == total_size, "must not change");
145 return _cds_i2i_entry_code_buffers;
146 }
147
148 // CDS code for dumping shared archive.
149
150 // Global object for holding classes that have been loaded. Since this
151 // is run at a safepoint just before exit, this is the entire set of classes.
152 static GrowableArray<Klass*>* _global_klass_objects;
153 static void collect_classes(Klass* k) {
154 _global_klass_objects->append_if_missing(k);
155 if (k->is_instance_klass()) {
156 // Add in the array classes too
157 InstanceKlass* ik = InstanceKlass::cast(k);
158 ik->array_klasses_do(collect_classes);
159 }
160 }
161
162 static void collect_classes2(Klass* k, ClassLoaderData* class_data) {
163 collect_classes(k);
164 }
165
166 static void remove_unshareable_in_classes() {
673 tty->print_cr(fmt_space, "md", md_bytes, md_t_perc, md_alloced, md_u_perc, p2i(md_low));
674 tty->print_cr(fmt_space, "mc", mc_bytes, mc_t_perc, mc_alloced, mc_u_perc, p2i(mc_low));
675 tty->print_cr(fmt_space, "st", ss_bytes, ss_t_perc, ss_bytes, 100.0, p2i(ss_low));
676 tty->print_cr("total : " SIZE_FORMAT_W(9) " [100.0%% of total] out of " SIZE_FORMAT_W(9) " bytes [%4.1f%% used]",
677 total_bytes, total_alloced, total_u_perc);
678
679 // Update the vtable pointers in all of the Klass objects in the
680 // heap. They should point to newly generated vtable.
681 patch_klass_vtables(vtbl_list, vtable);
682
683 // dunno what this is for.
684 char* saved_vtbl = (char*)os::malloc(vtbl_list_size * sizeof(void*), mtClass);
685 memmove(saved_vtbl, vtbl_list, vtbl_list_size * sizeof(void*));
686 memset(vtbl_list, 0, vtbl_list_size * sizeof(void*));
687
688 // Create and write the archive file that maps the shared spaces.
689
690 FileMapInfo* mapinfo = new FileMapInfo();
691 mapinfo->populate_header(MetaspaceShared::max_alignment());
692 mapinfo->set_misc_data_patching_start((char*)vtbl_list);
693 mapinfo->set_cds_i2i_entry_code_buffers(MetaspaceShared::cds_i2i_entry_code_buffers());
694 mapinfo->set_cds_i2i_entry_code_buffers_size(MetaspaceShared::cds_i2i_entry_code_buffers_size());
695
696 for (int pass=1; pass<=2; pass++) {
697 if (pass == 1) {
698 // The first pass doesn't actually write the data to disk. All it
699 // does is to update the fields in the mapinfo->_header.
700 } else {
701 // After the first pass, the contents of mapinfo->_header are finalized,
702 // so we can compute the header's CRC, and write the contents of the header
703 // and the regions into disk.
704 mapinfo->open_for_write();
705 mapinfo->set_header_crc(mapinfo->compute_header_crc());
706 }
707 mapinfo->write_header();
708 mapinfo->write_space(MetaspaceShared::ro, _loader_data->ro_metaspace(), true);
709 mapinfo->write_space(MetaspaceShared::rw, _loader_data->rw_metaspace(), false);
710 mapinfo->write_region(MetaspaceShared::md, _md_vs.low(),
711 pointer_delta(md_top, _md_vs.low(), sizeof(char)),
712 SharedMiscDataSize,
713 false, false);
714 mapinfo->write_region(MetaspaceShared::mc, _mc_vs.low(),
715 pointer_delta(mc_top, _mc_vs.low(), sizeof(char)),
716 SharedMiscCodeSize,
717 false, true);
718 mapinfo->write_string_regions(_string_regions);
719 }
720
721 mapinfo->close();
722
723 memmove(vtbl_list, saved_vtbl, vtbl_list_size * sizeof(void*));
724 os::free(saved_vtbl);
725
726 if (PrintSharedSpaces) {
727 DumpAllocClosure dac;
728 dac.iterate_metaspace(_loader_data->ro_metaspace(), DumpAllocClosure::RO);
729 dac.iterate_metaspace(_loader_data->rw_metaspace(), DumpAllocClosure::RW);
730
731 dac.dump_stats(int(ro_bytes), int(rw_bytes), int(md_bytes), int(mc_bytes));
732 }
733 #undef fmt_space
734 }
735
736
737 void MetaspaceShared::link_one_shared_class(Klass* obj, TRAPS) {
1049 #ifndef _WINDOWS
1050 // Release the entire mapped region
1051 shared_rs.release();
1052 #endif
1053 // If -Xshare:on is specified, print out the error message and exit VM,
1054 // otherwise, set UseSharedSpaces to false and continue.
1055 if (RequireSharedSpaces || PrintSharedArchiveAndExit) {
1056 vm_exit_during_initialization("Unable to use shared archive.", "Failed map_region for using -Xshare:on.");
1057 } else {
1058 FLAG_SET_DEFAULT(UseSharedSpaces, false);
1059 }
1060 return false;
1061 }
1062 }
1063
1064 // Read the miscellaneous data from the shared file, and
1065 // serialize it out to its various destinations.
1066
1067 void MetaspaceShared::initialize_shared_spaces() {
1068 FileMapInfo *mapinfo = FileMapInfo::current_info();
1069 _cds_i2i_entry_code_buffers = mapinfo->cds_i2i_entry_code_buffers();
1070 _cds_i2i_entry_code_buffers_size = mapinfo->cds_i2i_entry_code_buffers_size();
1071 char* buffer = mapinfo->misc_data_patching_start();
1072
1073 // Skip over (reserve space for) a list of addresses of C++ vtables
1074 // for Klass objects. They get filled in later.
1075
1076 void** vtbl_list = (void**)buffer;
1077 buffer += MetaspaceShared::vtbl_list_size * sizeof(void*);
1078 Universe::init_self_patching_vtbl_list(vtbl_list, vtbl_list_size);
1079
1080 // Skip over (reserve space for) dummy C++ vtables Klass objects.
1081 // They are used as is.
1082
1083 intptr_t vtable_size = *(intptr_t*)buffer;
1084 buffer += sizeof(intptr_t);
1085 buffer += vtable_size;
1086
1087 // Create the shared symbol table using the compact table at this spot in the
1088 // misc data space. (Todo: move this to read-only space. Currently
1089 // this is mapped copy-on-write but will never be written into).
1090
|