Tue, 16 Jun 2020 11:03:04 +0800
8217647: JFR: recordings on 32-bit systems unreadable
Reviewed-by: egahlin
Contributed-by: boris.ulasevich@bell-sw.com, markus.gronlund@oracle.com
apetushkov@9858 | 1 | /* |
mikael@9880 | 2 | * Copyright (c) 2011, 2019, Oracle and/or its affiliates. All rights reserved. |
apetushkov@9858 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
apetushkov@9858 | 4 | * |
apetushkov@9858 | 5 | * This code is free software; you can redistribute it and/or modify it |
apetushkov@9858 | 6 | * under the terms of the GNU General Public License version 2 only, as |
apetushkov@9858 | 7 | * published by the Free Software Foundation. |
apetushkov@9858 | 8 | * |
apetushkov@9858 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
apetushkov@9858 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
apetushkov@9858 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
apetushkov@9858 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
apetushkov@9858 | 13 | * accompanied this code). |
apetushkov@9858 | 14 | * |
apetushkov@9858 | 15 | * You should have received a copy of the GNU General Public License version |
apetushkov@9858 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
apetushkov@9858 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
apetushkov@9858 | 18 | * |
apetushkov@9858 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
apetushkov@9858 | 20 | * or visit www.oracle.com if you need additional information or have any |
apetushkov@9858 | 21 | * questions. |
apetushkov@9858 | 22 | * |
apetushkov@9858 | 23 | */ |
apetushkov@9858 | 24 | |
apetushkov@9858 | 25 | #include "precompiled.hpp" |
apetushkov@9858 | 26 | #include "jfr/jfr.hpp" |
apetushkov@9858 | 27 | #include "jfr/jni/jfrJavaSupport.hpp" |
apetushkov@9858 | 28 | #include "jfr/recorder/jfrRecorder.hpp" |
apetushkov@9858 | 29 | #include "jfr/recorder/repository/jfrChunkState.hpp" |
apetushkov@9858 | 30 | #include "jfr/recorder/repository/jfrChunkWriter.hpp" |
apetushkov@9858 | 31 | #include "jfr/recorder/repository/jfrRepository.hpp" |
apetushkov@9858 | 32 | #include "jfr/recorder/service/jfrPostBox.hpp" |
apetushkov@9858 | 33 | #include "memory/resourceArea.hpp" |
apetushkov@9858 | 34 | #include "runtime/mutex.hpp" |
apetushkov@9858 | 35 | #include "runtime/arguments.hpp" |
apetushkov@9858 | 36 | #include "runtime/os.hpp" |
apetushkov@9858 | 37 | #include "runtime/thread.inline.hpp" |
apetushkov@9858 | 38 | |
apetushkov@9858 | 39 | static JfrRepository* _instance = NULL; |
apetushkov@9858 | 40 | |
apetushkov@9858 | 41 | JfrRepository& JfrRepository::instance() { |
apetushkov@9858 | 42 | return *_instance; |
apetushkov@9858 | 43 | } |
apetushkov@9858 | 44 | |
apetushkov@9858 | 45 | static JfrChunkWriter* _chunkwriter = NULL; |
apetushkov@9858 | 46 | |
apetushkov@9858 | 47 | static bool initialize_chunkwriter() { |
apetushkov@9858 | 48 | assert(_chunkwriter == NULL, "invariant"); |
apetushkov@9858 | 49 | _chunkwriter = new JfrChunkWriter(); |
apetushkov@9858 | 50 | return _chunkwriter != NULL && _chunkwriter->initialize(); |
apetushkov@9858 | 51 | } |
apetushkov@9858 | 52 | |
apetushkov@9858 | 53 | JfrChunkWriter& JfrRepository::chunkwriter() { |
apetushkov@9858 | 54 | return *_chunkwriter; |
apetushkov@9858 | 55 | } |
apetushkov@9858 | 56 | |
apetushkov@9858 | 57 | JfrRepository::JfrRepository(JfrPostBox& post_box) : _path(NULL), _post_box(post_box) {} |
apetushkov@9858 | 58 | |
apetushkov@9858 | 59 | bool JfrRepository::initialize() { |
apetushkov@9858 | 60 | return initialize_chunkwriter(); |
apetushkov@9858 | 61 | } |
apetushkov@9858 | 62 | |
apetushkov@9858 | 63 | JfrRepository::~JfrRepository() { |
apetushkov@9858 | 64 | if (_path != NULL) { |
apetushkov@9858 | 65 | JfrCHeapObj::free(_path, strlen(_path) + 1); |
apetushkov@9858 | 66 | _path = NULL; |
apetushkov@9858 | 67 | } |
apetushkov@9858 | 68 | |
apetushkov@9858 | 69 | if (_chunkwriter != NULL) { |
apetushkov@9858 | 70 | delete _chunkwriter; |
apetushkov@9858 | 71 | _chunkwriter = NULL; |
apetushkov@9858 | 72 | } |
apetushkov@9858 | 73 | } |
apetushkov@9858 | 74 | |
apetushkov@9858 | 75 | JfrRepository* JfrRepository::create(JfrPostBox& post_box) { |
apetushkov@9858 | 76 | assert(_instance == NULL, "invariant"); |
apetushkov@9858 | 77 | _instance = new JfrRepository(post_box); |
apetushkov@9858 | 78 | return _instance; |
apetushkov@9858 | 79 | } |
apetushkov@9858 | 80 | |
apetushkov@9858 | 81 | void JfrRepository::destroy() { |
apetushkov@9858 | 82 | assert(_instance != NULL, "invariant"); |
apetushkov@9858 | 83 | delete _instance; |
apetushkov@9858 | 84 | _instance = NULL; |
apetushkov@9858 | 85 | } |
apetushkov@9858 | 86 | |
apetushkov@9858 | 87 | static const char vm_error_filename_fmt[] = "hs_err_pid%p.jfr"; |
apetushkov@9858 | 88 | static const char vm_oom_filename_fmt[] = "hs_oom_pid%p.jfr"; |
apetushkov@9858 | 89 | static const char vm_soe_filename_fmt[] = "hs_soe_pid%p.jfr"; |
apetushkov@9858 | 90 | static const char chunk_file_jfr_ext[] = ".jfr"; |
apetushkov@9858 | 91 | static const size_t iso8601_len = 19; // "YYYY-MM-DDTHH:MM:SS" |
apetushkov@9858 | 92 | |
apetushkov@9858 | 93 | static fio_fd open_exclusivly(const char* path) { |
apetushkov@9858 | 94 | return os::open(path, O_CREAT | O_WRONLY, S_IREAD | S_IWRITE); |
apetushkov@9858 | 95 | } |
apetushkov@9858 | 96 | |
apetushkov@9858 | 97 | static fio_fd open_existing(const char* path) { |
apetushkov@9858 | 98 | return os::open(path, O_RDWR, S_IREAD | S_IWRITE); |
apetushkov@9858 | 99 | } |
apetushkov@9858 | 100 | |
apetushkov@9858 | 101 | static int file_sort(const char** const file1, const char** file2) { |
apetushkov@9858 | 102 | assert(NULL != *file1 && NULL != *file2, "invariant"); |
apetushkov@9858 | 103 | int cmp = strncmp(*file1, *file2, iso8601_len); |
apetushkov@9858 | 104 | if (0 == cmp) { |
apetushkov@9858 | 105 | const char* const dot1 = strchr(*file1, '.'); |
apetushkov@9858 | 106 | assert(NULL != dot1, "invariant"); |
apetushkov@9858 | 107 | const char* const dot2 = strchr(*file2, '.'); |
apetushkov@9858 | 108 | assert(NULL != dot2, "invariant"); |
apetushkov@9858 | 109 | ptrdiff_t file1_len = dot1 - *file1; |
apetushkov@9858 | 110 | ptrdiff_t file2_len = dot2 - *file2; |
apetushkov@9858 | 111 | if (file1_len < file2_len) { |
apetushkov@9858 | 112 | return -1; |
apetushkov@9858 | 113 | } |
apetushkov@9858 | 114 | if (file1_len > file2_len) { |
apetushkov@9858 | 115 | return 1; |
apetushkov@9858 | 116 | } |
apetushkov@9858 | 117 | assert(file1_len == file2_len, "invariant"); |
apetushkov@9858 | 118 | cmp = strncmp(*file1, *file2, file1_len); |
apetushkov@9858 | 119 | } |
apetushkov@9858 | 120 | assert(cmp != 0, "invariant"); |
apetushkov@9858 | 121 | return cmp; |
apetushkov@9858 | 122 | } |
apetushkov@9858 | 123 | |
apetushkov@9858 | 124 | static void iso8601_to_date_time(char* iso8601_str) { |
apetushkov@9858 | 125 | assert(iso8601_str != NULL, "invariant"); |
apetushkov@9858 | 126 | assert(strlen(iso8601_str) == iso8601_len, "invariant"); |
apetushkov@9858 | 127 | // "YYYY-MM-DDTHH:MM:SS" |
apetushkov@9858 | 128 | for (size_t i = 0; i < iso8601_len; ++i) { |
apetushkov@9858 | 129 | switch(iso8601_str[i]) { |
apetushkov@9858 | 130 | case 'T' : |
apetushkov@9858 | 131 | case '-' : |
apetushkov@9858 | 132 | case ':' : |
apetushkov@9858 | 133 | iso8601_str[i] = '_'; |
apetushkov@9858 | 134 | break; |
apetushkov@9858 | 135 | } |
apetushkov@9858 | 136 | } |
apetushkov@9858 | 137 | // "YYYY_MM_DD_HH_MM_SS" |
apetushkov@9858 | 138 | } |
apetushkov@9858 | 139 | |
apetushkov@9858 | 140 | static void date_time(char* buffer, size_t buffer_len) { |
apetushkov@9858 | 141 | assert(buffer != NULL, "invariant"); |
apetushkov@9858 | 142 | assert(buffer_len >= iso8601_len, "buffer too small"); |
apetushkov@9858 | 143 | os::iso8601_time(buffer, buffer_len); |
apetushkov@9858 | 144 | assert(strlen(buffer) >= iso8601_len + 1, "invariant"); |
apetushkov@9858 | 145 | // "YYYY-MM-DDTHH:MM:SS" |
apetushkov@9858 | 146 | buffer[iso8601_len] = '\0'; |
apetushkov@9858 | 147 | iso8601_to_date_time(buffer); |
apetushkov@9858 | 148 | } |
apetushkov@9858 | 149 | |
bulasevich@9947 | 150 | static int64_t file_size(fio_fd fd) { |
apetushkov@9858 | 151 | assert(fd != invalid_fd, "invariant"); |
bulasevich@9947 | 152 | const int64_t current_offset = os::current_file_offset(fd); |
bulasevich@9947 | 153 | const int64_t size = os::lseek(fd, 0, SEEK_END); |
apetushkov@9858 | 154 | os::seek_to_file_offset(fd, current_offset); |
apetushkov@9858 | 155 | return size; |
apetushkov@9858 | 156 | } |
apetushkov@9858 | 157 | |
apetushkov@9858 | 158 | class RepositoryIterator : public StackObj { |
apetushkov@9858 | 159 | private: |
apetushkov@9858 | 160 | const char* const _repo; |
apetushkov@9858 | 161 | const size_t _repository_len; |
apetushkov@9858 | 162 | GrowableArray<const char*>* _files; |
apetushkov@9858 | 163 | const char* const fully_qualified(const char* entry) const; |
apetushkov@9858 | 164 | mutable int _iterator; |
apetushkov@9858 | 165 | |
apetushkov@9858 | 166 | public: |
apetushkov@9858 | 167 | RepositoryIterator(const char* repository, size_t repository_len); |
apetushkov@9858 | 168 | ~RepositoryIterator() {} |
apetushkov@9858 | 169 | debug_only(void print_repository_files() const;) |
apetushkov@9858 | 170 | const char* const filter(const char* entry) const; |
apetushkov@9858 | 171 | bool has_next() const; |
apetushkov@9858 | 172 | const char* const next() const; |
apetushkov@9858 | 173 | }; |
apetushkov@9858 | 174 | |
apetushkov@9858 | 175 | const char* const RepositoryIterator::fully_qualified(const char* entry) const { |
apetushkov@9858 | 176 | assert(NULL != entry, "invariant"); |
apetushkov@9858 | 177 | char* file_path_entry = NULL; |
apetushkov@9858 | 178 | // only use files that have content, not placeholders |
apetushkov@9858 | 179 | const char* const file_separator = os::file_separator(); |
apetushkov@9858 | 180 | if (NULL != file_separator) { |
apetushkov@9858 | 181 | const size_t entry_len = strlen(entry); |
apetushkov@9858 | 182 | const size_t file_separator_length = strlen(file_separator); |
apetushkov@9858 | 183 | const size_t file_path_entry_length = _repository_len + file_separator_length + entry_len; |
apetushkov@9858 | 184 | file_path_entry = NEW_RESOURCE_ARRAY_RETURN_NULL(char, file_path_entry_length + 1); |
apetushkov@9858 | 185 | if (NULL == file_path_entry) { |
apetushkov@9858 | 186 | return NULL; |
apetushkov@9858 | 187 | } |
apetushkov@9858 | 188 | int position = 0; |
apetushkov@9858 | 189 | position += jio_snprintf(&file_path_entry[position], _repository_len + 1, "%s", _repo); |
apetushkov@9858 | 190 | position += jio_snprintf(&file_path_entry[position], file_separator_length + 1, "%s", os::file_separator()); |
apetushkov@9858 | 191 | position += jio_snprintf(&file_path_entry[position], entry_len + 1, "%s", entry); |
apetushkov@9858 | 192 | file_path_entry[position] = '\0'; |
apetushkov@9858 | 193 | assert((size_t)position == file_path_entry_length, "invariant"); |
apetushkov@9858 | 194 | assert(strlen(file_path_entry) == (size_t)position, "invariant"); |
apetushkov@9858 | 195 | } |
apetushkov@9858 | 196 | return file_path_entry; |
apetushkov@9858 | 197 | } |
apetushkov@9858 | 198 | |
apetushkov@9858 | 199 | const char* const RepositoryIterator::filter(const char* entry) const { |
apetushkov@9858 | 200 | if (entry == NULL) { |
apetushkov@9858 | 201 | return NULL; |
apetushkov@9858 | 202 | } |
apetushkov@9858 | 203 | const size_t entry_len = strlen(entry); |
apetushkov@9858 | 204 | if (entry_len <= 2) { |
apetushkov@9858 | 205 | // for "." and ".." |
apetushkov@9858 | 206 | return NULL; |
apetushkov@9858 | 207 | } |
apetushkov@9858 | 208 | char* entry_name = NEW_RESOURCE_ARRAY_RETURN_NULL(char, entry_len + 1); |
apetushkov@9858 | 209 | if (entry_name == NULL) { |
apetushkov@9858 | 210 | return NULL; |
apetushkov@9858 | 211 | } |
mikael@9880 | 212 | strncpy(entry_name, entry, entry_len + 1); |
apetushkov@9858 | 213 | const char* const fully_qualified_path_entry = fully_qualified(entry_name); |
apetushkov@9858 | 214 | if (NULL == fully_qualified_path_entry) { |
apetushkov@9858 | 215 | return NULL; |
apetushkov@9858 | 216 | } |
apetushkov@9858 | 217 | const fio_fd entry_fd = open_existing(fully_qualified_path_entry); |
apetushkov@9858 | 218 | if (invalid_fd == entry_fd) { |
apetushkov@9858 | 219 | return NULL; |
apetushkov@9858 | 220 | } |
bulasevich@9947 | 221 | const int64_t entry_size = file_size(entry_fd); |
apetushkov@9858 | 222 | os::close(entry_fd); |
apetushkov@9858 | 223 | if (0 == entry_size) { |
apetushkov@9858 | 224 | return NULL; |
apetushkov@9858 | 225 | } |
apetushkov@9858 | 226 | return entry_name; |
apetushkov@9858 | 227 | } |
apetushkov@9858 | 228 | |
apetushkov@9858 | 229 | RepositoryIterator::RepositoryIterator(const char* repository, size_t repository_len) : |
apetushkov@9858 | 230 | _repo(repository), |
apetushkov@9858 | 231 | _repository_len(repository_len), |
apetushkov@9858 | 232 | _files(NULL), |
apetushkov@9858 | 233 | _iterator(0) { |
apetushkov@9858 | 234 | if (NULL != _repo) { |
apetushkov@9858 | 235 | assert(strlen(_repo) == _repository_len, "invariant"); |
apetushkov@9858 | 236 | _files = new GrowableArray<const char*>(10); |
apetushkov@9858 | 237 | DIR* dirp = os::opendir(_repo); |
apetushkov@9858 | 238 | if (dirp == NULL) { |
apetushkov@9858 | 239 | if (true) tty->print_cr("Unable to open repository %s", _repo); |
apetushkov@9858 | 240 | return; |
apetushkov@9858 | 241 | } |
apetushkov@9858 | 242 | struct dirent* dentry; |
apetushkov@9858 | 243 | while ((dentry = os::readdir(dirp)) != NULL) { |
apetushkov@9858 | 244 | const char* const entry_path = filter(dentry->d_name); |
apetushkov@9858 | 245 | if (NULL != entry_path) { |
apetushkov@9858 | 246 | _files->append(entry_path); |
apetushkov@9858 | 247 | } |
apetushkov@9858 | 248 | } |
apetushkov@9858 | 249 | os::closedir(dirp); |
apetushkov@9858 | 250 | if (_files->length() > 1) { |
apetushkov@9858 | 251 | _files->sort(file_sort); |
apetushkov@9858 | 252 | } |
apetushkov@9858 | 253 | } |
apetushkov@9858 | 254 | } |
apetushkov@9858 | 255 | |
apetushkov@9858 | 256 | #ifdef ASSERT |
apetushkov@9858 | 257 | void RepositoryIterator::print_repository_files() const { |
apetushkov@9858 | 258 | while (has_next()) { |
apetushkov@9858 | 259 | if (true) tty->print_cr( "%s", next()); |
apetushkov@9858 | 260 | } |
apetushkov@9858 | 261 | } |
apetushkov@9858 | 262 | #endif |
bulasevich@9947 | 263 | |
apetushkov@9858 | 264 | bool RepositoryIterator::has_next() const { |
apetushkov@9858 | 265 | return (_files != NULL && _iterator < _files->length()); |
apetushkov@9858 | 266 | } |
apetushkov@9858 | 267 | |
apetushkov@9858 | 268 | const char* const RepositoryIterator::next() const { |
apetushkov@9858 | 269 | return _iterator >= _files->length() ? NULL : fully_qualified(_files->at(_iterator++)); |
apetushkov@9858 | 270 | } |
apetushkov@9858 | 271 | |
apetushkov@9858 | 272 | static void write_emergency_file(fio_fd emergency_fd, const RepositoryIterator& iterator) { |
apetushkov@9858 | 273 | assert(emergency_fd != invalid_fd, "invariant"); |
apetushkov@9858 | 274 | const size_t size_of_file_copy_block = 1 * M; // 1 mb |
apetushkov@9858 | 275 | jbyte* const file_copy_block = NEW_RESOURCE_ARRAY_RETURN_NULL(jbyte, size_of_file_copy_block); |
apetushkov@9858 | 276 | if (file_copy_block == NULL) { |
apetushkov@9858 | 277 | return; |
apetushkov@9858 | 278 | } |
bulasevich@9947 | 279 | int64_t bytes_written_total = 0; |
apetushkov@9858 | 280 | while (iterator.has_next()) { |
apetushkov@9858 | 281 | fio_fd current_fd = invalid_fd; |
apetushkov@9858 | 282 | const char* const fqn = iterator.next(); |
apetushkov@9858 | 283 | if (fqn != NULL) { |
apetushkov@9858 | 284 | current_fd = open_existing(fqn); |
apetushkov@9858 | 285 | if (current_fd != invalid_fd) { |
bulasevich@9947 | 286 | const int64_t current_filesize = file_size(current_fd); |
apetushkov@9858 | 287 | assert(current_filesize > 0, "invariant"); |
bulasevich@9947 | 288 | int64_t bytes_read = 0; |
bulasevich@9947 | 289 | int64_t bytes_written = 0; |
apetushkov@9858 | 290 | while (bytes_read < current_filesize) { |
bulasevich@9947 | 291 | const ssize_t read_result = os::read_at(current_fd, file_copy_block, size_of_file_copy_block, bytes_read); |
bulasevich@9947 | 292 | if (-1 == read_result) { |
bulasevich@9947 | 293 | if (LogJFR) tty->print_cr("Unable to recover JFR data"); |
bulasevich@9947 | 294 | break; |
bulasevich@9947 | 295 | } |
bulasevich@9947 | 296 | bytes_read += (int64_t)read_result; |
bulasevich@9947 | 297 | assert(bytes_read - bytes_written <= (int64_t)size_of_file_copy_block, "invariant"); |
bulasevich@9947 | 298 | bytes_written += (int64_t)os::write(emergency_fd, file_copy_block, bytes_read - bytes_written); |
apetushkov@9858 | 299 | assert(bytes_read == bytes_written, "invariant"); |
apetushkov@9858 | 300 | } |
apetushkov@9858 | 301 | os::close(current_fd); |
apetushkov@9858 | 302 | bytes_written_total += bytes_written; |
apetushkov@9858 | 303 | } |
apetushkov@9858 | 304 | } |
apetushkov@9858 | 305 | } |
apetushkov@9858 | 306 | } |
apetushkov@9858 | 307 | |
apetushkov@9858 | 308 | static const char* create_emergency_dump_path() { |
apetushkov@9858 | 309 | assert(JfrStream_lock->owned_by_self(), "invariant"); |
apetushkov@9858 | 310 | char* buffer = NEW_RESOURCE_ARRAY_RETURN_NULL(char, O_BUFLEN); |
apetushkov@9858 | 311 | if (NULL == buffer) { |
apetushkov@9858 | 312 | return NULL; |
apetushkov@9858 | 313 | } |
apetushkov@9858 | 314 | const char* const cwd = os::get_current_directory(buffer, O_BUFLEN); |
apetushkov@9858 | 315 | if (NULL == cwd) { |
apetushkov@9858 | 316 | return NULL; |
apetushkov@9858 | 317 | } |
apetushkov@9858 | 318 | size_t pos = strlen(cwd); |
apetushkov@9858 | 319 | const int fsep_len = jio_snprintf(&buffer[pos], O_BUFLEN - pos, "%s", os::file_separator()); |
apetushkov@9858 | 320 | const char* filename_fmt = NULL; |
apetushkov@9858 | 321 | // fetch specific error cause |
apetushkov@9858 | 322 | switch (JfrJavaSupport::cause()) { |
apetushkov@9858 | 323 | case JfrJavaSupport::OUT_OF_MEMORY: |
apetushkov@9858 | 324 | filename_fmt = vm_oom_filename_fmt; |
apetushkov@9858 | 325 | break; |
apetushkov@9858 | 326 | case JfrJavaSupport::STACK_OVERFLOW: |
apetushkov@9858 | 327 | filename_fmt = vm_soe_filename_fmt; |
apetushkov@9858 | 328 | break; |
apetushkov@9858 | 329 | default: |
apetushkov@9858 | 330 | filename_fmt = vm_error_filename_fmt; |
apetushkov@9858 | 331 | } |
apetushkov@9858 | 332 | char* emergency_dump_path = NULL; |
apetushkov@9858 | 333 | pos += fsep_len; |
apetushkov@9858 | 334 | if (Arguments::copy_expand_pid(filename_fmt, strlen(filename_fmt), &buffer[pos], O_BUFLEN - pos)) { |
apetushkov@9858 | 335 | const size_t emergency_filename_length = strlen(buffer); |
apetushkov@9858 | 336 | emergency_dump_path = NEW_RESOURCE_ARRAY_RETURN_NULL(char, emergency_filename_length + 1); |
apetushkov@9858 | 337 | if (NULL == emergency_dump_path) { |
apetushkov@9858 | 338 | return NULL; |
apetushkov@9858 | 339 | } |
mikael@9880 | 340 | strncpy(emergency_dump_path, buffer, emergency_filename_length + 1); |
apetushkov@9858 | 341 | } |
apetushkov@9858 | 342 | return emergency_dump_path; |
apetushkov@9858 | 343 | } |
apetushkov@9858 | 344 | |
apetushkov@9858 | 345 | // Caller needs ResourceMark |
apetushkov@9858 | 346 | static const char* create_emergency_chunk_path(const char* repository_base, size_t repository_len) { |
apetushkov@9858 | 347 | assert(repository_base != NULL, "invariant"); |
apetushkov@9858 | 348 | assert(JfrStream_lock->owned_by_self(), "invariant"); |
apetushkov@9858 | 349 | // date time |
apetushkov@9858 | 350 | char date_time_buffer[32] = {0}; |
apetushkov@9858 | 351 | date_time(date_time_buffer, sizeof(date_time_buffer)); |
apetushkov@9858 | 352 | size_t date_time_len = strlen(date_time_buffer); |
apetushkov@9858 | 353 | size_t chunkname_max_len = repository_len // repository_base |
apetushkov@9858 | 354 | + 1 // "/" |
apetushkov@9858 | 355 | + date_time_len // date_time |
apetushkov@9858 | 356 | + strlen(chunk_file_jfr_ext) // .jfr |
apetushkov@9858 | 357 | + 1; |
apetushkov@9858 | 358 | char* chunk_path = NEW_RESOURCE_ARRAY_RETURN_NULL(char, chunkname_max_len); |
apetushkov@9858 | 359 | if (chunk_path == NULL) { |
apetushkov@9858 | 360 | return NULL; |
apetushkov@9858 | 361 | } |
apetushkov@9858 | 362 | // append the individual substrings |
apetushkov@9858 | 363 | jio_snprintf(chunk_path, chunkname_max_len, "%s%s%s%s", repository_base, os::file_separator(), date_time_buffer, chunk_file_jfr_ext); |
apetushkov@9858 | 364 | return chunk_path; |
apetushkov@9858 | 365 | } |
apetushkov@9858 | 366 | |
apetushkov@9858 | 367 | static fio_fd emergency_dump_file() { |
apetushkov@9858 | 368 | assert(JfrStream_lock->owned_by_self(), "invariant"); |
apetushkov@9858 | 369 | ResourceMark rm; |
apetushkov@9858 | 370 | const char* const emergency_dump_path = create_emergency_dump_path(); |
apetushkov@9858 | 371 | if (emergency_dump_path == NULL) { |
apetushkov@9858 | 372 | return invalid_fd; |
apetushkov@9858 | 373 | } |
apetushkov@9858 | 374 | const fio_fd fd = open_exclusivly(emergency_dump_path); |
apetushkov@9858 | 375 | if (fd != invalid_fd) { |
apetushkov@9858 | 376 | if (LogJFR) tty->print_cr( // For user, should not be "jfr, system" |
apetushkov@9858 | 377 | "Attempting to recover JFR data, emergency jfr file: %s", emergency_dump_path); |
apetushkov@9858 | 378 | } |
apetushkov@9858 | 379 | return fd; |
apetushkov@9858 | 380 | } |
apetushkov@9858 | 381 | |
apetushkov@9858 | 382 | static const char* emergency_path(const char* repository, size_t repository_len) { |
apetushkov@9858 | 383 | return repository == NULL ? create_emergency_dump_path() : create_emergency_chunk_path(repository, repository_len); |
apetushkov@9858 | 384 | } |
apetushkov@9858 | 385 | |
apetushkov@9858 | 386 | void JfrRepository::on_vm_error() { |
apetushkov@9858 | 387 | assert(!JfrStream_lock->owned_by_self(), "invariant"); |
apetushkov@9858 | 388 | const char* path = _path; |
apetushkov@9858 | 389 | if (path == NULL) { |
apetushkov@9858 | 390 | // completed already |
apetushkov@9858 | 391 | return; |
apetushkov@9858 | 392 | } |
apetushkov@9858 | 393 | ResourceMark rm; |
apetushkov@9858 | 394 | MutexLockerEx stream_lock(JfrStream_lock, Mutex::_no_safepoint_check_flag); |
apetushkov@9858 | 395 | const fio_fd emergency_fd = emergency_dump_file(); |
apetushkov@9858 | 396 | if (emergency_fd != invalid_fd) { |
apetushkov@9858 | 397 | RepositoryIterator iterator(path, strlen(path)); |
apetushkov@9858 | 398 | write_emergency_file(emergency_fd, iterator); |
apetushkov@9858 | 399 | os::close(emergency_fd); |
apetushkov@9858 | 400 | } |
apetushkov@9858 | 401 | } |
apetushkov@9858 | 402 | |
apetushkov@9858 | 403 | bool JfrRepository::set_path(const char* path) { |
apetushkov@9858 | 404 | assert(path != NULL, "trying to set the repository path with a NULL string!"); |
apetushkov@9858 | 405 | if (_path != NULL) { |
apetushkov@9858 | 406 | // delete existing |
apetushkov@9858 | 407 | JfrCHeapObj::free(_path, strlen(_path) + 1); |
apetushkov@9858 | 408 | } |
apetushkov@9858 | 409 | const size_t path_len = strlen(path); |
apetushkov@9858 | 410 | _path = JfrCHeapObj::new_array<char>(path_len + 1); |
apetushkov@9858 | 411 | if (_path == NULL) { |
apetushkov@9858 | 412 | return false; |
apetushkov@9858 | 413 | } |
mikael@9880 | 414 | strncpy(_path, path, path_len + 1); |
apetushkov@9858 | 415 | return true; |
apetushkov@9858 | 416 | } |
apetushkov@9858 | 417 | |
apetushkov@9858 | 418 | void JfrRepository::set_chunk_path(const char* path) { |
apetushkov@9858 | 419 | assert(JfrStream_lock->owned_by_self(), "invariant"); |
apetushkov@9858 | 420 | chunkwriter().set_chunk_path(path); |
apetushkov@9858 | 421 | } |
apetushkov@9858 | 422 | |
apetushkov@9858 | 423 | void JfrRepository::notify_on_new_chunk_path() { |
apetushkov@9858 | 424 | if (Jfr::is_recording()) { |
apetushkov@9858 | 425 | instance()._post_box.post(MSG_ROTATE); |
apetushkov@9858 | 426 | } |
apetushkov@9858 | 427 | } |
apetushkov@9858 | 428 | |
apetushkov@9858 | 429 | /** |
apetushkov@9858 | 430 | * Sets the file where data should be written. |
apetushkov@9858 | 431 | * |
apetushkov@9858 | 432 | * Recording Previous Current Action |
apetushkov@9858 | 433 | * ============================================== |
apetushkov@9858 | 434 | * true null null Ignore, keep recording in-memory |
apetushkov@9858 | 435 | * true null file1 Start disk recording |
apetushkov@9858 | 436 | * true file null Copy out metadata to disk and continue in-memory recording |
apetushkov@9858 | 437 | * true file1 file2 Copy out metadata and start with new File (file2) |
apetushkov@9858 | 438 | * false * null Ignore, but start recording to memory |
apetushkov@9858 | 439 | * false * file Ignore, but start recording to disk |
apetushkov@9858 | 440 | */ |
apetushkov@9858 | 441 | void JfrRepository::set_chunk_path(jstring path, JavaThread* jt) { |
apetushkov@9858 | 442 | DEBUG_ONLY(JfrJavaSupport::check_java_thread_in_vm(jt)); |
apetushkov@9858 | 443 | ResourceMark rm(jt); |
apetushkov@9858 | 444 | const char* const canonical_chunk_path = JfrJavaSupport::c_str(path, jt); |
apetushkov@9858 | 445 | { |
apetushkov@9858 | 446 | MutexLockerEx stream_lock(JfrStream_lock, Mutex::_no_safepoint_check_flag); |
apetushkov@9858 | 447 | if (NULL == canonical_chunk_path && !_chunkwriter->is_valid()) { |
apetushkov@9858 | 448 | // new output is NULL and current output is NULL |
apetushkov@9858 | 449 | return; |
apetushkov@9858 | 450 | } |
apetushkov@9858 | 451 | instance().set_chunk_path(canonical_chunk_path); |
apetushkov@9858 | 452 | } |
apetushkov@9858 | 453 | notify_on_new_chunk_path(); |
apetushkov@9858 | 454 | } |
apetushkov@9858 | 455 | |
apetushkov@9858 | 456 | void JfrRepository::set_path(jstring location, JavaThread* jt) { |
apetushkov@9858 | 457 | DEBUG_ONLY(JfrJavaSupport::check_java_thread_in_vm(jt)); |
apetushkov@9858 | 458 | ResourceMark rm(jt); |
apetushkov@9858 | 459 | const char* const path = JfrJavaSupport::c_str(location, jt); |
apetushkov@9858 | 460 | if (path != NULL) { |
apetushkov@9858 | 461 | instance().set_path(path); |
apetushkov@9858 | 462 | } |
apetushkov@9858 | 463 | } |
apetushkov@9858 | 464 | |
apetushkov@9858 | 465 | bool JfrRepository::open_chunk(bool vm_error /* false */) { |
apetushkov@9858 | 466 | assert(JfrStream_lock->owned_by_self(), "invariant"); |
apetushkov@9858 | 467 | if (vm_error) { |
apetushkov@9858 | 468 | ResourceMark rm; |
apetushkov@9858 | 469 | const char* repository_path = _path; |
apetushkov@9858 | 470 | const size_t repository_path_len = repository_path != NULL ? strlen(repository_path) : 0; |
apetushkov@9858 | 471 | const char* const path = emergency_path(repository_path, repository_path_len); |
apetushkov@9858 | 472 | _chunkwriter->set_chunk_path(path); |
apetushkov@9858 | 473 | } |
apetushkov@9858 | 474 | return _chunkwriter->open(); |
apetushkov@9858 | 475 | } |
apetushkov@9858 | 476 | |
bulasevich@9947 | 477 | size_t JfrRepository::close_chunk(int64_t metadata_offset) { |
apetushkov@9858 | 478 | return _chunkwriter->close(metadata_offset); |
apetushkov@9858 | 479 | } |