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