Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/jfr/utilities/jfrAllocation.cpp
38920 views
/*1* Copyright (c) 2014, 2018, 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 "jfr/recorder/jfrRecorder.hpp"26#include "jfr/utilities/jfrAllocation.hpp"27#include "jfr/utilities/jfrTypes.hpp"28#include "memory/allocation.inline.hpp"29#include "runtime/atomic.inline.hpp"30#include "runtime/orderAccess.inline.hpp"31#include "runtime/vm_version.hpp"32#include "runtime/mutexLocker.hpp"33#include "utilities/debug.hpp"34#include "utilities/macros.hpp"35#include "utilities/nativeCallStack.hpp"3637jlong atomic_add_jlong(jlong value, jlong volatile* const dest) {38jlong compare_value;39jlong exchange_value;40#ifndef SUPPORTS_NATIVE_CX841if (!VM_Version::supports_cx8()) {42MutexLockerEx mu(JfrCounters_lock, Mutex::_no_safepoint_check_flag);43return *dest += value;44}45#endif46do {47compare_value = OrderAccess::load_acquire(dest);48exchange_value = compare_value + value;49} while (Atomic::cmpxchg(exchange_value, dest, compare_value) != compare_value);50return exchange_value;51}5253#ifdef ASSERT5455// debug statistics56static volatile jlong _allocated_bytes = 0;57static volatile jlong _deallocated_bytes = 0;58static volatile jlong _live_set_bytes = 0;5960static void add(size_t alloc_size) {61if (!JfrRecorder::is_created()) {62const jlong total_allocated = atomic_add_jlong((jlong)alloc_size, &_allocated_bytes);63const jlong current_live_set = atomic_add_jlong((jlong)alloc_size, &_live_set_bytes);64if (LogJFR && Verbose) tty->print_cr("Allocation: [" SIZE_FORMAT "] bytes", alloc_size);65if (LogJFR && Verbose) tty->print_cr("Total alloc [" JLONG_FORMAT "] bytes", total_allocated);66if (LogJFR && Verbose) tty->print_cr("Liveset: [" JLONG_FORMAT "] bytes", current_live_set);67}68}6970static void subtract(size_t dealloc_size) {71if (!JfrRecorder::is_created()) {72const jlong total_deallocated = atomic_add_jlong((jlong)dealloc_size, &_deallocated_bytes);73const jlong current_live_set = atomic_add_jlong(((jlong)dealloc_size * -1), &_live_set_bytes);74if (LogJFR && Verbose) tty->print_cr("Deallocation: [" SIZE_FORMAT "] bytes", dealloc_size);75if (LogJFR && Verbose) tty->print_cr("Total dealloc [" JLONG_FORMAT "] bytes", total_deallocated);76if (LogJFR && Verbose) tty->print_cr("Liveset: [" JLONG_FORMAT "] bytes", current_live_set);77}78}7980static void hook_memory_deallocation(size_t dealloc_size) {81subtract(dealloc_size);82}83#endif // ASSERT8485static void hook_memory_allocation(const char* allocation, size_t alloc_size) {86if (NULL == allocation) {87if (!JfrRecorder::is_created()) {88if (LogJFR) tty->print_cr("Memory allocation failed for size [" SIZE_FORMAT "] bytes", alloc_size);89return;90} else {91// after critical startup, fail as by default92vm_exit_out_of_memory(alloc_size, OOM_MALLOC_ERROR, "AllocateHeap");93}94}95debug_only(add(alloc_size));96}9798void JfrCHeapObj::on_memory_allocation(const void* allocation, size_t size) {99hook_memory_allocation((const char*)allocation, size);100}101102void* JfrCHeapObj::operator new(size_t size) throw() {103return operator new(size, std::nothrow);104}105106void* JfrCHeapObj::operator new (size_t size, const std::nothrow_t& nothrow_constant) throw() {107void* const memory = CHeapObj<mtTracing>::operator new(size, nothrow_constant, CALLER_PC);108hook_memory_allocation((const char*)memory, size);109return memory;110}111112void* JfrCHeapObj::operator new [](size_t size) throw() {113return operator new[](size, std::nothrow);114}115116void* JfrCHeapObj::operator new [](size_t size, const std::nothrow_t& nothrow_constant) throw() {117void* const memory = CHeapObj<mtTracing>::operator new[](size, nothrow_constant, CALLER_PC);118hook_memory_allocation((const char*)memory, size);119return memory;120}121122void JfrCHeapObj::operator delete(void* p, size_t size) {123debug_only(hook_memory_deallocation(size);)124CHeapObj<mtTracing>::operator delete(p);125}126127void JfrCHeapObj::operator delete[](void* p, size_t size) {128debug_only(hook_memory_deallocation(size);)129CHeapObj<mtTracing>::operator delete[](p);130}131132char* JfrCHeapObj::realloc_array(char* old, size_t size) {133char* const memory = ReallocateHeap(old, size, mtTracing, AllocFailStrategy::RETURN_NULL);134hook_memory_allocation(memory, size);135return memory;136}137138void JfrCHeapObj::free(void* p, size_t size) {139debug_only(hook_memory_deallocation(size);)140FreeHeap(p);141}142143char* JfrCHeapObj::allocate_array_noinline(size_t elements, size_t element_size) {144return AllocateHeap(elements * element_size, mtTracing, CALLER_PC, AllocFailStrategy::RETURN_NULL);145}146147148