Path: blob/master/src/hotspot/share/services/memReporter.cpp
41145 views
/*1* Copyright (c) 2012, 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"24#include "memory/allocation.hpp"25#include "memory/metaspace.hpp"26#include "memory/metaspaceUtils.hpp"27#include "services/mallocTracker.hpp"28#include "services/memReporter.hpp"29#include "services/threadStackTracker.hpp"30#include "services/virtualMemoryTracker.hpp"31#include "utilities/globalDefinitions.hpp"3233size_t MemReporterBase::reserved_total(const MallocMemory* malloc, const VirtualMemory* vm) const {34return malloc->malloc_size() + malloc->arena_size() + vm->reserved();35}3637size_t MemReporterBase::committed_total(const MallocMemory* malloc, const VirtualMemory* vm) const {38return malloc->malloc_size() + malloc->arena_size() + vm->committed();39}4041void MemReporterBase::print_total(size_t reserved, size_t committed) const {42const char* scale = current_scale();43output()->print("reserved=" SIZE_FORMAT "%s, committed=" SIZE_FORMAT "%s",44amount_in_current_scale(reserved), scale, amount_in_current_scale(committed), scale);45}4647void MemReporterBase::print_malloc(size_t amount, size_t count, MEMFLAGS flag) const {48const char* scale = current_scale();49outputStream* out = output();50const char* alloc_type = (flag == mtThreadStack) ? "" : "malloc=";5152if (flag != mtNone) {53out->print("(%s" SIZE_FORMAT "%s type=%s", alloc_type,54amount_in_current_scale(amount), scale, NMTUtil::flag_to_name(flag));55} else {56out->print("(%s" SIZE_FORMAT "%s", alloc_type,57amount_in_current_scale(amount), scale);58}5960if (count > 0) {61out->print(" #" SIZE_FORMAT "", count);62}6364out->print(")");65}6667void MemReporterBase::print_virtual_memory(size_t reserved, size_t committed) const {68const char* scale = current_scale();69output()->print("(mmap: reserved=" SIZE_FORMAT "%s, committed=" SIZE_FORMAT "%s)",70amount_in_current_scale(reserved), scale, amount_in_current_scale(committed), scale);71}7273void MemReporterBase::print_malloc_line(size_t amount, size_t count) const {74output()->print("%28s", " ");75print_malloc(amount, count);76output()->print_cr(" ");77}7879void MemReporterBase::print_virtual_memory_line(size_t reserved, size_t committed) const {80output()->print("%28s", " ");81print_virtual_memory(reserved, committed);82output()->print_cr(" ");83}8485void MemReporterBase::print_arena_line(size_t amount, size_t count) const {86const char* scale = current_scale();87output()->print_cr("%27s (arena=" SIZE_FORMAT "%s #" SIZE_FORMAT ")", " ",88amount_in_current_scale(amount), scale, count);89}9091void MemReporterBase::print_virtual_memory_region(const char* type, address base, size_t size) const {92const char* scale = current_scale();93output()->print("[" PTR_FORMAT " - " PTR_FORMAT "] %s " SIZE_FORMAT "%s",94p2i(base), p2i(base + size), type, amount_in_current_scale(size), scale);95}969798void MemSummaryReporter::report() {99outputStream* out = output();100size_t total_reserved_amount = _malloc_snapshot->total() +101_vm_snapshot->total_reserved();102size_t total_committed_amount = _malloc_snapshot->total() +103_vm_snapshot->total_committed();104105// Overall total106out->print_cr("\nNative Memory Tracking:\n");107108if (scale() > 1) {109out->print_cr("(Omitting categories weighting less than 1%s)", current_scale());110out->cr();111}112113out->print("Total: ");114print_total(total_reserved_amount, total_committed_amount);115out->print("\n");116117// Summary by memory type118for (int index = 0; index < mt_number_of_types; index ++) {119MEMFLAGS flag = NMTUtil::index_to_flag(index);120// thread stack is reported as part of thread category121if (flag == mtThreadStack) continue;122MallocMemory* malloc_memory = _malloc_snapshot->by_type(flag);123VirtualMemory* virtual_memory = _vm_snapshot->by_type(flag);124125report_summary_of_type(flag, malloc_memory, virtual_memory);126}127}128129void MemSummaryReporter::report_summary_of_type(MEMFLAGS flag,130MallocMemory* malloc_memory, VirtualMemory* virtual_memory) {131132size_t reserved_amount = reserved_total (malloc_memory, virtual_memory);133size_t committed_amount = committed_total(malloc_memory, virtual_memory);134135// Count thread's native stack in "Thread" category136if (flag == mtThread) {137if (ThreadStackTracker::track_as_vm()) {138const VirtualMemory* thread_stack_usage =139(const VirtualMemory*)_vm_snapshot->by_type(mtThreadStack);140reserved_amount += thread_stack_usage->reserved();141committed_amount += thread_stack_usage->committed();142} else {143const MallocMemory* thread_stack_usage =144(const MallocMemory*)_malloc_snapshot->by_type(mtThreadStack);145reserved_amount += thread_stack_usage->malloc_size();146committed_amount += thread_stack_usage->malloc_size();147}148} else if (flag == mtNMT) {149// Count malloc headers in "NMT" category150reserved_amount += _malloc_snapshot->malloc_overhead()->size();151committed_amount += _malloc_snapshot->malloc_overhead()->size();152}153154if (amount_in_current_scale(reserved_amount) > 0) {155outputStream* out = output();156const char* scale = current_scale();157out->print("-%26s (", NMTUtil::flag_to_name(flag));158print_total(reserved_amount, committed_amount);159out->print_cr(")");160161if (flag == mtClass) {162// report class count163out->print_cr("%27s (classes #" SIZE_FORMAT ")",164" ", (_instance_class_count + _array_class_count));165out->print_cr("%27s ( instance classes #" SIZE_FORMAT ", array classes #" SIZE_FORMAT ")",166" ", _instance_class_count, _array_class_count);167} else if (flag == mtThread) {168if (ThreadStackTracker::track_as_vm()) {169const VirtualMemory* thread_stack_usage =170_vm_snapshot->by_type(mtThreadStack);171// report thread count172out->print_cr("%27s (thread #" SIZE_FORMAT ")", " ", ThreadStackTracker::thread_count());173out->print("%27s (stack: ", " ");174print_total(thread_stack_usage->reserved(), thread_stack_usage->committed());175} else {176MallocMemory* thread_stack_memory = _malloc_snapshot->by_type(mtThreadStack);177const char* scale = current_scale();178// report thread count179assert(ThreadStackTracker::thread_count() == 0, "Not used");180out->print_cr("%27s (thread #" SIZE_FORMAT ")", " ", thread_stack_memory->malloc_count());181out->print("%27s (Stack: " SIZE_FORMAT "%s", " ",182amount_in_current_scale(thread_stack_memory->malloc_size()), scale);183}184out->print_cr(")");185}186187// report malloc'd memory188if (amount_in_current_scale(malloc_memory->malloc_size()) > 0) {189// We don't know how many arena chunks are in used, so don't report the count190size_t count = (flag == mtChunk) ? 0 : malloc_memory->malloc_count();191print_malloc_line(malloc_memory->malloc_size(), count);192}193194if (amount_in_current_scale(virtual_memory->reserved()) > 0) {195print_virtual_memory_line(virtual_memory->reserved(), virtual_memory->committed());196}197198if (amount_in_current_scale(malloc_memory->arena_size()) > 0) {199print_arena_line(malloc_memory->arena_size(), malloc_memory->arena_count());200}201202if (flag == mtNMT &&203amount_in_current_scale(_malloc_snapshot->malloc_overhead()->size()) > 0) {204out->print_cr("%27s (tracking overhead=" SIZE_FORMAT "%s)", " ",205amount_in_current_scale(_malloc_snapshot->malloc_overhead()->size()), scale);206} else if (flag == mtClass) {207// Metadata information208report_metadata(Metaspace::NonClassType);209if (Metaspace::using_class_space()) {210report_metadata(Metaspace::ClassType);211}212}213out->print_cr(" ");214}215}216217void MemSummaryReporter::report_metadata(Metaspace::MetadataType type) const {218assert(type == Metaspace::NonClassType || type == Metaspace::ClassType,219"Invalid metadata type");220const char* name = (type == Metaspace::NonClassType) ?221"Metadata: " : "Class space:";222223outputStream* out = output();224const char* scale = current_scale();225const MetaspaceStats stats = MetaspaceUtils::get_statistics(type);226227size_t waste = stats.committed() - stats.used();228float waste_percentage = stats.committed() > 0 ? (((float)waste * 100)/stats.committed()) : 0.0f;229230out->print_cr("%27s ( %s)", " ", name);231out->print("%27s ( ", " ");232print_total(stats.reserved(), stats.committed());233out->print_cr(")");234out->print_cr("%27s ( used=" SIZE_FORMAT "%s)", " ", amount_in_current_scale(stats.used()), scale);235out->print_cr("%27s ( waste=" SIZE_FORMAT "%s =%2.2f%%)", " ", amount_in_current_scale(waste),236scale, waste_percentage);237}238239void MemDetailReporter::report_detail() {240// Start detail report241outputStream* out = output();242out->print_cr("Details:\n");243244int num_omitted =245report_malloc_sites() +246report_virtual_memory_allocation_sites();247if (num_omitted > 0) {248assert(scale() > 1, "sanity");249out->print_cr("(%d call sites weighting less than 1%s each omitted.)",250num_omitted, current_scale());251out->cr();252}253}254255int MemDetailReporter::report_malloc_sites() {256MallocSiteIterator malloc_itr = _baseline.malloc_sites(MemBaseline::by_size);257if (malloc_itr.is_empty()) return 0;258259outputStream* out = output();260261const MallocSite* malloc_site;262int num_omitted = 0;263while ((malloc_site = malloc_itr.next()) != NULL) {264// Don't report free sites; does not count toward omitted count.265if (malloc_site->size() == 0) {266continue;267}268// Don't report if site has allocated less than one unit of whatever our scale is269if (scale() > 1 && amount_in_current_scale(malloc_site->size()) == 0) {270num_omitted ++;271continue;272}273const NativeCallStack* stack = malloc_site->call_stack();274stack->print_on(out);275out->print("%29s", " ");276MEMFLAGS flag = malloc_site->flag();277assert(NMTUtil::flag_is_valid(flag) && flag != mtNone,278"Must have a valid memory type");279print_malloc(malloc_site->size(), malloc_site->count(),flag);280out->print_cr("\n");281}282return num_omitted;283}284285int MemDetailReporter::report_virtual_memory_allocation_sites() {286VirtualMemorySiteIterator virtual_memory_itr =287_baseline.virtual_memory_sites(MemBaseline::by_size);288289if (virtual_memory_itr.is_empty()) return 0;290291outputStream* out = output();292const VirtualMemoryAllocationSite* virtual_memory_site;293int num_omitted = 0;294while ((virtual_memory_site = virtual_memory_itr.next()) != NULL) {295// Don't report free sites; does not count toward omitted count.296if (virtual_memory_site->reserved() == 0) {297continue;298}299// Don't report if site has reserved less than one unit of whatever our scale is300if (scale() > 1 && amount_in_current_scale(virtual_memory_site->reserved()) == 0) {301num_omitted++;302continue;303}304const NativeCallStack* stack = virtual_memory_site->call_stack();305stack->print_on(out);306out->print("%28s (", " ");307print_total(virtual_memory_site->reserved(), virtual_memory_site->committed());308MEMFLAGS flag = virtual_memory_site->flag();309if (flag != mtNone) {310out->print(" Type=%s", NMTUtil::flag_to_name(flag));311}312out->print_cr(")\n");313}314return num_omitted;315}316317318void MemDetailReporter::report_virtual_memory_map() {319// Virtual memory map always in base address order320VirtualMemoryAllocationIterator itr = _baseline.virtual_memory_allocations();321const ReservedMemoryRegion* rgn;322323output()->print_cr("Virtual memory map:");324while ((rgn = itr.next()) != NULL) {325report_virtual_memory_region(rgn);326}327}328329void MemDetailReporter::report_virtual_memory_region(const ReservedMemoryRegion* reserved_rgn) {330assert(reserved_rgn != NULL, "NULL pointer");331332// Don't report if size is too small333if (amount_in_current_scale(reserved_rgn->size()) == 0) return;334335outputStream* out = output();336const char* scale = current_scale();337const NativeCallStack* stack = reserved_rgn->call_stack();338bool all_committed = reserved_rgn->size() == reserved_rgn->committed_size();339const char* region_type = (all_committed ? "reserved and committed" : "reserved");340out->print_cr(" ");341print_virtual_memory_region(region_type, reserved_rgn->base(), reserved_rgn->size());342out->print(" for %s", NMTUtil::flag_to_name(reserved_rgn->flag()));343if (stack->is_empty()) {344out->print_cr(" ");345} else {346out->print_cr(" from");347stack->print_on(out, 4);348}349350if (all_committed) {351CommittedRegionIterator itr = reserved_rgn->iterate_committed_regions();352const CommittedMemoryRegion* committed_rgn = itr.next();353if (committed_rgn->size() == reserved_rgn->size() && committed_rgn->call_stack()->equals(*stack)) {354// One region spanning the entire reserved region, with the same stack trace.355// Don't print this regions because the "reserved and committed" line above356// already indicates that the region is comitted.357assert(itr.next() == NULL, "Unexpectedly more than one regions");358return;359}360}361362CommittedRegionIterator itr = reserved_rgn->iterate_committed_regions();363const CommittedMemoryRegion* committed_rgn;364while ((committed_rgn = itr.next()) != NULL) {365// Don't report if size is too small366if (amount_in_current_scale(committed_rgn->size()) == 0) continue;367stack = committed_rgn->call_stack();368out->print("\n\t");369print_virtual_memory_region("committed", committed_rgn->base(), committed_rgn->size());370if (stack->is_empty()) {371out->print_cr(" ");372} else {373out->print_cr(" from");374stack->print_on(out, 12);375}376}377}378379void MemSummaryDiffReporter::report_diff() {380outputStream* out = output();381out->print_cr("\nNative Memory Tracking:\n");382383if (scale() > 1) {384out->print_cr("(Omitting categories weighting less than 1%s)", current_scale());385out->cr();386}387388// Overall diff389out->print("Total: ");390print_virtual_memory_diff(_current_baseline.total_reserved_memory(),391_current_baseline.total_committed_memory(), _early_baseline.total_reserved_memory(),392_early_baseline.total_committed_memory());393394out->print_cr("\n");395396// Summary diff by memory type397for (int index = 0; index < mt_number_of_types; index ++) {398MEMFLAGS flag = NMTUtil::index_to_flag(index);399// thread stack is reported as part of thread category400if (flag == mtThreadStack) continue;401diff_summary_of_type(flag,402_early_baseline.malloc_memory(flag),403_early_baseline.virtual_memory(flag),404_early_baseline.metaspace_stats(),405_current_baseline.malloc_memory(flag),406_current_baseline.virtual_memory(flag),407_current_baseline.metaspace_stats());408}409}410411void MemSummaryDiffReporter::print_malloc_diff(size_t current_amount, size_t current_count,412size_t early_amount, size_t early_count, MEMFLAGS flags) const {413const char* scale = current_scale();414outputStream* out = output();415const char* alloc_type = (flags == mtThread) ? "" : "malloc=";416417out->print("%s" SIZE_FORMAT "%s", alloc_type, amount_in_current_scale(current_amount), scale);418// Report type only if it is valid and not under "thread" category419if (flags != mtNone && flags != mtThread) {420out->print(" type=%s", NMTUtil::flag_to_name(flags));421}422423long amount_diff = diff_in_current_scale(current_amount, early_amount);424if (amount_diff != 0) {425out->print(" %+ld%s", amount_diff, scale);426}427if (current_count > 0) {428out->print(" #" SIZE_FORMAT "", current_count);429if (current_count != early_count) {430out->print(" %+d", (int)(current_count - early_count));431}432}433}434435void MemSummaryDiffReporter::print_arena_diff(size_t current_amount, size_t current_count,436size_t early_amount, size_t early_count) const {437const char* scale = current_scale();438outputStream* out = output();439out->print("arena=" SIZE_FORMAT "%s", amount_in_current_scale(current_amount), scale);440if (diff_in_current_scale(current_amount, early_amount) != 0) {441out->print(" %+ld", diff_in_current_scale(current_amount, early_amount));442}443444out->print(" #" SIZE_FORMAT "", current_count);445if (current_count != early_count) {446out->print(" %+d", (int)(current_count - early_count));447}448}449450void MemSummaryDiffReporter::print_virtual_memory_diff(size_t current_reserved, size_t current_committed,451size_t early_reserved, size_t early_committed) const {452const char* scale = current_scale();453outputStream* out = output();454out->print("reserved=" SIZE_FORMAT "%s", amount_in_current_scale(current_reserved), scale);455long reserved_diff = diff_in_current_scale(current_reserved, early_reserved);456if (reserved_diff != 0) {457out->print(" %+ld%s", reserved_diff, scale);458}459460out->print(", committed=" SIZE_FORMAT "%s", amount_in_current_scale(current_committed), scale);461long committed_diff = diff_in_current_scale(current_committed, early_committed);462if (committed_diff != 0) {463out->print(" %+ld%s", committed_diff, scale);464}465}466467468void MemSummaryDiffReporter::diff_summary_of_type(MEMFLAGS flag,469const MallocMemory* early_malloc, const VirtualMemory* early_vm,470const MetaspaceCombinedStats& early_ms,471const MallocMemory* current_malloc, const VirtualMemory* current_vm,472const MetaspaceCombinedStats& current_ms) const {473474outputStream* out = output();475const char* scale = current_scale();476477// Total reserved and committed memory in current baseline478size_t current_reserved_amount = reserved_total (current_malloc, current_vm);479size_t current_committed_amount = committed_total(current_malloc, current_vm);480481// Total reserved and committed memory in early baseline482size_t early_reserved_amount = reserved_total(early_malloc, early_vm);483size_t early_committed_amount = committed_total(early_malloc, early_vm);484485// Adjust virtual memory total486if (flag == mtThread) {487const VirtualMemory* early_thread_stack_usage =488_early_baseline.virtual_memory(mtThreadStack);489const VirtualMemory* current_thread_stack_usage =490_current_baseline.virtual_memory(mtThreadStack);491492early_reserved_amount += early_thread_stack_usage->reserved();493early_committed_amount += early_thread_stack_usage->committed();494495current_reserved_amount += current_thread_stack_usage->reserved();496current_committed_amount += current_thread_stack_usage->committed();497} else if (flag == mtNMT) {498early_reserved_amount += _early_baseline.malloc_tracking_overhead();499early_committed_amount += _early_baseline.malloc_tracking_overhead();500501current_reserved_amount += _current_baseline.malloc_tracking_overhead();502current_committed_amount += _current_baseline.malloc_tracking_overhead();503}504505if (amount_in_current_scale(current_reserved_amount) > 0 ||506diff_in_current_scale(current_reserved_amount, early_reserved_amount) != 0) {507508// print summary line509out->print("-%26s (", NMTUtil::flag_to_name(flag));510print_virtual_memory_diff(current_reserved_amount, current_committed_amount,511early_reserved_amount, early_committed_amount);512out->print_cr(")");513514// detail lines515if (flag == mtClass) {516// report class count517out->print("%27s (classes #" SIZE_FORMAT "", " ", _current_baseline.class_count());518int class_count_diff = (int)(_current_baseline.class_count() -519_early_baseline.class_count());520if (_current_baseline.class_count() != _early_baseline.class_count()) {521out->print(" %+d", (int)(_current_baseline.class_count() - _early_baseline.class_count()));522}523out->print_cr(")");524525out->print("%27s ( instance classes #" SIZE_FORMAT, " ", _current_baseline.instance_class_count());526if (_current_baseline.instance_class_count() != _early_baseline.instance_class_count()) {527out->print(" %+d", (int)(_current_baseline.instance_class_count() - _early_baseline.instance_class_count()));528}529out->print(", array classes #" SIZE_FORMAT, _current_baseline.array_class_count());530if (_current_baseline.array_class_count() != _early_baseline.array_class_count()) {531out->print(" %+d", (int)(_current_baseline.array_class_count() - _early_baseline.array_class_count()));532}533out->print_cr(")");534535} else if (flag == mtThread) {536// report thread count537out->print("%27s (thread #" SIZE_FORMAT "", " ", _current_baseline.thread_count());538int thread_count_diff = (int)(_current_baseline.thread_count() -539_early_baseline.thread_count());540if (thread_count_diff != 0) {541out->print(" %+d", thread_count_diff);542}543out->print_cr(")");544545out->print("%27s (stack: ", " ");546if (ThreadStackTracker::track_as_vm()) {547// report thread stack548const VirtualMemory* current_thread_stack =549_current_baseline.virtual_memory(mtThreadStack);550const VirtualMemory* early_thread_stack =551_early_baseline.virtual_memory(mtThreadStack);552553print_virtual_memory_diff(current_thread_stack->reserved(), current_thread_stack->committed(),554early_thread_stack->reserved(), early_thread_stack->committed());555} else {556const MallocMemory* current_thread_stack =557_current_baseline.malloc_memory(mtThreadStack);558const MallocMemory* early_thread_stack =559_early_baseline.malloc_memory(mtThreadStack);560561print_malloc_diff(current_thread_stack->malloc_size(), current_thread_stack->malloc_count(),562early_thread_stack->malloc_size(), early_thread_stack->malloc_count(), flag);563}564out->print_cr(")");565}566567// Report malloc'd memory568size_t current_malloc_amount = current_malloc->malloc_size();569size_t early_malloc_amount = early_malloc->malloc_size();570if (amount_in_current_scale(current_malloc_amount) > 0 ||571diff_in_current_scale(current_malloc_amount, early_malloc_amount) != 0) {572out->print("%28s(", " ");573print_malloc_diff(current_malloc_amount, (flag == mtChunk) ? 0 : current_malloc->malloc_count(),574early_malloc_amount, early_malloc->malloc_count(), mtNone);575out->print_cr(")");576}577578// Report virtual memory579if (amount_in_current_scale(current_vm->reserved()) > 0 ||580diff_in_current_scale(current_vm->reserved(), early_vm->reserved()) != 0) {581out->print("%27s (mmap: ", " ");582print_virtual_memory_diff(current_vm->reserved(), current_vm->committed(),583early_vm->reserved(), early_vm->committed());584out->print_cr(")");585}586587// Report arena memory588if (amount_in_current_scale(current_malloc->arena_size()) > 0 ||589diff_in_current_scale(current_malloc->arena_size(), early_malloc->arena_size()) != 0) {590out->print("%28s(", " ");591print_arena_diff(current_malloc->arena_size(), current_malloc->arena_count(),592early_malloc->arena_size(), early_malloc->arena_count());593out->print_cr(")");594}595596// Report native memory tracking overhead597if (flag == mtNMT) {598size_t current_tracking_overhead = amount_in_current_scale(_current_baseline.malloc_tracking_overhead());599size_t early_tracking_overhead = amount_in_current_scale(_early_baseline.malloc_tracking_overhead());600601out->print("%27s (tracking overhead=" SIZE_FORMAT "%s", " ",602amount_in_current_scale(_current_baseline.malloc_tracking_overhead()), scale);603604long overhead_diff = diff_in_current_scale(_current_baseline.malloc_tracking_overhead(),605_early_baseline.malloc_tracking_overhead());606if (overhead_diff != 0) {607out->print(" %+ld%s", overhead_diff, scale);608}609out->print_cr(")");610} else if (flag == mtClass) {611print_metaspace_diff(current_ms, early_ms);612}613out->print_cr(" ");614}615}616617void MemSummaryDiffReporter::print_metaspace_diff(const MetaspaceCombinedStats& current_ms,618const MetaspaceCombinedStats& early_ms) const {619print_metaspace_diff("Metadata", current_ms.non_class_space_stats(), early_ms.non_class_space_stats());620if (Metaspace::using_class_space()) {621print_metaspace_diff("Class space", current_ms.class_space_stats(), early_ms.class_space_stats());622}623}624625void MemSummaryDiffReporter::print_metaspace_diff(const char* header,626const MetaspaceStats& current_stats,627const MetaspaceStats& early_stats) const {628outputStream* out = output();629const char* scale = current_scale();630631out->print_cr("%27s: ( %s)", " ", header);632out->print("%27s ( ", " ");633print_virtual_memory_diff(current_stats.reserved(),634current_stats.committed(),635early_stats.reserved(),636early_stats.committed());637out->print_cr(")");638639long diff_used = diff_in_current_scale(current_stats.used(),640early_stats.used());641642size_t current_waste = current_stats.committed() - current_stats.used();643size_t early_waste = early_stats.committed() - early_stats.used();644long diff_waste = diff_in_current_scale(current_waste, early_waste);645646// Diff used647out->print("%27s ( used=" SIZE_FORMAT "%s", " ",648amount_in_current_scale(current_stats.used()), scale);649if (diff_used != 0) {650out->print(" %+ld%s", diff_used, scale);651}652out->print_cr(")");653654// Diff waste655const float waste_percentage = current_stats.committed() == 0 ? 0.0f :656(current_waste * 100.0f) / current_stats.committed();657out->print("%27s ( waste=" SIZE_FORMAT "%s =%2.2f%%", " ",658amount_in_current_scale(current_waste), scale, waste_percentage);659if (diff_waste != 0) {660out->print(" %+ld%s", diff_waste, scale);661}662out->print_cr(")");663}664665void MemDetailDiffReporter::report_diff() {666MemSummaryDiffReporter::report_diff();667diff_malloc_sites();668diff_virtual_memory_sites();669}670671void MemDetailDiffReporter::diff_malloc_sites() const {672MallocSiteIterator early_itr = _early_baseline.malloc_sites(MemBaseline::by_site_and_type);673MallocSiteIterator current_itr = _current_baseline.malloc_sites(MemBaseline::by_site_and_type);674675const MallocSite* early_site = early_itr.next();676const MallocSite* current_site = current_itr.next();677678while (early_site != NULL || current_site != NULL) {679if (early_site == NULL) {680new_malloc_site(current_site);681current_site = current_itr.next();682} else if (current_site == NULL) {683old_malloc_site(early_site);684early_site = early_itr.next();685} else {686int compVal = current_site->call_stack()->compare(*early_site->call_stack());687if (compVal < 0) {688new_malloc_site(current_site);689current_site = current_itr.next();690} else if (compVal > 0) {691old_malloc_site(early_site);692early_site = early_itr.next();693} else {694diff_malloc_site(early_site, current_site);695early_site = early_itr.next();696current_site = current_itr.next();697}698}699}700}701702void MemDetailDiffReporter::diff_virtual_memory_sites() const {703VirtualMemorySiteIterator early_itr = _early_baseline.virtual_memory_sites(MemBaseline::by_site);704VirtualMemorySiteIterator current_itr = _current_baseline.virtual_memory_sites(MemBaseline::by_site);705706const VirtualMemoryAllocationSite* early_site = early_itr.next();707const VirtualMemoryAllocationSite* current_site = current_itr.next();708709while (early_site != NULL || current_site != NULL) {710if (early_site == NULL) {711new_virtual_memory_site(current_site);712current_site = current_itr.next();713} else if (current_site == NULL) {714old_virtual_memory_site(early_site);715early_site = early_itr.next();716} else {717int compVal = current_site->call_stack()->compare(*early_site->call_stack());718if (compVal < 0) {719new_virtual_memory_site(current_site);720current_site = current_itr.next();721} else if (compVal > 0) {722old_virtual_memory_site(early_site);723early_site = early_itr.next();724} else {725diff_virtual_memory_site(early_site, current_site);726early_site = early_itr.next();727current_site = current_itr.next();728}729}730}731}732733734void MemDetailDiffReporter::new_malloc_site(const MallocSite* malloc_site) const {735diff_malloc_site(malloc_site->call_stack(), malloc_site->size(), malloc_site->count(),7360, 0, malloc_site->flag());737}738739void MemDetailDiffReporter::old_malloc_site(const MallocSite* malloc_site) const {740diff_malloc_site(malloc_site->call_stack(), 0, 0, malloc_site->size(),741malloc_site->count(), malloc_site->flag());742}743744void MemDetailDiffReporter::diff_malloc_site(const MallocSite* early,745const MallocSite* current) const {746if (early->flag() != current->flag()) {747// If malloc site type changed, treat it as deallocation of old type and748// allocation of new type.749old_malloc_site(early);750new_malloc_site(current);751} else {752diff_malloc_site(current->call_stack(), current->size(), current->count(),753early->size(), early->count(), early->flag());754}755}756757void MemDetailDiffReporter::diff_malloc_site(const NativeCallStack* stack, size_t current_size,758size_t current_count, size_t early_size, size_t early_count, MEMFLAGS flags) const {759outputStream* out = output();760761assert(stack != NULL, "NULL stack");762763if (diff_in_current_scale(current_size, early_size) == 0) {764return;765}766767stack->print_on(out);768out->print("%28s (", " ");769print_malloc_diff(current_size, current_count,770early_size, early_count, flags);771772out->print_cr(")\n");773}774775776void MemDetailDiffReporter::new_virtual_memory_site(const VirtualMemoryAllocationSite* site) const {777diff_virtual_memory_site(site->call_stack(), site->reserved(), site->committed(), 0, 0, site->flag());778}779780void MemDetailDiffReporter::old_virtual_memory_site(const VirtualMemoryAllocationSite* site) const {781diff_virtual_memory_site(site->call_stack(), 0, 0, site->reserved(), site->committed(), site->flag());782}783784void MemDetailDiffReporter::diff_virtual_memory_site(const VirtualMemoryAllocationSite* early,785const VirtualMemoryAllocationSite* current) const {786assert(early->flag() == current->flag(), "Should be the same");787diff_virtual_memory_site(current->call_stack(), current->reserved(), current->committed(),788early->reserved(), early->committed(), current->flag());789}790791void MemDetailDiffReporter::diff_virtual_memory_site(const NativeCallStack* stack, size_t current_reserved,792size_t current_committed, size_t early_reserved, size_t early_committed, MEMFLAGS flag) const {793outputStream* out = output();794795// no change796if (diff_in_current_scale(current_reserved, early_reserved) == 0 &&797diff_in_current_scale(current_committed, early_committed) == 0) {798return;799}800801stack->print_on(out);802out->print("%28s (mmap: ", " ");803print_virtual_memory_diff(current_reserved, current_committed,804early_reserved, early_committed);805806if (flag != mtNone) {807out->print(" Type=%s", NMTUtil::flag_to_name(flag));808}809810out->print_cr(")\n");811}812813814