zgu@3900: /* fparain@5047: * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. zgu@3900: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. zgu@3900: * zgu@3900: * This code is free software; you can redistribute it and/or modify it zgu@3900: * under the terms of the GNU General Public License version 2 only, as zgu@3900: * published by the Free Software Foundation. zgu@3900: * zgu@3900: * This code is distributed in the hope that it will be useful, but WITHOUT zgu@3900: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or zgu@3900: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License zgu@3900: * version 2 for more details (a copy is included in the LICENSE file that zgu@3900: * accompanied this code). zgu@3900: * zgu@3900: * You should have received a copy of the GNU General Public License version zgu@3900: * 2 along with this work; if not, write to the Free Software Foundation, zgu@3900: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. zgu@3900: * zgu@3900: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA zgu@3900: * or visit www.oracle.com if you need additional information or have any zgu@3900: * questions. zgu@3900: * zgu@3900: */ zgu@3900: #include "precompiled.hpp" zgu@3900: #include "services/nmtDCmd.hpp" zgu@3900: #include "services/memReporter.hpp" zgu@3900: #include "services/memTracker.hpp" zgu@3900: #include "utilities/globalDefinitions.hpp" zgu@3900: zgu@3900: NMTDCmd::NMTDCmd(outputStream* output, zgu@3900: bool heap): DCmdWithParser(output, heap), zgu@3900: _summary("summary", "request runtime to report current memory summary, " \ zgu@3900: "which includes total reserved and committed memory, along " \ zgu@3900: "with memory usage summary by each subsytem.", zgu@3900: "BOOLEAN", false, "false"), zgu@3900: _detail("detail", "request runtime to report memory allocation >= " zgu@3900: "1K by each callsite.", zgu@3900: "BOOLEAN", false, "false"), zgu@3900: _baseline("baseline", "request runtime to baseline current memory usage, " \ zgu@3900: "so it can be compared against in later time.", zgu@3900: "BOOLEAN", false, "false"), zgu@3900: _summary_diff("summary.diff", "request runtime to report memory summary " \ zgu@3900: "comparison against previous baseline.", zgu@3900: "BOOLEAN", false, "false"), zgu@3900: _detail_diff("detail.diff", "request runtime to report memory detail " \ zgu@3900: "comparison against previous baseline, which shows the memory " \ zgu@3900: "allocation activities at different callsites.", zgu@3900: "BOOLEAN", false, "false"), zgu@3900: _shutdown("shutdown", "request runtime to shutdown itself and free the " \ zgu@3900: "memory used by runtime.", zgu@3900: "BOOLEAN", false, "false"), zgu@4810: _auto_shutdown("autoShutdown", "automatically shutdown itself under " \ zgu@4810: "stress situation", zgu@4810: "BOOLEAN", true, "true"), zgu@3900: #ifndef PRODUCT zgu@3900: _debug("debug", "print tracker statistics. Debug only, not thread safe", \ zgu@3900: "BOOLEAN", false, "false"), zgu@3900: #endif zgu@3900: _scale("scale", "Memory usage in which scale, KB, MB or GB", zgu@3900: "STRING", false, "KB") { zgu@3900: _dcmdparser.add_dcmd_option(&_summary); zgu@3900: _dcmdparser.add_dcmd_option(&_detail); zgu@3900: _dcmdparser.add_dcmd_option(&_baseline); zgu@3900: _dcmdparser.add_dcmd_option(&_summary_diff); zgu@3900: _dcmdparser.add_dcmd_option(&_detail_diff); zgu@3900: _dcmdparser.add_dcmd_option(&_shutdown); zgu@4810: _dcmdparser.add_dcmd_option(&_auto_shutdown); zgu@3900: #ifndef PRODUCT zgu@3900: _dcmdparser.add_dcmd_option(&_debug); zgu@3900: #endif zgu@3900: _dcmdparser.add_dcmd_option(&_scale); zgu@3900: } zgu@3900: fparain@5047: void NMTDCmd::execute(DCmdSource source, TRAPS) { zgu@3900: const char* scale_value = _scale.value(); zgu@3900: size_t scale_unit; zgu@3900: if (strcmp(scale_value, "KB") == 0 || strcmp(scale_value, "kb") == 0) { zgu@3900: scale_unit = K; zgu@3900: } else if (strcmp(scale_value, "MB") == 0 || zgu@3900: strcmp(scale_value, "mb") == 0) { zgu@3900: scale_unit = M; zgu@3900: } else if (strcmp(scale_value, "GB") == 0 || zgu@3900: strcmp(scale_value, "gb") == 0) { zgu@3900: scale_unit = G; zgu@3900: } else { zgu@3900: output()->print_cr("Incorrect scale value: %s", scale_value); zgu@3900: return; zgu@3900: } zgu@3900: zgu@3900: int nopt = 0; zgu@4810: if (_summary.is_set() && _summary.value()) { ++nopt; } zgu@4810: if (_detail.is_set() && _detail.value()) { ++nopt; } zgu@4810: if (_baseline.is_set() && _baseline.value()) { ++nopt; } zgu@4810: if (_summary_diff.is_set() && _summary_diff.value()) { ++nopt; } zgu@4810: if (_detail_diff.is_set() && _detail_diff.value()) { ++nopt; } zgu@4810: if (_shutdown.is_set() && _shutdown.value()) { ++nopt; } zgu@4810: if (_auto_shutdown.is_set()) { ++nopt; } zgu@4810: zgu@3900: #ifndef PRODUCT zgu@4810: if (_debug.is_set() && _debug.value()) { ++nopt; } zgu@3900: #endif zgu@3900: zgu@4810: if (nopt > 1) { zgu@3900: output()->print_cr("At most one of the following option can be specified: " \ zgu@3900: "summary, detail, baseline, summary.diff, detail.diff, shutdown" zgu@3900: #ifndef PRODUCT zgu@4348: ", debug" zgu@3900: #endif zgu@3900: ); zgu@3900: return; zgu@4348: } else if (nopt == 0) { zgu@4348: if (_summary.is_set()) { zgu@4348: output()->print_cr("No command to execute"); zgu@4348: return; zgu@4348: } else { zgu@3900: _summary.set_value(true); zgu@4348: } zgu@3900: } zgu@3900: zgu@3900: #ifndef PRODUCT zgu@3900: if (_debug.value()) { zgu@3900: output()->print_cr("debug command is NOT thread-safe, may cause crash"); zgu@3900: MemTracker::print_tracker_stats(output()); zgu@3900: return; zgu@3900: } zgu@3900: #endif zgu@3900: zgu@3900: // native memory tracking has to be on zgu@3900: if (!MemTracker::is_on() || MemTracker::shutdown_in_progress()) { zgu@3900: // if it is not on, what's the reason? zgu@3900: output()->print_cr(MemTracker::reason()); zgu@3900: return; zgu@3900: } zgu@3900: zgu@3900: if (_summary.value()) { zgu@3900: BaselineTTYOutputer outputer(output()); zgu@3900: MemTracker::print_memory_usage(outputer, scale_unit, true); zgu@3900: } else if (_detail.value()) { zgu@3900: BaselineTTYOutputer outputer(output()); zgu@3900: MemTracker::print_memory_usage(outputer, scale_unit, false); zgu@3900: } else if (_baseline.value()) { zgu@3900: if (MemTracker::baseline()) { zgu@3900: output()->print_cr("Successfully baselined."); zgu@3900: } else { zgu@3900: output()->print_cr("Baseline failed."); zgu@3900: } zgu@3900: } else if (_summary_diff.value()) { zgu@3900: if (MemTracker::has_baseline()) { zgu@3900: BaselineTTYOutputer outputer(output()); zgu@3900: MemTracker::compare_memory_usage(outputer, scale_unit, true); zgu@3900: } else { zgu@3900: output()->print_cr("No baseline to compare, run 'baseline' command first"); zgu@3900: } zgu@3900: } else if (_detail_diff.value()) { zgu@3900: if (MemTracker::has_baseline()) { zgu@3900: BaselineTTYOutputer outputer(output()); zgu@3900: MemTracker::compare_memory_usage(outputer, scale_unit, false); zgu@3900: } else { zgu@3900: output()->print_cr("No baseline to compare to, run 'baseline' command first"); zgu@3900: } zgu@3900: } else if (_shutdown.value()) { zgu@3900: MemTracker::shutdown(MemTracker::NMT_shutdown_user); zgu@3900: output()->print_cr("Shutdown is in progress, it will take a few moments to " \ zgu@3900: "completely shutdown"); zgu@4810: } else if (_auto_shutdown.is_set()) { zgu@4810: MemTracker::set_autoShutdown(_auto_shutdown.value()); zgu@3900: } else { zgu@3900: ShouldNotReachHere(); zgu@3900: output()->print_cr("Unknown command"); zgu@3900: } zgu@3900: } zgu@3900: zgu@3900: int NMTDCmd::num_arguments() { zgu@3900: ResourceMark rm; zgu@3900: NMTDCmd* dcmd = new NMTDCmd(NULL, false); zgu@3900: if (dcmd != NULL) { zgu@3900: DCmdMark mark(dcmd); zgu@3900: return dcmd->_dcmdparser.num_arguments(); zgu@3900: } else { zgu@3900: return 0; zgu@3900: } zgu@3900: } zgu@3900: