src/share/vm/jfr/recorder/service/jfrRecorderService.cpp

Mon, 12 Aug 2019 18:30:40 +0300

author
apetushkov
date
Mon, 12 Aug 2019 18:30:40 +0300
changeset 9858
b985cbb00e68
child 9875
6388d0d497f7
permissions
-rw-r--r--

8223147: JFR Backport
8199712: Flight Recorder
8203346: JFR: Inconsistent signature of jfr_add_string_constant
8195817: JFR.stop should require name of recording
8195818: JFR.start should increase autogenerated name by one
8195819: Remove recording=x from jcmd JFR.check output
8203921: JFR thread sampling is missing fixes from JDK-8194552
8203929: Limit amount of data for JFR.dump
8203664: JFR start failure after AppCDS archive created with JFR StartFlightRecording
8003209: JFR events for network utilization
8207392: [PPC64] Implement JFR profiling
8202835: jfr/event/os/TestSystemProcess.java fails on missing events
Summary: Backport JFR from JDK11. Initial integration
Reviewed-by: neugens

apetushkov@9858 1 /*
apetushkov@9858 2 * Copyright (c) 2016, 2018, 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/jni/jfrJavaSupport.hpp"
apetushkov@9858 27 #include "jfr/leakprofiler/checkpoint/objectSampleCheckpoint.hpp"
apetushkov@9858 28 #include "jfr/recorder/jfrRecorder.hpp"
apetushkov@9858 29 #include "jfr/recorder/checkpoint/jfrCheckpointManager.hpp"
apetushkov@9858 30 #include "jfr/recorder/checkpoint/jfrMetadataEvent.hpp"
apetushkov@9858 31 #include "jfr/recorder/repository/jfrChunkSizeNotifier.hpp"
apetushkov@9858 32 #include "jfr/recorder/repository/jfrChunkWriter.hpp"
apetushkov@9858 33 #include "jfr/recorder/repository/jfrRepository.hpp"
apetushkov@9858 34 #include "jfr/recorder/service/jfrPostBox.hpp"
apetushkov@9858 35 #include "jfr/recorder/service/jfrRecorderService.hpp"
apetushkov@9858 36 #include "jfr/recorder/stacktrace/jfrStackTraceRepository.hpp"
apetushkov@9858 37 #include "jfr/recorder/storage/jfrStorage.hpp"
apetushkov@9858 38 #include "jfr/recorder/storage/jfrStorageControl.hpp"
apetushkov@9858 39 #include "jfr/recorder/stringpool/jfrStringPool.hpp"
apetushkov@9858 40 #include "jfr/utilities/jfrAllocation.hpp"
apetushkov@9858 41 #include "jfr/utilities/jfrTime.hpp"
apetushkov@9858 42 #include "jfr/writers/jfrJavaEventWriter.hpp"
apetushkov@9858 43 #include "jfr/utilities/jfrTypes.hpp"
apetushkov@9858 44 #include "memory/resourceArea.hpp"
apetushkov@9858 45 #include "runtime/atomic.hpp"
apetushkov@9858 46 #include "runtime/handles.inline.hpp"
apetushkov@9858 47 #include "runtime/mutexLocker.hpp"
apetushkov@9858 48 #include "runtime/orderAccess.hpp"
apetushkov@9858 49 #include "runtime/os.hpp"
apetushkov@9858 50 #include "runtime/safepoint.hpp"
apetushkov@9858 51 #include "runtime/thread.inline.hpp"
apetushkov@9858 52 #include "runtime/vm_operations.hpp"
apetushkov@9858 53 #include "runtime/vmThread.hpp"
apetushkov@9858 54
apetushkov@9858 55 // set data iff *dest == NULL
apetushkov@9858 56 static bool try_set(void* const data, void** dest, bool clear) {
apetushkov@9858 57 assert(data != NULL, "invariant");
apetushkov@9858 58 void* const current = OrderAccess::load_ptr_acquire(dest);
apetushkov@9858 59 if (current != NULL) {
apetushkov@9858 60 if (current != data) {
apetushkov@9858 61 // already set
apetushkov@9858 62 return false;
apetushkov@9858 63 }
apetushkov@9858 64 assert(current == data, "invariant");
apetushkov@9858 65 if (!clear) {
apetushkov@9858 66 // recursion disallowed
apetushkov@9858 67 return false;
apetushkov@9858 68 }
apetushkov@9858 69 }
apetushkov@9858 70 return Atomic::cmpxchg_ptr(clear ? NULL : data, dest, current) == current;
apetushkov@9858 71 }
apetushkov@9858 72
apetushkov@9858 73 static void* rotation_thread = NULL;
apetushkov@9858 74 static const int rotation_try_limit = 1000;
apetushkov@9858 75 static const int rotation_retry_sleep_millis = 10;
apetushkov@9858 76
apetushkov@9858 77 class RotationLock : public StackObj {
apetushkov@9858 78 private:
apetushkov@9858 79 Thread* const _thread;
apetushkov@9858 80 bool _acquired;
apetushkov@9858 81
apetushkov@9858 82 void log(bool recursion) {
apetushkov@9858 83 assert(!_acquired, "invariant");
apetushkov@9858 84 const char* error_msg = NULL;
apetushkov@9858 85 if (recursion) {
apetushkov@9858 86 error_msg = "Unable to issue rotation due to recursive calls.";
apetushkov@9858 87 }
apetushkov@9858 88 else {
apetushkov@9858 89 error_msg = "Unable to issue rotation due to wait timeout.";
apetushkov@9858 90 }
apetushkov@9858 91 if (LogJFR) tty->print_cr( // For user, should not be "jfr, system"
apetushkov@9858 92 "%s", error_msg);
apetushkov@9858 93 }
apetushkov@9858 94 public:
apetushkov@9858 95 RotationLock(Thread* thread) : _thread(thread), _acquired(false) {
apetushkov@9858 96 assert(_thread != NULL, "invariant");
apetushkov@9858 97 if (_thread == rotation_thread) {
apetushkov@9858 98 // recursion not supported
apetushkov@9858 99 log(true);
apetushkov@9858 100 return;
apetushkov@9858 101 }
apetushkov@9858 102
apetushkov@9858 103 // limited to not spin indefinitely
apetushkov@9858 104 for (int i = 0; i < rotation_try_limit; ++i) {
apetushkov@9858 105 if (try_set(_thread, &rotation_thread, false)) {
apetushkov@9858 106 _acquired = true;
apetushkov@9858 107 assert(_thread == rotation_thread, "invariant");
apetushkov@9858 108 return;
apetushkov@9858 109 }
apetushkov@9858 110 if (_thread->is_Java_thread()) {
apetushkov@9858 111 // in order to allow the system to move to a safepoint
apetushkov@9858 112 MutexLockerEx msg_lock(JfrMsg_lock);
apetushkov@9858 113 JfrMsg_lock->wait(false, rotation_retry_sleep_millis);
apetushkov@9858 114 }
apetushkov@9858 115 else {
apetushkov@9858 116 os::naked_short_sleep(rotation_retry_sleep_millis);
apetushkov@9858 117 }
apetushkov@9858 118 }
apetushkov@9858 119 log(false);
apetushkov@9858 120 }
apetushkov@9858 121
apetushkov@9858 122 ~RotationLock() {
apetushkov@9858 123 assert(_thread != NULL, "invariant");
apetushkov@9858 124 if (_acquired) {
apetushkov@9858 125 assert(_thread == rotation_thread, "invariant");
apetushkov@9858 126 while (!try_set(_thread, &rotation_thread, true));
apetushkov@9858 127 }
apetushkov@9858 128 }
apetushkov@9858 129 bool not_acquired() const { return !_acquired; }
apetushkov@9858 130 };
apetushkov@9858 131
apetushkov@9858 132 static intptr_t write_checkpoint_event_prologue(JfrChunkWriter& cw, u8 type_id) {
apetushkov@9858 133 const intptr_t prev_cp_offset = cw.previous_checkpoint_offset();
apetushkov@9858 134 const intptr_t prev_cp_relative_offset = 0 == prev_cp_offset ? 0 : prev_cp_offset - cw.current_offset();
apetushkov@9858 135 cw.reserve(sizeof(u4));
apetushkov@9858 136 cw.write<u8>(EVENT_CHECKPOINT);
apetushkov@9858 137 cw.write(JfrTicks::now());
apetushkov@9858 138 cw.write<jlong>((jlong)0);
apetushkov@9858 139 cw.write<jlong>((jlong)prev_cp_relative_offset); // write previous checkpoint offset delta
apetushkov@9858 140 cw.write<bool>(false); // flushpoint
apetushkov@9858 141 cw.write<u4>((u4)1); // nof types in this checkpoint
apetushkov@9858 142 cw.write<u8>(type_id);
apetushkov@9858 143 const intptr_t number_of_elements_offset = cw.current_offset();
apetushkov@9858 144 cw.reserve(sizeof(u4));
apetushkov@9858 145 return number_of_elements_offset;
apetushkov@9858 146 }
apetushkov@9858 147
apetushkov@9858 148 template <typename ContentFunctor>
apetushkov@9858 149 class WriteCheckpointEvent : public StackObj {
apetushkov@9858 150 private:
apetushkov@9858 151 JfrChunkWriter& _cw;
apetushkov@9858 152 u8 _type_id;
apetushkov@9858 153 ContentFunctor& _content_functor;
apetushkov@9858 154 public:
apetushkov@9858 155 WriteCheckpointEvent(JfrChunkWriter& cw, u8 type_id, ContentFunctor& functor) :
apetushkov@9858 156 _cw(cw),
apetushkov@9858 157 _type_id(type_id),
apetushkov@9858 158 _content_functor(functor) {
apetushkov@9858 159 assert(_cw.is_valid(), "invariant");
apetushkov@9858 160 }
apetushkov@9858 161 bool process() {
apetushkov@9858 162 // current_cp_offset is also offset for the event size header field
apetushkov@9858 163 const intptr_t current_cp_offset = _cw.current_offset();
apetushkov@9858 164 const intptr_t num_elements_offset = write_checkpoint_event_prologue(_cw, _type_id);
apetushkov@9858 165 // invocation
apetushkov@9858 166 _content_functor.process();
apetushkov@9858 167 const u4 number_of_elements = (u4)_content_functor.processed();
apetushkov@9858 168 if (number_of_elements == 0) {
apetushkov@9858 169 // nothing to do, rewind writer to start
apetushkov@9858 170 _cw.seek(current_cp_offset);
apetushkov@9858 171 return true;
apetushkov@9858 172 }
apetushkov@9858 173 assert(number_of_elements > 0, "invariant");
apetushkov@9858 174 assert(_cw.current_offset() > num_elements_offset, "invariant");
apetushkov@9858 175 _cw.write_padded_at_offset<u4>(number_of_elements, num_elements_offset);
apetushkov@9858 176 _cw.write_padded_at_offset<u4>((u4)_cw.current_offset() - current_cp_offset, current_cp_offset);
apetushkov@9858 177 // update writer with last checkpoint position
apetushkov@9858 178 _cw.set_previous_checkpoint_offset(current_cp_offset);
apetushkov@9858 179 return true;
apetushkov@9858 180 }
apetushkov@9858 181 };
apetushkov@9858 182
apetushkov@9858 183 template <typename Instance, size_t(Instance::*func)()>
apetushkov@9858 184 class ServiceFunctor {
apetushkov@9858 185 private:
apetushkov@9858 186 Instance& _instance;
apetushkov@9858 187 size_t _processed;
apetushkov@9858 188 public:
apetushkov@9858 189 ServiceFunctor(Instance& instance) : _instance(instance), _processed(0) {}
apetushkov@9858 190 bool process() {
apetushkov@9858 191 _processed = (_instance.*func)();
apetushkov@9858 192 return true;
apetushkov@9858 193 }
apetushkov@9858 194 size_t processed() const { return _processed; }
apetushkov@9858 195 };
apetushkov@9858 196
apetushkov@9858 197 template <typename Instance, void(Instance::*func)()>
apetushkov@9858 198 class JfrVMOperation : public VM_Operation {
apetushkov@9858 199 private:
apetushkov@9858 200 Instance& _instance;
apetushkov@9858 201 public:
apetushkov@9858 202 JfrVMOperation(Instance& instance) : _instance(instance) {}
apetushkov@9858 203 void doit() { (_instance.*func)(); }
apetushkov@9858 204 VMOp_Type type() const { return VMOp_JFRCheckpoint; }
apetushkov@9858 205 Mode evaluation_mode() const { return _safepoint; } // default
apetushkov@9858 206 };
apetushkov@9858 207
apetushkov@9858 208 class WriteStackTraceRepository : public StackObj {
apetushkov@9858 209 private:
apetushkov@9858 210 JfrStackTraceRepository& _repo;
apetushkov@9858 211 JfrChunkWriter& _cw;
apetushkov@9858 212 size_t _elements_processed;
apetushkov@9858 213 bool _clear;
apetushkov@9858 214
apetushkov@9858 215 public:
apetushkov@9858 216 WriteStackTraceRepository(JfrStackTraceRepository& repo, JfrChunkWriter& cw, bool clear) :
apetushkov@9858 217 _repo(repo), _cw(cw), _elements_processed(0), _clear(clear) {}
apetushkov@9858 218 bool process() {
apetushkov@9858 219 _elements_processed = _repo.write(_cw, _clear);
apetushkov@9858 220 return true;
apetushkov@9858 221 }
apetushkov@9858 222 size_t processed() const { return _elements_processed; }
apetushkov@9858 223 void reset() { _elements_processed = 0; }
apetushkov@9858 224 };
apetushkov@9858 225
apetushkov@9858 226 static bool recording = false;
apetushkov@9858 227
apetushkov@9858 228 static void set_recording_state(bool is_recording) {
apetushkov@9858 229 OrderAccess::storestore();
apetushkov@9858 230 recording = is_recording;
apetushkov@9858 231 }
apetushkov@9858 232
apetushkov@9858 233 bool JfrRecorderService::is_recording() {
apetushkov@9858 234 return recording;
apetushkov@9858 235 }
apetushkov@9858 236
apetushkov@9858 237 JfrRecorderService::JfrRecorderService() :
apetushkov@9858 238 _checkpoint_manager(JfrCheckpointManager::instance()),
apetushkov@9858 239 _chunkwriter(JfrRepository::chunkwriter()),
apetushkov@9858 240 _repository(JfrRepository::instance()),
apetushkov@9858 241 _storage(JfrStorage::instance()),
apetushkov@9858 242 _stack_trace_repository(JfrStackTraceRepository::instance()),
apetushkov@9858 243 _string_pool(JfrStringPool::instance()) {}
apetushkov@9858 244
apetushkov@9858 245 void JfrRecorderService::start() {
apetushkov@9858 246 RotationLock rl(Thread::current());
apetushkov@9858 247 if (rl.not_acquired()) {
apetushkov@9858 248 return;
apetushkov@9858 249 }
apetushkov@9858 250 if (LogJFR) tty->print_cr("Request to START recording");
apetushkov@9858 251 assert(!is_recording(), "invariant");
apetushkov@9858 252 clear();
apetushkov@9858 253 set_recording_state(true);
apetushkov@9858 254 assert(is_recording(), "invariant");
apetushkov@9858 255 open_new_chunk();
apetushkov@9858 256 if (LogJFR) tty->print_cr("Recording STARTED");
apetushkov@9858 257 }
apetushkov@9858 258
apetushkov@9858 259 void JfrRecorderService::clear() {
apetushkov@9858 260 ResourceMark rm;
apetushkov@9858 261 HandleMark hm;
apetushkov@9858 262 pre_safepoint_clear();
apetushkov@9858 263 invoke_safepoint_clear();
apetushkov@9858 264 post_safepoint_clear();
apetushkov@9858 265 }
apetushkov@9858 266
apetushkov@9858 267 void JfrRecorderService::pre_safepoint_clear() {
apetushkov@9858 268 _stack_trace_repository.clear();
apetushkov@9858 269 _string_pool.clear();
apetushkov@9858 270 _storage.clear();
apetushkov@9858 271 }
apetushkov@9858 272
apetushkov@9858 273 void JfrRecorderService::invoke_safepoint_clear() {
apetushkov@9858 274 JfrVMOperation<JfrRecorderService, &JfrRecorderService::safepoint_clear> safepoint_task(*this);
apetushkov@9858 275 VMThread::execute(&safepoint_task);
apetushkov@9858 276 }
apetushkov@9858 277
apetushkov@9858 278 //
apetushkov@9858 279 // safepoint clear sequence
apetushkov@9858 280 //
apetushkov@9858 281 // clear stacktrace repository ->
apetushkov@9858 282 // clear string pool ->
apetushkov@9858 283 // clear storage ->
apetushkov@9858 284 // shift epoch ->
apetushkov@9858 285 // update time
apetushkov@9858 286 //
apetushkov@9858 287 void JfrRecorderService::safepoint_clear() {
apetushkov@9858 288 assert(SafepointSynchronize::is_at_safepoint(), "invariant");
apetushkov@9858 289 _stack_trace_repository.clear();
apetushkov@9858 290 _string_pool.clear();
apetushkov@9858 291 _storage.clear();
apetushkov@9858 292 _checkpoint_manager.shift_epoch();
apetushkov@9858 293 _chunkwriter.time_stamp_chunk_now();
apetushkov@9858 294 }
apetushkov@9858 295
apetushkov@9858 296 void JfrRecorderService::post_safepoint_clear() {
apetushkov@9858 297 _checkpoint_manager.clear();
apetushkov@9858 298 }
apetushkov@9858 299
apetushkov@9858 300 static void stop() {
apetushkov@9858 301 assert(JfrRecorderService::is_recording(), "invariant");
apetushkov@9858 302 if (LogJFR) tty->print_cr("Recording STOPPED");
apetushkov@9858 303 set_recording_state(false);
apetushkov@9858 304 assert(!JfrRecorderService::is_recording(), "invariant");
apetushkov@9858 305 }
apetushkov@9858 306
apetushkov@9858 307 void JfrRecorderService::rotate(int msgs) {
apetushkov@9858 308 RotationLock rl(Thread::current());
apetushkov@9858 309 if (rl.not_acquired()) {
apetushkov@9858 310 return;
apetushkov@9858 311 }
apetushkov@9858 312 static bool vm_error = false;
apetushkov@9858 313 if (msgs & MSGBIT(MSG_VM_ERROR)) {
apetushkov@9858 314 vm_error = true;
apetushkov@9858 315 prepare_for_vm_error_rotation();
apetushkov@9858 316 }
apetushkov@9858 317 if (msgs & (MSGBIT(MSG_STOP))) {
apetushkov@9858 318 stop();
apetushkov@9858 319 }
apetushkov@9858 320 // action determined by chunkwriter state
apetushkov@9858 321 if (!_chunkwriter.is_valid()) {
apetushkov@9858 322 in_memory_rotation();
apetushkov@9858 323 return;
apetushkov@9858 324 }
apetushkov@9858 325 if (vm_error) {
apetushkov@9858 326 vm_error_rotation();
apetushkov@9858 327 return;
apetushkov@9858 328 }
apetushkov@9858 329 chunk_rotation();
apetushkov@9858 330 }
apetushkov@9858 331
apetushkov@9858 332 void JfrRecorderService::prepare_for_vm_error_rotation() {
apetushkov@9858 333 if (!_chunkwriter.is_valid()) {
apetushkov@9858 334 open_new_chunk(true);
apetushkov@9858 335 }
apetushkov@9858 336 _checkpoint_manager.register_service_thread(Thread::current());
apetushkov@9858 337 }
apetushkov@9858 338
apetushkov@9858 339 void JfrRecorderService::open_new_chunk(bool vm_error) {
apetushkov@9858 340 assert(!_chunkwriter.is_valid(), "invariant");
apetushkov@9858 341 assert(!JfrStream_lock->owned_by_self(), "invariant");
apetushkov@9858 342 MutexLockerEx stream_lock(JfrStream_lock, Mutex::_no_safepoint_check_flag);
apetushkov@9858 343 if (!_repository.open_chunk(vm_error)) {
apetushkov@9858 344 assert(!_chunkwriter.is_valid(), "invariant");
apetushkov@9858 345 _storage.control().set_to_disk(false);
apetushkov@9858 346 return;
apetushkov@9858 347 }
apetushkov@9858 348 assert(_chunkwriter.is_valid(), "invariant");
apetushkov@9858 349 _storage.control().set_to_disk(true);
apetushkov@9858 350 }
apetushkov@9858 351
apetushkov@9858 352 void JfrRecorderService::in_memory_rotation() {
apetushkov@9858 353 assert(!_chunkwriter.is_valid(), "invariant");
apetushkov@9858 354 // currently running an in-memory recording
apetushkov@9858 355 open_new_chunk();
apetushkov@9858 356 if (_chunkwriter.is_valid()) {
apetushkov@9858 357 // dump all in-memory buffer data to the newly created chunk
apetushkov@9858 358 serialize_storage_from_in_memory_recording();
apetushkov@9858 359 }
apetushkov@9858 360 }
apetushkov@9858 361
apetushkov@9858 362 void JfrRecorderService::serialize_storage_from_in_memory_recording() {
apetushkov@9858 363 assert(!JfrStream_lock->owned_by_self(), "not holding stream lock!");
apetushkov@9858 364 MutexLockerEx stream_lock(JfrStream_lock, Mutex::_no_safepoint_check_flag);
apetushkov@9858 365 _storage.write();
apetushkov@9858 366 }
apetushkov@9858 367
apetushkov@9858 368 void JfrRecorderService::chunk_rotation() {
apetushkov@9858 369 finalize_current_chunk();
apetushkov@9858 370 open_new_chunk();
apetushkov@9858 371 }
apetushkov@9858 372
apetushkov@9858 373 void JfrRecorderService::finalize_current_chunk() {
apetushkov@9858 374 assert(_chunkwriter.is_valid(), "invariant");
apetushkov@9858 375 write();
apetushkov@9858 376 assert(!_chunkwriter.is_valid(), "invariant");
apetushkov@9858 377 }
apetushkov@9858 378
apetushkov@9858 379 void JfrRecorderService::write() {
apetushkov@9858 380 ResourceMark rm;
apetushkov@9858 381 HandleMark hm;
apetushkov@9858 382 pre_safepoint_write();
apetushkov@9858 383 invoke_safepoint_write();
apetushkov@9858 384 post_safepoint_write();
apetushkov@9858 385 }
apetushkov@9858 386
apetushkov@9858 387 typedef ServiceFunctor<JfrStringPool, &JfrStringPool::write> WriteStringPool;
apetushkov@9858 388 typedef ServiceFunctor<JfrStringPool, &JfrStringPool::write_at_safepoint> WriteStringPoolSafepoint;
apetushkov@9858 389 typedef WriteCheckpointEvent<WriteStackTraceRepository> WriteStackTraceCheckpoint;
apetushkov@9858 390 typedef WriteCheckpointEvent<WriteStringPool> WriteStringPoolCheckpoint;
apetushkov@9858 391 typedef WriteCheckpointEvent<WriteStringPoolSafepoint> WriteStringPoolCheckpointSafepoint;
apetushkov@9858 392
apetushkov@9858 393 static void write_stacktrace_checkpoint(JfrStackTraceRepository& stack_trace_repo, JfrChunkWriter& chunkwriter, bool clear) {
apetushkov@9858 394 WriteStackTraceRepository write_stacktrace_repo(stack_trace_repo, chunkwriter, clear);
apetushkov@9858 395 WriteStackTraceCheckpoint write_stack_trace_checkpoint(chunkwriter, TYPE_STACKTRACE, write_stacktrace_repo);
apetushkov@9858 396 write_stack_trace_checkpoint.process();
apetushkov@9858 397 }
apetushkov@9858 398
apetushkov@9858 399 static void write_stringpool_checkpoint(JfrStringPool& string_pool, JfrChunkWriter& chunkwriter) {
apetushkov@9858 400 WriteStringPool write_string_pool(string_pool);
apetushkov@9858 401 WriteStringPoolCheckpoint write_string_pool_checkpoint(chunkwriter, TYPE_STRING, write_string_pool);
apetushkov@9858 402 write_string_pool_checkpoint.process();
apetushkov@9858 403 }
apetushkov@9858 404
apetushkov@9858 405 static void write_stringpool_checkpoint_safepoint(JfrStringPool& string_pool, JfrChunkWriter& chunkwriter) {
apetushkov@9858 406 WriteStringPoolSafepoint write_string_pool(string_pool);
apetushkov@9858 407 WriteStringPoolCheckpointSafepoint write_string_pool_checkpoint(chunkwriter, TYPE_STRING, write_string_pool);
apetushkov@9858 408 write_string_pool_checkpoint.process();
apetushkov@9858 409 }
apetushkov@9858 410
apetushkov@9858 411 //
apetushkov@9858 412 // pre-safepoint write sequence
apetushkov@9858 413 //
apetushkov@9858 414 // lock stream lock ->
apetushkov@9858 415 // write non-safepoint dependent types ->
apetushkov@9858 416 // write checkpoint epoch transition list->
apetushkov@9858 417 // write stack trace checkpoint ->
apetushkov@9858 418 // write string pool checkpoint ->
apetushkov@9858 419 // write storage ->
apetushkov@9858 420 // release stream lock
apetushkov@9858 421 //
apetushkov@9858 422 void JfrRecorderService::pre_safepoint_write() {
apetushkov@9858 423 MutexLockerEx stream_lock(JfrStream_lock, Mutex::_no_safepoint_check_flag);
apetushkov@9858 424 assert(_chunkwriter.is_valid(), "invariant");
apetushkov@9858 425 _checkpoint_manager.write_types();
apetushkov@9858 426 _checkpoint_manager.write_epoch_transition_mspace();
apetushkov@9858 427 write_stacktrace_checkpoint(_stack_trace_repository, _chunkwriter, false);
apetushkov@9858 428 write_stringpool_checkpoint(_string_pool, _chunkwriter);
apetushkov@9858 429 _storage.write();
apetushkov@9858 430 }
apetushkov@9858 431
apetushkov@9858 432 void JfrRecorderService::invoke_safepoint_write() {
apetushkov@9858 433 JfrVMOperation<JfrRecorderService, &JfrRecorderService::safepoint_write> safepoint_task(*this);
apetushkov@9858 434 VMThread::execute(&safepoint_task);
apetushkov@9858 435 }
apetushkov@9858 436
apetushkov@9858 437 static void write_object_sample_stacktrace(JfrStackTraceRepository& stack_trace_repository) {
apetushkov@9858 438 WriteObjectSampleStacktrace object_sample_stacktrace(stack_trace_repository);
apetushkov@9858 439 object_sample_stacktrace.process();
apetushkov@9858 440 }
apetushkov@9858 441
apetushkov@9858 442 //
apetushkov@9858 443 // safepoint write sequence
apetushkov@9858 444 //
apetushkov@9858 445 // lock stream lock ->
apetushkov@9858 446 // write object sample stacktraces ->
apetushkov@9858 447 // write stacktrace repository ->
apetushkov@9858 448 // write string pool ->
apetushkov@9858 449 // write safepoint dependent types ->
apetushkov@9858 450 // write storage ->
apetushkov@9858 451 // shift_epoch ->
apetushkov@9858 452 // update time ->
apetushkov@9858 453 // lock metadata descriptor ->
apetushkov@9858 454 // release stream lock
apetushkov@9858 455 //
apetushkov@9858 456 void JfrRecorderService::safepoint_write() {
apetushkov@9858 457 assert(SafepointSynchronize::is_at_safepoint(), "invariant");
apetushkov@9858 458 MutexLockerEx stream_lock(JfrStream_lock, Mutex::_no_safepoint_check_flag);
apetushkov@9858 459 write_object_sample_stacktrace(_stack_trace_repository);
apetushkov@9858 460 write_stacktrace_checkpoint(_stack_trace_repository, _chunkwriter, true);
apetushkov@9858 461 write_stringpool_checkpoint_safepoint(_string_pool, _chunkwriter);
apetushkov@9858 462 _checkpoint_manager.write_safepoint_types();
apetushkov@9858 463 _storage.write_at_safepoint();
apetushkov@9858 464 _checkpoint_manager.shift_epoch();
apetushkov@9858 465 _chunkwriter.time_stamp_chunk_now();
apetushkov@9858 466 JfrMetadataEvent::lock();
apetushkov@9858 467 }
apetushkov@9858 468
apetushkov@9858 469 static jlong write_metadata_event(JfrChunkWriter& chunkwriter) {
apetushkov@9858 470 assert(chunkwriter.is_valid(), "invariant");
apetushkov@9858 471 const jlong metadata_offset = chunkwriter.current_offset();
apetushkov@9858 472 JfrMetadataEvent::write(chunkwriter, metadata_offset);
apetushkov@9858 473 return metadata_offset;
apetushkov@9858 474 }
apetushkov@9858 475
apetushkov@9858 476 //
apetushkov@9858 477 // post-safepoint write sequence
apetushkov@9858 478 //
apetushkov@9858 479 // lock stream lock ->
apetushkov@9858 480 // write type set ->
apetushkov@9858 481 // write checkpoints ->
apetushkov@9858 482 // write metadata event ->
apetushkov@9858 483 // write chunk header ->
apetushkov@9858 484 // close chunk fd ->
apetushkov@9858 485 // release stream lock
apetushkov@9858 486 //
apetushkov@9858 487 void JfrRecorderService::post_safepoint_write() {
apetushkov@9858 488 assert(_chunkwriter.is_valid(), "invariant");
apetushkov@9858 489 // During the safepoint tasks just completed, the system transitioned to a new epoch.
apetushkov@9858 490 // Type tagging is epoch relative which entails we are able to write out the
apetushkov@9858 491 // already tagged artifacts for the previous epoch. We can accomplish this concurrently
apetushkov@9858 492 // with threads now tagging artifacts in relation to the new, now updated, epoch and remain outside of a safepoint.
apetushkov@9858 493 _checkpoint_manager.write_type_set();
apetushkov@9858 494 MutexLockerEx stream_lock(JfrStream_lock, Mutex::_no_safepoint_check_flag);
apetushkov@9858 495 // serialize any outstanding checkpoint memory
apetushkov@9858 496 _checkpoint_manager.write();
apetushkov@9858 497 // serialize the metadata descriptor event and close out the chunk
apetushkov@9858 498 _repository.close_chunk(write_metadata_event(_chunkwriter));
apetushkov@9858 499 assert(!_chunkwriter.is_valid(), "invariant");
apetushkov@9858 500 }
apetushkov@9858 501
apetushkov@9858 502 void JfrRecorderService::vm_error_rotation() {
apetushkov@9858 503 if (_chunkwriter.is_valid()) {
apetushkov@9858 504 finalize_current_chunk_on_vm_error();
apetushkov@9858 505 assert(!_chunkwriter.is_valid(), "invariant");
apetushkov@9858 506 _repository.on_vm_error();
apetushkov@9858 507 }
apetushkov@9858 508 }
apetushkov@9858 509
apetushkov@9858 510 void JfrRecorderService::finalize_current_chunk_on_vm_error() {
apetushkov@9858 511 assert(_chunkwriter.is_valid(), "invariant");
apetushkov@9858 512 pre_safepoint_write();
apetushkov@9858 513 JfrMetadataEvent::lock();
apetushkov@9858 514 // Do not attempt safepoint dependent operations during emergency dump.
apetushkov@9858 515 // Optimistically write tagged artifacts.
apetushkov@9858 516 _checkpoint_manager.shift_epoch();
apetushkov@9858 517 _checkpoint_manager.write_type_set();
apetushkov@9858 518 // update time
apetushkov@9858 519 _chunkwriter.time_stamp_chunk_now();
apetushkov@9858 520 post_safepoint_write();
apetushkov@9858 521 assert(!_chunkwriter.is_valid(), "invariant");
apetushkov@9858 522 }
apetushkov@9858 523
apetushkov@9858 524 void JfrRecorderService::process_full_buffers() {
apetushkov@9858 525 if (_chunkwriter.is_valid()) {
apetushkov@9858 526 assert(!JfrStream_lock->owned_by_self(), "invariant");
apetushkov@9858 527 MutexLockerEx stream_lock(JfrStream_lock, Mutex::_no_safepoint_check_flag);
apetushkov@9858 528 _storage.write_full();
apetushkov@9858 529 }
apetushkov@9858 530 }
apetushkov@9858 531
apetushkov@9858 532 void JfrRecorderService::scavenge() {
apetushkov@9858 533 _storage.scavenge();
apetushkov@9858 534 }
apetushkov@9858 535
apetushkov@9858 536 void JfrRecorderService::evaluate_chunk_size_for_rotation() {
apetushkov@9858 537 const size_t size_written = _chunkwriter.size_written();
apetushkov@9858 538 if (size_written > JfrChunkSizeNotifier::chunk_size_threshold()) {
apetushkov@9858 539 JfrChunkSizeNotifier::notify();
apetushkov@9858 540 }
apetushkov@9858 541 }

mercurial