src/share/vm/services/memTracker.cpp

Wed, 27 Aug 2014 08:19:12 -0400

author
zgu
date
Wed, 27 Aug 2014 08:19:12 -0400
changeset 7074
833b0f92429a
parent 6911
ce8f6bb717c9
child 7077
36c9011aaead
permissions
-rw-r--r--

8046598: Scalable Native memory tracking development
Summary: Enhance scalability of native memory tracking
Reviewed-by: coleenp, ctornqvi, gtriantafill

zgu@3900 1 /*
zgu@7074 2 * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved.
zgu@3900 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
zgu@3900 4 *
zgu@3900 5 * This code is free software; you can redistribute it and/or modify it
zgu@3900 6 * under the terms of the GNU General Public License version 2 only, as
zgu@3900 7 * published by the Free Software Foundation.
zgu@3900 8 *
zgu@3900 9 * This code is distributed in the hope that it will be useful, but WITHOUT
zgu@3900 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
zgu@3900 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
zgu@3900 12 * version 2 for more details (a copy is included in the LICENSE file that
zgu@3900 13 * accompanied this code).
zgu@3900 14 *
zgu@3900 15 * You should have received a copy of the GNU General Public License version
zgu@3900 16 * 2 along with this work; if not, write to the Free Software Foundation,
zgu@3900 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
zgu@3900 18 *
zgu@3900 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
zgu@3900 20 * or visit www.oracle.com if you need additional information or have any
zgu@3900 21 * questions.
zgu@3900 22 *
zgu@3900 23 */
zgu@3900 24 #include "precompiled.hpp"
zgu@3900 25
zgu@7074 26 #include "runtime/mutex.hpp"
zgu@7074 27 #include "services/memBaseline.hpp"
zgu@3900 28 #include "services/memReporter.hpp"
zgu@7074 29 #include "services/mallocTracker.inline.hpp"
zgu@3900 30 #include "services/memTracker.hpp"
jprovino@5188 31 #include "utilities/defaultStream.hpp"
zgu@3900 32
zgu@7074 33 #ifdef SOLARIS
zgu@7074 34 volatile bool NMT_stack_walkable = false;
zgu@7074 35 #else
zgu@7074 36 volatile bool NMT_stack_walkable = true;
zgu@7074 37 #endif
zgu@3900 38
zgu@7074 39 volatile NMT_TrackingLevel MemTracker::_tracking_level = NMT_unknown;
zgu@7074 40 NMT_TrackingLevel MemTracker::_cmdline_tracking_level = NMT_unknown;
zgu@7074 41
zgu@7074 42 NativeCallStack emptyStack(0, false);
zgu@7074 43
zgu@7074 44 MemBaseline MemTracker::_baseline;
zgu@7074 45 Mutex* MemTracker::_query_lock = NULL;
zgu@7074 46 bool MemTracker::_is_nmt_env_valid = true;
zgu@7074 47
zgu@7074 48
zgu@7074 49 NMT_TrackingLevel MemTracker::init_tracking_level() {
zgu@7074 50 NMT_TrackingLevel level = NMT_off;
zgu@7074 51 char buf[64];
zgu@7074 52 char nmt_option[64];
zgu@7074 53 jio_snprintf(buf, sizeof(buf), "NMT_LEVEL_%d", os::current_process_id());
zgu@7074 54 if (os::getenv(buf, nmt_option, sizeof(nmt_option))) {
zgu@7074 55 if (strcmp(nmt_option, "summary") == 0) {
zgu@7074 56 level = NMT_summary;
zgu@7074 57 } else if (strcmp(nmt_option, "detail") == 0) {
zgu@7074 58 #if PLATFORM_NATIVE_STACK_WALKING_SUPPORTED
zgu@7074 59 level = NMT_detail;
zgu@7074 60 #else
zgu@7074 61 level = NMT_summary;
zgu@7074 62 #endif // PLATFORM_NATIVE_STACK_WALKING_SUPPORTED
zgu@7074 63 } else if (strcmp(nmt_option, "off") != 0) {
zgu@7074 64 // The option value is invalid
zgu@7074 65 _is_nmt_env_valid = false;
zgu@3900 66 }
zgu@7074 67
zgu@7074 68 // Remove the environment variable to avoid leaking to child processes
zgu@7074 69 os::unsetenv(buf);
zgu@3900 70 }
zgu@7074 71
zgu@7074 72 if (!MallocTracker::initialize(level) ||
zgu@7074 73 !VirtualMemoryTracker::initialize(level)) {
zgu@7074 74 level = NMT_off;
zgu@7074 75 }
zgu@7074 76 return level;
zgu@3900 77 }
zgu@3900 78
zgu@7074 79 void MemTracker::init() {
zgu@7074 80 if (tracking_level() >= NMT_summary) {
zgu@3936 81 _query_lock = new (std::nothrow) Mutex(Monitor::max_nonleaf, "NMT_queryLock");
zgu@7074 82 // Already OOM. It is unlikely, but still have to handle it.
zgu@3936 83 if (_query_lock == NULL) {
zgu@7074 84 shutdown();
zgu@3900 85 }
zgu@3900 86 }
zgu@3900 87 }
zgu@3900 88
zgu@7074 89 bool MemTracker::check_launcher_nmt_support(const char* value) {
zgu@7074 90 if (strcmp(value, "=detail") == 0) {
zgu@7074 91 #if !PLATFORM_NATIVE_STACK_WALKING_SUPPORTED
zgu@7074 92 jio_fprintf(defaultStream::error_stream(),
zgu@7074 93 "NMT detail is not supported on this platform. Using NMT summary instead.\n");
zgu@7074 94 if (MemTracker::tracking_level() != NMT_summary) {
zgu@7074 95 return false;
zgu@7074 96 }
zgu@7074 97 #else
zgu@7074 98 if (MemTracker::tracking_level() != NMT_detail) {
zgu@7074 99 return false;
zgu@7074 100 }
zgu@7074 101 #endif
zgu@7074 102 } else if (strcmp(value, "=summary") == 0) {
zgu@7074 103 if (MemTracker::tracking_level() != NMT_summary) {
zgu@7074 104 return false;
zgu@7074 105 }
zgu@7074 106 } else if (strcmp(value, "=off") == 0) {
zgu@7074 107 if (MemTracker::tracking_level() != NMT_off) {
zgu@7074 108 return false;
zgu@7074 109 }
zgu@7074 110 } else {
zgu@7074 111 _is_nmt_env_valid = false;
zgu@3900 112 }
zgu@3900 113
zgu@7074 114 return true;
zgu@3900 115 }
zgu@3900 116
zgu@7074 117 bool MemTracker::verify_nmt_option() {
zgu@7074 118 return _is_nmt_env_valid;
zgu@7074 119 }
zgu@7074 120
zgu@7074 121 void* MemTracker::malloc_base(void* memblock) {
zgu@7074 122 return MallocTracker::get_base(memblock);
zgu@7074 123 }
zgu@7074 124
zgu@7074 125 void Tracker::record(address addr, size_t size) {
zgu@7074 126 if (MemTracker::tracking_level() < NMT_summary) return;
zgu@7074 127 switch(_type) {
zgu@7074 128 case uncommit:
zgu@7074 129 VirtualMemoryTracker::remove_uncommitted_region(addr, size);
zgu@7074 130 break;
zgu@7074 131 case release:
zgu@7074 132 VirtualMemoryTracker::remove_released_region(addr, size);
zgu@7074 133 break;
zgu@7074 134 default:
zgu@7074 135 ShouldNotReachHere();
zgu@3900 136 }
zgu@3900 137 }
zgu@3900 138
zgu@7074 139
zgu@7074 140 // Shutdown can only be issued via JCmd, and NMT JCmd is serialized
zgu@7074 141 // by lock
zgu@7074 142 void MemTracker::shutdown() {
zgu@7074 143 // We can only shutdown NMT to minimal tracking level if it is
zgu@7074 144 // ever on.
zgu@7074 145 if (tracking_level () > NMT_minimal) {
zgu@7074 146 transition_to(NMT_minimal);
zgu@3900 147 }
zgu@3900 148 }
zgu@3900 149
zgu@7074 150 bool MemTracker::transition_to(NMT_TrackingLevel level) {
zgu@7074 151 NMT_TrackingLevel current_level = tracking_level();
zgu@3900 152
zgu@7074 153 if (current_level == level) {
zgu@7074 154 return true;
zgu@7074 155 } else if (current_level > level) {
zgu@7074 156 // Downgrade tracking level, we want to lower the tracking
zgu@7074 157 // level first
zgu@7074 158 _tracking_level = level;
zgu@7074 159 // Make _tracking_level visible immediately.
zgu@7074 160 OrderAccess::fence();
zgu@7074 161 VirtualMemoryTracker::transition(current_level, level);
zgu@7074 162 MallocTracker::transition(current_level, level);
zgu@7074 163
zgu@7074 164 if (level == NMT_minimal) _baseline.reset();
zgu@3900 165 } else {
zgu@7074 166 VirtualMemoryTracker::transition(current_level, level);
zgu@7074 167 MallocTracker::transition(current_level, level);
zgu@7074 168
zgu@7074 169 _tracking_level = level;
zgu@7074 170 // Make _tracking_level visible immediately.
zgu@7074 171 OrderAccess::fence();
zgu@3900 172 }
zgu@3900 173
zgu@7074 174 return true;
zgu@3900 175 }
zgu@3900 176
zgu@7074 177 void MemTracker::final_report(outputStream* output) {
zgu@7074 178 assert(output != NULL, "No output stream");
zgu@7074 179 if (tracking_level() >= NMT_summary) {
zgu@7074 180 MallocMemorySnapshot* malloc_memory_snapshot =
zgu@7074 181 MallocMemorySummary::as_snapshot();
zgu@7074 182 malloc_memory_snapshot->make_adjustment();
zgu@7074 183
zgu@7074 184 VirtualMemorySnapshot* virtual_memory_snapshot =
zgu@7074 185 VirtualMemorySummary::as_snapshot();
zgu@7074 186
zgu@7074 187 MemSummaryReporter rptr(malloc_memory_snapshot,
zgu@7074 188 virtual_memory_snapshot, output);
zgu@7074 189 rptr.report();
zgu@7074 190 // shutdown NMT, the data no longer accurate
zgu@7074 191 shutdown();
zgu@3900 192 }
zgu@3900 193 }
zgu@3900 194
zgu@7074 195 // This is a walker to gather malloc site hashtable statistics,
zgu@7074 196 // the result is used for tuning.
zgu@7074 197 class StatisticsWalker : public MallocSiteWalker {
zgu@7074 198 private:
zgu@7074 199 enum Threshold {
zgu@7074 200 // aggregates statistics over this threshold into one
zgu@7074 201 // line item.
zgu@7074 202 report_threshold = 20
zgu@7074 203 };
zgu@3900 204
zgu@7074 205 private:
zgu@7074 206 // Number of allocation sites that have all memory freed
zgu@7074 207 int _empty_entries;
zgu@7074 208 // Total number of allocation sites, include empty sites
zgu@7074 209 int _total_entries;
zgu@7074 210 // Number of captured call stack distribution
zgu@7074 211 int _stack_depth_distribution[NMT_TrackingStackDepth];
zgu@7074 212 // Hash distribution
zgu@7074 213 int _hash_distribution[report_threshold];
zgu@7074 214 // Number of hash buckets that have entries over the threshold
zgu@7074 215 int _bucket_over_threshold;
zgu@7074 216
zgu@7074 217 // The hash bucket that walker is currently walking
zgu@7074 218 int _current_hash_bucket;
zgu@7074 219 // The length of current hash bucket
zgu@7074 220 int _current_bucket_length;
zgu@7074 221 // Number of hash buckets that are not empty
zgu@7074 222 int _used_buckets;
zgu@7074 223 // Longest hash bucket length
zgu@7074 224 int _longest_bucket_length;
zgu@7074 225
zgu@7074 226 public:
zgu@7074 227 StatisticsWalker() : _empty_entries(0), _total_entries(0) {
zgu@7074 228 int index = 0;
zgu@7074 229 for (index = 0; index < NMT_TrackingStackDepth; index ++) {
zgu@7074 230 _stack_depth_distribution[index] = 0;
zgu@7074 231 }
zgu@7074 232 for (index = 0; index < report_threshold; index ++) {
zgu@7074 233 _hash_distribution[index] = 0;
zgu@7074 234 }
zgu@7074 235 _bucket_over_threshold = 0;
zgu@7074 236 _longest_bucket_length = 0;
zgu@7074 237 _current_hash_bucket = -1;
zgu@7074 238 _current_bucket_length = 0;
zgu@7074 239 _used_buckets = 0;
zgu@3900 240 }
zgu@3900 241
zgu@7074 242 virtual bool at(const MallocSite* e) {
zgu@7074 243 if (e->size() == 0) _empty_entries ++;
zgu@7074 244 _total_entries ++;
zgu@3900 245
zgu@7074 246 // stack depth distrubution
zgu@7074 247 int frames = e->call_stack()->frames();
zgu@7074 248 _stack_depth_distribution[frames - 1] ++;
zgu@3900 249
zgu@7074 250 // hash distribution
zgu@7074 251 int hash_bucket = e->hash() % MallocSiteTable::hash_buckets();
zgu@7074 252 if (_current_hash_bucket == -1) {
zgu@7074 253 _current_hash_bucket = hash_bucket;
zgu@7074 254 _current_bucket_length = 1;
zgu@7074 255 } else if (_current_hash_bucket == hash_bucket) {
zgu@7074 256 _current_bucket_length ++;
zgu@7074 257 } else {
zgu@7074 258 record_bucket_length(_current_bucket_length);
zgu@7074 259 _current_hash_bucket = hash_bucket;
zgu@7074 260 _current_bucket_length = 1;
zgu@3900 261 }
zgu@7074 262 return true;
zgu@3900 263 }
zgu@3900 264
zgu@7074 265 // walk completed
zgu@7074 266 void completed() {
zgu@7074 267 record_bucket_length(_current_bucket_length);
zgu@3900 268 }
zgu@3900 269
zgu@7074 270 void report_statistics(outputStream* out) {
zgu@7074 271 int index;
zgu@7074 272 out->print_cr("Malloc allocation site table:");
zgu@7074 273 out->print_cr("\tTotal entries: %d", _total_entries);
zgu@7074 274 out->print_cr("\tEmpty entries: %d (%2.2f%%)", _empty_entries, ((float)_empty_entries * 100) / _total_entries);
zgu@7074 275 out->print_cr(" ");
zgu@7074 276 out->print_cr("Hash distribution:");
zgu@7074 277 if (_used_buckets < MallocSiteTable::hash_buckets()) {
zgu@7074 278 out->print_cr("empty bucket: %d", (MallocSiteTable::hash_buckets() - _used_buckets));
zgu@7074 279 }
zgu@7074 280 for (index = 0; index < report_threshold; index ++) {
zgu@7074 281 if (_hash_distribution[index] != 0) {
zgu@7074 282 if (index == 0) {
zgu@7074 283 out->print_cr(" %d entry: %d", 1, _hash_distribution[0]);
zgu@7074 284 } else if (index < 9) { // single digit
zgu@7074 285 out->print_cr(" %d entries: %d", (index + 1), _hash_distribution[index]);
zgu@7074 286 } else {
zgu@7074 287 out->print_cr(" %d entries: %d", (index + 1), _hash_distribution[index]);
zgu@7074 288 }
zgu@3900 289 }
zgu@3900 290 }
zgu@7074 291 if (_bucket_over_threshold > 0) {
zgu@7074 292 out->print_cr(" >%d entries: %d", report_threshold, _bucket_over_threshold);
zgu@7074 293 }
zgu@7074 294 out->print_cr("most entries: %d", _longest_bucket_length);
zgu@7074 295 out->print_cr(" ");
zgu@7074 296 out->print_cr("Call stack depth distribution:");
zgu@7074 297 for (index = 0; index < NMT_TrackingStackDepth; index ++) {
zgu@7074 298 if (_stack_depth_distribution[index] > 0) {
zgu@7074 299 out->print_cr("\t%d: %d", index + 1, _stack_depth_distribution[index]);
zgu@3900 300 }
zgu@3900 301 }
zgu@3900 302 }
zgu@3900 303
zgu@7074 304 private:
zgu@7074 305 void record_bucket_length(int length) {
zgu@7074 306 _used_buckets ++;
zgu@7074 307 if (length <= report_threshold) {
zgu@7074 308 _hash_distribution[length - 1] ++;
zgu@7074 309 } else {
zgu@7074 310 _bucket_over_threshold ++;
zgu@3900 311 }
zgu@7074 312 _longest_bucket_length = MAX2(_longest_bucket_length, length);
zgu@3900 313 }
zgu@7074 314 };
zgu@7074 315
zgu@7074 316
zgu@7074 317 void MemTracker::tuning_statistics(outputStream* out) {
zgu@7074 318 // NMT statistics
zgu@7074 319 StatisticsWalker walker;
zgu@7074 320 MallocSiteTable::walk_malloc_site(&walker);
zgu@7074 321 walker.completed();
zgu@7074 322
zgu@7074 323 out->print_cr("Native Memory Tracking Statistics:");
zgu@7074 324 out->print_cr("Malloc allocation site table size: %d", MallocSiteTable::hash_buckets());
zgu@7074 325 out->print_cr(" Tracking stack depth: %d", NMT_TrackingStackDepth);
zgu@7074 326 NOT_PRODUCT(out->print_cr("Peak concurrent access: %d", MallocSiteTable::access_peak_count());)
zgu@7074 327 out->print_cr(" ");
zgu@7074 328 walker.report_statistics(out);
zgu@3900 329 }
zgu@3900 330

mercurial