src/share/vm/services/memTracker.cpp

Thu, 14 Aug 2014 09:02:51 -0400

author
zgu
date
Thu, 14 Aug 2014 09:02:51 -0400
changeset 7077
36c9011aaead
parent 7074
833b0f92429a
child 7078
c6211b707068
permissions
-rw-r--r--

8054368: nsk/jdi/VirtualMachine/exit/exit002 crash with detail tracking on (NMT2)
Summary: Dynamic allocate _reserved_regions instead of static object to avoid racing during process exit
Reviewed-by: dholmes, coleenp

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@7077 80 NMT_TrackingLevel level = tracking_level();
zgu@7077 81 if (level >= NMT_summary) {
zgu@7077 82 if (!VirtualMemoryTracker::late_initialize(level)) {
zgu@7077 83 shutdown();
zgu@7077 84 return;
zgu@7077 85 }
zgu@3936 86 _query_lock = new (std::nothrow) Mutex(Monitor::max_nonleaf, "NMT_queryLock");
zgu@7074 87 // Already OOM. It is unlikely, but still have to handle it.
zgu@3936 88 if (_query_lock == NULL) {
zgu@7074 89 shutdown();
zgu@3900 90 }
zgu@3900 91 }
zgu@3900 92 }
zgu@3900 93
zgu@7074 94 bool MemTracker::check_launcher_nmt_support(const char* value) {
zgu@7074 95 if (strcmp(value, "=detail") == 0) {
zgu@7074 96 #if !PLATFORM_NATIVE_STACK_WALKING_SUPPORTED
zgu@7074 97 jio_fprintf(defaultStream::error_stream(),
zgu@7074 98 "NMT detail is not supported on this platform. Using NMT summary instead.\n");
zgu@7074 99 if (MemTracker::tracking_level() != NMT_summary) {
zgu@7074 100 return false;
zgu@7074 101 }
zgu@7074 102 #else
zgu@7074 103 if (MemTracker::tracking_level() != NMT_detail) {
zgu@7074 104 return false;
zgu@7074 105 }
zgu@7074 106 #endif
zgu@7074 107 } else if (strcmp(value, "=summary") == 0) {
zgu@7074 108 if (MemTracker::tracking_level() != NMT_summary) {
zgu@7074 109 return false;
zgu@7074 110 }
zgu@7074 111 } else if (strcmp(value, "=off") == 0) {
zgu@7074 112 if (MemTracker::tracking_level() != NMT_off) {
zgu@7074 113 return false;
zgu@7074 114 }
zgu@7074 115 } else {
zgu@7074 116 _is_nmt_env_valid = false;
zgu@3900 117 }
zgu@3900 118
zgu@7074 119 return true;
zgu@3900 120 }
zgu@3900 121
zgu@7074 122 bool MemTracker::verify_nmt_option() {
zgu@7074 123 return _is_nmt_env_valid;
zgu@7074 124 }
zgu@7074 125
zgu@7074 126 void* MemTracker::malloc_base(void* memblock) {
zgu@7074 127 return MallocTracker::get_base(memblock);
zgu@7074 128 }
zgu@7074 129
zgu@7074 130 void Tracker::record(address addr, size_t size) {
zgu@7074 131 if (MemTracker::tracking_level() < NMT_summary) return;
zgu@7074 132 switch(_type) {
zgu@7074 133 case uncommit:
zgu@7074 134 VirtualMemoryTracker::remove_uncommitted_region(addr, size);
zgu@7074 135 break;
zgu@7074 136 case release:
zgu@7074 137 VirtualMemoryTracker::remove_released_region(addr, size);
zgu@7074 138 break;
zgu@7074 139 default:
zgu@7074 140 ShouldNotReachHere();
zgu@3900 141 }
zgu@3900 142 }
zgu@3900 143
zgu@7074 144
zgu@7074 145 // Shutdown can only be issued via JCmd, and NMT JCmd is serialized
zgu@7074 146 // by lock
zgu@7074 147 void MemTracker::shutdown() {
zgu@7074 148 // We can only shutdown NMT to minimal tracking level if it is
zgu@7074 149 // ever on.
zgu@7074 150 if (tracking_level () > NMT_minimal) {
zgu@7074 151 transition_to(NMT_minimal);
zgu@3900 152 }
zgu@3900 153 }
zgu@3900 154
zgu@7074 155 bool MemTracker::transition_to(NMT_TrackingLevel level) {
zgu@7074 156 NMT_TrackingLevel current_level = tracking_level();
zgu@3900 157
zgu@7074 158 if (current_level == level) {
zgu@7074 159 return true;
zgu@7074 160 } else if (current_level > level) {
zgu@7074 161 // Downgrade tracking level, we want to lower the tracking
zgu@7074 162 // level first
zgu@7074 163 _tracking_level = level;
zgu@7074 164 // Make _tracking_level visible immediately.
zgu@7074 165 OrderAccess::fence();
zgu@7074 166 VirtualMemoryTracker::transition(current_level, level);
zgu@7074 167 MallocTracker::transition(current_level, level);
zgu@7074 168
zgu@7074 169 if (level == NMT_minimal) _baseline.reset();
zgu@3900 170 } else {
zgu@7074 171 VirtualMemoryTracker::transition(current_level, level);
zgu@7074 172 MallocTracker::transition(current_level, level);
zgu@7074 173
zgu@7074 174 _tracking_level = level;
zgu@7074 175 // Make _tracking_level visible immediately.
zgu@7074 176 OrderAccess::fence();
zgu@3900 177 }
zgu@3900 178
zgu@7074 179 return true;
zgu@3900 180 }
zgu@3900 181
zgu@7074 182 void MemTracker::final_report(outputStream* output) {
zgu@7074 183 assert(output != NULL, "No output stream");
zgu@7074 184 if (tracking_level() >= NMT_summary) {
zgu@7074 185 MallocMemorySnapshot* malloc_memory_snapshot =
zgu@7074 186 MallocMemorySummary::as_snapshot();
zgu@7074 187 malloc_memory_snapshot->make_adjustment();
zgu@7074 188
zgu@7074 189 VirtualMemorySnapshot* virtual_memory_snapshot =
zgu@7074 190 VirtualMemorySummary::as_snapshot();
zgu@7074 191
zgu@7074 192 MemSummaryReporter rptr(malloc_memory_snapshot,
zgu@7074 193 virtual_memory_snapshot, output);
zgu@7074 194 rptr.report();
zgu@7074 195 // shutdown NMT, the data no longer accurate
zgu@7074 196 shutdown();
zgu@3900 197 }
zgu@3900 198 }
zgu@3900 199
zgu@7074 200 // This is a walker to gather malloc site hashtable statistics,
zgu@7074 201 // the result is used for tuning.
zgu@7074 202 class StatisticsWalker : public MallocSiteWalker {
zgu@7074 203 private:
zgu@7074 204 enum Threshold {
zgu@7074 205 // aggregates statistics over this threshold into one
zgu@7074 206 // line item.
zgu@7074 207 report_threshold = 20
zgu@7074 208 };
zgu@3900 209
zgu@7074 210 private:
zgu@7074 211 // Number of allocation sites that have all memory freed
zgu@7074 212 int _empty_entries;
zgu@7074 213 // Total number of allocation sites, include empty sites
zgu@7074 214 int _total_entries;
zgu@7074 215 // Number of captured call stack distribution
zgu@7074 216 int _stack_depth_distribution[NMT_TrackingStackDepth];
zgu@7074 217 // Hash distribution
zgu@7074 218 int _hash_distribution[report_threshold];
zgu@7074 219 // Number of hash buckets that have entries over the threshold
zgu@7074 220 int _bucket_over_threshold;
zgu@7074 221
zgu@7074 222 // The hash bucket that walker is currently walking
zgu@7074 223 int _current_hash_bucket;
zgu@7074 224 // The length of current hash bucket
zgu@7074 225 int _current_bucket_length;
zgu@7074 226 // Number of hash buckets that are not empty
zgu@7074 227 int _used_buckets;
zgu@7074 228 // Longest hash bucket length
zgu@7074 229 int _longest_bucket_length;
zgu@7074 230
zgu@7074 231 public:
zgu@7074 232 StatisticsWalker() : _empty_entries(0), _total_entries(0) {
zgu@7074 233 int index = 0;
zgu@7074 234 for (index = 0; index < NMT_TrackingStackDepth; index ++) {
zgu@7074 235 _stack_depth_distribution[index] = 0;
zgu@7074 236 }
zgu@7074 237 for (index = 0; index < report_threshold; index ++) {
zgu@7074 238 _hash_distribution[index] = 0;
zgu@7074 239 }
zgu@7074 240 _bucket_over_threshold = 0;
zgu@7074 241 _longest_bucket_length = 0;
zgu@7074 242 _current_hash_bucket = -1;
zgu@7074 243 _current_bucket_length = 0;
zgu@7074 244 _used_buckets = 0;
zgu@3900 245 }
zgu@3900 246
zgu@7074 247 virtual bool at(const MallocSite* e) {
zgu@7074 248 if (e->size() == 0) _empty_entries ++;
zgu@7074 249 _total_entries ++;
zgu@3900 250
zgu@7074 251 // stack depth distrubution
zgu@7074 252 int frames = e->call_stack()->frames();
zgu@7074 253 _stack_depth_distribution[frames - 1] ++;
zgu@3900 254
zgu@7074 255 // hash distribution
zgu@7074 256 int hash_bucket = e->hash() % MallocSiteTable::hash_buckets();
zgu@7074 257 if (_current_hash_bucket == -1) {
zgu@7074 258 _current_hash_bucket = hash_bucket;
zgu@7074 259 _current_bucket_length = 1;
zgu@7074 260 } else if (_current_hash_bucket == hash_bucket) {
zgu@7074 261 _current_bucket_length ++;
zgu@7074 262 } else {
zgu@7074 263 record_bucket_length(_current_bucket_length);
zgu@7074 264 _current_hash_bucket = hash_bucket;
zgu@7074 265 _current_bucket_length = 1;
zgu@3900 266 }
zgu@7074 267 return true;
zgu@3900 268 }
zgu@3900 269
zgu@7074 270 // walk completed
zgu@7074 271 void completed() {
zgu@7074 272 record_bucket_length(_current_bucket_length);
zgu@3900 273 }
zgu@3900 274
zgu@7074 275 void report_statistics(outputStream* out) {
zgu@7074 276 int index;
zgu@7074 277 out->print_cr("Malloc allocation site table:");
zgu@7074 278 out->print_cr("\tTotal entries: %d", _total_entries);
zgu@7074 279 out->print_cr("\tEmpty entries: %d (%2.2f%%)", _empty_entries, ((float)_empty_entries * 100) / _total_entries);
zgu@7074 280 out->print_cr(" ");
zgu@7074 281 out->print_cr("Hash distribution:");
zgu@7074 282 if (_used_buckets < MallocSiteTable::hash_buckets()) {
zgu@7074 283 out->print_cr("empty bucket: %d", (MallocSiteTable::hash_buckets() - _used_buckets));
zgu@7074 284 }
zgu@7074 285 for (index = 0; index < report_threshold; index ++) {
zgu@7074 286 if (_hash_distribution[index] != 0) {
zgu@7074 287 if (index == 0) {
zgu@7074 288 out->print_cr(" %d entry: %d", 1, _hash_distribution[0]);
zgu@7074 289 } else if (index < 9) { // single digit
zgu@7074 290 out->print_cr(" %d entries: %d", (index + 1), _hash_distribution[index]);
zgu@7074 291 } else {
zgu@7074 292 out->print_cr(" %d entries: %d", (index + 1), _hash_distribution[index]);
zgu@7074 293 }
zgu@3900 294 }
zgu@3900 295 }
zgu@7074 296 if (_bucket_over_threshold > 0) {
zgu@7074 297 out->print_cr(" >%d entries: %d", report_threshold, _bucket_over_threshold);
zgu@7074 298 }
zgu@7074 299 out->print_cr("most entries: %d", _longest_bucket_length);
zgu@7074 300 out->print_cr(" ");
zgu@7074 301 out->print_cr("Call stack depth distribution:");
zgu@7074 302 for (index = 0; index < NMT_TrackingStackDepth; index ++) {
zgu@7074 303 if (_stack_depth_distribution[index] > 0) {
zgu@7074 304 out->print_cr("\t%d: %d", index + 1, _stack_depth_distribution[index]);
zgu@3900 305 }
zgu@3900 306 }
zgu@3900 307 }
zgu@3900 308
zgu@7074 309 private:
zgu@7074 310 void record_bucket_length(int length) {
zgu@7074 311 _used_buckets ++;
zgu@7074 312 if (length <= report_threshold) {
zgu@7074 313 _hash_distribution[length - 1] ++;
zgu@7074 314 } else {
zgu@7074 315 _bucket_over_threshold ++;
zgu@3900 316 }
zgu@7074 317 _longest_bucket_length = MAX2(_longest_bucket_length, length);
zgu@3900 318 }
zgu@7074 319 };
zgu@7074 320
zgu@7074 321
zgu@7074 322 void MemTracker::tuning_statistics(outputStream* out) {
zgu@7074 323 // NMT statistics
zgu@7074 324 StatisticsWalker walker;
zgu@7074 325 MallocSiteTable::walk_malloc_site(&walker);
zgu@7074 326 walker.completed();
zgu@7074 327
zgu@7074 328 out->print_cr("Native Memory Tracking Statistics:");
zgu@7074 329 out->print_cr("Malloc allocation site table size: %d", MallocSiteTable::hash_buckets());
zgu@7074 330 out->print_cr(" Tracking stack depth: %d", NMT_TrackingStackDepth);
zgu@7074 331 NOT_PRODUCT(out->print_cr("Peak concurrent access: %d", MallocSiteTable::access_peak_count());)
zgu@7074 332 out->print_cr(" ");
zgu@7074 333 walker.report_statistics(out);
zgu@3900 334 }
zgu@3900 335

mercurial