src/share/vm/jfr/recorder/checkpoint/jfrCheckpointManager.cpp

Wed, 17 Jun 2020 11:43:05 +0300

author
apetushkov
date
Wed, 17 Jun 2020 11:43:05 +0300
changeset 9928
d2c2cd90513e
parent 9858
b985cbb00e68
permissions
-rw-r--r--

8220293: Deadlock in JFR string pool
Reviewed-by: rehn, egahlin

apetushkov@9858 1 /*
apetushkov@9928 2 * Copyright (c) 2016, 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 "classfile/javaClasses.hpp"
apetushkov@9858 27 #include "jfr/recorder/jfrRecorder.hpp"
apetushkov@9858 28 #include "jfr/recorder/checkpoint/jfrCheckpointManager.hpp"
apetushkov@9858 29 #include "jfr/recorder/checkpoint/jfrCheckpointWriter.hpp"
apetushkov@9858 30 #include "jfr/recorder/checkpoint/types/jfrTypeManager.hpp"
apetushkov@9858 31 #include "jfr/recorder/checkpoint/types/traceid/jfrTraceIdEpoch.hpp"
apetushkov@9858 32 #include "jfr/recorder/service/jfrOptionSet.hpp"
apetushkov@9858 33 #include "jfr/recorder/storage/jfrMemorySpace.inline.hpp"
apetushkov@9858 34 #include "jfr/recorder/storage/jfrStorageUtils.inline.hpp"
apetushkov@9858 35 #include "jfr/recorder/repository/jfrChunkWriter.hpp"
apetushkov@9858 36 #include "jfr/utilities/jfrBigEndian.hpp"
apetushkov@9858 37 #include "jfr/utilities/jfrTypes.hpp"
apetushkov@9858 38 #include "memory/resourceArea.hpp"
apetushkov@9858 39 #include "runtime/mutexLocker.hpp"
apetushkov@9858 40 #include "runtime/orderAccess.inline.hpp"
apetushkov@9858 41 #include "runtime/os.hpp"
apetushkov@9858 42 #include "runtime/safepoint.hpp"
apetushkov@9858 43
apetushkov@9858 44 typedef JfrCheckpointManager::Buffer* BufferPtr;
apetushkov@9858 45
apetushkov@9858 46 static JfrCheckpointManager* _instance = NULL;
apetushkov@9858 47
apetushkov@9858 48 JfrCheckpointManager& JfrCheckpointManager::instance() {
apetushkov@9858 49 return *_instance;
apetushkov@9858 50 }
apetushkov@9858 51
apetushkov@9858 52 JfrCheckpointManager* JfrCheckpointManager::create(JfrChunkWriter& cw) {
apetushkov@9858 53 assert(_instance == NULL, "invariant");
apetushkov@9858 54 _instance = new JfrCheckpointManager(cw);
apetushkov@9858 55 return _instance;
apetushkov@9858 56 }
apetushkov@9858 57
apetushkov@9858 58 void JfrCheckpointManager::destroy() {
apetushkov@9858 59 assert(_instance != NULL, "invariant");
apetushkov@9858 60 delete _instance;
apetushkov@9858 61 _instance = NULL;
apetushkov@9858 62 }
apetushkov@9858 63
apetushkov@9858 64 JfrCheckpointManager::JfrCheckpointManager(JfrChunkWriter& cw) :
apetushkov@9858 65 _free_list_mspace(NULL),
apetushkov@9858 66 _epoch_transition_mspace(NULL),
apetushkov@9858 67 _lock(NULL),
apetushkov@9858 68 _service_thread(NULL),
apetushkov@9858 69 _chunkwriter(cw),
apetushkov@9858 70 _checkpoint_epoch_state(JfrTraceIdEpoch::current()) {}
apetushkov@9858 71
apetushkov@9858 72 JfrCheckpointManager::~JfrCheckpointManager() {
apetushkov@9858 73 if (_free_list_mspace != NULL) {
apetushkov@9858 74 delete _free_list_mspace;
apetushkov@9858 75 }
apetushkov@9858 76 if (_epoch_transition_mspace != NULL) {
apetushkov@9858 77 delete _epoch_transition_mspace;
apetushkov@9858 78 }
apetushkov@9858 79 if (_lock != NULL) {
apetushkov@9858 80 delete _lock;
apetushkov@9858 81 }
apetushkov@9858 82 JfrTypeManager::clear();
apetushkov@9858 83 }
apetushkov@9858 84
apetushkov@9858 85 static const size_t unlimited_mspace_size = 0;
apetushkov@9858 86 static const size_t checkpoint_buffer_cache_count = 2;
apetushkov@9858 87 static const size_t checkpoint_buffer_size = 512 * K;
apetushkov@9858 88
apetushkov@9858 89 static JfrCheckpointMspace* create_mspace(size_t buffer_size, size_t limit, size_t cache_count, JfrCheckpointManager* system) {
apetushkov@9858 90 JfrCheckpointMspace* mspace = new JfrCheckpointMspace(buffer_size, limit, cache_count, system);
apetushkov@9858 91 if (mspace != NULL) {
apetushkov@9858 92 mspace->initialize();
apetushkov@9858 93 }
apetushkov@9858 94 return mspace;
apetushkov@9858 95 }
apetushkov@9858 96
apetushkov@9858 97 bool JfrCheckpointManager::initialize() {
apetushkov@9858 98 assert(_free_list_mspace == NULL, "invariant");
apetushkov@9858 99 _free_list_mspace = create_mspace(checkpoint_buffer_size, unlimited_mspace_size, checkpoint_buffer_cache_count, this);
apetushkov@9858 100 if (_free_list_mspace == NULL) {
apetushkov@9858 101 return false;
apetushkov@9858 102 }
apetushkov@9858 103 assert(_epoch_transition_mspace == NULL, "invariant");
apetushkov@9858 104 _epoch_transition_mspace = create_mspace(checkpoint_buffer_size, unlimited_mspace_size, checkpoint_buffer_cache_count, this);
apetushkov@9858 105 if (_epoch_transition_mspace == NULL) {
apetushkov@9858 106 return false;
apetushkov@9858 107 }
apetushkov@9858 108 assert(_lock == NULL, "invariant");
apetushkov@9858 109 _lock = new Mutex(Monitor::leaf - 1, "Checkpoint mutex", Mutex::_allow_vm_block_flag);
apetushkov@9858 110 if (_lock == NULL) {
apetushkov@9858 111 return false;
apetushkov@9858 112 }
apetushkov@9858 113 return JfrTypeManager::initialize();
apetushkov@9858 114 }
apetushkov@9858 115
apetushkov@9858 116 bool JfrCheckpointManager::use_epoch_transition_mspace(const Thread* thread) const {
apetushkov@9858 117 return _service_thread != thread && OrderAccess::load_acquire((u1*)&_checkpoint_epoch_state) != JfrTraceIdEpoch::current();
apetushkov@9858 118 }
apetushkov@9858 119
apetushkov@9858 120 void JfrCheckpointManager::synchronize_epoch() {
apetushkov@9858 121 assert(_checkpoint_epoch_state != JfrTraceIdEpoch::current(), "invariant");
apetushkov@9858 122 OrderAccess::storestore();
apetushkov@9858 123 _checkpoint_epoch_state = JfrTraceIdEpoch::current();
apetushkov@9858 124 }
apetushkov@9858 125
apetushkov@9858 126 void JfrCheckpointManager::shift_epoch() {
apetushkov@9858 127 debug_only(const u1 current_epoch = JfrTraceIdEpoch::current();)
apetushkov@9858 128 JfrTraceIdEpoch::shift_epoch();
apetushkov@9858 129 assert(current_epoch != JfrTraceIdEpoch::current(), "invariant");
apetushkov@9858 130 }
apetushkov@9858 131
apetushkov@9858 132 void JfrCheckpointManager::register_service_thread(const Thread* thread) {
apetushkov@9858 133 _service_thread = thread;
apetushkov@9858 134 }
apetushkov@9858 135
apetushkov@9858 136 void JfrCheckpointManager::register_full(BufferPtr t, Thread* thread) {
apetushkov@9858 137 // nothing here at the moment
apetushkov@9928 138 assert(t != NULL, "invariant");
apetushkov@9928 139 assert(t->acquired_by(thread), "invariant");
apetushkov@9858 140 assert(t->retired(), "invariant");
apetushkov@9858 141 }
apetushkov@9858 142
apetushkov@9858 143 void JfrCheckpointManager::lock() {
apetushkov@9858 144 assert(!_lock->owned_by_self(), "invariant");
apetushkov@9858 145 _lock->lock_without_safepoint_check();
apetushkov@9858 146 }
apetushkov@9858 147
apetushkov@9858 148 void JfrCheckpointManager::unlock() {
apetushkov@9858 149 _lock->unlock();
apetushkov@9858 150 }
apetushkov@9858 151
apetushkov@9858 152 #ifdef ASSERT
apetushkov@9858 153
apetushkov@9858 154 bool JfrCheckpointManager::is_locked() const {
apetushkov@9858 155 return _lock->owned_by_self();
apetushkov@9858 156 }
apetushkov@9858 157
apetushkov@9858 158 static void assert_free_lease(const BufferPtr buffer) {
apetushkov@9858 159 assert(buffer != NULL, "invariant");
apetushkov@9858 160 assert(buffer->acquired_by_self(), "invariant");
apetushkov@9858 161 assert(buffer->lease(), "invariant");
apetushkov@9858 162 }
apetushkov@9858 163
apetushkov@9858 164 static void assert_release(const BufferPtr buffer) {
apetushkov@9858 165 assert(buffer != NULL, "invariant");
apetushkov@9858 166 assert(buffer->lease(), "invariant");
apetushkov@9858 167 assert(buffer->acquired_by_self(), "invariant");
apetushkov@9858 168 }
apetushkov@9858 169
apetushkov@9858 170 #endif // ASSERT
apetushkov@9858 171
apetushkov@9858 172 static BufferPtr lease_free(size_t size, JfrCheckpointMspace* mspace, size_t retry_count, Thread* thread) {
apetushkov@9858 173 static const size_t max_elem_size = mspace->min_elem_size(); // min is max
apetushkov@9858 174 BufferPtr buffer;
apetushkov@9858 175 if (size <= max_elem_size) {
apetushkov@9858 176 BufferPtr buffer = mspace_get_free_lease_with_retry(size, mspace, retry_count, thread);
apetushkov@9858 177 if (buffer != NULL) {
apetushkov@9858 178 DEBUG_ONLY(assert_free_lease(buffer);)
apetushkov@9858 179 return buffer;
apetushkov@9858 180 }
apetushkov@9858 181 }
apetushkov@9858 182 buffer = mspace_allocate_transient_lease_to_free(size, mspace, thread);
apetushkov@9858 183 DEBUG_ONLY(assert_free_lease(buffer);)
apetushkov@9858 184 return buffer;
apetushkov@9858 185 }
apetushkov@9858 186
apetushkov@9858 187 static const size_t lease_retry = 10;
apetushkov@9858 188
apetushkov@9858 189 BufferPtr JfrCheckpointManager::lease_buffer(Thread* thread, size_t size /* 0 */) {
apetushkov@9858 190 JfrCheckpointManager& manager = instance();
apetushkov@9858 191 if (manager.use_epoch_transition_mspace(thread)) {
apetushkov@9858 192 return lease_free(size, manager._epoch_transition_mspace, lease_retry, thread);
apetushkov@9858 193 }
apetushkov@9858 194 return lease_free(size, manager._free_list_mspace, lease_retry, thread);
apetushkov@9858 195 }
apetushkov@9858 196
apetushkov@9858 197 /*
apetushkov@9858 198 * If the buffer was a "lease" from the free list, release back.
apetushkov@9858 199 *
apetushkov@9858 200 * The buffer is effectively invalidated for the thread post-return,
apetushkov@9858 201 * and the caller should take means to ensure that it is not referenced.
apetushkov@9858 202 */
apetushkov@9858 203 static void release(BufferPtr const buffer, Thread* thread) {
apetushkov@9858 204 DEBUG_ONLY(assert_release(buffer);)
apetushkov@9858 205 buffer->clear_lease();
apetushkov@9858 206 buffer->release();
apetushkov@9858 207 }
apetushkov@9858 208
apetushkov@9858 209 BufferPtr JfrCheckpointManager::flush(BufferPtr old, size_t used, size_t requested, Thread* thread) {
apetushkov@9858 210 assert(old != NULL, "invariant");
apetushkov@9858 211 assert(old->lease(), "invariant");
apetushkov@9858 212 if (0 == requested) {
apetushkov@9858 213 // indicates a lease is being returned
apetushkov@9858 214 release(old, thread);
apetushkov@9858 215 return NULL;
apetushkov@9858 216 }
apetushkov@9858 217 // migration of in-flight information
apetushkov@9858 218 BufferPtr const new_buffer = lease_buffer(thread, used + requested);
apetushkov@9858 219 if (new_buffer != NULL) {
apetushkov@9858 220 migrate_outstanding_writes(old, new_buffer, used, requested);
apetushkov@9858 221 }
apetushkov@9858 222 release(old, thread);
apetushkov@9858 223 return new_buffer; // might be NULL
apetushkov@9858 224 }
apetushkov@9858 225
apetushkov@9858 226 // offsets into the JfrCheckpointEntry
apetushkov@9858 227 static const juint starttime_offset = sizeof(jlong);
apetushkov@9858 228 static const juint duration_offset = starttime_offset + sizeof(jlong);
apetushkov@9858 229 static const juint flushpoint_offset = duration_offset + sizeof(jlong);
apetushkov@9858 230 static const juint types_offset = flushpoint_offset + sizeof(juint);
apetushkov@9858 231 static const juint payload_offset = types_offset + sizeof(juint);
apetushkov@9858 232
apetushkov@9858 233 template <typename Return>
apetushkov@9858 234 static Return read_data(const u1* data) {
apetushkov@9858 235 return JfrBigEndian::read<Return>(data);
apetushkov@9858 236 }
apetushkov@9858 237
apetushkov@9858 238 static jlong total_size(const u1* data) {
apetushkov@9858 239 return read_data<jlong>(data);
apetushkov@9858 240 }
apetushkov@9858 241
apetushkov@9858 242 static jlong starttime(const u1* data) {
apetushkov@9858 243 return read_data<jlong>(data + starttime_offset);
apetushkov@9858 244 }
apetushkov@9858 245
apetushkov@9858 246 static jlong duration(const u1* data) {
apetushkov@9858 247 return read_data<jlong>(data + duration_offset);
apetushkov@9858 248 }
apetushkov@9858 249
apetushkov@9858 250 static bool is_flushpoint(const u1* data) {
apetushkov@9858 251 return read_data<juint>(data + flushpoint_offset) == (juint)1;
apetushkov@9858 252 }
apetushkov@9858 253
apetushkov@9858 254 static juint number_of_types(const u1* data) {
apetushkov@9858 255 return read_data<juint>(data + types_offset);
apetushkov@9858 256 }
apetushkov@9858 257
apetushkov@9858 258 static void write_checkpoint_header(JfrChunkWriter& cw, intptr_t offset_prev_cp_event, const u1* data) {
apetushkov@9858 259 cw.reserve(sizeof(u4));
apetushkov@9858 260 cw.write((u8)EVENT_CHECKPOINT);
apetushkov@9858 261 cw.write(starttime(data));
apetushkov@9858 262 cw.write(duration(data));
apetushkov@9858 263 cw.write((jlong)offset_prev_cp_event);
apetushkov@9858 264 cw.write(is_flushpoint(data));
apetushkov@9858 265 cw.write(number_of_types(data));
apetushkov@9858 266 }
apetushkov@9858 267
apetushkov@9858 268 static void write_checkpoint_content(JfrChunkWriter& cw, const u1* data, size_t size) {
apetushkov@9858 269 assert(data != NULL, "invariant");
apetushkov@9858 270 cw.write_unbuffered(data + payload_offset, size);
apetushkov@9858 271 }
apetushkov@9858 272
apetushkov@9858 273 static size_t write_checkpoint_event(JfrChunkWriter& cw, const u1* data) {
apetushkov@9858 274 assert(data != NULL, "invariant");
apetushkov@9858 275 const intptr_t previous_checkpoint_event = cw.previous_checkpoint_offset();
apetushkov@9858 276 const intptr_t event_begin = cw.current_offset();
apetushkov@9858 277 const intptr_t offset_to_previous_checkpoint_event = 0 == previous_checkpoint_event ? 0 : previous_checkpoint_event - event_begin;
apetushkov@9858 278 const jlong total_checkpoint_size = total_size(data);
apetushkov@9858 279 write_checkpoint_header(cw, offset_to_previous_checkpoint_event, data);
apetushkov@9858 280 write_checkpoint_content(cw, data, total_checkpoint_size - sizeof(JfrCheckpointEntry));
apetushkov@9858 281 const jlong checkpoint_event_size = cw.current_offset() - event_begin;
apetushkov@9858 282 cw.write_padded_at_offset<u4>(checkpoint_event_size, event_begin);
apetushkov@9858 283 cw.set_previous_checkpoint_offset(event_begin);
apetushkov@9858 284 return (size_t)total_checkpoint_size;
apetushkov@9858 285 }
apetushkov@9858 286
apetushkov@9858 287 static size_t write_checkpoints(JfrChunkWriter& cw, const u1* data, size_t size) {
apetushkov@9858 288 assert(cw.is_valid(), "invariant");
apetushkov@9858 289 assert(data != NULL, "invariant");
apetushkov@9858 290 assert(size > 0, "invariant");
apetushkov@9858 291 const u1* const limit = data + size;
apetushkov@9858 292 const u1* next_entry = data;
apetushkov@9858 293 size_t processed = 0;
apetushkov@9858 294 while (next_entry < limit) {
apetushkov@9858 295 const size_t checkpoint_size = write_checkpoint_event(cw, next_entry);
apetushkov@9858 296 processed += checkpoint_size;
apetushkov@9858 297 next_entry += checkpoint_size;
apetushkov@9858 298 }
apetushkov@9858 299 assert(next_entry == limit, "invariant");
apetushkov@9858 300 return processed;
apetushkov@9858 301 }
apetushkov@9858 302
apetushkov@9858 303 template <typename T>
apetushkov@9858 304 class CheckpointWriteOp {
apetushkov@9858 305 private:
apetushkov@9858 306 JfrChunkWriter& _writer;
apetushkov@9858 307 size_t _processed;
apetushkov@9858 308 public:
apetushkov@9858 309 typedef T Type;
apetushkov@9858 310 CheckpointWriteOp(JfrChunkWriter& writer) : _writer(writer), _processed(0) {}
apetushkov@9858 311 bool write(Type* t, const u1* data, size_t size) {
apetushkov@9858 312 _processed += write_checkpoints(_writer, data, size);
apetushkov@9858 313 return true;
apetushkov@9858 314 }
apetushkov@9858 315 size_t processed() const { return _processed; }
apetushkov@9858 316 };
apetushkov@9858 317
apetushkov@9858 318 typedef CheckpointWriteOp<JfrCheckpointMspace::Type> WriteOperation;
apetushkov@9858 319 typedef MutexedWriteOp<WriteOperation> MutexedWriteOperation;
apetushkov@9858 320 typedef ReleaseOp<JfrCheckpointMspace> CheckpointReleaseOperation;
apetushkov@9858 321 typedef CompositeOperation<MutexedWriteOperation, CheckpointReleaseOperation> CheckpointWriteOperation;
apetushkov@9858 322
apetushkov@9858 323 static size_t write_mspace_exclusive(JfrCheckpointMspace* mspace, JfrChunkWriter& chunkwriter) {
apetushkov@9858 324 Thread* const thread = Thread::current();
apetushkov@9858 325 WriteOperation wo(chunkwriter);
apetushkov@9858 326 MutexedWriteOperation mwo(wo);
apetushkov@9858 327 CheckpointReleaseOperation cro(mspace, thread, false);
apetushkov@9858 328 CheckpointWriteOperation cpwo(&mwo, &cro);
apetushkov@9858 329 assert(mspace->is_full_empty(), "invariant");
apetushkov@9858 330 process_free_list(cpwo, mspace);
apetushkov@9858 331 return wo.processed();
apetushkov@9858 332 }
apetushkov@9858 333
apetushkov@9858 334 size_t JfrCheckpointManager::write() {
apetushkov@9858 335 const size_t processed = write_mspace_exclusive(_free_list_mspace, _chunkwriter);
apetushkov@9858 336 synchronize_epoch();
apetushkov@9858 337 return processed;
apetushkov@9858 338 }
apetushkov@9858 339
apetushkov@9858 340 size_t JfrCheckpointManager::write_epoch_transition_mspace() {
apetushkov@9858 341 return write_mspace_exclusive(_epoch_transition_mspace, _chunkwriter);
apetushkov@9858 342 }
apetushkov@9858 343
apetushkov@9858 344 typedef DiscardOp<DefaultDiscarder<JfrBuffer> > DiscardOperation;
apetushkov@9858 345 size_t JfrCheckpointManager::clear() {
apetushkov@9858 346 DiscardOperation discarder(mutexed); // mutexed discard mode
apetushkov@9858 347 process_free_list(discarder, _free_list_mspace);
apetushkov@9858 348 process_free_list(discarder, _epoch_transition_mspace);
apetushkov@9858 349 synchronize_epoch();
apetushkov@9858 350 return discarder.processed();
apetushkov@9858 351 }
apetushkov@9858 352
apetushkov@9858 353 size_t JfrCheckpointManager::write_types() {
apetushkov@9858 354 JfrCheckpointWriter writer(false, true, Thread::current());
apetushkov@9858 355 JfrTypeManager::write_types(writer);
apetushkov@9858 356 return writer.used_size();
apetushkov@9858 357 }
apetushkov@9858 358
apetushkov@9858 359 size_t JfrCheckpointManager::write_safepoint_types() {
apetushkov@9858 360 // this is also a "flushpoint"
apetushkov@9858 361 JfrCheckpointWriter writer(true, true, Thread::current());
apetushkov@9858 362 JfrTypeManager::write_safepoint_types(writer);
apetushkov@9858 363 return writer.used_size();
apetushkov@9858 364 }
apetushkov@9858 365
apetushkov@9858 366 void JfrCheckpointManager::write_type_set() {
apetushkov@9858 367 JfrTypeManager::write_type_set();
apetushkov@9858 368 }
apetushkov@9858 369
apetushkov@9858 370 void JfrCheckpointManager::write_type_set_for_unloaded_classes() {
apetushkov@9858 371 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint!");
apetushkov@9858 372 JfrTypeManager::write_type_set_for_unloaded_classes();
apetushkov@9858 373 }
apetushkov@9858 374
apetushkov@9858 375 void JfrCheckpointManager::create_thread_checkpoint(JavaThread* jt) {
apetushkov@9858 376 JfrTypeManager::create_thread_checkpoint(jt);
apetushkov@9858 377 }
apetushkov@9858 378
apetushkov@9858 379 void JfrCheckpointManager::write_thread_checkpoint(JavaThread* jt) {
apetushkov@9858 380 JfrTypeManager::write_thread_checkpoint(jt);
apetushkov@9858 381 }

mercurial