Mercurial > hg > graal-jvmci-8
annotate src/share/vm/services/memTracker.cpp @ 20197:ce8f6bb717c9
8042195: Introduce umbrella header orderAccess.inline.hpp.
Reviewed-by: dholmes, kvn, stefank, twisti
author | goetz |
---|---|
date | Tue, 29 Apr 2014 15:17:27 +0200 |
parents | 248c459b2b75 |
children | 833b0f92429a |
rev | line source |
---|---|
6197 | 1 /* |
9061
4c8bb5e4f68f
8011161: NMT: Memory leak when encountering out of memory error while initializing memory snapshot
zgu
parents:
8810
diff
changeset
|
2 * Copyright (c) 2012, 2013, 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 | |
7464
ecd24264898b
8005048: NMT: #loaded classes needs to just show the # defined classes
zgu
parents:
7172
diff
changeset
|
26 #include "oops/instanceKlass.hpp" |
6197 | 27 #include "runtime/atomic.hpp" |
28 #include "runtime/interfaceSupport.hpp" | |
29 #include "runtime/mutexLocker.hpp" | |
30 #include "runtime/safepoint.hpp" | |
31 #include "runtime/threadCritical.hpp" | |
20197
ce8f6bb717c9
8042195: Introduce umbrella header orderAccess.inline.hpp.
goetz
parents:
11152
diff
changeset
|
32 #include "runtime/thread.inline.hpp" |
7971
4102b59539ce
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
7464
diff
changeset
|
33 #include "runtime/vm_operations.hpp" |
6197 | 34 #include "services/memPtr.hpp" |
35 #include "services/memReporter.hpp" | |
36 #include "services/memTracker.hpp" | |
37 #include "utilities/decoder.hpp" | |
10355
fb14e9ed1594
8011064: Some tests have failed with SIGSEGV on arm-hflt on build b82
jprovino
parents:
10148
diff
changeset
|
38 #include "utilities/defaultStream.hpp" |
6197 | 39 #include "utilities/globalDefinitions.hpp" |
40 | |
41 bool NMT_track_callsite = false; | |
42 | |
43 // walk all 'known' threads at NMT sync point, and collect their recorders | |
44 void SyncThreadRecorderClosure::do_thread(Thread* thread) { | |
45 assert(SafepointSynchronize::is_at_safepoint(), "Safepoint required"); | |
46 if (thread->is_Java_thread()) { | |
47 JavaThread* javaThread = (JavaThread*)thread; | |
48 MemRecorder* recorder = javaThread->get_recorder(); | |
49 if (recorder != NULL) { | |
50 MemTracker::enqueue_pending_recorder(recorder); | |
51 javaThread->set_recorder(NULL); | |
52 } | |
53 } | |
54 _thread_count ++; | |
55 } | |
56 | |
57 | |
10096
35f8765422b9
8010151: nsk/regression/b6653214 fails "assert(snapshot != NULL) failed: Worker should not be started"
zgu
parents:
9061
diff
changeset
|
58 MemRecorder* volatile MemTracker::_global_recorder = NULL; |
6197 | 59 MemSnapshot* MemTracker::_snapshot = NULL; |
60 MemBaseline MemTracker::_baseline; | |
6232
f1f45dddb0bd
7181986: NMT ON: Assertion failure when running jdi ExpiredRequestDeletionTest
zgu
parents:
6197
diff
changeset
|
61 Mutex* MemTracker::_query_lock = NULL; |
10096
35f8765422b9
8010151: nsk/regression/b6653214 fails "assert(snapshot != NULL) failed: Worker should not be started"
zgu
parents:
9061
diff
changeset
|
62 MemRecorder* volatile MemTracker::_merge_pending_queue = NULL; |
35f8765422b9
8010151: nsk/regression/b6653214 fails "assert(snapshot != NULL) failed: Worker should not be started"
zgu
parents:
9061
diff
changeset
|
63 MemRecorder* volatile MemTracker::_pooled_recorders = NULL; |
6197 | 64 MemTrackWorker* MemTracker::_worker_thread = NULL; |
65 int MemTracker::_sync_point_skip_count = 0; | |
66 MemTracker::NMTLevel MemTracker::_tracking_level = MemTracker::NMT_off; | |
67 volatile MemTracker::NMTStates MemTracker::_state = NMT_uninited; | |
68 MemTracker::ShutdownReason MemTracker::_reason = NMT_shutdown_none; | |
69 int MemTracker::_thread_count = 255; | |
70 volatile jint MemTracker::_pooled_recorder_count = 0; | |
7971
4102b59539ce
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
7464
diff
changeset
|
71 volatile unsigned long MemTracker::_processing_generation = 0; |
4102b59539ce
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
7464
diff
changeset
|
72 volatile bool MemTracker::_worker_thread_idle = false; |
10986
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
73 volatile jint MemTracker::_pending_op_count = 0; |
8810
06db4c0afbf3
8009298: NMT: Special version of class loading/unloading with runThese stresses out NMT
zgu
parents:
7971
diff
changeset
|
74 volatile bool MemTracker::_slowdown_calling_thread = false; |
6197 | 75 debug_only(intx MemTracker::_main_thread_tid = 0;) |
6607
e5bf1c79ed5b
7191124: Optimized build is broken due to inconsistent use of DEBUG_ONLY and NOT_PRODUCT macros in NMT
zgu
parents:
6235
diff
changeset
|
76 NOT_PRODUCT(volatile jint MemTracker::_pending_recorder_count = 0;) |
6197 | 77 |
78 void MemTracker::init_tracking_options(const char* option_line) { | |
79 _tracking_level = NMT_off; | |
7172
bbc14465e7db
8003689: MemTracker::init_tracking_options() reads outside array if commandline argument is empty
zgu
parents:
6882
diff
changeset
|
80 if (strcmp(option_line, "=summary") == 0) { |
6197 | 81 _tracking_level = NMT_summary; |
7172
bbc14465e7db
8003689: MemTracker::init_tracking_options() reads outside array if commandline argument is empty
zgu
parents:
6882
diff
changeset
|
82 } else if (strcmp(option_line, "=detail") == 0) { |
10355
fb14e9ed1594
8011064: Some tests have failed with SIGSEGV on arm-hflt on build b82
jprovino
parents:
10148
diff
changeset
|
83 // detail relies on a stack-walking ability that may not |
fb14e9ed1594
8011064: Some tests have failed with SIGSEGV on arm-hflt on build b82
jprovino
parents:
10148
diff
changeset
|
84 // be available depending on platform and/or compiler flags |
11129 | 85 #if PLATFORM_NATIVE_STACK_WALKING_SUPPORTED |
10355
fb14e9ed1594
8011064: Some tests have failed with SIGSEGV on arm-hflt on build b82
jprovino
parents:
10148
diff
changeset
|
86 _tracking_level = NMT_detail; |
11129 | 87 #else |
10355
fb14e9ed1594
8011064: Some tests have failed with SIGSEGV on arm-hflt on build b82
jprovino
parents:
10148
diff
changeset
|
88 jio_fprintf(defaultStream::error_stream(), |
11129 | 89 "NMT detail is not supported on this platform. Using NMT summary instead.\n"); |
10355
fb14e9ed1594
8011064: Some tests have failed with SIGSEGV on arm-hflt on build b82
jprovino
parents:
10148
diff
changeset
|
90 _tracking_level = NMT_summary; |
11129 | 91 #endif |
7172
bbc14465e7db
8003689: MemTracker::init_tracking_options() reads outside array if commandline argument is empty
zgu
parents:
6882
diff
changeset
|
92 } else if (strcmp(option_line, "=off") != 0) { |
bbc14465e7db
8003689: MemTracker::init_tracking_options() reads outside array if commandline argument is empty
zgu
parents:
6882
diff
changeset
|
93 vm_exit_during_initialization("Syntax error, expecting -XX:NativeMemoryTracking=[off|summary|detail]", NULL); |
6197 | 94 } |
95 } | |
96 | |
97 // first phase of bootstrapping, when VM is still in single-threaded mode. | |
98 void MemTracker::bootstrap_single_thread() { | |
99 if (_tracking_level > NMT_off) { | |
100 assert(_state == NMT_uninited, "wrong state"); | |
101 | |
102 // NMT is not supported with UseMallocOnly is on. NMT can NOT | |
103 // handle the amount of malloc data without significantly impacting | |
104 // runtime performance when this flag is on. | |
105 if (UseMallocOnly) { | |
106 shutdown(NMT_use_malloc_only); | |
107 return; | |
108 } | |
109 | |
6232
f1f45dddb0bd
7181986: NMT ON: Assertion failure when running jdi ExpiredRequestDeletionTest
zgu
parents:
6197
diff
changeset
|
110 _query_lock = new (std::nothrow) Mutex(Monitor::max_nonleaf, "NMT_queryLock"); |
f1f45dddb0bd
7181986: NMT ON: Assertion failure when running jdi ExpiredRequestDeletionTest
zgu
parents:
6197
diff
changeset
|
111 if (_query_lock == NULL) { |
f1f45dddb0bd
7181986: NMT ON: Assertion failure when running jdi ExpiredRequestDeletionTest
zgu
parents:
6197
diff
changeset
|
112 shutdown(NMT_out_of_memory); |
f1f45dddb0bd
7181986: NMT ON: Assertion failure when running jdi ExpiredRequestDeletionTest
zgu
parents:
6197
diff
changeset
|
113 return; |
f1f45dddb0bd
7181986: NMT ON: Assertion failure when running jdi ExpiredRequestDeletionTest
zgu
parents:
6197
diff
changeset
|
114 } |
f1f45dddb0bd
7181986: NMT ON: Assertion failure when running jdi ExpiredRequestDeletionTest
zgu
parents:
6197
diff
changeset
|
115 |
6197 | 116 debug_only(_main_thread_tid = os::current_thread_id();) |
117 _state = NMT_bootstrapping_single_thread; | |
118 NMT_track_callsite = (_tracking_level == NMT_detail && can_walk_stack()); | |
119 } | |
120 } | |
121 | |
122 // second phase of bootstrapping, when VM is about to or already entered multi-theaded mode. | |
123 void MemTracker::bootstrap_multi_thread() { | |
124 if (_tracking_level > NMT_off && _state == NMT_bootstrapping_single_thread) { | |
125 // create nmt lock for multi-thread execution | |
126 assert(_main_thread_tid == os::current_thread_id(), "wrong thread"); | |
127 _state = NMT_bootstrapping_multi_thread; | |
128 NMT_track_callsite = (_tracking_level == NMT_detail && can_walk_stack()); | |
129 } | |
130 } | |
131 | |
132 // fully start nmt | |
133 void MemTracker::start() { | |
134 // Native memory tracking is off from command line option | |
135 if (_tracking_level == NMT_off || shutdown_in_progress()) return; | |
136 | |
137 assert(_main_thread_tid == os::current_thread_id(), "wrong thread"); | |
138 assert(_state == NMT_bootstrapping_multi_thread, "wrong state"); | |
139 | |
140 _snapshot = new (std::nothrow)MemSnapshot(); | |
9061
4c8bb5e4f68f
8011161: NMT: Memory leak when encountering out of memory error while initializing memory snapshot
zgu
parents:
8810
diff
changeset
|
141 if (_snapshot != NULL) { |
10096
35f8765422b9
8010151: nsk/regression/b6653214 fails "assert(snapshot != NULL) failed: Worker should not be started"
zgu
parents:
9061
diff
changeset
|
142 if (!_snapshot->out_of_memory() && start_worker(_snapshot)) { |
6197 | 143 _state = NMT_started; |
144 NMT_track_callsite = (_tracking_level == NMT_detail && can_walk_stack()); | |
145 return; | |
146 } | |
9061
4c8bb5e4f68f
8011161: NMT: Memory leak when encountering out of memory error while initializing memory snapshot
zgu
parents:
8810
diff
changeset
|
147 |
4c8bb5e4f68f
8011161: NMT: Memory leak when encountering out of memory error while initializing memory snapshot
zgu
parents:
8810
diff
changeset
|
148 delete _snapshot; |
4c8bb5e4f68f
8011161: NMT: Memory leak when encountering out of memory error while initializing memory snapshot
zgu
parents:
8810
diff
changeset
|
149 _snapshot = NULL; |
6197 | 150 } |
151 | |
152 // fail to start native memory tracking, shut it down | |
153 shutdown(NMT_initialization); | |
154 } | |
155 | |
156 /** | |
157 * Shutting down native memory tracking. | |
158 * We can not shutdown native memory tracking immediately, so we just | |
159 * setup shutdown pending flag, every native memory tracking component | |
160 * should orderly shut itself down. | |
161 * | |
162 * The shutdown sequences: | |
163 * 1. MemTracker::shutdown() sets MemTracker to shutdown pending state | |
164 * 2. Worker thread calls MemTracker::final_shutdown(), which transites | |
165 * MemTracker to final shutdown state. | |
166 * 3. At sync point, MemTracker does final cleanup, before sets memory | |
167 * tracking level to off to complete shutdown. | |
168 */ | |
169 void MemTracker::shutdown(ShutdownReason reason) { | |
170 if (_tracking_level == NMT_off) return; | |
171 | |
172 if (_state <= NMT_bootstrapping_single_thread) { | |
173 // we still in single thread mode, there is not contention | |
174 _state = NMT_shutdown_pending; | |
175 _reason = reason; | |
176 } else { | |
177 // we want to know who initialized shutdown | |
178 if ((jint)NMT_started == Atomic::cmpxchg((jint)NMT_shutdown_pending, | |
179 (jint*)&_state, (jint)NMT_started)) { | |
180 _reason = reason; | |
181 } | |
182 } | |
183 } | |
184 | |
185 // final phase of shutdown | |
186 void MemTracker::final_shutdown() { | |
187 // delete all pending recorders and pooled recorders | |
188 delete_all_pending_recorders(); | |
189 delete_all_pooled_recorders(); | |
190 | |
191 { | |
192 // shared baseline and snapshot are the only objects needed to | |
193 // create query results | |
6232
f1f45dddb0bd
7181986: NMT ON: Assertion failure when running jdi ExpiredRequestDeletionTest
zgu
parents:
6197
diff
changeset
|
194 MutexLockerEx locker(_query_lock, true); |
6197 | 195 // cleanup baseline data and snapshot |
196 _baseline.clear(); | |
197 delete _snapshot; | |
198 _snapshot = NULL; | |
199 } | |
200 | |
201 // shutdown shared decoder instance, since it is only | |
202 // used by native memory tracking so far. | |
203 Decoder::shutdown(); | |
204 | |
205 MemTrackWorker* worker = NULL; | |
206 { | |
207 ThreadCritical tc; | |
208 // can not delete worker inside the thread critical | |
209 if (_worker_thread != NULL && Thread::current() == _worker_thread) { | |
210 worker = _worker_thread; | |
211 _worker_thread = NULL; | |
212 } | |
213 } | |
214 if (worker != NULL) { | |
215 delete worker; | |
216 } | |
217 _state = NMT_final_shutdown; | |
218 } | |
219 | |
220 // delete all pooled recorders | |
221 void MemTracker::delete_all_pooled_recorders() { | |
222 // free all pooled recorders | |
10096
35f8765422b9
8010151: nsk/regression/b6653214 fails "assert(snapshot != NULL) failed: Worker should not be started"
zgu
parents:
9061
diff
changeset
|
223 MemRecorder* volatile cur_head = _pooled_recorders; |
6197 | 224 if (cur_head != NULL) { |
225 MemRecorder* null_ptr = NULL; | |
226 while (cur_head != NULL && (void*)cur_head != Atomic::cmpxchg_ptr((void*)null_ptr, | |
227 (void*)&_pooled_recorders, (void*)cur_head)) { | |
228 cur_head = _pooled_recorders; | |
229 } | |
230 if (cur_head != NULL) { | |
231 delete cur_head; | |
232 _pooled_recorder_count = 0; | |
233 } | |
234 } | |
235 } | |
236 | |
237 // delete all recorders in pending queue | |
238 void MemTracker::delete_all_pending_recorders() { | |
239 // free all pending recorders | |
240 MemRecorder* pending_head = get_pending_recorders(); | |
241 if (pending_head != NULL) { | |
242 delete pending_head; | |
243 } | |
244 } | |
245 | |
246 /* | |
247 * retrieve per-thread recorder of specified thread. | |
248 * if thread == NULL, it means global recorder | |
249 */ | |
250 MemRecorder* MemTracker::get_thread_recorder(JavaThread* thread) { | |
251 if (shutdown_in_progress()) return NULL; | |
252 | |
253 MemRecorder* rc; | |
254 if (thread == NULL) { | |
255 rc = _global_recorder; | |
256 } else { | |
257 rc = thread->get_recorder(); | |
258 } | |
259 | |
260 if (rc != NULL && rc->is_full()) { | |
261 enqueue_pending_recorder(rc); | |
262 rc = NULL; | |
263 } | |
264 | |
265 if (rc == NULL) { | |
266 rc = get_new_or_pooled_instance(); | |
267 if (thread == NULL) { | |
268 _global_recorder = rc; | |
269 } else { | |
270 thread->set_recorder(rc); | |
271 } | |
272 } | |
273 return rc; | |
274 } | |
275 | |
276 /* | |
277 * get a per-thread recorder from pool, or create a new one if | |
278 * there is not one available. | |
279 */ | |
280 MemRecorder* MemTracker::get_new_or_pooled_instance() { | |
281 MemRecorder* cur_head = const_cast<MemRecorder*> (_pooled_recorders); | |
282 if (cur_head == NULL) { | |
283 MemRecorder* rec = new (std::nothrow)MemRecorder(); | |
284 if (rec == NULL || rec->out_of_memory()) { | |
285 shutdown(NMT_out_of_memory); | |
286 if (rec != NULL) { | |
287 delete rec; | |
288 rec = NULL; | |
289 } | |
290 } | |
291 return rec; | |
292 } else { | |
293 MemRecorder* next_head = cur_head->next(); | |
294 if ((void*)cur_head != Atomic::cmpxchg_ptr((void*)next_head, (void*)&_pooled_recorders, | |
295 (void*)cur_head)) { | |
296 return get_new_or_pooled_instance(); | |
297 } | |
298 cur_head->set_next(NULL); | |
299 Atomic::dec(&_pooled_recorder_count); | |
7971
4102b59539ce
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
7464
diff
changeset
|
300 cur_head->set_generation(); |
6197 | 301 return cur_head; |
302 } | |
303 } | |
304 | |
305 /* | |
306 * retrieve all recorders in pending queue, and empty the queue | |
307 */ | |
308 MemRecorder* MemTracker::get_pending_recorders() { | |
309 MemRecorder* cur_head = const_cast<MemRecorder*>(_merge_pending_queue); | |
310 MemRecorder* null_ptr = NULL; | |
311 while ((void*)cur_head != Atomic::cmpxchg_ptr((void*)null_ptr, (void*)&_merge_pending_queue, | |
312 (void*)cur_head)) { | |
313 cur_head = const_cast<MemRecorder*>(_merge_pending_queue); | |
314 } | |
6607
e5bf1c79ed5b
7191124: Optimized build is broken due to inconsistent use of DEBUG_ONLY and NOT_PRODUCT macros in NMT
zgu
parents:
6235
diff
changeset
|
315 NOT_PRODUCT(Atomic::store(0, &_pending_recorder_count)); |
6197 | 316 return cur_head; |
317 } | |
318 | |
319 /* | |
320 * release a recorder to recorder pool. | |
321 */ | |
322 void MemTracker::release_thread_recorder(MemRecorder* rec) { | |
323 assert(rec != NULL, "null recorder"); | |
324 // we don't want to pool too many recorders | |
325 rec->set_next(NULL); | |
326 if (shutdown_in_progress() || _pooled_recorder_count > _thread_count * 2) { | |
327 delete rec; | |
328 return; | |
329 } | |
330 | |
331 rec->clear(); | |
332 MemRecorder* cur_head = const_cast<MemRecorder*>(_pooled_recorders); | |
333 rec->set_next(cur_head); | |
334 while ((void*)cur_head != Atomic::cmpxchg_ptr((void*)rec, (void*)&_pooled_recorders, | |
335 (void*)cur_head)) { | |
336 cur_head = const_cast<MemRecorder*>(_pooled_recorders); | |
337 rec->set_next(cur_head); | |
338 } | |
339 Atomic::inc(&_pooled_recorder_count); | |
340 } | |
341 | |
342 // write a record to proper recorder. No lock can be taken from this method | |
343 // down. | |
10986
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
344 void MemTracker::write_tracking_record(address addr, MEMFLAGS flags, |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
345 size_t size, jint seq, address pc, JavaThread* thread) { |
6197 | 346 |
6231 | 347 MemRecorder* rc = get_thread_recorder(thread); |
6197 | 348 if (rc != NULL) { |
10986
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
349 rc->record(addr, flags, size, seq, pc); |
6197 | 350 } |
351 } | |
352 | |
353 /** | |
354 * enqueue a recorder to pending queue | |
355 */ | |
356 void MemTracker::enqueue_pending_recorder(MemRecorder* rec) { | |
357 assert(rec != NULL, "null recorder"); | |
358 | |
359 // we are shutting down, so just delete it | |
360 if (shutdown_in_progress()) { | |
361 rec->set_next(NULL); | |
362 delete rec; | |
363 return; | |
364 } | |
365 | |
366 MemRecorder* cur_head = const_cast<MemRecorder*>(_merge_pending_queue); | |
367 rec->set_next(cur_head); | |
368 while ((void*)cur_head != Atomic::cmpxchg_ptr((void*)rec, (void*)&_merge_pending_queue, | |
369 (void*)cur_head)) { | |
370 cur_head = const_cast<MemRecorder*>(_merge_pending_queue); | |
371 rec->set_next(cur_head); | |
372 } | |
6607
e5bf1c79ed5b
7191124: Optimized build is broken due to inconsistent use of DEBUG_ONLY and NOT_PRODUCT macros in NMT
zgu
parents:
6235
diff
changeset
|
373 NOT_PRODUCT(Atomic::inc(&_pending_recorder_count);) |
6197 | 374 } |
375 | |
376 /* | |
377 * The method is called at global safepoint | |
378 * during it synchronization process. | |
379 * 1. enqueue all JavaThreads' per-thread recorders | |
380 * 2. enqueue global recorder | |
381 * 3. retrieve all pending recorders | |
382 * 4. reset global sequence number generator | |
383 * 5. call worker's sync | |
384 */ | |
385 #define MAX_SAFEPOINTS_TO_SKIP 128 | |
386 #define SAFE_SEQUENCE_THRESHOLD 30 | |
387 #define HIGH_GENERATION_THRESHOLD 60 | |
8810
06db4c0afbf3
8009298: NMT: Special version of class loading/unloading with runThese stresses out NMT
zgu
parents:
7971
diff
changeset
|
388 #define MAX_RECORDER_THREAD_RATIO 30 |
11137
c9a5fab39234
8012241: NMT huge memory footprint, it usually leads to OOME
zgu
parents:
10986
diff
changeset
|
389 #define MAX_RECORDER_PER_THREAD 100 |
6197 | 390 |
391 void MemTracker::sync() { | |
392 assert(_tracking_level > NMT_off, "NMT is not enabled"); | |
393 assert(SafepointSynchronize::is_at_safepoint(), "Safepoint required"); | |
394 | |
395 // Some GC tests hit large number of safepoints in short period of time | |
396 // without meaningful activities. We should prevent going to | |
397 // sync point in these cases, which can potentially exhaust generation buffer. | |
398 // Here is the factots to determine if we should go into sync point: | |
399 // 1. not to overflow sequence number | |
400 // 2. if we are in danger to overflow generation buffer | |
401 // 3. how many safepoints we already skipped sync point | |
402 if (_state == NMT_started) { | |
403 // worker thread is not ready, no one can manage generation | |
404 // buffer, so skip this safepoint | |
405 if (_worker_thread == NULL) return; | |
406 | |
407 if (_sync_point_skip_count < MAX_SAFEPOINTS_TO_SKIP) { | |
408 int per_seq_in_use = SequenceGenerator::peek() * 100 / max_jint; | |
409 int per_gen_in_use = _worker_thread->generations_in_use() * 100 / MAX_GENERATIONS; | |
410 if (per_seq_in_use < SAFE_SEQUENCE_THRESHOLD && per_gen_in_use >= HIGH_GENERATION_THRESHOLD) { | |
411 _sync_point_skip_count ++; | |
412 return; | |
413 } | |
414 } | |
415 { | |
416 // This method is running at safepoint, with ThreadCritical lock, | |
417 // it should guarantee that NMT is fully sync-ed. | |
418 ThreadCritical tc; | |
6231 | 419 |
10986
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
420 // We can NOT execute NMT sync-point if there are pending tracking ops. |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
421 if (_pending_op_count == 0) { |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
422 SequenceGenerator::reset(); |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
423 _sync_point_skip_count = 0; |
6882
716c64bda5ba
7199092: NMT: NMT needs to deal overlapped virtual memory ranges
zgu
parents:
6781
diff
changeset
|
424 |
10986
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
425 // walk all JavaThreads to collect recorders |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
426 SyncThreadRecorderClosure stc; |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
427 Threads::threads_do(&stc); |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
428 |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
429 _thread_count = stc.get_thread_count(); |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
430 MemRecorder* pending_recorders = get_pending_recorders(); |
6231 | 431 |
10986
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
432 if (_global_recorder != NULL) { |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
433 _global_recorder->set_next(pending_recorders); |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
434 pending_recorders = _global_recorder; |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
435 _global_recorder = NULL; |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
436 } |
6231 | 437 |
10986
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
438 // see if NMT has too many outstanding recorder instances, it usually |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
439 // means that worker thread is lagging behind in processing them. |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
440 if (!AutoShutdownNMT) { |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
441 _slowdown_calling_thread = (MemRecorder::_instance_count > MAX_RECORDER_THREAD_RATIO * _thread_count); |
11137
c9a5fab39234
8012241: NMT huge memory footprint, it usually leads to OOME
zgu
parents:
10986
diff
changeset
|
442 } else { |
c9a5fab39234
8012241: NMT huge memory footprint, it usually leads to OOME
zgu
parents:
10986
diff
changeset
|
443 // If auto shutdown is on, enforce MAX_RECORDER_PER_THREAD threshold to prevent OOM |
c9a5fab39234
8012241: NMT huge memory footprint, it usually leads to OOME
zgu
parents:
10986
diff
changeset
|
444 if (MemRecorder::_instance_count >= _thread_count * MAX_RECORDER_PER_THREAD) { |
c9a5fab39234
8012241: NMT huge memory footprint, it usually leads to OOME
zgu
parents:
10986
diff
changeset
|
445 shutdown(NMT_out_of_memory); |
c9a5fab39234
8012241: NMT huge memory footprint, it usually leads to OOME
zgu
parents:
10986
diff
changeset
|
446 } |
10986
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
447 } |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
448 |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
449 // check _worker_thread with lock to avoid racing condition |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
450 if (_worker_thread != NULL) { |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
451 _worker_thread->at_sync_point(pending_recorders, InstanceKlass::number_of_instance_classes()); |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
452 } |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
453 assert(SequenceGenerator::peek() == 1, "Should not have memory activities during sync-point"); |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
454 } else { |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
455 _sync_point_skip_count ++; |
6197 | 456 } |
457 } | |
458 } | |
459 | |
460 // now, it is the time to shut whole things off | |
461 if (_state == NMT_final_shutdown) { | |
462 // walk all JavaThreads to delete all recorders | |
463 SyncThreadRecorderClosure stc; | |
464 Threads::threads_do(&stc); | |
465 // delete global recorder | |
466 { | |
467 ThreadCritical tc; | |
468 if (_global_recorder != NULL) { | |
469 delete _global_recorder; | |
470 _global_recorder = NULL; | |
471 } | |
472 } | |
6231 | 473 MemRecorder* pending_recorders = get_pending_recorders(); |
474 if (pending_recorders != NULL) { | |
475 delete pending_recorders; | |
476 } | |
477 // try at a later sync point to ensure MemRecorder instance drops to zero to | |
478 // completely shutdown NMT | |
479 if (MemRecorder::_instance_count == 0) { | |
480 _state = NMT_shutdown; | |
481 _tracking_level = NMT_off; | |
482 } | |
6197 | 483 } |
484 } | |
485 | |
486 /* | |
487 * Start worker thread. | |
488 */ | |
10096
35f8765422b9
8010151: nsk/regression/b6653214 fails "assert(snapshot != NULL) failed: Worker should not be started"
zgu
parents:
9061
diff
changeset
|
489 bool MemTracker::start_worker(MemSnapshot* snapshot) { |
35f8765422b9
8010151: nsk/regression/b6653214 fails "assert(snapshot != NULL) failed: Worker should not be started"
zgu
parents:
9061
diff
changeset
|
490 assert(_worker_thread == NULL && _snapshot != NULL, "Just Check"); |
35f8765422b9
8010151: nsk/regression/b6653214 fails "assert(snapshot != NULL) failed: Worker should not be started"
zgu
parents:
9061
diff
changeset
|
491 _worker_thread = new (std::nothrow) MemTrackWorker(snapshot); |
35f8765422b9
8010151: nsk/regression/b6653214 fails "assert(snapshot != NULL) failed: Worker should not be started"
zgu
parents:
9061
diff
changeset
|
492 if (_worker_thread == NULL) { |
35f8765422b9
8010151: nsk/regression/b6653214 fails "assert(snapshot != NULL) failed: Worker should not be started"
zgu
parents:
9061
diff
changeset
|
493 return false; |
35f8765422b9
8010151: nsk/regression/b6653214 fails "assert(snapshot != NULL) failed: Worker should not be started"
zgu
parents:
9061
diff
changeset
|
494 } else if (_worker_thread->has_error()) { |
35f8765422b9
8010151: nsk/regression/b6653214 fails "assert(snapshot != NULL) failed: Worker should not be started"
zgu
parents:
9061
diff
changeset
|
495 delete _worker_thread; |
35f8765422b9
8010151: nsk/regression/b6653214 fails "assert(snapshot != NULL) failed: Worker should not be started"
zgu
parents:
9061
diff
changeset
|
496 _worker_thread = NULL; |
6197 | 497 return false; |
498 } | |
499 _worker_thread->start(); | |
500 return true; | |
501 } | |
502 | |
503 /* | |
504 * We need to collect a JavaThread's per-thread recorder | |
505 * before it exits. | |
506 */ | |
507 void MemTracker::thread_exiting(JavaThread* thread) { | |
508 if (is_on()) { | |
509 MemRecorder* rec = thread->get_recorder(); | |
510 if (rec != NULL) { | |
511 enqueue_pending_recorder(rec); | |
512 thread->set_recorder(NULL); | |
513 } | |
514 } | |
515 } | |
516 | |
517 // baseline current memory snapshot | |
518 bool MemTracker::baseline() { | |
10148 | 519 MutexLocker lock(_query_lock); |
6197 | 520 MemSnapshot* snapshot = get_snapshot(); |
521 if (snapshot != NULL) { | |
522 return _baseline.baseline(*snapshot, false); | |
523 } | |
524 return false; | |
525 } | |
526 | |
527 // print memory usage from current snapshot | |
528 bool MemTracker::print_memory_usage(BaselineOutputer& out, size_t unit, bool summary_only) { | |
529 MemBaseline baseline; | |
10148 | 530 MutexLocker lock(_query_lock); |
6197 | 531 MemSnapshot* snapshot = get_snapshot(); |
532 if (snapshot != NULL && baseline.baseline(*snapshot, summary_only)) { | |
533 BaselineReporter reporter(out, unit); | |
534 reporter.report_baseline(baseline, summary_only); | |
535 return true; | |
536 } | |
537 return false; | |
538 } | |
539 | |
7971
4102b59539ce
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
7464
diff
changeset
|
540 // Whitebox API for blocking until the current generation of NMT data has been merged |
4102b59539ce
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
7464
diff
changeset
|
541 bool MemTracker::wbtest_wait_for_data_merge() { |
4102b59539ce
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
7464
diff
changeset
|
542 // NMT can't be shutdown while we're holding _query_lock |
10148 | 543 MutexLocker lock(_query_lock); |
7971
4102b59539ce
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
7464
diff
changeset
|
544 assert(_worker_thread != NULL, "Invalid query"); |
4102b59539ce
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
7464
diff
changeset
|
545 // the generation at query time, so NMT will spin till this generation is processed |
4102b59539ce
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
7464
diff
changeset
|
546 unsigned long generation_at_query_time = SequenceGenerator::current_generation(); |
4102b59539ce
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
7464
diff
changeset
|
547 unsigned long current_processing_generation = _processing_generation; |
4102b59539ce
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
7464
diff
changeset
|
548 // if generation counter overflown |
4102b59539ce
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
7464
diff
changeset
|
549 bool generation_overflown = (generation_at_query_time < current_processing_generation); |
4102b59539ce
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
7464
diff
changeset
|
550 long generations_to_wrap = MAX_UNSIGNED_LONG - current_processing_generation; |
4102b59539ce
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
7464
diff
changeset
|
551 // spin |
4102b59539ce
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
7464
diff
changeset
|
552 while (!shutdown_in_progress()) { |
4102b59539ce
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
7464
diff
changeset
|
553 if (!generation_overflown) { |
4102b59539ce
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
7464
diff
changeset
|
554 if (current_processing_generation > generation_at_query_time) { |
4102b59539ce
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
7464
diff
changeset
|
555 return true; |
4102b59539ce
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
7464
diff
changeset
|
556 } |
4102b59539ce
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
7464
diff
changeset
|
557 } else { |
4102b59539ce
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
7464
diff
changeset
|
558 assert(generations_to_wrap >= 0, "Sanity check"); |
4102b59539ce
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
7464
diff
changeset
|
559 long current_generations_to_wrap = MAX_UNSIGNED_LONG - current_processing_generation; |
4102b59539ce
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
7464
diff
changeset
|
560 assert(current_generations_to_wrap >= 0, "Sanity check"); |
4102b59539ce
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
7464
diff
changeset
|
561 // to overflow an unsigned long should take long time, so to_wrap check should be sufficient |
4102b59539ce
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
7464
diff
changeset
|
562 if (current_generations_to_wrap > generations_to_wrap && |
4102b59539ce
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
7464
diff
changeset
|
563 current_processing_generation > generation_at_query_time) { |
4102b59539ce
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
7464
diff
changeset
|
564 return true; |
4102b59539ce
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
7464
diff
changeset
|
565 } |
4102b59539ce
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
7464
diff
changeset
|
566 } |
4102b59539ce
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
7464
diff
changeset
|
567 |
4102b59539ce
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
7464
diff
changeset
|
568 // if worker thread is idle, but generation is not advancing, that means |
4102b59539ce
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
7464
diff
changeset
|
569 // there is not safepoint to let NMT advance generation, force one. |
4102b59539ce
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
7464
diff
changeset
|
570 if (_worker_thread_idle) { |
4102b59539ce
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
7464
diff
changeset
|
571 VM_ForceSafepoint vfs; |
4102b59539ce
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
7464
diff
changeset
|
572 VMThread::execute(&vfs); |
4102b59539ce
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
7464
diff
changeset
|
573 } |
4102b59539ce
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
7464
diff
changeset
|
574 MemSnapshot* snapshot = get_snapshot(); |
4102b59539ce
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
7464
diff
changeset
|
575 if (snapshot == NULL) { |
4102b59539ce
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
7464
diff
changeset
|
576 return false; |
4102b59539ce
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
7464
diff
changeset
|
577 } |
4102b59539ce
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
7464
diff
changeset
|
578 snapshot->wait(1000); |
4102b59539ce
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
7464
diff
changeset
|
579 current_processing_generation = _processing_generation; |
4102b59539ce
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
7464
diff
changeset
|
580 } |
4102b59539ce
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
7464
diff
changeset
|
581 // We end up here if NMT is shutting down before our data has been merged |
4102b59539ce
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
7464
diff
changeset
|
582 return false; |
4102b59539ce
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
7464
diff
changeset
|
583 } |
4102b59539ce
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
7464
diff
changeset
|
584 |
6197 | 585 // compare memory usage between current snapshot and baseline |
586 bool MemTracker::compare_memory_usage(BaselineOutputer& out, size_t unit, bool summary_only) { | |
10148 | 587 MutexLocker lock(_query_lock); |
6197 | 588 if (_baseline.baselined()) { |
589 MemBaseline baseline; | |
590 MemSnapshot* snapshot = get_snapshot(); | |
591 if (snapshot != NULL && baseline.baseline(*snapshot, summary_only)) { | |
592 BaselineReporter reporter(out, unit); | |
593 reporter.diff_baselines(baseline, _baseline, summary_only); | |
594 return true; | |
595 } | |
596 } | |
597 return false; | |
598 } | |
599 | |
600 #ifndef PRODUCT | |
601 void MemTracker::walk_stack(int toSkip, char* buf, int len) { | |
602 int cur_len = 0; | |
603 char tmp[1024]; | |
604 address pc; | |
605 | |
606 while (cur_len < len) { | |
607 pc = os::get_caller_pc(toSkip + 1); | |
608 if (pc != NULL && os::dll_address_to_function_name(pc, tmp, sizeof(tmp), NULL)) { | |
609 jio_snprintf(&buf[cur_len], (len - cur_len), "%s\n", tmp); | |
610 cur_len = (int)strlen(buf); | |
611 } else { | |
612 buf[cur_len] = '\0'; | |
613 break; | |
614 } | |
615 toSkip ++; | |
616 } | |
617 } | |
618 | |
619 void MemTracker::print_tracker_stats(outputStream* st) { | |
620 st->print_cr("\nMemory Tracker Stats:"); | |
621 st->print_cr("\tMax sequence number = %d", SequenceGenerator::max_seq_num()); | |
622 st->print_cr("\tthead count = %d", _thread_count); | |
623 st->print_cr("\tArena instance = %d", Arena::_instance_count); | |
624 st->print_cr("\tpooled recorder count = %d", _pooled_recorder_count); | |
625 st->print_cr("\tqueued recorder count = %d", _pending_recorder_count); | |
626 st->print_cr("\tmemory recorder instance count = %d", MemRecorder::_instance_count); | |
627 if (_worker_thread != NULL) { | |
628 st->print_cr("\tWorker thread:"); | |
629 st->print_cr("\t\tSync point count = %d", _worker_thread->_sync_point_count); | |
630 st->print_cr("\t\tpending recorder count = %d", _worker_thread->count_pending_recorders()); | |
631 st->print_cr("\t\tmerge count = %d", _worker_thread->_merge_count); | |
632 } else { | |
633 st->print_cr("\tWorker thread is not started"); | |
634 } | |
635 st->print_cr(" "); | |
636 | |
637 if (_snapshot != NULL) { | |
638 _snapshot->print_snapshot_stats(st); | |
639 } else { | |
640 st->print_cr("No snapshot"); | |
641 } | |
642 } | |
643 #endif | |
644 | |
10986
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
645 |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
646 // Tracker Implementation |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
647 |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
648 /* |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
649 * Create a tracker. |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
650 * This is a fairly complicated constructor, as it has to make two important decisions: |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
651 * 1) Does it need to take ThreadCritical lock to write tracking record |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
652 * 2) Does it need to pre-reserve a sequence number for the tracking record |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
653 * |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
654 * The rules to determine if ThreadCritical is needed: |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
655 * 1. When nmt is in single-threaded bootstrapping mode, no lock is needed as VM |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
656 * still in single thread mode. |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
657 * 2. For all threads other than JavaThread, ThreadCritical is needed |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
658 * to write to recorders to global recorder. |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
659 * 3. For JavaThreads that are no longer visible by safepoint, also |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
660 * need to take ThreadCritical and records are written to global |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
661 * recorders, since these threads are NOT walked by Threads.do_thread(). |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
662 * 4. JavaThreads that are running in safepoint-safe states do not stop |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
663 * for safepoints, ThreadCritical lock should be taken to write |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
664 * memory records. |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
665 * 5. JavaThreads that are running in VM state do not need any lock and |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
666 * records are written to per-thread recorders. |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
667 * 6. For a thread has yet to attach VM 'Thread', they need to take |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
668 * ThreadCritical to write to global recorder. |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
669 * |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
670 * The memory operations that need pre-reserve sequence numbers: |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
671 * The memory operations that "release" memory blocks and the |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
672 * operations can fail, need to pre-reserve sequence number. They |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
673 * are realloc, uncommit and release. |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
674 * |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
675 * The reason for pre-reserve sequence number, is to prevent race condition: |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
676 * Thread 1 Thread 2 |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
677 * <release> |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
678 * <allocate> |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
679 * <write allocate record> |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
680 * <write release record> |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
681 * if Thread 2 happens to obtain the memory address Thread 1 just released, |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
682 * then NMT can mistakenly report the memory is free. |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
683 * |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
684 * Noticeably, free() does not need pre-reserve sequence number, because the call |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
685 * does not fail, so we can alway write "release" record before the memory is actaully |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
686 * freed. |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
687 * |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
688 * For realloc, uncommit and release, following coding pattern should be used: |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
689 * |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
690 * MemTracker::Tracker tkr = MemTracker::get_realloc_tracker(); |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
691 * ptr = ::realloc(...); |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
692 * if (ptr == NULL) { |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
693 * tkr.record(...) |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
694 * } else { |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
695 * tkr.discard(); |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
696 * } |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
697 * |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
698 * MemTracker::Tracker tkr = MemTracker::get_virtual_memory_uncommit_tracker(); |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
699 * if (uncommit(...)) { |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
700 * tkr.record(...); |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
701 * } else { |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
702 * tkr.discard(); |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
703 * } |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
704 * |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
705 * MemTracker::Tracker tkr = MemTracker::get_virtual_memory_release_tracker(); |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
706 * if (release(...)) { |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
707 * tkr.record(...); |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
708 * } else { |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
709 * tkr.discard(); |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
710 * } |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
711 * |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
712 * Since pre-reserved sequence number is only good for the generation that it is acquired, |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
713 * when there is pending Tracker that reserved sequence number, NMT sync-point has |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
714 * to be skipped to prevent from advancing generation. This is done by inc and dec |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
715 * MemTracker::_pending_op_count, when MemTracker::_pending_op_count > 0, NMT sync-point is skipped. |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
716 * Not all pre-reservation of sequence number will increment pending op count. For JavaThreads |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
717 * that honor safepoints, safepoint can not occur during the memory operations, so the |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
718 * pre-reserved sequence number won't cross the generation boundry. |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
719 */ |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
720 MemTracker::Tracker::Tracker(MemoryOperation op, Thread* thr) { |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
721 _op = NoOp; |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
722 _seq = 0; |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
723 if (MemTracker::is_on()) { |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
724 _java_thread = NULL; |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
725 _op = op; |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
726 |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
727 // figure out if ThreadCritical lock is needed to write this operation |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
728 // to MemTracker |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
729 if (MemTracker::is_single_threaded_bootstrap()) { |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
730 thr = NULL; |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
731 } else if (thr == NULL) { |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
732 // don't use Thread::current(), since it is possible that |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
733 // the calling thread has yet to attach to VM 'Thread', |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
734 // which will result assertion failure |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
735 thr = ThreadLocalStorage::thread(); |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
736 } |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
737 |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
738 if (thr != NULL) { |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
739 // Check NMT load |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
740 MemTracker::check_NMT_load(thr); |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
741 |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
742 if (thr->is_Java_thread() && ((JavaThread*)thr)->is_safepoint_visible()) { |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
743 _java_thread = (JavaThread*)thr; |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
744 JavaThreadState state = _java_thread->thread_state(); |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
745 // JavaThreads that are safepoint safe, can run through safepoint, |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
746 // so ThreadCritical is needed to ensure no threads at safepoint create |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
747 // new records while the records are being gathered and the sequence number is changing |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
748 _need_thread_critical_lock = |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
749 SafepointSynchronize::safepoint_safe(_java_thread, state); |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
750 } else { |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
751 _need_thread_critical_lock = true; |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
752 } |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
753 } else { |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
754 _need_thread_critical_lock |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
755 = !MemTracker::is_single_threaded_bootstrap(); |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
756 } |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
757 |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
758 // see if we need to pre-reserve sequence number for this operation |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
759 if (_op == Realloc || _op == Uncommit || _op == Release) { |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
760 if (_need_thread_critical_lock) { |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
761 ThreadCritical tc; |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
762 MemTracker::inc_pending_op_count(); |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
763 _seq = SequenceGenerator::next(); |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
764 } else { |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
765 // for the threads that honor safepoints, no safepoint can occur |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
766 // during the lifespan of tracker, so we don't need to increase |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
767 // pending op count. |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
768 _seq = SequenceGenerator::next(); |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
769 } |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
770 } |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
771 } |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
772 } |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
773 |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
774 void MemTracker::Tracker::discard() { |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
775 if (MemTracker::is_on() && _seq != 0) { |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
776 if (_need_thread_critical_lock) { |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
777 ThreadCritical tc; |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
778 MemTracker::dec_pending_op_count(); |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
779 } |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
780 _seq = 0; |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
781 } |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
782 } |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
783 |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
784 |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
785 void MemTracker::Tracker::record(address old_addr, address new_addr, size_t size, |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
786 MEMFLAGS flags, address pc) { |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
787 assert(old_addr != NULL && new_addr != NULL, "Sanity check"); |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
788 assert(_op == Realloc || _op == NoOp, "Wrong call"); |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
789 if (MemTracker::is_on() && NMT_CAN_TRACK(flags) && _op != NoOp) { |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
790 assert(_seq > 0, "Need pre-reserve sequence number"); |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
791 if (_need_thread_critical_lock) { |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
792 ThreadCritical tc; |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
793 // free old address, use pre-reserved sequence number |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
794 MemTracker::write_tracking_record(old_addr, MemPointerRecord::free_tag(), |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
795 0, _seq, pc, _java_thread); |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
796 MemTracker::write_tracking_record(new_addr, flags | MemPointerRecord::malloc_tag(), |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
797 size, SequenceGenerator::next(), pc, _java_thread); |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
798 // decrement MemTracker pending_op_count |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
799 MemTracker::dec_pending_op_count(); |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
800 } else { |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
801 // free old address, use pre-reserved sequence number |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
802 MemTracker::write_tracking_record(old_addr, MemPointerRecord::free_tag(), |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
803 0, _seq, pc, _java_thread); |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
804 MemTracker::write_tracking_record(new_addr, flags | MemPointerRecord::malloc_tag(), |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
805 size, SequenceGenerator::next(), pc, _java_thread); |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
806 } |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
807 _seq = 0; |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
808 } |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
809 } |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
810 |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
811 void MemTracker::Tracker::record(address addr, size_t size, MEMFLAGS flags, address pc) { |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
812 // OOM already? |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
813 if (addr == NULL) return; |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
814 |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
815 if (MemTracker::is_on() && NMT_CAN_TRACK(flags) && _op != NoOp) { |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
816 bool pre_reserved_seq = (_seq != 0); |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
817 address pc = CALLER_CALLER_PC; |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
818 MEMFLAGS orig_flags = flags; |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
819 |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
820 // or the tagging flags |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
821 switch(_op) { |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
822 case Malloc: |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
823 flags |= MemPointerRecord::malloc_tag(); |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
824 break; |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
825 case Free: |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
826 flags = MemPointerRecord::free_tag(); |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
827 break; |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
828 case Realloc: |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
829 fatal("Use the other Tracker::record()"); |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
830 break; |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
831 case Reserve: |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
832 case ReserveAndCommit: |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
833 flags |= MemPointerRecord::virtual_memory_reserve_tag(); |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
834 break; |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
835 case Commit: |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
836 flags = MemPointerRecord::virtual_memory_commit_tag(); |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
837 break; |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
838 case Type: |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
839 flags |= MemPointerRecord::virtual_memory_type_tag(); |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
840 break; |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
841 case Uncommit: |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
842 assert(pre_reserved_seq, "Need pre-reserve sequence number"); |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
843 flags = MemPointerRecord::virtual_memory_uncommit_tag(); |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
844 break; |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
845 case Release: |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
846 assert(pre_reserved_seq, "Need pre-reserve sequence number"); |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
847 flags = MemPointerRecord::virtual_memory_release_tag(); |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
848 break; |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
849 case ArenaSize: |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
850 // a bit of hack here, add a small postive offset to arena |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
851 // address for its size record, so the size record is sorted |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
852 // right after arena record. |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
853 flags = MemPointerRecord::arena_size_tag(); |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
854 addr += sizeof(void*); |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
855 break; |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
856 case StackRelease: |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
857 flags = MemPointerRecord::virtual_memory_release_tag(); |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
858 break; |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
859 default: |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
860 ShouldNotReachHere(); |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
861 } |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
862 |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
863 // write memory tracking record |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
864 if (_need_thread_critical_lock) { |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
865 ThreadCritical tc; |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
866 if (_seq == 0) _seq = SequenceGenerator::next(); |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
867 MemTracker::write_tracking_record(addr, flags, size, _seq, pc, _java_thread); |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
868 if (_op == ReserveAndCommit) { |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
869 MemTracker::write_tracking_record(addr, orig_flags | MemPointerRecord::virtual_memory_commit_tag(), |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
870 size, SequenceGenerator::next(), pc, _java_thread); |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
871 } |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
872 if (pre_reserved_seq) MemTracker::dec_pending_op_count(); |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
873 } else { |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
874 if (_seq == 0) _seq = SequenceGenerator::next(); |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
875 MemTracker::write_tracking_record(addr, flags, size, _seq, pc, _java_thread); |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
876 if (_op == ReserveAndCommit) { |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
877 MemTracker::write_tracking_record(addr, orig_flags | MemPointerRecord::virtual_memory_commit_tag(), |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
878 size, SequenceGenerator::next(), pc, _java_thread); |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
879 } |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
880 } |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
881 _seq = 0; |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
882 } |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
883 } |
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10355
diff
changeset
|
884 |