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