Path: blob/master/src/hotspot/share/services/mallocTracker.cpp
41145 views
/*1* Copyright (c) 2014, 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*/23#include "precompiled.hpp"2425#include "services/mallocSiteTable.hpp"26#include "services/mallocTracker.hpp"27#include "services/mallocTracker.inline.hpp"28#include "services/memTracker.hpp"2930size_t MallocMemorySummary::_snapshot[CALC_OBJ_SIZE_IN_TYPE(MallocMemorySnapshot, size_t)];3132#ifdef ASSERT33void MemoryCounter::update_peak_count(size_t count) {34size_t peak_cnt = peak_count();35while (peak_cnt < count) {36size_t old_cnt = Atomic::cmpxchg(&_peak_count, peak_cnt, count, memory_order_relaxed);37if (old_cnt != peak_cnt) {38peak_cnt = old_cnt;39}40}41}4243void MemoryCounter::update_peak_size(size_t sz) {44size_t peak_sz = peak_size();45while (peak_sz < sz) {46size_t old_sz = Atomic::cmpxchg(&_peak_size, peak_sz, sz, memory_order_relaxed);47if (old_sz != peak_sz) {48peak_sz = old_sz;49}50}51}5253size_t MemoryCounter::peak_count() const {54return Atomic::load(&_peak_count);55}5657size_t MemoryCounter::peak_size() const {58return Atomic::load(&_peak_size);59}60#endif6162// Total malloc'd memory amount63size_t MallocMemorySnapshot::total() const {64size_t amount = 0;65for (int index = 0; index < mt_number_of_types; index ++) {66amount += _malloc[index].malloc_size();67}68amount += _tracking_header.size() + total_arena();69return amount;70}7172// Total malloc'd memory used by arenas73size_t MallocMemorySnapshot::total_arena() const {74size_t amount = 0;75for (int index = 0; index < mt_number_of_types; index ++) {76amount += _malloc[index].arena_size();77}78return amount;79}8081// Make adjustment by subtracting chunks used by arenas82// from total chunks to get total free chunk size83void MallocMemorySnapshot::make_adjustment() {84size_t arena_size = total_arena();85int chunk_idx = NMTUtil::flag_to_index(mtChunk);86_malloc[chunk_idx].record_free(arena_size);87}888990void MallocMemorySummary::initialize() {91assert(sizeof(_snapshot) >= sizeof(MallocMemorySnapshot), "Sanity Check");92// Uses placement new operator to initialize static area.93::new ((void*)_snapshot)MallocMemorySnapshot();94}9596void MallocHeader::release() const {97// Tracking already shutdown, no housekeeping is needed anymore98if (MemTracker::tracking_level() <= NMT_minimal) return;99100MallocMemorySummary::record_free(size(), flags());101MallocMemorySummary::record_free_malloc_header(sizeof(MallocHeader));102if (MemTracker::tracking_level() == NMT_detail) {103MallocSiteTable::deallocation_at(size(), _bucket_idx, _pos_idx);104}105}106107bool MallocHeader::record_malloc_site(const NativeCallStack& stack, size_t size,108size_t* bucket_idx, size_t* pos_idx, MEMFLAGS flags) const {109bool ret = MallocSiteTable::allocation_at(stack, size, bucket_idx, pos_idx, flags);110111// Something went wrong, could be OOM or overflow malloc site table.112// We want to keep tracking data under OOM circumstance, so transition to113// summary tracking.114if (!ret) {115MemTracker::transition_to(NMT_summary);116}117return ret;118}119120bool MallocHeader::get_stack(NativeCallStack& stack) const {121return MallocSiteTable::access_stack(stack, _bucket_idx, _pos_idx);122}123124bool MallocTracker::initialize(NMT_TrackingLevel level) {125if (level >= NMT_summary) {126MallocMemorySummary::initialize();127}128129if (level == NMT_detail) {130return MallocSiteTable::initialize();131}132return true;133}134135bool MallocTracker::transition(NMT_TrackingLevel from, NMT_TrackingLevel to) {136assert(from != NMT_off, "Can not transition from off state");137assert(to != NMT_off, "Can not transition to off state");138assert (from != NMT_minimal, "cannot transition from minimal state");139140if (from == NMT_detail) {141assert(to == NMT_minimal || to == NMT_summary, "Just check");142MallocSiteTable::shutdown();143}144return true;145}146147// Record a malloc memory allocation148void* MallocTracker::record_malloc(void* malloc_base, size_t size, MEMFLAGS flags,149const NativeCallStack& stack, NMT_TrackingLevel level) {150assert(level != NMT_off, "precondition");151void* memblock; // the address for user data152MallocHeader* header = NULL;153154if (malloc_base == NULL) {155return NULL;156}157158// Uses placement global new operator to initialize malloc header159160header = ::new (malloc_base)MallocHeader(size, flags, stack, level);161memblock = (void*)((char*)malloc_base + sizeof(MallocHeader));162163// The alignment check: 8 bytes alignment for 32 bit systems.164// 16 bytes alignment for 64-bit systems.165assert(((size_t)memblock & (sizeof(size_t) * 2 - 1)) == 0, "Alignment check");166167#ifdef ASSERT168if (level > NMT_minimal) {169// Read back170assert(get_size(memblock) == size, "Wrong size");171assert(get_flags(memblock) == flags, "Wrong flags");172}173#endif174175return memblock;176}177178void* MallocTracker::record_free(void* memblock) {179assert(MemTracker::tracking_level() != NMT_off && memblock != NULL, "precondition");180MallocHeader* header = malloc_header(memblock);181header->release();182return (void*)header;183}184185186