@@ -1033,6 +1033,11 @@ bool FileMapInfo::init_from_file(int fd, bool is_static) {
1033
1033
return true ;
1034
1034
}
1035
1035
1036
+ void FileMapInfo::seek_to_position (size_t pos) {
1037
+ if (lseek (_fd, (long )pos, SEEK_SET) < 0 ) {
1038
+ fail_stop (" Unable to seek to position " SIZE_FORMAT, pos);
1039
+ }
1040
+ }
1036
1041
1037
1042
// Read the FileMapInfo information from the file.
1038
1043
bool FileMapInfo::open_for_read (const char * path) {
@@ -1088,14 +1093,26 @@ void FileMapInfo::open_for_write(const char* path) {
1088
1093
os::strerror (errno));
1089
1094
}
1090
1095
_fd = fd;
1091
- _file_offset = 0 ;
1092
1096
_file_open = true ;
1097
+
1098
+ // Seek past the header. We will write the header after all regions are written
1099
+ // and their CRCs computed.
1100
+ size_t header_bytes = header ()->header_size ();
1101
+ if (header ()->magic () == CDS_DYNAMIC_ARCHIVE_MAGIC) {
1102
+ header_bytes += strlen (Arguments::GetSharedArchivePath ()) + 1 ;
1103
+ }
1104
+
1105
+ header_bytes = align_up (header_bytes, os::vm_allocation_granularity ());
1106
+ _file_offset = header_bytes;
1107
+ seek_to_position (_file_offset);
1093
1108
}
1094
1109
1095
1110
1096
1111
// Write the header to the file, seek to the next allocation boundary.
1097
1112
1098
1113
void FileMapInfo::write_header () {
1114
+ _file_offset = 0 ;
1115
+ seek_to_position (_file_offset);
1099
1116
char * base_archive_name = NULL ;
1100
1117
if (header ()->magic () == CDS_DYNAMIC_ARCHIVE_MAGIC) {
1101
1118
base_archive_name = (char *)Arguments::GetSharedArchivePath ();
@@ -1108,7 +1125,6 @@ void FileMapInfo::write_header() {
1108
1125
if (base_archive_name != NULL ) {
1109
1126
write_bytes (base_archive_name, header ()->base_archive_name_size ());
1110
1127
}
1111
- align_file_position ();
1112
1128
}
1113
1129
1114
1130
void FileMapRegion::init (bool is_heap_region, char * base, size_t size, bool read_only,
@@ -1132,9 +1148,6 @@ void FileMapRegion::init(bool is_heap_region, char* base, size_t size, bool read
1132
1148
_crc = crc;
1133
1149
}
1134
1150
1135
- // Dump region to file.
1136
- // This is called twice for each region during archiving, once before
1137
- // the archive file is open (_file_open is false) and once after.
1138
1151
void FileMapInfo::write_region (int region, char * base, size_t size,
1139
1152
bool read_only, bool allow_exec) {
1140
1153
assert (DumpSharedSpaces || DynamicDumpSharedSpaces, " Dump time only" );
@@ -1146,14 +1159,10 @@ void FileMapInfo::write_region(int region, char* base, size_t size,
1146
1159
target_base = DynamicArchive::buffer_to_target (base);
1147
1160
}
1148
1161
1149
- if (_file_open) {
1150
- guarantee (si->file_offset () == _file_offset, " file offset mismatch." );
1151
- log_info (cds)(" Shared file region %d: " SIZE_FORMAT_HEX_W (08 )
1152
- " bytes, addr " INTPTR_FORMAT " file offset " SIZE_FORMAT_HEX_W (08 ),
1153
- region, size, p2i (target_base), _file_offset);
1154
- } else {
1155
- si->set_file_offset (_file_offset);
1156
- }
1162
+ si->set_file_offset (_file_offset);
1163
+ log_info (cds)(" Shared file region %d: " SIZE_FORMAT_HEX_W (08 )
1164
+ " bytes, addr " INTPTR_FORMAT " file offset " SIZE_FORMAT_HEX_W (08 ),
1165
+ region, size, p2i (target_base), _file_offset);
1157
1166
1158
1167
int crc = ClassLoader::crc32 (0 , base, (jint)size);
1159
1168
si->init (HeapShared::is_heap_region (region), target_base, size, read_only, allow_exec, crc);
@@ -1196,8 +1205,7 @@ void FileMapInfo::write_region(int region, char* base, size_t size,
1196
1205
// +-- gap
1197
1206
size_t FileMapInfo::write_archive_heap_regions (GrowableArray<MemRegion> *heap_mem,
1198
1207
GrowableArray<ArchiveHeapOopmapInfo> *oopmaps,
1199
- int first_region_id, int max_num_regions,
1200
- bool print_log) {
1208
+ int first_region_id, int max_num_regions) {
1201
1209
assert (max_num_regions <= 2 , " Only support maximum 2 memory regions" );
1202
1210
1203
1211
int arr_len = heap_mem == NULL ? 0 : heap_mem->length ();
@@ -1221,10 +1229,8 @@ size_t FileMapInfo::write_archive_heap_regions(GrowableArray<MemRegion> *heap_me
1221
1229
total_size += size;
1222
1230
}
1223
1231
1224
- if (print_log) {
1225
- log_info (cds)(" Archive heap region %d " INTPTR_FORMAT " - " INTPTR_FORMAT " = " SIZE_FORMAT_W (8 ) " bytes" ,
1226
- i, p2i (start), p2i (start + size), size);
1227
- }
1232
+ log_info (cds)(" Archive heap region %d " INTPTR_FORMAT " - " INTPTR_FORMAT " = " SIZE_FORMAT_W (8 ) " bytes" ,
1233
+ i, p2i (start), p2i (start + size), size);
1228
1234
write_region (i, start, size, false , false );
1229
1235
if (size > 0 ) {
1230
1236
space_at (i)->init_oopmap (oopmaps->at (arr_idx)._oopmap ,
@@ -1237,14 +1243,13 @@ size_t FileMapInfo::write_archive_heap_regions(GrowableArray<MemRegion> *heap_me
1237
1243
// Dump bytes to file -- at the current file position.
1238
1244
1239
1245
void FileMapInfo::write_bytes (const void * buffer, size_t nbytes) {
1240
- if (_file_open) {
1241
- size_t n = os::write (_fd, buffer, (unsigned int )nbytes);
1242
- if (n != nbytes) {
1243
- // If the shared archive is corrupted, close it and remove it.
1244
- close ();
1245
- remove (_full_path);
1246
- fail_stop (" Unable to write to shared archive file." );
1247
- }
1246
+ assert (_file_open, " must be" );
1247
+ size_t n = os::write (_fd, buffer, (unsigned int )nbytes);
1248
+ if (n != nbytes) {
1249
+ // If the shared archive is corrupted, close it and remove it.
1250
+ close ();
1251
+ remove (_full_path);
1252
+ fail_stop (" Unable to write to shared archive file." );
1248
1253
}
1249
1254
_file_offset += nbytes;
1250
1255
}
@@ -1257,20 +1262,17 @@ bool FileMapInfo::is_file_position_aligned() const {
1257
1262
// Align file position to an allocation unit boundary.
1258
1263
1259
1264
void FileMapInfo::align_file_position () {
1265
+ assert (_file_open, " must be" );
1260
1266
size_t new_file_offset = align_up (_file_offset,
1261
- os::vm_allocation_granularity ());
1267
+ os::vm_allocation_granularity ());
1262
1268
if (new_file_offset != _file_offset) {
1263
1269
_file_offset = new_file_offset;
1264
- if (_file_open) {
1265
- // Seek one byte back from the target and write a byte to insure
1266
- // that the written file is the correct length.
1267
- _file_offset -= 1 ;
1268
- if (lseek (_fd, (long )_file_offset, SEEK_SET) < 0 ) {
1269
- fail_stop (" Unable to seek." );
1270
- }
1271
- char zero = 0 ;
1272
- write_bytes (&zero, 1 );
1273
- }
1270
+ // Seek one byte back from the target and write a byte to insure
1271
+ // that the written file is the correct length.
1272
+ _file_offset -= 1 ;
1273
+ seek_to_position (_file_offset);
1274
+ char zero = 0 ;
1275
+ write_bytes (&zero, 1 );
1274
1276
}
1275
1277
}
1276
1278
0 commit comments