comparison src/share/vm/runtime/thread.cpp @ 6275:957c266d8bc5

Merge with http://hg.openjdk.java.net/hsx/hsx24/hotspot/
author Doug Simon <doug.simon@oracle.com>
date Tue, 21 Aug 2012 10:39:19 +0200
parents 120820e30baa 24b9c7f4cae6
children e522a00b91aa
comparison
equal deleted inserted replaced
5891:fd8832ae511d 6275:957c266d8bc5
74 #include "runtime/vframe_hp.hpp" 74 #include "runtime/vframe_hp.hpp"
75 #include "runtime/vmThread.hpp" 75 #include "runtime/vmThread.hpp"
76 #include "runtime/vm_operations.hpp" 76 #include "runtime/vm_operations.hpp"
77 #include "services/attachListener.hpp" 77 #include "services/attachListener.hpp"
78 #include "services/management.hpp" 78 #include "services/management.hpp"
79 #include "services/memTracker.hpp"
79 #include "services/threadService.hpp" 80 #include "services/threadService.hpp"
80 #include "trace/traceEventTypes.hpp" 81 #include "trace/traceEventTypes.hpp"
81 #include "utilities/defaultStream.hpp" 82 #include "utilities/defaultStream.hpp"
82 #include "utilities/dtrace.hpp" 83 #include "utilities/dtrace.hpp"
83 #include "utilities/events.hpp" 84 #include "utilities/events.hpp"
160 161
161 #define DTRACE_THREAD_PROBE(probe, javathread) 162 #define DTRACE_THREAD_PROBE(probe, javathread)
162 163
163 #endif // ndef DTRACE_ENABLED 164 #endif // ndef DTRACE_ENABLED
164 165
166
165 // Class hierarchy 167 // Class hierarchy
166 // - Thread 168 // - Thread
167 // - VMThread 169 // - VMThread
168 // - WatcherThread 170 // - WatcherThread
169 // - ConcurrentMarkSweepThread 171 // - ConcurrentMarkSweepThread
170 // - JavaThread 172 // - JavaThread
171 // - CompilerThread 173 // - CompilerThread
172 174
173 // ======= Thread ======== 175 // ======= Thread ========
174
175 // Support for forcing alignment of thread objects for biased locking 176 // Support for forcing alignment of thread objects for biased locking
176 void* Thread::operator new(size_t size) { 177 void* Thread::allocate(size_t size, bool throw_excpt, MEMFLAGS flags) {
177 if (UseBiasedLocking) { 178 if (UseBiasedLocking) {
178 const int alignment = markOopDesc::biased_lock_alignment; 179 const int alignment = markOopDesc::biased_lock_alignment;
179 size_t aligned_size = size + (alignment - sizeof(intptr_t)); 180 size_t aligned_size = size + (alignment - sizeof(intptr_t));
180 void* real_malloc_addr = CHeapObj::operator new(aligned_size); 181 void* real_malloc_addr = throw_excpt? AllocateHeap(aligned_size, flags, CURRENT_PC)
182 : os::malloc(aligned_size, flags, CURRENT_PC);
181 void* aligned_addr = (void*) align_size_up((intptr_t) real_malloc_addr, alignment); 183 void* aligned_addr = (void*) align_size_up((intptr_t) real_malloc_addr, alignment);
182 assert(((uintptr_t) aligned_addr + (uintptr_t) size) <= 184 assert(((uintptr_t) aligned_addr + (uintptr_t) size) <=
183 ((uintptr_t) real_malloc_addr + (uintptr_t) aligned_size), 185 ((uintptr_t) real_malloc_addr + (uintptr_t) aligned_size),
184 "JavaThread alignment code overflowed allocated storage"); 186 "JavaThread alignment code overflowed allocated storage");
185 if (TraceBiasedLocking) { 187 if (TraceBiasedLocking) {
188 real_malloc_addr, aligned_addr); 190 real_malloc_addr, aligned_addr);
189 } 191 }
190 ((Thread*) aligned_addr)->_real_malloc_address = real_malloc_addr; 192 ((Thread*) aligned_addr)->_real_malloc_address = real_malloc_addr;
191 return aligned_addr; 193 return aligned_addr;
192 } else { 194 } else {
193 return CHeapObj::operator new(size); 195 return throw_excpt? AllocateHeap(size, flags, CURRENT_PC)
196 : os::malloc(size, flags, CURRENT_PC);
194 } 197 }
195 } 198 }
196 199
197 void Thread::operator delete(void* p) { 200 void Thread::operator delete(void* p) {
198 if (UseBiasedLocking) { 201 if (UseBiasedLocking) {
199 void* real_malloc_addr = ((Thread*) p)->_real_malloc_address; 202 void* real_malloc_addr = ((Thread*) p)->_real_malloc_address;
200 CHeapObj::operator delete(real_malloc_addr); 203 FreeHeap(real_malloc_addr, mtThread);
201 } else { 204 } else {
202 CHeapObj::operator delete(p); 205 FreeHeap(p, mtThread);
203 } 206 }
204 } 207 }
205 208
206 209
207 // Base class for all threads: VMThread, WatcherThread, ConcurrentMarkSweepThread, 210 // Base class for all threads: VMThread, WatcherThread, ConcurrentMarkSweepThread,
215 set_self_raw_id(0); 218 set_self_raw_id(0);
216 set_lgrp_id(-1); 219 set_lgrp_id(-1);
217 220
218 // allocated data structures 221 // allocated data structures
219 set_osthread(NULL); 222 set_osthread(NULL);
220 set_resource_area(new ResourceArea()); 223 set_resource_area(new (mtThread)ResourceArea());
221 set_handle_area(new HandleArea(NULL)); 224 set_handle_area(new (mtThread) HandleArea(NULL));
222 set_active_handles(NULL); 225 set_active_handles(NULL);
223 set_free_handle_block(NULL); 226 set_free_handle_block(NULL);
224 set_last_handle_mark(NULL); 227 set_last_handle_mark(NULL);
225 228
226 // This initial value ==> never claimed. 229 // This initial value ==> never claimed.
248 omFreeCount = 0 ; 251 omFreeCount = 0 ;
249 omFreeProvision = 32 ; 252 omFreeProvision = 32 ;
250 omInUseList = NULL ; 253 omInUseList = NULL ;
251 omInUseCount = 0 ; 254 omInUseCount = 0 ;
252 255
256 #ifdef ASSERT
257 _visited_for_critical_count = false;
258 #endif
259
253 _SR_lock = new Monitor(Mutex::suspend_resume, "SR_lock", true); 260 _SR_lock = new Monitor(Mutex::suspend_resume, "SR_lock", true);
254 _suspend_flags = 0; 261 _suspend_flags = 0;
255 262
256 // thread-specific hashCode stream generator state - Marsaglia shift-xor form 263 // thread-specific hashCode stream generator state - Marsaglia shift-xor form
257 _hashStateX = os::random() ; 264 _hashStateX = os::random() ;
303 // initialize structure dependent on thread local storage 310 // initialize structure dependent on thread local storage
304 ThreadLocalStorage::set_thread(this); 311 ThreadLocalStorage::set_thread(this);
305 312
306 // set up any platform-specific state. 313 // set up any platform-specific state.
307 os::initialize_thread(); 314 os::initialize_thread();
308
309 } 315 }
310 316
311 void Thread::record_stack_base_and_size() { 317 void Thread::record_stack_base_and_size() {
312 set_stack_base(os::current_stack_base()); 318 set_stack_base(os::current_stack_base());
313 set_stack_size(os::current_stack_size()); 319 set_stack_size(os::current_stack_size());
320
321 // record thread's native stack, stack grows downward
322 address vm_base = _stack_base - _stack_size;
323 MemTracker::record_virtual_memory_reserve(vm_base, _stack_size,
324 CURRENT_PC, this);
325 MemTracker::record_virtual_memory_type(vm_base, mtThreadStack);
314 } 326 }
315 327
316 328
317 Thread::~Thread() { 329 Thread::~Thread() {
318 // Reclaim the objectmonitors from the omFreeList of the moribund thread. 330 // Reclaim the objectmonitors from the omFreeList of the moribund thread.
319 ObjectSynchronizer::omFlush (this) ; 331 ObjectSynchronizer::omFlush (this) ;
332
333 MemTracker::record_virtual_memory_release((_stack_base - _stack_size),
334 _stack_size, this);
320 335
321 // deallocate data structures 336 // deallocate data structures
322 delete resource_area(); 337 delete resource_area();
323 // since the handle marks are using the handle area, we have to deallocated the root 338 // since the handle marks are using the handle area, we have to deallocated the root
324 // handle mark before deallocating the thread's handle area, 339 // handle mark before deallocating the thread's handle area,
993 JavaValue result(T_VOID); 1008 JavaValue result(T_VOID);
994 JavaCalls::call_static(&result, klass, vmSymbols::initializeSystemClass_name(), 1009 JavaCalls::call_static(&result, klass, vmSymbols::initializeSystemClass_name(),
995 vmSymbols::void_method_signature(), CHECK); 1010 vmSymbols::void_method_signature(), CHECK);
996 } 1011 }
997 1012
1013 char java_runtime_name[128] = "";
1014
1015 // extract the JRE name from sun.misc.Version.java_runtime_name
1016 static const char* get_java_runtime_name(TRAPS) {
1017 klassOop k = SystemDictionary::find(vmSymbols::sun_misc_Version(),
1018 Handle(), Handle(), CHECK_AND_CLEAR_NULL);
1019 fieldDescriptor fd;
1020 bool found = k != NULL &&
1021 instanceKlass::cast(k)->find_local_field(vmSymbols::java_runtime_name_name(),
1022 vmSymbols::string_signature(), &fd);
1023 if (found) {
1024 oop name_oop = k->java_mirror()->obj_field(fd.offset());
1025 if (name_oop == NULL)
1026 return NULL;
1027 const char* name = java_lang_String::as_utf8_string(name_oop,
1028 java_runtime_name,
1029 sizeof(java_runtime_name));
1030 return name;
1031 } else {
1032 return NULL;
1033 }
1034 }
1035
998 // General purpose hook into Java code, run once when the VM is initialized. 1036 // General purpose hook into Java code, run once when the VM is initialized.
999 // The Java library method itself may be changed independently from the VM. 1037 // The Java library method itself may be changed independently from the VM.
1000 static void call_postVMInitHook(TRAPS) { 1038 static void call_postVMInitHook(TRAPS) {
1001 klassOop k = SystemDictionary::PostVMInitHook_klass(); 1039 klassOop k = SystemDictionary::PostVMInitHook_klass();
1002 instanceKlassHandle klass (THREAD, k); 1040 instanceKlassHandle klass (THREAD, k);
1102 _processed_thread = NULL; 1140 _processed_thread = NULL;
1103 } 1141 }
1104 1142
1105 NamedThread::~NamedThread() { 1143 NamedThread::~NamedThread() {
1106 if (_name != NULL) { 1144 if (_name != NULL) {
1107 FREE_C_HEAP_ARRAY(char, _name); 1145 FREE_C_HEAP_ARRAY(char, _name, mtThread);
1108 _name = NULL; 1146 _name = NULL;
1109 } 1147 }
1110 } 1148 }
1111 1149
1112 void NamedThread::set_name(const char* format, ...) { 1150 void NamedThread::set_name(const char* format, ...) {
1113 guarantee(_name == NULL, "Only get to set name once."); 1151 guarantee(_name == NULL, "Only get to set name once.");
1114 _name = NEW_C_HEAP_ARRAY(char, max_name_len); 1152 _name = NEW_C_HEAP_ARRAY(char, max_name_len, mtThread);
1115 guarantee(_name != NULL, "alloc failure"); 1153 guarantee(_name != NULL, "alloc failure");
1116 va_list ap; 1154 va_list ap;
1117 va_start(ap, format); 1155 va_start(ap, format);
1118 jio_vsnprintf(_name, max_name_len, format, ap); 1156 jio_vsnprintf(_name, max_name_len, format, ap);
1119 va_end(ap); 1157 va_end(ap);
1294 set_deopt_nmethod(NULL); 1332 set_deopt_nmethod(NULL);
1295 clear_must_deopt_id(); 1333 clear_must_deopt_id();
1296 set_monitor_chunks(NULL); 1334 set_monitor_chunks(NULL);
1297 set_next(NULL); 1335 set_next(NULL);
1298 set_thread_state(_thread_new); 1336 set_thread_state(_thread_new);
1337 set_recorder(NULL);
1299 _terminated = _not_terminated; 1338 _terminated = _not_terminated;
1300 _privileged_stack_top = NULL; 1339 _privileged_stack_top = NULL;
1301 _array_for_gc = NULL; 1340 _array_for_gc = NULL;
1302 _suspend_equivalent = false; 1341 _suspend_equivalent = false;
1303 _in_deopt_handler = 0; 1342 _in_deopt_handler = 0;
1377 _jni_attach_state = _attaching_via_jni; 1416 _jni_attach_state = _attaching_via_jni;
1378 } else { 1417 } else {
1379 _jni_attach_state = _not_attaching_via_jni; 1418 _jni_attach_state = _not_attaching_via_jni;
1380 } 1419 }
1381 assert(_deferred_card_mark.is_empty(), "Default MemRegion ctor"); 1420 assert(_deferred_card_mark.is_empty(), "Default MemRegion ctor");
1421 _safepoint_visible = false;
1382 } 1422 }
1383 1423
1384 bool JavaThread::reguard_stack(address cur_sp) { 1424 bool JavaThread::reguard_stack(address cur_sp) {
1385 if (_stack_guard_state != stack_guard_yellow_disabled) { 1425 if (_stack_guard_state != stack_guard_yellow_disabled) {
1386 return true; // Stack already guarded or guard pages not needed. 1426 return true; // Stack already guarded or guard pages not needed.
1439 // %note runtime_23 1479 // %note runtime_23
1440 os::ThreadType thr_type = os::java_thread; 1480 os::ThreadType thr_type = os::java_thread;
1441 thr_type = entry_point == &compiler_thread_entry ? os::compiler_thread : 1481 thr_type = entry_point == &compiler_thread_entry ? os::compiler_thread :
1442 os::java_thread; 1482 os::java_thread;
1443 os::create_thread(this, thr_type, stack_sz); 1483 os::create_thread(this, thr_type, stack_sz);
1444 1484 _safepoint_visible = false;
1445 // The _osthread may be NULL here because we ran out of memory (too many threads active). 1485 // The _osthread may be NULL here because we ran out of memory (too many threads active).
1446 // We need to throw and OutOfMemoryError - however we cannot do this here because the caller 1486 // We need to throw and OutOfMemoryError - however we cannot do this here because the caller
1447 // may hold a lock and all locks must be unlocked before throwing the exception (throwing 1487 // may hold a lock and all locks must be unlocked before throwing the exception (throwing
1448 // the exception consists of creating the exception object & initializing it, initialization 1488 // the exception consists of creating the exception object & initializing it, initialization
1449 // will leave the VM via a JavaCall and then all locks must be unlocked). 1489 // will leave the VM via a JavaCall and then all locks must be unlocked).
1456 1496
1457 JavaThread::~JavaThread() { 1497 JavaThread::~JavaThread() {
1458 if (TraceThreadEvents) { 1498 if (TraceThreadEvents) {
1459 tty->print_cr("terminate thread %p", this); 1499 tty->print_cr("terminate thread %p", this);
1460 } 1500 }
1501
1502 // Info NMT that this JavaThread is exiting, its memory
1503 // recorder should be collected
1504 assert(!is_safepoint_visible(), "wrong state");
1505 MemTracker::thread_exiting(this);
1461 1506
1462 // JSR166 -- return the parker to the free list 1507 // JSR166 -- return the parker to the free list
1463 Parker::Release(_parker); 1508 Parker::Release(_parker);
1464 _parker = NULL ; 1509 _parker = NULL ;
1465 1510
2911 2956
2912 // JVMTI PopFrame support 2957 // JVMTI PopFrame support
2913 void JavaThread::popframe_preserve_args(ByteSize size_in_bytes, void* start) { 2958 void JavaThread::popframe_preserve_args(ByteSize size_in_bytes, void* start) {
2914 assert(_popframe_preserved_args == NULL, "should not wipe out old PopFrame preserved arguments"); 2959 assert(_popframe_preserved_args == NULL, "should not wipe out old PopFrame preserved arguments");
2915 if (in_bytes(size_in_bytes) != 0) { 2960 if (in_bytes(size_in_bytes) != 0) {
2916 _popframe_preserved_args = NEW_C_HEAP_ARRAY(char, in_bytes(size_in_bytes)); 2961 _popframe_preserved_args = NEW_C_HEAP_ARRAY(char, in_bytes(size_in_bytes), mtThread);
2917 _popframe_preserved_args_size = in_bytes(size_in_bytes); 2962 _popframe_preserved_args_size = in_bytes(size_in_bytes);
2918 Copy::conjoint_jbytes(start, _popframe_preserved_args, _popframe_preserved_args_size); 2963 Copy::conjoint_jbytes(start, _popframe_preserved_args, _popframe_preserved_args_size);
2919 } 2964 }
2920 } 2965 }
2921 2966
2933 return in_WordSize(sz / wordSize); 2978 return in_WordSize(sz / wordSize);
2934 } 2979 }
2935 2980
2936 void JavaThread::popframe_free_preserved_args() { 2981 void JavaThread::popframe_free_preserved_args() {
2937 assert(_popframe_preserved_args != NULL, "should not free PopFrame preserved arguments twice"); 2982 assert(_popframe_preserved_args != NULL, "should not free PopFrame preserved arguments twice");
2938 FREE_C_HEAP_ARRAY(char, (char*) _popframe_preserved_args); 2983 FREE_C_HEAP_ARRAY(char, (char*) _popframe_preserved_args, mtThread);
2939 _popframe_preserved_args = NULL; 2984 _popframe_preserved_args = NULL;
2940 _popframe_preserved_args_size = 0; 2985 _popframe_preserved_args_size = 0;
2941 } 2986 }
2942 2987
2943 #ifndef PRODUCT 2988 #ifndef PRODUCT
3169 3214
3170 // Initialize the os module after parsing the args 3215 // Initialize the os module after parsing the args
3171 jint os_init_2_result = os::init_2(); 3216 jint os_init_2_result = os::init_2();
3172 if (os_init_2_result != JNI_OK) return os_init_2_result; 3217 if (os_init_2_result != JNI_OK) return os_init_2_result;
3173 3218
3219 // intialize TLS
3220 ThreadLocalStorage::init();
3221
3222 // Bootstrap native memory tracking, so it can start recording memory
3223 // activities before worker thread is started. This is the first phase
3224 // of bootstrapping, VM is currently running in single-thread mode.
3225 MemTracker::bootstrap_single_thread();
3226
3174 // Initialize output stream logging 3227 // Initialize output stream logging
3175 ostream_init_log(); 3228 ostream_init_log();
3176 3229
3177 // Convert -Xrun to -agentlib: if there is no JVM_OnLoad 3230 // Convert -Xrun to -agentlib: if there is no JVM_OnLoad
3178 // Must be before create_vm_init_agents() 3231 // Must be before create_vm_init_agents()
3187 3240
3188 // Initialize Threads state 3241 // Initialize Threads state
3189 _thread_list = NULL; 3242 _thread_list = NULL;
3190 _number_of_threads = 0; 3243 _number_of_threads = 0;
3191 _number_of_non_daemon_threads = 0; 3244 _number_of_non_daemon_threads = 0;
3192
3193 // Initialize TLS
3194 ThreadLocalStorage::init();
3195 3245
3196 // Initialize global data structures and create system classes in heap 3246 // Initialize global data structures and create system classes in heap
3197 vm_init_globals(); 3247 vm_init_globals();
3198 3248
3199 // Attach the main thread to this os thread 3249 // Attach the main thread to this os thread
3222 main_thread->create_stack_guard_pages(); 3272 main_thread->create_stack_guard_pages();
3223 3273
3224 // Initialize Java-Level synchronization subsystem 3274 // Initialize Java-Level synchronization subsystem
3225 ObjectMonitor::Initialize() ; 3275 ObjectMonitor::Initialize() ;
3226 3276
3277 // Second phase of bootstrapping, VM is about entering multi-thread mode
3278 MemTracker::bootstrap_multi_thread();
3279
3227 // Initialize global modules 3280 // Initialize global modules
3228 jint status = init_globals(); 3281 jint status = init_globals();
3229 if (status != JNI_OK) { 3282 if (status != JNI_OK) {
3230 delete main_thread; 3283 delete main_thread;
3231 *canTryAgain = false; // don't let caller call JNI_CreateJavaVM again 3284 *canTryAgain = false; // don't let caller call JNI_CreateJavaVM again
3248 if (VerifyBeforeGC && 3301 if (VerifyBeforeGC &&
3249 Universe::heap()->total_collections() >= VerifyGCStartAt) { 3302 Universe::heap()->total_collections() >= VerifyGCStartAt) {
3250 Universe::heap()->prepare_for_verify(); 3303 Universe::heap()->prepare_for_verify();
3251 Universe::verify(); // make sure we're starting with a clean slate 3304 Universe::verify(); // make sure we're starting with a clean slate
3252 } 3305 }
3306
3307 // Fully start NMT
3308 MemTracker::start();
3253 3309
3254 // Create the VMThread 3310 // Create the VMThread
3255 { TraceTime timer("Start VMThread", TraceStartupTime); 3311 { TraceTime timer("Start VMThread", TraceStartupTime);
3256 VMThread::create(); 3312 VMThread::create();
3257 Thread* vmthread = VMThread::vm_thread(); 3313 Thread* vmthread = VMThread::vm_thread();
3358 initialize_class(vmSymbols::java_lang_reflect_Method(), CHECK_0); 3414 initialize_class(vmSymbols::java_lang_reflect_Method(), CHECK_0);
3359 initialize_class(vmSymbols::java_lang_ref_Finalizer(), CHECK_0); 3415 initialize_class(vmSymbols::java_lang_ref_Finalizer(), CHECK_0);
3360 // The VM creates & returns objects of this class. Make sure it's initialized. 3416 // The VM creates & returns objects of this class. Make sure it's initialized.
3361 initialize_class(vmSymbols::java_lang_Class(), CHECK_0); 3417 initialize_class(vmSymbols::java_lang_Class(), CHECK_0);
3362 call_initializeSystemClass(CHECK_0); 3418 call_initializeSystemClass(CHECK_0);
3419
3420 // get the Java runtime name after java.lang.System is initialized
3421 JDK_Version::set_runtime_name(get_java_runtime_name(THREAD));
3363 } else { 3422 } else {
3364 warning("java.lang.System not initialized"); 3423 warning("java.lang.System not initialized");
3365 } 3424 }
3366 3425
3367 // an instance of OutOfMemory exception has been allocated earlier 3426 // an instance of OutOfMemory exception has been allocated earlier
3474 // back-end can launch with -Xdebug -Xrunjdwp. 3533 // back-end can launch with -Xdebug -Xrunjdwp.
3475 if (!EagerXrunInit && Arguments::init_libraries_at_startup()) { 3534 if (!EagerXrunInit && Arguments::init_libraries_at_startup()) {
3476 create_vm_init_libraries(); 3535 create_vm_init_libraries();
3477 } 3536 }
3478 3537
3538 // Notify JVMTI agents that VM initialization is complete - nop if no agents.
3539 JvmtiExport::post_vm_initialized();
3540
3479 if (!TRACE_START()) { 3541 if (!TRACE_START()) {
3480 vm_exit_during_initialization(Handle(THREAD, PENDING_EXCEPTION)); 3542 vm_exit_during_initialization(Handle(THREAD, PENDING_EXCEPTION));
3481 } 3543 }
3482
3483 // Notify JVMTI agents that VM initialization is complete - nop if no agents.
3484 JvmtiExport::post_vm_initialized();
3485 3544
3486 if (CleanChunkPoolAsync) { 3545 if (CleanChunkPoolAsync) {
3487 Chunk::start_chunk_pool_cleaner_task(); 3546 Chunk::start_chunk_pool_cleaner_task();
3488 } 3547 }
3489 3548
3550 if (agent->is_absolute_path()) { 3609 if (agent->is_absolute_path()) {
3551 library = os::dll_load(name, ebuf, sizeof ebuf); 3610 library = os::dll_load(name, ebuf, sizeof ebuf);
3552 if (library == NULL) { 3611 if (library == NULL) {
3553 const char *sub_msg = " in absolute path, with error: "; 3612 const char *sub_msg = " in absolute path, with error: ";
3554 size_t len = strlen(msg) + strlen(name) + strlen(sub_msg) + strlen(ebuf) + 1; 3613 size_t len = strlen(msg) + strlen(name) + strlen(sub_msg) + strlen(ebuf) + 1;
3555 char *buf = NEW_C_HEAP_ARRAY(char, len); 3614 char *buf = NEW_C_HEAP_ARRAY(char, len, mtThread);
3556 jio_snprintf(buf, len, "%s%s%s%s", msg, name, sub_msg, ebuf); 3615 jio_snprintf(buf, len, "%s%s%s%s", msg, name, sub_msg, ebuf);
3557 // If we can't find the agent, exit. 3616 // If we can't find the agent, exit.
3558 vm_exit_during_initialization(buf, NULL); 3617 vm_exit_during_initialization(buf, NULL);
3559 FREE_C_HEAP_ARRAY(char, buf); 3618 FREE_C_HEAP_ARRAY(char, buf, mtThread);
3560 } 3619 }
3561 } else { 3620 } else {
3562 // Try to load the agent from the standard dll directory 3621 // Try to load the agent from the standard dll directory
3563 os::dll_build_name(buffer, sizeof(buffer), Arguments::get_dll_dir(), name); 3622 os::dll_build_name(buffer, sizeof(buffer), Arguments::get_dll_dir(), name);
3564 library = os::dll_load(buffer, ebuf, sizeof ebuf); 3623 library = os::dll_load(buffer, ebuf, sizeof ebuf);
3568 char *props = Arguments::get_kernel_properties(); 3627 char *props = Arguments::get_kernel_properties();
3569 char *home = Arguments::get_java_home(); 3628 char *home = Arguments::get_java_home();
3570 const char *fmt = "%s/bin/java %s -Dkernel.background.download=false" 3629 const char *fmt = "%s/bin/java %s -Dkernel.background.download=false"
3571 " sun.jkernel.DownloadManager -download client_jvm"; 3630 " sun.jkernel.DownloadManager -download client_jvm";
3572 size_t length = strlen(props) + strlen(home) + strlen(fmt) + 1; 3631 size_t length = strlen(props) + strlen(home) + strlen(fmt) + 1;
3573 char *cmd = NEW_C_HEAP_ARRAY(char, length); 3632 char *cmd = NEW_C_HEAP_ARRAY(char, length, mtThread);
3574 jio_snprintf(cmd, length, fmt, home, props); 3633 jio_snprintf(cmd, length, fmt, home, props);
3575 int status = os::fork_and_exec(cmd); 3634 int status = os::fork_and_exec(cmd);
3576 FreeHeap(props); 3635 FreeHeap(props);
3577 if (status == -1) { 3636 if (status == -1) {
3578 warning(cmd); 3637 warning(cmd);
3579 vm_exit_during_initialization("fork_and_exec failed: %s", 3638 vm_exit_during_initialization("fork_and_exec failed: %s",
3580 strerror(errno)); 3639 strerror(errno));
3581 } 3640 }
3582 FREE_C_HEAP_ARRAY(char, cmd); 3641 FREE_C_HEAP_ARRAY(char, cmd, mtThread);
3583 // when this comes back the instrument.dll should be where it belongs. 3642 // when this comes back the instrument.dll should be where it belongs.
3584 library = os::dll_load(buffer, ebuf, sizeof ebuf); 3643 library = os::dll_load(buffer, ebuf, sizeof ebuf);
3585 } 3644 }
3586 #endif // KERNEL 3645 #endif // KERNEL
3587 if (library == NULL) { // Try the local directory 3646 if (library == NULL) { // Try the local directory
3589 os::dll_build_name(buffer, sizeof(buffer), ns, name); 3648 os::dll_build_name(buffer, sizeof(buffer), ns, name);
3590 library = os::dll_load(buffer, ebuf, sizeof ebuf); 3649 library = os::dll_load(buffer, ebuf, sizeof ebuf);
3591 if (library == NULL) { 3650 if (library == NULL) {
3592 const char *sub_msg = " on the library path, with error: "; 3651 const char *sub_msg = " on the library path, with error: ";
3593 size_t len = strlen(msg) + strlen(name) + strlen(sub_msg) + strlen(ebuf) + 1; 3652 size_t len = strlen(msg) + strlen(name) + strlen(sub_msg) + strlen(ebuf) + 1;
3594 char *buf = NEW_C_HEAP_ARRAY(char, len); 3653 char *buf = NEW_C_HEAP_ARRAY(char, len, mtThread);
3595 jio_snprintf(buf, len, "%s%s%s%s", msg, name, sub_msg, ebuf); 3654 jio_snprintf(buf, len, "%s%s%s%s", msg, name, sub_msg, ebuf);
3596 // If we can't find the agent, exit. 3655 // If we can't find the agent, exit.
3597 vm_exit_during_initialization(buf, NULL); 3656 vm_exit_during_initialization(buf, NULL);
3598 FREE_C_HEAP_ARRAY(char, buf); 3657 FREE_C_HEAP_ARRAY(char, buf, mtThread);
3599 } 3658 }
3600 } 3659 }
3601 } 3660 }
3602 agent->set_os_lib(library); 3661 agent->set_os_lib(library);
3603 } 3662 }
3762 // there is a serious error in VM. The two shutdown paths are not exactly 3821 // there is a serious error in VM. The two shutdown paths are not exactly
3763 // the same, but they share Shutdown.shutdown() at Java level and before_exit() 3822 // the same, but they share Shutdown.shutdown() at Java level and before_exit()
3764 // and VM_Exit op at VM level. 3823 // and VM_Exit op at VM level.
3765 // 3824 //
3766 // Shutdown sequence: 3825 // Shutdown sequence:
3826 // + Shutdown native memory tracking if it is on
3767 // + Wait until we are the last non-daemon thread to execute 3827 // + Wait until we are the last non-daemon thread to execute
3768 // <-- every thing is still working at this moment --> 3828 // <-- every thing is still working at this moment -->
3769 // + Call java.lang.Shutdown.shutdown(), which will invoke Java level 3829 // + Call java.lang.Shutdown.shutdown(), which will invoke Java level
3770 // shutdown hooks, run finalizers if finalization-on-exit 3830 // shutdown hooks, run finalizers if finalization-on-exit
3771 // + Call before_exit(), prepare for VM exit 3831 // + Call before_exit(), prepare for VM exit
3807 // 3867 //
3808 Threads_lock->wait(!Mutex::_no_safepoint_check_flag, 0, 3868 Threads_lock->wait(!Mutex::_no_safepoint_check_flag, 0,
3809 Mutex::_as_suspend_equivalent_flag); 3869 Mutex::_as_suspend_equivalent_flag);
3810 } 3870 }
3811 3871
3872 // Shutdown NMT before exit. Otherwise,
3873 // it will run into trouble when system destroys static variables.
3874 MemTracker::shutdown(MemTracker::NMT_normal);
3875
3812 // Hang forever on exit if we are reporting an error. 3876 // Hang forever on exit if we are reporting an error.
3813 if (ShowMessageBoxOnError && is_error_reported()) { 3877 if (ShowMessageBoxOnError && is_error_reported()) {
3814 os::infinite_sleep(); 3878 os::infinite_sleep();
3815 } 3879 }
3816 os::wait_for_keypress_at_exit(); 3880 os::wait_for_keypress_at_exit();
3913 if ((!force_daemon) && (threadObj == NULL || !java_lang_Thread::is_daemon(threadObj))) { 3977 if ((!force_daemon) && (threadObj == NULL || !java_lang_Thread::is_daemon(threadObj))) {
3914 _number_of_non_daemon_threads++; 3978 _number_of_non_daemon_threads++;
3915 daemon = false; 3979 daemon = false;
3916 } 3980 }
3917 3981
3982 p->set_safepoint_visible(true);
3983
3918 ThreadService::add_thread(p, daemon); 3984 ThreadService::add_thread(p, daemon);
3919 3985
3920 // Possible GC point. 3986 // Possible GC point.
3921 Events::log(p, "Thread added: " INTPTR_FORMAT, p); 3987 Events::log(p, "Thread added: " INTPTR_FORMAT, p);
3922 } 3988 }
3958 // Make sure that safepoint code disregard this thread. This is needed since 4024 // Make sure that safepoint code disregard this thread. This is needed since
3959 // the thread might mess around with locks after this point. This can cause it 4025 // the thread might mess around with locks after this point. This can cause it
3960 // to do callbacks into the safepoint code. However, the safepoint code is not aware 4026 // to do callbacks into the safepoint code. However, the safepoint code is not aware
3961 // of this thread since it is removed from the queue. 4027 // of this thread since it is removed from the queue.
3962 p->set_terminated_value(); 4028 p->set_terminated_value();
4029
4030 // Now, this thread is not visible to safepoint
4031 p->set_safepoint_visible(false);
4032
3963 } // unlock Threads_lock 4033 } // unlock Threads_lock
3964 4034
3965 // Since Events::log uses a lock, we grab it outside the Threads_lock 4035 // Since Events::log uses a lock, we grab it outside the Threads_lock
3966 Events::log(p, "Thread exited: " INTPTR_FORMAT, p); 4036 Events::log(p, "Thread exited: " INTPTR_FORMAT, p);
3967 } 4037 }