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