Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/services/mallocTracker.cpp
32285 views
/*1* Copyright (c) 2014, 2017, 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 "runtime/atomic.hpp"26#include "runtime/atomic.inline.hpp"27#include "services/mallocSiteTable.hpp"28#include "services/mallocTracker.hpp"29#include "services/mallocTracker.inline.hpp"30#include "services/memTracker.hpp"3132size_t MallocMemorySummary::_snapshot[CALC_OBJ_SIZE_IN_TYPE(MallocMemorySnapshot, size_t)];3334// Total malloc'd memory amount35size_t MallocMemorySnapshot::total() const {36size_t amount = 0;37for (int index = 0; index < mt_number_of_types; index ++) {38amount += _malloc[index].malloc_size();39}40amount += _tracking_header.size() + total_arena();41return amount;42}4344// Total malloc'd memory used by arenas45size_t MallocMemorySnapshot::total_arena() const {46size_t amount = 0;47for (int index = 0; index < mt_number_of_types; index ++) {48amount += _malloc[index].arena_size();49}50return amount;51}5253// Make adjustment by subtracting chunks used by arenas54// from total chunks to get total free chunck size55void MallocMemorySnapshot::make_adjustment() {56size_t arena_size = total_arena();57int chunk_idx = NMTUtil::flag_to_index(mtChunk);58_malloc[chunk_idx].record_free(arena_size);59}606162void MallocMemorySummary::initialize() {63assert(sizeof(_snapshot) >= sizeof(MallocMemorySnapshot), "Sanity Check");64// Uses placement new operator to initialize static area.65::new ((void*)_snapshot)MallocMemorySnapshot();66}6768void MallocHeader::release() const {69// Tracking already shutdown, no housekeeping is needed anymore70if (MemTracker::tracking_level() <= NMT_minimal) return;7172MallocMemorySummary::record_free(size(), flags());73MallocMemorySummary::record_free_malloc_header(sizeof(MallocHeader));74if (MemTracker::tracking_level() == NMT_detail) {75MallocSiteTable::deallocation_at(size(), _bucket_idx, _pos_idx);76}77}7879bool MallocHeader::record_malloc_site(const NativeCallStack& stack, size_t size,80size_t* bucket_idx, size_t* pos_idx, MEMFLAGS flags) const {81bool ret = MallocSiteTable::allocation_at(stack, size, bucket_idx, pos_idx, flags);8283// Something went wrong, could be OOM or overflow malloc site table.84// We want to keep tracking data under OOM circumstance, so transition to85// summary tracking.86if (!ret) {87MemTracker::transition_to(NMT_summary);88}89return ret;90}9192bool MallocHeader::get_stack(NativeCallStack& stack) const {93return MallocSiteTable::access_stack(stack, _bucket_idx, _pos_idx);94}9596bool MallocTracker::initialize(NMT_TrackingLevel level) {97if (level >= NMT_summary) {98MallocMemorySummary::initialize();99}100101if (level == NMT_detail) {102return MallocSiteTable::initialize();103}104return true;105}106107bool MallocTracker::transition(NMT_TrackingLevel from, NMT_TrackingLevel to) {108assert(from != NMT_off, "Can not transition from off state");109assert(to != NMT_off, "Can not transition to off state");110assert (from != NMT_minimal, "cannot transition from minimal state");111112if (from == NMT_detail) {113assert(to == NMT_minimal || to == NMT_summary, "Just check");114MallocSiteTable::shutdown();115}116return true;117}118119// Record a malloc memory allocation120void* MallocTracker::record_malloc(void* malloc_base, size_t size, MEMFLAGS flags,121const NativeCallStack& stack, NMT_TrackingLevel level) {122void* memblock; // the address for user data123MallocHeader* header = NULL;124125if (malloc_base == NULL) {126return NULL;127}128129// Uses placement global new operator to initialize malloc header130131if (level == NMT_off) {132return malloc_base;133}134135header = ::new (malloc_base)MallocHeader(size, flags, stack, level);136memblock = (void*)((char*)malloc_base + sizeof(MallocHeader));137138// The alignment check: 8 bytes alignment for 32 bit systems.139// 16 bytes alignment for 64-bit systems.140assert(((size_t)memblock & (sizeof(size_t) * 2 - 1)) == 0, "Alignment check");141142#ifdef ASSERT143if (level > NMT_minimal) {144// Read back145assert(get_size(memblock) == size, "Wrong size");146assert(get_flags(memblock) == flags, "Wrong flags");147}148#endif149150return memblock;151}152153void* MallocTracker::record_free(void* memblock) {154// Never turned on155if (MemTracker::tracking_level() == NMT_off ||156memblock == NULL) {157return memblock;158}159MallocHeader* header = malloc_header(memblock);160header->release();161162return (void*)header;163}164165166167168