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