zgu@3900: /* zgu@7074: * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved. zgu@3900: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. zgu@3900: * zgu@3900: * This code is free software; you can redistribute it and/or modify it zgu@3900: * under the terms of the GNU General Public License version 2 only, as zgu@3900: * published by the Free Software Foundation. zgu@3900: * zgu@3900: * This code is distributed in the hope that it will be useful, but WITHOUT zgu@3900: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or zgu@3900: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License zgu@3900: * version 2 for more details (a copy is included in the LICENSE file that zgu@3900: * accompanied this code). zgu@3900: * zgu@3900: * You should have received a copy of the GNU General Public License version zgu@3900: * 2 along with this work; if not, write to the Free Software Foundation, zgu@3900: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. zgu@3900: * zgu@3900: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA zgu@3900: * or visit www.oracle.com if you need additional information or have any zgu@3900: * questions. zgu@3900: * zgu@3900: */ zgu@3900: #include "precompiled.hpp" zgu@3900: zgu@7074: #include "runtime/mutex.hpp" zgu@7074: #include "services/memBaseline.hpp" zgu@3900: #include "services/memReporter.hpp" zgu@7074: #include "services/mallocTracker.inline.hpp" zgu@3900: #include "services/memTracker.hpp" jprovino@5188: #include "utilities/defaultStream.hpp" zgu@3900: zgu@7074: #ifdef SOLARIS zgu@7074: volatile bool NMT_stack_walkable = false; zgu@7074: #else zgu@7074: volatile bool NMT_stack_walkable = true; zgu@7074: #endif zgu@3900: zgu@7074: volatile NMT_TrackingLevel MemTracker::_tracking_level = NMT_unknown; zgu@7074: NMT_TrackingLevel MemTracker::_cmdline_tracking_level = NMT_unknown; zgu@7074: zgu@7074: NativeCallStack emptyStack(0, false); zgu@7074: zgu@7074: MemBaseline MemTracker::_baseline; zgu@7074: Mutex* MemTracker::_query_lock = NULL; zgu@7074: bool MemTracker::_is_nmt_env_valid = true; zgu@7074: zgu@7074: zgu@7074: NMT_TrackingLevel MemTracker::init_tracking_level() { zgu@7074: NMT_TrackingLevel level = NMT_off; zgu@7074: char buf[64]; zgu@7074: char nmt_option[64]; zgu@7074: jio_snprintf(buf, sizeof(buf), "NMT_LEVEL_%d", os::current_process_id()); zgu@7074: if (os::getenv(buf, nmt_option, sizeof(nmt_option))) { zgu@7074: if (strcmp(nmt_option, "summary") == 0) { zgu@7074: level = NMT_summary; zgu@7074: } else if (strcmp(nmt_option, "detail") == 0) { zgu@7074: #if PLATFORM_NATIVE_STACK_WALKING_SUPPORTED zgu@7074: level = NMT_detail; zgu@7074: #else zgu@7074: level = NMT_summary; zgu@7074: #endif // PLATFORM_NATIVE_STACK_WALKING_SUPPORTED zgu@7074: } else if (strcmp(nmt_option, "off") != 0) { zgu@7074: // The option value is invalid zgu@7074: _is_nmt_env_valid = false; zgu@3900: } zgu@7074: zgu@7074: // Remove the environment variable to avoid leaking to child processes zgu@7074: os::unsetenv(buf); zgu@3900: } zgu@7074: zgu@7074: if (!MallocTracker::initialize(level) || zgu@7074: !VirtualMemoryTracker::initialize(level)) { zgu@7074: level = NMT_off; zgu@7074: } zgu@7074: return level; zgu@3900: } zgu@3900: zgu@7074: void MemTracker::init() { zgu@7074: if (tracking_level() >= NMT_summary) { zgu@3936: _query_lock = new (std::nothrow) Mutex(Monitor::max_nonleaf, "NMT_queryLock"); zgu@7074: // Already OOM. It is unlikely, but still have to handle it. zgu@3936: if (_query_lock == NULL) { zgu@7074: shutdown(); zgu@3900: } zgu@3900: } zgu@3900: } zgu@3900: zgu@7074: bool MemTracker::check_launcher_nmt_support(const char* value) { zgu@7074: if (strcmp(value, "=detail") == 0) { zgu@7074: #if !PLATFORM_NATIVE_STACK_WALKING_SUPPORTED zgu@7074: jio_fprintf(defaultStream::error_stream(), zgu@7074: "NMT detail is not supported on this platform. Using NMT summary instead.\n"); zgu@7074: if (MemTracker::tracking_level() != NMT_summary) { zgu@7074: return false; zgu@7074: } zgu@7074: #else zgu@7074: if (MemTracker::tracking_level() != NMT_detail) { zgu@7074: return false; zgu@7074: } zgu@7074: #endif zgu@7074: } else if (strcmp(value, "=summary") == 0) { zgu@7074: if (MemTracker::tracking_level() != NMT_summary) { zgu@7074: return false; zgu@7074: } zgu@7074: } else if (strcmp(value, "=off") == 0) { zgu@7074: if (MemTracker::tracking_level() != NMT_off) { zgu@7074: return false; zgu@7074: } zgu@7074: } else { zgu@7074: _is_nmt_env_valid = false; zgu@3900: } zgu@3900: zgu@7074: return true; zgu@3900: } zgu@3900: zgu@7074: bool MemTracker::verify_nmt_option() { zgu@7074: return _is_nmt_env_valid; zgu@7074: } zgu@7074: zgu@7074: void* MemTracker::malloc_base(void* memblock) { zgu@7074: return MallocTracker::get_base(memblock); zgu@7074: } zgu@7074: zgu@7074: void Tracker::record(address addr, size_t size) { zgu@7074: if (MemTracker::tracking_level() < NMT_summary) return; zgu@7074: switch(_type) { zgu@7074: case uncommit: zgu@7074: VirtualMemoryTracker::remove_uncommitted_region(addr, size); zgu@7074: break; zgu@7074: case release: zgu@7074: VirtualMemoryTracker::remove_released_region(addr, size); zgu@7074: break; zgu@7074: default: zgu@7074: ShouldNotReachHere(); zgu@3900: } zgu@3900: } zgu@3900: zgu@7074: zgu@7074: // Shutdown can only be issued via JCmd, and NMT JCmd is serialized zgu@7074: // by lock zgu@7074: void MemTracker::shutdown() { zgu@7074: // We can only shutdown NMT to minimal tracking level if it is zgu@7074: // ever on. zgu@7074: if (tracking_level () > NMT_minimal) { zgu@7074: transition_to(NMT_minimal); zgu@3900: } zgu@3900: } zgu@3900: zgu@7074: bool MemTracker::transition_to(NMT_TrackingLevel level) { zgu@7074: NMT_TrackingLevel current_level = tracking_level(); zgu@3900: zgu@7074: if (current_level == level) { zgu@7074: return true; zgu@7074: } else if (current_level > level) { zgu@7074: // Downgrade tracking level, we want to lower the tracking zgu@7074: // level first zgu@7074: _tracking_level = level; zgu@7074: // Make _tracking_level visible immediately. zgu@7074: OrderAccess::fence(); zgu@7074: VirtualMemoryTracker::transition(current_level, level); zgu@7074: MallocTracker::transition(current_level, level); zgu@7074: zgu@7074: if (level == NMT_minimal) _baseline.reset(); zgu@3900: } else { zgu@7074: VirtualMemoryTracker::transition(current_level, level); zgu@7074: MallocTracker::transition(current_level, level); zgu@7074: zgu@7074: _tracking_level = level; zgu@7074: // Make _tracking_level visible immediately. zgu@7074: OrderAccess::fence(); zgu@3900: } zgu@3900: zgu@7074: return true; zgu@3900: } zgu@3900: zgu@7074: void MemTracker::final_report(outputStream* output) { zgu@7074: assert(output != NULL, "No output stream"); zgu@7074: if (tracking_level() >= NMT_summary) { zgu@7074: MallocMemorySnapshot* malloc_memory_snapshot = zgu@7074: MallocMemorySummary::as_snapshot(); zgu@7074: malloc_memory_snapshot->make_adjustment(); zgu@7074: zgu@7074: VirtualMemorySnapshot* virtual_memory_snapshot = zgu@7074: VirtualMemorySummary::as_snapshot(); zgu@7074: zgu@7074: MemSummaryReporter rptr(malloc_memory_snapshot, zgu@7074: virtual_memory_snapshot, output); zgu@7074: rptr.report(); zgu@7074: // shutdown NMT, the data no longer accurate zgu@7074: shutdown(); zgu@3900: } zgu@3900: } zgu@3900: zgu@7074: // This is a walker to gather malloc site hashtable statistics, zgu@7074: // the result is used for tuning. zgu@7074: class StatisticsWalker : public MallocSiteWalker { zgu@7074: private: zgu@7074: enum Threshold { zgu@7074: // aggregates statistics over this threshold into one zgu@7074: // line item. zgu@7074: report_threshold = 20 zgu@7074: }; zgu@3900: zgu@7074: private: zgu@7074: // Number of allocation sites that have all memory freed zgu@7074: int _empty_entries; zgu@7074: // Total number of allocation sites, include empty sites zgu@7074: int _total_entries; zgu@7074: // Number of captured call stack distribution zgu@7074: int _stack_depth_distribution[NMT_TrackingStackDepth]; zgu@7074: // Hash distribution zgu@7074: int _hash_distribution[report_threshold]; zgu@7074: // Number of hash buckets that have entries over the threshold zgu@7074: int _bucket_over_threshold; zgu@7074: zgu@7074: // The hash bucket that walker is currently walking zgu@7074: int _current_hash_bucket; zgu@7074: // The length of current hash bucket zgu@7074: int _current_bucket_length; zgu@7074: // Number of hash buckets that are not empty zgu@7074: int _used_buckets; zgu@7074: // Longest hash bucket length zgu@7074: int _longest_bucket_length; zgu@7074: zgu@7074: public: zgu@7074: StatisticsWalker() : _empty_entries(0), _total_entries(0) { zgu@7074: int index = 0; zgu@7074: for (index = 0; index < NMT_TrackingStackDepth; index ++) { zgu@7074: _stack_depth_distribution[index] = 0; zgu@7074: } zgu@7074: for (index = 0; index < report_threshold; index ++) { zgu@7074: _hash_distribution[index] = 0; zgu@7074: } zgu@7074: _bucket_over_threshold = 0; zgu@7074: _longest_bucket_length = 0; zgu@7074: _current_hash_bucket = -1; zgu@7074: _current_bucket_length = 0; zgu@7074: _used_buckets = 0; zgu@3900: } zgu@3900: zgu@7074: virtual bool at(const MallocSite* e) { zgu@7074: if (e->size() == 0) _empty_entries ++; zgu@7074: _total_entries ++; zgu@3900: zgu@7074: // stack depth distrubution zgu@7074: int frames = e->call_stack()->frames(); zgu@7074: _stack_depth_distribution[frames - 1] ++; zgu@3900: zgu@7074: // hash distribution zgu@7074: int hash_bucket = e->hash() % MallocSiteTable::hash_buckets(); zgu@7074: if (_current_hash_bucket == -1) { zgu@7074: _current_hash_bucket = hash_bucket; zgu@7074: _current_bucket_length = 1; zgu@7074: } else if (_current_hash_bucket == hash_bucket) { zgu@7074: _current_bucket_length ++; zgu@7074: } else { zgu@7074: record_bucket_length(_current_bucket_length); zgu@7074: _current_hash_bucket = hash_bucket; zgu@7074: _current_bucket_length = 1; zgu@3900: } zgu@7074: return true; zgu@3900: } zgu@3900: zgu@7074: // walk completed zgu@7074: void completed() { zgu@7074: record_bucket_length(_current_bucket_length); zgu@3900: } zgu@3900: zgu@7074: void report_statistics(outputStream* out) { zgu@7074: int index; zgu@7074: out->print_cr("Malloc allocation site table:"); zgu@7074: out->print_cr("\tTotal entries: %d", _total_entries); zgu@7074: out->print_cr("\tEmpty entries: %d (%2.2f%%)", _empty_entries, ((float)_empty_entries * 100) / _total_entries); zgu@7074: out->print_cr(" "); zgu@7074: out->print_cr("Hash distribution:"); zgu@7074: if (_used_buckets < MallocSiteTable::hash_buckets()) { zgu@7074: out->print_cr("empty bucket: %d", (MallocSiteTable::hash_buckets() - _used_buckets)); zgu@7074: } zgu@7074: for (index = 0; index < report_threshold; index ++) { zgu@7074: if (_hash_distribution[index] != 0) { zgu@7074: if (index == 0) { zgu@7074: out->print_cr(" %d entry: %d", 1, _hash_distribution[0]); zgu@7074: } else if (index < 9) { // single digit zgu@7074: out->print_cr(" %d entries: %d", (index + 1), _hash_distribution[index]); zgu@7074: } else { zgu@7074: out->print_cr(" %d entries: %d", (index + 1), _hash_distribution[index]); zgu@7074: } zgu@3900: } zgu@3900: } zgu@7074: if (_bucket_over_threshold > 0) { zgu@7074: out->print_cr(" >%d entries: %d", report_threshold, _bucket_over_threshold); zgu@7074: } zgu@7074: out->print_cr("most entries: %d", _longest_bucket_length); zgu@7074: out->print_cr(" "); zgu@7074: out->print_cr("Call stack depth distribution:"); zgu@7074: for (index = 0; index < NMT_TrackingStackDepth; index ++) { zgu@7074: if (_stack_depth_distribution[index] > 0) { zgu@7074: out->print_cr("\t%d: %d", index + 1, _stack_depth_distribution[index]); zgu@3900: } zgu@3900: } zgu@3900: } zgu@3900: zgu@7074: private: zgu@7074: void record_bucket_length(int length) { zgu@7074: _used_buckets ++; zgu@7074: if (length <= report_threshold) { zgu@7074: _hash_distribution[length - 1] ++; zgu@7074: } else { zgu@7074: _bucket_over_threshold ++; zgu@3900: } zgu@7074: _longest_bucket_length = MAX2(_longest_bucket_length, length); zgu@3900: } zgu@7074: }; zgu@7074: zgu@7074: zgu@7074: void MemTracker::tuning_statistics(outputStream* out) { zgu@7074: // NMT statistics zgu@7074: StatisticsWalker walker; zgu@7074: MallocSiteTable::walk_malloc_site(&walker); zgu@7074: walker.completed(); zgu@7074: zgu@7074: out->print_cr("Native Memory Tracking Statistics:"); zgu@7074: out->print_cr("Malloc allocation site table size: %d", MallocSiteTable::hash_buckets()); zgu@7074: out->print_cr(" Tracking stack depth: %d", NMT_TrackingStackDepth); zgu@7074: NOT_PRODUCT(out->print_cr("Peak concurrent access: %d", MallocSiteTable::access_peak_count());) zgu@7074: out->print_cr(" "); zgu@7074: walker.report_statistics(out); zgu@3900: } zgu@3900: