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