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