author | zgu |
Tue, 14 Aug 2012 13:56:46 -0400 | |
changeset 13478 | 85fe611440e5 |
parent 13304 | f97566d01de4 |
child 13859 | 7fe4578493fc |
permissions | -rw-r--r-- |
13195 | 1 |
/* |
2 |
* Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. |
|
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 |
||
26 |
#include "runtime/atomic.hpp" |
|
27 |
#include "runtime/interfaceSupport.hpp" |
|
28 |
#include "runtime/mutexLocker.hpp" |
|
29 |
#include "runtime/safepoint.hpp" |
|
30 |
#include "runtime/threadCritical.hpp" |
|
31 |
#include "services/memPtr.hpp" |
|
32 |
#include "services/memReporter.hpp" |
|
33 |
#include "services/memTracker.hpp" |
|
34 |
#include "utilities/decoder.hpp" |
|
35 |
#include "utilities/globalDefinitions.hpp" |
|
36 |
||
37 |
bool NMT_track_callsite = false; |
|
38 |
||
39 |
// walk all 'known' threads at NMT sync point, and collect their recorders |
|
40 |
void SyncThreadRecorderClosure::do_thread(Thread* thread) { |
|
41 |
assert(SafepointSynchronize::is_at_safepoint(), "Safepoint required"); |
|
42 |
if (thread->is_Java_thread()) { |
|
43 |
JavaThread* javaThread = (JavaThread*)thread; |
|
44 |
MemRecorder* recorder = javaThread->get_recorder(); |
|
45 |
if (recorder != NULL) { |
|
46 |
MemTracker::enqueue_pending_recorder(recorder); |
|
47 |
javaThread->set_recorder(NULL); |
|
48 |
} |
|
49 |
} |
|
50 |
_thread_count ++; |
|
51 |
} |
|
52 |
||
53 |
||
54 |
MemRecorder* MemTracker::_global_recorder = NULL; |
|
55 |
MemSnapshot* MemTracker::_snapshot = NULL; |
|
56 |
MemBaseline MemTracker::_baseline; |
|
13301
f6dd749390dd
7181986: NMT ON: Assertion failure when running jdi ExpiredRequestDeletionTest
zgu
parents:
13195
diff
changeset
|
57 |
Mutex* MemTracker::_query_lock = NULL; |
13195 | 58 |
volatile MemRecorder* MemTracker::_merge_pending_queue = NULL; |
59 |
volatile MemRecorder* MemTracker::_pooled_recorders = NULL; |
|
60 |
MemTrackWorker* MemTracker::_worker_thread = NULL; |
|
61 |
int MemTracker::_sync_point_skip_count = 0; |
|
62 |
MemTracker::NMTLevel MemTracker::_tracking_level = MemTracker::NMT_off; |
|
63 |
volatile MemTracker::NMTStates MemTracker::_state = NMT_uninited; |
|
64 |
MemTracker::ShutdownReason MemTracker::_reason = NMT_shutdown_none; |
|
65 |
int MemTracker::_thread_count = 255; |
|
66 |
volatile jint MemTracker::_pooled_recorder_count = 0; |
|
67 |
debug_only(intx MemTracker::_main_thread_tid = 0;) |
|
13478
85fe611440e5
7191124: Optimized build is broken due to inconsistent use of DEBUG_ONLY and NOT_PRODUCT macros in NMT
zgu
parents:
13304
diff
changeset
|
68 |
NOT_PRODUCT(volatile jint MemTracker::_pending_recorder_count = 0;) |
13195 | 69 |
|
70 |
void MemTracker::init_tracking_options(const char* option_line) { |
|
71 |
_tracking_level = NMT_off; |
|
72 |
if (strncmp(option_line, "=summary", 8) == 0) { |
|
73 |
_tracking_level = NMT_summary; |
|
74 |
} else if (strncmp(option_line, "=detail", 8) == 0) { |
|
75 |
_tracking_level = NMT_detail; |
|
76 |
} |
|
77 |
} |
|
78 |
||
79 |
// first phase of bootstrapping, when VM is still in single-threaded mode. |
|
80 |
void MemTracker::bootstrap_single_thread() { |
|
81 |
if (_tracking_level > NMT_off) { |
|
82 |
assert(_state == NMT_uninited, "wrong state"); |
|
83 |
||
84 |
// NMT is not supported with UseMallocOnly is on. NMT can NOT |
|
85 |
// handle the amount of malloc data without significantly impacting |
|
86 |
// runtime performance when this flag is on. |
|
87 |
if (UseMallocOnly) { |
|
88 |
shutdown(NMT_use_malloc_only); |
|
89 |
return; |
|
90 |
} |
|
91 |
||
13301
f6dd749390dd
7181986: NMT ON: Assertion failure when running jdi ExpiredRequestDeletionTest
zgu
parents:
13195
diff
changeset
|
92 |
_query_lock = new (std::nothrow) Mutex(Monitor::max_nonleaf, "NMT_queryLock"); |
f6dd749390dd
7181986: NMT ON: Assertion failure when running jdi ExpiredRequestDeletionTest
zgu
parents:
13195
diff
changeset
|
93 |
if (_query_lock == NULL) { |
f6dd749390dd
7181986: NMT ON: Assertion failure when running jdi ExpiredRequestDeletionTest
zgu
parents:
13195
diff
changeset
|
94 |
shutdown(NMT_out_of_memory); |
f6dd749390dd
7181986: NMT ON: Assertion failure when running jdi ExpiredRequestDeletionTest
zgu
parents:
13195
diff
changeset
|
95 |
return; |
f6dd749390dd
7181986: NMT ON: Assertion failure when running jdi ExpiredRequestDeletionTest
zgu
parents:
13195
diff
changeset
|
96 |
} |
f6dd749390dd
7181986: NMT ON: Assertion failure when running jdi ExpiredRequestDeletionTest
zgu
parents:
13195
diff
changeset
|
97 |
|
13195 | 98 |
debug_only(_main_thread_tid = os::current_thread_id();) |
99 |
_state = NMT_bootstrapping_single_thread; |
|
100 |
NMT_track_callsite = (_tracking_level == NMT_detail && can_walk_stack()); |
|
101 |
} |
|
102 |
} |
|
103 |
||
104 |
// second phase of bootstrapping, when VM is about to or already entered multi-theaded mode. |
|
105 |
void MemTracker::bootstrap_multi_thread() { |
|
106 |
if (_tracking_level > NMT_off && _state == NMT_bootstrapping_single_thread) { |
|
107 |
// create nmt lock for multi-thread execution |
|
108 |
assert(_main_thread_tid == os::current_thread_id(), "wrong thread"); |
|
109 |
_state = NMT_bootstrapping_multi_thread; |
|
110 |
NMT_track_callsite = (_tracking_level == NMT_detail && can_walk_stack()); |
|
111 |
} |
|
112 |
} |
|
113 |
||
114 |
// fully start nmt |
|
115 |
void MemTracker::start() { |
|
116 |
// Native memory tracking is off from command line option |
|
117 |
if (_tracking_level == NMT_off || shutdown_in_progress()) return; |
|
118 |
||
119 |
assert(_main_thread_tid == os::current_thread_id(), "wrong thread"); |
|
120 |
assert(_state == NMT_bootstrapping_multi_thread, "wrong state"); |
|
121 |
||
122 |
_snapshot = new (std::nothrow)MemSnapshot(); |
|
123 |
if (_snapshot != NULL && !_snapshot->out_of_memory()) { |
|
124 |
if (start_worker()) { |
|
125 |
_state = NMT_started; |
|
126 |
NMT_track_callsite = (_tracking_level == NMT_detail && can_walk_stack()); |
|
127 |
return; |
|
128 |
} |
|
129 |
} |
|
130 |
||
131 |
// fail to start native memory tracking, shut it down |
|
132 |
shutdown(NMT_initialization); |
|
133 |
} |
|
134 |
||
135 |
/** |
|
136 |
* Shutting down native memory tracking. |
|
137 |
* We can not shutdown native memory tracking immediately, so we just |
|
138 |
* setup shutdown pending flag, every native memory tracking component |
|
139 |
* should orderly shut itself down. |
|
140 |
* |
|
141 |
* The shutdown sequences: |
|
142 |
* 1. MemTracker::shutdown() sets MemTracker to shutdown pending state |
|
143 |
* 2. Worker thread calls MemTracker::final_shutdown(), which transites |
|
144 |
* MemTracker to final shutdown state. |
|
145 |
* 3. At sync point, MemTracker does final cleanup, before sets memory |
|
146 |
* tracking level to off to complete shutdown. |
|
147 |
*/ |
|
148 |
void MemTracker::shutdown(ShutdownReason reason) { |
|
149 |
if (_tracking_level == NMT_off) return; |
|
150 |
||
151 |
if (_state <= NMT_bootstrapping_single_thread) { |
|
152 |
// we still in single thread mode, there is not contention |
|
153 |
_state = NMT_shutdown_pending; |
|
154 |
_reason = reason; |
|
155 |
} else { |
|
156 |
// we want to know who initialized shutdown |
|
157 |
if ((jint)NMT_started == Atomic::cmpxchg((jint)NMT_shutdown_pending, |
|
158 |
(jint*)&_state, (jint)NMT_started)) { |
|
159 |
_reason = reason; |
|
160 |
} |
|
161 |
} |
|
162 |
} |
|
163 |
||
164 |
// final phase of shutdown |
|
165 |
void MemTracker::final_shutdown() { |
|
166 |
// delete all pending recorders and pooled recorders |
|
167 |
delete_all_pending_recorders(); |
|
168 |
delete_all_pooled_recorders(); |
|
169 |
||
170 |
{ |
|
171 |
// shared baseline and snapshot are the only objects needed to |
|
172 |
// create query results |
|
13301
f6dd749390dd
7181986: NMT ON: Assertion failure when running jdi ExpiredRequestDeletionTest
zgu
parents:
13195
diff
changeset
|
173 |
MutexLockerEx locker(_query_lock, true); |
13195 | 174 |
// cleanup baseline data and snapshot |
175 |
_baseline.clear(); |
|
176 |
delete _snapshot; |
|
177 |
_snapshot = NULL; |
|
178 |
} |
|
179 |
||
180 |
// shutdown shared decoder instance, since it is only |
|
181 |
// used by native memory tracking so far. |
|
182 |
Decoder::shutdown(); |
|
183 |
||
184 |
MemTrackWorker* worker = NULL; |
|
185 |
{ |
|
186 |
ThreadCritical tc; |
|
187 |
// can not delete worker inside the thread critical |
|
188 |
if (_worker_thread != NULL && Thread::current() == _worker_thread) { |
|
189 |
worker = _worker_thread; |
|
190 |
_worker_thread = NULL; |
|
191 |
} |
|
192 |
} |
|
193 |
if (worker != NULL) { |
|
194 |
delete worker; |
|
195 |
} |
|
196 |
_state = NMT_final_shutdown; |
|
197 |
} |
|
198 |
||
199 |
// delete all pooled recorders |
|
200 |
void MemTracker::delete_all_pooled_recorders() { |
|
201 |
// free all pooled recorders |
|
202 |
volatile MemRecorder* cur_head = _pooled_recorders; |
|
203 |
if (cur_head != NULL) { |
|
204 |
MemRecorder* null_ptr = NULL; |
|
205 |
while (cur_head != NULL && (void*)cur_head != Atomic::cmpxchg_ptr((void*)null_ptr, |
|
206 |
(void*)&_pooled_recorders, (void*)cur_head)) { |
|
207 |
cur_head = _pooled_recorders; |
|
208 |
} |
|
209 |
if (cur_head != NULL) { |
|
210 |
delete cur_head; |
|
211 |
_pooled_recorder_count = 0; |
|
212 |
} |
|
213 |
} |
|
214 |
} |
|
215 |
||
216 |
// delete all recorders in pending queue |
|
217 |
void MemTracker::delete_all_pending_recorders() { |
|
218 |
// free all pending recorders |
|
219 |
MemRecorder* pending_head = get_pending_recorders(); |
|
220 |
if (pending_head != NULL) { |
|
221 |
delete pending_head; |
|
222 |
} |
|
223 |
} |
|
224 |
||
225 |
/* |
|
226 |
* retrieve per-thread recorder of specified thread. |
|
227 |
* if thread == NULL, it means global recorder |
|
228 |
*/ |
|
229 |
MemRecorder* MemTracker::get_thread_recorder(JavaThread* thread) { |
|
230 |
if (shutdown_in_progress()) return NULL; |
|
231 |
||
232 |
MemRecorder* rc; |
|
233 |
if (thread == NULL) { |
|
234 |
rc = _global_recorder; |
|
235 |
} else { |
|
236 |
rc = thread->get_recorder(); |
|
237 |
} |
|
238 |
||
239 |
if (rc != NULL && rc->is_full()) { |
|
240 |
enqueue_pending_recorder(rc); |
|
241 |
rc = NULL; |
|
242 |
} |
|
243 |
||
244 |
if (rc == NULL) { |
|
245 |
rc = get_new_or_pooled_instance(); |
|
246 |
if (thread == NULL) { |
|
247 |
_global_recorder = rc; |
|
248 |
} else { |
|
249 |
thread->set_recorder(rc); |
|
250 |
} |
|
251 |
} |
|
252 |
return rc; |
|
253 |
} |
|
254 |
||
255 |
/* |
|
256 |
* get a per-thread recorder from pool, or create a new one if |
|
257 |
* there is not one available. |
|
258 |
*/ |
|
259 |
MemRecorder* MemTracker::get_new_or_pooled_instance() { |
|
260 |
MemRecorder* cur_head = const_cast<MemRecorder*> (_pooled_recorders); |
|
261 |
if (cur_head == NULL) { |
|
262 |
MemRecorder* rec = new (std::nothrow)MemRecorder(); |
|
263 |
if (rec == NULL || rec->out_of_memory()) { |
|
264 |
shutdown(NMT_out_of_memory); |
|
265 |
if (rec != NULL) { |
|
266 |
delete rec; |
|
267 |
rec = NULL; |
|
268 |
} |
|
269 |
} |
|
270 |
return rec; |
|
271 |
} else { |
|
272 |
MemRecorder* next_head = cur_head->next(); |
|
273 |
if ((void*)cur_head != Atomic::cmpxchg_ptr((void*)next_head, (void*)&_pooled_recorders, |
|
274 |
(void*)cur_head)) { |
|
275 |
return get_new_or_pooled_instance(); |
|
276 |
} |
|
277 |
cur_head->set_next(NULL); |
|
278 |
Atomic::dec(&_pooled_recorder_count); |
|
279 |
debug_only(cur_head->set_generation();) |
|
280 |
return cur_head; |
|
281 |
} |
|
282 |
} |
|
283 |
||
284 |
/* |
|
285 |
* retrieve all recorders in pending queue, and empty the queue |
|
286 |
*/ |
|
287 |
MemRecorder* MemTracker::get_pending_recorders() { |
|
288 |
MemRecorder* cur_head = const_cast<MemRecorder*>(_merge_pending_queue); |
|
289 |
MemRecorder* null_ptr = NULL; |
|
290 |
while ((void*)cur_head != Atomic::cmpxchg_ptr((void*)null_ptr, (void*)&_merge_pending_queue, |
|
291 |
(void*)cur_head)) { |
|
292 |
cur_head = const_cast<MemRecorder*>(_merge_pending_queue); |
|
293 |
} |
|
13478
85fe611440e5
7191124: Optimized build is broken due to inconsistent use of DEBUG_ONLY and NOT_PRODUCT macros in NMT
zgu
parents:
13304
diff
changeset
|
294 |
NOT_PRODUCT(Atomic::store(0, &_pending_recorder_count)); |
13195 | 295 |
return cur_head; |
296 |
} |
|
297 |
||
298 |
/* |
|
299 |
* release a recorder to recorder pool. |
|
300 |
*/ |
|
301 |
void MemTracker::release_thread_recorder(MemRecorder* rec) { |
|
302 |
assert(rec != NULL, "null recorder"); |
|
303 |
// we don't want to pool too many recorders |
|
304 |
rec->set_next(NULL); |
|
305 |
if (shutdown_in_progress() || _pooled_recorder_count > _thread_count * 2) { |
|
306 |
delete rec; |
|
307 |
return; |
|
308 |
} |
|
309 |
||
310 |
rec->clear(); |
|
311 |
MemRecorder* cur_head = const_cast<MemRecorder*>(_pooled_recorders); |
|
312 |
rec->set_next(cur_head); |
|
313 |
while ((void*)cur_head != Atomic::cmpxchg_ptr((void*)rec, (void*)&_pooled_recorders, |
|
314 |
(void*)cur_head)) { |
|
315 |
cur_head = const_cast<MemRecorder*>(_pooled_recorders); |
|
316 |
rec->set_next(cur_head); |
|
317 |
} |
|
318 |
Atomic::inc(&_pooled_recorder_count); |
|
319 |
} |
|
320 |
||
321 |
/* |
|
322 |
* This is the most important method in whole nmt implementation. |
|
323 |
* |
|
324 |
* Create a memory record. |
|
325 |
* 1. When nmt is in single-threaded bootstrapping mode, no lock is needed as VM |
|
326 |
* still in single thread mode. |
|
327 |
* 2. For all threads other than JavaThread, ThreadCritical is needed |
|
328 |
* to write to recorders to global recorder. |
|
329 |
* 3. For JavaThreads that are not longer visible by safepoint, also |
|
330 |
* need to take ThreadCritical and records are written to global |
|
331 |
* recorders, since these threads are NOT walked by Threads.do_thread(). |
|
332 |
* 4. JavaThreads that are running in native state, have to transition |
|
333 |
* to VM state before writing to per-thread recorders. |
|
334 |
* 5. JavaThreads that are running in VM state do not need any lock and |
|
335 |
* records are written to per-thread recorders. |
|
336 |
* 6. For a thread has yet to attach VM 'Thread', they need to take |
|
337 |
* ThreadCritical to write to global recorder. |
|
338 |
* |
|
339 |
* Important note: |
|
340 |
* NO LOCK should be taken inside ThreadCritical lock !!! |
|
341 |
*/ |
|
342 |
void MemTracker::create_memory_record(address addr, MEMFLAGS flags, |
|
343 |
size_t size, address pc, Thread* thread) { |
|
344 |
if (!shutdown_in_progress()) { |
|
345 |
// single thread, we just write records direct to global recorder,' |
|
346 |
// with any lock |
|
347 |
if (_state == NMT_bootstrapping_single_thread) { |
|
348 |
assert(_main_thread_tid == os::current_thread_id(), "wrong thread"); |
|
349 |
thread = NULL; |
|
350 |
} else { |
|
351 |
if (thread == NULL) { |
|
352 |
// don't use Thread::current(), since it is possible that |
|
353 |
// the calling thread has yet to attach to VM 'Thread', |
|
354 |
// which will result assertion failure |
|
355 |
thread = ThreadLocalStorage::thread(); |
|
356 |
} |
|
357 |
} |
|
358 |
||
359 |
if (thread != NULL) { |
|
360 |
if (thread->is_Java_thread() && ((JavaThread*)thread)->is_safepoint_visible()) { |
|
13300 | 361 |
JavaThread* java_thread = static_cast<JavaThread*>(thread); |
362 |
JavaThreadState state = java_thread->thread_state(); |
|
363 |
if (SafepointSynchronize::safepoint_safe(java_thread, state)) { |
|
364 |
// JavaThreads that are safepoint safe, can run through safepoint, |
|
365 |
// so ThreadCritical is needed to ensure no threads at safepoint create |
|
366 |
// new records while the records are being gathered and the sequence number is changing |
|
367 |
ThreadCritical tc; |
|
368 |
create_record_in_recorder(addr, flags, size, pc, java_thread); |
|
13195 | 369 |
} else { |
13300 | 370 |
create_record_in_recorder(addr, flags, size, pc, java_thread); |
13195 | 371 |
} |
372 |
} else { |
|
373 |
// other threads, such as worker and watcher threads, etc. need to |
|
374 |
// take ThreadCritical to write to global recorder |
|
375 |
ThreadCritical tc; |
|
376 |
create_record_in_recorder(addr, flags, size, pc, NULL); |
|
377 |
} |
|
378 |
} else { |
|
379 |
if (_state == NMT_bootstrapping_single_thread) { |
|
380 |
// single thread, no lock needed |
|
381 |
create_record_in_recorder(addr, flags, size, pc, NULL); |
|
382 |
} else { |
|
383 |
// for thread has yet to attach VM 'Thread', we can not use VM mutex. |
|
384 |
// use native thread critical instead |
|
385 |
ThreadCritical tc; |
|
386 |
create_record_in_recorder(addr, flags, size, pc, NULL); |
|
387 |
} |
|
388 |
} |
|
389 |
} |
|
390 |
} |
|
391 |
||
392 |
// write a record to proper recorder. No lock can be taken from this method |
|
393 |
// down. |
|
394 |
void MemTracker::create_record_in_recorder(address addr, MEMFLAGS flags, |
|
13300 | 395 |
size_t size, address pc, JavaThread* thread) { |
13195 | 396 |
|
13300 | 397 |
MemRecorder* rc = get_thread_recorder(thread); |
13195 | 398 |
if (rc != NULL) { |
399 |
rc->record(addr, flags, size, pc); |
|
400 |
} |
|
401 |
} |
|
402 |
||
403 |
/** |
|
404 |
* enqueue a recorder to pending queue |
|
405 |
*/ |
|
406 |
void MemTracker::enqueue_pending_recorder(MemRecorder* rec) { |
|
407 |
assert(rec != NULL, "null recorder"); |
|
408 |
||
409 |
// we are shutting down, so just delete it |
|
410 |
if (shutdown_in_progress()) { |
|
411 |
rec->set_next(NULL); |
|
412 |
delete rec; |
|
413 |
return; |
|
414 |
} |
|
415 |
||
416 |
MemRecorder* cur_head = const_cast<MemRecorder*>(_merge_pending_queue); |
|
417 |
rec->set_next(cur_head); |
|
418 |
while ((void*)cur_head != Atomic::cmpxchg_ptr((void*)rec, (void*)&_merge_pending_queue, |
|
419 |
(void*)cur_head)) { |
|
420 |
cur_head = const_cast<MemRecorder*>(_merge_pending_queue); |
|
421 |
rec->set_next(cur_head); |
|
422 |
} |
|
13478
85fe611440e5
7191124: Optimized build is broken due to inconsistent use of DEBUG_ONLY and NOT_PRODUCT macros in NMT
zgu
parents:
13304
diff
changeset
|
423 |
NOT_PRODUCT(Atomic::inc(&_pending_recorder_count);) |
13195 | 424 |
} |
425 |
||
426 |
/* |
|
427 |
* The method is called at global safepoint |
|
428 |
* during it synchronization process. |
|
429 |
* 1. enqueue all JavaThreads' per-thread recorders |
|
430 |
* 2. enqueue global recorder |
|
431 |
* 3. retrieve all pending recorders |
|
432 |
* 4. reset global sequence number generator |
|
433 |
* 5. call worker's sync |
|
434 |
*/ |
|
435 |
#define MAX_SAFEPOINTS_TO_SKIP 128 |
|
436 |
#define SAFE_SEQUENCE_THRESHOLD 30 |
|
437 |
#define HIGH_GENERATION_THRESHOLD 60 |
|
438 |
||
439 |
void MemTracker::sync() { |
|
440 |
assert(_tracking_level > NMT_off, "NMT is not enabled"); |
|
441 |
assert(SafepointSynchronize::is_at_safepoint(), "Safepoint required"); |
|
442 |
||
443 |
// Some GC tests hit large number of safepoints in short period of time |
|
444 |
// without meaningful activities. We should prevent going to |
|
445 |
// sync point in these cases, which can potentially exhaust generation buffer. |
|
446 |
// Here is the factots to determine if we should go into sync point: |
|
447 |
// 1. not to overflow sequence number |
|
448 |
// 2. if we are in danger to overflow generation buffer |
|
449 |
// 3. how many safepoints we already skipped sync point |
|
450 |
if (_state == NMT_started) { |
|
451 |
// worker thread is not ready, no one can manage generation |
|
452 |
// buffer, so skip this safepoint |
|
453 |
if (_worker_thread == NULL) return; |
|
454 |
||
455 |
if (_sync_point_skip_count < MAX_SAFEPOINTS_TO_SKIP) { |
|
456 |
int per_seq_in_use = SequenceGenerator::peek() * 100 / max_jint; |
|
457 |
int per_gen_in_use = _worker_thread->generations_in_use() * 100 / MAX_GENERATIONS; |
|
458 |
if (per_seq_in_use < SAFE_SEQUENCE_THRESHOLD && per_gen_in_use >= HIGH_GENERATION_THRESHOLD) { |
|
459 |
_sync_point_skip_count ++; |
|
460 |
return; |
|
461 |
} |
|
462 |
} |
|
463 |
_sync_point_skip_count = 0; |
|
464 |
{ |
|
465 |
// This method is running at safepoint, with ThreadCritical lock, |
|
466 |
// it should guarantee that NMT is fully sync-ed. |
|
467 |
ThreadCritical tc; |
|
13300 | 468 |
|
469 |
// walk all JavaThreads to collect recorders |
|
470 |
SyncThreadRecorderClosure stc; |
|
471 |
Threads::threads_do(&stc); |
|
472 |
||
473 |
_thread_count = stc.get_thread_count(); |
|
474 |
MemRecorder* pending_recorders = get_pending_recorders(); |
|
475 |
||
13195 | 476 |
if (_global_recorder != NULL) { |
477 |
_global_recorder->set_next(pending_recorders); |
|
478 |
pending_recorders = _global_recorder; |
|
479 |
_global_recorder = NULL; |
|
480 |
} |
|
481 |
SequenceGenerator::reset(); |
|
482 |
// check _worker_thread with lock to avoid racing condition |
|
483 |
if (_worker_thread != NULL) { |
|
484 |
_worker_thread->at_sync_point(pending_recorders); |
|
485 |
} |
|
486 |
} |
|
487 |
} |
|
488 |
||
489 |
// now, it is the time to shut whole things off |
|
490 |
if (_state == NMT_final_shutdown) { |
|
491 |
// walk all JavaThreads to delete all recorders |
|
492 |
SyncThreadRecorderClosure stc; |
|
493 |
Threads::threads_do(&stc); |
|
494 |
// delete global recorder |
|
495 |
{ |
|
496 |
ThreadCritical tc; |
|
497 |
if (_global_recorder != NULL) { |
|
498 |
delete _global_recorder; |
|
499 |
_global_recorder = NULL; |
|
500 |
} |
|
501 |
} |
|
13300 | 502 |
MemRecorder* pending_recorders = get_pending_recorders(); |
503 |
if (pending_recorders != NULL) { |
|
504 |
delete pending_recorders; |
|
505 |
} |
|
506 |
// try at a later sync point to ensure MemRecorder instance drops to zero to |
|
507 |
// completely shutdown NMT |
|
508 |
if (MemRecorder::_instance_count == 0) { |
|
509 |
_state = NMT_shutdown; |
|
510 |
_tracking_level = NMT_off; |
|
511 |
} |
|
13195 | 512 |
} |
513 |
} |
|
514 |
||
515 |
/* |
|
516 |
* Start worker thread. |
|
517 |
*/ |
|
518 |
bool MemTracker::start_worker() { |
|
519 |
assert(_worker_thread == NULL, "Just Check"); |
|
520 |
_worker_thread = new (std::nothrow) MemTrackWorker(); |
|
521 |
if (_worker_thread == NULL || _worker_thread->has_error()) { |
|
522 |
shutdown(NMT_initialization); |
|
523 |
return false; |
|
524 |
} |
|
525 |
_worker_thread->start(); |
|
526 |
return true; |
|
527 |
} |
|
528 |
||
529 |
/* |
|
530 |
* We need to collect a JavaThread's per-thread recorder |
|
531 |
* before it exits. |
|
532 |
*/ |
|
533 |
void MemTracker::thread_exiting(JavaThread* thread) { |
|
534 |
if (is_on()) { |
|
535 |
MemRecorder* rec = thread->get_recorder(); |
|
536 |
if (rec != NULL) { |
|
537 |
enqueue_pending_recorder(rec); |
|
538 |
thread->set_recorder(NULL); |
|
539 |
} |
|
540 |
} |
|
541 |
} |
|
542 |
||
543 |
// baseline current memory snapshot |
|
544 |
bool MemTracker::baseline() { |
|
13301
f6dd749390dd
7181986: NMT ON: Assertion failure when running jdi ExpiredRequestDeletionTest
zgu
parents:
13195
diff
changeset
|
545 |
MutexLockerEx lock(_query_lock, true); |
13195 | 546 |
MemSnapshot* snapshot = get_snapshot(); |
547 |
if (snapshot != NULL) { |
|
548 |
return _baseline.baseline(*snapshot, false); |
|
549 |
} |
|
550 |
return false; |
|
551 |
} |
|
552 |
||
553 |
// print memory usage from current snapshot |
|
554 |
bool MemTracker::print_memory_usage(BaselineOutputer& out, size_t unit, bool summary_only) { |
|
555 |
MemBaseline baseline; |
|
13301
f6dd749390dd
7181986: NMT ON: Assertion failure when running jdi ExpiredRequestDeletionTest
zgu
parents:
13195
diff
changeset
|
556 |
MutexLockerEx lock(_query_lock, true); |
13195 | 557 |
MemSnapshot* snapshot = get_snapshot(); |
558 |
if (snapshot != NULL && baseline.baseline(*snapshot, summary_only)) { |
|
559 |
BaselineReporter reporter(out, unit); |
|
560 |
reporter.report_baseline(baseline, summary_only); |
|
561 |
return true; |
|
562 |
} |
|
563 |
return false; |
|
564 |
} |
|
565 |
||
566 |
// compare memory usage between current snapshot and baseline |
|
567 |
bool MemTracker::compare_memory_usage(BaselineOutputer& out, size_t unit, bool summary_only) { |
|
13301
f6dd749390dd
7181986: NMT ON: Assertion failure when running jdi ExpiredRequestDeletionTest
zgu
parents:
13195
diff
changeset
|
568 |
MutexLockerEx lock(_query_lock, true); |
13195 | 569 |
if (_baseline.baselined()) { |
570 |
MemBaseline baseline; |
|
571 |
MemSnapshot* snapshot = get_snapshot(); |
|
572 |
if (snapshot != NULL && baseline.baseline(*snapshot, summary_only)) { |
|
573 |
BaselineReporter reporter(out, unit); |
|
574 |
reporter.diff_baselines(baseline, _baseline, summary_only); |
|
575 |
return true; |
|
576 |
} |
|
577 |
} |
|
578 |
return false; |
|
579 |
} |
|
580 |
||
581 |
#ifndef PRODUCT |
|
582 |
void MemTracker::walk_stack(int toSkip, char* buf, int len) { |
|
583 |
int cur_len = 0; |
|
584 |
char tmp[1024]; |
|
585 |
address pc; |
|
586 |
||
587 |
while (cur_len < len) { |
|
588 |
pc = os::get_caller_pc(toSkip + 1); |
|
589 |
if (pc != NULL && os::dll_address_to_function_name(pc, tmp, sizeof(tmp), NULL)) { |
|
590 |
jio_snprintf(&buf[cur_len], (len - cur_len), "%s\n", tmp); |
|
591 |
cur_len = (int)strlen(buf); |
|
592 |
} else { |
|
593 |
buf[cur_len] = '\0'; |
|
594 |
break; |
|
595 |
} |
|
596 |
toSkip ++; |
|
597 |
} |
|
598 |
} |
|
599 |
||
600 |
void MemTracker::print_tracker_stats(outputStream* st) { |
|
601 |
st->print_cr("\nMemory Tracker Stats:"); |
|
602 |
st->print_cr("\tMax sequence number = %d", SequenceGenerator::max_seq_num()); |
|
603 |
st->print_cr("\tthead count = %d", _thread_count); |
|
604 |
st->print_cr("\tArena instance = %d", Arena::_instance_count); |
|
605 |
st->print_cr("\tpooled recorder count = %d", _pooled_recorder_count); |
|
606 |
st->print_cr("\tqueued recorder count = %d", _pending_recorder_count); |
|
607 |
st->print_cr("\tmemory recorder instance count = %d", MemRecorder::_instance_count); |
|
608 |
if (_worker_thread != NULL) { |
|
609 |
st->print_cr("\tWorker thread:"); |
|
610 |
st->print_cr("\t\tSync point count = %d", _worker_thread->_sync_point_count); |
|
611 |
st->print_cr("\t\tpending recorder count = %d", _worker_thread->count_pending_recorders()); |
|
612 |
st->print_cr("\t\tmerge count = %d", _worker_thread->_merge_count); |
|
613 |
} else { |
|
614 |
st->print_cr("\tWorker thread is not started"); |
|
615 |
} |
|
616 |
st->print_cr(" "); |
|
617 |
||
618 |
if (_snapshot != NULL) { |
|
619 |
_snapshot->print_snapshot_stats(st); |
|
620 |
} else { |
|
621 |
st->print_cr("No snapshot"); |
|
622 |
} |
|
623 |
} |
|
624 |
#endif |
|
625 |