jdk

Форк
0
/
nmtDCmd.cpp 
187 строк · 6.7 Кб
1
/*
2
 * Copyright (c) 2012, 2024, Oracle and/or its affiliates. All rights reserved.
3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
 *
5
 * This code is free software; you can redistribute it and/or modify it
6
 * under the terms of the GNU General Public License version 2 only, as
7
 * published by the Free Software Foundation.
8
 *
9
 * This code is distributed in the hope that it will be useful, but WITHOUT
10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12
 * version 2 for more details (a copy is included in the LICENSE file that
13
 * accompanied this code).
14
 *
15
 * You should have received a copy of the GNU General Public License version
16
 * 2 along with this work; if not, write to the Free Software Foundation,
17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
 *
19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
 * or visit www.oracle.com if you need additional information or have any
21
 * questions.
22
 *
23
 */
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"
33

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);
64
}
65

66

67
size_t NMTDCmd::get_scale(const char* scale) const {
68
  if (scale == nullptr) return 0;
69
  return NMTUtil::scale_from_name(scale);
70
}
71

72
void NMTDCmd::execute(DCmdSource source, TRAPS) {
73
  // Check NMT state
74
  //  native memory tracking has to be on
75
  if (MemTracker::tracking_level() == NMT_off) {
76
    output()->print_cr("Native memory tracking is not enabled");
77
    return;
78
  }
79

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);
84
    return;
85
  }
86

87
  int nopt = 0;
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; }
94

95
  if (nopt > 1) {
96
      output()->print_cr("At most one of the following option can be specified: " \
97
        "summary, detail, metadata, baseline, summary.diff, detail.diff");
98
      return;
99
  } else if (nopt == 0) {
100
    if (_summary.is_set()) {
101
      output()->print_cr("No command to execute");
102
      return;
103
    } else {
104
      _summary.set_value(true);
105
    }
106
  }
107

108
  // Serialize NMT query
109
  MutexLocker locker(THREAD, MemTracker::query_lock());
110

111
  if (_summary.value()) {
112
    report(true, scale_unit);
113
  } else if (_detail.value()) {
114
    if (!check_detail_tracking_level(output())) {
115
      return;
116
    }
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);
126
    } else {
127
      output()->print_cr("No baseline for comparison");
128
    }
129
  } else if (_detail_diff.value()) {
130
    if (!check_detail_tracking_level(output())) {
131
      return;
132
    }
133
    MemBaseline& baseline = MemTracker::get_baseline();
134
    if (baseline.baseline_type() == MemBaseline::Detail_baselined) {
135
      report_diff(false, scale_unit);
136
    } else {
137
      output()->print_cr("No detail baseline for comparison");
138
    }
139
  } else if (_statistics.value()) {
140
    if (MemTracker::enabled()) {
141
      MemTracker::tuning_statistics(output());
142
    } else {
143
      output()->print_cr("Native memory tracking is not enabled");
144
    }
145
  } else {
146
    ShouldNotReachHere();
147
    output()->print_cr("Unknown command");
148
  }
149
}
150

151
void NMTDCmd::report(bool summaryOnly, size_t scale_unit) {
152
  MemBaseline baseline;
153
  baseline.baseline(summaryOnly);
154
  if (summaryOnly) {
155
    MemSummaryReporter rpt(baseline, output(), scale_unit);
156
    rpt.report();
157
  } else {
158
    MemDetailReporter rpt(baseline, output(), scale_unit);
159
    rpt.report();
160
  }
161
}
162

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");
169

170
  MemBaseline baseline;
171
  baseline.baseline(summaryOnly);
172
  if (summaryOnly) {
173
    MemSummaryDiffReporter rpt(early_baseline, baseline, output(), scale_unit);
174
    rpt.report_diff();
175
  } else {
176
    MemDetailDiffReporter rpt(early_baseline, baseline, output(), scale_unit);
177
    rpt.report_diff();
178
  }
179
}
180

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");
184
    return false;
185
  }
186
  return true;
187
}
188

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.