objectSampler.cpp 8.8 KB
Newer Older
A
apetushkov 已提交
1
/*
2
 * Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.
A
apetushkov 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * 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 www.oracle.com if you need additional information or have any
 * questions.
 *
 */
#include "precompiled.hpp"
#include "jfr/jfrEvents.hpp"
#include "jfr/leakprofiler/sampling/objectSample.hpp"
#include "jfr/leakprofiler/sampling/objectSampler.hpp"
#include "jfr/leakprofiler/sampling/sampleList.hpp"
#include "jfr/leakprofiler/sampling/samplePriorityQueue.hpp"
#include "jfr/recorder/jfrEventSetting.inline.hpp"
#include "jfr/recorder/checkpoint/jfrCheckpointManager.hpp"
#include "jfr/recorder/stacktrace/jfrStackTraceRepository.hpp"
#include "jfr/support/jfrThreadLocal.hpp"
#include "jfr/utilities/jfrTryLock.hpp"
#include "memory/universe.hpp"
#include "oops/oop.inline.hpp"
37 38 39
#include "runtime/atomic.hpp"
#include "runtime/orderAccess.hpp"
#include "runtime/safepoint.hpp"
A
apetushkov 已提交
40 41
#include "runtime/thread.hpp"

42 43 44 45 46 47 48
static ObjectSampler* _instance = NULL;

static ObjectSampler& instance() {
  assert(_instance != NULL, "invariant");
  return *_instance;
}

A
apetushkov 已提交
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
ObjectSampler::ObjectSampler(size_t size) :
  _priority_queue(new SamplePriorityQueue(size)),
  _list(new SampleList(size)),
  _last_sweep(JfrTicks::now()),
  _total_allocated(0),
  _threshold(0),
  _size(size),
  _dead_samples(false) {}

ObjectSampler::~ObjectSampler() {
  delete _priority_queue;
  _priority_queue = NULL;
  delete _list;
  _list = NULL;
}

65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
bool ObjectSampler::create(size_t size) {
  assert(SafepointSynchronize::is_at_safepoint(), "invariant");
  assert(_instance == NULL, "invariant");
  _instance = new ObjectSampler(size);
  return _instance != NULL;
}

bool ObjectSampler::is_created() {
  return _instance != NULL;
}

ObjectSampler* ObjectSampler::sampler() {
  assert(is_created(), "invariant");
  return _instance;
}

void ObjectSampler::destroy() {
  assert(SafepointSynchronize::is_at_safepoint(), "invariant");
  if (_instance != NULL) {
    ObjectSampler* const sampler = _instance;
    _instance = NULL;
    delete sampler;
A
apetushkov 已提交
87
  }
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
}

static volatile int _lock = 0;

ObjectSampler* ObjectSampler::acquire() {
  assert(is_created(), "invariant");
  while (Atomic::cmpxchg(1, &_lock, 0) == 1) {}
  return _instance;
}

void ObjectSampler::release() {
  assert(is_created(), "invariant");
  OrderAccess::fence();
  _lock = 0;
}
A
apetushkov 已提交
103

104 105 106 107 108 109 110 111
static traceid get_thread_id(JavaThread* thread) {
  assert(thread != NULL, "invariant");
  if (thread->threadObj() == NULL) {
    return 0;
  }
  const JfrThreadLocal* const tl = thread->jfr_thread_local();
  assert(tl != NULL, "invariant");
  if (!tl->has_thread_checkpoint()) {
A
apetushkov 已提交
112 113
    JfrCheckpointManager::create_thread_checkpoint(thread);
  }
114 115 116
  assert(tl->has_thread_checkpoint(), "invariant");
  return tl->thread_id();
}
A
apetushkov 已提交
117

118 119 120 121 122 123
// Populates the thread local stack frames, but does not add them
// to the stacktrace repository (...yet, see stacktrace_id() below)
//
void ObjectSampler::fill_stacktrace(JfrStackTrace* stacktrace, JavaThread* thread) {
  assert(stacktrace != NULL, "invariant");
  assert(thread != NULL, "invariant");
A
apetushkov 已提交
124
  if (JfrEventSetting::has_stacktrace(EventOldObjectSample::eventId)) {
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
    JfrStackTraceRepository::fill_stacktrace_for(thread, stacktrace, 0);
  }
}

// We were successful in acquiring the try lock and have been selected for adding a sample.
// Go ahead with installing our previously taken stacktrace into the stacktrace repository.
//
traceid ObjectSampler::stacktrace_id(const JfrStackTrace* stacktrace, JavaThread* thread) {
  assert(stacktrace != NULL, "invariant");
  assert(stacktrace->hash() != 0, "invariant");
  const traceid stacktrace_id = JfrStackTraceRepository::add(stacktrace, thread);
  thread->jfr_thread_local()->set_cached_stack_trace_id(stacktrace_id, stacktrace->hash());
  return stacktrace_id;
}

void ObjectSampler::sample(HeapWord* obj, size_t allocated, JavaThread* thread) {
  assert(thread != NULL, "invariant");
  assert(is_created(), "invariant");

  const traceid thread_id = get_thread_id(thread);
  if (thread_id == 0) {
    return;
A
apetushkov 已提交
147
  }
148 149 150
  const JfrThreadLocal* const tl = thread->jfr_thread_local();
  JfrStackTrace stacktrace(tl->stackframes(), tl->stackdepth());
  fill_stacktrace(&stacktrace, thread);
A
apetushkov 已提交
151

152 153
  // try enter critical section
  JfrTryLock tryLock(&_lock);
A
apetushkov 已提交
154 155 156 157 158
  if (!tryLock.has_lock()) {
    if (LogJFR && Verbose) tty->print_cr("Skipping old object sample due to lock contention");
    return;
  }

159 160 161 162 163 164 165 166 167
  instance().add(obj, allocated, thread_id, &stacktrace, thread);
}

void ObjectSampler::add(HeapWord* obj, size_t allocated, traceid thread_id, JfrStackTrace* stacktrace, JavaThread* thread) {
  assert(stacktrace != NULL, "invariant");
  assert(thread_id != 0, "invariant");
  assert(thread != NULL, "invariant");
  assert(thread->jfr_thread_local()->has_thread_checkpoint(), "invariant");

A
apetushkov 已提交
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
  if (_dead_samples) {
    scavenge();
    assert(!_dead_samples, "invariant");
  }

  _total_allocated += allocated;
  const size_t span = _total_allocated - _priority_queue->total();
  ObjectSample* sample;
  if ((size_t)_priority_queue->count() == _size) {
    assert(_list->count() == _size, "invariant");
    const ObjectSample* peek = _priority_queue->peek();
    if (peek->span() > span) {
      // quick reject, will not fit
      return;
    }
    sample = _list->reuse(_priority_queue->pop());
  } else {
    sample = _list->get();
  }

  assert(sample != NULL, "invariant");
  sample->set_thread_id(thread_id);
  sample->set_thread_checkpoint(thread->jfr_thread_local()->thread_checkpoint());

192 193 194 195
  const unsigned int stacktrace_hash = stacktrace->hash();
  if (stacktrace_hash != 0) {
    sample->set_stack_trace_id(stacktrace_id(stacktrace, thread));
    sample->set_stack_trace_hash(stacktrace_hash);
A
apetushkov 已提交
196 197 198 199 200 201 202 203 204 205
  }

  sample->set_span(allocated);
  sample->set_object((oop)obj);
  sample->set_allocated(allocated);
  sample->set_allocation_time(JfrTicks::now());
  sample->set_heap_used_at_last_gc(Universe::get_heap_used_at_last_gc());
  _priority_queue->push(sample);
}

206
void ObjectSampler::scavenge() {
A
apetushkov 已提交
207 208 209
  ObjectSample* current = _list->last();
  while (current != NULL) {
    ObjectSample* next = current->next();
210 211
    if (current->is_dead()) {
      remove_dead(current);
A
apetushkov 已提交
212 213 214
    }
    current = next;
  }
215
  _dead_samples = false;
A
apetushkov 已提交
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
}

void ObjectSampler::remove_dead(ObjectSample* sample) {
  assert(sample != NULL, "invariant");
  assert(sample->is_dead(), "invariant");
  ObjectSample* const previous = sample->prev();
  // push span on to previous
  if (previous != NULL) {
    _priority_queue->remove(previous);
    previous->add_span(sample->span());
    _priority_queue->push(previous);
  }
  _priority_queue->remove(sample);
  _list->release(sample);
}

232 233 234 235 236
void ObjectSampler::oops_do(BoolObjectClosure* is_alive, OopClosure* f) {
  assert(is_created(), "invariant");
  assert(SafepointSynchronize::is_at_safepoint(), "invariant");
  ObjectSampler& sampler = instance();
  ObjectSample* current = sampler._list->last();
A
apetushkov 已提交
237 238
  while (current != NULL) {
    ObjectSample* next = current->next();
239 240 241 242 243 244 245 246
    if (!current->is_dead()) {
      if (is_alive->do_object_b(current->object())) {
        // The weakly referenced object is alive, update pointer
        f->do_oop(const_cast<oop*>(current->object_addr()));
      } else {
        current->set_dead();
        sampler._dead_samples = true;
      }
A
apetushkov 已提交
247 248 249
    }
    current = next;
  }
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
  sampler._last_sweep = JfrTicks::now();
}

const ObjectSample* ObjectSampler::last() const {
  return _list->last();
}

const ObjectSample* ObjectSampler::first() const {
  return _list->first();
}

const ObjectSample* ObjectSampler::last_resolved() const {
  return _list->last_resolved();
}

void ObjectSampler::set_last_resolved(const ObjectSample* sample) {
  _list->set_last_resolved(sample);
A
apetushkov 已提交
267 268 269 270 271 272 273 274 275 276 277 278 279
}

int ObjectSampler::item_count() const {
  return _priority_queue->count();
}

const ObjectSample* ObjectSampler::item_at(int index) const {
  return _priority_queue->item_at(index);
}

ObjectSample* ObjectSampler::item_at(int index) {
  return const_cast<ObjectSample*>(
    const_cast<const ObjectSampler*>(this)->item_at(index)
280
                                  );
A
apetushkov 已提交
281 282 283 284 285
}

const JfrTicks& ObjectSampler::last_sweep() const {
  return _last_sweep;
}