Path: blob/master/src/hotspot/share/services/memTracker.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 "jvm.h"25#include "memory/metaspaceUtils.hpp"26#include "runtime/atomic.hpp"27#include "runtime/orderAccess.hpp"28#include "runtime/vmThread.hpp"29#include "runtime/vmOperations.hpp"30#include "services/memBaseline.hpp"31#include "services/memReporter.hpp"32#include "services/mallocTracker.inline.hpp"33#include "services/memTracker.hpp"34#include "services/threadStackTracker.hpp"35#include "utilities/debug.hpp"36#include "utilities/defaultStream.hpp"37#include "utilities/vmError.hpp"3839#ifdef _WINDOWS40#include <windows.h>41#endif4243volatile NMT_TrackingLevel MemTracker::_tracking_level = NMT_unknown;44NMT_TrackingLevel MemTracker::_cmdline_tracking_level = NMT_unknown;4546MemBaseline MemTracker::_baseline;47bool MemTracker::_is_nmt_env_valid = true;4849static const size_t buffer_size = 64;5051NMT_TrackingLevel MemTracker::init_tracking_level() {52// Memory type is encoded into tracking header as a byte field,53// make sure that we don't overflow it.54STATIC_ASSERT(mt_number_of_types <= max_jubyte);5556char nmt_env_variable[buffer_size];57jio_snprintf(nmt_env_variable, sizeof(nmt_env_variable), "NMT_LEVEL_%d", os::current_process_id());58const char* nmt_env_value;59#ifdef _WINDOWS60// Read the NMT environment variable from the PEB instead of the CRT61char value[buffer_size];62nmt_env_value = GetEnvironmentVariable(nmt_env_variable, value, (DWORD)sizeof(value)) != 0 ? value : NULL;63#else64nmt_env_value = ::getenv(nmt_env_variable);65#endif66NMT_TrackingLevel level = NMT_off;67if (nmt_env_value != NULL) {68if (strcmp(nmt_env_value, "summary") == 0) {69level = NMT_summary;70} else if (strcmp(nmt_env_value, "detail") == 0) {71level = NMT_detail;72} else if (strcmp(nmt_env_value, "off") != 0) {73// The value of the environment variable is invalid74_is_nmt_env_valid = false;75}76// Remove the environment variable to avoid leaking to child processes77os::unsetenv(nmt_env_variable);78}7980if (!MallocTracker::initialize(level) ||81!VirtualMemoryTracker::initialize(level)) {82level = NMT_off;83}84return level;85}8687void MemTracker::init() {88NMT_TrackingLevel level = tracking_level();89if (level >= NMT_summary) {90if (!VirtualMemoryTracker::late_initialize(level) ||91!ThreadStackTracker::late_initialize(level)) {92shutdown();93return;94}95}96}9798bool MemTracker::check_launcher_nmt_support(const char* value) {99if (strcmp(value, "=detail") == 0) {100if (MemTracker::tracking_level() != NMT_detail) {101return false;102}103} else if (strcmp(value, "=summary") == 0) {104if (MemTracker::tracking_level() != NMT_summary) {105return false;106}107} else if (strcmp(value, "=off") == 0) {108if (MemTracker::tracking_level() != NMT_off) {109return false;110}111} else {112_is_nmt_env_valid = false;113}114115return true;116}117118bool MemTracker::verify_nmt_option() {119return _is_nmt_env_valid;120}121122void* MemTracker::malloc_base(void* memblock) {123return MallocTracker::get_base(memblock);124}125126void Tracker::record(address addr, size_t size) {127if (MemTracker::tracking_level() < NMT_summary) return;128switch(_type) {129case uncommit:130VirtualMemoryTracker::remove_uncommitted_region(addr, size);131break;132case release:133VirtualMemoryTracker::remove_released_region(addr, size);134break;135default:136ShouldNotReachHere();137}138}139140141// Shutdown can only be issued via JCmd, and NMT JCmd is serialized by lock142void MemTracker::shutdown() {143// We can only shutdown NMT to minimal tracking level if it is ever on.144if (tracking_level() > NMT_minimal) {145transition_to(NMT_minimal);146}147}148149bool MemTracker::transition_to(NMT_TrackingLevel level) {150NMT_TrackingLevel current_level = tracking_level();151152assert(level != NMT_off || current_level == NMT_off, "Cannot transition NMT to off");153154if (current_level == level) {155return true;156} else if (current_level > level) {157// Downgrade tracking level, we want to lower the tracking level first158_tracking_level = level;159// Make _tracking_level visible immediately.160OrderAccess::fence();161VirtualMemoryTracker::transition(current_level, level);162MallocTracker::transition(current_level, level);163ThreadStackTracker::transition(current_level, level);164} else {165// Upgrading tracking level is not supported and has never been supported.166// Allocating and deallocating malloc tracking structures is not thread safe and167// leads to inconsistencies unless a lot coarser locks are added.168}169return true;170}171172// Report during error reporting.173void MemTracker::error_report(outputStream* output) {174if (tracking_level() >= NMT_summary) {175report(true, output, MemReporterBase::default_scale); // just print summary for error case.176}177}178179// Report when handling PrintNMTStatistics before VM shutdown.180static volatile bool g_final_report_did_run = false;181void MemTracker::final_report(outputStream* output) {182// This function is called during both error reporting and normal VM exit.183// However, it should only ever run once. E.g. if the VM crashes after184// printing the final report during normal VM exit, it should not print185// the final report again. In addition, it should be guarded from186// recursive calls in case NMT reporting itself crashes.187if (Atomic::cmpxchg(&g_final_report_did_run, false, true) == false) {188NMT_TrackingLevel level = tracking_level();189if (level >= NMT_summary) {190report(level == NMT_summary, output, 1);191}192}193}194195void MemTracker::report(bool summary_only, outputStream* output, size_t scale) {196assert(output != NULL, "No output stream");197MemBaseline baseline;198if (baseline.baseline(summary_only)) {199if (summary_only) {200MemSummaryReporter rpt(baseline, output, scale);201rpt.report();202} else {203MemDetailReporter rpt(baseline, output, scale);204rpt.report();205output->print("Metaspace:");206// The basic metaspace report avoids any locking and should be safe to207// be called at any time.208MetaspaceUtils::print_basic_report(output, scale);209}210}211}212213void MemTracker::tuning_statistics(outputStream* out) {214// NMT statistics215out->print_cr("Native Memory Tracking Statistics:");216out->print_cr("Malloc allocation site table size: %d", MallocSiteTable::hash_buckets());217out->print_cr(" Tracking stack depth: %d", NMT_TrackingStackDepth);218NOT_PRODUCT(out->print_cr("Peak concurrent access: %d", MallocSiteTable::access_peak_count());)219out->cr();220MallocSiteTable::print_tuning_statistics(out);221}222223224