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