author | zgu |
Tue, 31 Jul 2018 07:35:16 -0400 | |
changeset 51264 | cf34c71ca27c |
parent 50965 | 29eaf3feab30 |
child 51308 | acf02a6f369e |
permissions | -rw-r--r-- |
13195 | 1 |
/* |
49480
d7df2dd501ce
8199809: Don't include frame.inline.hpp and other.inline.hpp from .hpp files
coleenp
parents:
47914
diff
changeset
|
2 |
* Copyright (c) 2012, 2018, 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 |
|
25946 | 27 |
#include "runtime/mutex.hpp" |
50429
83aec1d357d4
8204301: Make OrderAccess functions available to hpp rather than inline.hpp files
coleenp
parents:
49980
diff
changeset
|
28 |
#include "runtime/orderAccess.hpp" |
47914
564882d918d4
8190357: NMT: Include metadata information in NMT final report when PrintNMTStatistics is on
zgu
parents:
47765
diff
changeset
|
29 |
#include "runtime/vmThread.hpp" |
564882d918d4
8190357: NMT: Include metadata information in NMT final report when PrintNMTStatistics is on
zgu
parents:
47765
diff
changeset
|
30 |
#include "runtime/vm_operations.hpp" |
25946 | 31 |
#include "services/memBaseline.hpp" |
13195 | 32 |
#include "services/memReporter.hpp" |
25946 | 33 |
#include "services/mallocTracker.inline.hpp" |
13195 | 34 |
#include "services/memTracker.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 |
|
25946 | 39 |
#ifdef SOLARIS |
40 |
volatile bool NMT_stack_walkable = false; |
|
41 |
#else |
|
42 |
volatile bool NMT_stack_walkable = true; |
|
43 |
#endif |
|
13195 | 44 |
|
25946 | 45 |
volatile NMT_TrackingLevel MemTracker::_tracking_level = NMT_unknown; |
46 |
NMT_TrackingLevel MemTracker::_cmdline_tracking_level = NMT_unknown; |
|
47 |
||
48 |
MemBaseline MemTracker::_baseline; |
|
49 |
Mutex* MemTracker::_query_lock = NULL; |
|
50 |
bool MemTracker::_is_nmt_env_valid = true; |
|
13195 | 51 |
|
52 |
||
25946 | 53 |
NMT_TrackingLevel MemTracker::init_tracking_level() { |
51264
cf34c71ca27c
8208499: NMT: Missing memory tag for Safepoint polling page
zgu
parents:
50965
diff
changeset
|
54 |
// 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
|
55 |
// make sure that we don't overflow it. |
cf34c71ca27c
8208499: NMT: Missing memory tag for Safepoint polling page
zgu
parents:
50965
diff
changeset
|
56 |
STATIC_ASSERT(mt_number_of_types <= max_jubyte); |
cf34c71ca27c
8208499: NMT: Missing memory tag for Safepoint polling page
zgu
parents:
50965
diff
changeset
|
57 |
|
25946 | 58 |
NMT_TrackingLevel level = NMT_off; |
59 |
char buf[64]; |
|
60 |
jio_snprintf(buf, sizeof(buf), "NMT_LEVEL_%d", os::current_process_id()); |
|
30125 | 61 |
const char *nmt_option = ::getenv(buf); |
62 |
if (nmt_option != NULL) { |
|
25946 | 63 |
if (strcmp(nmt_option, "summary") == 0) { |
64 |
level = NMT_summary; |
|
65 |
} else if (strcmp(nmt_option, "detail") == 0) { |
|
66 |
level = NMT_detail; |
|
67 |
} else if (strcmp(nmt_option, "off") != 0) { |
|
68 |
// The option value is invalid |
|
69 |
_is_nmt_env_valid = false; |
|
13195 | 70 |
} |
71 |
||
25946 | 72 |
// Remove the environment variable to avoid leaking to child processes |
73 |
os::unsetenv(buf); |
|
74 |
} |
|
13301
f6dd749390dd
7181986: NMT ON: Assertion failure when running jdi ExpiredRequestDeletionTest
zgu
parents:
13195
diff
changeset
|
75 |
|
25946 | 76 |
if (!MallocTracker::initialize(level) || |
77 |
!VirtualMemoryTracker::initialize(level)) { |
|
78 |
level = NMT_off; |
|
13195 | 79 |
} |
25946 | 80 |
return level; |
13195 | 81 |
} |
82 |
||
25946 | 83 |
void MemTracker::init() { |
26136
254c226e4402
8054368: nsk/jdi/VirtualMachine/exit/exit002 crash with detail tracking on (NMT2)
zgu
parents:
25946
diff
changeset
|
84 |
NMT_TrackingLevel level = tracking_level(); |
254c226e4402
8054368: nsk/jdi/VirtualMachine/exit/exit002 crash with detail tracking on (NMT2)
zgu
parents:
25946
diff
changeset
|
85 |
if (level >= NMT_summary) { |
254c226e4402
8054368: nsk/jdi/VirtualMachine/exit/exit002 crash with detail tracking on (NMT2)
zgu
parents:
25946
diff
changeset
|
86 |
if (!VirtualMemoryTracker::late_initialize(level)) { |
254c226e4402
8054368: nsk/jdi/VirtualMachine/exit/exit002 crash with detail tracking on (NMT2)
zgu
parents:
25946
diff
changeset
|
87 |
shutdown(); |
254c226e4402
8054368: nsk/jdi/VirtualMachine/exit/exit002 crash with detail tracking on (NMT2)
zgu
parents:
25946
diff
changeset
|
88 |
return; |
254c226e4402
8054368: nsk/jdi/VirtualMachine/exit/exit002 crash with detail tracking on (NMT2)
zgu
parents:
25946
diff
changeset
|
89 |
} |
25946 | 90 |
_query_lock = new (std::nothrow) Mutex(Monitor::max_nonleaf, "NMT_queryLock"); |
91 |
// Already OOM. It is unlikely, but still have to handle it. |
|
92 |
if (_query_lock == NULL) { |
|
93 |
shutdown(); |
|
13195 | 94 |
} |
95 |
} |
|
96 |
} |
|
97 |
||
25946 | 98 |
bool MemTracker::check_launcher_nmt_support(const char* value) { |
99 |
if (strcmp(value, "=detail") == 0) { |
|
100 |
if (MemTracker::tracking_level() != NMT_detail) { |
|
101 |
return false; |
|
102 |
} |
|
103 |
} else if (strcmp(value, "=summary") == 0) { |
|
104 |
if (MemTracker::tracking_level() != NMT_summary) { |
|
105 |
return false; |
|
106 |
} |
|
107 |
} else if (strcmp(value, "=off") == 0) { |
|
108 |
if (MemTracker::tracking_level() != NMT_off) { |
|
109 |
return false; |
|
110 |
} |
|
111 |
} else { |
|
112 |
_is_nmt_env_valid = false; |
|
113 |
} |
|
114 |
||
115 |
return true; |
|
116 |
} |
|
117 |
||
118 |
bool MemTracker::verify_nmt_option() { |
|
119 |
return _is_nmt_env_valid; |
|
120 |
} |
|
121 |
||
122 |
void* MemTracker::malloc_base(void* memblock) { |
|
123 |
return MallocTracker::get_base(memblock); |
|
124 |
} |
|
125 |
||
126 |
void Tracker::record(address addr, size_t size) { |
|
127 |
if (MemTracker::tracking_level() < NMT_summary) return; |
|
128 |
switch(_type) { |
|
129 |
case uncommit: |
|
130 |
VirtualMemoryTracker::remove_uncommitted_region(addr, size); |
|
131 |
break; |
|
132 |
case release: |
|
133 |
VirtualMemoryTracker::remove_released_region(addr, size); |
|
134 |
break; |
|
135 |
default: |
|
136 |
ShouldNotReachHere(); |
|
13195 | 137 |
} |
138 |
} |
|
139 |
||
25946 | 140 |
|
27162
0a4a7276949b
8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents:
26145
diff
changeset
|
141 |
// Shutdown can only be issued via JCmd, and NMT JCmd is serialized by lock |
25946 | 142 |
void MemTracker::shutdown() { |
27162
0a4a7276949b
8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents:
26145
diff
changeset
|
143 |
// We can only shutdown NMT to minimal tracking level if it is ever on. |
25946 | 144 |
if (tracking_level () > NMT_minimal) { |
145 |
transition_to(NMT_minimal); |
|
146 |
} |
|
147 |
} |
|
148 |
||
149 |
bool MemTracker::transition_to(NMT_TrackingLevel level) { |
|
150 |
NMT_TrackingLevel current_level = tracking_level(); |
|
13195 | 151 |
|
27162
0a4a7276949b
8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents:
26145
diff
changeset
|
152 |
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
|
153 |
|
25946 | 154 |
if (current_level == level) { |
155 |
return true; |
|
156 |
} else if (current_level > level) { |
|
27162
0a4a7276949b
8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents:
26145
diff
changeset
|
157 |
// Downgrade tracking level, we want to lower the tracking level first |
25946 | 158 |
_tracking_level = level; |
159 |
// Make _tracking_level visible immediately. |
|
160 |
OrderAccess::fence(); |
|
161 |
VirtualMemoryTracker::transition(current_level, level); |
|
162 |
MallocTracker::transition(current_level, level); |
|
13195 | 163 |
} else { |
27162
0a4a7276949b
8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents:
26145
diff
changeset
|
164 |
// Upgrading tracking level is not supported and has never been supported. |
0a4a7276949b
8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents:
26145
diff
changeset
|
165 |
// Allocating and deallocating malloc tracking structures is not thread safe and |
0a4a7276949b
8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents:
26145
diff
changeset
|
166 |
// leads to inconsistencies unless a lot coarser locks are added. |
13195 | 167 |
} |
25946 | 168 |
return true; |
13195 | 169 |
} |
170 |
||
27162
0a4a7276949b
8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents:
26145
diff
changeset
|
171 |
void MemTracker::report(bool summary_only, outputStream* output) { |
0a4a7276949b
8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents:
26145
diff
changeset
|
172 |
assert(output != NULL, "No output stream"); |
0a4a7276949b
8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents:
26145
diff
changeset
|
173 |
MemBaseline baseline; |
0a4a7276949b
8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents:
26145
diff
changeset
|
174 |
if (baseline.baseline(summary_only)) { |
0a4a7276949b
8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents:
26145
diff
changeset
|
175 |
if (summary_only) { |
0a4a7276949b
8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents:
26145
diff
changeset
|
176 |
MemSummaryReporter rpt(baseline, output); |
0a4a7276949b
8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents:
26145
diff
changeset
|
177 |
rpt.report(); |
0a4a7276949b
8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents:
26145
diff
changeset
|
178 |
} else { |
0a4a7276949b
8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents:
26145
diff
changeset
|
179 |
MemDetailReporter rpt(baseline, output); |
0a4a7276949b
8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents:
26145
diff
changeset
|
180 |
rpt.report(); |
49980 | 181 |
output->print("Metaspace:"); |
47914
564882d918d4
8190357: NMT: Include metadata information in NMT final report when PrintNMTStatistics is on
zgu
parents:
47765
diff
changeset
|
182 |
// Metadata reporting requires a safepoint, so avoid it if VM is not in good state. |
564882d918d4
8190357: NMT: Include metadata information in NMT final report when PrintNMTStatistics is on
zgu
parents:
47765
diff
changeset
|
183 |
assert(!VMError::fatal_error_in_progress(), "Do not report metadata in error report"); |
49980 | 184 |
VM_PrintMetadata vmop(output, K, |
185 |
MetaspaceUtils::rf_show_loaders | |
|
186 |
MetaspaceUtils::rf_break_down_by_spacetype); |
|
47914
564882d918d4
8190357: NMT: Include metadata information in NMT final report when PrintNMTStatistics is on
zgu
parents:
47765
diff
changeset
|
187 |
VMThread::execute(&vmop); |
27162
0a4a7276949b
8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents:
26145
diff
changeset
|
188 |
} |
13195 | 189 |
} |
190 |
} |
|
191 |
||
25946 | 192 |
// This is a walker to gather malloc site hashtable statistics, |
193 |
// the result is used for tuning. |
|
194 |
class StatisticsWalker : public MallocSiteWalker { |
|
195 |
private: |
|
196 |
enum Threshold { |
|
197 |
// aggregates statistics over this threshold into one |
|
198 |
// line item. |
|
199 |
report_threshold = 20 |
|
200 |
}; |
|
13195 | 201 |
|
25946 | 202 |
private: |
203 |
// Number of allocation sites that have all memory freed |
|
204 |
int _empty_entries; |
|
205 |
// Total number of allocation sites, include empty sites |
|
206 |
int _total_entries; |
|
207 |
// Number of captured call stack distribution |
|
208 |
int _stack_depth_distribution[NMT_TrackingStackDepth]; |
|
209 |
// Hash distribution |
|
210 |
int _hash_distribution[report_threshold]; |
|
211 |
// Number of hash buckets that have entries over the threshold |
|
212 |
int _bucket_over_threshold; |
|
13195 | 213 |
|
25946 | 214 |
// The hash bucket that walker is currently walking |
215 |
int _current_hash_bucket; |
|
216 |
// The length of current hash bucket |
|
217 |
int _current_bucket_length; |
|
218 |
// Number of hash buckets that are not empty |
|
219 |
int _used_buckets; |
|
220 |
// Longest hash bucket length |
|
221 |
int _longest_bucket_length; |
|
13195 | 222 |
|
25946 | 223 |
public: |
224 |
StatisticsWalker() : _empty_entries(0), _total_entries(0) { |
|
225 |
int index = 0; |
|
226 |
for (index = 0; index < NMT_TrackingStackDepth; index ++) { |
|
227 |
_stack_depth_distribution[index] = 0; |
|
13195 | 228 |
} |
25946 | 229 |
for (index = 0; index < report_threshold; index ++) { |
230 |
_hash_distribution[index] = 0; |
|
231 |
} |
|
232 |
_bucket_over_threshold = 0; |
|
233 |
_longest_bucket_length = 0; |
|
234 |
_current_hash_bucket = -1; |
|
235 |
_current_bucket_length = 0; |
|
236 |
_used_buckets = 0; |
|
13195 | 237 |
} |
238 |
||
25946 | 239 |
virtual bool do_malloc_site(const MallocSite* e) { |
240 |
if (e->size() == 0) _empty_entries ++; |
|
241 |
_total_entries ++; |
|
242 |
||
243 |
// stack depth distrubution |
|
244 |
int frames = e->call_stack()->frames(); |
|
245 |
_stack_depth_distribution[frames - 1] ++; |
|
246 |
||
247 |
// hash distribution |
|
248 |
int hash_bucket = e->hash() % MallocSiteTable::hash_buckets(); |
|
249 |
if (_current_hash_bucket == -1) { |
|
250 |
_current_hash_bucket = hash_bucket; |
|
251 |
_current_bucket_length = 1; |
|
252 |
} else if (_current_hash_bucket == hash_bucket) { |
|
253 |
_current_bucket_length ++; |
|
254 |
} else { |
|
255 |
record_bucket_length(_current_bucket_length); |
|
256 |
_current_hash_bucket = hash_bucket; |
|
257 |
_current_bucket_length = 1; |
|
258 |
} |
|
259 |
return true; |
|
13195 | 260 |
} |
261 |
||
25946 | 262 |
// walk completed |
263 |
void completed() { |
|
264 |
record_bucket_length(_current_bucket_length); |
|
265 |
} |
|
13195 | 266 |
|
25946 | 267 |
void report_statistics(outputStream* out) { |
268 |
int index; |
|
269 |
out->print_cr("Malloc allocation site table:"); |
|
270 |
out->print_cr("\tTotal entries: %d", _total_entries); |
|
271 |
out->print_cr("\tEmpty entries: %d (%2.2f%%)", _empty_entries, ((float)_empty_entries * 100) / _total_entries); |
|
272 |
out->print_cr(" "); |
|
273 |
out->print_cr("Hash distribution:"); |
|
274 |
if (_used_buckets < MallocSiteTable::hash_buckets()) { |
|
275 |
out->print_cr("empty bucket: %d", (MallocSiteTable::hash_buckets() - _used_buckets)); |
|
276 |
} |
|
277 |
for (index = 0; index < report_threshold; index ++) { |
|
278 |
if (_hash_distribution[index] != 0) { |
|
279 |
if (index == 0) { |
|
280 |
out->print_cr(" %d entry: %d", 1, _hash_distribution[0]); |
|
281 |
} else if (index < 9) { // single digit |
|
282 |
out->print_cr(" %d entries: %d", (index + 1), _hash_distribution[index]); |
|
283 |
} else { |
|
284 |
out->print_cr(" %d entries: %d", (index + 1), _hash_distribution[index]); |
|
285 |
} |
|
13195 | 286 |
} |
287 |
} |
|
25946 | 288 |
if (_bucket_over_threshold > 0) { |
289 |
out->print_cr(" >%d entries: %d", report_threshold, _bucket_over_threshold); |
|
290 |
} |
|
291 |
out->print_cr("most entries: %d", _longest_bucket_length); |
|
292 |
out->print_cr(" "); |
|
293 |
out->print_cr("Call stack depth distribution:"); |
|
294 |
for (index = 0; index < NMT_TrackingStackDepth; index ++) { |
|
295 |
if (_stack_depth_distribution[index] > 0) { |
|
296 |
out->print_cr("\t%d: %d", index + 1, _stack_depth_distribution[index]); |
|
13195 | 297 |
} |
298 |
} |
|
299 |
} |
|
300 |
||
25946 | 301 |
private: |
302 |
void record_bucket_length(int length) { |
|
303 |
_used_buckets ++; |
|
304 |
if (length <= report_threshold) { |
|
305 |
_hash_distribution[length - 1] ++; |
|
306 |
} else { |
|
307 |
_bucket_over_threshold ++; |
|
13300 | 308 |
} |
25946 | 309 |
_longest_bucket_length = MAX2(_longest_bucket_length, length); |
13195 | 310 |
} |
25946 | 311 |
}; |
13195 | 312 |
|
18086
f44cf213a775
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
17838
diff
changeset
|
313 |
|
25946 | 314 |
void MemTracker::tuning_statistics(outputStream* out) { |
315 |
// NMT statistics |
|
316 |
StatisticsWalker walker; |
|
317 |
MallocSiteTable::walk_malloc_site(&walker); |
|
318 |
walker.completed(); |
|
18086
f44cf213a775
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
17838
diff
changeset
|
319 |
|
25946 | 320 |
out->print_cr("Native Memory Tracking Statistics:"); |
321 |
out->print_cr("Malloc allocation site table size: %d", MallocSiteTable::hash_buckets()); |
|
322 |
out->print_cr(" Tracking stack depth: %d", NMT_TrackingStackDepth); |
|
323 |
NOT_PRODUCT(out->print_cr("Peak concurrent access: %d", MallocSiteTable::access_peak_count());) |
|
324 |
out->print_cr(" "); |
|
325 |
walker.report_statistics(out); |
|
18086
f44cf213a775
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
17838
diff
changeset
|
326 |
} |