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