Path: blob/master/src/hotspot/share/jfr/utilities/jfrAllocation.cpp
41149 views
/*1* Copyright (c) 2014, 2020, 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 "logging/log.hpp"28#include "memory/allocation.inline.hpp"29#include "runtime/atomic.hpp"30#include "runtime/vm_version.hpp"31#include "services/memTracker.hpp"32#include "utilities/debug.hpp"33#include "utilities/macros.hpp"34#include "utilities/nativeCallStack.hpp"3536#ifdef ASSERT37static jlong atomic_add_jlong(jlong value, jlong volatile* const dest) {38assert(VM_Version::supports_cx8(), "unsupported");39jlong compare_value;40jlong exchange_value;41do {42compare_value = *dest;43exchange_value = compare_value + value;44} while (Atomic::cmpxchg(dest, compare_value, exchange_value) != compare_value);45return exchange_value;46}4748// debug statistics49static volatile jlong _allocated_bytes = 0;50static volatile jlong _deallocated_bytes = 0;51static volatile jlong _live_set_bytes = 0;5253static void add(size_t alloc_size) {54if (!JfrRecorder::is_created()) {55const jlong total_allocated = atomic_add_jlong((jlong)alloc_size, &_allocated_bytes);56const jlong current_live_set = atomic_add_jlong((jlong)alloc_size, &_live_set_bytes);57log_trace(jfr, system)("Allocation: [" SIZE_FORMAT "] bytes", alloc_size);58log_trace(jfr, system)("Total alloc [" JLONG_FORMAT "] bytes", total_allocated);59log_trace(jfr, system)("Liveset: [" JLONG_FORMAT "] bytes", current_live_set);60}61}6263static void subtract(size_t dealloc_size) {64if (!JfrRecorder::is_created()) {65const jlong total_deallocated = atomic_add_jlong((jlong)dealloc_size, &_deallocated_bytes);66const jlong current_live_set = atomic_add_jlong(((jlong)dealloc_size * -1), &_live_set_bytes);67log_trace(jfr, system)("Deallocation: [" SIZE_FORMAT "] bytes", dealloc_size);68log_trace(jfr, system)("Total dealloc [" JLONG_FORMAT "] bytes", total_deallocated);69log_trace(jfr, system)("Liveset: [" JLONG_FORMAT "] bytes", current_live_set);70}71}7273static void hook_memory_deallocation(size_t dealloc_size) {74subtract(dealloc_size);75}76#endif // ASSERT7778static void hook_memory_allocation(const char* allocation, size_t alloc_size) {79if (NULL == allocation) {80if (!JfrRecorder::is_created()) {81log_warning(jfr, system)("Memory allocation failed for size [" SIZE_FORMAT "] bytes", alloc_size);82return;83} else {84// after critical startup, fail as by default85vm_exit_out_of_memory(alloc_size, OOM_MALLOC_ERROR, "AllocateHeap");86}87}88debug_only(add(alloc_size));89}9091void JfrCHeapObj::on_memory_allocation(const void* allocation, size_t size) {92hook_memory_allocation((const char*)allocation, size);93}9495void* JfrCHeapObj::operator new(size_t size) throw() {96return operator new(size, std::nothrow);97}9899void* JfrCHeapObj::operator new (size_t size, const std::nothrow_t& nothrow_constant) throw() {100void* const memory = CHeapObj<mtTracing>::operator new(size, nothrow_constant, CALLER_PC);101hook_memory_allocation((const char*)memory, size);102return memory;103}104105void* JfrCHeapObj::operator new [](size_t size) throw() {106return operator new[](size, std::nothrow);107}108109void* JfrCHeapObj::operator new [](size_t size, const std::nothrow_t& nothrow_constant) throw() {110void* const memory = CHeapObj<mtTracing>::operator new[](size, nothrow_constant, CALLER_PC);111hook_memory_allocation((const char*)memory, size);112return memory;113}114115void JfrCHeapObj::operator delete(void* p, size_t size) {116debug_only(hook_memory_deallocation(size);)117CHeapObj<mtTracing>::operator delete(p);118}119120void JfrCHeapObj::operator delete[](void* p, size_t size) {121debug_only(hook_memory_deallocation(size);)122CHeapObj<mtTracing>::operator delete[](p);123}124125char* JfrCHeapObj::realloc_array(char* old, size_t size) {126char* const memory = ReallocateHeap(old, size, mtTracing, AllocFailStrategy::RETURN_NULL);127hook_memory_allocation(memory, size);128return memory;129}130131void JfrCHeapObj::free(void* p, size_t size) {132debug_only(hook_memory_deallocation(size);)133FreeHeap(p);134}135136char* JfrCHeapObj::allocate_array_noinline(size_t elements, size_t element_size) {137return AllocateHeap(elements * element_size, mtTracing, CALLER_PC, AllocFailStrategy::RETURN_NULL);138}139140141