Mercurial > hg > graal-jvmci-8
annotate src/share/vm/services/memTracker.cpp @ 20363:36c9011aaead
8054368: nsk/jdi/VirtualMachine/exit/exit002 crash with detail tracking on (NMT2)
Summary: Dynamic allocate _reserved_regions instead of static object to avoid racing during process exit
Reviewed-by: dholmes, coleenp
author | zgu |
---|---|
date | Thu, 14 Aug 2014 09:02:51 -0400 |
parents | 833b0f92429a |
children | c6211b707068 |
rev | line source |
---|---|
6197 | 1 /* |
20360 | 2 * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved. |
6197 | 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 | |
20360 | 26 #include "runtime/mutex.hpp" |
27 #include "services/memBaseline.hpp" | |
6197 | 28 #include "services/memReporter.hpp" |
20360 | 29 #include "services/mallocTracker.inline.hpp" |
6197 | 30 #include "services/memTracker.hpp" |
10355
fb14e9ed1594
8011064: Some tests have failed with SIGSEGV on arm-hflt on build b82
jprovino
parents:
10148
diff
changeset
|
31 #include "utilities/defaultStream.hpp" |
6197 | 32 |
20360 | 33 #ifdef SOLARIS |
34 volatile bool NMT_stack_walkable = false; | |
35 #else | |
36 volatile bool NMT_stack_walkable = true; | |
37 #endif | |
6197 | 38 |
20360 | 39 volatile NMT_TrackingLevel MemTracker::_tracking_level = NMT_unknown; |
40 NMT_TrackingLevel MemTracker::_cmdline_tracking_level = NMT_unknown; | |
41 | |
42 NativeCallStack emptyStack(0, false); | |
43 | |
44 MemBaseline MemTracker::_baseline; | |
45 Mutex* MemTracker::_query_lock = NULL; | |
46 bool MemTracker::_is_nmt_env_valid = true; | |
6197 | 47 |
48 | |
20360 | 49 NMT_TrackingLevel MemTracker::init_tracking_level() { |
50 NMT_TrackingLevel level = NMT_off; | |
51 char buf[64]; | |
52 char nmt_option[64]; | |
53 jio_snprintf(buf, sizeof(buf), "NMT_LEVEL_%d", os::current_process_id()); | |
54 if (os::getenv(buf, nmt_option, sizeof(nmt_option))) { | |
55 if (strcmp(nmt_option, "summary") == 0) { | |
56 level = NMT_summary; | |
57 } else if (strcmp(nmt_option, "detail") == 0) { | |
11129 | 58 #if PLATFORM_NATIVE_STACK_WALKING_SUPPORTED |
20360 | 59 level = NMT_detail; |
11129 | 60 #else |
20360 | 61 level = NMT_summary; |
62 #endif // PLATFORM_NATIVE_STACK_WALKING_SUPPORTED | |
63 } else if (strcmp(nmt_option, "off") != 0) { | |
64 // The option value is invalid | |
65 _is_nmt_env_valid = false; | |
6197 | 66 } |
67 | |
20360 | 68 // Remove the environment variable to avoid leaking to child processes |
69 os::unsetenv(buf); | |
70 } | |
6232
f1f45dddb0bd
7181986: NMT ON: Assertion failure when running jdi ExpiredRequestDeletionTest
zgu
parents:
6197
diff
changeset
|
71 |
20360 | 72 if (!MallocTracker::initialize(level) || |
73 !VirtualMemoryTracker::initialize(level)) { | |
74 level = NMT_off; | |
6197 | 75 } |
20360 | 76 return level; |
6197 | 77 } |
78 | |
20360 | 79 void MemTracker::init() { |
20363
36c9011aaead
8054368: nsk/jdi/VirtualMachine/exit/exit002 crash with detail tracking on (NMT2)
zgu
parents:
20360
diff
changeset
|
80 NMT_TrackingLevel level = tracking_level(); |
36c9011aaead
8054368: nsk/jdi/VirtualMachine/exit/exit002 crash with detail tracking on (NMT2)
zgu
parents:
20360
diff
changeset
|
81 if (level >= NMT_summary) { |
36c9011aaead
8054368: nsk/jdi/VirtualMachine/exit/exit002 crash with detail tracking on (NMT2)
zgu
parents:
20360
diff
changeset
|
82 if (!VirtualMemoryTracker::late_initialize(level)) { |
36c9011aaead
8054368: nsk/jdi/VirtualMachine/exit/exit002 crash with detail tracking on (NMT2)
zgu
parents:
20360
diff
changeset
|
83 shutdown(); |
36c9011aaead
8054368: nsk/jdi/VirtualMachine/exit/exit002 crash with detail tracking on (NMT2)
zgu
parents:
20360
diff
changeset
|
84 return; |
36c9011aaead
8054368: nsk/jdi/VirtualMachine/exit/exit002 crash with detail tracking on (NMT2)
zgu
parents:
20360
diff
changeset
|
85 } |
20360 | 86 _query_lock = new (std::nothrow) Mutex(Monitor::max_nonleaf, "NMT_queryLock"); |
87 // Already OOM. It is unlikely, but still have to handle it. | |
88 if (_query_lock == NULL) { | |
89 shutdown(); | |
6197 | 90 } |
91 } | |
92 } | |
93 | |
20360 | 94 bool MemTracker::check_launcher_nmt_support(const char* value) { |
95 if (strcmp(value, "=detail") == 0) { | |
96 #if !PLATFORM_NATIVE_STACK_WALKING_SUPPORTED | |
97 jio_fprintf(defaultStream::error_stream(), | |
98 "NMT detail is not supported on this platform. Using NMT summary instead.\n"); | |
99 if (MemTracker::tracking_level() != NMT_summary) { | |
100 return false; | |
101 } | |
102 #else | |
103 if (MemTracker::tracking_level() != NMT_detail) { | |
104 return false; | |
105 } | |
106 #endif | |
107 } else if (strcmp(value, "=summary") == 0) { | |
108 if (MemTracker::tracking_level() != NMT_summary) { | |
109 return false; | |
110 } | |
111 } else if (strcmp(value, "=off") == 0) { | |
112 if (MemTracker::tracking_level() != NMT_off) { | |
113 return false; | |
114 } | |
115 } else { | |
116 _is_nmt_env_valid = false; | |
117 } | |
118 | |
119 return true; | |
120 } | |
121 | |
122 bool MemTracker::verify_nmt_option() { | |
123 return _is_nmt_env_valid; | |
124 } | |
125 | |
126 void* MemTracker::malloc_base(void* memblock) { | |
127 return MallocTracker::get_base(memblock); | |
128 } | |
129 | |
130 void Tracker::record(address addr, size_t size) { | |
131 if (MemTracker::tracking_level() < NMT_summary) return; | |
132 switch(_type) { | |
133 case uncommit: | |
134 VirtualMemoryTracker::remove_uncommitted_region(addr, size); | |
135 break; | |
136 case release: | |
137 VirtualMemoryTracker::remove_released_region(addr, size); | |
138 break; | |
139 default: | |
140 ShouldNotReachHere(); | |
6197 | 141 } |
142 } | |
143 | |
20360 | 144 |
145 // Shutdown can only be issued via JCmd, and NMT JCmd is serialized | |
146 // by lock | |
147 void MemTracker::shutdown() { | |
148 // We can only shutdown NMT to minimal tracking level if it is | |
149 // ever on. | |
150 if (tracking_level () > NMT_minimal) { | |
151 transition_to(NMT_minimal); | |
152 } | |
153 } | |
154 | |
155 bool MemTracker::transition_to(NMT_TrackingLevel level) { | |
156 NMT_TrackingLevel current_level = tracking_level(); | |
6197 | 157 |
20360 | 158 if (current_level == level) { |
159 return true; | |
160 } else if (current_level > level) { | |
161 // Downgrade tracking level, we want to lower the tracking | |
162 // level first | |
163 _tracking_level = level; | |
164 // Make _tracking_level visible immediately. | |
165 OrderAccess::fence(); | |
166 VirtualMemoryTracker::transition(current_level, level); | |
167 MallocTracker::transition(current_level, level); | |
168 | |
169 if (level == NMT_minimal) _baseline.reset(); | |
6197 | 170 } else { |
20360 | 171 VirtualMemoryTracker::transition(current_level, level); |
172 MallocTracker::transition(current_level, level); | |
6197 | 173 |
20360 | 174 _tracking_level = level; |
175 // Make _tracking_level visible immediately. | |
176 OrderAccess::fence(); | |
6197 | 177 } |
178 | |
20360 | 179 return true; |
6197 | 180 } |
181 | |
20360 | 182 void MemTracker::final_report(outputStream* output) { |
183 assert(output != NULL, "No output stream"); | |
184 if (tracking_level() >= NMT_summary) { | |
185 MallocMemorySnapshot* malloc_memory_snapshot = | |
186 MallocMemorySummary::as_snapshot(); | |
187 malloc_memory_snapshot->make_adjustment(); | |
188 | |
189 VirtualMemorySnapshot* virtual_memory_snapshot = | |
190 VirtualMemorySummary::as_snapshot(); | |
191 | |
192 MemSummaryReporter rptr(malloc_memory_snapshot, | |
193 virtual_memory_snapshot, output); | |
194 rptr.report(); | |
195 // shutdown NMT, the data no longer accurate | |
196 shutdown(); | |
6197 | 197 } |
198 } | |
199 | |
20360 | 200 // This is a walker to gather malloc site hashtable statistics, |
201 // the result is used for tuning. | |
202 class StatisticsWalker : public MallocSiteWalker { | |
203 private: | |
204 enum Threshold { | |
205 // aggregates statistics over this threshold into one | |
206 // line item. | |
207 report_threshold = 20 | |
208 }; | |
6197 | 209 |
20360 | 210 private: |
211 // Number of allocation sites that have all memory freed | |
212 int _empty_entries; | |
213 // Total number of allocation sites, include empty sites | |
214 int _total_entries; | |
215 // Number of captured call stack distribution | |
216 int _stack_depth_distribution[NMT_TrackingStackDepth]; | |
217 // Hash distribution | |
218 int _hash_distribution[report_threshold]; | |
219 // Number of hash buckets that have entries over the threshold | |
220 int _bucket_over_threshold; | |
6197 | 221 |
20360 | 222 // The hash bucket that walker is currently walking |
223 int _current_hash_bucket; | |
224 // The length of current hash bucket | |
225 int _current_bucket_length; | |
226 // Number of hash buckets that are not empty | |
227 int _used_buckets; | |
228 // Longest hash bucket length | |
229 int _longest_bucket_length; | |
6197 | 230 |
20360 | 231 public: |
232 StatisticsWalker() : _empty_entries(0), _total_entries(0) { | |
233 int index = 0; | |
234 for (index = 0; index < NMT_TrackingStackDepth; index ++) { | |
235 _stack_depth_distribution[index] = 0; | |
6197 | 236 } |
20360 | 237 for (index = 0; index < report_threshold; index ++) { |
238 _hash_distribution[index] = 0; | |
239 } | |
240 _bucket_over_threshold = 0; | |
241 _longest_bucket_length = 0; | |
242 _current_hash_bucket = -1; | |
243 _current_bucket_length = 0; | |
244 _used_buckets = 0; | |
6197 | 245 } |
246 | |
20360 | 247 virtual bool at(const MallocSite* e) { |
248 if (e->size() == 0) _empty_entries ++; | |
249 _total_entries ++; | |
250 | |
251 // stack depth distrubution | |
252 int frames = e->call_stack()->frames(); | |
253 _stack_depth_distribution[frames - 1] ++; | |
254 | |
255 // hash distribution | |
256 int hash_bucket = e->hash() % MallocSiteTable::hash_buckets(); | |
257 if (_current_hash_bucket == -1) { | |
258 _current_hash_bucket = hash_bucket; | |
259 _current_bucket_length = 1; | |
260 } else if (_current_hash_bucket == hash_bucket) { | |
261 _current_bucket_length ++; | |
262 } else { | |
263 record_bucket_length(_current_bucket_length); | |
264 _current_hash_bucket = hash_bucket; | |
265 _current_bucket_length = 1; | |
266 } | |
267 return true; | |
6197 | 268 } |
269 | |
20360 | 270 // walk completed |
271 void completed() { | |
272 record_bucket_length(_current_bucket_length); | |
273 } | |
6197 | 274 |
20360 | 275 void report_statistics(outputStream* out) { |
276 int index; | |
277 out->print_cr("Malloc allocation site table:"); | |
278 out->print_cr("\tTotal entries: %d", _total_entries); | |
279 out->print_cr("\tEmpty entries: %d (%2.2f%%)", _empty_entries, ((float)_empty_entries * 100) / _total_entries); | |
280 out->print_cr(" "); | |
281 out->print_cr("Hash distribution:"); | |
282 if (_used_buckets < MallocSiteTable::hash_buckets()) { | |
283 out->print_cr("empty bucket: %d", (MallocSiteTable::hash_buckets() - _used_buckets)); | |
284 } | |
285 for (index = 0; index < report_threshold; index ++) { | |
286 if (_hash_distribution[index] != 0) { | |
287 if (index == 0) { | |
288 out->print_cr(" %d entry: %d", 1, _hash_distribution[0]); | |
289 } else if (index < 9) { // single digit | |
290 out->print_cr(" %d entries: %d", (index + 1), _hash_distribution[index]); | |
291 } else { | |
292 out->print_cr(" %d entries: %d", (index + 1), _hash_distribution[index]); | |
293 } | |
6197 | 294 } |
295 } | |
20360 | 296 if (_bucket_over_threshold > 0) { |
297 out->print_cr(" >%d entries: %d", report_threshold, _bucket_over_threshold); | |
298 } | |
299 out->print_cr("most entries: %d", _longest_bucket_length); | |
300 out->print_cr(" "); | |
301 out->print_cr("Call stack depth distribution:"); | |
302 for (index = 0; index < NMT_TrackingStackDepth; index ++) { | |
303 if (_stack_depth_distribution[index] > 0) { | |
304 out->print_cr("\t%d: %d", index + 1, _stack_depth_distribution[index]); | |
6197 | 305 } |
306 } | |
307 } | |
308 | |
20360 | 309 private: |
310 void record_bucket_length(int length) { | |
311 _used_buckets ++; | |
312 if (length <= report_threshold) { | |
313 _hash_distribution[length - 1] ++; | |
314 } else { | |
315 _bucket_over_threshold ++; | |
6231 | 316 } |
20360 | 317 _longest_bucket_length = MAX2(_longest_bucket_length, length); |
6197 | 318 } |
20360 | 319 }; |
6197 | 320 |
10986
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
321 |
20360 | 322 void MemTracker::tuning_statistics(outputStream* out) { |
323 // NMT statistics | |
324 StatisticsWalker walker; | |
325 MallocSiteTable::walk_malloc_site(&walker); | |
326 walker.completed(); | |
10986
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
327 |
20360 | 328 out->print_cr("Native Memory Tracking Statistics:"); |
329 out->print_cr("Malloc allocation site table size: %d", MallocSiteTable::hash_buckets()); | |
330 out->print_cr(" Tracking stack depth: %d", NMT_TrackingStackDepth); | |
331 NOT_PRODUCT(out->print_cr("Peak concurrent access: %d", MallocSiteTable::access_peak_count());) | |
332 out->print_cr(" "); | |
333 walker.report_statistics(out); | |
10986
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
334 } |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
335 |