src/share/vm/services/nmtDCmd.cpp

Thu, 12 Oct 2017 21:27:07 +0800

author
aoqi
date
Thu, 12 Oct 2017 21:27:07 +0800
changeset 7535
7ae4e26cb1e0
parent 7074
833b0f92429a
parent 6876
710a3c8b516e
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24 #include "precompiled.hpp"
zgu@7074 25
zgu@7074 26 #include "runtime/mutexLocker.hpp"
aoqi@0 27 #include "services/nmtDCmd.hpp"
aoqi@0 28 #include "services/memReporter.hpp"
aoqi@0 29 #include "services/memTracker.hpp"
aoqi@0 30 #include "utilities/globalDefinitions.hpp"
aoqi@0 31
aoqi@0 32 NMTDCmd::NMTDCmd(outputStream* output,
aoqi@0 33 bool heap): DCmdWithParser(output, heap),
aoqi@0 34 _summary("summary", "request runtime to report current memory summary, " \
aoqi@0 35 "which includes total reserved and committed memory, along " \
aoqi@0 36 "with memory usage summary by each subsytem.",
aoqi@0 37 "BOOLEAN", false, "false"),
aoqi@0 38 _detail("detail", "request runtime to report memory allocation >= "
aoqi@0 39 "1K by each callsite.",
aoqi@0 40 "BOOLEAN", false, "false"),
aoqi@0 41 _baseline("baseline", "request runtime to baseline current memory usage, " \
aoqi@0 42 "so it can be compared against in later time.",
aoqi@0 43 "BOOLEAN", false, "false"),
aoqi@0 44 _summary_diff("summary.diff", "request runtime to report memory summary " \
aoqi@0 45 "comparison against previous baseline.",
aoqi@0 46 "BOOLEAN", false, "false"),
aoqi@0 47 _detail_diff("detail.diff", "request runtime to report memory detail " \
aoqi@0 48 "comparison against previous baseline, which shows the memory " \
aoqi@0 49 "allocation activities at different callsites.",
aoqi@0 50 "BOOLEAN", false, "false"),
aoqi@0 51 _shutdown("shutdown", "request runtime to shutdown itself and free the " \
aoqi@0 52 "memory used by runtime.",
aoqi@0 53 "BOOLEAN", false, "false"),
zgu@7074 54 _statistics("statistics", "print tracker statistics for tuning purpose.", \
aoqi@0 55 "BOOLEAN", false, "false"),
aoqi@0 56 _scale("scale", "Memory usage in which scale, KB, MB or GB",
aoqi@0 57 "STRING", false, "KB") {
aoqi@0 58 _dcmdparser.add_dcmd_option(&_summary);
aoqi@0 59 _dcmdparser.add_dcmd_option(&_detail);
aoqi@0 60 _dcmdparser.add_dcmd_option(&_baseline);
aoqi@0 61 _dcmdparser.add_dcmd_option(&_summary_diff);
aoqi@0 62 _dcmdparser.add_dcmd_option(&_detail_diff);
aoqi@0 63 _dcmdparser.add_dcmd_option(&_shutdown);
zgu@7074 64 _dcmdparser.add_dcmd_option(&_statistics);
aoqi@0 65 _dcmdparser.add_dcmd_option(&_scale);
aoqi@0 66 }
aoqi@0 67
zgu@7074 68
zgu@7074 69 size_t NMTDCmd::get_scale(const char* scale) const {
zgu@7074 70 if (scale == NULL) return 0;
zgu@7074 71 return NMTUtil::scale_from_name(scale);
zgu@7074 72 }
zgu@7074 73
aoqi@0 74 void NMTDCmd::execute(DCmdSource source, TRAPS) {
zgu@7074 75 // Check NMT state
zgu@7074 76 // native memory tracking has to be on
zgu@7074 77 if (MemTracker::tracking_level() == NMT_off) {
zgu@7074 78 output()->print_cr("Native memory tracking is not enabled");
zgu@7074 79 return;
zgu@7074 80 } else if (MemTracker::tracking_level() == NMT_minimal) {
zgu@7074 81 output()->print_cr("Native memory tracking has been shutdown");
zgu@7074 82 return;
zgu@7074 83 }
zgu@7074 84
aoqi@0 85 const char* scale_value = _scale.value();
zgu@7074 86 size_t scale_unit = get_scale(scale_value);
zgu@7074 87 if (scale_unit == 0) {
aoqi@0 88 output()->print_cr("Incorrect scale value: %s", scale_value);
aoqi@0 89 return;
aoqi@0 90 }
aoqi@0 91
aoqi@0 92 int nopt = 0;
aoqi@0 93 if (_summary.is_set() && _summary.value()) { ++nopt; }
aoqi@0 94 if (_detail.is_set() && _detail.value()) { ++nopt; }
aoqi@0 95 if (_baseline.is_set() && _baseline.value()) { ++nopt; }
aoqi@0 96 if (_summary_diff.is_set() && _summary_diff.value()) { ++nopt; }
aoqi@0 97 if (_detail_diff.is_set() && _detail_diff.value()) { ++nopt; }
aoqi@0 98 if (_shutdown.is_set() && _shutdown.value()) { ++nopt; }
zgu@7074 99 if (_statistics.is_set() && _statistics.value()) { ++nopt; }
aoqi@0 100
aoqi@0 101 if (nopt > 1) {
aoqi@0 102 output()->print_cr("At most one of the following option can be specified: " \
zgu@7074 103 "summary, detail, baseline, summary.diff, detail.diff, shutdown");
aoqi@0 104 return;
aoqi@0 105 } else if (nopt == 0) {
aoqi@0 106 if (_summary.is_set()) {
aoqi@0 107 output()->print_cr("No command to execute");
aoqi@0 108 return;
aoqi@0 109 } else {
aoqi@0 110 _summary.set_value(true);
aoqi@0 111 }
aoqi@0 112 }
aoqi@0 113
zgu@7074 114 // Serialize NMT query
zgu@7074 115 MutexLocker locker(MemTracker::query_lock());
zgu@7074 116
zgu@7074 117 if (_summary.value()) {
zgu@7074 118 report(true, scale_unit);
zgu@7074 119 } else if (_detail.value()) {
zgu@7074 120 if (!check_detail_tracking_level(output())) {
aoqi@0 121 return;
aoqi@0 122 }
zgu@7074 123 report(false, scale_unit);
zgu@7074 124 } else if (_baseline.value()) {
zgu@7074 125 MemBaseline& baseline = MemTracker::get_baseline();
zgu@7074 126 if (!baseline.baseline(MemTracker::tracking_level() != NMT_detail)) {
zgu@7074 127 output()->print_cr("Baseline failed");
zgu@7074 128 } else {
zgu@7074 129 output()->print_cr("Baseline succeeded");
zgu@7074 130 }
zgu@7074 131 } else if (_summary_diff.value()) {
zgu@7074 132 MemBaseline& baseline = MemTracker::get_baseline();
zgu@7074 133 if (baseline.baseline_type() >= MemBaseline::Summary_baselined) {
zgu@7074 134 report_diff(true, scale_unit);
zgu@7074 135 } else {
zgu@7074 136 output()->print_cr("No baseline for comparison");
zgu@7074 137 }
zgu@7074 138 } else if (_detail_diff.value()) {
zgu@7074 139 if (!check_detail_tracking_level(output())) {
aoqi@0 140 return;
aoqi@0 141 }
zgu@7074 142 MemBaseline& baseline = MemTracker::get_baseline();
zgu@7074 143 if (baseline.baseline_type() == MemBaseline::Detail_baselined) {
zgu@7074 144 report_diff(false, scale_unit);
aoqi@0 145 } else {
zgu@7074 146 output()->print_cr("No detail baseline for comparison");
aoqi@0 147 }
aoqi@0 148 } else if (_shutdown.value()) {
zgu@7074 149 MemTracker::shutdown();
zgu@7074 150 output()->print_cr("Native memory tracking has been turned off");
zgu@7074 151 } else if (_statistics.value()) {
zgu@7074 152 if (check_detail_tracking_level(output())) {
zgu@7074 153 MemTracker::tuning_statistics(output());
zgu@7074 154 }
aoqi@0 155 } else {
aoqi@0 156 ShouldNotReachHere();
aoqi@0 157 output()->print_cr("Unknown command");
aoqi@0 158 }
aoqi@0 159 }
aoqi@0 160
aoqi@0 161 int NMTDCmd::num_arguments() {
aoqi@0 162 ResourceMark rm;
aoqi@0 163 NMTDCmd* dcmd = new NMTDCmd(NULL, false);
aoqi@0 164 if (dcmd != NULL) {
aoqi@0 165 DCmdMark mark(dcmd);
aoqi@0 166 return dcmd->_dcmdparser.num_arguments();
aoqi@0 167 } else {
aoqi@0 168 return 0;
aoqi@0 169 }
aoqi@0 170 }
aoqi@0 171
zgu@7074 172 void NMTDCmd::report(bool summaryOnly, size_t scale_unit) {
zgu@7074 173 MemBaseline baseline;
zgu@7074 174 if (baseline.baseline(summaryOnly)) {
zgu@7074 175 if (summaryOnly) {
zgu@7074 176 MemSummaryReporter rpt(baseline, output(), scale_unit);
zgu@7074 177 rpt.report();
zgu@7074 178 } else {
zgu@7074 179 MemDetailReporter rpt(baseline, output(), scale_unit);
zgu@7074 180 rpt.report();
zgu@7074 181 }
zgu@7074 182 }
zgu@7074 183 }
zgu@7074 184
zgu@7074 185 void NMTDCmd::report_diff(bool summaryOnly, size_t scale_unit) {
zgu@7074 186 MemBaseline& early_baseline = MemTracker::get_baseline();
zgu@7074 187 assert(early_baseline.baseline_type() != MemBaseline::Not_baselined,
zgu@7074 188 "Not yet baselined");
zgu@7074 189 assert(summaryOnly || early_baseline.baseline_type() == MemBaseline::Detail_baselined,
zgu@7074 190 "Not a detail baseline");
zgu@7074 191
zgu@7074 192 MemBaseline baseline;
zgu@7074 193 if (baseline.baseline(summaryOnly)) {
zgu@7074 194 if (summaryOnly) {
zgu@7074 195 MemSummaryDiffReporter rpt(early_baseline, baseline, output(), scale_unit);
zgu@7074 196 rpt.report_diff();
zgu@7074 197 } else {
zgu@7074 198 MemDetailDiffReporter rpt(early_baseline, baseline, output(), scale_unit);
zgu@7074 199 rpt.report_diff();
zgu@7074 200 }
zgu@7074 201 }
zgu@7074 202 }
zgu@7074 203
zgu@7074 204 bool NMTDCmd::check_detail_tracking_level(outputStream* out) {
zgu@7074 205 if (MemTracker::tracking_level() == NMT_detail) {
zgu@7074 206 return true;
zgu@7074 207 } else if (MemTracker::cmdline_tracking_level() == NMT_detail) {
zgu@7074 208 out->print_cr("Tracking level has been downgraded due to lack of resources");
zgu@7074 209 return false;
zgu@7074 210 } else {
zgu@7074 211 out->print_cr("Detail tracking is not enabled");
zgu@7074 212 return false;
zgu@7074 213 }
zgu@7074 214 }

mercurial