1145 } 1146 } 1147 1148 void NamedThread::set_name(const char* format, ...) { 1149 guarantee(_name == NULL, "Only get to set name once."); 1150 _name = NEW_C_HEAP_ARRAY(char, max_name_len, mtThread); 1151 guarantee(_name != NULL, "alloc failure"); 1152 va_list ap; 1153 va_start(ap, format); 1154 jio_vsnprintf(_name, max_name_len, format, ap); 1155 va_end(ap); 1156 } 1157 1158 // ======= WatcherThread ======== 1159 1160 // The watcher thread exists to simulate timer interrupts. It should 1161 // be replaced by an abstraction over whatever native support for 1162 // timer interrupts exists on the platform. 1163 1164 WatcherThread* WatcherThread::_watcher_thread = NULL; 1165 volatile bool WatcherThread::_should_terminate = false; 1166 1167 WatcherThread::WatcherThread() : Thread() { 1168 assert(watcher_thread() == NULL, "we can only allocate one WatcherThread"); 1169 if (os::create_thread(this, os::watcher_thread)) { 1170 _watcher_thread = this; 1171 1172 // Set the watcher thread to the highest OS priority which should not be 1173 // used, unless a Java thread with priority java.lang.Thread.MAX_PRIORITY 1174 // is created. The only normal thread using this priority is the reference 1175 // handler thread, which runs for very short intervals only. 1176 // If the VMThread's priority is not lower than the WatcherThread profiling 1177 // will be inaccurate. 1178 os::set_priority(this, MaxPriority); 1179 if (!DisableStartThread) { 1180 os::start_thread(this); 1181 } 1182 } 1183 } 1184 1185 void WatcherThread::run() { 1186 assert(this == watcher_thread(), "just checking"); 1187 1188 this->record_stack_base_and_size(); 1189 this->initialize_thread_local_storage(); 1190 this->set_active_handles(JNIHandleBlock::allocate_block()); 1191 while(!_should_terminate) { 1192 assert(watcher_thread() == Thread::current(), "thread consistency check"); 1193 assert(watcher_thread() == this, "thread consistency check"); 1194 1195 // Calculate how long it'll be until the next PeriodicTask work 1196 // should be done, and sleep that amount of time. 1197 size_t time_to_wait = PeriodicTask::time_to_wait(); 1198 1199 // we expect this to timeout - we only ever get unparked when 1200 // we should terminate 1201 { 1202 OSThreadWaitState osts(this->osthread(), false /* not Object.wait() */); 1203 1204 jlong prev_time = os::javaTimeNanos(); 1205 for (;;) { 1206 int res= _SleepEvent->park(time_to_wait); 1207 if (res == OS_TIMEOUT || _should_terminate) 1208 break; 1209 // spurious wakeup of some kind 1210 jlong now = os::javaTimeNanos(); 1211 time_to_wait -= (now - prev_time) / 1000000; 1212 if (time_to_wait <= 0) 1213 break; 1214 prev_time = now; 1215 } 1216 } 1217 1218 if (is_error_reported()) { 1219 // A fatal error has happened, the error handler(VMError::report_and_die) 1220 // should abort JVM after creating an error log file. However in some 1221 // rare cases, the error handler itself might deadlock. Here we try to 1222 // kill JVM if the fatal error handler fails to abort in 2 minutes. 1223 // 1224 // This code is in WatcherThread because WatcherThread wakes up 1225 // periodically so the fatal error handler doesn't need to do anything; 1226 // also because the WatcherThread is less likely to crash than other 1227 // threads. 1228 1229 for (;;) { 1230 if (!ShowMessageBoxOnError 1231 && (OnError == NULL || OnError[0] == '\0') 1232 && Arguments::abort_hook() == NULL) { 1233 os::sleep(this, 2 * 60 * 1000, false); 1234 fdStream err(defaultStream::output_fd()); 1235 err.print_raw_cr("# [ timer expired, abort... ]"); 1236 // skip atexit/vm_exit/vm_abort hooks 1237 os::die(); 1238 } 1239 1240 // Wake up 5 seconds later, the fatal handler may reset OnError or 1241 // ShowMessageBoxOnError when it is ready to abort. 1242 os::sleep(this, 5 * 1000, false); 1243 } 1244 } 1245 1246 PeriodicTask::real_time_tick(time_to_wait); 1247 1248 // If we have no more tasks left due to dynamic disenrollment, 1249 // shut down the thread since we don't currently support dynamic enrollment 1250 if (PeriodicTask::num_tasks() == 0) { 1251 _should_terminate = true; 1252 } 1253 } 1254 1255 // Signal that it is terminated 1256 { 1257 MutexLockerEx mu(Terminator_lock, Mutex::_no_safepoint_check_flag); 1258 _watcher_thread = NULL; 1259 Terminator_lock->notify(); 1260 } 1261 1262 // Thread destructor usually does this.. 1263 ThreadLocalStorage::set_thread(NULL); 1264 } 1265 1266 void WatcherThread::start() { 1267 if (watcher_thread() == NULL) { 1268 _should_terminate = false; 1269 // Create the single instance of WatcherThread 1270 new WatcherThread(); 1271 } 1272 } 1273 1274 void WatcherThread::stop() { 1275 // it is ok to take late safepoints here, if needed 1276 MutexLocker mu(Terminator_lock); 1277 _should_terminate = true; 1278 OrderAccess::fence(); // ensure WatcherThread sees update in main loop 1279 1280 Thread* watcher = watcher_thread(); 1281 if (watcher != NULL) 1282 watcher->_SleepEvent->unpark(); 1283 1284 while(watcher_thread() != NULL) { 1285 // This wait should make safepoint checks, wait without a timeout, 1286 // and wait as a suspend-equivalent condition. 1287 // 1288 // Note: If the FlatProfiler is running, then this thread is waiting 1289 // for the WatcherThread to terminate and the WatcherThread, via the 1290 // FlatProfiler task, is waiting for the external suspend request on 1291 // this thread to complete. wait_for_ext_suspend_completion() will 1292 // eventually timeout, but that takes time. Making this wait a 1293 // suspend-equivalent condition solves that timeout problem. 1294 // 1295 Terminator_lock->wait(!Mutex::_no_safepoint_check_flag, 0, 1296 Mutex::_as_suspend_equivalent_flag); 1297 } 1298 } 1299 1300 void WatcherThread::print_on(outputStream* st) const { 1301 st->print("\"%s\" ", name()); 1302 Thread::print_on(st); 1303 st->cr(); 1304 } 1305 1306 // ======= JavaThread ======== 1307 1308 // A JavaThread is a normal Java thread 1309 1310 void JavaThread::initialize() { 1311 // Initialize fields 1312 1313 // Set the claimed par_id to -1 (ie not claiming any par_ids) 1314 set_claimed_par_id(-1); 1315 1316 set_saved_exception_pc(NULL); 1317 set_threadObj(NULL); 1318 _anchor.clear(); 1319 set_entry_point(NULL); 3546 vm_exit(1); 3547 } 3548 3549 if (Arguments::has_profile()) FlatProfiler::engage(main_thread, true); 3550 if (Arguments::has_alloc_profile()) AllocationProfiler::engage(); 3551 if (MemProfiling) MemProfiler::engage(); 3552 StatSampler::engage(); 3553 if (CheckJNICalls) JniPeriodicChecker::engage(); 3554 3555 BiasedLocking::init(); 3556 3557 if (JDK_Version::current().post_vm_init_hook_enabled()) { 3558 call_postVMInitHook(THREAD); 3559 // The Java side of PostVMInitHook.run must deal with all 3560 // exceptions and provide means of diagnosis. 3561 if (HAS_PENDING_EXCEPTION) { 3562 CLEAR_PENDING_EXCEPTION; 3563 } 3564 } 3565 3566 // Start up the WatcherThread if there are any periodic tasks 3567 // NOTE: All PeriodicTasks should be registered by now. If they 3568 // aren't, late joiners might appear to start slowly (we might 3569 // take a while to process their first tick). 3570 if (PeriodicTask::num_tasks() > 0) { 3571 WatcherThread::start(); 3572 } 3573 3574 // Give os specific code one last chance to start 3575 os::init_3(); 3576 3577 create_vm_timer.end(); 3578 return JNI_OK; 3579 } 3580 3581 // type for the Agent_OnLoad and JVM_OnLoad entry points 3582 extern "C" { 3583 typedef jint (JNICALL *OnLoadEntry_t)(JavaVM *, char *, void *); 3584 } 3585 // Find a command line agent library and return its entry point for 3586 // -agentlib: -agentpath: -Xrun 3587 // num_symbol_entries must be passed-in since only the caller knows the number of symbols in the array. 3588 static OnLoadEntry_t lookup_on_load(AgentLibrary* agent, const char *on_load_symbols[], size_t num_symbol_entries) { 3589 OnLoadEntry_t on_load_entry = NULL; 3590 void *library = agent->os_lib(); // check if we have looked it up before 3591 3592 if (library == NULL) { | 1145 } 1146 } 1147 1148 void NamedThread::set_name(const char* format, ...) { 1149 guarantee(_name == NULL, "Only get to set name once."); 1150 _name = NEW_C_HEAP_ARRAY(char, max_name_len, mtThread); 1151 guarantee(_name != NULL, "alloc failure"); 1152 va_list ap; 1153 va_start(ap, format); 1154 jio_vsnprintf(_name, max_name_len, format, ap); 1155 va_end(ap); 1156 } 1157 1158 // ======= WatcherThread ======== 1159 1160 // The watcher thread exists to simulate timer interrupts. It should 1161 // be replaced by an abstraction over whatever native support for 1162 // timer interrupts exists on the platform. 1163 1164 WatcherThread* WatcherThread::_watcher_thread = NULL; 1165 bool WatcherThread::_startable = false; 1166 volatile bool WatcherThread::_should_terminate = false; 1167 1168 WatcherThread::WatcherThread() : Thread() { 1169 assert(watcher_thread() == NULL, "we can only allocate one WatcherThread"); 1170 if (os::create_thread(this, os::watcher_thread)) { 1171 _watcher_thread = this; 1172 1173 // Set the watcher thread to the highest OS priority which should not be 1174 // used, unless a Java thread with priority java.lang.Thread.MAX_PRIORITY 1175 // is created. The only normal thread using this priority is the reference 1176 // handler thread, which runs for very short intervals only. 1177 // If the VMThread's priority is not lower than the WatcherThread profiling 1178 // will be inaccurate. 1179 os::set_priority(this, MaxPriority); 1180 if (!DisableStartThread) { 1181 os::start_thread(this); 1182 } 1183 } 1184 } 1185 1186 jint WatcherThread::sleep() const { 1187 MutexLockerEx ml(PeriodicTask_lock, Mutex::_no_safepoint_check_flag); 1188 1189 // remaining will be zero if there are no tasks, 1190 // causing the WatcherThread to sleep until a task is 1191 // enrolled 1192 jint remaining = PeriodicTask::time_to_wait(); 1193 jint time_slept = 0; 1194 1195 // we expect this to timeout - we only ever get unparked when 1196 // we should terminate or when a new task has been enrolled 1197 OSThreadWaitState osts(this->osthread(), false /* not Object.wait() */); 1198 1199 jlong time_before_loop = os::javaTimeNanos(); 1200 1201 for (;;) { 1202 bool timedout = PeriodicTask_lock->wait(Mutex::_no_safepoint_check_flag, remaining); 1203 jlong now = os::javaTimeNanos(); 1204 1205 if (remaining == 0) { 1206 // if we didn't have any tasks we could have waited for a long time 1207 // consider the time_slept zero and reset time_before_loop 1208 time_slept = 0; 1209 time_before_loop = now; 1210 } else { 1211 // need to recalulate since we might have new tasks in _tasks 1212 time_slept = (jint) ((now - time_before_loop) / 1000000); 1213 } 1214 1215 // Change to task list or spurious wakeup of some kind 1216 if (timedout || _should_terminate) { 1217 break; 1218 } 1219 1220 remaining = PeriodicTask::time_to_wait(); 1221 if (remaining == 0) { 1222 // Last task was just disenrolled so loop around and wait until 1223 // another task gets enrolled 1224 continue; 1225 } 1226 1227 remaining -= time_slept; 1228 if (remaining <= 0) 1229 break; 1230 } 1231 1232 return time_slept; 1233 } 1234 1235 void WatcherThread::run() { 1236 assert(this == watcher_thread(), "just checking"); 1237 1238 this->record_stack_base_and_size(); 1239 this->initialize_thread_local_storage(); 1240 this->set_active_handles(JNIHandleBlock::allocate_block()); 1241 while(!_should_terminate) { 1242 assert(watcher_thread() == Thread::current(), "thread consistency check"); 1243 assert(watcher_thread() == this, "thread consistency check"); 1244 1245 // Calculate how long it'll be until the next PeriodicTask work 1246 // should be done, and sleep that amount of time. 1247 jint time_waited = sleep(); 1248 1249 if (is_error_reported()) { 1250 // A fatal error has happened, the error handler(VMError::report_and_die) 1251 // should abort JVM after creating an error log file. However in some 1252 // rare cases, the error handler itself might deadlock. Here we try to 1253 // kill JVM if the fatal error handler fails to abort in 2 minutes. 1254 // 1255 // This code is in WatcherThread because WatcherThread wakes up 1256 // periodically so the fatal error handler doesn't need to do anything; 1257 // also because the WatcherThread is less likely to crash than other 1258 // threads. 1259 1260 for (;;) { 1261 if (!ShowMessageBoxOnError 1262 && (OnError == NULL || OnError[0] == '\0') 1263 && Arguments::abort_hook() == NULL) { 1264 os::sleep(this, 2 * 60 * 1000, false); 1265 fdStream err(defaultStream::output_fd()); 1266 err.print_raw_cr("# [ timer expired, abort... ]"); 1267 // skip atexit/vm_exit/vm_abort hooks 1268 os::die(); 1269 } 1270 1271 // Wake up 5 seconds later, the fatal handler may reset OnError or 1272 // ShowMessageBoxOnError when it is ready to abort. 1273 os::sleep(this, 5 * 1000, false); 1274 } 1275 } 1276 1277 PeriodicTask::real_time_tick(time_waited); 1278 } 1279 1280 // Signal that it is terminated 1281 { 1282 MutexLockerEx mu(Terminator_lock, Mutex::_no_safepoint_check_flag); 1283 _watcher_thread = NULL; 1284 Terminator_lock->notify(); 1285 } 1286 1287 // Thread destructor usually does this.. 1288 ThreadLocalStorage::set_thread(NULL); 1289 } 1290 1291 void WatcherThread::start() { 1292 assert(PeriodicTask_lock->owned_by_self(), "PeriodicTask_lock required"); 1293 1294 if (watcher_thread() == NULL && _startable) { 1295 _should_terminate = false; 1296 // Create the single instance of WatcherThread 1297 new WatcherThread(); 1298 } 1299 } 1300 1301 void WatcherThread::make_startable() { 1302 assert(PeriodicTask_lock->owned_by_self(), "PeriodicTask_lock required"); 1303 _startable = true; 1304 } 1305 1306 void WatcherThread::stop() { 1307 { 1308 MutexLockerEx ml(PeriodicTask_lock, Mutex::_no_safepoint_check_flag); 1309 _should_terminate = true; 1310 OrderAccess::fence(); // ensure WatcherThread sees update in main loop 1311 1312 WatcherThread* watcher = watcher_thread(); 1313 if (watcher != NULL) 1314 watcher->unpark(); 1315 } 1316 1317 // it is ok to take late safepoints here, if needed 1318 MutexLocker mu(Terminator_lock); 1319 1320 while(watcher_thread() != NULL) { 1321 // This wait should make safepoint checks, wait without a timeout, 1322 // and wait as a suspend-equivalent condition. 1323 // 1324 // Note: If the FlatProfiler is running, then this thread is waiting 1325 // for the WatcherThread to terminate and the WatcherThread, via the 1326 // FlatProfiler task, is waiting for the external suspend request on 1327 // this thread to complete. wait_for_ext_suspend_completion() will 1328 // eventually timeout, but that takes time. Making this wait a 1329 // suspend-equivalent condition solves that timeout problem. 1330 // 1331 Terminator_lock->wait(!Mutex::_no_safepoint_check_flag, 0, 1332 Mutex::_as_suspend_equivalent_flag); 1333 } 1334 } 1335 1336 void WatcherThread::unpark() { 1337 MutexLockerEx ml(PeriodicTask_lock->owned_by_self() ? NULL : PeriodicTask_lock, Mutex::_no_safepoint_check_flag); 1338 PeriodicTask_lock->notify(); 1339 } 1340 1341 void WatcherThread::print_on(outputStream* st) const { 1342 st->print("\"%s\" ", name()); 1343 Thread::print_on(st); 1344 st->cr(); 1345 } 1346 1347 // ======= JavaThread ======== 1348 1349 // A JavaThread is a normal Java thread 1350 1351 void JavaThread::initialize() { 1352 // Initialize fields 1353 1354 // Set the claimed par_id to -1 (ie not claiming any par_ids) 1355 set_claimed_par_id(-1); 1356 1357 set_saved_exception_pc(NULL); 1358 set_threadObj(NULL); 1359 _anchor.clear(); 1360 set_entry_point(NULL); 3587 vm_exit(1); 3588 } 3589 3590 if (Arguments::has_profile()) FlatProfiler::engage(main_thread, true); 3591 if (Arguments::has_alloc_profile()) AllocationProfiler::engage(); 3592 if (MemProfiling) MemProfiler::engage(); 3593 StatSampler::engage(); 3594 if (CheckJNICalls) JniPeriodicChecker::engage(); 3595 3596 BiasedLocking::init(); 3597 3598 if (JDK_Version::current().post_vm_init_hook_enabled()) { 3599 call_postVMInitHook(THREAD); 3600 // The Java side of PostVMInitHook.run must deal with all 3601 // exceptions and provide means of diagnosis. 3602 if (HAS_PENDING_EXCEPTION) { 3603 CLEAR_PENDING_EXCEPTION; 3604 } 3605 } 3606 3607 { 3608 MutexLockerEx ml(PeriodicTask_lock, Mutex::_no_safepoint_check_flag); 3609 // Make sure the watcher thread can be started by WatcherThread::start() 3610 // or by dynamic enrollment. 3611 WatcherThread::make_startable(); 3612 // Start up the WatcherThread if there are any periodic tasks 3613 // NOTE: All PeriodicTasks should be registered by now. If they 3614 // aren't, late joiners might appear to start slowly (we might 3615 // take a while to process their first tick). 3616 if (PeriodicTask::num_tasks() > 0) { 3617 WatcherThread::start(); 3618 } 3619 } 3620 3621 // Give os specific code one last chance to start 3622 os::init_3(); 3623 3624 create_vm_timer.end(); 3625 return JNI_OK; 3626 } 3627 3628 // type for the Agent_OnLoad and JVM_OnLoad entry points 3629 extern "C" { 3630 typedef jint (JNICALL *OnLoadEntry_t)(JavaVM *, char *, void *); 3631 } 3632 // Find a command line agent library and return its entry point for 3633 // -agentlib: -agentpath: -Xrun 3634 // num_symbol_entries must be passed-in since only the caller knows the number of symbols in the array. 3635 static OnLoadEntry_t lookup_on_load(AgentLibrary* agent, const char *on_load_symbols[], size_t num_symbol_entries) { 3636 OnLoadEntry_t on_load_entry = NULL; 3637 void *library = agent->os_lib(); // check if we have looked it up before 3638 3639 if (library == NULL) { |