Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/services/memTracker.cpp
32285 views
/*1* Copyright (c) 2012, 2014, 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/mutex.hpp"26#include "services/memBaseline.hpp"27#include "services/memReporter.hpp"28#include "services/mallocTracker.inline.hpp"29#include "services/memTracker.hpp"30#include "utilities/defaultStream.hpp"3132#ifdef SOLARIS33volatile bool NMT_stack_walkable = false;34#else35volatile bool NMT_stack_walkable = true;36#endif3738volatile NMT_TrackingLevel MemTracker::_tracking_level = NMT_unknown;39NMT_TrackingLevel MemTracker::_cmdline_tracking_level = NMT_unknown;4041MemBaseline MemTracker::_baseline;42Mutex* MemTracker::_query_lock = NULL;43bool MemTracker::_is_nmt_env_valid = true;444546NMT_TrackingLevel MemTracker::init_tracking_level() {47NMT_TrackingLevel level = NMT_off;48char buf[64];49char nmt_option[64];50jio_snprintf(buf, sizeof(buf), "NMT_LEVEL_%d", os::current_process_id());51if (os::getenv(buf, nmt_option, sizeof(nmt_option))) {52if (strcmp(nmt_option, "summary") == 0) {53level = NMT_summary;54} else if (strcmp(nmt_option, "detail") == 0) {55#if PLATFORM_NATIVE_STACK_WALKING_SUPPORTED56level = NMT_detail;57#else58level = NMT_summary;59#endif // PLATFORM_NATIVE_STACK_WALKING_SUPPORTED60} else if (strcmp(nmt_option, "off") != 0) {61// The option value is invalid62_is_nmt_env_valid = false;63}6465// Remove the environment variable to avoid leaking to child processes66os::unsetenv(buf);67}6869// Construct NativeCallStack::EMPTY_STACK. It may get constructed twice,70// but it is benign, the results are the same.71::new ((void*)&NativeCallStack::EMPTY_STACK) NativeCallStack(0, false);7273if (!MallocTracker::initialize(level) ||74!VirtualMemoryTracker::initialize(level)) {75level = NMT_off;76}77return level;78}7980void MemTracker::init() {81NMT_TrackingLevel level = tracking_level();82if (level >= NMT_summary) {83if (!VirtualMemoryTracker::late_initialize(level)) {84shutdown();85return;86}87_query_lock = new (std::nothrow) Mutex(Monitor::max_nonleaf, "NMT_queryLock");88// Already OOM. It is unlikely, but still have to handle it.89if (_query_lock == NULL) {90shutdown();91}92}93}9495bool MemTracker::check_launcher_nmt_support(const char* value) {96if (strcmp(value, "=detail") == 0) {97#if !PLATFORM_NATIVE_STACK_WALKING_SUPPORTED98jio_fprintf(defaultStream::error_stream(),99"NMT detail is not supported on this platform. Using NMT summary instead.\n");100if (MemTracker::tracking_level() != NMT_summary) {101return false;102}103#else104if (MemTracker::tracking_level() != NMT_detail) {105return false;106}107#endif108} else if (strcmp(value, "=summary") == 0) {109if (MemTracker::tracking_level() != NMT_summary) {110return false;111}112} else if (strcmp(value, "=off") == 0) {113if (MemTracker::tracking_level() != NMT_off) {114return false;115}116} else {117_is_nmt_env_valid = false;118}119120return true;121}122123bool MemTracker::verify_nmt_option() {124return _is_nmt_env_valid;125}126127void* MemTracker::malloc_base(void* memblock) {128return MallocTracker::get_base(memblock);129}130131void Tracker::record(address addr, size_t size) {132if (MemTracker::tracking_level() < NMT_summary) return;133switch(_type) {134case uncommit:135VirtualMemoryTracker::remove_uncommitted_region(addr, size);136break;137case release:138VirtualMemoryTracker::remove_released_region(addr, size);139break;140default:141ShouldNotReachHere();142}143}144145146// Shutdown can only be issued via JCmd, and NMT JCmd is serialized by lock147void MemTracker::shutdown() {148// We can only shutdown NMT to minimal tracking level if it is ever on.149if (tracking_level () > NMT_minimal) {150transition_to(NMT_minimal);151}152}153154bool MemTracker::transition_to(NMT_TrackingLevel level) {155NMT_TrackingLevel current_level = tracking_level();156157assert(level != NMT_off || current_level == NMT_off, "Cannot transition NMT to off");158159if (current_level == level) {160return true;161} else if (current_level > level) {162// Downgrade tracking level, we want to lower the tracking level first163_tracking_level = level;164// Make _tracking_level visible immediately.165OrderAccess::fence();166VirtualMemoryTracker::transition(current_level, level);167MallocTracker::transition(current_level, level);168} else {169// Upgrading tracking level is not supported and has never been supported.170// Allocating and deallocating malloc tracking structures is not thread safe and171// leads to inconsistencies unless a lot coarser locks are added.172}173return true;174}175176void MemTracker::report(bool summary_only, outputStream* output) {177assert(output != NULL, "No output stream");178MemBaseline baseline;179if (baseline.baseline(summary_only)) {180if (summary_only) {181MemSummaryReporter rpt(baseline, output);182rpt.report();183} else {184MemDetailReporter rpt(baseline, output);185rpt.report();186}187}188}189190// This is a walker to gather malloc site hashtable statistics,191// the result is used for tuning.192class StatisticsWalker : public MallocSiteWalker {193private:194enum Threshold {195// aggregates statistics over this threshold into one196// line item.197report_threshold = 20198};199200private:201// Number of allocation sites that have all memory freed202int _empty_entries;203// Total number of allocation sites, include empty sites204int _total_entries;205// Number of captured call stack distribution206int _stack_depth_distribution[NMT_TrackingStackDepth];207// Hash distribution208int _hash_distribution[report_threshold];209// Number of hash buckets that have entries over the threshold210int _bucket_over_threshold;211212// The hash bucket that walker is currently walking213int _current_hash_bucket;214// The length of current hash bucket215int _current_bucket_length;216// Number of hash buckets that are not empty217int _used_buckets;218// Longest hash bucket length219int _longest_bucket_length;220221public:222StatisticsWalker() : _empty_entries(0), _total_entries(0) {223int index = 0;224for (index = 0; index < NMT_TrackingStackDepth; index ++) {225_stack_depth_distribution[index] = 0;226}227for (index = 0; index < report_threshold; index ++) {228_hash_distribution[index] = 0;229}230_bucket_over_threshold = 0;231_longest_bucket_length = 0;232_current_hash_bucket = -1;233_current_bucket_length = 0;234_used_buckets = 0;235}236237virtual bool at(const MallocSite* e) {238if (e->size() == 0) _empty_entries ++;239_total_entries ++;240241// stack depth distrubution242int frames = e->call_stack()->frames();243_stack_depth_distribution[frames - 1] ++;244245// hash distribution246int hash_bucket = e->hash() % MallocSiteTable::hash_buckets();247if (_current_hash_bucket == -1) {248_current_hash_bucket = hash_bucket;249_current_bucket_length = 1;250} else if (_current_hash_bucket == hash_bucket) {251_current_bucket_length ++;252} else {253record_bucket_length(_current_bucket_length);254_current_hash_bucket = hash_bucket;255_current_bucket_length = 1;256}257return true;258}259260// walk completed261void completed() {262record_bucket_length(_current_bucket_length);263}264265void report_statistics(outputStream* out) {266int index;267out->print_cr("Malloc allocation site table:");268out->print_cr("\tTotal entries: %d", _total_entries);269out->print_cr("\tEmpty entries: %d (%2.2f%%)", _empty_entries, ((float)_empty_entries * 100) / _total_entries);270out->print_cr(" ");271out->print_cr("Hash distribution:");272if (_used_buckets < MallocSiteTable::hash_buckets()) {273out->print_cr("empty bucket: %d", (MallocSiteTable::hash_buckets() - _used_buckets));274}275for (index = 0; index < report_threshold; index ++) {276if (_hash_distribution[index] != 0) {277if (index == 0) {278out->print_cr(" %d entry: %d", 1, _hash_distribution[0]);279} else if (index < 9) { // single digit280out->print_cr(" %d entries: %d", (index + 1), _hash_distribution[index]);281} else {282out->print_cr(" %d entries: %d", (index + 1), _hash_distribution[index]);283}284}285}286if (_bucket_over_threshold > 0) {287out->print_cr(" >%d entries: %d", report_threshold, _bucket_over_threshold);288}289out->print_cr("most entries: %d", _longest_bucket_length);290out->print_cr(" ");291out->print_cr("Call stack depth distribution:");292for (index = 0; index < NMT_TrackingStackDepth; index ++) {293if (_stack_depth_distribution[index] > 0) {294out->print_cr("\t%d: %d", index + 1, _stack_depth_distribution[index]);295}296}297}298299private:300void record_bucket_length(int length) {301_used_buckets ++;302if (length <= report_threshold) {303_hash_distribution[length - 1] ++;304} else {305_bucket_over_threshold ++;306}307_longest_bucket_length = MAX2(_longest_bucket_length, length);308}309};310311312void MemTracker::tuning_statistics(outputStream* out) {313// NMT statistics314StatisticsWalker walker;315MallocSiteTable::walk_malloc_site(&walker);316walker.completed();317318out->print_cr("Native Memory Tracking Statistics:");319out->print_cr("Malloc allocation site table size: %d", MallocSiteTable::hash_buckets());320out->print_cr(" Tracking stack depth: %d", NMT_TrackingStackDepth);321NOT_PRODUCT(out->print_cr("Peak concurrent access: %d", MallocSiteTable::access_peak_count());)322out->print_cr(" ");323walker.report_statistics(out);324}325326327328