comparison src/share/vm/runtime/thread.cpp @ 6197:d2a62e0f25eb

6995781: Native Memory Tracking (Phase 1) 7151532: DCmd for hotspot native memory tracking Summary: Implementation of native memory tracking phase 1, which tracks VM native memory usage, and related DCmd Reviewed-by: acorn, coleenp, fparain
author zgu
date Thu, 28 Jun 2012 17:03:16 -0400
parents df4cd4aac5c1
children 24b9c7f4cae6
comparison
equal deleted inserted replaced
6174:74533f63b116 6197:d2a62e0f25eb
71 #include "runtime/vframe_hp.hpp" 71 #include "runtime/vframe_hp.hpp"
72 #include "runtime/vmThread.hpp" 72 #include "runtime/vmThread.hpp"
73 #include "runtime/vm_operations.hpp" 73 #include "runtime/vm_operations.hpp"
74 #include "services/attachListener.hpp" 74 #include "services/attachListener.hpp"
75 #include "services/management.hpp" 75 #include "services/management.hpp"
76 #include "services/memTracker.hpp"
76 #include "services/threadService.hpp" 77 #include "services/threadService.hpp"
77 #include "trace/traceEventTypes.hpp" 78 #include "trace/traceEventTypes.hpp"
78 #include "utilities/defaultStream.hpp" 79 #include "utilities/defaultStream.hpp"
79 #include "utilities/dtrace.hpp" 80 #include "utilities/dtrace.hpp"
80 #include "utilities/events.hpp" 81 #include "utilities/events.hpp"
157 158
158 #define DTRACE_THREAD_PROBE(probe, javathread) 159 #define DTRACE_THREAD_PROBE(probe, javathread)
159 160
160 #endif // ndef DTRACE_ENABLED 161 #endif // ndef DTRACE_ENABLED
161 162
163
162 // Class hierarchy 164 // Class hierarchy
163 // - Thread 165 // - Thread
164 // - VMThread 166 // - VMThread
165 // - WatcherThread 167 // - WatcherThread
166 // - ConcurrentMarkSweepThread 168 // - ConcurrentMarkSweepThread
167 // - JavaThread 169 // - JavaThread
168 // - CompilerThread 170 // - CompilerThread
169 171
170 // ======= Thread ======== 172 // ======= Thread ========
171
172 // Support for forcing alignment of thread objects for biased locking 173 // Support for forcing alignment of thread objects for biased locking
173 void* Thread::operator new(size_t size) { 174 void* Thread::allocate(size_t size, bool throw_excpt, MEMFLAGS flags) {
174 if (UseBiasedLocking) { 175 if (UseBiasedLocking) {
175 const int alignment = markOopDesc::biased_lock_alignment; 176 const int alignment = markOopDesc::biased_lock_alignment;
176 size_t aligned_size = size + (alignment - sizeof(intptr_t)); 177 size_t aligned_size = size + (alignment - sizeof(intptr_t));
177 void* real_malloc_addr = CHeapObj::operator new(aligned_size); 178 void* real_malloc_addr = throw_excpt? AllocateHeap(aligned_size, flags, CURRENT_PC)
179 : os::malloc(aligned_size, flags, CURRENT_PC);
178 void* aligned_addr = (void*) align_size_up((intptr_t) real_malloc_addr, alignment); 180 void* aligned_addr = (void*) align_size_up((intptr_t) real_malloc_addr, alignment);
179 assert(((uintptr_t) aligned_addr + (uintptr_t) size) <= 181 assert(((uintptr_t) aligned_addr + (uintptr_t) size) <=
180 ((uintptr_t) real_malloc_addr + (uintptr_t) aligned_size), 182 ((uintptr_t) real_malloc_addr + (uintptr_t) aligned_size),
181 "JavaThread alignment code overflowed allocated storage"); 183 "JavaThread alignment code overflowed allocated storage");
182 if (TraceBiasedLocking) { 184 if (TraceBiasedLocking) {
185 real_malloc_addr, aligned_addr); 187 real_malloc_addr, aligned_addr);
186 } 188 }
187 ((Thread*) aligned_addr)->_real_malloc_address = real_malloc_addr; 189 ((Thread*) aligned_addr)->_real_malloc_address = real_malloc_addr;
188 return aligned_addr; 190 return aligned_addr;
189 } else { 191 } else {
190 return CHeapObj::operator new(size); 192 return throw_excpt? AllocateHeap(size, flags, CURRENT_PC)
193 : os::malloc(size, flags, CURRENT_PC);
191 } 194 }
192 } 195 }
193 196
194 void Thread::operator delete(void* p) { 197 void Thread::operator delete(void* p) {
195 if (UseBiasedLocking) { 198 if (UseBiasedLocking) {
196 void* real_malloc_addr = ((Thread*) p)->_real_malloc_address; 199 void* real_malloc_addr = ((Thread*) p)->_real_malloc_address;
197 CHeapObj::operator delete(real_malloc_addr); 200 FreeHeap(real_malloc_addr, mtThread);
198 } else { 201 } else {
199 CHeapObj::operator delete(p); 202 FreeHeap(p, mtThread);
200 } 203 }
201 } 204 }
202 205
203 206
204 // Base class for all threads: VMThread, WatcherThread, ConcurrentMarkSweepThread, 207 // Base class for all threads: VMThread, WatcherThread, ConcurrentMarkSweepThread,
212 set_self_raw_id(0); 215 set_self_raw_id(0);
213 set_lgrp_id(-1); 216 set_lgrp_id(-1);
214 217
215 // allocated data structures 218 // allocated data structures
216 set_osthread(NULL); 219 set_osthread(NULL);
217 set_resource_area(new ResourceArea()); 220 set_resource_area(new (mtThread)ResourceArea());
218 set_handle_area(new HandleArea(NULL)); 221 set_handle_area(new (mtThread) HandleArea(NULL));
219 set_active_handles(NULL); 222 set_active_handles(NULL);
220 set_free_handle_block(NULL); 223 set_free_handle_block(NULL);
221 set_last_handle_mark(NULL); 224 set_last_handle_mark(NULL);
222 225
223 // This initial value ==> never claimed. 226 // This initial value ==> never claimed.
304 // initialize structure dependent on thread local storage 307 // initialize structure dependent on thread local storage
305 ThreadLocalStorage::set_thread(this); 308 ThreadLocalStorage::set_thread(this);
306 309
307 // set up any platform-specific state. 310 // set up any platform-specific state.
308 os::initialize_thread(); 311 os::initialize_thread();
309
310 } 312 }
311 313
312 void Thread::record_stack_base_and_size() { 314 void Thread::record_stack_base_and_size() {
313 set_stack_base(os::current_stack_base()); 315 set_stack_base(os::current_stack_base());
314 set_stack_size(os::current_stack_size()); 316 set_stack_size(os::current_stack_size());
317
318 // record thread's native stack, stack grows downward
319 address vm_base = _stack_base - _stack_size;
320 MemTracker::record_virtual_memory_reserve(vm_base, _stack_size,
321 CURRENT_PC, this);
322 MemTracker::record_virtual_memory_type(vm_base, mtThreadStack);
315 } 323 }
316 324
317 325
318 Thread::~Thread() { 326 Thread::~Thread() {
319 // Reclaim the objectmonitors from the omFreeList of the moribund thread. 327 // Reclaim the objectmonitors from the omFreeList of the moribund thread.
320 ObjectSynchronizer::omFlush (this) ; 328 ObjectSynchronizer::omFlush (this) ;
329
330 MemTracker::record_virtual_memory_release((_stack_base - _stack_size),
331 _stack_size, this);
321 332
322 // deallocate data structures 333 // deallocate data structures
323 delete resource_area(); 334 delete resource_area();
324 // since the handle marks are using the handle area, we have to deallocated the root 335 // since the handle marks are using the handle area, we have to deallocated the root
325 // handle mark before deallocating the thread's handle area, 336 // handle mark before deallocating the thread's handle area,
1103 _processed_thread = NULL; 1114 _processed_thread = NULL;
1104 } 1115 }
1105 1116
1106 NamedThread::~NamedThread() { 1117 NamedThread::~NamedThread() {
1107 if (_name != NULL) { 1118 if (_name != NULL) {
1108 FREE_C_HEAP_ARRAY(char, _name); 1119 FREE_C_HEAP_ARRAY(char, _name, mtThread);
1109 _name = NULL; 1120 _name = NULL;
1110 } 1121 }
1111 } 1122 }
1112 1123
1113 void NamedThread::set_name(const char* format, ...) { 1124 void NamedThread::set_name(const char* format, ...) {
1114 guarantee(_name == NULL, "Only get to set name once."); 1125 guarantee(_name == NULL, "Only get to set name once.");
1115 _name = NEW_C_HEAP_ARRAY(char, max_name_len); 1126 _name = NEW_C_HEAP_ARRAY(char, max_name_len, mtThread);
1116 guarantee(_name != NULL, "alloc failure"); 1127 guarantee(_name != NULL, "alloc failure");
1117 va_list ap; 1128 va_list ap;
1118 va_start(ap, format); 1129 va_start(ap, format);
1119 jio_vsnprintf(_name, max_name_len, format, ap); 1130 jio_vsnprintf(_name, max_name_len, format, ap);
1120 va_end(ap); 1131 va_end(ap);
1293 set_deopt_nmethod(NULL); 1304 set_deopt_nmethod(NULL);
1294 clear_must_deopt_id(); 1305 clear_must_deopt_id();
1295 set_monitor_chunks(NULL); 1306 set_monitor_chunks(NULL);
1296 set_next(NULL); 1307 set_next(NULL);
1297 set_thread_state(_thread_new); 1308 set_thread_state(_thread_new);
1309 set_recorder(NULL);
1298 _terminated = _not_terminated; 1310 _terminated = _not_terminated;
1299 _privileged_stack_top = NULL; 1311 _privileged_stack_top = NULL;
1300 _array_for_gc = NULL; 1312 _array_for_gc = NULL;
1301 _suspend_equivalent = false; 1313 _suspend_equivalent = false;
1302 _in_deopt_handler = 0; 1314 _in_deopt_handler = 0;
1368 _jni_attach_state = _attaching_via_jni; 1380 _jni_attach_state = _attaching_via_jni;
1369 } else { 1381 } else {
1370 _jni_attach_state = _not_attaching_via_jni; 1382 _jni_attach_state = _not_attaching_via_jni;
1371 } 1383 }
1372 assert(_deferred_card_mark.is_empty(), "Default MemRegion ctor"); 1384 assert(_deferred_card_mark.is_empty(), "Default MemRegion ctor");
1385 _safepoint_visible = false;
1373 } 1386 }
1374 1387
1375 bool JavaThread::reguard_stack(address cur_sp) { 1388 bool JavaThread::reguard_stack(address cur_sp) {
1376 if (_stack_guard_state != stack_guard_yellow_disabled) { 1389 if (_stack_guard_state != stack_guard_yellow_disabled) {
1377 return true; // Stack already guarded or guard pages not needed. 1390 return true; // Stack already guarded or guard pages not needed.
1430 // %note runtime_23 1443 // %note runtime_23
1431 os::ThreadType thr_type = os::java_thread; 1444 os::ThreadType thr_type = os::java_thread;
1432 thr_type = entry_point == &compiler_thread_entry ? os::compiler_thread : 1445 thr_type = entry_point == &compiler_thread_entry ? os::compiler_thread :
1433 os::java_thread; 1446 os::java_thread;
1434 os::create_thread(this, thr_type, stack_sz); 1447 os::create_thread(this, thr_type, stack_sz);
1435 1448 _safepoint_visible = false;
1436 // The _osthread may be NULL here because we ran out of memory (too many threads active). 1449 // The _osthread may be NULL here because we ran out of memory (too many threads active).
1437 // We need to throw and OutOfMemoryError - however we cannot do this here because the caller 1450 // We need to throw and OutOfMemoryError - however we cannot do this here because the caller
1438 // may hold a lock and all locks must be unlocked before throwing the exception (throwing 1451 // may hold a lock and all locks must be unlocked before throwing the exception (throwing
1439 // the exception consists of creating the exception object & initializing it, initialization 1452 // the exception consists of creating the exception object & initializing it, initialization
1440 // will leave the VM via a JavaCall and then all locks must be unlocked). 1453 // will leave the VM via a JavaCall and then all locks must be unlocked).
1447 1460
1448 JavaThread::~JavaThread() { 1461 JavaThread::~JavaThread() {
1449 if (TraceThreadEvents) { 1462 if (TraceThreadEvents) {
1450 tty->print_cr("terminate thread %p", this); 1463 tty->print_cr("terminate thread %p", this);
1451 } 1464 }
1465
1466 // Info NMT that this JavaThread is exiting, its memory
1467 // recorder should be collected
1468 assert(!is_safepoint_visible(), "wrong state");
1469 MemTracker::thread_exiting(this);
1452 1470
1453 // JSR166 -- return the parker to the free list 1471 // JSR166 -- return the parker to the free list
1454 Parker::Release(_parker); 1472 Parker::Release(_parker);
1455 _parker = NULL ; 1473 _parker = NULL ;
1456 1474
2890 2908
2891 // JVMTI PopFrame support 2909 // JVMTI PopFrame support
2892 void JavaThread::popframe_preserve_args(ByteSize size_in_bytes, void* start) { 2910 void JavaThread::popframe_preserve_args(ByteSize size_in_bytes, void* start) {
2893 assert(_popframe_preserved_args == NULL, "should not wipe out old PopFrame preserved arguments"); 2911 assert(_popframe_preserved_args == NULL, "should not wipe out old PopFrame preserved arguments");
2894 if (in_bytes(size_in_bytes) != 0) { 2912 if (in_bytes(size_in_bytes) != 0) {
2895 _popframe_preserved_args = NEW_C_HEAP_ARRAY(char, in_bytes(size_in_bytes)); 2913 _popframe_preserved_args = NEW_C_HEAP_ARRAY(char, in_bytes(size_in_bytes), mtThread);
2896 _popframe_preserved_args_size = in_bytes(size_in_bytes); 2914 _popframe_preserved_args_size = in_bytes(size_in_bytes);
2897 Copy::conjoint_jbytes(start, _popframe_preserved_args, _popframe_preserved_args_size); 2915 Copy::conjoint_jbytes(start, _popframe_preserved_args, _popframe_preserved_args_size);
2898 } 2916 }
2899 } 2917 }
2900 2918
2912 return in_WordSize(sz / wordSize); 2930 return in_WordSize(sz / wordSize);
2913 } 2931 }
2914 2932
2915 void JavaThread::popframe_free_preserved_args() { 2933 void JavaThread::popframe_free_preserved_args() {
2916 assert(_popframe_preserved_args != NULL, "should not free PopFrame preserved arguments twice"); 2934 assert(_popframe_preserved_args != NULL, "should not free PopFrame preserved arguments twice");
2917 FREE_C_HEAP_ARRAY(char, (char*) _popframe_preserved_args); 2935 FREE_C_HEAP_ARRAY(char, (char*) _popframe_preserved_args, mtThread);
2918 _popframe_preserved_args = NULL; 2936 _popframe_preserved_args = NULL;
2919 _popframe_preserved_args_size = 0; 2937 _popframe_preserved_args_size = 0;
2920 } 2938 }
2921 2939
2922 #ifndef PRODUCT 2940 #ifndef PRODUCT
3161 3179
3162 // Initialize the os module after parsing the args 3180 // Initialize the os module after parsing the args
3163 jint os_init_2_result = os::init_2(); 3181 jint os_init_2_result = os::init_2();
3164 if (os_init_2_result != JNI_OK) return os_init_2_result; 3182 if (os_init_2_result != JNI_OK) return os_init_2_result;
3165 3183
3184 // intialize TLS
3185 ThreadLocalStorage::init();
3186
3187 // Bootstrap native memory tracking, so it can start recording memory
3188 // activities before worker thread is started. This is the first phase
3189 // of bootstrapping, VM is currently running in single-thread mode.
3190 MemTracker::bootstrap_single_thread();
3191
3166 // Initialize output stream logging 3192 // Initialize output stream logging
3167 ostream_init_log(); 3193 ostream_init_log();
3168 3194
3169 // Convert -Xrun to -agentlib: if there is no JVM_OnLoad 3195 // Convert -Xrun to -agentlib: if there is no JVM_OnLoad
3170 // Must be before create_vm_init_agents() 3196 // Must be before create_vm_init_agents()
3179 3205
3180 // Initialize Threads state 3206 // Initialize Threads state
3181 _thread_list = NULL; 3207 _thread_list = NULL;
3182 _number_of_threads = 0; 3208 _number_of_threads = 0;
3183 _number_of_non_daemon_threads = 0; 3209 _number_of_non_daemon_threads = 0;
3184
3185 // Initialize TLS
3186 ThreadLocalStorage::init();
3187 3210
3188 // Initialize global data structures and create system classes in heap 3211 // Initialize global data structures and create system classes in heap
3189 vm_init_globals(); 3212 vm_init_globals();
3190 3213
3191 // Attach the main thread to this os thread 3214 // Attach the main thread to this os thread
3214 main_thread->create_stack_guard_pages(); 3237 main_thread->create_stack_guard_pages();
3215 3238
3216 // Initialize Java-Level synchronization subsystem 3239 // Initialize Java-Level synchronization subsystem
3217 ObjectMonitor::Initialize() ; 3240 ObjectMonitor::Initialize() ;
3218 3241
3242 // Second phase of bootstrapping, VM is about entering multi-thread mode
3243 MemTracker::bootstrap_multi_thread();
3244
3219 // Initialize global modules 3245 // Initialize global modules
3220 jint status = init_globals(); 3246 jint status = init_globals();
3221 if (status != JNI_OK) { 3247 if (status != JNI_OK) {
3222 delete main_thread; 3248 delete main_thread;
3223 *canTryAgain = false; // don't let caller call JNI_CreateJavaVM again 3249 *canTryAgain = false; // don't let caller call JNI_CreateJavaVM again
3240 if (VerifyBeforeGC && 3266 if (VerifyBeforeGC &&
3241 Universe::heap()->total_collections() >= VerifyGCStartAt) { 3267 Universe::heap()->total_collections() >= VerifyGCStartAt) {
3242 Universe::heap()->prepare_for_verify(); 3268 Universe::heap()->prepare_for_verify();
3243 Universe::verify(); // make sure we're starting with a clean slate 3269 Universe::verify(); // make sure we're starting with a clean slate
3244 } 3270 }
3271
3272 // Fully start NMT
3273 MemTracker::start();
3245 3274
3246 // Create the VMThread 3275 // Create the VMThread
3247 { TraceTime timer("Start VMThread", TraceStartupTime); 3276 { TraceTime timer("Start VMThread", TraceStartupTime);
3248 VMThread::create(); 3277 VMThread::create();
3249 Thread* vmthread = VMThread::vm_thread(); 3278 Thread* vmthread = VMThread::vm_thread();
3542 if (agent->is_absolute_path()) { 3571 if (agent->is_absolute_path()) {
3543 library = os::dll_load(name, ebuf, sizeof ebuf); 3572 library = os::dll_load(name, ebuf, sizeof ebuf);
3544 if (library == NULL) { 3573 if (library == NULL) {
3545 const char *sub_msg = " in absolute path, with error: "; 3574 const char *sub_msg = " in absolute path, with error: ";
3546 size_t len = strlen(msg) + strlen(name) + strlen(sub_msg) + strlen(ebuf) + 1; 3575 size_t len = strlen(msg) + strlen(name) + strlen(sub_msg) + strlen(ebuf) + 1;
3547 char *buf = NEW_C_HEAP_ARRAY(char, len); 3576 char *buf = NEW_C_HEAP_ARRAY(char, len, mtThread);
3548 jio_snprintf(buf, len, "%s%s%s%s", msg, name, sub_msg, ebuf); 3577 jio_snprintf(buf, len, "%s%s%s%s", msg, name, sub_msg, ebuf);
3549 // If we can't find the agent, exit. 3578 // If we can't find the agent, exit.
3550 vm_exit_during_initialization(buf, NULL); 3579 vm_exit_during_initialization(buf, NULL);
3551 FREE_C_HEAP_ARRAY(char, buf); 3580 FREE_C_HEAP_ARRAY(char, buf, mtThread);
3552 } 3581 }
3553 } else { 3582 } else {
3554 // Try to load the agent from the standard dll directory 3583 // Try to load the agent from the standard dll directory
3555 os::dll_build_name(buffer, sizeof(buffer), Arguments::get_dll_dir(), name); 3584 os::dll_build_name(buffer, sizeof(buffer), Arguments::get_dll_dir(), name);
3556 library = os::dll_load(buffer, ebuf, sizeof ebuf); 3585 library = os::dll_load(buffer, ebuf, sizeof ebuf);
3560 char *props = Arguments::get_kernel_properties(); 3589 char *props = Arguments::get_kernel_properties();
3561 char *home = Arguments::get_java_home(); 3590 char *home = Arguments::get_java_home();
3562 const char *fmt = "%s/bin/java %s -Dkernel.background.download=false" 3591 const char *fmt = "%s/bin/java %s -Dkernel.background.download=false"
3563 " sun.jkernel.DownloadManager -download client_jvm"; 3592 " sun.jkernel.DownloadManager -download client_jvm";
3564 size_t length = strlen(props) + strlen(home) + strlen(fmt) + 1; 3593 size_t length = strlen(props) + strlen(home) + strlen(fmt) + 1;
3565 char *cmd = NEW_C_HEAP_ARRAY(char, length); 3594 char *cmd = NEW_C_HEAP_ARRAY(char, length, mtThread);
3566 jio_snprintf(cmd, length, fmt, home, props); 3595 jio_snprintf(cmd, length, fmt, home, props);
3567 int status = os::fork_and_exec(cmd); 3596 int status = os::fork_and_exec(cmd);
3568 FreeHeap(props); 3597 FreeHeap(props);
3569 if (status == -1) { 3598 if (status == -1) {
3570 warning(cmd); 3599 warning(cmd);
3571 vm_exit_during_initialization("fork_and_exec failed: %s", 3600 vm_exit_during_initialization("fork_and_exec failed: %s",
3572 strerror(errno)); 3601 strerror(errno));
3573 } 3602 }
3574 FREE_C_HEAP_ARRAY(char, cmd); 3603 FREE_C_HEAP_ARRAY(char, cmd, mtThread);
3575 // when this comes back the instrument.dll should be where it belongs. 3604 // when this comes back the instrument.dll should be where it belongs.
3576 library = os::dll_load(buffer, ebuf, sizeof ebuf); 3605 library = os::dll_load(buffer, ebuf, sizeof ebuf);
3577 } 3606 }
3578 #endif // KERNEL 3607 #endif // KERNEL
3579 if (library == NULL) { // Try the local directory 3608 if (library == NULL) { // Try the local directory
3581 os::dll_build_name(buffer, sizeof(buffer), ns, name); 3610 os::dll_build_name(buffer, sizeof(buffer), ns, name);
3582 library = os::dll_load(buffer, ebuf, sizeof ebuf); 3611 library = os::dll_load(buffer, ebuf, sizeof ebuf);
3583 if (library == NULL) { 3612 if (library == NULL) {
3584 const char *sub_msg = " on the library path, with error: "; 3613 const char *sub_msg = " on the library path, with error: ";
3585 size_t len = strlen(msg) + strlen(name) + strlen(sub_msg) + strlen(ebuf) + 1; 3614 size_t len = strlen(msg) + strlen(name) + strlen(sub_msg) + strlen(ebuf) + 1;
3586 char *buf = NEW_C_HEAP_ARRAY(char, len); 3615 char *buf = NEW_C_HEAP_ARRAY(char, len, mtThread);
3587 jio_snprintf(buf, len, "%s%s%s%s", msg, name, sub_msg, ebuf); 3616 jio_snprintf(buf, len, "%s%s%s%s", msg, name, sub_msg, ebuf);
3588 // If we can't find the agent, exit. 3617 // If we can't find the agent, exit.
3589 vm_exit_during_initialization(buf, NULL); 3618 vm_exit_during_initialization(buf, NULL);
3590 FREE_C_HEAP_ARRAY(char, buf); 3619 FREE_C_HEAP_ARRAY(char, buf, mtThread);
3591 } 3620 }
3592 } 3621 }
3593 } 3622 }
3594 agent->set_os_lib(library); 3623 agent->set_os_lib(library);
3595 } 3624 }
3754 // there is a serious error in VM. The two shutdown paths are not exactly 3783 // there is a serious error in VM. The two shutdown paths are not exactly
3755 // the same, but they share Shutdown.shutdown() at Java level and before_exit() 3784 // the same, but they share Shutdown.shutdown() at Java level and before_exit()
3756 // and VM_Exit op at VM level. 3785 // and VM_Exit op at VM level.
3757 // 3786 //
3758 // Shutdown sequence: 3787 // Shutdown sequence:
3788 // + Shutdown native memory tracking if it is on
3759 // + Wait until we are the last non-daemon thread to execute 3789 // + Wait until we are the last non-daemon thread to execute
3760 // <-- every thing is still working at this moment --> 3790 // <-- every thing is still working at this moment -->
3761 // + Call java.lang.Shutdown.shutdown(), which will invoke Java level 3791 // + Call java.lang.Shutdown.shutdown(), which will invoke Java level
3762 // shutdown hooks, run finalizers if finalization-on-exit 3792 // shutdown hooks, run finalizers if finalization-on-exit
3763 // + Call before_exit(), prepare for VM exit 3793 // + Call before_exit(), prepare for VM exit
3799 // 3829 //
3800 Threads_lock->wait(!Mutex::_no_safepoint_check_flag, 0, 3830 Threads_lock->wait(!Mutex::_no_safepoint_check_flag, 0,
3801 Mutex::_as_suspend_equivalent_flag); 3831 Mutex::_as_suspend_equivalent_flag);
3802 } 3832 }
3803 3833
3834 // Shutdown NMT before exit. Otherwise,
3835 // it will run into trouble when system destroys static variables.
3836 MemTracker::shutdown(MemTracker::NMT_normal);
3837
3804 // Hang forever on exit if we are reporting an error. 3838 // Hang forever on exit if we are reporting an error.
3805 if (ShowMessageBoxOnError && is_error_reported()) { 3839 if (ShowMessageBoxOnError && is_error_reported()) {
3806 os::infinite_sleep(); 3840 os::infinite_sleep();
3807 } 3841 }
3808 os::wait_for_keypress_at_exit(); 3842 os::wait_for_keypress_at_exit();
3905 if ((!force_daemon) && (threadObj == NULL || !java_lang_Thread::is_daemon(threadObj))) { 3939 if ((!force_daemon) && (threadObj == NULL || !java_lang_Thread::is_daemon(threadObj))) {
3906 _number_of_non_daemon_threads++; 3940 _number_of_non_daemon_threads++;
3907 daemon = false; 3941 daemon = false;
3908 } 3942 }
3909 3943
3944 p->set_safepoint_visible(true);
3945
3910 ThreadService::add_thread(p, daemon); 3946 ThreadService::add_thread(p, daemon);
3911 3947
3912 // Possible GC point. 3948 // Possible GC point.
3913 Events::log(p, "Thread added: " INTPTR_FORMAT, p); 3949 Events::log(p, "Thread added: " INTPTR_FORMAT, p);
3914 } 3950 }
3950 // Make sure that safepoint code disregard this thread. This is needed since 3986 // Make sure that safepoint code disregard this thread. This is needed since
3951 // the thread might mess around with locks after this point. This can cause it 3987 // the thread might mess around with locks after this point. This can cause it
3952 // to do callbacks into the safepoint code. However, the safepoint code is not aware 3988 // to do callbacks into the safepoint code. However, the safepoint code is not aware
3953 // of this thread since it is removed from the queue. 3989 // of this thread since it is removed from the queue.
3954 p->set_terminated_value(); 3990 p->set_terminated_value();
3991
3992 // Now, this thread is not visible to safepoint
3993 p->set_safepoint_visible(false);
3994
3955 } // unlock Threads_lock 3995 } // unlock Threads_lock
3956 3996
3957 // Since Events::log uses a lock, we grab it outside the Threads_lock 3997 // Since Events::log uses a lock, we grab it outside the Threads_lock
3958 Events::log(p, "Thread exited: " INTPTR_FORMAT, p); 3998 Events::log(p, "Thread exited: " INTPTR_FORMAT, p);
3959 } 3999 }