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::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 = JfrTicks::now(); 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 JfrTicks& ObjectSampler::last_sweep() const { 191 return _last_sweep; 192 }