1 /*
   2  * Copyright (c) 2014, 2019, 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 #include "precompiled.hpp"
  25 #include "jfr/recorder/jfrEventSetting.hpp"
  26 #include "jfr/recorder/access/jfrThreadData.hpp"
  27 #include "jfr/recorder/checkpoint/jfrCheckpointManager.hpp"
  28 #include "jfr/recorder/stacktrace/jfrStackTraceRepository.hpp"
  29 #include "jfr/leakprofiler/sampling/objectSample.hpp"
  30 #include "jfr/leakprofiler/sampling/objectSampler.hpp"
  31 #include "jfr/leakprofiler/sampling/sampleList.hpp"
  32 #include "jfr/leakprofiler/sampling/samplePriorityQueue.hpp"
  33 #include "jfr/utilities/jfrTryLock.hpp"
  34 #include "oops/oop.inline.hpp"
  35 #include "runtime/thread.hpp"
  36 #include "trace/tracing.hpp"
  37 
  38 ObjectSampler::ObjectSampler(size_t size) :
  39   _priority_queue(new SamplePriorityQueue(size)),
  40   _list(new SampleList(size)),
  41   _last_sweep(Tracing::time()),
  42   _total_allocated(0),
  43   _threshold(0),
  44   _size(size),
  45   _tryLock(0),
  46   _dead_samples(false) {}
  47 
  48 ObjectSampler::~ObjectSampler() {
  49   delete _priority_queue;
  50   _priority_queue = NULL;
  51   delete _list;
  52   _list = NULL;
  53 }
  54 
  55 void ObjectSampler::add(HeapWord* obj, size_t allocated, JavaThread* thread) {
  56   assert(thread != NULL, "invariant");
  57   const traceid thread_id = thread->threadObj() != NULL ? thread->trace_data()->thread_id() : 0;
  58   if (thread_id == 0) {
  59     return;
  60   }
  61   assert(thread_id != 0, "invariant");
  62 
  63   if (!thread->trace_data()->has_thread_checkpoint()) {
  64     JfrCheckpointManager::create_thread_checkpoint(thread);
  65     assert(thread->trace_data()->has_thread_checkpoint(), "invariant");
  66   }
  67 
  68   traceid stack_trace_id = 0;
  69   unsigned int stack_trace_hash = 0;
  70   if (JfrEventSetting::has_stacktrace(EventOldObjectSample::eventId)) {
  71     stack_trace_id = JfrStackTraceRepository::record(thread, 0, &stack_trace_hash);
  72     thread->trace_data()->set_cached_stack_trace_id(stack_trace_id, stack_trace_hash);
  73   }
  74 
  75   const JfrTraceTime allocation_time = Tracing::time();
  76 
  77   JfrTryLock tryLock(&_tryLock);
  78   if (!tryLock.has_lock()) {
  79     log_trace(jfr, oldobject, sampling)("Skipping old object sample due to lock contention");
  80     return;
  81   }
  82 
  83   if (_dead_samples) {
  84     scavenge();
  85     assert(!_dead_samples, "invariant");
  86   }
  87 
  88   _total_allocated += allocated;
  89   const size_t span = _total_allocated - _priority_queue->total();
  90   ObjectSample* sample;
  91   if ((size_t)_priority_queue->count() == _size) {
  92     assert(_list->count() == _size, "invariant");
  93     const ObjectSample* peek = _priority_queue->peek();
  94     if (peek->span() > span) {
  95       // quick reject, will not fit
  96       return;
  97     }
  98     sample = _list->reuse(_priority_queue->pop());
  99   } else {
 100     sample = _list->get();
 101   }
 102 
 103   assert(sample != NULL, "invariant");
 104   assert(thread_id != 0, "invariant");
 105   sample->set_thread_id(thread_id);
 106   sample->set_thread_checkpoint(thread->trace_data()->thread_checkpoint());
 107 
 108   if (stack_trace_id != 0) {
 109     sample->set_stack_trace_id(stack_trace_id);
 110     sample->set_stack_trace_hash(stack_trace_hash);
 111   }
 112 
 113   sample->set_span(allocated);
 114   sample->set_object((oop)obj);
 115   sample->set_allocated(allocated);
 116   sample->set_allocation_time(allocation_time);
 117   _priority_queue->push(sample);
 118 }
 119 
 120 const ObjectSample* ObjectSampler::last() const {
 121   return _list->last();
 122 }
 123 
 124 const ObjectSample* ObjectSampler::last_resolved() const {
 125   return _list->last_resolved();
 126 }
 127 
 128 void ObjectSampler::set_last_resolved(const ObjectSample* sample) {
 129   _list->set_last_resolved(sample);
 130 }
 131 
 132 void ObjectSampler::oops_do(BoolObjectClosure* is_alive, OopClosure* f) {
 133   ObjectSample* current = _list->last();
 134   while (current != NULL) {
 135     ObjectSample* next = current->next();
 136     if (!current->is_dead()) {
 137       if (is_alive->do_object_b(current->object())) {
 138         // The weakly referenced object is alive, update pointer
 139         f->do_oop(const_cast<oop*>(current->object_addr()));
 140       } else {
 141         current->set_dead();
 142         _dead_samples = true;
 143       }
 144     }
 145     current = next;
 146   }
 147   _last_sweep = Tracing::time();
 148 }
 149 
 150 void ObjectSampler::remove_dead(ObjectSample* sample) {
 151   assert(sample != NULL, "invariant");
 152   assert(sample->is_dead(), "invariant");
 153   ObjectSample* const previous = sample->prev();
 154   // push span on to previous
 155   if (previous != NULL) {
 156     _priority_queue->remove(previous);
 157     previous->add_span(sample->span());
 158     _priority_queue->push(previous);
 159   }
 160   _priority_queue->remove(sample);
 161   _list->release(sample);
 162 }
 163 
 164 void ObjectSampler::scavenge() {
 165   ObjectSample* current = _list->last();
 166   while (current != NULL) {
 167     ObjectSample* next = current->next();
 168     if (current->is_dead()) {
 169       remove_dead(current);
 170     }
 171     current = next;
 172   }
 173   _dead_samples = false;
 174 }
 175 
 176 int ObjectSampler::item_count() const {
 177   return _priority_queue->count();
 178 }
 179 
 180 const ObjectSample* ObjectSampler::item_at(int index) const {
 181   return _priority_queue->item_at(index);
 182 }
 183 
 184 ObjectSample* ObjectSampler::item_at(int index) {
 185   return const_cast<ObjectSample*>(
 186     const_cast<const ObjectSampler*>(this)->item_at(index)
 187                                    );
 188 }
 189 
 190 const JfrTraceTime& ObjectSampler::last_sweep() const {
 191   return _last_sweep;
 192 }