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