author | zgu |
Mon, 17 Sep 2012 10:20:04 -0400 | |
changeset 13859 | 7fe4578493fc |
parent 13478 | 85fe611440e5 |
child 13872 | bbee2239e884 |
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) { |
|
13859
7fe4578493fc
7190089: NMT ON: NMT failed assertion on thread's stack base address
zgu
parents:
13478
diff
changeset
|
344 |
assert(addr != NULL, "Sanity check"); |
13195 | 345 |
if (!shutdown_in_progress()) { |
346 |
// single thread, we just write records direct to global recorder,' |
|
347 |
// with any lock |
|
348 |
if (_state == NMT_bootstrapping_single_thread) { |
|
349 |
assert(_main_thread_tid == os::current_thread_id(), "wrong thread"); |
|
350 |
thread = NULL; |
|
351 |
} else { |
|
352 |
if (thread == NULL) { |
|
353 |
// don't use Thread::current(), since it is possible that |
|
354 |
// the calling thread has yet to attach to VM 'Thread', |
|
355 |
// which will result assertion failure |
|
356 |
thread = ThreadLocalStorage::thread(); |
|
357 |
} |
|
358 |
} |
|
359 |
||
360 |
if (thread != NULL) { |
|
361 |
if (thread->is_Java_thread() && ((JavaThread*)thread)->is_safepoint_visible()) { |
|
13300 | 362 |
JavaThread* java_thread = static_cast<JavaThread*>(thread); |
363 |
JavaThreadState state = java_thread->thread_state(); |
|
364 |
if (SafepointSynchronize::safepoint_safe(java_thread, state)) { |
|
365 |
// JavaThreads that are safepoint safe, can run through safepoint, |
|
366 |
// so ThreadCritical is needed to ensure no threads at safepoint create |
|
367 |
// new records while the records are being gathered and the sequence number is changing |
|
368 |
ThreadCritical tc; |
|
369 |
create_record_in_recorder(addr, flags, size, pc, java_thread); |
|
13195 | 370 |
} else { |
13300 | 371 |
create_record_in_recorder(addr, flags, size, pc, java_thread); |
13195 | 372 |
} |
373 |
} else { |
|
374 |
// other threads, such as worker and watcher threads, etc. need to |
|
375 |
// take ThreadCritical to write to global recorder |
|
376 |
ThreadCritical tc; |
|
377 |
create_record_in_recorder(addr, flags, size, pc, NULL); |
|
378 |
} |
|
379 |
} else { |
|
380 |
if (_state == NMT_bootstrapping_single_thread) { |
|
381 |
// single thread, no lock needed |
|
382 |
create_record_in_recorder(addr, flags, size, pc, NULL); |
|
383 |
} else { |
|
384 |
// for thread has yet to attach VM 'Thread', we can not use VM mutex. |
|
385 |
// use native thread critical instead |
|
386 |
ThreadCritical tc; |
|
387 |
create_record_in_recorder(addr, flags, size, pc, NULL); |
|
388 |
} |
|
389 |
} |
|
390 |
} |
|
391 |
} |
|
392 |
||
393 |
// write a record to proper recorder. No lock can be taken from this method |
|
394 |
// down. |
|
395 |
void MemTracker::create_record_in_recorder(address addr, MEMFLAGS flags, |
|
13300 | 396 |
size_t size, address pc, JavaThread* thread) { |
13195 | 397 |
|
13300 | 398 |
MemRecorder* rc = get_thread_recorder(thread); |
13195 | 399 |
if (rc != NULL) { |
400 |
rc->record(addr, flags, size, pc); |
|
401 |
} |
|
402 |
} |
|
403 |
||
404 |
/** |
|
405 |
* enqueue a recorder to pending queue |
|
406 |
*/ |
|
407 |
void MemTracker::enqueue_pending_recorder(MemRecorder* rec) { |
|
408 |
assert(rec != NULL, "null recorder"); |
|
409 |
||
410 |
// we are shutting down, so just delete it |
|
411 |
if (shutdown_in_progress()) { |
|
412 |
rec->set_next(NULL); |
|
413 |
delete rec; |
|
414 |
return; |
|
415 |
} |
|
416 |
||
417 |
MemRecorder* cur_head = const_cast<MemRecorder*>(_merge_pending_queue); |
|
418 |
rec->set_next(cur_head); |
|
419 |
while ((void*)cur_head != Atomic::cmpxchg_ptr((void*)rec, (void*)&_merge_pending_queue, |
|
420 |
(void*)cur_head)) { |
|
421 |
cur_head = const_cast<MemRecorder*>(_merge_pending_queue); |
|
422 |
rec->set_next(cur_head); |
|
423 |
} |
|
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
|
424 |
NOT_PRODUCT(Atomic::inc(&_pending_recorder_count);) |
13195 | 425 |
} |
426 |
||
427 |
/* |
|
428 |
* The method is called at global safepoint |
|
429 |
* during it synchronization process. |
|
430 |
* 1. enqueue all JavaThreads' per-thread recorders |
|
431 |
* 2. enqueue global recorder |
|
432 |
* 3. retrieve all pending recorders |
|
433 |
* 4. reset global sequence number generator |
|
434 |
* 5. call worker's sync |
|
435 |
*/ |
|
436 |
#define MAX_SAFEPOINTS_TO_SKIP 128 |
|
437 |
#define SAFE_SEQUENCE_THRESHOLD 30 |
|
438 |
#define HIGH_GENERATION_THRESHOLD 60 |
|
439 |
||
440 |
void MemTracker::sync() { |
|
441 |
assert(_tracking_level > NMT_off, "NMT is not enabled"); |
|
442 |
assert(SafepointSynchronize::is_at_safepoint(), "Safepoint required"); |
|
443 |
||
444 |
// Some GC tests hit large number of safepoints in short period of time |
|
445 |
// without meaningful activities. We should prevent going to |
|
446 |
// sync point in these cases, which can potentially exhaust generation buffer. |
|
447 |
// Here is the factots to determine if we should go into sync point: |
|
448 |
// 1. not to overflow sequence number |
|
449 |
// 2. if we are in danger to overflow generation buffer |
|
450 |
// 3. how many safepoints we already skipped sync point |
|
451 |
if (_state == NMT_started) { |
|
452 |
// worker thread is not ready, no one can manage generation |
|
453 |
// buffer, so skip this safepoint |
|
454 |
if (_worker_thread == NULL) return; |
|
455 |
||
456 |
if (_sync_point_skip_count < MAX_SAFEPOINTS_TO_SKIP) { |
|
457 |
int per_seq_in_use = SequenceGenerator::peek() * 100 / max_jint; |
|
458 |
int per_gen_in_use = _worker_thread->generations_in_use() * 100 / MAX_GENERATIONS; |
|
459 |
if (per_seq_in_use < SAFE_SEQUENCE_THRESHOLD && per_gen_in_use >= HIGH_GENERATION_THRESHOLD) { |
|
460 |
_sync_point_skip_count ++; |
|
461 |
return; |
|
462 |
} |
|
463 |
} |
|
464 |
_sync_point_skip_count = 0; |
|
465 |
{ |
|
466 |
// This method is running at safepoint, with ThreadCritical lock, |
|
467 |
// it should guarantee that NMT is fully sync-ed. |
|
468 |
ThreadCritical tc; |
|
13300 | 469 |
|
470 |
// walk all JavaThreads to collect recorders |
|
471 |
SyncThreadRecorderClosure stc; |
|
472 |
Threads::threads_do(&stc); |
|
473 |
||
474 |
_thread_count = stc.get_thread_count(); |
|
475 |
MemRecorder* pending_recorders = get_pending_recorders(); |
|
476 |
||
13195 | 477 |
if (_global_recorder != NULL) { |
478 |
_global_recorder->set_next(pending_recorders); |
|
479 |
pending_recorders = _global_recorder; |
|
480 |
_global_recorder = NULL; |
|
481 |
} |
|
482 |
SequenceGenerator::reset(); |
|
483 |
// check _worker_thread with lock to avoid racing condition |
|
484 |
if (_worker_thread != NULL) { |
|
485 |
_worker_thread->at_sync_point(pending_recorders); |
|
486 |
} |
|
487 |
} |
|
488 |
} |
|
489 |
||
490 |
// now, it is the time to shut whole things off |
|
491 |
if (_state == NMT_final_shutdown) { |
|
492 |
// walk all JavaThreads to delete all recorders |
|
493 |
SyncThreadRecorderClosure stc; |
|
494 |
Threads::threads_do(&stc); |
|
495 |
// delete global recorder |
|
496 |
{ |
|
497 |
ThreadCritical tc; |
|
498 |
if (_global_recorder != NULL) { |
|
499 |
delete _global_recorder; |
|
500 |
_global_recorder = NULL; |
|
501 |
} |
|
502 |
} |
|
13300 | 503 |
MemRecorder* pending_recorders = get_pending_recorders(); |
504 |
if (pending_recorders != NULL) { |
|
505 |
delete pending_recorders; |
|
506 |
} |
|
507 |
// try at a later sync point to ensure MemRecorder instance drops to zero to |
|
508 |
// completely shutdown NMT |
|
509 |
if (MemRecorder::_instance_count == 0) { |
|
510 |
_state = NMT_shutdown; |
|
511 |
_tracking_level = NMT_off; |
|
512 |
} |
|
13195 | 513 |
} |
514 |
} |
|
515 |
||
516 |
/* |
|
517 |
* Start worker thread. |
|
518 |
*/ |
|
519 |
bool MemTracker::start_worker() { |
|
520 |
assert(_worker_thread == NULL, "Just Check"); |
|
521 |
_worker_thread = new (std::nothrow) MemTrackWorker(); |
|
522 |
if (_worker_thread == NULL || _worker_thread->has_error()) { |
|
523 |
shutdown(NMT_initialization); |
|
524 |
return false; |
|
525 |
} |
|
526 |
_worker_thread->start(); |
|
527 |
return true; |
|
528 |
} |
|
529 |
||
530 |
/* |
|
531 |
* We need to collect a JavaThread's per-thread recorder |
|
532 |
* before it exits. |
|
533 |
*/ |
|
534 |
void MemTracker::thread_exiting(JavaThread* thread) { |
|
535 |
if (is_on()) { |
|
536 |
MemRecorder* rec = thread->get_recorder(); |
|
537 |
if (rec != NULL) { |
|
538 |
enqueue_pending_recorder(rec); |
|
539 |
thread->set_recorder(NULL); |
|
540 |
} |
|
541 |
} |
|
542 |
} |
|
543 |
||
544 |
// baseline current memory snapshot |
|
545 |
bool MemTracker::baseline() { |
|
13301
f6dd749390dd
7181986: NMT ON: Assertion failure when running jdi ExpiredRequestDeletionTest
zgu
parents:
13195
diff
changeset
|
546 |
MutexLockerEx lock(_query_lock, true); |
13195 | 547 |
MemSnapshot* snapshot = get_snapshot(); |
548 |
if (snapshot != NULL) { |
|
549 |
return _baseline.baseline(*snapshot, false); |
|
550 |
} |
|
551 |
return false; |
|
552 |
} |
|
553 |
||
554 |
// print memory usage from current snapshot |
|
555 |
bool MemTracker::print_memory_usage(BaselineOutputer& out, size_t unit, bool summary_only) { |
|
556 |
MemBaseline baseline; |
|
13301
f6dd749390dd
7181986: NMT ON: Assertion failure when running jdi ExpiredRequestDeletionTest
zgu
parents:
13195
diff
changeset
|
557 |
MutexLockerEx lock(_query_lock, true); |
13195 | 558 |
MemSnapshot* snapshot = get_snapshot(); |
559 |
if (snapshot != NULL && baseline.baseline(*snapshot, summary_only)) { |
|
560 |
BaselineReporter reporter(out, unit); |
|
561 |
reporter.report_baseline(baseline, summary_only); |
|
562 |
return true; |
|
563 |
} |
|
564 |
return false; |
|
565 |
} |
|
566 |
||
567 |
// compare memory usage between current snapshot and baseline |
|
568 |
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
|
569 |
MutexLockerEx lock(_query_lock, true); |
13195 | 570 |
if (_baseline.baselined()) { |
571 |
MemBaseline baseline; |
|
572 |
MemSnapshot* snapshot = get_snapshot(); |
|
573 |
if (snapshot != NULL && baseline.baseline(*snapshot, summary_only)) { |
|
574 |
BaselineReporter reporter(out, unit); |
|
575 |
reporter.diff_baselines(baseline, _baseline, summary_only); |
|
576 |
return true; |
|
577 |
} |
|
578 |
} |
|
579 |
return false; |
|
580 |
} |
|
581 |
||
582 |
#ifndef PRODUCT |
|
583 |
void MemTracker::walk_stack(int toSkip, char* buf, int len) { |
|
584 |
int cur_len = 0; |
|
585 |
char tmp[1024]; |
|
586 |
address pc; |
|
587 |
||
588 |
while (cur_len < len) { |
|
589 |
pc = os::get_caller_pc(toSkip + 1); |
|
590 |
if (pc != NULL && os::dll_address_to_function_name(pc, tmp, sizeof(tmp), NULL)) { |
|
591 |
jio_snprintf(&buf[cur_len], (len - cur_len), "%s\n", tmp); |
|
592 |
cur_len = (int)strlen(buf); |
|
593 |
} else { |
|
594 |
buf[cur_len] = '\0'; |
|
595 |
break; |
|
596 |
} |
|
597 |
toSkip ++; |
|
598 |
} |
|
599 |
} |
|
600 |
||
601 |
void MemTracker::print_tracker_stats(outputStream* st) { |
|
602 |
st->print_cr("\nMemory Tracker Stats:"); |
|
603 |
st->print_cr("\tMax sequence number = %d", SequenceGenerator::max_seq_num()); |
|
604 |
st->print_cr("\tthead count = %d", _thread_count); |
|
605 |
st->print_cr("\tArena instance = %d", Arena::_instance_count); |
|
606 |
st->print_cr("\tpooled recorder count = %d", _pooled_recorder_count); |
|
607 |
st->print_cr("\tqueued recorder count = %d", _pending_recorder_count); |
|
608 |
st->print_cr("\tmemory recorder instance count = %d", MemRecorder::_instance_count); |
|
609 |
if (_worker_thread != NULL) { |
|
610 |
st->print_cr("\tWorker thread:"); |
|
611 |
st->print_cr("\t\tSync point count = %d", _worker_thread->_sync_point_count); |
|
612 |
st->print_cr("\t\tpending recorder count = %d", _worker_thread->count_pending_recorders()); |
|
613 |
st->print_cr("\t\tmerge count = %d", _worker_thread->_merge_count); |
|
614 |
} else { |
|
615 |
st->print_cr("\tWorker thread is not started"); |
|
616 |
} |
|
617 |
st->print_cr(" "); |
|
618 |
||
619 |
if (_snapshot != NULL) { |
|
620 |
_snapshot->print_snapshot_stats(st); |
|
621 |
} else { |
|
622 |
st->print_cr("No snapshot"); |
|
623 |
} |
|
624 |
} |
|
625 |
#endif |
|
626 |