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