Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/services/memReporter.cpp
32285 views
/*1* Copyright (c) 2012, 2019, 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 "memory/allocation.hpp"26#include "services/mallocTracker.hpp"27#include "services/memReporter.hpp"28#include "services/virtualMemoryTracker.hpp"29#include "utilities/globalDefinitions.hpp"3031size_t MemReporterBase::reserved_total(const MallocMemory* malloc, const VirtualMemory* vm) const {32return malloc->malloc_size() + malloc->arena_size() + vm->reserved();33}3435size_t MemReporterBase::committed_total(const MallocMemory* malloc, const VirtualMemory* vm) const {36return malloc->malloc_size() + malloc->arena_size() + vm->committed();37}3839void MemReporterBase::print_total(size_t reserved, size_t committed) const {40const char* scale = current_scale();41output()->print("reserved=" SIZE_FORMAT "%s, committed=" SIZE_FORMAT "%s",42amount_in_current_scale(reserved), scale, amount_in_current_scale(committed), scale);43}4445void MemReporterBase::print_malloc(size_t amount, size_t count, MEMFLAGS flag) const {46const char* scale = current_scale();47outputStream* out = output();48if (flag != mtNone) {49out->print("(malloc=" SIZE_FORMAT "%s type=%s",50amount_in_current_scale(amount), scale, NMTUtil::flag_to_name(flag));51} else {52out->print("(malloc=" SIZE_FORMAT "%s",53amount_in_current_scale(amount), scale);54}5556if (count > 0) {57out->print(" #" SIZE_FORMAT "", count);58}5960out->print(")");61}6263void MemReporterBase::print_virtual_memory(size_t reserved, size_t committed) const {64const char* scale = current_scale();65output()->print("(mmap: reserved=" SIZE_FORMAT "%s, committed=" SIZE_FORMAT "%s)",66amount_in_current_scale(reserved), scale, amount_in_current_scale(committed), scale);67}6869void MemReporterBase::print_malloc_line(size_t amount, size_t count) const {70output()->print("%28s", " ");71print_malloc(amount, count);72output()->print_cr(" ");73}7475void MemReporterBase::print_virtual_memory_line(size_t reserved, size_t committed) const {76output()->print("%28s", " ");77print_virtual_memory(reserved, committed);78output()->print_cr(" ");79}8081void MemReporterBase::print_arena_line(size_t amount, size_t count) const {82const char* scale = current_scale();83output()->print_cr("%27s (arena=" SIZE_FORMAT "%s #" SIZE_FORMAT ")", " ",84amount_in_current_scale(amount), scale, count);85}8687void MemReporterBase::print_virtual_memory_region(const char* type, address base, size_t size) const {88const char* scale = current_scale();89output()->print("[" PTR_FORMAT " - " PTR_FORMAT "] %s " SIZE_FORMAT "%s",90p2i(base), p2i(base + size), type, amount_in_current_scale(size), scale);91}929394void MemSummaryReporter::report() {95const char* scale = current_scale();96outputStream* out = output();97size_t total_reserved_amount = _malloc_snapshot->total() +98_vm_snapshot->total_reserved();99size_t total_committed_amount = _malloc_snapshot->total() +100_vm_snapshot->total_committed();101102// Overall total103out->print_cr("\nNative Memory Tracking:\n");104out->print("Total: ");105print_total(total_reserved_amount, total_committed_amount);106out->print("\n");107108// Summary by memory type109for (int index = 0; index < mt_number_of_types; index ++) {110MEMFLAGS flag = NMTUtil::index_to_flag(index);111// thread stack is reported as part of thread category112if (flag == mtThreadStack) continue;113MallocMemory* malloc_memory = _malloc_snapshot->by_type(flag);114VirtualMemory* virtual_memory = _vm_snapshot->by_type(flag);115116report_summary_of_type(flag, malloc_memory, virtual_memory);117}118}119120void MemSummaryReporter::report_summary_of_type(MEMFLAGS flag,121MallocMemory* malloc_memory, VirtualMemory* virtual_memory) {122123size_t reserved_amount = reserved_total (malloc_memory, virtual_memory);124size_t committed_amount = committed_total(malloc_memory, virtual_memory);125126// Count thread's native stack in "Thread" category127if (flag == mtThread) {128const VirtualMemory* thread_stack_usage =129(const VirtualMemory*)_vm_snapshot->by_type(mtThreadStack);130reserved_amount += thread_stack_usage->reserved();131committed_amount += thread_stack_usage->committed();132} else if (flag == mtNMT) {133// Count malloc headers in "NMT" category134reserved_amount += _malloc_snapshot->malloc_overhead()->size();135committed_amount += _malloc_snapshot->malloc_overhead()->size();136}137138if (amount_in_current_scale(reserved_amount) > 0) {139outputStream* out = output();140const char* scale = current_scale();141out->print("-%26s (", NMTUtil::flag_to_name(flag));142print_total(reserved_amount, committed_amount);143out->print_cr(")");144145if (flag == mtClass) {146// report class count147out->print_cr("%27s (classes #" SIZE_FORMAT ")", " ", _class_count);148} else if (flag == mtThread) {149// report thread count150out->print_cr("%27s (thread #" SIZE_FORMAT ")", " ", _malloc_snapshot->thread_count());151const VirtualMemory* thread_stack_usage =152_vm_snapshot->by_type(mtThreadStack);153out->print("%27s (stack: ", " ");154print_total(thread_stack_usage->reserved(), thread_stack_usage->committed());155out->print_cr(")");156}157158// report malloc'd memory159if (amount_in_current_scale(malloc_memory->malloc_size()) > 0) {160// We don't know how many arena chunks are in used, so don't report the count161size_t count = (flag == mtChunk) ? 0 : malloc_memory->malloc_count();162print_malloc_line(malloc_memory->malloc_size(), count);163}164165if (amount_in_current_scale(virtual_memory->reserved()) > 0) {166print_virtual_memory_line(virtual_memory->reserved(), virtual_memory->committed());167}168169if (amount_in_current_scale(malloc_memory->arena_size()) > 0) {170print_arena_line(malloc_memory->arena_size(), malloc_memory->arena_count());171}172173if (flag == mtNMT &&174amount_in_current_scale(_malloc_snapshot->malloc_overhead()->size()) > 0) {175out->print_cr("%27s (tracking overhead=" SIZE_FORMAT "%s)", " ",176amount_in_current_scale(_malloc_snapshot->malloc_overhead()->size()), scale);177}178179out->print_cr(" ");180}181}182183void MemDetailReporter::report_detail() {184// Start detail report185outputStream* out = output();186out->print_cr("Details:\n");187188report_malloc_sites();189report_virtual_memory_allocation_sites();190}191192void MemDetailReporter::report_malloc_sites() {193MallocSiteIterator malloc_itr = _baseline.malloc_sites(MemBaseline::by_size);194if (malloc_itr.is_empty()) return;195196outputStream* out = output();197198const MallocSite* malloc_site;199while ((malloc_site = malloc_itr.next()) != NULL) {200// Don't report if size is too small201if (amount_in_current_scale(malloc_site->size()) == 0)202continue;203204const NativeCallStack* stack = malloc_site->call_stack();205stack->print_on(out);206out->print("%29s", " ");207MEMFLAGS flag = malloc_site->flag();208assert((flag >= 0 && flag < (int)mt_number_of_types) && flag != mtNone,209"Must have a valid memory type");210print_malloc(malloc_site->size(), malloc_site->count(),flag);211out->print_cr("\n");212}213}214215void MemDetailReporter::report_virtual_memory_allocation_sites() {216VirtualMemorySiteIterator virtual_memory_itr =217_baseline.virtual_memory_sites(MemBaseline::by_size);218219if (virtual_memory_itr.is_empty()) return;220221outputStream* out = output();222const VirtualMemoryAllocationSite* virtual_memory_site;223224while ((virtual_memory_site = virtual_memory_itr.next()) != NULL) {225// Don't report if size is too small226if (amount_in_current_scale(virtual_memory_site->reserved()) == 0)227continue;228229const NativeCallStack* stack = virtual_memory_site->call_stack();230stack->print_on(out);231out->print("%28s (", " ");232print_total(virtual_memory_site->reserved(), virtual_memory_site->committed());233MEMFLAGS flag = virtual_memory_site->flag();234if (flag != mtNone) {235out->print(" Type=%s", NMTUtil::flag_to_name(flag));236}237out->print_cr(")\n");238}239}240241242void MemDetailReporter::report_virtual_memory_map() {243// Virtual memory map always in base address order244VirtualMemoryAllocationIterator itr = _baseline.virtual_memory_allocations();245const ReservedMemoryRegion* rgn;246247output()->print_cr("Virtual memory map:");248while ((rgn = itr.next()) != NULL) {249report_virtual_memory_region(rgn);250}251}252253void MemDetailReporter::report_virtual_memory_region(const ReservedMemoryRegion* reserved_rgn) {254assert(reserved_rgn != NULL, "NULL pointer");255256// Don't report if size is too small257if (amount_in_current_scale(reserved_rgn->size()) == 0) return;258259outputStream* out = output();260const char* scale = current_scale();261const NativeCallStack* stack = reserved_rgn->call_stack();262bool all_committed = reserved_rgn->all_committed();263const char* region_type = (all_committed ? "reserved and committed" : "reserved");264out->print_cr(" ");265print_virtual_memory_region(region_type, reserved_rgn->base(), reserved_rgn->size());266out->print(" for %s", NMTUtil::flag_to_name(reserved_rgn->flag()));267if (stack->is_empty()) {268out->print_cr(" ");269} else {270out->print_cr(" from");271stack->print_on(out, 4);272}273274if (all_committed) return;275276CommittedRegionIterator itr = reserved_rgn->iterate_committed_regions();277const CommittedMemoryRegion* committed_rgn;278while ((committed_rgn = itr.next()) != NULL) {279// Don't report if size is too small280if (amount_in_current_scale(committed_rgn->size()) == 0) continue;281stack = committed_rgn->call_stack();282out->print("\n\t");283print_virtual_memory_region("committed", committed_rgn->base(), committed_rgn->size());284if (stack->is_empty()) {285out->print_cr(" ");286} else {287out->print_cr(" from");288stack->print_on(out, 12);289}290}291}292293void MemSummaryDiffReporter::report_diff() {294const char* scale = current_scale();295outputStream* out = output();296out->print_cr("\nNative Memory Tracking:\n");297298// Overall diff299out->print("Total: ");300print_virtual_memory_diff(_current_baseline.total_reserved_memory(),301_current_baseline.total_committed_memory(), _early_baseline.total_reserved_memory(),302_early_baseline.total_committed_memory());303304out->print_cr("\n");305306// Summary diff by memory type307for (int index = 0; index < mt_number_of_types; index ++) {308MEMFLAGS flag = NMTUtil::index_to_flag(index);309// thread stack is reported as part of thread category310if (flag == mtThreadStack) continue;311diff_summary_of_type(flag, _early_baseline.malloc_memory(flag),312_early_baseline.virtual_memory(flag), _current_baseline.malloc_memory(flag),313_current_baseline.virtual_memory(flag));314}315}316317void MemSummaryDiffReporter::print_malloc_diff(size_t current_amount, size_t current_count,318size_t early_amount, size_t early_count, MEMFLAGS flags) const {319const char* scale = current_scale();320outputStream* out = output();321322out->print("malloc=" SIZE_FORMAT "%s", amount_in_current_scale(current_amount), scale);323// Report type only if it is valid324if (flags != mtNone) {325out->print(" type=%s", NMTUtil::flag_to_name(flags));326}327328long amount_diff = diff_in_current_scale(current_amount, early_amount);329if (amount_diff != 0) {330out->print(" %+ld%s", amount_diff, scale);331}332if (current_count > 0) {333out->print(" #" SIZE_FORMAT "", current_count);334if (current_count != early_count) {335out->print(" %+d", (int)(current_count - early_count));336}337}338}339340void MemSummaryDiffReporter::print_arena_diff(size_t current_amount, size_t current_count,341size_t early_amount, size_t early_count) const {342const char* scale = current_scale();343outputStream* out = output();344out->print("arena=" SIZE_FORMAT "%s", amount_in_current_scale(current_amount), scale);345if (diff_in_current_scale(current_amount, early_amount) != 0) {346out->print(" %+ld", diff_in_current_scale(current_amount, early_amount));347}348349out->print(" #" SIZE_FORMAT "", current_count);350if (current_count != early_count) {351out->print(" %+d", (int)(current_count - early_count));352}353}354355void MemSummaryDiffReporter::print_virtual_memory_diff(size_t current_reserved, size_t current_committed,356size_t early_reserved, size_t early_committed) const {357const char* scale = current_scale();358outputStream* out = output();359out->print("reserved=" SIZE_FORMAT "%s", amount_in_current_scale(current_reserved), scale);360long reserved_diff = diff_in_current_scale(current_reserved, early_reserved);361if (reserved_diff != 0) {362out->print(" %+ld%s", reserved_diff, scale);363}364365out->print(", committed=" SIZE_FORMAT "%s", amount_in_current_scale(current_committed), scale);366long committed_diff = diff_in_current_scale(current_committed, early_committed);367if (committed_diff != 0) {368out->print(" %+ld%s", committed_diff, scale);369}370}371372373void MemSummaryDiffReporter::diff_summary_of_type(MEMFLAGS flag, const MallocMemory* early_malloc,374const VirtualMemory* early_vm, const MallocMemory* current_malloc,375const VirtualMemory* current_vm) const {376377outputStream* out = output();378const char* scale = current_scale();379380// Total reserved and committed memory in current baseline381size_t current_reserved_amount = reserved_total (current_malloc, current_vm);382size_t current_committed_amount = committed_total(current_malloc, current_vm);383384// Total reserved and committed memory in early baseline385size_t early_reserved_amount = reserved_total(early_malloc, early_vm);386size_t early_committed_amount = committed_total(early_malloc, early_vm);387388// Adjust virtual memory total389if (flag == mtThread) {390const VirtualMemory* early_thread_stack_usage =391_early_baseline.virtual_memory(mtThreadStack);392const VirtualMemory* current_thread_stack_usage =393_current_baseline.virtual_memory(mtThreadStack);394395early_reserved_amount += early_thread_stack_usage->reserved();396early_committed_amount += early_thread_stack_usage->committed();397398current_reserved_amount += current_thread_stack_usage->reserved();399current_committed_amount += current_thread_stack_usage->committed();400} else if (flag == mtNMT) {401early_reserved_amount += _early_baseline.malloc_tracking_overhead();402early_committed_amount += _early_baseline.malloc_tracking_overhead();403404current_reserved_amount += _current_baseline.malloc_tracking_overhead();405current_committed_amount += _current_baseline.malloc_tracking_overhead();406}407408if (amount_in_current_scale(current_reserved_amount) > 0 ||409diff_in_current_scale(current_reserved_amount, early_reserved_amount) != 0) {410411// print summary line412out->print("-%26s (", NMTUtil::flag_to_name(flag));413print_virtual_memory_diff(current_reserved_amount, current_committed_amount,414early_reserved_amount, early_committed_amount);415out->print_cr(")");416417// detail lines418if (flag == mtClass) {419// report class count420out->print("%27s (classes #" SIZE_FORMAT "", " ", _current_baseline.class_count());421int class_count_diff = (int)(_current_baseline.class_count() -422_early_baseline.class_count());423if (_current_baseline.class_count() != _early_baseline.class_count()) {424out->print(" %+d", (int)(_current_baseline.class_count() - _early_baseline.class_count()));425}426out->print_cr(")");427} else if (flag == mtThread) {428// report thread count429out->print("%27s (thread #" SIZE_FORMAT "", " ", _current_baseline.thread_count());430int thread_count_diff = (int)(_current_baseline.thread_count() -431_early_baseline.thread_count());432if (thread_count_diff != 0) {433out->print(" %+d", thread_count_diff);434}435out->print_cr(")");436437// report thread stack438const VirtualMemory* current_thread_stack =439_current_baseline.virtual_memory(mtThreadStack);440const VirtualMemory* early_thread_stack =441_early_baseline.virtual_memory(mtThreadStack);442443out->print("%27s (stack: ", " ");444print_virtual_memory_diff(current_thread_stack->reserved(), current_thread_stack->committed(),445early_thread_stack->reserved(), early_thread_stack->committed());446out->print_cr(")");447}448449// Report malloc'd memory450size_t current_malloc_amount = current_malloc->malloc_size();451size_t early_malloc_amount = early_malloc->malloc_size();452if (amount_in_current_scale(current_malloc_amount) > 0 ||453diff_in_current_scale(current_malloc_amount, early_malloc_amount) != 0) {454out->print("%28s(", " ");455print_malloc_diff(current_malloc_amount, (flag == mtChunk) ? 0 : current_malloc->malloc_count(),456early_malloc_amount, early_malloc->malloc_count(), mtNone);457out->print_cr(")");458}459460// Report virtual memory461if (amount_in_current_scale(current_vm->reserved()) > 0 ||462diff_in_current_scale(current_vm->reserved(), early_vm->reserved()) != 0) {463out->print("%27s (mmap: ", " ");464print_virtual_memory_diff(current_vm->reserved(), current_vm->committed(),465early_vm->reserved(), early_vm->committed());466out->print_cr(")");467}468469// Report arena memory470if (amount_in_current_scale(current_malloc->arena_size()) > 0 ||471diff_in_current_scale(current_malloc->arena_size(), early_malloc->arena_size()) != 0) {472out->print("%28s(", " ");473print_arena_diff(current_malloc->arena_size(), current_malloc->arena_count(),474early_malloc->arena_size(), early_malloc->arena_count());475out->print_cr(")");476}477478// Report native memory tracking overhead479if (flag == mtNMT) {480size_t current_tracking_overhead = amount_in_current_scale(_current_baseline.malloc_tracking_overhead());481size_t early_tracking_overhead = amount_in_current_scale(_early_baseline.malloc_tracking_overhead());482483out->print("%27s (tracking overhead=" SIZE_FORMAT "%s", " ",484amount_in_current_scale(_current_baseline.malloc_tracking_overhead()), scale);485486long overhead_diff = diff_in_current_scale(_current_baseline.malloc_tracking_overhead(),487_early_baseline.malloc_tracking_overhead());488if (overhead_diff != 0) {489out->print(" %+ld%s", overhead_diff, scale);490}491out->print_cr(")");492}493out->print_cr(" ");494}495}496497void MemDetailDiffReporter::report_diff() {498MemSummaryDiffReporter::report_diff();499diff_malloc_sites();500diff_virtual_memory_sites();501}502503void MemDetailDiffReporter::diff_malloc_sites() const {504MallocSiteIterator early_itr = _early_baseline.malloc_sites(MemBaseline::by_site_and_type);505MallocSiteIterator current_itr = _current_baseline.malloc_sites(MemBaseline::by_site_and_type);506507const MallocSite* early_site = early_itr.next();508const MallocSite* current_site = current_itr.next();509510while (early_site != NULL || current_site != NULL) {511if (early_site == NULL) {512new_malloc_site(current_site);513current_site = current_itr.next();514} else if (current_site == NULL) {515old_malloc_site(early_site);516early_site = early_itr.next();517} else {518int compVal = current_site->call_stack()->compare(*early_site->call_stack());519if (compVal < 0) {520new_malloc_site(current_site);521current_site = current_itr.next();522} else if (compVal > 0) {523old_malloc_site(early_site);524early_site = early_itr.next();525} else {526diff_malloc_site(early_site, current_site);527early_site = early_itr.next();528current_site = current_itr.next();529}530}531}532}533534void MemDetailDiffReporter::diff_virtual_memory_sites() const {535VirtualMemorySiteIterator early_itr = _early_baseline.virtual_memory_sites(MemBaseline::by_site);536VirtualMemorySiteIterator current_itr = _current_baseline.virtual_memory_sites(MemBaseline::by_site);537538const VirtualMemoryAllocationSite* early_site = early_itr.next();539const VirtualMemoryAllocationSite* current_site = current_itr.next();540541while (early_site != NULL || current_site != NULL) {542if (early_site == NULL) {543new_virtual_memory_site(current_site);544current_site = current_itr.next();545} else if (current_site == NULL) {546old_virtual_memory_site(early_site);547early_site = early_itr.next();548} else {549int compVal = current_site->call_stack()->compare(*early_site->call_stack());550if (compVal < 0) {551new_virtual_memory_site(current_site);552current_site = current_itr.next();553} else if (compVal > 0) {554old_virtual_memory_site(early_site);555early_site = early_itr.next();556} else {557diff_virtual_memory_site(early_site, current_site);558early_site = early_itr.next();559current_site = current_itr.next();560}561}562}563}564565566void MemDetailDiffReporter::new_malloc_site(const MallocSite* malloc_site) const {567diff_malloc_site(malloc_site->call_stack(), malloc_site->size(), malloc_site->count(),5680, 0, malloc_site->flag());569}570571void MemDetailDiffReporter::old_malloc_site(const MallocSite* malloc_site) const {572diff_malloc_site(malloc_site->call_stack(), 0, 0, malloc_site->size(),573malloc_site->count(), malloc_site->flag());574}575576void MemDetailDiffReporter::diff_malloc_site(const MallocSite* early,577const MallocSite* current) const {578if (early->flag() != current->flag()) {579// If malloc site type changed, treat it as deallocation of old type and580// allocation of new type.581old_malloc_site(early);582new_malloc_site(current);583} else {584diff_malloc_site(current->call_stack(), current->size(), current->count(),585early->size(), early->count(), early->flag());586}587}588589void MemDetailDiffReporter::diff_malloc_site(const NativeCallStack* stack, size_t current_size,590size_t current_count, size_t early_size, size_t early_count, MEMFLAGS flags) const {591outputStream* out = output();592593assert(stack != NULL, "NULL stack");594595if (diff_in_current_scale(current_size, early_size) == 0) {596return;597}598599stack->print_on(out);600out->print("%28s (", " ");601print_malloc_diff(current_size, current_count,602early_size, early_count, flags);603604out->print_cr(")\n");605}606607608void MemDetailDiffReporter::new_virtual_memory_site(const VirtualMemoryAllocationSite* site) const {609diff_virtual_memory_site(site->call_stack(), site->reserved(), site->committed(), 0, 0, site->flag());610}611612void MemDetailDiffReporter::old_virtual_memory_site(const VirtualMemoryAllocationSite* site) const {613diff_virtual_memory_site(site->call_stack(), 0, 0, site->reserved(), site->committed(), site->flag());614}615616void MemDetailDiffReporter::diff_virtual_memory_site(const VirtualMemoryAllocationSite* early,617const VirtualMemoryAllocationSite* current) const {618assert(early->flag() == current->flag(), "Should be the same");619diff_virtual_memory_site(current->call_stack(), current->reserved(), current->committed(),620early->reserved(), early->committed(), current->flag());621}622623void MemDetailDiffReporter::diff_virtual_memory_site(const NativeCallStack* stack, size_t current_reserved,624size_t current_committed, size_t early_reserved, size_t early_committed, MEMFLAGS flag) const {625outputStream* out = output();626627// no change628if (diff_in_current_scale(current_reserved, early_reserved) == 0 &&629diff_in_current_scale(current_committed, early_committed) == 0) {630return;631}632633stack->print_on(out);634out->print("%28s (mmap: ", " ");635print_virtual_memory_diff(current_reserved, current_committed,636early_reserved, early_committed);637638if (flag != mtNone) {639out->print(" Type=%s", NMTUtil::flag_to_name(flag));640}641642out->print_cr(")\n");643}644645646647