view src/hotspot/share/jfr/leakprofiler/checkpoint/eventEmitter.cpp @ 55571:49102ba8cf14

8214542: JFR: Old Object Sample event slow on a deep heap in debug builds Reviewed-by: egahlin, rwestberg
author mgronlun
date Wed, 03 Jul 2019 11:34:46 +0200
children 90ead0febf56
line wrap: on
line source
 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit if you need additional information or have any
 * questions.

#include "precompiled.hpp"
#include "jfr/jfrEvents.hpp"
#include "jfr/leakprofiler/chains/edgeStore.hpp"
#include "jfr/leakprofiler/chains/pathToGcRootsOperation.hpp"
#include "jfr/leakprofiler/checkpoint/eventEmitter.hpp"
#include "jfr/leakprofiler/checkpoint/objectSampleCheckpoint.hpp"
#include "jfr/leakprofiler/sampling/objectSample.hpp"
#include "jfr/leakprofiler/sampling/objectSampler.hpp"
#include "logging/log.hpp"
#include "memory/resourceArea.hpp"
#include "oops/markOop.hpp"
#include "oops/oop.inline.hpp"
#include "runtime/thread.inline.hpp"
#include "runtime/vmThread.hpp"

EventEmitter::EventEmitter(const JfrTicks& start_time, const JfrTicks& end_time) :
  _thread_id(_thread->jfr_thread_local()->thread_id()) {}

EventEmitter::~EventEmitter() {
  // restore / reset thread local stack trace and thread id

void EventEmitter::emit(ObjectSampler* sampler, int64_t cutoff_ticks, bool emit_all) {
  assert(sampler != NULL, "invariant");

  ResourceMark rm;
  EdgeStore edge_store;
  if (cutoff_ticks <= 0) {
    // no reference chains
    JfrTicks time_stamp = JfrTicks::now();
    EventEmitter emitter(time_stamp, time_stamp);
    emitter.write_events(sampler, &edge_store, emit_all);
  // events emitted with reference chains require a safepoint operation
  PathToGcRootsOperation op(sampler, &edge_store, cutoff_ticks, emit_all);

size_t EventEmitter::write_events(ObjectSampler* object_sampler, EdgeStore* edge_store, bool emit_all) {
  assert(_thread == Thread::current(), "invariant");
  assert(_thread->jfr_thread_local() == _jfr_thread_local, "invariant");
  assert(object_sampler != NULL, "invariant");
  assert(edge_store != NULL, "invariant");

  const jlong last_sweep = emit_all ? max_jlong : object_sampler->last_sweep().value();
  size_t count = 0;

  const ObjectSample* current = object_sampler->first();
  while (current != NULL) {
    ObjectSample* prev = current->prev();
    if (current->is_alive_and_older_than(last_sweep)) {
      write_event(current, edge_store);
    current = prev;

  if (count > 0) {
    // serialize associated checkpoints and potential chains
    ObjectSampleCheckpoint::write(object_sampler, edge_store, emit_all, _thread);
  return count;

static int array_size(const oop object) {
  assert(object != NULL, "invariant");
  if (object->is_array()) {
    return arrayOop(object)->length();
  return min_jint;

void EventEmitter::write_event(const ObjectSample* sample, EdgeStore* edge_store) {
  assert(sample != NULL, "invariant");
  assert(!sample->is_dead(), "invariant");
  assert(edge_store != NULL, "invariant");
  assert(_jfr_thread_local != NULL, "invariant");

  const oop* object_addr = sample->object_addr();
  traceid gc_root_id = 0;
  const Edge* edge = NULL;
  if (SafepointSynchronize::is_at_safepoint()) {
    edge = (const Edge*)(*object_addr)->mark();
  if (edge == NULL) {
    // In order to dump out a representation of the event
    // even though it was not reachable / too long to reach,
    // we need to register a top level edge for this object.
    edge = edge_store->put(object_addr);
  } else {
    gc_root_id = edge_store->gc_root_id(edge);

  assert(edge != NULL, "invariant");
  const traceid object_id = edge_store->get_id(edge);
  assert(object_id != 0, "invariant");

  EventOldObjectSample e(UNTIMED);

  // Temporarily assigning both the stack trace id and thread id
  // onto the thread local data structure of the emitter thread (for the duration
  // of the commit() call). This trick provides a means to override
  // the event generation mechanism by injecting externally provided id's.
  // At this particular location, it allows us to emit an old object event
  // supplying information from where the actual sampling occurred.
  assert(sample->has_thread(), "invariant");