aoqi@0: /* aoqi@0: * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: #include "precompiled.hpp" zgu@7074: zgu@7074: #include "runtime/mutexLocker.hpp" aoqi@0: #include "services/nmtDCmd.hpp" aoqi@0: #include "services/memReporter.hpp" aoqi@0: #include "services/memTracker.hpp" aoqi@0: #include "utilities/globalDefinitions.hpp" aoqi@0: aoqi@0: NMTDCmd::NMTDCmd(outputStream* output, aoqi@0: bool heap): DCmdWithParser(output, heap), aoqi@0: _summary("summary", "request runtime to report current memory summary, " \ aoqi@0: "which includes total reserved and committed memory, along " \ aoqi@0: "with memory usage summary by each subsytem.", aoqi@0: "BOOLEAN", false, "false"), aoqi@0: _detail("detail", "request runtime to report memory allocation >= " aoqi@0: "1K by each callsite.", aoqi@0: "BOOLEAN", false, "false"), aoqi@0: _baseline("baseline", "request runtime to baseline current memory usage, " \ aoqi@0: "so it can be compared against in later time.", aoqi@0: "BOOLEAN", false, "false"), aoqi@0: _summary_diff("summary.diff", "request runtime to report memory summary " \ aoqi@0: "comparison against previous baseline.", aoqi@0: "BOOLEAN", false, "false"), aoqi@0: _detail_diff("detail.diff", "request runtime to report memory detail " \ aoqi@0: "comparison against previous baseline, which shows the memory " \ aoqi@0: "allocation activities at different callsites.", aoqi@0: "BOOLEAN", false, "false"), aoqi@0: _shutdown("shutdown", "request runtime to shutdown itself and free the " \ aoqi@0: "memory used by runtime.", aoqi@0: "BOOLEAN", false, "false"), zgu@7074: _statistics("statistics", "print tracker statistics for tuning purpose.", \ aoqi@0: "BOOLEAN", false, "false"), aoqi@0: _scale("scale", "Memory usage in which scale, KB, MB or GB", aoqi@0: "STRING", false, "KB") { aoqi@0: _dcmdparser.add_dcmd_option(&_summary); aoqi@0: _dcmdparser.add_dcmd_option(&_detail); aoqi@0: _dcmdparser.add_dcmd_option(&_baseline); aoqi@0: _dcmdparser.add_dcmd_option(&_summary_diff); aoqi@0: _dcmdparser.add_dcmd_option(&_detail_diff); aoqi@0: _dcmdparser.add_dcmd_option(&_shutdown); zgu@7074: _dcmdparser.add_dcmd_option(&_statistics); aoqi@0: _dcmdparser.add_dcmd_option(&_scale); aoqi@0: } aoqi@0: zgu@7074: zgu@7074: size_t NMTDCmd::get_scale(const char* scale) const { zgu@7074: if (scale == NULL) return 0; zgu@7074: return NMTUtil::scale_from_name(scale); zgu@7074: } zgu@7074: aoqi@0: void NMTDCmd::execute(DCmdSource source, TRAPS) { zgu@7074: // Check NMT state zgu@7074: // native memory tracking has to be on zgu@7074: if (MemTracker::tracking_level() == NMT_off) { zgu@7074: output()->print_cr("Native memory tracking is not enabled"); zgu@7074: return; zgu@7074: } else if (MemTracker::tracking_level() == NMT_minimal) { zgu@7074: output()->print_cr("Native memory tracking has been shutdown"); zgu@7074: return; zgu@7074: } zgu@7074: aoqi@0: const char* scale_value = _scale.value(); zgu@7074: size_t scale_unit = get_scale(scale_value); zgu@7074: if (scale_unit == 0) { aoqi@0: output()->print_cr("Incorrect scale value: %s", scale_value); aoqi@0: return; aoqi@0: } aoqi@0: aoqi@0: int nopt = 0; aoqi@0: if (_summary.is_set() && _summary.value()) { ++nopt; } aoqi@0: if (_detail.is_set() && _detail.value()) { ++nopt; } aoqi@0: if (_baseline.is_set() && _baseline.value()) { ++nopt; } aoqi@0: if (_summary_diff.is_set() && _summary_diff.value()) { ++nopt; } aoqi@0: if (_detail_diff.is_set() && _detail_diff.value()) { ++nopt; } aoqi@0: if (_shutdown.is_set() && _shutdown.value()) { ++nopt; } zgu@7074: if (_statistics.is_set() && _statistics.value()) { ++nopt; } aoqi@0: aoqi@0: if (nopt > 1) { aoqi@0: output()->print_cr("At most one of the following option can be specified: " \ zgu@7074: "summary, detail, baseline, summary.diff, detail.diff, shutdown"); aoqi@0: return; aoqi@0: } else if (nopt == 0) { aoqi@0: if (_summary.is_set()) { aoqi@0: output()->print_cr("No command to execute"); aoqi@0: return; aoqi@0: } else { aoqi@0: _summary.set_value(true); aoqi@0: } aoqi@0: } aoqi@0: zgu@7074: // Serialize NMT query zgu@7074: MutexLocker locker(MemTracker::query_lock()); zgu@7074: zgu@7074: if (_summary.value()) { zgu@7074: report(true, scale_unit); zgu@7074: } else if (_detail.value()) { zgu@7074: if (!check_detail_tracking_level(output())) { aoqi@0: return; aoqi@0: } zgu@7074: report(false, scale_unit); zgu@7074: } else if (_baseline.value()) { zgu@7074: MemBaseline& baseline = MemTracker::get_baseline(); zgu@7074: if (!baseline.baseline(MemTracker::tracking_level() != NMT_detail)) { zgu@7074: output()->print_cr("Baseline failed"); zgu@7074: } else { zgu@7074: output()->print_cr("Baseline succeeded"); zgu@7074: } zgu@7074: } else if (_summary_diff.value()) { zgu@7074: MemBaseline& baseline = MemTracker::get_baseline(); zgu@7074: if (baseline.baseline_type() >= MemBaseline::Summary_baselined) { zgu@7074: report_diff(true, scale_unit); zgu@7074: } else { zgu@7074: output()->print_cr("No baseline for comparison"); zgu@7074: } zgu@7074: } else if (_detail_diff.value()) { zgu@7074: if (!check_detail_tracking_level(output())) { aoqi@0: return; aoqi@0: } zgu@7074: MemBaseline& baseline = MemTracker::get_baseline(); zgu@7074: if (baseline.baseline_type() == MemBaseline::Detail_baselined) { zgu@7074: report_diff(false, scale_unit); aoqi@0: } else { zgu@7074: output()->print_cr("No detail baseline for comparison"); aoqi@0: } aoqi@0: } else if (_shutdown.value()) { zgu@7074: MemTracker::shutdown(); zgu@7074: output()->print_cr("Native memory tracking has been turned off"); zgu@7074: } else if (_statistics.value()) { zgu@7074: if (check_detail_tracking_level(output())) { zgu@7074: MemTracker::tuning_statistics(output()); zgu@7074: } aoqi@0: } else { aoqi@0: ShouldNotReachHere(); aoqi@0: output()->print_cr("Unknown command"); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: int NMTDCmd::num_arguments() { aoqi@0: ResourceMark rm; aoqi@0: NMTDCmd* dcmd = new NMTDCmd(NULL, false); aoqi@0: if (dcmd != NULL) { aoqi@0: DCmdMark mark(dcmd); aoqi@0: return dcmd->_dcmdparser.num_arguments(); aoqi@0: } else { aoqi@0: return 0; aoqi@0: } aoqi@0: } aoqi@0: zgu@7074: void NMTDCmd::report(bool summaryOnly, size_t scale_unit) { zgu@7074: MemBaseline baseline; zgu@7074: if (baseline.baseline(summaryOnly)) { zgu@7074: if (summaryOnly) { zgu@7074: MemSummaryReporter rpt(baseline, output(), scale_unit); zgu@7074: rpt.report(); zgu@7074: } else { zgu@7074: MemDetailReporter rpt(baseline, output(), scale_unit); zgu@7074: rpt.report(); zgu@7074: } zgu@7074: } zgu@7074: } zgu@7074: zgu@7074: void NMTDCmd::report_diff(bool summaryOnly, size_t scale_unit) { zgu@7074: MemBaseline& early_baseline = MemTracker::get_baseline(); zgu@7074: assert(early_baseline.baseline_type() != MemBaseline::Not_baselined, zgu@7074: "Not yet baselined"); zgu@7074: assert(summaryOnly || early_baseline.baseline_type() == MemBaseline::Detail_baselined, zgu@7074: "Not a detail baseline"); zgu@7074: zgu@7074: MemBaseline baseline; zgu@7074: if (baseline.baseline(summaryOnly)) { zgu@7074: if (summaryOnly) { zgu@7074: MemSummaryDiffReporter rpt(early_baseline, baseline, output(), scale_unit); zgu@7074: rpt.report_diff(); zgu@7074: } else { zgu@7074: MemDetailDiffReporter rpt(early_baseline, baseline, output(), scale_unit); zgu@7074: rpt.report_diff(); zgu@7074: } zgu@7074: } zgu@7074: } zgu@7074: zgu@7074: bool NMTDCmd::check_detail_tracking_level(outputStream* out) { zgu@7074: if (MemTracker::tracking_level() == NMT_detail) { zgu@7074: return true; zgu@7074: } else if (MemTracker::cmdline_tracking_level() == NMT_detail) { zgu@7074: out->print_cr("Tracking level has been downgraded due to lack of resources"); zgu@7074: return false; zgu@7074: } else { zgu@7074: out->print_cr("Detail tracking is not enabled"); zgu@7074: return false; zgu@7074: } zgu@7074: }