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