1 /*
   2  * Copyright (c) 2019, 2020, 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/jfrEvents.hpp"
  27 #include "jfr/leakprofiler/chains/edgeStore.hpp"
  28 #include "jfr/leakprofiler/chains/pathToGcRootsOperation.hpp"
  29 #include "jfr/leakprofiler/checkpoint/eventEmitter.hpp"
  30 #include "jfr/leakprofiler/checkpoint/objectSampleCheckpoint.hpp"
  31 #include "jfr/leakprofiler/sampling/objectSample.hpp"
  32 #include "jfr/leakprofiler/sampling/objectSampler.hpp"
  33 #include "jfr/leakprofiler/utilities/unifiedOopRef.inline.hpp"
  34 #include "logging/log.hpp"
  35 #include "memory/resourceArea.hpp"
  36 #include "oops/markWord.hpp"
  37 #include "oops/oop.inline.hpp"
  38 #include "runtime/mutexLocker.hpp"
  39 #include "runtime/thread.inline.hpp"
  40 #include "runtime/vmThread.hpp"
  41 
  42 EventEmitter::EventEmitter(const JfrTicks& start_time, const JfrTicks& end_time) :
  43   _start_time(start_time),
  44   _end_time(end_time),
  45   _thread(Thread::current()),
  46   _jfr_thread_local(_thread->jfr_thread_local()),
  47   _thread_id(_thread->jfr_thread_local()->thread_id()) {}
  48 
  49 EventEmitter::~EventEmitter() {
  50   // restore / reset thread local stack trace and thread id
  51   _jfr_thread_local->set_thread_id(_thread_id);
  52   _jfr_thread_local->clear_cached_stack_trace();
  53 }
  54 
  55 void EventEmitter::emit(ObjectSampler* sampler, int64_t cutoff_ticks, bool emit_all) {
  56   assert(sampler != NULL, "invariant");
  57   ResourceMark rm;
  58   EdgeStore edge_store;
  59   if (cutoff_ticks <= 0) {
  60     // no reference chains
  61     MutexLocker lock(JfrStream_lock, Mutex::_no_safepoint_check_flag);
  62     // The lock is needed here to prevent the recorder thread (running flush())
  63     // from writing old object events out from the thread local buffer
  64     // before the required constant pools have been serialized.
  65     JfrTicks time_stamp = JfrTicks::now();
  66     EventEmitter emitter(time_stamp, time_stamp);
  67     emitter.write_events(sampler, &edge_store, emit_all);
  68     return;
  69   }
  70   // events emitted with reference chains require a safepoint operation
  71   PathToGcRootsOperation op(sampler, &edge_store, cutoff_ticks, emit_all);
  72   VMThread::execute(&op);
  73 }
  74 
  75 size_t EventEmitter::write_events(ObjectSampler* object_sampler, EdgeStore* edge_store, bool emit_all) {
  76   assert_locked_or_safepoint(JfrStream_lock);
  77   assert(_thread == Thread::current(), "invariant");
  78   assert(_thread->jfr_thread_local() == _jfr_thread_local, "invariant");
  79   assert(object_sampler != NULL, "invariant");
  80   assert(edge_store != NULL, "invariant");
  81 
  82   const jlong last_sweep = emit_all ? max_jlong : object_sampler->last_sweep().value();
  83   size_t count = 0;
  84 
  85   const ObjectSample* current = object_sampler->first();
  86   while (current != NULL) {
  87     ObjectSample* prev = current->prev();
  88     if (current->is_alive_and_older_than(last_sweep)) {
  89       write_event(current, edge_store);
  90       ++count;
  91     }
  92     current = prev;
  93   }
  94 
  95   if (count > 0) {
  96     // serialize associated checkpoints and potential chains
  97     ObjectSampleCheckpoint::write(object_sampler, edge_store, emit_all, _thread);
  98   }
  99   return count;
 100 }
 101 
 102 static int array_size(const oop object) {
 103   assert(object != NULL, "invariant");
 104   if (object->is_array()) {
 105     return arrayOop(object)->length();
 106   }
 107   return min_jint;
 108 }
 109 
 110 void EventEmitter::write_event(const ObjectSample* sample, EdgeStore* edge_store) {
 111   assert(sample != NULL, "invariant");
 112   assert(!sample->is_dead(), "invariant");
 113   assert(edge_store != NULL, "invariant");
 114   assert(_jfr_thread_local != NULL, "invariant");
 115 
 116   traceid gc_root_id = 0;
 117   const Edge* edge = NULL;
 118   if (SafepointSynchronize::is_at_safepoint()) {
 119     if (!sample->object()->mark().is_marked()) {
 120       edge = (const Edge*)(sample->object())->mark().to_pointer();
 121     }
 122   }
 123   if (edge == NULL) {
 124     // In order to dump out a representation of the event
 125     // even though it was not reachable / too long to reach,
 126     // we need to register a top level edge for this object.
 127     edge = edge_store->put(UnifiedOopRef::encode_in_native(sample->object_addr()));
 128   } else {
 129     gc_root_id = edge_store->gc_root_id(edge);
 130   }
 131 
 132   assert(edge != NULL, "invariant");
 133   const traceid object_id = edge_store->get_id(edge);
 134   assert(object_id != 0, "invariant");
 135 
 136   Tickspan object_age = Ticks(_start_time.value()) - sample->allocation_time();
 137 
 138   EventOldObjectSample e(UNTIMED);
 139   e.set_starttime(_start_time);
 140   e.set_endtime(_end_time);
 141   e.set_allocationTime(sample->allocation_time());
 142   e.set_objectAge(object_age);
 143   e.set_lastKnownHeapUsage(sample->heap_used_at_last_gc());
 144   e.set_object(object_id);
 145   e.set_arrayElements(array_size(edge->pointee()));
 146   e.set_root(gc_root_id);
 147 
 148   // Temporarily assigning both the stack trace id and thread id
 149   // onto the thread local data structure of the emitter thread (for the duration
 150   // of the commit() call). This trick provides a means to override
 151   // the event generation mechanism by injecting externally provided id's.
 152   // At this particular location, it allows us to emit an old object event
 153   // supplying information from where the actual sampling occurred.
 154   _jfr_thread_local->set_cached_stack_trace_id(sample->stack_trace_id());
 155   assert(sample->has_thread(), "invariant");
 156   _jfr_thread_local->set_thread_id(sample->thread_id());
 157   e.commit();
 158 }