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