src/hotspot/share/jfr/leakprofiler/emitEventOperation.cpp
author iignatyev
Mon, 10 Dec 2018 11:04:55 -0800
changeset 52934 8deeb7bba516
parent 52850 f527b24990d7
child 53013 c8b2a408628b
permissions -rw-r--r--
8214917: CTW testlibrary shouldn't ignore errors raised by the library itself Reviewed-by: kvn, roland
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
     1
/*
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
     2
 * Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved.
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
     4
 *
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
     7
 * published by the Free Software Foundation.
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
     8
 *
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    13
 * accompanied this code).
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    14
 *
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    18
 *
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    21
 * questions.
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    22
 *
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    23
 */
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    24
#include "precompiled.hpp"
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    25
#include "gc/shared/collectedHeap.hpp"
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    26
#include "jfr/jfrEvents.hpp"
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    27
#include "jfr/leakprofiler/utilities/granularTimer.hpp"
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    28
#include "jfr/leakprofiler/chains/rootSetClosure.hpp"
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    29
#include "jfr/leakprofiler/chains/edge.hpp"
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    30
#include "jfr/leakprofiler/chains/edgeQueue.hpp"
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    31
#include "jfr/leakprofiler/chains/edgeStore.hpp"
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    32
#include "jfr/leakprofiler/chains/bitset.hpp"
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    33
#include "jfr/leakprofiler/sampling/objectSample.hpp"
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    34
#include "jfr/leakprofiler/leakProfiler.hpp"
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    35
#include "jfr/leakprofiler/checkpoint/objectSampleCheckpoint.hpp"
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    36
#include "jfr/leakprofiler/sampling/objectSampler.hpp"
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    37
#include "jfr/leakprofiler/emitEventOperation.hpp"
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    38
#include "jfr/leakprofiler/chains/bfsClosure.hpp"
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    39
#include "jfr/leakprofiler/chains/dfsClosure.hpp"
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    40
#include "jfr/leakprofiler/chains/objectSampleMarker.hpp"
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    41
#include "jfr/recorder/checkpoint/jfrCheckpointWriter.hpp"
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    42
#include "jfr/support/jfrThreadId.hpp"
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    43
#include "logging/log.hpp"
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    44
#include "memory/resourceArea.hpp"
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    45
#include "memory/universe.hpp"
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    46
#include "oops/markOop.hpp"
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    47
#include "oops/oop.inline.hpp"
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    48
#include "runtime/safepoint.hpp"
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    49
#include "runtime/vmThread.hpp"
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    50
#include "utilities/globalDefinitions.hpp"
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    51
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    52
/* The EdgeQueue is backed by directly managed virtual memory.
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    53
 * We will attempt to dimension an initial reservation
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    54
 * in proportion to the size of the heap (represented by heap_region).
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    55
 * Initial memory reservation: 5% of the heap OR at least 32 Mb
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    56
 * Commit ratio: 1 : 10 (subject to allocation granularties)
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    57
 */
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    58
static size_t edge_queue_memory_reservation(const MemRegion& heap_region) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    59
  const size_t memory_reservation_bytes = MAX2(heap_region.byte_size() / 20, 32*M);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    60
  assert(memory_reservation_bytes >= (size_t)32*M, "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    61
  return memory_reservation_bytes;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    62
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    63
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    64
static size_t edge_queue_memory_commit_size(size_t memory_reservation_bytes) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    65
  const size_t memory_commit_block_size_bytes = memory_reservation_bytes / 10;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    66
  assert(memory_commit_block_size_bytes >= (size_t)3*M, "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    67
  return memory_commit_block_size_bytes;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    68
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    69
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    70
static void log_edge_queue_summary(const EdgeQueue& edge_queue) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    71
  log_trace(jfr, system)("EdgeQueue reserved size total: " SIZE_FORMAT " [KB]", edge_queue.reserved_size() / K);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    72
  log_trace(jfr, system)("EdgeQueue edges total: " SIZE_FORMAT, edge_queue.top());
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    73
  log_trace(jfr, system)("EdgeQueue liveset total: " SIZE_FORMAT " [KB]", edge_queue.live_set() / K);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    74
  if (edge_queue.reserved_size() > 0) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    75
    log_trace(jfr, system)("EdgeQueue commit reserve ratio: %f\n",
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    76
      ((double)edge_queue.live_set() / (double)edge_queue.reserved_size()));
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    77
  }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    78
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    79
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    80
void EmitEventOperation::doit() {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    81
  assert(LeakProfiler::is_running(), "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    82
  _object_sampler = LeakProfiler::object_sampler();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    83
  assert(_object_sampler != NULL, "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    84
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    85
  _vm_thread = VMThread::vm_thread();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    86
  assert(_vm_thread == Thread::current(), "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    87
  _vm_thread_local = _vm_thread->jfr_thread_local();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    88
  assert(_vm_thread_local != NULL, "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    89
  assert(_vm_thread->jfr_thread_local()->thread_id() == JFR_THREAD_ID(_vm_thread), "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    90
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    91
  // The VM_Operation::evaluate() which invoked doit()
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    92
  // contains a top level ResourceMark
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    93
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    94
  // save the original markWord for the potential leak objects
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    95
  // to be restored on function exit
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    96
  ObjectSampleMarker marker;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    97
  if (ObjectSampleCheckpoint::mark(marker, _emit_all) == 0) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    98
    return;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    99
  }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   100
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   101
  EdgeStore edge_store;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   102
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   103
  GranularTimer::start(_cutoff_ticks, 1000000);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   104
  if (_cutoff_ticks <= 0) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   105
    // no chains
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   106
    write_events(&edge_store);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   107
    return;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   108
  }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   109
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   110
  assert(_cutoff_ticks > 0, "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   111
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   112
  // The bitset used for marking is dimensioned as a function of the heap size
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   113
  const MemRegion heap_region = Universe::heap()->reserved_region();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   114
  BitSet mark_bits(heap_region);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   115
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   116
  // The edge queue is dimensioned as a fraction of the heap size
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   117
  const size_t edge_queue_reservation_size = edge_queue_memory_reservation(heap_region);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   118
  EdgeQueue edge_queue(edge_queue_reservation_size, edge_queue_memory_commit_size(edge_queue_reservation_size));
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   119
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   120
  // The initialize() routines will attempt to reserve and allocate backing storage memory.
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   121
  // Failure to accommodate will render root chain processing impossible.
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   122
  // As a fallback on failure, just write out the existing samples, flat, without chains.
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   123
  if (!(mark_bits.initialize() && edge_queue.initialize())) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   124
    log_warning(jfr)("Unable to allocate memory for root chain processing");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   125
    write_events(&edge_store);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   126
    return;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   127
  }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   128
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   129
  // necessary condition for attempting a root set iteration
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   130
  Universe::heap()->ensure_parsability(false);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   131
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   132
  RootSetClosure::add_to_queue(&edge_queue);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   133
  if (edge_queue.is_full()) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   134
    // Pathological case where roots don't fit in queue
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   135
    // Do a depth-first search, but mark roots first
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   136
    // to avoid walking sideways over roots
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   137
    DFSClosure::find_leaks_from_root_set(&edge_store, &mark_bits);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   138
  } else {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   139
    BFSClosure bfs(&edge_queue, &edge_store, &mark_bits);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   140
    bfs.process();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   141
  }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   142
  GranularTimer::stop();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   143
  write_events(&edge_store);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   144
  log_edge_queue_summary(edge_queue);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   145
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   146
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   147
int EmitEventOperation::write_events(EdgeStore* edge_store) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   148
  assert(_object_sampler != NULL, "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   149
  assert(edge_store != NULL, "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   150
  assert(_vm_thread != NULL, "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   151
  assert(_vm_thread_local != NULL, "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   152
  assert(SafepointSynchronize::is_at_safepoint(), "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   153
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   154
  // save thread id in preparation for thread local trace data manipulations
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   155
  const traceid vmthread_id = _vm_thread_local->thread_id();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   156
  assert(_vm_thread_local->thread_id() == JFR_THREAD_ID(_vm_thread), "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   157
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   158
  const jlong last_sweep = _emit_all ? max_jlong : _object_sampler->last_sweep().value();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   159
  int count = 0;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   160
52850
f527b24990d7 8205516: JFR tool
egahlin
parents: 50880
diff changeset
   161
  const ObjectSample* current = _object_sampler->first();
f527b24990d7 8205516: JFR tool
egahlin
parents: 50880
diff changeset
   162
  while (current != NULL) {
f527b24990d7 8205516: JFR tool
egahlin
parents: 50880
diff changeset
   163
    ObjectSample* prev = current->prev();
f527b24990d7 8205516: JFR tool
egahlin
parents: 50880
diff changeset
   164
    if (current->is_alive_and_older_than(last_sweep)) {
f527b24990d7 8205516: JFR tool
egahlin
parents: 50880
diff changeset
   165
      write_event(current, edge_store);
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   166
      ++count;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   167
    }
52850
f527b24990d7 8205516: JFR tool
egahlin
parents: 50880
diff changeset
   168
    current = prev;
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   169
  }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   170
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   171
  // restore thread local stack trace and thread id
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   172
  _vm_thread_local->set_thread_id(vmthread_id);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   173
  _vm_thread_local->clear_cached_stack_trace();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   174
  assert(_vm_thread_local->thread_id() == JFR_THREAD_ID(_vm_thread), "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   175
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   176
  if (count > 0) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   177
    // serialize assoicated checkpoints
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   178
    ObjectSampleCheckpoint::write(edge_store, _emit_all, _vm_thread);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   179
  }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   180
  return count;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   181
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   182
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   183
static int array_size(const oop object) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   184
  assert(object != NULL, "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   185
  if (object->is_array()) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   186
    return arrayOop(object)->length();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   187
  }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   188
  return -1;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   189
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   190
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   191
void EmitEventOperation::write_event(const ObjectSample* sample, EdgeStore* edge_store) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   192
  assert(sample != NULL, "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   193
  assert(!sample->is_dead(), "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   194
  assert(edge_store != NULL, "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   195
  assert(_vm_thread_local != NULL, "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   196
  const oop* object_addr = sample->object_addr();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   197
  assert(*object_addr != NULL, "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   198
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   199
  const Edge* edge = (const Edge*)(*object_addr)->mark();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   200
  traceid gc_root_id = 0;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   201
  if (edge == NULL) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   202
    // In order to dump out a representation of the event
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   203
    // even though it was not reachable / too long to reach,
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   204
    // we need to register a top level edge for this object
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   205
    Edge e(NULL, object_addr);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   206
    edge_store->add_chain(&e, 1);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   207
    edge = (const Edge*)(*object_addr)->mark();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   208
  } else {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   209
    gc_root_id = edge_store->get_root_id(edge);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   210
  }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   211
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   212
  assert(edge != NULL, "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   213
  assert(edge->pointee() == *object_addr, "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   214
  const traceid object_id = edge_store->get_id(edge);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   215
  assert(object_id != 0, "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   216
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   217
  EventOldObjectSample e(UNTIMED);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   218
  e.set_starttime(GranularTimer::start_time());
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   219
  e.set_endtime(GranularTimer::end_time());
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   220
  e.set_allocationTime(sample->allocation_time());
50880
e1117321adaf 8197425: Liveset information for Old Object sample event
egahlin
parents: 50113
diff changeset
   221
  e.set_lastKnownHeapUsage(sample->heap_used_at_last_gc());
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   222
  e.set_object(object_id);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   223
  e.set_arrayElements(array_size(*object_addr));
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   224
  e.set_root(gc_root_id);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   225
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   226
  // Temporarily assigning both the stack trace id and thread id
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   227
  // onto the thread local data structure of the VMThread (for the duration
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   228
  // of the commit() call). This trick provides a means to override
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   229
  // the event generation mechanism by injecting externally provided id's.
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   230
  // Here, in particular, this allows us to emit an old object event
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   231
  // supplying information from where the actual sampling occurred.
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   232
  _vm_thread_local->set_cached_stack_trace_id(sample->stack_trace_id());
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   233
  assert(sample->has_thread(), "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   234
  _vm_thread_local->set_thread_id(sample->thread_id());
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   235
  e.commit();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   236
}