24
#include "precompiled.hpp"
25
#include "memory/resourceArea.hpp"
26
#include "nmt/memReporter.hpp"
27
#include "nmt/memTracker.hpp"
28
#include "nmt/nmtDCmd.hpp"
29
#include "runtime/mutexLocker.hpp"
30
#include "runtime/vmOperations.hpp"
31
#include "runtime/vmThread.hpp"
32
#include "utilities/globalDefinitions.hpp"
34
NMTDCmd::NMTDCmd(outputStream* output,
35
bool heap): DCmdWithParser(output, heap),
36
_summary("summary", "request runtime to report current memory summary, " \
37
"which includes total reserved and committed memory, along " \
38
"with memory usage summary by each subsystem.",
39
"BOOLEAN", false, "false"),
40
_detail("detail", "request runtime to report memory allocation >= "
41
"1K by each callsite.",
42
"BOOLEAN", false, "false"),
43
_baseline("baseline", "request runtime to baseline current memory usage, " \
44
"so it can be compared against in later time.",
45
"BOOLEAN", false, "false"),
46
_summary_diff("summary.diff", "request runtime to report memory summary " \
47
"comparison against previous baseline.",
48
"BOOLEAN", false, "false"),
49
_detail_diff("detail.diff", "request runtime to report memory detail " \
50
"comparison against previous baseline, which shows the memory " \
51
"allocation activities at different callsites.",
52
"BOOLEAN", false, "false"),
53
_statistics("statistics", "print tracker statistics for tuning purpose.", \
54
"BOOLEAN", false, "false"),
55
_scale("scale", "Memory usage in which scale, KB, MB or GB",
56
"STRING", false, "KB") {
57
_dcmdparser.add_dcmd_option(&_summary);
58
_dcmdparser.add_dcmd_option(&_detail);
59
_dcmdparser.add_dcmd_option(&_baseline);
60
_dcmdparser.add_dcmd_option(&_summary_diff);
61
_dcmdparser.add_dcmd_option(&_detail_diff);
62
_dcmdparser.add_dcmd_option(&_statistics);
63
_dcmdparser.add_dcmd_option(&_scale);
67
size_t NMTDCmd::get_scale(const char* scale) const {
68
if (scale == nullptr) return 0;
69
return NMTUtil::scale_from_name(scale);
72
void NMTDCmd::execute(DCmdSource source, TRAPS) {
75
if (MemTracker::tracking_level() == NMT_off) {
76
output()->print_cr("Native memory tracking is not enabled");
80
const char* scale_value = _scale.value() != nullptr ? _scale.value() : "(null)";
81
size_t scale_unit = get_scale(scale_value);
82
if (scale_unit == 0) {
83
output()->print_cr("Incorrect scale value: %s", scale_value);
88
if (_summary.is_set() && _summary.value()) { ++nopt; }
89
if (_detail.is_set() && _detail.value()) { ++nopt; }
90
if (_baseline.is_set() && _baseline.value()) { ++nopt; }
91
if (_summary_diff.is_set() && _summary_diff.value()) { ++nopt; }
92
if (_detail_diff.is_set() && _detail_diff.value()) { ++nopt; }
93
if (_statistics.is_set() && _statistics.value()) { ++nopt; }
96
output()->print_cr("At most one of the following option can be specified: " \
97
"summary, detail, metadata, baseline, summary.diff, detail.diff");
99
} else if (nopt == 0) {
100
if (_summary.is_set()) {
101
output()->print_cr("No command to execute");
104
_summary.set_value(true);
109
MutexLocker locker(THREAD, MemTracker::query_lock());
111
if (_summary.value()) {
112
report(true, scale_unit);
113
} else if (_detail.value()) {
114
if (!check_detail_tracking_level(output())) {
117
report(false, scale_unit);
118
} else if (_baseline.value()) {
119
MemBaseline& baseline = MemTracker::get_baseline();
120
baseline.baseline(MemTracker::tracking_level() != NMT_detail);
121
output()->print_cr("Baseline taken");
122
} else if (_summary_diff.value()) {
123
MemBaseline& baseline = MemTracker::get_baseline();
124
if (baseline.baseline_type() >= MemBaseline::Summary_baselined) {
125
report_diff(true, scale_unit);
127
output()->print_cr("No baseline for comparison");
129
} else if (_detail_diff.value()) {
130
if (!check_detail_tracking_level(output())) {
133
MemBaseline& baseline = MemTracker::get_baseline();
134
if (baseline.baseline_type() == MemBaseline::Detail_baselined) {
135
report_diff(false, scale_unit);
137
output()->print_cr("No detail baseline for comparison");
139
} else if (_statistics.value()) {
140
if (MemTracker::enabled()) {
141
MemTracker::tuning_statistics(output());
143
output()->print_cr("Native memory tracking is not enabled");
146
ShouldNotReachHere();
147
output()->print_cr("Unknown command");
151
void NMTDCmd::report(bool summaryOnly, size_t scale_unit) {
152
MemBaseline baseline;
153
baseline.baseline(summaryOnly);
155
MemSummaryReporter rpt(baseline, output(), scale_unit);
158
MemDetailReporter rpt(baseline, output(), scale_unit);
163
void NMTDCmd::report_diff(bool summaryOnly, size_t scale_unit) {
164
MemBaseline& early_baseline = MemTracker::get_baseline();
165
assert(early_baseline.baseline_type() != MemBaseline::Not_baselined,
166
"Not yet baselined");
167
assert(summaryOnly || early_baseline.baseline_type() == MemBaseline::Detail_baselined,
168
"Not a detail baseline");
170
MemBaseline baseline;
171
baseline.baseline(summaryOnly);
173
MemSummaryDiffReporter rpt(early_baseline, baseline, output(), scale_unit);
176
MemDetailDiffReporter rpt(early_baseline, baseline, output(), scale_unit);
181
bool NMTDCmd::check_detail_tracking_level(outputStream* out) {
182
if (MemTracker::tracking_level() != NMT_detail) {
183
out->print_cr("Detail tracking is not enabled");