Path: blob/master/src/hotspot/share/services/memTracker.cpp
64440 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 "jvm.h"25#include "logging/log.hpp"26#include "logging/logStream.hpp"27#include "memory/metaspaceUtils.hpp"28#include "runtime/atomic.hpp"29#include "runtime/globals.hpp"30#include "runtime/orderAccess.hpp"31#include "runtime/vmThread.hpp"32#include "runtime/vmOperations.hpp"33#include "services/memBaseline.hpp"34#include "services/memReporter.hpp"35#include "services/mallocTracker.inline.hpp"36#include "services/memTracker.hpp"37#include "services/nmtCommon.hpp"38#include "services/nmtPreInit.hpp"39#include "services/threadStackTracker.hpp"40#include "utilities/debug.hpp"41#include "utilities/defaultStream.hpp"42#include "utilities/vmError.hpp"4344#ifdef _WINDOWS45#include <windows.h>46#endif4748volatile NMT_TrackingLevel MemTracker::_tracking_level = NMT_unknown;49NMT_TrackingLevel MemTracker::_cmdline_tracking_level = NMT_unknown;5051MemBaseline MemTracker::_baseline;5253void MemTracker::initialize() {54bool rc = true;55assert(_tracking_level == NMT_unknown, "only call once");5657NMT_TrackingLevel level = NMTUtil::parse_tracking_level(NativeMemoryTracking);58// Should have been validated before in arguments.cpp59assert(level == NMT_off || level == NMT_summary || level == NMT_detail,60"Invalid setting for NativeMemoryTracking (%s)", NativeMemoryTracking);6162// Memory type is encoded into tracking header as a byte field,63// make sure that we don't overflow it.64STATIC_ASSERT(mt_number_of_types <= max_jubyte);6566if (level > NMT_off) {67if (!MallocTracker::initialize(level) ||68!VirtualMemoryTracker::initialize(level) ||69!ThreadStackTracker::initialize(level)) {70assert(false, "NMT initialization failed");71level = NMT_off;72log_warning(nmt)("NMT initialization failed. NMT disabled.");73return;74}75}7677NMTPreInit::pre_to_post();7879_tracking_level = _cmdline_tracking_level = level;8081// Log state right after NMT initialization82if (log_is_enabled(Info, nmt)) {83LogTarget(Info, nmt) lt;84LogStream ls(lt);85ls.print_cr("NMT initialized: %s", NMTUtil::tracking_level_to_string(_tracking_level));86ls.print_cr("Preinit state: ");87NMTPreInit::print_state(&ls);88ls.cr();89}90}9192void* MemTracker::malloc_base(void* memblock) {93return MallocTracker::get_base(memblock);94}9596void Tracker::record(address addr, size_t size) {97if (MemTracker::tracking_level() < NMT_summary) return;98switch(_type) {99case uncommit:100VirtualMemoryTracker::remove_uncommitted_region(addr, size);101break;102case release:103VirtualMemoryTracker::remove_released_region(addr, size);104break;105default:106ShouldNotReachHere();107}108}109110111// Shutdown can only be issued via JCmd, and NMT JCmd is serialized by lock112void MemTracker::shutdown() {113// We can only shutdown NMT to minimal tracking level if it is ever on.114if (tracking_level() > NMT_minimal) {115transition_to(NMT_minimal);116}117}118119bool MemTracker::transition_to(NMT_TrackingLevel level) {120NMT_TrackingLevel current_level = tracking_level();121122assert(level != NMT_off || current_level == NMT_off, "Cannot transition NMT to off");123124if (current_level == level) {125return true;126} else if (current_level > level) {127// Downgrade tracking level, we want to lower the tracking level first128_tracking_level = level;129// Make _tracking_level visible immediately.130OrderAccess::fence();131VirtualMemoryTracker::transition(current_level, level);132MallocTracker::transition(current_level, level);133ThreadStackTracker::transition(current_level, level);134} else {135// Upgrading tracking level is not supported and has never been supported.136// Allocating and deallocating malloc tracking structures is not thread safe and137// leads to inconsistencies unless a lot coarser locks are added.138}139return true;140}141142// Report during error reporting.143void MemTracker::error_report(outputStream* output) {144if (tracking_level() >= NMT_summary) {145report(true, output, MemReporterBase::default_scale); // just print summary for error case.146output->print("Preinit state:");147NMTPreInit::print_state(output);148}149}150151// Report when handling PrintNMTStatistics before VM shutdown.152static volatile bool g_final_report_did_run = false;153void MemTracker::final_report(outputStream* output) {154// This function is called during both error reporting and normal VM exit.155// However, it should only ever run once. E.g. if the VM crashes after156// printing the final report during normal VM exit, it should not print157// the final report again. In addition, it should be guarded from158// recursive calls in case NMT reporting itself crashes.159if (Atomic::cmpxchg(&g_final_report_did_run, false, true) == false) {160NMT_TrackingLevel level = tracking_level();161if (level >= NMT_summary) {162report(level == NMT_summary, output, 1);163}164}165}166167void MemTracker::report(bool summary_only, outputStream* output, size_t scale) {168assert(output != NULL, "No output stream");169MemBaseline baseline;170if (baseline.baseline(summary_only)) {171if (summary_only) {172MemSummaryReporter rpt(baseline, output, scale);173rpt.report();174} else {175MemDetailReporter rpt(baseline, output, scale);176rpt.report();177output->print("Metaspace:");178// The basic metaspace report avoids any locking and should be safe to179// be called at any time.180MetaspaceUtils::print_basic_report(output, scale);181}182}183}184185void MemTracker::tuning_statistics(outputStream* out) {186// NMT statistics187out->print_cr("Native Memory Tracking Statistics:");188out->print_cr("State: %s", NMTUtil::tracking_level_to_string(_tracking_level));189out->print_cr("Malloc allocation site table size: %d", MallocSiteTable::hash_buckets());190out->print_cr(" Tracking stack depth: %d", NMT_TrackingStackDepth);191NOT_PRODUCT(out->print_cr("Peak concurrent access: %d", MallocSiteTable::access_peak_count());)192out->cr();193MallocSiteTable::print_tuning_statistics(out);194out->cr();195out->print_cr("Preinit state:");196NMTPreInit::print_state(out);197out->cr();198}199200201