1 /*
   2  * Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "classfile/javaClasses.inline.hpp"
  27 #include "jfr/recorder/jfrRecorder.hpp"
  28 #include "jfr/recorder/access/jfrbackend.hpp"
  29 #include "jfr/recorder/access/jfrOptionSet.hpp"
  30 #include "jfr/recorder/checkpoint/jfrCheckpointManager.hpp"
  31 #include "jfr/recorder/checkpoint/jfrCheckpointWriter.hpp"
  32 #include "jfr/recorder/checkpoint/constant/jfrConstantManager.hpp"
  33 #include "jfr/recorder/checkpoint/constant/traceid/jfrTraceIdEpoch.hpp"
  34 #include "jfr/recorder/storage/jfrMemorySpace.inline.hpp"
  35 #include "jfr/recorder/storage/jfrStorageUtils.inline.hpp"
  36 #include "jfr/recorder/repository/jfrChunkWriter.hpp"
  37 #include "jfr/utilities/jfrBigEndian.hpp"
  38 #include "memory/resourceArea.hpp"
  39 #include "runtime/mutexLocker.hpp"
  40 #include "runtime/orderAccess.inline.hpp"
  41 #include "runtime/safepoint.hpp"
  42 #include "tracefiles/traceTypes.hpp"
  43 
  44 typedef JfrCheckpointManager::Buffer* BufferPtr;
  45 
  46 static JfrCheckpointManager* _instance = NULL;
  47 
  48 JfrCheckpointManager& JfrCheckpointManager::instance() {
  49   return *_instance;
  50 }
  51 
  52 JfrCheckpointManager* JfrCheckpointManager::create(JfrChunkWriter& cw) {
  53   assert(_instance == NULL, "invariant");
  54   _instance = new JfrCheckpointManager(cw);
  55   return _instance;
  56 }
  57 
  58 void JfrCheckpointManager::destroy() {
  59   assert(_instance != NULL, "invariant");
  60   delete _instance;
  61   _instance = NULL;
  62 }
  63 
  64 JfrCheckpointManager::JfrCheckpointManager(JfrChunkWriter& cw) :
  65   _epoch_transition_list(),
  66   _free_list_mspace(NULL),
  67   _transient_mspace(NULL),
  68   _lock(NULL),
  69   _constant_manager(NULL),
  70   _service_thread(NULL),
  71   _chunkwriter(cw),
  72   _checkpoint_epoch_state(JfrTraceIdEpoch::epoch()) {}
  73 
  74 JfrCheckpointManager::~JfrCheckpointManager() {
  75   if (_free_list_mspace != NULL) {
  76     delete _free_list_mspace;
  77   }
  78   if (_transient_mspace != NULL) {
  79     delete _transient_mspace;
  80   }
  81   if (_lock != NULL) {
  82     delete _lock;
  83   }
  84   if (_constant_manager) {
  85     delete _constant_manager;
  86   }
  87   assert(_epoch_transition_list.count() == 0, "invariant");
  88 }
  89 
  90 static const size_t unlimited_mspace_size = 0;
  91 static const size_t checkpoint_buffer_cache_count = 2;
  92 static const size_t checkpoint_buffer_size = M;
  93 static const size_t limited_mspace_size = checkpoint_buffer_cache_count * checkpoint_buffer_size;
  94 
  95 static JfrCheckpointMspace* create_mspace(size_t buffer_size, size_t limit, size_t cache_count, JfrCheckpointManager* system) {
  96   JfrCheckpointMspace* mspace = new JfrCheckpointMspace(buffer_size, limit, cache_count, system);
  97   if (mspace != NULL) {
  98     mspace->initialize();
  99   }
 100   return mspace;
 101 }
 102 
 103 bool JfrCheckpointManager::initialize() {
 104   assert(_free_list_mspace == NULL, "invariant");
 105   _free_list_mspace = create_mspace(checkpoint_buffer_size, limited_mspace_size, checkpoint_buffer_cache_count, this);
 106   if (_free_list_mspace == NULL) {
 107     return false;
 108   }
 109   assert(_transient_mspace == NULL, "invariant");
 110   _transient_mspace = create_mspace((size_t)JfrOptionSet::global_buffer_size(), unlimited_mspace_size, 0, this);
 111   if (_transient_mspace == NULL) {
 112     return false;
 113   }
 114   assert(_constant_manager == NULL, "invariant");
 115   _constant_manager = new JfrConstantManager();
 116   if (_constant_manager == NULL || !_constant_manager->initialize()) {
 117     return false;
 118   }
 119   assert(_lock == NULL, "invariant");
 120   _lock = new Mutex(Monitor::leaf - 1, "Checkpoint mutex", Mutex::_allow_vm_block_flag, Monitor::_safepoint_check_never);
 121   return _lock != NULL;
 122 }
 123 
 124 bool JfrCheckpointManager::use_epoch_transition_list(const Thread* thread) const {
 125   return _service_thread != thread && OrderAccess::load_acquire(&_checkpoint_epoch_state) != JfrTraceIdEpoch::epoch();
 126 }
 127 
 128 void JfrCheckpointManager::synchronize_epoch() {
 129   assert(_checkpoint_epoch_state != JfrTraceIdEpoch::epoch(), "invariant");
 130   OrderAccess::storestore();
 131   _checkpoint_epoch_state = JfrTraceIdEpoch::epoch();
 132 }
 133 
 134 void JfrCheckpointManager::register_service_thread(const Thread* thread) {
 135   _service_thread = thread;
 136 }
 137 
 138 void JfrCheckpointManager::register_full(BufferPtr t, Thread* thread) {
 139   // nothing here at the moment
 140   assert(t->retired(), "invariant");
 141 }
 142 
 143 void JfrCheckpointManager::lock() {
 144   assert(!_lock->owned_by_self(), "invariant");
 145   _lock->lock_without_safepoint_check();
 146 }
 147 
 148 void JfrCheckpointManager::unlock() {
 149   _lock->unlock();
 150 }
 151 
 152 void JfrCheckpointManager::shift_epoch() {
 153   debug_only(const u1 current_epoch = JfrTraceIdEpoch::current();)
 154   JfrTraceIdEpoch::shift_epoch();
 155   assert(current_epoch != JfrTraceIdEpoch::current(), "invariant");
 156 }
 157 
 158 #ifdef ASSERT
 159 static void assert_free_lease(const BufferPtr buffer) {
 160   assert(buffer != NULL, "invariant");
 161   assert(buffer->acquired_by_self(), "invariant");
 162   assert(!buffer->transient(), "invariant");
 163   assert(buffer->lease(), "invariant");
 164 }
 165 
 166 static void assert_transient_lease(const BufferPtr buffer) {
 167   assert(buffer->acquired_by_self(), "invariant");
 168   assert(buffer->transient(), "invariant");
 169   assert(buffer->lease(), "invariant");
 170 }
 171 
 172 static void assert_release(const BufferPtr buffer) {
 173   assert(buffer != NULL, "invariant");
 174   assert(buffer->lease(), "invariant");
 175   assert(buffer->acquired_by_self(), "invariant");
 176 }
 177 
 178 #endif // ASSERT
 179 
 180 static const size_t lease_retry = 10;
 181 
 182 static BufferPtr lease_free(size_t size, JfrCheckpointMspace* mspace, Thread* thread) {
 183   return mspace_get_free_lease_with_retry(size, mspace, lease_retry, thread);
 184 }
 185 
 186 BufferPtr JfrCheckpointManager::lease_buffer(Thread* thread, size_t size /* 0 */) {
 187   if (instance().use_epoch_transition_list(thread)) {
 188     // epoch has changed
 189     return instance().lease_transient(size, thread);
 190   }
 191   static const size_t max_elem_size = instance()._free_list_mspace->min_elem_size(); // min is max
 192   if (size <= max_elem_size) {
 193     BufferPtr const buffer = lease_free(size, instance()._free_list_mspace, thread);
 194     if (buffer != NULL) {
 195       DEBUG_ONLY(assert_free_lease(buffer);)
 196       return buffer;
 197     }
 198   }
 199   return instance().lease_transient(size, thread);
 200 }
 201 
 202 /*
 203 * 1. If the buffer was a "lease" from the free list, release back.
 204 * 2. If the buffer is transient (temporal dynamically allocated), retire and release.
 205 *
 206 * The buffer is effectively invalidated for the thread post-return,
 207 * and the caller should take means to ensure that it is not referenced.
 208 */
 209 static void release(BufferPtr const buffer, Thread* thread) {
 210   DEBUG_ONLY(assert_release(buffer);)
 211   buffer->clear_lease();
 212   if (buffer->transient()) {
 213     buffer->set_retired();
 214   } else {
 215     buffer->release();
 216   }
 217 }
 218 
 219 BufferPtr JfrCheckpointManager::flush(BufferPtr old, size_t used, size_t requested, Thread* thread) {
 220   assert(old != NULL, "invariant");
 221   assert(old->lease(), "invariant");
 222   if (0 == requested) {
 223     // indicates a lease is being returned
 224     release(old, thread);
 225     return NULL;
 226   }
 227   // migration of in-flight information
 228   BufferPtr const new_buffer = lease_buffer(thread, used + requested);
 229   if (new_buffer != NULL) {
 230     migrate_outstanding_writes(old, new_buffer, used, requested);
 231   }
 232   release(old, thread);
 233   return new_buffer; // might be NULL
 234 }
 235 
 236 BufferPtr JfrCheckpointManager::lease_transient(size_t size, Thread* thread) {
 237   BufferPtr buffer = mspace_allocate_transient_lease(size, _transient_mspace, thread);
 238   if (buffer == NULL) {
 239     log_warning(jfr)("Unable to allocate " SIZE_FORMAT " bytes of transient memory.", size);
 240     return NULL;
 241   }
 242   DEBUG_ONLY(assert_transient_lease(buffer);)
 243   MutexLockerEx lock(_lock, Mutex::_no_safepoint_check_flag);
 244   if (use_epoch_transition_list(thread)) {
 245     _epoch_transition_list.append(buffer);
 246   } else {
 247     _transient_mspace->insert_full_tail(buffer);
 248   }
 249   return buffer;
 250 }
 251 
 252 template <typename Processor>
 253 static void process_epoch_transition_list(Processor& processor, JfrCheckpointMspace* mspace, JfrCheckpointMspace::List& list) {
 254   assert(mspace != NULL, "invariant");
 255   assert(!SafepointSynchronize::is_at_safepoint(), "invariant");
 256   if (list.count() == 0) {
 257     // nothing to do
 258     return;
 259   }
 260   // Fetch epoch transition list nodes.
 261   // No inserts will happen concurrently so we are ok outside the lock
 262   JfrCheckpointMspace::Type* buffer = list.clear();
 263   assert(list.count() == 0, "invariant");
 264   while (buffer != NULL) {
 265     assert(buffer->retired(), "invariant");
 266     assert(buffer->transient(), "invariant");
 267     JfrCheckpointMspace::Type* next = buffer->next();
 268     processor.process(buffer);
 269     mspace->deallocate(buffer);
 270     buffer = next;
 271   }
 272 }
 273 
 274 // offsets into the JfrCheckpointEntry
 275 static const juint starttime_offset = sizeof(jlong);
 276 static const juint duration_offset = starttime_offset + sizeof(jlong);
 277 static const juint flushpoint_offset = duration_offset + sizeof(jlong);
 278 static const juint constant_types_offset = flushpoint_offset + sizeof(juint);
 279 static const juint payload_offset = constant_types_offset + sizeof(juint);
 280 
 281 template <typename Return>
 282 static Return read_data(const u1* data) {
 283   return JfrBigEndian::read<Return>(data);
 284 }
 285 
 286 static jlong total_size(const u1* data) {
 287   return read_data<jlong>(data);
 288 }
 289 
 290 static jlong starttime(const u1* data) {
 291   return read_data<jlong>(data + starttime_offset);
 292 }
 293 
 294 static jlong duration(const u1* data) {
 295   return read_data<jlong>(data + duration_offset);
 296 }
 297 
 298 static bool is_flushpoint(const u1* data) {
 299   return read_data<juint>(data + flushpoint_offset) == (juint)1;
 300 }
 301 
 302 static juint number_of_constant_types(const u1* data) {
 303   return read_data<juint>(data + constant_types_offset);
 304 }
 305 
 306 static void write_checkpoint_header(JfrChunkWriter& cw, intptr_t offset_prev_cp_event, const u1* data) {
 307   cw.reserve(sizeof(u4));
 308   cw.write((u8)EVENT_CHECKPOINT);
 309   cw.write(starttime(data));
 310   cw.write(duration(data));
 311   cw.write((jlong)offset_prev_cp_event);
 312   cw.write(is_flushpoint(data));
 313   cw.write(number_of_constant_types(data));
 314 }
 315 
 316 static void write_checkpoint_content(JfrChunkWriter& cw, const u1* data, size_t size) {
 317   assert(data != NULL, "invariant");
 318   cw.write_unbuffered(data + payload_offset, size);
 319 }
 320 
 321 static size_t write_checkpoint_event(JfrChunkWriter& cw, const u1* data) {
 322   assert(data != NULL, "invariant");
 323   const intptr_t previous_checkpoint_event = cw.previous_checkpoint_offset();
 324   const intptr_t event_begin = cw.current_offset();
 325   const intptr_t offset_to_previous_checkpoint_event = 0 == previous_checkpoint_event ? 0 : previous_checkpoint_event - event_begin;
 326   const jlong total_checkpoint_size = total_size(data);
 327   write_checkpoint_header(cw, offset_to_previous_checkpoint_event, data);
 328   write_checkpoint_content(cw, data, total_checkpoint_size - sizeof(JfrCheckpointEntry));
 329   const jlong checkpoint_event_size = cw.current_offset() - event_begin;
 330   cw.write_padded_at_offset<u4>(checkpoint_event_size, event_begin);
 331   cw.set_previous_checkpoint_offset(event_begin);
 332   return (size_t)total_checkpoint_size;
 333 }
 334 
 335 static size_t write_checkpoints(JfrChunkWriter& cw, const u1* data, size_t size) {
 336   assert(cw.is_valid(), "invariant");
 337   assert(data != NULL, "invariant");
 338   assert(size > 0, "invariant");
 339   const u1* const limit = data + size;
 340   const u1* next_entry = data;
 341   size_t processed = 0;
 342   while (next_entry < limit) {
 343     const size_t checkpoint_size = write_checkpoint_event(cw, next_entry);
 344     processed += checkpoint_size;
 345     next_entry += checkpoint_size;
 346   }
 347   assert(next_entry == limit, "invariant");
 348   return processed;
 349 }
 350 
 351 template <typename T>
 352 class CheckpointWriteOp {
 353  private:
 354   JfrChunkWriter& _writer;
 355   size_t _processed;
 356  public:
 357   typedef T Type;
 358   CheckpointWriteOp(JfrChunkWriter& writer) : _writer(writer), _processed(0) {}
 359   bool write(Type* t, const u1* data, size_t size) {
 360     _processed += write_checkpoints(_writer, data, size);
 361     return true;
 362   }
 363   size_t processed() const { return _processed; }
 364 };
 365 
 366 typedef CheckpointWriteOp<JfrCheckpointMspace::Type> WriteOperation;
 367 typedef ConcurrentWriteOp<WriteOperation> ConcurrentWriteOperation;
 368 typedef MutexedReleaseOp<JfrCheckpointMspace> CheckpointReleaseOperation;
 369 typedef CompositeOperation<ConcurrentWriteOperation, CheckpointReleaseOperation> CheckpointWriteOperation;
 370 
 371 size_t JfrCheckpointManager::write() {
 372   Thread* const thread = Thread::current();
 373   WriteOperation wo(_chunkwriter);
 374   ConcurrentWriteOperation cwo(wo);
 375   CheckpointReleaseOperation cro(_free_list_mspace, thread);
 376   CheckpointWriteOperation cpwo(&cwo, &cro);
 377   assert(_free_list_mspace->is_full_empty(), "invariant");
 378   process_free_list(cpwo, _free_list_mspace);
 379   CheckpointReleaseOperation transient_release(_transient_mspace, thread);
 380   CheckpointWriteOperation transient_writer(&cwo, &transient_release);
 381   assert(_transient_mspace->is_free_empty(), "invariant");
 382   process_full_list(transient_writer, _transient_mspace);
 383   synchronize_epoch();
 384   return wo.processed();
 385 }
 386 
 387 typedef MutexedWriteOp<WriteOperation> MutexedWriteOperation;
 388 
 389 size_t JfrCheckpointManager::write_epoch_transition_list() {
 390   WriteOperation wo(_chunkwriter);
 391   MutexedWriteOperation epoch_list_writer(wo); // mutexed write mode
 392   process_epoch_transition_list(epoch_list_writer, _transient_mspace, _epoch_transition_list);
 393   return epoch_list_writer.processed();
 394 }
 395 
 396 typedef DiscardOp<DefaultDiscarder<JfrBuffer> > DiscardOperation;
 397 size_t JfrCheckpointManager::clear() {
 398   DiscardOperation discarder(concurrent); // concurrent discard mode
 399   process_full_list(discarder, _transient_mspace);
 400   process_epoch_transition_list(discarder, _transient_mspace, _epoch_transition_list);
 401   process_free_list(discarder, _free_list_mspace);
 402   synchronize_epoch();
 403   return discarder.processed();
 404 }
 405 
 406 bool JfrCheckpointManager::register_serializer(JfrConstantTypeId id, bool require_safepoint, bool permit_cache, JfrConstantSerializer* cs) {
 407   assert(cs != NULL, "invariant");
 408   return instance()._constant_manager->register_serializer(id, require_safepoint, permit_cache, cs);
 409 }
 410 
 411 size_t JfrCheckpointManager::write_constant_types() {
 412   JfrCheckpointWriter writer(false, true, Thread::current());
 413   _constant_manager->write_constants(writer);
 414   return writer.used_size();
 415 }
 416 
 417 size_t JfrCheckpointManager::write_safepoint_constant_types() {
 418   // this is also a "flushpoint"
 419   JfrCheckpointWriter writer(true, true, Thread::current());
 420   _constant_manager->write_safepoint_constants(writer);
 421   return writer.used_size();
 422 }
 423 
 424 void JfrCheckpointManager::write_constant_tag_set() {
 425   _constant_manager->write_constant_tag_set();
 426 }
 427 
 428 void JfrCheckpointManager::write_constant_tag_set_for_unloaded_classes() {
 429   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint!");
 430   instance()._constant_manager->write_constant_tag_set_for_unloaded_classes();
 431 }
 432 
 433 void JfrCheckpointManager::create_thread_checkpoint(JavaThread* jt) {
 434   instance()._constant_manager->create_thread_checkpoint(jt);
 435 }
 436 
 437 void JfrCheckpointManager::write_thread_checkpoint(JavaThread* jt) {
 438   instance()._constant_manager->write_thread_checkpoint(jt);
 439 }