author | stefank |
Mon, 25 Nov 2019 12:33:15 +0100 | |
changeset 59252 | 623722a6aeb9 |
parent 58927 | d8aced36e872 |
child 59290 | 97d13893ec3c |
permissions | -rw-r--r-- |
13195 | 1 |
/* |
54292
c31faeacf00a
8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
52877
diff
changeset
|
2 |
* Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved. |
13195 | 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" |
|
47765
b7c7428eaab9
8189610: Reconcile jvm.h and all jvm_md.h between java.base and hotspot
coleenp
parents:
47216
diff
changeset
|
25 |
#include "jvm.h" |
13195 | 26 |
|
50429
83aec1d357d4
8204301: Make OrderAccess functions available to hpp rather than inline.hpp files
coleenp
parents:
49980
diff
changeset
|
27 |
#include "runtime/orderAccess.hpp" |
47914
564882d918d4
8190357: NMT: Include metadata information in NMT final report when PrintNMTStatistics is on
zgu
parents:
47765
diff
changeset
|
28 |
#include "runtime/vmThread.hpp" |
52877
9e041366c764
8214850: Rename vm_operations.?pp files to vmOperations.?pp files
tschatzl
parents:
52581
diff
changeset
|
29 |
#include "runtime/vmOperations.hpp" |
25946 | 30 |
#include "services/memBaseline.hpp" |
13195 | 31 |
#include "services/memReporter.hpp" |
25946 | 32 |
#include "services/mallocTracker.inline.hpp" |
13195 | 33 |
#include "services/memTracker.hpp" |
54292
c31faeacf00a
8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
52877
diff
changeset
|
34 |
#include "services/threadStackTracker.hpp" |
51264
cf34c71ca27c
8208499: NMT: Missing memory tag for Safepoint polling page
zgu
parents:
50965
diff
changeset
|
35 |
#include "utilities/debug.hpp" |
17838
89e464689cb1
8011064: Some tests have failed with SIGSEGV on arm-hflt on build b82
jprovino
parents:
17074
diff
changeset
|
36 |
#include "utilities/defaultStream.hpp" |
47914
564882d918d4
8190357: NMT: Include metadata information in NMT final report when PrintNMTStatistics is on
zgu
parents:
47765
diff
changeset
|
37 |
#include "utilities/vmError.hpp" |
13195 | 38 |
|
51306 | 39 |
#ifdef _WINDOWS |
40 |
#include <windows.h> |
|
41 |
#endif |
|
42 |
||
25946 | 43 |
#ifdef SOLARIS |
44 |
volatile bool NMT_stack_walkable = false; |
|
45 |
#else |
|
46 |
volatile bool NMT_stack_walkable = true; |
|
47 |
#endif |
|
13195 | 48 |
|
25946 | 49 |
volatile NMT_TrackingLevel MemTracker::_tracking_level = NMT_unknown; |
50 |
NMT_TrackingLevel MemTracker::_cmdline_tracking_level = NMT_unknown; |
|
51 |
||
52 |
MemBaseline MemTracker::_baseline; |
|
53 |
bool MemTracker::_is_nmt_env_valid = true; |
|
13195 | 54 |
|
51306 | 55 |
static const size_t buffer_size = 64; |
13195 | 56 |
|
25946 | 57 |
NMT_TrackingLevel MemTracker::init_tracking_level() { |
51264
cf34c71ca27c
8208499: NMT: Missing memory tag for Safepoint polling page
zgu
parents:
50965
diff
changeset
|
58 |
// Memory type is encoded into tracking header as a byte field, |
cf34c71ca27c
8208499: NMT: Missing memory tag for Safepoint polling page
zgu
parents:
50965
diff
changeset
|
59 |
// make sure that we don't overflow it. |
cf34c71ca27c
8208499: NMT: Missing memory tag for Safepoint polling page
zgu
parents:
50965
diff
changeset
|
60 |
STATIC_ASSERT(mt_number_of_types <= max_jubyte); |
cf34c71ca27c
8208499: NMT: Missing memory tag for Safepoint polling page
zgu
parents:
50965
diff
changeset
|
61 |
|
51306 | 62 |
char nmt_env_variable[buffer_size]; |
63 |
jio_snprintf(nmt_env_variable, sizeof(nmt_env_variable), "NMT_LEVEL_%d", os::current_process_id()); |
|
64 |
const char* nmt_env_value; |
|
65 |
#ifdef _WINDOWS |
|
66 |
// Read the NMT environment variable from the PEB instead of the CRT |
|
67 |
char value[buffer_size]; |
|
68 |
nmt_env_value = GetEnvironmentVariable(nmt_env_variable, value, (DWORD)sizeof(value)) != 0 ? value : NULL; |
|
69 |
#else |
|
70 |
nmt_env_value = ::getenv(nmt_env_variable); |
|
71 |
#endif |
|
25946 | 72 |
NMT_TrackingLevel level = NMT_off; |
51306 | 73 |
if (nmt_env_value != NULL) { |
74 |
if (strcmp(nmt_env_value, "summary") == 0) { |
|
25946 | 75 |
level = NMT_summary; |
51306 | 76 |
} else if (strcmp(nmt_env_value, "detail") == 0) { |
25946 | 77 |
level = NMT_detail; |
51306 | 78 |
} else if (strcmp(nmt_env_value, "off") != 0) { |
79 |
// The value of the environment variable is invalid |
|
25946 | 80 |
_is_nmt_env_valid = false; |
13195 | 81 |
} |
25946 | 82 |
// Remove the environment variable to avoid leaking to child processes |
51306 | 83 |
os::unsetenv(nmt_env_variable); |
25946 | 84 |
} |
13301
f6dd749390dd
7181986: NMT ON: Assertion failure when running jdi ExpiredRequestDeletionTest
zgu
parents:
13195
diff
changeset
|
85 |
|
25946 | 86 |
if (!MallocTracker::initialize(level) || |
87 |
!VirtualMemoryTracker::initialize(level)) { |
|
88 |
level = NMT_off; |
|
13195 | 89 |
} |
25946 | 90 |
return level; |
13195 | 91 |
} |
92 |
||
25946 | 93 |
void MemTracker::init() { |
26136
254c226e4402
8054368: nsk/jdi/VirtualMachine/exit/exit002 crash with detail tracking on (NMT2)
zgu
parents:
25946
diff
changeset
|
94 |
NMT_TrackingLevel level = tracking_level(); |
254c226e4402
8054368: nsk/jdi/VirtualMachine/exit/exit002 crash with detail tracking on (NMT2)
zgu
parents:
25946
diff
changeset
|
95 |
if (level >= NMT_summary) { |
54292
c31faeacf00a
8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
52877
diff
changeset
|
96 |
if (!VirtualMemoryTracker::late_initialize(level) || |
c31faeacf00a
8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
52877
diff
changeset
|
97 |
!ThreadStackTracker::late_initialize(level)) { |
26136
254c226e4402
8054368: nsk/jdi/VirtualMachine/exit/exit002 crash with detail tracking on (NMT2)
zgu
parents:
25946
diff
changeset
|
98 |
shutdown(); |
254c226e4402
8054368: nsk/jdi/VirtualMachine/exit/exit002 crash with detail tracking on (NMT2)
zgu
parents:
25946
diff
changeset
|
99 |
return; |
254c226e4402
8054368: nsk/jdi/VirtualMachine/exit/exit002 crash with detail tracking on (NMT2)
zgu
parents:
25946
diff
changeset
|
100 |
} |
13195 | 101 |
} |
102 |
} |
|
103 |
||
25946 | 104 |
bool MemTracker::check_launcher_nmt_support(const char* value) { |
105 |
if (strcmp(value, "=detail") == 0) { |
|
106 |
if (MemTracker::tracking_level() != NMT_detail) { |
|
107 |
return false; |
|
108 |
} |
|
109 |
} else if (strcmp(value, "=summary") == 0) { |
|
110 |
if (MemTracker::tracking_level() != NMT_summary) { |
|
111 |
return false; |
|
112 |
} |
|
113 |
} else if (strcmp(value, "=off") == 0) { |
|
114 |
if (MemTracker::tracking_level() != NMT_off) { |
|
115 |
return false; |
|
116 |
} |
|
117 |
} else { |
|
118 |
_is_nmt_env_valid = false; |
|
119 |
} |
|
120 |
||
121 |
return true; |
|
122 |
} |
|
123 |
||
124 |
bool MemTracker::verify_nmt_option() { |
|
125 |
return _is_nmt_env_valid; |
|
126 |
} |
|
127 |
||
128 |
void* MemTracker::malloc_base(void* memblock) { |
|
129 |
return MallocTracker::get_base(memblock); |
|
130 |
} |
|
131 |
||
132 |
void Tracker::record(address addr, size_t size) { |
|
133 |
if (MemTracker::tracking_level() < NMT_summary) return; |
|
134 |
switch(_type) { |
|
135 |
case uncommit: |
|
136 |
VirtualMemoryTracker::remove_uncommitted_region(addr, size); |
|
137 |
break; |
|
138 |
case release: |
|
139 |
VirtualMemoryTracker::remove_released_region(addr, size); |
|
140 |
break; |
|
141 |
default: |
|
142 |
ShouldNotReachHere(); |
|
13195 | 143 |
} |
144 |
} |
|
145 |
||
25946 | 146 |
|
27162
0a4a7276949b
8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents:
26145
diff
changeset
|
147 |
// Shutdown can only be issued via JCmd, and NMT JCmd is serialized by lock |
25946 | 148 |
void MemTracker::shutdown() { |
27162
0a4a7276949b
8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents:
26145
diff
changeset
|
149 |
// We can only shutdown NMT to minimal tracking level if it is ever on. |
58927
d8aced36e872
8233494: Avoid calling MallocTracker::record_malloc and record_free when NMT is off
redestad
parents:
55652
diff
changeset
|
150 |
if (tracking_level() > NMT_minimal) { |
25946 | 151 |
transition_to(NMT_minimal); |
152 |
} |
|
153 |
} |
|
154 |
||
155 |
bool MemTracker::transition_to(NMT_TrackingLevel level) { |
|
156 |
NMT_TrackingLevel current_level = tracking_level(); |
|
13195 | 157 |
|
27162
0a4a7276949b
8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents:
26145
diff
changeset
|
158 |
assert(level != NMT_off || current_level == NMT_off, "Cannot transition NMT to off"); |
0a4a7276949b
8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents:
26145
diff
changeset
|
159 |
|
25946 | 160 |
if (current_level == level) { |
161 |
return true; |
|
162 |
} else if (current_level > level) { |
|
27162
0a4a7276949b
8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents:
26145
diff
changeset
|
163 |
// Downgrade tracking level, we want to lower the tracking level first |
25946 | 164 |
_tracking_level = level; |
165 |
// Make _tracking_level visible immediately. |
|
166 |
OrderAccess::fence(); |
|
167 |
VirtualMemoryTracker::transition(current_level, level); |
|
168 |
MallocTracker::transition(current_level, level); |
|
54292
c31faeacf00a
8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
52877
diff
changeset
|
169 |
ThreadStackTracker::transition(current_level, level); |
13195 | 170 |
} else { |
27162
0a4a7276949b
8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents:
26145
diff
changeset
|
171 |
// Upgrading tracking level is not supported and has never been supported. |
0a4a7276949b
8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents:
26145
diff
changeset
|
172 |
// Allocating and deallocating malloc tracking structures is not thread safe and |
0a4a7276949b
8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents:
26145
diff
changeset
|
173 |
// leads to inconsistencies unless a lot coarser locks are added. |
13195 | 174 |
} |
25946 | 175 |
return true; |
13195 | 176 |
} |
177 |
||
55652 | 178 |
|
179 |
static volatile bool g_final_report_did_run = false; |
|
180 |
void MemTracker::final_report(outputStream* output) { |
|
181 |
// This function is called during both error reporting and normal VM exit. |
|
182 |
// However, it should only ever run once. E.g. if the VM crashes after |
|
183 |
// printing the final report during normal VM exit, it should not print |
|
184 |
// the final report again. In addition, it should be guarded from |
|
185 |
// recursive calls in case NMT reporting itself crashes. |
|
59252
623722a6aeb9
8234740: Harmonize parameter order in Atomic - cmpxchg
stefank
parents:
58927
diff
changeset
|
186 |
if (Atomic::cmpxchg(&g_final_report_did_run, false, true) == false) { |
55652 | 187 |
NMT_TrackingLevel level = tracking_level(); |
188 |
if (level >= NMT_summary) { |
|
189 |
report(level == NMT_summary, output); |
|
190 |
} |
|
191 |
} |
|
192 |
} |
|
193 |
||
27162
0a4a7276949b
8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents:
26145
diff
changeset
|
194 |
void MemTracker::report(bool summary_only, outputStream* output) { |
0a4a7276949b
8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents:
26145
diff
changeset
|
195 |
assert(output != NULL, "No output stream"); |
0a4a7276949b
8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents:
26145
diff
changeset
|
196 |
MemBaseline baseline; |
0a4a7276949b
8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents:
26145
diff
changeset
|
197 |
if (baseline.baseline(summary_only)) { |
0a4a7276949b
8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents:
26145
diff
changeset
|
198 |
if (summary_only) { |
0a4a7276949b
8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents:
26145
diff
changeset
|
199 |
MemSummaryReporter rpt(baseline, output); |
0a4a7276949b
8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents:
26145
diff
changeset
|
200 |
rpt.report(); |
0a4a7276949b
8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents:
26145
diff
changeset
|
201 |
} else { |
0a4a7276949b
8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents:
26145
diff
changeset
|
202 |
MemDetailReporter rpt(baseline, output); |
0a4a7276949b
8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents:
26145
diff
changeset
|
203 |
rpt.report(); |
49980 | 204 |
output->print("Metaspace:"); |
55652 | 205 |
// The basic metaspace report avoids any locking and should be safe to |
206 |
// be called at any time. |
|
207 |
MetaspaceUtils::print_basic_report(output, K); |
|
27162
0a4a7276949b
8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents:
26145
diff
changeset
|
208 |
} |
13195 | 209 |
} |
210 |
} |
|
211 |
||
25946 | 212 |
// This is a walker to gather malloc site hashtable statistics, |
213 |
// the result is used for tuning. |
|
214 |
class StatisticsWalker : public MallocSiteWalker { |
|
215 |
private: |
|
216 |
enum Threshold { |
|
217 |
// aggregates statistics over this threshold into one |
|
218 |
// line item. |
|
219 |
report_threshold = 20 |
|
220 |
}; |
|
13195 | 221 |
|
25946 | 222 |
private: |
223 |
// Number of allocation sites that have all memory freed |
|
224 |
int _empty_entries; |
|
225 |
// Total number of allocation sites, include empty sites |
|
226 |
int _total_entries; |
|
227 |
// Number of captured call stack distribution |
|
228 |
int _stack_depth_distribution[NMT_TrackingStackDepth]; |
|
229 |
// Hash distribution |
|
230 |
int _hash_distribution[report_threshold]; |
|
231 |
// Number of hash buckets that have entries over the threshold |
|
232 |
int _bucket_over_threshold; |
|
13195 | 233 |
|
25946 | 234 |
// The hash bucket that walker is currently walking |
235 |
int _current_hash_bucket; |
|
236 |
// The length of current hash bucket |
|
237 |
int _current_bucket_length; |
|
238 |
// Number of hash buckets that are not empty |
|
239 |
int _used_buckets; |
|
240 |
// Longest hash bucket length |
|
241 |
int _longest_bucket_length; |
|
13195 | 242 |
|
25946 | 243 |
public: |
244 |
StatisticsWalker() : _empty_entries(0), _total_entries(0) { |
|
245 |
int index = 0; |
|
246 |
for (index = 0; index < NMT_TrackingStackDepth; index ++) { |
|
247 |
_stack_depth_distribution[index] = 0; |
|
13195 | 248 |
} |
25946 | 249 |
for (index = 0; index < report_threshold; index ++) { |
250 |
_hash_distribution[index] = 0; |
|
251 |
} |
|
252 |
_bucket_over_threshold = 0; |
|
253 |
_longest_bucket_length = 0; |
|
254 |
_current_hash_bucket = -1; |
|
255 |
_current_bucket_length = 0; |
|
256 |
_used_buckets = 0; |
|
13195 | 257 |
} |
258 |
||
25946 | 259 |
virtual bool do_malloc_site(const MallocSite* e) { |
260 |
if (e->size() == 0) _empty_entries ++; |
|
261 |
_total_entries ++; |
|
262 |
||
263 |
// stack depth distrubution |
|
264 |
int frames = e->call_stack()->frames(); |
|
265 |
_stack_depth_distribution[frames - 1] ++; |
|
266 |
||
267 |
// hash distribution |
|
268 |
int hash_bucket = e->hash() % MallocSiteTable::hash_buckets(); |
|
269 |
if (_current_hash_bucket == -1) { |
|
270 |
_current_hash_bucket = hash_bucket; |
|
271 |
_current_bucket_length = 1; |
|
272 |
} else if (_current_hash_bucket == hash_bucket) { |
|
273 |
_current_bucket_length ++; |
|
274 |
} else { |
|
275 |
record_bucket_length(_current_bucket_length); |
|
276 |
_current_hash_bucket = hash_bucket; |
|
277 |
_current_bucket_length = 1; |
|
278 |
} |
|
279 |
return true; |
|
13195 | 280 |
} |
281 |
||
25946 | 282 |
// walk completed |
283 |
void completed() { |
|
284 |
record_bucket_length(_current_bucket_length); |
|
285 |
} |
|
13195 | 286 |
|
25946 | 287 |
void report_statistics(outputStream* out) { |
288 |
int index; |
|
289 |
out->print_cr("Malloc allocation site table:"); |
|
290 |
out->print_cr("\tTotal entries: %d", _total_entries); |
|
291 |
out->print_cr("\tEmpty entries: %d (%2.2f%%)", _empty_entries, ((float)_empty_entries * 100) / _total_entries); |
|
292 |
out->print_cr(" "); |
|
293 |
out->print_cr("Hash distribution:"); |
|
294 |
if (_used_buckets < MallocSiteTable::hash_buckets()) { |
|
295 |
out->print_cr("empty bucket: %d", (MallocSiteTable::hash_buckets() - _used_buckets)); |
|
296 |
} |
|
297 |
for (index = 0; index < report_threshold; index ++) { |
|
298 |
if (_hash_distribution[index] != 0) { |
|
299 |
if (index == 0) { |
|
300 |
out->print_cr(" %d entry: %d", 1, _hash_distribution[0]); |
|
301 |
} else if (index < 9) { // single digit |
|
302 |
out->print_cr(" %d entries: %d", (index + 1), _hash_distribution[index]); |
|
303 |
} else { |
|
304 |
out->print_cr(" %d entries: %d", (index + 1), _hash_distribution[index]); |
|
305 |
} |
|
13195 | 306 |
} |
307 |
} |
|
25946 | 308 |
if (_bucket_over_threshold > 0) { |
309 |
out->print_cr(" >%d entries: %d", report_threshold, _bucket_over_threshold); |
|
310 |
} |
|
311 |
out->print_cr("most entries: %d", _longest_bucket_length); |
|
312 |
out->print_cr(" "); |
|
313 |
out->print_cr("Call stack depth distribution:"); |
|
314 |
for (index = 0; index < NMT_TrackingStackDepth; index ++) { |
|
315 |
if (_stack_depth_distribution[index] > 0) { |
|
316 |
out->print_cr("\t%d: %d", index + 1, _stack_depth_distribution[index]); |
|
13195 | 317 |
} |
318 |
} |
|
319 |
} |
|
320 |
||
25946 | 321 |
private: |
322 |
void record_bucket_length(int length) { |
|
323 |
_used_buckets ++; |
|
324 |
if (length <= report_threshold) { |
|
325 |
_hash_distribution[length - 1] ++; |
|
326 |
} else { |
|
327 |
_bucket_over_threshold ++; |
|
13300 | 328 |
} |
25946 | 329 |
_longest_bucket_length = MAX2(_longest_bucket_length, length); |
13195 | 330 |
} |
25946 | 331 |
}; |
13195 | 332 |
|
18086
f44cf213a775
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
17838
diff
changeset
|
333 |
|
25946 | 334 |
void MemTracker::tuning_statistics(outputStream* out) { |
335 |
// NMT statistics |
|
336 |
StatisticsWalker walker; |
|
337 |
MallocSiteTable::walk_malloc_site(&walker); |
|
338 |
walker.completed(); |
|
18086
f44cf213a775
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
17838
diff
changeset
|
339 |
|
25946 | 340 |
out->print_cr("Native Memory Tracking Statistics:"); |
341 |
out->print_cr("Malloc allocation site table size: %d", MallocSiteTable::hash_buckets()); |
|
342 |
out->print_cr(" Tracking stack depth: %d", NMT_TrackingStackDepth); |
|
343 |
NOT_PRODUCT(out->print_cr("Peak concurrent access: %d", MallocSiteTable::access_peak_count());) |
|
344 |
out->print_cr(" "); |
|
345 |
walker.report_statistics(out); |
|
18086
f44cf213a775
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
17838
diff
changeset
|
346 |
} |