src/hotspot/share/jfr/leakprofiler/leakProfiler.cpp
author egahlin
Tue, 15 May 2018 20:24:34 +0200
changeset 50113 caf115bb98ad
child 50122 d2bcbcf0d3d9
permissions -rw-r--r--
8199712: Flight Recorder Reviewed-by: coleenp, ihse, erikj, dsamersoff, mseledtsov, egahlin, mgronlun Contributed-by: erik.gahlin@oracle.com, markus.gronlund@oracle.com
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
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    25
#include "precompiled.hpp"
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    26
#include "jfr/leakprofiler/emitEventOperation.hpp"
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    27
#include "jfr/leakprofiler/leakProfiler.hpp"
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    28
#include "jfr/leakprofiler/startOperation.hpp"
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    29
#include "jfr/leakprofiler/stopOperation.hpp"
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    30
#include "jfr/leakprofiler/sampling/objectSampler.hpp"
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    31
#include "jfr/recorder/service/jfrOptionSet.hpp"
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    32
#include "memory/iterator.hpp"
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    33
#include "oops/oop.hpp"
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    34
#include "runtime/atomic.hpp"
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    35
#include "runtime/orderAccess.inline.hpp"
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    36
#include "runtime/thread.hpp"
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    37
#include "runtime/vmThread.hpp"
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    38
#include "utilities/ostream.hpp"
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    39
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    40
// Only to be updated during safepoint
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    41
ObjectSampler* LeakProfiler::_object_sampler = NULL;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    42
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    43
static volatile jbyte suspended = 0;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    44
bool LeakProfiler::start(jint sample_count) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    45
  if (_object_sampler != NULL) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    46
    // already started
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    47
    return true;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    48
  }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    49
  // Allows user to disable leak profiler on command line by setting queue size to zero.
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    50
  if (sample_count > 0) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    51
    StartOperation op(sample_count);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    52
    VMThread::execute(&op);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    53
    return _object_sampler != NULL;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    54
  }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    55
  return false;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    56
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    57
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    58
bool LeakProfiler::stop() {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    59
  if (_object_sampler == NULL) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    60
    // already stopped/not started
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    61
    return true;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    62
  }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    63
  StopOperation op;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    64
  VMThread::execute(&op);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    65
  return _object_sampler == NULL;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    66
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    67
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    68
void LeakProfiler::emit_events(jlong cutoff_ticks, bool emit_all) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    69
  if (!is_running()) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    70
    return;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    71
  }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    72
  EmitEventOperation op(cutoff_ticks, emit_all);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    73
  VMThread::execute(&op);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    74
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    75
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    76
void LeakProfiler::oops_do(BoolObjectClosure* is_alive, OopClosure* f) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    77
  assert(SafepointSynchronize::is_at_safepoint(),
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    78
    "Leak Profiler::oops_do(...) may only be called during safepoint");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    79
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    80
  if (_object_sampler != NULL) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    81
    _object_sampler->oops_do(is_alive, f);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    82
  }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    83
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    84
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    85
void LeakProfiler::sample(HeapWord* object,
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    86
                          size_t size,
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    87
                          JavaThread* thread) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    88
  assert(is_running(), "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    89
  assert(thread != NULL, "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    90
  assert(thread->thread_state() == _thread_in_vm, "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    91
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    92
  // exclude compiler threads and code sweeper thread
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    93
  if (thread->is_hidden_from_external_view()) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    94
    return;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    95
  }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    96
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    97
  _object_sampler->add(object, size, thread);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    98
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    99
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   100
ObjectSampler* LeakProfiler::object_sampler() {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   101
  assert(is_suspended() || SafepointSynchronize::is_at_safepoint(),
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   102
    "Leak Profiler::object_sampler() may only be called during safepoint");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   103
  return _object_sampler;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   104
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   105
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   106
void LeakProfiler::set_object_sampler(ObjectSampler* object_sampler) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   107
  assert(SafepointSynchronize::is_at_safepoint(),
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   108
    "Leak Profiler::set_object_sampler() may only be called during safepoint");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   109
  _object_sampler = object_sampler;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   110
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   111
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   112
bool LeakProfiler::is_running() {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   113
  return _object_sampler != NULL && !suspended;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   114
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   115
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   116
bool LeakProfiler::is_suspended() {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   117
  return _object_sampler != NULL && suspended;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   118
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   119
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   120
void LeakProfiler::resume() {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   121
  assert(is_suspended(), "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   122
  OrderAccess::storestore();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   123
  Atomic::store((jbyte)0, &suspended);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   124
  assert(is_running(), "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   125
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   126
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   127
void LeakProfiler::suspend() {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   128
  assert(SafepointSynchronize::is_at_safepoint(), "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   129
  assert(_object_sampler != NULL, "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   130
  assert(!is_suspended(), "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   131
  suspended = (jbyte)1; // safepoint visible
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   132
}