1 /*
   2  * Copyright (c) 2012, 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 "services/nmtDCmd.hpp"
  26 #include "services/memReporter.hpp"
  27 #include "services/memTracker.hpp"
  28 #include "utilities/globalDefinitions.hpp"
  29 
  30 NMTDCmd::NMTDCmd(outputStream* output,
  31   bool heap): DCmdWithParser(output, heap),
  32   _summary("summary", "request runtime to report current memory summary, " \
  33            "which includes total reserved and committed memory, along " \
  34            "with memory usage summary by each subsytem.",
  35            "BOOLEAN", false, "false"),
  36   _detail("detail", "request runtime to report memory allocation >= "
  37            "1K by each callsite.",
  38            "BOOLEAN", false, "false"),
  39   _baseline("baseline", "request runtime to baseline current memory usage, " \
  40             "so it can be compared against in later time.",
  41             "BOOLEAN", false, "false"),
  42   _summary_diff("summary.diff", "request runtime to report memory summary " \
  43             "comparison against previous baseline.",
  44             "BOOLEAN", false, "false"),
  45   _detail_diff("detail.diff", "request runtime to report memory detail " \
  46             "comparison against previous baseline, which shows the memory " \
  47             "allocation activities at different callsites.",
  48             "BOOLEAN", false, "false"),
  49   _shutdown("shutdown", "request runtime to shutdown itself and free the " \
  50             "memory used by runtime.",
  51             "BOOLEAN", false, "false"),
  52 #ifndef PRODUCT
  53   _debug("debug", "print tracker statistics. Debug only, not thread safe", \
  54             "BOOLEAN", false, "false"),
  55 #endif
  56   _scale("scale", "Memory usage in which scale, KB, MB or GB",
  57        "STRING", false, "KB") {
  58   _dcmdparser.add_dcmd_option(&_summary);
  59   _dcmdparser.add_dcmd_option(&_detail);
  60   _dcmdparser.add_dcmd_option(&_baseline);
  61   _dcmdparser.add_dcmd_option(&_summary_diff);
  62   _dcmdparser.add_dcmd_option(&_detail_diff);
  63   _dcmdparser.add_dcmd_option(&_shutdown);
  64 #ifndef PRODUCT
  65   _dcmdparser.add_dcmd_option(&_debug);
  66 #endif
  67   _dcmdparser.add_dcmd_option(&_scale);
  68 }
  69 
  70 void NMTDCmd::execute(TRAPS) {
  71   const char* scale_value = _scale.value();
  72   size_t scale_unit;
  73   if (strcmp(scale_value, "KB") == 0 || strcmp(scale_value, "kb") == 0) {
  74     scale_unit = K;
  75   } else if (strcmp(scale_value, "MB") == 0 ||
  76              strcmp(scale_value, "mb") == 0) {
  77     scale_unit = M;
  78   } else if (strcmp(scale_value, "GB") == 0 ||
  79              strcmp(scale_value, "gb") == 0) {
  80     scale_unit = G;
  81   } else {
  82     output()->print_cr("Incorrect scale value: %s", scale_value);
  83     return;
  84   }
  85 
  86   int nopt = 0;
  87   if(_summary.is_set()) { ++nopt; }
  88   if(_detail.is_set()) { ++nopt; }
  89   if(_baseline.is_set()) { ++nopt; }
  90   if(_summary_diff.is_set()) { ++nopt; }
  91   if(_detail_diff.is_set()) { ++nopt; }
  92   if(_shutdown.is_set()) { ++nopt; }
  93 #ifndef PRODUCT
  94   if(_debug.is_set()) { ++nopt; }
  95 #endif
  96 
  97   if(nopt > 1) {
  98       output()->print_cr("At most one of the following option can be specified: " \
  99         "summary, detail, baseline, summary.diff, detail.diff, shutdown"
 100 #ifndef PRODUCT
 101         " ,debug"
 102 #endif
 103       );
 104       return;
 105   }
 106 
 107   if(nopt == 0) {
 108       _summary.set_value(true);
 109   }
 110 
 111 #ifndef PRODUCT
 112   if (_debug.value()) {
 113     output()->print_cr("debug command is NOT thread-safe, may cause crash");
 114     MemTracker::print_tracker_stats(output());
 115     return;
 116   }
 117 #endif
 118 
 119   // native memory tracking has to be on
 120   if (!MemTracker::is_on() || MemTracker::shutdown_in_progress()) {
 121     // if it is not on, what's the reason?
 122     output()->print_cr(MemTracker::reason());
 123     return;
 124   }
 125 
 126   if (_summary.value()) {
 127     BaselineTTYOutputer outputer(output());
 128     MemTracker::print_memory_usage(outputer, scale_unit, true);
 129   } else if (_detail.value()) {
 130     BaselineTTYOutputer outputer(output());
 131     MemTracker::print_memory_usage(outputer, scale_unit, false);
 132   } else if (_baseline.value()) {
 133     if (MemTracker::baseline()) {
 134       output()->print_cr("Successfully baselined.");
 135     } else {
 136       output()->print_cr("Baseline failed.");
 137     }
 138   } else if (_summary_diff.value()) {
 139     if (MemTracker::has_baseline()) {
 140       BaselineTTYOutputer outputer(output());
 141       MemTracker::compare_memory_usage(outputer, scale_unit, true);
 142     } else {
 143       output()->print_cr("No baseline to compare, run 'baseline' command first");
 144     }
 145   } else if (_detail_diff.value()) {
 146     if (MemTracker::has_baseline()) {
 147       BaselineTTYOutputer outputer(output());
 148       MemTracker::compare_memory_usage(outputer, scale_unit, false);
 149     } else {
 150       output()->print_cr("No baseline to compare to, run 'baseline' command first");
 151     }
 152   } else if (_shutdown.value()) {
 153     MemTracker::shutdown(MemTracker::NMT_shutdown_user);
 154     output()->print_cr("Shutdown is in progress, it will take a few moments to " \
 155       "completely shutdown");
 156   } else {
 157     ShouldNotReachHere();
 158     output()->print_cr("Unknown command");
 159   }
 160 }
 161 
 162 int NMTDCmd::num_arguments() {
 163   ResourceMark rm;
 164   NMTDCmd* dcmd = new NMTDCmd(NULL, false);
 165   if (dcmd != NULL) {
 166     DCmdMark mark(dcmd);
 167     return dcmd->_dcmdparser.num_arguments();
 168   } else {
 169     return 0;
 170   }
 171 }
 172