Path: blob/master/src/hotspot/share/jfr/support/jfrObjectAllocationSample.cpp
41149 views
/*1* Copyright (c) 2020, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "gc/shared/threadLocalAllocBuffer.inline.hpp"26#include "gc/shared/tlab_globals.hpp"27#include "jfr/jfrEvents.hpp"28#include "jfr/support/jfrObjectAllocationSample.hpp"29#include "utilities/globalDefinitions.hpp"3031static THREAD_LOCAL int64_t _last_allocated_bytes = 0;3233inline void send_allocation_sample(const Klass* klass, int64_t allocated_bytes) {34assert(allocated_bytes > 0, "invariant");35EventObjectAllocationSample event;36if (event.should_commit()) {37const size_t weight = allocated_bytes - _last_allocated_bytes;38assert(weight > 0, "invariant");39event.set_objectClass(klass);40event.set_weight(weight);41event.commit();42_last_allocated_bytes = allocated_bytes;43}44}4546inline bool send_allocation_sample_with_result(const Klass* klass, int64_t allocated_bytes) {47assert(allocated_bytes > 0, "invariant");48EventObjectAllocationSample event;49if (event.should_commit()) {50const size_t weight = allocated_bytes - _last_allocated_bytes;51assert(weight > 0, "invariant");52event.set_objectClass(klass);53event.set_weight(weight);54event.commit();55_last_allocated_bytes = allocated_bytes;56return true;57}58return false;59}6061inline intptr_t estimate_tlab_size_bytes(Thread* thread) {62const size_t desired_tlab_size_bytes = thread->tlab().desired_size() * HeapWordSize;63const size_t alignment_reserve_bytes = thread->tlab().alignment_reserve_in_bytes();64assert(desired_tlab_size_bytes > alignment_reserve_bytes, "invariant");65return static_cast<intptr_t>(desired_tlab_size_bytes - alignment_reserve_bytes);66}6768inline int64_t load_allocated_bytes(Thread* thread) {69assert(thread != NULL, "invariant");70const int64_t allocated_bytes = thread->allocated_bytes();71if (allocated_bytes < _last_allocated_bytes) {72// A hw thread can detach and reattach to the VM, and when it does,73// it gets a new JavaThread representation. The thread local variable74// tracking _last_allocated_bytes is mapped to the existing hw thread,75// so it needs to be reset.76_last_allocated_bytes = 0;77}78return allocated_bytes == _last_allocated_bytes ? 0 : allocated_bytes;79}8081// To avoid large objects from being undersampled compared to the regular TLAB samples,82// the data amount is normalized as if it was a TLAB, giving a number of TLAB sampling attempts to the large object.83static void normalize_as_tlab_and_send_allocation_samples(const Klass* klass, intptr_t obj_alloc_size_bytes, Thread* thread) {84const int64_t allocated_bytes = load_allocated_bytes(thread);85assert(allocated_bytes > 0, "invariant"); // obj_alloc_size_bytes is already attributed to allocated_bytes at this point.86if (!UseTLAB) {87send_allocation_sample(klass, allocated_bytes);88return;89}90const intptr_t tlab_size_bytes = estimate_tlab_size_bytes(thread);91if (allocated_bytes - _last_allocated_bytes < tlab_size_bytes) {92return;93}94assert(obj_alloc_size_bytes > 0, "invariant");95do {96if (send_allocation_sample_with_result(klass, allocated_bytes)) {97return;98}99obj_alloc_size_bytes -= tlab_size_bytes;100} while (obj_alloc_size_bytes > 0);101}102103void JfrObjectAllocationSample::send_event(const Klass* klass, size_t alloc_size, bool outside_tlab, Thread* thread) {104if (outside_tlab) {105normalize_as_tlab_and_send_allocation_samples(klass, static_cast<intptr_t>(alloc_size), thread);106return;107}108const int64_t allocated_bytes = load_allocated_bytes(thread);109if (allocated_bytes == 0) {110return;111}112send_allocation_sample(klass, allocated_bytes);113}114115116