Mercurial > hg > graal-compiler
annotate src/share/vm/services/memTracker.cpp @ 20591:28051f14c328
8043158: Crash in CodeSweeperSweepNoFlushTest in CompileQueue::free_all()
Summary: Acquire lock before calling notify()
Reviewed-by: twisti, thartmann
author | anoll |
---|---|
date | Fri, 20 Jun 2014 09:25:13 +0200 |
parents | 417e3b8d04c5 |
children |
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 MemBaseline MemTracker::_baseline; | |
43 Mutex* MemTracker::_query_lock = NULL; | |
44 bool MemTracker::_is_nmt_env_valid = true; | |
6197 | 45 |
46 | |
20360 | 47 NMT_TrackingLevel MemTracker::init_tracking_level() { |
48 NMT_TrackingLevel level = NMT_off; | |
49 char buf[64]; | |
50 char nmt_option[64]; | |
51 jio_snprintf(buf, sizeof(buf), "NMT_LEVEL_%d", os::current_process_id()); | |
52 if (os::getenv(buf, nmt_option, sizeof(nmt_option))) { | |
53 if (strcmp(nmt_option, "summary") == 0) { | |
54 level = NMT_summary; | |
55 } else if (strcmp(nmt_option, "detail") == 0) { | |
11129 | 56 #if PLATFORM_NATIVE_STACK_WALKING_SUPPORTED |
20360 | 57 level = NMT_detail; |
11129 | 58 #else |
20360 | 59 level = NMT_summary; |
60 #endif // PLATFORM_NATIVE_STACK_WALKING_SUPPORTED | |
61 } else if (strcmp(nmt_option, "off") != 0) { | |
62 // The option value is invalid | |
63 _is_nmt_env_valid = false; | |
6197 | 64 } |
65 | |
20360 | 66 // Remove the environment variable to avoid leaking to child processes |
67 os::unsetenv(buf); | |
68 } | |
6232
f1f45dddb0bd
7181986: NMT ON: Assertion failure when running jdi ExpiredRequestDeletionTest
zgu
parents:
6197
diff
changeset
|
69 |
20364 | 70 // Construct NativeCallStack::EMPTY_STACK. It may get constructed twice, |
71 // but it is benign, the results are the same. | |
72 ::new ((void*)&NativeCallStack::EMPTY_STACK) NativeCallStack(0, false); | |
73 | |
20360 | 74 if (!MallocTracker::initialize(level) || |
75 !VirtualMemoryTracker::initialize(level)) { | |
76 level = NMT_off; | |
6197 | 77 } |
20360 | 78 return level; |
6197 | 79 } |
80 | |
20360 | 81 void MemTracker::init() { |
20363
36c9011aaead
8054368: nsk/jdi/VirtualMachine/exit/exit002 crash with detail tracking on (NMT2)
zgu
parents:
20360
diff
changeset
|
82 NMT_TrackingLevel level = tracking_level(); |
36c9011aaead
8054368: nsk/jdi/VirtualMachine/exit/exit002 crash with detail tracking on (NMT2)
zgu
parents:
20360
diff
changeset
|
83 if (level >= NMT_summary) { |
36c9011aaead
8054368: nsk/jdi/VirtualMachine/exit/exit002 crash with detail tracking on (NMT2)
zgu
parents:
20360
diff
changeset
|
84 if (!VirtualMemoryTracker::late_initialize(level)) { |
36c9011aaead
8054368: nsk/jdi/VirtualMachine/exit/exit002 crash with detail tracking on (NMT2)
zgu
parents:
20360
diff
changeset
|
85 shutdown(); |
36c9011aaead
8054368: nsk/jdi/VirtualMachine/exit/exit002 crash with detail tracking on (NMT2)
zgu
parents:
20360
diff
changeset
|
86 return; |
36c9011aaead
8054368: nsk/jdi/VirtualMachine/exit/exit002 crash with detail tracking on (NMT2)
zgu
parents:
20360
diff
changeset
|
87 } |
20360 | 88 _query_lock = new (std::nothrow) Mutex(Monitor::max_nonleaf, "NMT_queryLock"); |
89 // Already OOM. It is unlikely, but still have to handle it. | |
90 if (_query_lock == NULL) { | |
91 shutdown(); | |
6197 | 92 } |
93 } | |
94 } | |
95 | |
20360 | 96 bool MemTracker::check_launcher_nmt_support(const char* value) { |
97 if (strcmp(value, "=detail") == 0) { | |
98 #if !PLATFORM_NATIVE_STACK_WALKING_SUPPORTED | |
99 jio_fprintf(defaultStream::error_stream(), | |
100 "NMT detail is not supported on this platform. Using NMT summary instead.\n"); | |
101 if (MemTracker::tracking_level() != NMT_summary) { | |
102 return false; | |
103 } | |
104 #else | |
105 if (MemTracker::tracking_level() != NMT_detail) { | |
106 return false; | |
107 } | |
108 #endif | |
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(); | |
6197 | 143 } |
144 } | |
145 | |
20360 | 146 |
20553
417e3b8d04c5
8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents:
20364
diff
changeset
|
147 // Shutdown can only be issued via JCmd, and NMT JCmd is serialized by lock |
20360 | 148 void MemTracker::shutdown() { |
20553
417e3b8d04c5
8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents:
20364
diff
changeset
|
149 // We can only shutdown NMT to minimal tracking level if it is ever on. |
20360 | 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 |
20553
417e3b8d04c5
8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents:
20364
diff
changeset
|
158 assert(level != NMT_off || current_level == NMT_off, "Cannot transition NMT to off"); |
417e3b8d04c5
8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents:
20364
diff
changeset
|
159 |
20360 | 160 if (current_level == level) { |
161 return true; | |
162 } else if (current_level > level) { | |
20553
417e3b8d04c5
8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents:
20364
diff
changeset
|
163 // Downgrade tracking level, we want to lower the tracking level first |
20360 | 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); | |
6197 | 169 } else { |
20553
417e3b8d04c5
8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents:
20364
diff
changeset
|
170 // Upgrading tracking level is not supported and has never been supported. |
417e3b8d04c5
8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents:
20364
diff
changeset
|
171 // Allocating and deallocating malloc tracking structures is not thread safe and |
417e3b8d04c5
8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents:
20364
diff
changeset
|
172 // leads to inconsistencies unless a lot coarser locks are added. |
6197 | 173 } |
20360 | 174 return true; |
6197 | 175 } |
176 | |
20553
417e3b8d04c5
8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents:
20364
diff
changeset
|
177 void MemTracker::report(bool summary_only, outputStream* output) { |
417e3b8d04c5
8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents:
20364
diff
changeset
|
178 assert(output != NULL, "No output stream"); |
417e3b8d04c5
8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents:
20364
diff
changeset
|
179 MemBaseline baseline; |
417e3b8d04c5
8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents:
20364
diff
changeset
|
180 if (baseline.baseline(summary_only)) { |
417e3b8d04c5
8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents:
20364
diff
changeset
|
181 if (summary_only) { |
417e3b8d04c5
8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents:
20364
diff
changeset
|
182 MemSummaryReporter rpt(baseline, output); |
417e3b8d04c5
8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents:
20364
diff
changeset
|
183 rpt.report(); |
417e3b8d04c5
8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents:
20364
diff
changeset
|
184 } else { |
417e3b8d04c5
8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents:
20364
diff
changeset
|
185 MemDetailReporter rpt(baseline, output); |
417e3b8d04c5
8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents:
20364
diff
changeset
|
186 rpt.report(); |
417e3b8d04c5
8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents:
20364
diff
changeset
|
187 } |
6197 | 188 } |
189 } | |
190 | |
20360 | 191 // This is a walker to gather malloc site hashtable statistics, |
192 // the result is used for tuning. | |
193 class StatisticsWalker : public MallocSiteWalker { | |
194 private: | |
195 enum Threshold { | |
196 // aggregates statistics over this threshold into one | |
197 // line item. | |
198 report_threshold = 20 | |
199 }; | |
6197 | 200 |
20360 | 201 private: |
202 // Number of allocation sites that have all memory freed | |
203 int _empty_entries; | |
204 // Total number of allocation sites, include empty sites | |
205 int _total_entries; | |
206 // Number of captured call stack distribution | |
207 int _stack_depth_distribution[NMT_TrackingStackDepth]; | |
208 // Hash distribution | |
209 int _hash_distribution[report_threshold]; | |
210 // Number of hash buckets that have entries over the threshold | |
211 int _bucket_over_threshold; | |
6197 | 212 |
20360 | 213 // The hash bucket that walker is currently walking |
214 int _current_hash_bucket; | |
215 // The length of current hash bucket | |
216 int _current_bucket_length; | |
217 // Number of hash buckets that are not empty | |
218 int _used_buckets; | |
219 // Longest hash bucket length | |
220 int _longest_bucket_length; | |
6197 | 221 |
20360 | 222 public: |
223 StatisticsWalker() : _empty_entries(0), _total_entries(0) { | |
224 int index = 0; | |
225 for (index = 0; index < NMT_TrackingStackDepth; index ++) { | |
226 _stack_depth_distribution[index] = 0; | |
6197 | 227 } |
20360 | 228 for (index = 0; index < report_threshold; index ++) { |
229 _hash_distribution[index] = 0; | |
230 } | |
231 _bucket_over_threshold = 0; | |
232 _longest_bucket_length = 0; | |
233 _current_hash_bucket = -1; | |
234 _current_bucket_length = 0; | |
235 _used_buckets = 0; | |
6197 | 236 } |
237 | |
20360 | 238 virtual bool at(const MallocSite* e) { |
239 if (e->size() == 0) _empty_entries ++; | |
240 _total_entries ++; | |
241 | |
242 // stack depth distrubution | |
243 int frames = e->call_stack()->frames(); | |
244 _stack_depth_distribution[frames - 1] ++; | |
245 | |
246 // hash distribution | |
247 int hash_bucket = e->hash() % MallocSiteTable::hash_buckets(); | |
248 if (_current_hash_bucket == -1) { | |
249 _current_hash_bucket = hash_bucket; | |
250 _current_bucket_length = 1; | |
251 } else if (_current_hash_bucket == hash_bucket) { | |
252 _current_bucket_length ++; | |
253 } else { | |
254 record_bucket_length(_current_bucket_length); | |
255 _current_hash_bucket = hash_bucket; | |
256 _current_bucket_length = 1; | |
257 } | |
258 return true; | |
6197 | 259 } |
260 | |
20360 | 261 // walk completed |
262 void completed() { | |
263 record_bucket_length(_current_bucket_length); | |
264 } | |
6197 | 265 |
20360 | 266 void report_statistics(outputStream* out) { |
267 int index; | |
268 out->print_cr("Malloc allocation site table:"); | |
269 out->print_cr("\tTotal entries: %d", _total_entries); | |
270 out->print_cr("\tEmpty entries: %d (%2.2f%%)", _empty_entries, ((float)_empty_entries * 100) / _total_entries); | |
271 out->print_cr(" "); | |
272 out->print_cr("Hash distribution:"); | |
273 if (_used_buckets < MallocSiteTable::hash_buckets()) { | |
274 out->print_cr("empty bucket: %d", (MallocSiteTable::hash_buckets() - _used_buckets)); | |
275 } | |
276 for (index = 0; index < report_threshold; index ++) { | |
277 if (_hash_distribution[index] != 0) { | |
278 if (index == 0) { | |
279 out->print_cr(" %d entry: %d", 1, _hash_distribution[0]); | |
280 } else if (index < 9) { // single digit | |
281 out->print_cr(" %d entries: %d", (index + 1), _hash_distribution[index]); | |
282 } else { | |
283 out->print_cr(" %d entries: %d", (index + 1), _hash_distribution[index]); | |
284 } | |
6197 | 285 } |
286 } | |
20360 | 287 if (_bucket_over_threshold > 0) { |
288 out->print_cr(" >%d entries: %d", report_threshold, _bucket_over_threshold); | |
289 } | |
290 out->print_cr("most entries: %d", _longest_bucket_length); | |
291 out->print_cr(" "); | |
292 out->print_cr("Call stack depth distribution:"); | |
293 for (index = 0; index < NMT_TrackingStackDepth; index ++) { | |
294 if (_stack_depth_distribution[index] > 0) { | |
295 out->print_cr("\t%d: %d", index + 1, _stack_depth_distribution[index]); | |
6197 | 296 } |
297 } | |
298 } | |
299 | |
20360 | 300 private: |
301 void record_bucket_length(int length) { | |
302 _used_buckets ++; | |
303 if (length <= report_threshold) { | |
304 _hash_distribution[length - 1] ++; | |
305 } else { | |
306 _bucket_over_threshold ++; | |
6231 | 307 } |
20360 | 308 _longest_bucket_length = MAX2(_longest_bucket_length, length); |
6197 | 309 } |
20360 | 310 }; |
6197 | 311 |
10986
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
312 |
20360 | 313 void MemTracker::tuning_statistics(outputStream* out) { |
314 // NMT statistics | |
315 StatisticsWalker walker; | |
316 MallocSiteTable::walk_malloc_site(&walker); | |
317 walker.completed(); | |
10986
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
318 |
20360 | 319 out->print_cr("Native Memory Tracking Statistics:"); |
320 out->print_cr("Malloc allocation site table size: %d", MallocSiteTable::hash_buckets()); | |
321 out->print_cr(" Tracking stack depth: %d", NMT_TrackingStackDepth); | |
322 NOT_PRODUCT(out->print_cr("Peak concurrent access: %d", MallocSiteTable::access_peak_count());) | |
323 out->print_cr(" "); | |
324 walker.report_statistics(out); | |
10986
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
325 } |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
326 |