src/os/linux/vm/os_linux.cpp

Print this page
rev 4773 : 8005849: JEP 167: Event-Based JVM Tracing
Reviewed-by: acorn, coleenp, sla
Contributed-by: Karen Kinnear <karen.kinnear@oracle.com>, Bengt Rutisson <bengt.rutisson@oracle.com>, Calvin Cheung <calvin.cheung@oracle.com>, Erik Gahlin <erik.gahlin@oracle.com>, Erik Helin <erik.helin@oracle.com>, Jesper Wilhelmsson <jesper.wilhelmsson@oracle.com>, Keith McGuigan <keith.mcguigan@oracle.com>, Mattias Tobiasson <mattias.tobiasson@oracle.com>, Markus Gronlund <markus.gronlund@oracle.com>, Mikael Auno <mikael.auno@oracle.com>, Nils Eliasson <nils.eliasson@oracle.com>, Nils Loodin <nils.loodin@oracle.com>, Rickard Backman <rickard.backman@oracle.com>, Staffan Larsen <staffan.larsen@oracle.com>, Stefan Karlsson <stefan.karlsson@oracle.com>, Yekaterina Kantserova <yekaterina.kantserova@oracle.com>


 128 
 129 static jlong initial_time_count=0;
 130 
 131 static int clock_tics_per_sec = 100;
 132 
 133 // For diagnostics to print a message once. see run_periodic_checks
 134 static sigset_t check_signal_done;
 135 static bool check_signals = true;;
 136 
 137 static pid_t _initial_pid = 0;
 138 
 139 /* Signal number used to suspend/resume a thread */
 140 
 141 /* do not use any signal number less than SIGSEGV, see 4355769 */
 142 static int SR_signum = SIGUSR2;
 143 sigset_t SR_sigset;
 144 
 145 /* Used to protect dlsym() calls */
 146 static pthread_mutex_t dl_mutex;
 147 



 148 #ifdef JAVASE_EMBEDDED
 149 class MemNotifyThread: public Thread {
 150   friend class VMStructs;
 151  public:
 152   virtual void run();
 153 
 154  private:
 155   static MemNotifyThread* _memnotify_thread;
 156   int _fd;
 157 
 158  public:
 159 
 160   // Constructor
 161   MemNotifyThread(int fd);
 162 
 163   // Tester
 164   bool is_memnotify_thread() const { return true; }
 165 
 166   // Printing
 167   char* name() const { return (char*)"Linux MemNotify Thread"; }


2380 UserHandler(int sig, void *siginfo, void *context) {
2381   // 4511530 - sem_post is serialized and handled by the manager thread. When
2382   // the program is interrupted by Ctrl-C, SIGINT is sent to every thread. We
2383   // don't want to flood the manager thread with sem_post requests.
2384   if (sig == SIGINT && Atomic::add(1, &sigint_count) > 1)
2385       return;
2386 
2387   // Ctrl-C is pressed during error reporting, likely because the error
2388   // handler fails to abort. Let VM die immediately.
2389   if (sig == SIGINT && is_error_reported()) {
2390      os::die();
2391   }
2392 
2393   os::signal_notify(sig);
2394 }
2395 
2396 void* os::user_handler() {
2397   return CAST_FROM_FN_PTR(void*, UserHandler);
2398 }
2399 



















































2400 extern "C" {
2401   typedef void (*sa_handler_t)(int);
2402   typedef void (*sa_sigaction_t)(int, siginfo_t *, void *);
2403 }
2404 
2405 void* os::signal(int signal_number, void* handler) {
2406   struct sigaction sigAct, oldSigAct;
2407 
2408   sigfillset(&(sigAct.sa_mask));
2409   sigAct.sa_flags   = SA_RESTART|SA_SIGINFO;
2410   sigAct.sa_handler = CAST_TO_FN_PTR(sa_handler_t, handler);
2411 
2412   if (sigaction(signal_number, &sigAct, &oldSigAct)) {
2413     // -1 means registration failed
2414     return (void *)-1;
2415   }
2416 
2417   return CAST_FROM_FN_PTR(void*, oldSigAct.sa_handler);
2418 }
2419 
2420 void os::signal_raise(int signal_number) {
2421   ::raise(signal_number);
2422 }
2423 
2424 /*
2425  * The following code is moved from os.cpp for making this
2426  * code platform specific, which it is by its very nature.
2427  */
2428 
2429 // Will be modified when max signal is changed to be dynamic
2430 int os::sigexitnum_pd() {
2431   return NSIG;
2432 }
2433 
2434 // a counter for each possible signal value
2435 static volatile jint pending_signals[NSIG+1] = { 0 };
2436 
2437 // Linux(POSIX) specific hand shaking semaphore.
2438 static sem_t sig_sem;

2439 
2440 void os::signal_init_pd() {
2441   // Initialize signal structures
2442   ::memset((void*)pending_signals, 0, sizeof(pending_signals));
2443 
2444   // Initialize signal semaphore
2445   ::sem_init(&sig_sem, 0, 0);
2446 }
2447 
2448 void os::signal_notify(int sig) {
2449   Atomic::inc(&pending_signals[sig]);
2450   ::sem_post(&sig_sem);
2451 }
2452 
2453 static int check_pending_signals(bool wait) {
2454   Atomic::store(0, &sigint_count);
2455   for (;;) {
2456     for (int i = 0; i < NSIG + 1; i++) {
2457       jint n = pending_signals[i];
2458       if (n > 0 && n == Atomic::cmpxchg(n - 1, &pending_signals[i], n)) {


3532 //      that runs in the watcher thread.
3533 //  The remaining code is greatly simplified from the more general suspension
3534 //  code that used to be used.
3535 //
3536 //  The protocol is quite simple:
3537 //  - suspend:
3538 //      - sends a signal to the target thread
3539 //      - polls the suspend state of the osthread using a yield loop
3540 //      - target thread signal handler (SR_handler) sets suspend state
3541 //        and blocks in sigsuspend until continued
3542 //  - resume:
3543 //      - sets target osthread state to continue
3544 //      - sends signal to end the sigsuspend loop in the SR_handler
3545 //
3546 //  Note that the SR_lock plays no role in this suspend/resume protocol.
3547 //
3548 
3549 static void resume_clear_context(OSThread *osthread) {
3550   osthread->set_ucontext(NULL);
3551   osthread->set_siginfo(NULL);
3552 
3553   // notify the suspend action is completed, we have now resumed
3554   osthread->sr.clear_suspended();
3555 }
3556 
3557 static void suspend_save_context(OSThread *osthread, siginfo_t* siginfo, ucontext_t* context) {
3558   osthread->set_ucontext(context);
3559   osthread->set_siginfo(siginfo);
3560 }
3561 
3562 //
3563 // Handler function invoked when a thread's execution is suspended or
3564 // resumed. We have to be careful that only async-safe functions are
3565 // called here (Note: most pthread functions are not async safe and
3566 // should be avoided.)
3567 //
3568 // Note: sigwait() is a more natural fit than sigsuspend() from an
3569 // interface point of view, but sigwait() prevents the signal hander
3570 // from being run. libpthread would get very confused by not having
3571 // its signal handlers run and prevents sigwait()'s use with the
3572 // mutex granting granting signal.
3573 //
3574 // Currently only ever called on the VMThread
3575 //
3576 static void SR_handler(int sig, siginfo_t* siginfo, ucontext_t* context) {
3577   // Save and restore errno to avoid confusing native code with EINTR
3578   // after sigsuspend.
3579   int old_errno = errno;
3580 
3581   Thread* thread = Thread::current();
3582   OSThread* osthread = thread->osthread();
3583   assert(thread->is_VM_thread(), "Must be VMThread");
3584   // read current suspend action
3585   int action = osthread->sr.suspend_action();
3586   if (action == os::Linux::SuspendResume::SR_SUSPEND) {
3587     suspend_save_context(osthread, siginfo, context);
3588 
3589     // Notify the suspend action is about to be completed. do_suspend()
3590     // waits until SR_SUSPENDED is set and then returns. We will wait
3591     // here for a resume signal and that completes the suspend-other
3592     // action. do_suspend/do_resume is always called as a pair from
3593     // the same thread - so there are no races
3594 
3595     // notify the caller
3596     osthread->sr.set_suspended();

3597 



3598     sigset_t suspend_set;  // signals for sigsuspend()
3599 
3600     // get current set of blocked signals and unblock resume signal
3601     pthread_sigmask(SIG_BLOCK, NULL, &suspend_set);
3602     sigdelset(&suspend_set, SR_signum);
3603 

3604     // wait here until we are resumed
3605     do {
3606       sigsuspend(&suspend_set);
3607       // ignore all returns until we get a resume signal
3608     } while (osthread->sr.suspend_action() != os::Linux::SuspendResume::SR_CONTINUE);
3609 
3610     resume_clear_context(osthread);





3611 


3612   } else {
3613     assert(action == os::Linux::SuspendResume::SR_CONTINUE, "unexpected sr action");
3614     // nothing special to do - just leave the handler








3615   }
3616 
3617   errno = old_errno;
3618 }
3619 
3620 
3621 static int SR_initialize() {
3622   struct sigaction act;
3623   char *s;
3624   /* Get signal number to use for suspend/resume */
3625   if ((s = ::getenv("_JAVA_SR_SIGNUM")) != 0) {
3626     int sig = ::strtol(s, 0, 10);
3627     if (sig > 0 || sig < _NSIG) {
3628         SR_signum = sig;
3629     }
3630   }
3631 
3632   assert(SR_signum > SIGSEGV && SR_signum > SIGBUS,
3633         "SR_signum must be greater than max(SIGSEGV, SIGBUS), see 4355769");
3634 


3638   /* Set up signal handler for suspend/resume */
3639   act.sa_flags = SA_RESTART|SA_SIGINFO;
3640   act.sa_handler = (void (*)(int)) SR_handler;
3641 
3642   // SR_signum is blocked by default.
3643   // 4528190 - We also need to block pthread restart signal (32 on all
3644   // supported Linux platforms). Note that LinuxThreads need to block
3645   // this signal for all threads to work properly. So we don't have
3646   // to use hard-coded signal number when setting up the mask.
3647   pthread_sigmask(SIG_BLOCK, NULL, &act.sa_mask);
3648 
3649   if (sigaction(SR_signum, &act, 0) == -1) {
3650     return -1;
3651   }
3652 
3653   // Save signal flag
3654   os::Linux::set_our_sigflags(SR_signum, act.sa_flags);
3655   return 0;
3656 }
3657 











3658 
3659 // returns true on success and false on error - really an error is fatal
3660 // but this seems the normal response to library errors
3661 static bool do_suspend(OSThread* osthread) {
3662   // mark as suspended and send signal
3663   osthread->sr.set_suspend_action(os::Linux::SuspendResume::SR_SUSPEND);
3664   int status = pthread_kill(osthread->pthread_id(), SR_signum);
3665   assert_status(status == 0, status, "pthread_kill");
3666 
3667   // check status and wait until notified of suspension
3668   if (status == 0) {
3669     for (int i = 0; !osthread->sr.is_suspended(); i++) {
3670       os::yield_all(i);

3671     }
3672     osthread->sr.set_suspend_action(os::Linux::SuspendResume::SR_NONE);
3673     return true;

3674   }
3675   else {
3676     osthread->sr.set_suspend_action(os::Linux::SuspendResume::SR_NONE);














3677     return false;
3678   }





3679 }
3680 
3681 static void do_resume(OSThread* osthread) {
3682   assert(osthread->sr.is_suspended(), "thread should be suspended");
3683   osthread->sr.set_suspend_action(os::Linux::SuspendResume::SR_CONTINUE);
3684 
3685   int status = pthread_kill(osthread->pthread_id(), SR_signum);
3686   assert_status(status == 0, status, "pthread_kill");
3687   // check status and wait unit notified of resumption
3688   if (status == 0) {
3689     for (int i = 0; osthread->sr.is_suspended(); i++) {
3690       os::yield_all(i);






3691     }


3692   }
3693   osthread->sr.set_suspend_action(os::Linux::SuspendResume::SR_NONE);


3694 }
3695 
3696 ////////////////////////////////////////////////////////////////////////////////
3697 // interrupt support
3698 
3699 void os::interrupt(Thread* thread) {
3700   assert(Thread::current() == thread || Threads_lock->owned_by_self(),
3701     "possibility of dangling Thread pointer");
3702 
3703   OSThread* osthread = thread->osthread();
3704 
3705   if (!osthread->interrupted()) {
3706     osthread->set_interrupted(true);
3707     // More than one thread can get here with the same value of osthread,
3708     // resulting in multiple notifications.  We do, however, want the store
3709     // to interrupted() to be visible to other threads before we execute unpark().
3710     OrderAccess::fence();
3711     ParkEvent * const slp = thread->_SleepEvent ;
3712     if (slp != NULL) slp->unpark() ;
3713   }


4445   return online_cpus;
4446 }
4447 
4448 void os::set_native_thread_name(const char *name) {
4449   // Not yet implemented.
4450   return;
4451 }
4452 
4453 bool os::distribute_processes(uint length, uint* distribution) {
4454   // Not yet implemented.
4455   return false;
4456 }
4457 
4458 bool os::bind_to_processor(uint processor_id) {
4459   // Not yet implemented.
4460   return false;
4461 }
4462 
4463 ///
4464 
4465 // Suspends the target using the signal mechanism and then grabs the PC before
4466 // resuming the target. Used by the flat-profiler only
4467 ExtendedPC os::get_thread_pc(Thread* thread) {
4468   // Make sure that it is called by the watcher for the VMThread
4469   assert(Thread::current()->is_Watcher_thread(), "Must be watcher");
4470   assert(thread->is_VM_thread(), "Can only be called for VMThread");

4471 
4472   ExtendedPC epc;








4473 







4474   OSThread* osthread = thread->osthread();
4475   if (do_suspend(osthread)) {
4476     if (osthread->ucontext() != NULL) {
4477       epc = os::Linux::ucontext_get_pc(osthread->ucontext());
4478     } else {
4479       // NULL context is unexpected, double-check this is the VMThread
4480       guarantee(thread->is_VM_thread(), "can only be called for VMThread");
4481     }
4482     do_resume(osthread);
4483   }
4484   // failure means pthread_kill failed for some reason - arguably this is
4485   // a fatal problem, but such problems are ignored elsewhere




4486 
4487   return epc;


4488 }
4489 
4490 int os::Linux::safe_cond_timedwait(pthread_cond_t *_cond, pthread_mutex_t *_mutex, const struct timespec *_abstime)
4491 {
4492    if (is_NPTL()) {
4493       return pthread_cond_timedwait(_cond, _mutex, _abstime);
4494    } else {
4495       // 6292965: LinuxThreads pthread_cond_timedwait() resets FPU control
4496       // word back to default 64bit precision if condvar is signaled. Java
4497       // wants 53bit precision.  Save and restore current value.
4498       int fpu = get_fpu_control_word();
4499       int status = pthread_cond_timedwait(_cond, _mutex, _abstime);
4500       set_fpu_control_word(fpu);
4501       return status;
4502    }
4503 }
4504 
4505 ////////////////////////////////////////////////////////////////////////////////
4506 // debug support
4507 


5589       // We might want to do something like the following if we find the GC's are not helping...
5590       // Universe::heap()->size_policy()->set_gc_time_limit_exceeded(true);
5591     }
5592   }
5593 }
5594 
5595 //
5596 // See if the /dev/mem_notify device exists, and if so, start a thread to monitor it.
5597 //
5598 void MemNotifyThread::start() {
5599   int    fd;
5600   fd = open ("/dev/mem_notify", O_RDONLY, 0);
5601   if (fd < 0) {
5602       return;
5603   }
5604 
5605   if (memnotify_thread() == NULL) {
5606     new MemNotifyThread(fd);
5607   }
5608 }

5609 #endif // JAVASE_EMBEDDED


 128 
 129 static jlong initial_time_count=0;
 130 
 131 static int clock_tics_per_sec = 100;
 132 
 133 // For diagnostics to print a message once. see run_periodic_checks
 134 static sigset_t check_signal_done;
 135 static bool check_signals = true;;
 136 
 137 static pid_t _initial_pid = 0;
 138 
 139 /* Signal number used to suspend/resume a thread */
 140 
 141 /* do not use any signal number less than SIGSEGV, see 4355769 */
 142 static int SR_signum = SIGUSR2;
 143 sigset_t SR_sigset;
 144 
 145 /* Used to protect dlsym() calls */
 146 static pthread_mutex_t dl_mutex;
 147 
 148 // Declarations
 149 static void unpackTime(timespec* absTime, bool isAbsolute, jlong time);
 150 
 151 #ifdef JAVASE_EMBEDDED
 152 class MemNotifyThread: public Thread {
 153   friend class VMStructs;
 154  public:
 155   virtual void run();
 156 
 157  private:
 158   static MemNotifyThread* _memnotify_thread;
 159   int _fd;
 160 
 161  public:
 162 
 163   // Constructor
 164   MemNotifyThread(int fd);
 165 
 166   // Tester
 167   bool is_memnotify_thread() const { return true; }
 168 
 169   // Printing
 170   char* name() const { return (char*)"Linux MemNotify Thread"; }


2383 UserHandler(int sig, void *siginfo, void *context) {
2384   // 4511530 - sem_post is serialized and handled by the manager thread. When
2385   // the program is interrupted by Ctrl-C, SIGINT is sent to every thread. We
2386   // don't want to flood the manager thread with sem_post requests.
2387   if (sig == SIGINT && Atomic::add(1, &sigint_count) > 1)
2388       return;
2389 
2390   // Ctrl-C is pressed during error reporting, likely because the error
2391   // handler fails to abort. Let VM die immediately.
2392   if (sig == SIGINT && is_error_reported()) {
2393      os::die();
2394   }
2395 
2396   os::signal_notify(sig);
2397 }
2398 
2399 void* os::user_handler() {
2400   return CAST_FROM_FN_PTR(void*, UserHandler);
2401 }
2402 
2403 class Semaphore : public StackObj {
2404   public:
2405     Semaphore();
2406     ~Semaphore();
2407     void signal();
2408     void wait();
2409     bool trywait();
2410     bool timedwait(unsigned int sec, int nsec);
2411   private:
2412     sem_t _semaphore;
2413 };
2414 
2415 
2416 Semaphore::Semaphore() {
2417   sem_init(&_semaphore, 0, 0);
2418 }
2419 
2420 Semaphore::~Semaphore() {
2421   sem_destroy(&_semaphore);
2422 }
2423 
2424 void Semaphore::signal() {
2425   sem_post(&_semaphore);
2426 }
2427 
2428 void Semaphore::wait() {
2429   sem_wait(&_semaphore);
2430 }
2431 
2432 bool Semaphore::trywait() {
2433   return sem_trywait(&_semaphore) == 0;
2434 }
2435 
2436 bool Semaphore::timedwait(unsigned int sec, int nsec) {
2437   struct timespec ts;
2438   unpackTime(&ts, false, (sec * NANOSECS_PER_SEC) + nsec);
2439 
2440   while (1) {
2441     int result = sem_timedwait(&_semaphore, &ts);
2442     if (result == 0) {
2443       return true;
2444     } else if (errno == EINTR) {
2445       continue;
2446     } else if (errno == ETIMEDOUT) {
2447       return false;
2448     } else {
2449       return false;
2450     }
2451   }
2452 }
2453 
2454 extern "C" {
2455   typedef void (*sa_handler_t)(int);
2456   typedef void (*sa_sigaction_t)(int, siginfo_t *, void *);
2457 }
2458 
2459 void* os::signal(int signal_number, void* handler) {
2460   struct sigaction sigAct, oldSigAct;
2461 
2462   sigfillset(&(sigAct.sa_mask));
2463   sigAct.sa_flags   = SA_RESTART|SA_SIGINFO;
2464   sigAct.sa_handler = CAST_TO_FN_PTR(sa_handler_t, handler);
2465 
2466   if (sigaction(signal_number, &sigAct, &oldSigAct)) {
2467     // -1 means registration failed
2468     return (void *)-1;
2469   }
2470 
2471   return CAST_FROM_FN_PTR(void*, oldSigAct.sa_handler);
2472 }
2473 
2474 void os::signal_raise(int signal_number) {
2475   ::raise(signal_number);
2476 }
2477 
2478 /*
2479  * The following code is moved from os.cpp for making this
2480  * code platform specific, which it is by its very nature.
2481  */
2482 
2483 // Will be modified when max signal is changed to be dynamic
2484 int os::sigexitnum_pd() {
2485   return NSIG;
2486 }
2487 
2488 // a counter for each possible signal value
2489 static volatile jint pending_signals[NSIG+1] = { 0 };
2490 
2491 // Linux(POSIX) specific hand shaking semaphore.
2492 static sem_t sig_sem;
2493 static Semaphore sr_semaphore;
2494 
2495 void os::signal_init_pd() {
2496   // Initialize signal structures
2497   ::memset((void*)pending_signals, 0, sizeof(pending_signals));
2498 
2499   // Initialize signal semaphore
2500   ::sem_init(&sig_sem, 0, 0);
2501 }
2502 
2503 void os::signal_notify(int sig) {
2504   Atomic::inc(&pending_signals[sig]);
2505   ::sem_post(&sig_sem);
2506 }
2507 
2508 static int check_pending_signals(bool wait) {
2509   Atomic::store(0, &sigint_count);
2510   for (;;) {
2511     for (int i = 0; i < NSIG + 1; i++) {
2512       jint n = pending_signals[i];
2513       if (n > 0 && n == Atomic::cmpxchg(n - 1, &pending_signals[i], n)) {


3587 //      that runs in the watcher thread.
3588 //  The remaining code is greatly simplified from the more general suspension
3589 //  code that used to be used.
3590 //
3591 //  The protocol is quite simple:
3592 //  - suspend:
3593 //      - sends a signal to the target thread
3594 //      - polls the suspend state of the osthread using a yield loop
3595 //      - target thread signal handler (SR_handler) sets suspend state
3596 //        and blocks in sigsuspend until continued
3597 //  - resume:
3598 //      - sets target osthread state to continue
3599 //      - sends signal to end the sigsuspend loop in the SR_handler
3600 //
3601 //  Note that the SR_lock plays no role in this suspend/resume protocol.
3602 //
3603 
3604 static void resume_clear_context(OSThread *osthread) {
3605   osthread->set_ucontext(NULL);
3606   osthread->set_siginfo(NULL);



3607 }
3608 
3609 static void suspend_save_context(OSThread *osthread, siginfo_t* siginfo, ucontext_t* context) {
3610   osthread->set_ucontext(context);
3611   osthread->set_siginfo(siginfo);
3612 }
3613 
3614 //
3615 // Handler function invoked when a thread's execution is suspended or
3616 // resumed. We have to be careful that only async-safe functions are
3617 // called here (Note: most pthread functions are not async safe and
3618 // should be avoided.)
3619 //
3620 // Note: sigwait() is a more natural fit than sigsuspend() from an
3621 // interface point of view, but sigwait() prevents the signal hander
3622 // from being run. libpthread would get very confused by not having
3623 // its signal handlers run and prevents sigwait()'s use with the
3624 // mutex granting granting signal.
3625 //
3626 // Currently only ever called on the VMThread and JavaThreads (PC sampling)
3627 //
3628 static void SR_handler(int sig, siginfo_t* siginfo, ucontext_t* context) {
3629   // Save and restore errno to avoid confusing native code with EINTR
3630   // after sigsuspend.
3631   int old_errno = errno;
3632 
3633   Thread* thread = Thread::current();
3634   OSThread* osthread = thread->osthread();
3635   assert(thread->is_VM_thread() || thread->is_Java_thread(), "Must be VMThread or JavaThread");










3636 
3637   os::SuspendResume::State current = osthread->sr.state();
3638   if (current == os::SuspendResume::SR_SUSPEND_REQUEST) {
3639     suspend_save_context(osthread, siginfo, context);
3640 
3641     // attempt to switch the state, we assume we had a SUSPEND_REQUEST
3642     os::SuspendResume::State state = osthread->sr.suspended();
3643     if (state == os::SuspendResume::SR_SUSPENDED) {
3644       sigset_t suspend_set;  // signals for sigsuspend()
3645 
3646       // get current set of blocked signals and unblock resume signal
3647       pthread_sigmask(SIG_BLOCK, NULL, &suspend_set);
3648       sigdelset(&suspend_set, SR_signum);
3649 
3650       sr_semaphore.signal();
3651       // wait here until we are resumed
3652       while (1) {
3653         sigsuspend(&suspend_set);


3654 
3655         os::SuspendResume::State result = osthread->sr.running();
3656         if (result == os::SuspendResume::SR_RUNNING) {
3657           sr_semaphore.signal();
3658           break;
3659         }
3660       }
3661 
3662     } else if (state == os::SuspendResume::SR_RUNNING) {
3663       // request was cancelled, continue
3664     } else {
3665       ShouldNotReachHere();
3666     }
3667 
3668     resume_clear_context(osthread);
3669   } else if (current == os::SuspendResume::SR_RUNNING) {
3670     // request was cancelled, continue
3671   } else if (current == os::SuspendResume::SR_WAKEUP_REQUEST) {
3672     // ignore
3673   } else {
3674     // ignore
3675   }
3676 
3677   errno = old_errno;
3678 }
3679 
3680 
3681 static int SR_initialize() {
3682   struct sigaction act;
3683   char *s;
3684   /* Get signal number to use for suspend/resume */
3685   if ((s = ::getenv("_JAVA_SR_SIGNUM")) != 0) {
3686     int sig = ::strtol(s, 0, 10);
3687     if (sig > 0 || sig < _NSIG) {
3688         SR_signum = sig;
3689     }
3690   }
3691 
3692   assert(SR_signum > SIGSEGV && SR_signum > SIGBUS,
3693         "SR_signum must be greater than max(SIGSEGV, SIGBUS), see 4355769");
3694 


3698   /* Set up signal handler for suspend/resume */
3699   act.sa_flags = SA_RESTART|SA_SIGINFO;
3700   act.sa_handler = (void (*)(int)) SR_handler;
3701 
3702   // SR_signum is blocked by default.
3703   // 4528190 - We also need to block pthread restart signal (32 on all
3704   // supported Linux platforms). Note that LinuxThreads need to block
3705   // this signal for all threads to work properly. So we don't have
3706   // to use hard-coded signal number when setting up the mask.
3707   pthread_sigmask(SIG_BLOCK, NULL, &act.sa_mask);
3708 
3709   if (sigaction(SR_signum, &act, 0) == -1) {
3710     return -1;
3711   }
3712 
3713   // Save signal flag
3714   os::Linux::set_our_sigflags(SR_signum, act.sa_flags);
3715   return 0;
3716 }
3717 
3718 static int sr_notify(OSThread* osthread) {
3719   int status = pthread_kill(osthread->pthread_id(), SR_signum);
3720   assert_status(status == 0, status, "pthread_kill");
3721   return status;
3722 }
3723 
3724 // "Randomly" selected value for how long we want to spin
3725 // before bailing out on suspending a thread, also how often
3726 // we send a signal to a thread we want to resume
3727 static const int RANDOMLY_LARGE_INTEGER = 1000000;
3728 static const int RANDOMLY_LARGE_INTEGER2 = 100;
3729 
3730 // returns true on success and false on error - really an error is fatal
3731 // but this seems the normal response to library errors
3732 static bool do_suspend(OSThread* osthread) {
3733   assert(osthread->sr.is_running(), "thread should be running");
3734   assert(!sr_semaphore.trywait(), "semaphore has invalid state");


3735 
3736   // mark as suspended and send signal
3737   if (osthread->sr.request_suspend() != os::SuspendResume::SR_SUSPEND_REQUEST) {
3738     // failed to switch, state wasn't running?
3739     ShouldNotReachHere();
3740     return false;
3741   }
3742 
3743   if (sr_notify(osthread) != 0) {
3744     ShouldNotReachHere();
3745   }
3746 
3747   // managed to send the signal and switch to SUSPEND_REQUEST, now wait for SUSPENDED
3748   while (true) {
3749     if (sr_semaphore.timedwait(0, 2 * NANOSECS_PER_MILLISEC)) {
3750       break;
3751     } else {
3752       // timeout
3753       os::SuspendResume::State cancelled = osthread->sr.cancel_suspend();
3754       if (cancelled == os::SuspendResume::SR_RUNNING) {
3755         return false;
3756       } else if (cancelled == os::SuspendResume::SR_SUSPENDED) {
3757         // make sure that we consume the signal on the semaphore as well
3758         sr_semaphore.wait();
3759         break;
3760       } else {
3761         ShouldNotReachHere();
3762         return false;
3763       }
3764     }
3765   }
3766 
3767   guarantee(osthread->sr.is_suspended(), "Must be suspended");
3768   return true;
3769 }
3770 
3771 static void do_resume(OSThread* osthread) {
3772   assert(osthread->sr.is_suspended(), "thread should be suspended");
3773   assert(!sr_semaphore.trywait(), "invalid semaphore state");
3774 
3775   if (osthread->sr.request_wakeup() != os::SuspendResume::SR_WAKEUP_REQUEST) {
3776     // failed to switch to WAKEUP_REQUEST
3777     ShouldNotReachHere();
3778     return;
3779   }
3780 
3781   while (true) {
3782     if (sr_notify(osthread) == 0) {
3783       if (sr_semaphore.timedwait(0, 2 * NANOSECS_PER_MILLISEC)) {
3784         if (osthread->sr.is_running()) {
3785           return;
3786         }
3787       }
3788     } else {
3789       ShouldNotReachHere();
3790     }
3791   }
3792 
3793   guarantee(osthread->sr.is_running(), "Must be running!");
3794 }
3795 
3796 ////////////////////////////////////////////////////////////////////////////////
3797 // interrupt support
3798 
3799 void os::interrupt(Thread* thread) {
3800   assert(Thread::current() == thread || Threads_lock->owned_by_self(),
3801     "possibility of dangling Thread pointer");
3802 
3803   OSThread* osthread = thread->osthread();
3804 
3805   if (!osthread->interrupted()) {
3806     osthread->set_interrupted(true);
3807     // More than one thread can get here with the same value of osthread,
3808     // resulting in multiple notifications.  We do, however, want the store
3809     // to interrupted() to be visible to other threads before we execute unpark().
3810     OrderAccess::fence();
3811     ParkEvent * const slp = thread->_SleepEvent ;
3812     if (slp != NULL) slp->unpark() ;
3813   }


4545   return online_cpus;
4546 }
4547 
4548 void os::set_native_thread_name(const char *name) {
4549   // Not yet implemented.
4550   return;
4551 }
4552 
4553 bool os::distribute_processes(uint length, uint* distribution) {
4554   // Not yet implemented.
4555   return false;
4556 }
4557 
4558 bool os::bind_to_processor(uint processor_id) {
4559   // Not yet implemented.
4560   return false;
4561 }
4562 
4563 ///
4564 
4565 void os::SuspendedThreadTask::internal_do_task() {
4566   if (do_suspend(_thread->osthread())) {
4567     SuspendedThreadTaskContext context(_thread, _thread->osthread()->ucontext());
4568     do_task(context);
4569     do_resume(_thread->osthread());
4570   }
4571 }
4572 
4573 class PcFetcher : public os::SuspendedThreadTask {
4574 public:
4575   PcFetcher(Thread* thread) : os::SuspendedThreadTask(thread) {}
4576   ExtendedPC result();
4577 protected:
4578   void do_task(const os::SuspendedThreadTaskContext& context);
4579 private:
4580   ExtendedPC _epc;
4581 };
4582 
4583 ExtendedPC PcFetcher::result() {
4584   guarantee(is_done(), "task is not done yet.");
4585   return _epc;
4586 }
4587 
4588 void PcFetcher::do_task(const os::SuspendedThreadTaskContext& context) {
4589   Thread* thread = context.thread();
4590   OSThread* osthread = thread->osthread();

4591   if (osthread->ucontext() != NULL) {
4592     _epc = os::Linux::ucontext_get_pc((ucontext_t *) context.ucontext());
4593   } else {
4594     // NULL context is unexpected, double-check this is the VMThread
4595     guarantee(thread->is_VM_thread(), "can only be called for VMThread");
4596   }
4597 }
4598 
4599 // Suspends the target using the signal mechanism and then grabs the PC before
4600 // resuming the target. Used by the flat-profiler only
4601 ExtendedPC os::get_thread_pc(Thread* thread) {
4602   // Make sure that it is called by the watcher for the VMThread
4603   assert(Thread::current()->is_Watcher_thread(), "Must be watcher");
4604   assert(thread->is_VM_thread(), "Can only be called for VMThread");
4605 
4606   PcFetcher fetcher(thread);
4607   fetcher.run();
4608   return fetcher.result();
4609 }
4610 
4611 int os::Linux::safe_cond_timedwait(pthread_cond_t *_cond, pthread_mutex_t *_mutex, const struct timespec *_abstime)
4612 {
4613    if (is_NPTL()) {
4614       return pthread_cond_timedwait(_cond, _mutex, _abstime);
4615    } else {
4616       // 6292965: LinuxThreads pthread_cond_timedwait() resets FPU control
4617       // word back to default 64bit precision if condvar is signaled. Java
4618       // wants 53bit precision.  Save and restore current value.
4619       int fpu = get_fpu_control_word();
4620       int status = pthread_cond_timedwait(_cond, _mutex, _abstime);
4621       set_fpu_control_word(fpu);
4622       return status;
4623    }
4624 }
4625 
4626 ////////////////////////////////////////////////////////////////////////////////
4627 // debug support
4628 


5710       // We might want to do something like the following if we find the GC's are not helping...
5711       // Universe::heap()->size_policy()->set_gc_time_limit_exceeded(true);
5712     }
5713   }
5714 }
5715 
5716 //
5717 // See if the /dev/mem_notify device exists, and if so, start a thread to monitor it.
5718 //
5719 void MemNotifyThread::start() {
5720   int    fd;
5721   fd = open ("/dev/mem_notify", O_RDONLY, 0);
5722   if (fd < 0) {
5723       return;
5724   }
5725 
5726   if (memnotify_thread() == NULL) {
5727     new MemNotifyThread(fd);
5728   }
5729 }
5730 
5731 #endif // JAVASE_EMBEDDED