82 assert(_instance != NULL, "invariant"); |
81 assert(_instance != NULL, "invariant"); |
83 delete _instance; |
82 delete _instance; |
84 _instance = NULL; |
83 _instance = NULL; |
85 } |
84 } |
86 |
85 |
87 static const char vm_error_filename_fmt[] = "hs_err_pid%p.jfr"; |
|
88 static const char vm_oom_filename_fmt[] = "hs_oom_pid%p.jfr"; |
|
89 static const char vm_soe_filename_fmt[] = "hs_soe_pid%p.jfr"; |
|
90 static const char chunk_file_jfr_ext[] = ".jfr"; |
|
91 static const size_t iso8601_len = 19; // "YYYY-MM-DDTHH:MM:SS" |
|
92 |
|
93 static fio_fd open_exclusivly(const char* path) { |
|
94 return os::open(path, O_CREAT | O_WRONLY, S_IREAD | S_IWRITE); |
|
95 } |
|
96 |
|
97 static fio_fd open_existing(const char* path) { |
|
98 return os::open(path, O_RDWR, S_IREAD | S_IWRITE); |
|
99 } |
|
100 |
|
101 static int file_sort(const char** const file1, const char** file2) { |
|
102 assert(NULL != *file1 && NULL != *file2, "invariant"); |
|
103 int cmp = strncmp(*file1, *file2, iso8601_len); |
|
104 if (0 == cmp) { |
|
105 const char* const dot1 = strchr(*file1, '.'); |
|
106 assert(NULL != dot1, "invariant"); |
|
107 const char* const dot2 = strchr(*file2, '.'); |
|
108 assert(NULL != dot2, "invariant"); |
|
109 ptrdiff_t file1_len = dot1 - *file1; |
|
110 ptrdiff_t file2_len = dot2 - *file2; |
|
111 if (file1_len < file2_len) { |
|
112 return -1; |
|
113 } |
|
114 if (file1_len > file2_len) { |
|
115 return 1; |
|
116 } |
|
117 assert(file1_len == file2_len, "invariant"); |
|
118 cmp = strncmp(*file1, *file2, file1_len); |
|
119 } |
|
120 assert(cmp != 0, "invariant"); |
|
121 return cmp; |
|
122 } |
|
123 |
|
124 static void iso8601_to_date_time(char* iso8601_str) { |
|
125 assert(iso8601_str != NULL, "invariant"); |
|
126 assert(strlen(iso8601_str) == iso8601_len, "invariant"); |
|
127 // "YYYY-MM-DDTHH:MM:SS" |
|
128 for (size_t i = 0; i < iso8601_len; ++i) { |
|
129 switch(iso8601_str[i]) { |
|
130 case 'T' : |
|
131 case '-' : |
|
132 case ':' : |
|
133 iso8601_str[i] = '_'; |
|
134 break; |
|
135 } |
|
136 } |
|
137 // "YYYY_MM_DD_HH_MM_SS" |
|
138 } |
|
139 |
|
140 static void date_time(char* buffer, size_t buffer_len) { |
|
141 assert(buffer != NULL, "invariant"); |
|
142 assert(buffer_len >= iso8601_len, "buffer too small"); |
|
143 os::iso8601_time(buffer, buffer_len); |
|
144 assert(strlen(buffer) >= iso8601_len + 1, "invariant"); |
|
145 // "YYYY-MM-DDTHH:MM:SS" |
|
146 buffer[iso8601_len] = '\0'; |
|
147 iso8601_to_date_time(buffer); |
|
148 } |
|
149 |
|
150 static int64_t file_size(fio_fd fd) { |
|
151 assert(fd != invalid_fd, "invariant"); |
|
152 const int64_t current_offset = os::current_file_offset(fd); |
|
153 const int64_t size = os::lseek(fd, 0, SEEK_END); |
|
154 os::seek_to_file_offset(fd, current_offset); |
|
155 return size; |
|
156 } |
|
157 |
|
158 class RepositoryIterator : public StackObj { |
|
159 private: |
|
160 const char* const _repo; |
|
161 const size_t _repository_len; |
|
162 GrowableArray<const char*>* _files; |
|
163 const char* const fully_qualified(const char* entry) const; |
|
164 mutable int _iterator; |
|
165 |
|
166 public: |
|
167 RepositoryIterator(const char* repository, size_t repository_len); |
|
168 ~RepositoryIterator() {} |
|
169 debug_only(void print_repository_files() const;) |
|
170 const char* const filter(const char* entry) const; |
|
171 bool has_next() const; |
|
172 const char* const next() const; |
|
173 }; |
|
174 |
|
175 const char* const RepositoryIterator::fully_qualified(const char* entry) const { |
|
176 assert(NULL != entry, "invariant"); |
|
177 char* file_path_entry = NULL; |
|
178 // only use files that have content, not placeholders |
|
179 const char* const file_separator = os::file_separator(); |
|
180 if (NULL != file_separator) { |
|
181 const size_t entry_len = strlen(entry); |
|
182 const size_t file_separator_length = strlen(file_separator); |
|
183 const size_t file_path_entry_length = _repository_len + file_separator_length + entry_len; |
|
184 file_path_entry = NEW_RESOURCE_ARRAY_RETURN_NULL(char, file_path_entry_length + 1); |
|
185 if (NULL == file_path_entry) { |
|
186 return NULL; |
|
187 } |
|
188 int position = 0; |
|
189 position += jio_snprintf(&file_path_entry[position], _repository_len + 1, "%s", _repo); |
|
190 position += jio_snprintf(&file_path_entry[position], file_separator_length + 1, "%s", os::file_separator()); |
|
191 position += jio_snprintf(&file_path_entry[position], entry_len + 1, "%s", entry); |
|
192 file_path_entry[position] = '\0'; |
|
193 assert((size_t)position == file_path_entry_length, "invariant"); |
|
194 assert(strlen(file_path_entry) == (size_t)position, "invariant"); |
|
195 } |
|
196 return file_path_entry; |
|
197 } |
|
198 |
|
199 const char* const RepositoryIterator::filter(const char* entry) const { |
|
200 if (entry == NULL) { |
|
201 return NULL; |
|
202 } |
|
203 const size_t entry_len = strlen(entry); |
|
204 if (entry_len <= 2) { |
|
205 // for "." and ".." |
|
206 return NULL; |
|
207 } |
|
208 char* entry_name = NEW_RESOURCE_ARRAY_RETURN_NULL(char, entry_len + 1); |
|
209 if (entry_name == NULL) { |
|
210 return NULL; |
|
211 } |
|
212 strncpy(entry_name, entry, entry_len + 1); |
|
213 const char* const fully_qualified_path_entry = fully_qualified(entry_name); |
|
214 if (NULL == fully_qualified_path_entry) { |
|
215 return NULL; |
|
216 } |
|
217 const fio_fd entry_fd = open_existing(fully_qualified_path_entry); |
|
218 if (invalid_fd == entry_fd) { |
|
219 return NULL; |
|
220 } |
|
221 const int64_t entry_size = file_size(entry_fd); |
|
222 os::close(entry_fd); |
|
223 if (0 == entry_size) { |
|
224 return NULL; |
|
225 } |
|
226 return entry_name; |
|
227 } |
|
228 |
|
229 RepositoryIterator::RepositoryIterator(const char* repository, size_t repository_len) : |
|
230 _repo(repository), |
|
231 _repository_len(repository_len), |
|
232 _files(NULL), |
|
233 _iterator(0) { |
|
234 if (NULL != _repo) { |
|
235 assert(strlen(_repo) == _repository_len, "invariant"); |
|
236 _files = new GrowableArray<const char*>(10); |
|
237 DIR* dirp = os::opendir(_repo); |
|
238 if (dirp == NULL) { |
|
239 log_error(jfr, system)("Unable to open repository %s", _repo); |
|
240 return; |
|
241 } |
|
242 struct dirent* dentry; |
|
243 while ((dentry = os::readdir(dirp)) != NULL) { |
|
244 const char* const entry_path = filter(dentry->d_name); |
|
245 if (NULL != entry_path) { |
|
246 _files->append(entry_path); |
|
247 } |
|
248 } |
|
249 os::closedir(dirp); |
|
250 if (_files->length() > 1) { |
|
251 _files->sort(file_sort); |
|
252 } |
|
253 } |
|
254 } |
|
255 |
|
256 #ifdef ASSERT |
|
257 void RepositoryIterator::print_repository_files() const { |
|
258 while (has_next()) { |
|
259 log_error(jfr, system)( "%s", next()); |
|
260 } |
|
261 } |
|
262 #endif |
|
263 |
|
264 bool RepositoryIterator::has_next() const { |
|
265 return (_files != NULL && _iterator < _files->length()); |
|
266 } |
|
267 |
|
268 const char* const RepositoryIterator::next() const { |
|
269 return _iterator >= _files->length() ? NULL : fully_qualified(_files->at(_iterator++)); |
|
270 } |
|
271 |
|
272 static void write_emergency_file(fio_fd emergency_fd, const RepositoryIterator& iterator) { |
|
273 assert(emergency_fd != invalid_fd, "invariant"); |
|
274 const size_t size_of_file_copy_block = 1 * M; // 1 mb |
|
275 jbyte* const file_copy_block = NEW_RESOURCE_ARRAY_RETURN_NULL(jbyte, size_of_file_copy_block); |
|
276 if (file_copy_block == NULL) { |
|
277 return; |
|
278 } |
|
279 int64_t bytes_written_total = 0; |
|
280 while (iterator.has_next()) { |
|
281 fio_fd current_fd = invalid_fd; |
|
282 const char* const fqn = iterator.next(); |
|
283 if (fqn != NULL) { |
|
284 current_fd = open_existing(fqn); |
|
285 if (current_fd != invalid_fd) { |
|
286 const int64_t current_filesize = file_size(current_fd); |
|
287 assert(current_filesize > 0, "invariant"); |
|
288 int64_t bytes_read = 0; |
|
289 int64_t bytes_written = 0; |
|
290 while (bytes_read < current_filesize) { |
|
291 const ssize_t read_result = os::read_at(current_fd, file_copy_block, size_of_file_copy_block, bytes_read); |
|
292 if (-1 == read_result) { |
|
293 log_info(jfr) ( // For user, should not be "jfr, system" |
|
294 "Unable to recover JFR data"); |
|
295 break; |
|
296 } |
|
297 bytes_read += (int64_t)read_result; |
|
298 assert(bytes_read - bytes_written <= (int64_t)size_of_file_copy_block, "invariant"); |
|
299 bytes_written += (int64_t)os::write(emergency_fd, file_copy_block, bytes_read - bytes_written); |
|
300 assert(bytes_read == bytes_written, "invariant"); |
|
301 } |
|
302 os::close(current_fd); |
|
303 bytes_written_total += bytes_written; |
|
304 } |
|
305 } |
|
306 } |
|
307 } |
|
308 |
|
309 static const char* create_emergency_dump_path() { |
|
310 assert(JfrStream_lock->owned_by_self(), "invariant"); |
|
311 char* buffer = NEW_RESOURCE_ARRAY_RETURN_NULL(char, O_BUFLEN); |
|
312 if (NULL == buffer) { |
|
313 return NULL; |
|
314 } |
|
315 const char* const cwd = os::get_current_directory(buffer, O_BUFLEN); |
|
316 if (NULL == cwd) { |
|
317 return NULL; |
|
318 } |
|
319 size_t pos = strlen(cwd); |
|
320 const int fsep_len = jio_snprintf(&buffer[pos], O_BUFLEN - pos, "%s", os::file_separator()); |
|
321 const char* filename_fmt = NULL; |
|
322 // fetch specific error cause |
|
323 switch (JfrJavaSupport::cause()) { |
|
324 case JfrJavaSupport::OUT_OF_MEMORY: |
|
325 filename_fmt = vm_oom_filename_fmt; |
|
326 break; |
|
327 case JfrJavaSupport::STACK_OVERFLOW: |
|
328 filename_fmt = vm_soe_filename_fmt; |
|
329 break; |
|
330 default: |
|
331 filename_fmt = vm_error_filename_fmt; |
|
332 } |
|
333 char* emergency_dump_path = NULL; |
|
334 pos += fsep_len; |
|
335 if (Arguments::copy_expand_pid(filename_fmt, strlen(filename_fmt), &buffer[pos], O_BUFLEN - pos)) { |
|
336 const size_t emergency_filename_length = strlen(buffer); |
|
337 emergency_dump_path = NEW_RESOURCE_ARRAY_RETURN_NULL(char, emergency_filename_length + 1); |
|
338 if (NULL == emergency_dump_path) { |
|
339 return NULL; |
|
340 } |
|
341 strncpy(emergency_dump_path, buffer, emergency_filename_length + 1); |
|
342 } |
|
343 return emergency_dump_path; |
|
344 } |
|
345 |
|
346 // Caller needs ResourceMark |
|
347 static const char* create_emergency_chunk_path(const char* repository_base, size_t repository_len) { |
|
348 assert(repository_base != NULL, "invariant"); |
|
349 assert(JfrStream_lock->owned_by_self(), "invariant"); |
|
350 // date time |
|
351 char date_time_buffer[32] = {0}; |
|
352 date_time(date_time_buffer, sizeof(date_time_buffer)); |
|
353 size_t date_time_len = strlen(date_time_buffer); |
|
354 size_t chunkname_max_len = repository_len // repository_base |
|
355 + 1 // "/" |
|
356 + date_time_len // date_time |
|
357 + strlen(chunk_file_jfr_ext) // .jfr |
|
358 + 1; |
|
359 char* chunk_path = NEW_RESOURCE_ARRAY_RETURN_NULL(char, chunkname_max_len); |
|
360 if (chunk_path == NULL) { |
|
361 return NULL; |
|
362 } |
|
363 // append the individual substrings |
|
364 jio_snprintf(chunk_path, chunkname_max_len, "%s%s%s%s", repository_base, os::file_separator(), date_time_buffer, chunk_file_jfr_ext); |
|
365 return chunk_path; |
|
366 } |
|
367 |
|
368 static fio_fd emergency_dump_file() { |
|
369 assert(JfrStream_lock->owned_by_self(), "invariant"); |
|
370 ResourceMark rm; |
|
371 const char* const emergency_dump_path = create_emergency_dump_path(); |
|
372 if (emergency_dump_path == NULL) { |
|
373 return invalid_fd; |
|
374 } |
|
375 const fio_fd fd = open_exclusivly(emergency_dump_path); |
|
376 if (fd != invalid_fd) { |
|
377 log_info(jfr)( // For user, should not be "jfr, system" |
|
378 "Attempting to recover JFR data, emergency jfr file: %s", emergency_dump_path); |
|
379 } |
|
380 return fd; |
|
381 } |
|
382 |
|
383 static const char* emergency_path(const char* repository, size_t repository_len) { |
|
384 return repository == NULL ? create_emergency_dump_path() : create_emergency_chunk_path(repository, repository_len); |
|
385 } |
|
386 |
|
387 void JfrRepository::on_vm_error() { |
86 void JfrRepository::on_vm_error() { |
388 assert(!JfrStream_lock->owned_by_self(), "invariant"); |
87 assert(!JfrStream_lock->owned_by_self(), "invariant"); |
389 const char* path = _path; |
88 if (_path == NULL) { |
390 if (path == NULL) { |
|
391 // completed already |
89 // completed already |
392 return; |
90 return; |
393 } |
91 } |
394 ResourceMark rm; |
92 JfrEmergencyDump::on_vm_error(_path); |
395 MutexLockerEx stream_lock(JfrStream_lock, Mutex::_no_safepoint_check_flag); |
|
396 const fio_fd emergency_fd = emergency_dump_file(); |
|
397 if (emergency_fd != invalid_fd) { |
|
398 RepositoryIterator iterator(path, strlen(path)); |
|
399 write_emergency_file(emergency_fd, iterator); |
|
400 os::close(emergency_fd); |
|
401 } |
|
402 } |
93 } |
403 |
94 |
404 bool JfrRepository::set_path(const char* path) { |
95 bool JfrRepository::set_path(const char* path) { |
405 assert(path != NULL, "trying to set the repository path with a NULL string!"); |
96 assert(path != NULL, "trying to set the repository path with a NULL string!"); |
406 if (_path != NULL) { |
97 if (_path != NULL) { |