src/share/vm/services/nmtDCmd.cpp

Thu, 28 Jun 2012 17:03:16 -0400

author
zgu
date
Thu, 28 Jun 2012 17:03:16 -0400
changeset 3900
d2a62e0f25eb
child 4348
3f84e17b6bca
permissions
-rw-r--r--

6995781: Native Memory Tracking (Phase 1)
7151532: DCmd for hotspot native memory tracking
Summary: Implementation of native memory tracking phase 1, which tracks VM native memory usage, and related DCmd
Reviewed-by: acorn, coleenp, fparain

zgu@3900 1 /*
zgu@3900 2 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
zgu@3900 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
zgu@3900 4 *
zgu@3900 5 * This code is free software; you can redistribute it and/or modify it
zgu@3900 6 * under the terms of the GNU General Public License version 2 only, as
zgu@3900 7 * published by the Free Software Foundation.
zgu@3900 8 *
zgu@3900 9 * This code is distributed in the hope that it will be useful, but WITHOUT
zgu@3900 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
zgu@3900 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
zgu@3900 12 * version 2 for more details (a copy is included in the LICENSE file that
zgu@3900 13 * accompanied this code).
zgu@3900 14 *
zgu@3900 15 * You should have received a copy of the GNU General Public License version
zgu@3900 16 * 2 along with this work; if not, write to the Free Software Foundation,
zgu@3900 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
zgu@3900 18 *
zgu@3900 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
zgu@3900 20 * or visit www.oracle.com if you need additional information or have any
zgu@3900 21 * questions.
zgu@3900 22 *
zgu@3900 23 */
zgu@3900 24 #include "precompiled.hpp"
zgu@3900 25 #include "services/nmtDCmd.hpp"
zgu@3900 26 #include "services/memReporter.hpp"
zgu@3900 27 #include "services/memTracker.hpp"
zgu@3900 28 #include "utilities/globalDefinitions.hpp"
zgu@3900 29
zgu@3900 30 NMTDCmd::NMTDCmd(outputStream* output,
zgu@3900 31 bool heap): DCmdWithParser(output, heap),
zgu@3900 32 _summary("summary", "request runtime to report current memory summary, " \
zgu@3900 33 "which includes total reserved and committed memory, along " \
zgu@3900 34 "with memory usage summary by each subsytem.",
zgu@3900 35 "BOOLEAN", false, "false"),
zgu@3900 36 _detail("detail", "request runtime to report memory allocation >= "
zgu@3900 37 "1K by each callsite.",
zgu@3900 38 "BOOLEAN", false, "false"),
zgu@3900 39 _baseline("baseline", "request runtime to baseline current memory usage, " \
zgu@3900 40 "so it can be compared against in later time.",
zgu@3900 41 "BOOLEAN", false, "false"),
zgu@3900 42 _summary_diff("summary.diff", "request runtime to report memory summary " \
zgu@3900 43 "comparison against previous baseline.",
zgu@3900 44 "BOOLEAN", false, "false"),
zgu@3900 45 _detail_diff("detail.diff", "request runtime to report memory detail " \
zgu@3900 46 "comparison against previous baseline, which shows the memory " \
zgu@3900 47 "allocation activities at different callsites.",
zgu@3900 48 "BOOLEAN", false, "false"),
zgu@3900 49 _shutdown("shutdown", "request runtime to shutdown itself and free the " \
zgu@3900 50 "memory used by runtime.",
zgu@3900 51 "BOOLEAN", false, "false"),
zgu@3900 52 #ifndef PRODUCT
zgu@3900 53 _debug("debug", "print tracker statistics. Debug only, not thread safe", \
zgu@3900 54 "BOOLEAN", false, "false"),
zgu@3900 55 #endif
zgu@3900 56 _scale("scale", "Memory usage in which scale, KB, MB or GB",
zgu@3900 57 "STRING", false, "KB") {
zgu@3900 58 _dcmdparser.add_dcmd_option(&_summary);
zgu@3900 59 _dcmdparser.add_dcmd_option(&_detail);
zgu@3900 60 _dcmdparser.add_dcmd_option(&_baseline);
zgu@3900 61 _dcmdparser.add_dcmd_option(&_summary_diff);
zgu@3900 62 _dcmdparser.add_dcmd_option(&_detail_diff);
zgu@3900 63 _dcmdparser.add_dcmd_option(&_shutdown);
zgu@3900 64 #ifndef PRODUCT
zgu@3900 65 _dcmdparser.add_dcmd_option(&_debug);
zgu@3900 66 #endif
zgu@3900 67 _dcmdparser.add_dcmd_option(&_scale);
zgu@3900 68 }
zgu@3900 69
zgu@3900 70 void NMTDCmd::execute(TRAPS) {
zgu@3900 71 const char* scale_value = _scale.value();
zgu@3900 72 size_t scale_unit;
zgu@3900 73 if (strcmp(scale_value, "KB") == 0 || strcmp(scale_value, "kb") == 0) {
zgu@3900 74 scale_unit = K;
zgu@3900 75 } else if (strcmp(scale_value, "MB") == 0 ||
zgu@3900 76 strcmp(scale_value, "mb") == 0) {
zgu@3900 77 scale_unit = M;
zgu@3900 78 } else if (strcmp(scale_value, "GB") == 0 ||
zgu@3900 79 strcmp(scale_value, "gb") == 0) {
zgu@3900 80 scale_unit = G;
zgu@3900 81 } else {
zgu@3900 82 output()->print_cr("Incorrect scale value: %s", scale_value);
zgu@3900 83 return;
zgu@3900 84 }
zgu@3900 85
zgu@3900 86 int nopt = 0;
zgu@3900 87 if(_summary.is_set()) { ++nopt; }
zgu@3900 88 if(_detail.is_set()) { ++nopt; }
zgu@3900 89 if(_baseline.is_set()) { ++nopt; }
zgu@3900 90 if(_summary_diff.is_set()) { ++nopt; }
zgu@3900 91 if(_detail_diff.is_set()) { ++nopt; }
zgu@3900 92 if(_shutdown.is_set()) { ++nopt; }
zgu@3900 93 #ifndef PRODUCT
zgu@3900 94 if(_debug.is_set()) { ++nopt; }
zgu@3900 95 #endif
zgu@3900 96
zgu@3900 97 if(nopt > 1) {
zgu@3900 98 output()->print_cr("At most one of the following option can be specified: " \
zgu@3900 99 "summary, detail, baseline, summary.diff, detail.diff, shutdown"
zgu@3900 100 #ifndef PRODUCT
zgu@3900 101 " ,debug"
zgu@3900 102 #endif
zgu@3900 103 );
zgu@3900 104 return;
zgu@3900 105 }
zgu@3900 106
zgu@3900 107 if(nopt == 0) {
zgu@3900 108 _summary.set_value(true);
zgu@3900 109 }
zgu@3900 110
zgu@3900 111 #ifndef PRODUCT
zgu@3900 112 if (_debug.value()) {
zgu@3900 113 output()->print_cr("debug command is NOT thread-safe, may cause crash");
zgu@3900 114 MemTracker::print_tracker_stats(output());
zgu@3900 115 return;
zgu@3900 116 }
zgu@3900 117 #endif
zgu@3900 118
zgu@3900 119 // native memory tracking has to be on
zgu@3900 120 if (!MemTracker::is_on() || MemTracker::shutdown_in_progress()) {
zgu@3900 121 // if it is not on, what's the reason?
zgu@3900 122 output()->print_cr(MemTracker::reason());
zgu@3900 123 return;
zgu@3900 124 }
zgu@3900 125
zgu@3900 126 if (_summary.value()) {
zgu@3900 127 BaselineTTYOutputer outputer(output());
zgu@3900 128 MemTracker::print_memory_usage(outputer, scale_unit, true);
zgu@3900 129 } else if (_detail.value()) {
zgu@3900 130 BaselineTTYOutputer outputer(output());
zgu@3900 131 MemTracker::print_memory_usage(outputer, scale_unit, false);
zgu@3900 132 } else if (_baseline.value()) {
zgu@3900 133 if (MemTracker::baseline()) {
zgu@3900 134 output()->print_cr("Successfully baselined.");
zgu@3900 135 } else {
zgu@3900 136 output()->print_cr("Baseline failed.");
zgu@3900 137 }
zgu@3900 138 } else if (_summary_diff.value()) {
zgu@3900 139 if (MemTracker::has_baseline()) {
zgu@3900 140 BaselineTTYOutputer outputer(output());
zgu@3900 141 MemTracker::compare_memory_usage(outputer, scale_unit, true);
zgu@3900 142 } else {
zgu@3900 143 output()->print_cr("No baseline to compare, run 'baseline' command first");
zgu@3900 144 }
zgu@3900 145 } else if (_detail_diff.value()) {
zgu@3900 146 if (MemTracker::has_baseline()) {
zgu@3900 147 BaselineTTYOutputer outputer(output());
zgu@3900 148 MemTracker::compare_memory_usage(outputer, scale_unit, false);
zgu@3900 149 } else {
zgu@3900 150 output()->print_cr("No baseline to compare to, run 'baseline' command first");
zgu@3900 151 }
zgu@3900 152 } else if (_shutdown.value()) {
zgu@3900 153 MemTracker::shutdown(MemTracker::NMT_shutdown_user);
zgu@3900 154 output()->print_cr("Shutdown is in progress, it will take a few moments to " \
zgu@3900 155 "completely shutdown");
zgu@3900 156 } else {
zgu@3900 157 ShouldNotReachHere();
zgu@3900 158 output()->print_cr("Unknown command");
zgu@3900 159 }
zgu@3900 160 }
zgu@3900 161
zgu@3900 162 int NMTDCmd::num_arguments() {
zgu@3900 163 ResourceMark rm;
zgu@3900 164 NMTDCmd* dcmd = new NMTDCmd(NULL, false);
zgu@3900 165 if (dcmd != NULL) {
zgu@3900 166 DCmdMark mark(dcmd);
zgu@3900 167 return dcmd->_dcmdparser.num_arguments();
zgu@3900 168 } else {
zgu@3900 169 return 0;
zgu@3900 170 }
zgu@3900 171 }
zgu@3900 172

mercurial