src/os/bsd/vm/os_bsd.cpp

Print this page
rev 2869 : 7117303: VM uses non-monotonic time source and complains that it is non-monotonic
Summary: Replaces calls to os::javaTimeMillis(), which does not guarantee montonicity, in GC code to os::javaTimeNanos() with a suitable conversion factor. os::javaTimeNanos() mostly guarantees montonicity depending on the underlying OS implementation and, as a result, a better alternative. Changes in OS files are to make use of the newly defined constants in globalDefinitions.hpp.
Reviewed-by:


 133 
 134 #if defined(__FreeBSD__) || defined(__NetBSD__)
 135 # include <elf.h>
 136 #endif
 137 
 138 #ifdef __APPLE__
 139 # include <mach/mach.h> // semaphore_* API
 140 # include <mach-o/dyld.h>
 141 # include <sys/proc_info.h>
 142 # include <objc/objc-auto.h>
 143 #endif
 144 
 145 #ifndef MAP_ANONYMOUS
 146 #define MAP_ANONYMOUS MAP_ANON
 147 #endif
 148 
 149 #define MAX_PATH    (2 * K)
 150 
 151 // for timer info max values which include all bits
 152 #define ALL_64_BITS CONST64(0xFFFFFFFFFFFFFFFF)
 153 #define SEC_IN_NANOSECS  1000000000LL
 154 
 155 #define LARGEPAGES_BIT (1 << 6)
 156 ////////////////////////////////////////////////////////////////////////////////
 157 // global variables
 158 julong os::Bsd::_physical_memory = 0;
 159 
 160 #ifndef _ALLBSD_SOURCE
 161 address   os::Bsd::_initial_thread_stack_bottom = NULL;
 162 uintptr_t os::Bsd::_initial_thread_stack_size   = 0;
 163 #endif
 164 
 165 int (*os::Bsd::_clock_gettime)(clockid_t, struct timespec *) = NULL;
 166 #ifndef _ALLBSD_SOURCE
 167 int (*os::Bsd::_pthread_getcpuclockid)(pthread_t, clockid_t *) = NULL;
 168 Mutex* os::Bsd::_createThread_lock = NULL;
 169 #endif
 170 pthread_t os::Bsd::_main_thread;
 171 int os::Bsd::_page_size = -1;
 172 #ifndef _ALLBSD_SOURCE
 173 bool os::Bsd::_is_floating_stack = false;


3428 
3429   if (i < max_tries) {
3430     _highest_vm_reserved_address = MAX2(old_highest, (address)requested_addr + bytes);
3431     return requested_addr;
3432   } else {
3433     _highest_vm_reserved_address = old_highest;
3434     return NULL;
3435   }
3436 }
3437 
3438 size_t os::read(int fd, void *buf, unsigned int nBytes) {
3439   RESTARTABLE_RETURN_INT(::read(fd, buf, nBytes));
3440 }
3441 
3442 // TODO-FIXME: reconcile Solaris' os::sleep with the bsd variation.
3443 // Solaris uses poll(), bsd uses park().
3444 // Poll() is likely a better choice, assuming that Thread.interrupt()
3445 // generates a SIGUSRx signal. Note that SIGUSR1 can interfere with
3446 // SIGSEGV, see 4355769.
3447 
3448 const int NANOSECS_PER_MILLISECS = 1000000;
3449 
3450 int os::sleep(Thread* thread, jlong millis, bool interruptible) {
3451   assert(thread == Thread::current(),  "thread consistency check");
3452 
3453   ParkEvent * const slp = thread->_SleepEvent ;
3454   slp->reset() ;
3455   OrderAccess::fence() ;
3456 
3457   if (interruptible) {
3458     jlong prevtime = javaTimeNanos();
3459 
3460     for (;;) {
3461       if (os::is_interrupted(thread, true)) {
3462         return OS_INTRPT;
3463       }
3464 
3465       jlong newtime = javaTimeNanos();
3466 
3467       if (newtime - prevtime < 0) {
3468         // time moving backwards, should only happen if no monotonic clock
3469         // not a guarantee() because JVM should not abort on kernel/glibc bugs
3470         assert(!Bsd::supports_monotonic_clock(), "time moving backwards");
3471       } else {
3472         millis -= (newtime - prevtime) / NANOSECS_PER_MILLISECS;
3473       }
3474 
3475       if(millis <= 0) {
3476         return OS_OK;
3477       }
3478 
3479       prevtime = newtime;
3480 
3481       {
3482         assert(thread->is_Java_thread(), "sanity check");
3483         JavaThread *jt = (JavaThread *) thread;
3484         ThreadBlockInVM tbivm(jt);
3485         OSThreadWaitState osts(jt->osthread(), false /* not Object.wait() */);
3486 
3487         jt->set_suspend_equivalent();
3488         // cleared by handle_special_suspend_equivalent_condition() or
3489         // java_suspend_self() via check_and_wait_while_suspended()
3490 
3491         slp->park(millis);
3492 
3493         // were we externally suspended while we were waiting?
3494         jt->check_and_wait_while_suspended();
3495       }
3496     }
3497   } else {
3498     OSThreadWaitState osts(thread->osthread(), false /* not Object.wait() */);
3499     jlong prevtime = javaTimeNanos();
3500 
3501     for (;;) {
3502       // It'd be nice to avoid the back-to-back javaTimeNanos() calls on
3503       // the 1st iteration ...
3504       jlong newtime = javaTimeNanos();
3505 
3506       if (newtime - prevtime < 0) {
3507         // time moving backwards, should only happen if no monotonic clock
3508         // not a guarantee() because JVM should not abort on kernel/glibc bugs
3509         assert(!Bsd::supports_monotonic_clock(), "time moving backwards");
3510       } else {
3511         millis -= (newtime - prevtime) / NANOSECS_PER_MILLISECS;
3512       }
3513 
3514       if(millis <= 0) break ;
3515 
3516       prevtime = newtime;
3517       slp->park(millis);
3518     }
3519     return OS_OK ;
3520   }
3521 }
3522 
3523 int os::naked_sleep() {
3524   // %% make the sleep time an integer flag. for now use 1 millisec.
3525   return os::sleep(Thread::current(), 1, false);
3526 }
3527 
3528 // Sleep forever; naked call to OS-specific sleep; use with CAUTION
3529 void os::infinite_sleep() {
3530   while (true) {    // sleep forever ...
3531     ::sleep(100);   // ... 100 seconds at a time


4180         tty->print_cr("Info: AllowUserSignalHandlers is activated, all active signal checking is disabled");
4181         check_signals = false;
4182       }
4183     }
4184   }
4185 }
4186 
4187 #ifndef _ALLBSD_SOURCE
4188 // This is the fastest way to get thread cpu time on Bsd.
4189 // Returns cpu time (user+sys) for any thread, not only for current.
4190 // POSIX compliant clocks are implemented in the kernels 2.6.16+.
4191 // It might work on 2.6.10+ with a special kernel/glibc patch.
4192 // For reference, please, see IEEE Std 1003.1-2004:
4193 //   http://www.unix.org/single_unix_specification
4194 
4195 jlong os::Bsd::fast_thread_cpu_time(clockid_t clockid) {
4196   struct timespec tp;
4197   int rc = os::Bsd::clock_gettime(clockid, &tp);
4198   assert(rc == 0, "clock_gettime is expected to return 0 code");
4199 
4200   return (tp.tv_sec * SEC_IN_NANOSECS) + tp.tv_nsec;
4201 }
4202 #endif
4203 
4204 /////
4205 // glibc on Bsd platform uses non-documented flag
4206 // to indicate, that some special sort of signal
4207 // trampoline is used.
4208 // We will never set this flag, and we should
4209 // ignore this flag in our diagnostic
4210 #ifdef SIGNIFICANT_SIGNAL_MASK
4211 #undef SIGNIFICANT_SIGNAL_MASK
4212 #endif
4213 #define SIGNIFICANT_SIGNAL_MASK (~0x04000000)
4214 
4215 static const char* get_signal_handler_name(address handler,
4216                                            char* buf, int buflen) {
4217   int offset;
4218   bool found = os::dll_address_to_library_name(handler, buf, buflen, &offset);
4219   if (found) {
4220     // skip directory names


5505   // circumstances this can cause a thread to return prematurely from
5506   // cond_{timed}wait() but the spurious wakeup is benign and the victim will
5507   // simply re-test the condition and re-park itself.
5508 }
5509 
5510 
5511 // JSR166
5512 // -------------------------------------------------------
5513 
5514 /*
5515  * The solaris and bsd implementations of park/unpark are fairly
5516  * conservative for now, but can be improved. They currently use a
5517  * mutex/condvar pair, plus a a count.
5518  * Park decrements count if > 0, else does a condvar wait.  Unpark
5519  * sets count to 1 and signals condvar.  Only one thread ever waits
5520  * on the condvar. Contention seen when trying to park implies that someone
5521  * is unparking you, so don't wait. And spurious returns are fine, so there
5522  * is no need to track notifications.
5523  */
5524 
5525 
5526 #define NANOSECS_PER_SEC 1000000000
5527 #define NANOSECS_PER_MILLISEC 1000000
5528 #define MAX_SECS 100000000
5529 /*
5530  * This code is common to bsd and solaris and will be moved to a
5531  * common place in dolphin.
5532  *
5533  * The passed in time value is either a relative time in nanoseconds
5534  * or an absolute time in milliseconds. Either way it has to be unpacked
5535  * into suitable seconds and nanoseconds components and stored in the
5536  * given timespec structure.
5537  * Given time is a 64-bit value and the time_t used in the timespec is only
5538  * a signed-32-bit value (except on 64-bit Bsd) we have to watch for
5539  * overflow if times way in the future are given. Further on Solaris versions
5540  * prior to 10 there is a restriction (see cond_timedwait) that the specified
5541  * number of seconds, in abstime, is less than current_time  + 100,000,000.
5542  * As it will be 28 years before "now + 100000000" will overflow we can
5543  * ignore overflow and just impose a hard-limit on seconds using the value
5544  * of "now + 100,000,000". This places a limit on the timeout of about 3.17
5545  * years from "now".
5546  */
5547 




 133 
 134 #if defined(__FreeBSD__) || defined(__NetBSD__)
 135 # include <elf.h>
 136 #endif
 137 
 138 #ifdef __APPLE__
 139 # include <mach/mach.h> // semaphore_* API
 140 # include <mach-o/dyld.h>
 141 # include <sys/proc_info.h>
 142 # include <objc/objc-auto.h>
 143 #endif
 144 
 145 #ifndef MAP_ANONYMOUS
 146 #define MAP_ANONYMOUS MAP_ANON
 147 #endif
 148 
 149 #define MAX_PATH    (2 * K)
 150 
 151 // for timer info max values which include all bits
 152 #define ALL_64_BITS CONST64(0xFFFFFFFFFFFFFFFF)

 153 
 154 #define LARGEPAGES_BIT (1 << 6)
 155 ////////////////////////////////////////////////////////////////////////////////
 156 // global variables
 157 julong os::Bsd::_physical_memory = 0;
 158 
 159 #ifndef _ALLBSD_SOURCE
 160 address   os::Bsd::_initial_thread_stack_bottom = NULL;
 161 uintptr_t os::Bsd::_initial_thread_stack_size   = 0;
 162 #endif
 163 
 164 int (*os::Bsd::_clock_gettime)(clockid_t, struct timespec *) = NULL;
 165 #ifndef _ALLBSD_SOURCE
 166 int (*os::Bsd::_pthread_getcpuclockid)(pthread_t, clockid_t *) = NULL;
 167 Mutex* os::Bsd::_createThread_lock = NULL;
 168 #endif
 169 pthread_t os::Bsd::_main_thread;
 170 int os::Bsd::_page_size = -1;
 171 #ifndef _ALLBSD_SOURCE
 172 bool os::Bsd::_is_floating_stack = false;


3427 
3428   if (i < max_tries) {
3429     _highest_vm_reserved_address = MAX2(old_highest, (address)requested_addr + bytes);
3430     return requested_addr;
3431   } else {
3432     _highest_vm_reserved_address = old_highest;
3433     return NULL;
3434   }
3435 }
3436 
3437 size_t os::read(int fd, void *buf, unsigned int nBytes) {
3438   RESTARTABLE_RETURN_INT(::read(fd, buf, nBytes));
3439 }
3440 
3441 // TODO-FIXME: reconcile Solaris' os::sleep with the bsd variation.
3442 // Solaris uses poll(), bsd uses park().
3443 // Poll() is likely a better choice, assuming that Thread.interrupt()
3444 // generates a SIGUSRx signal. Note that SIGUSR1 can interfere with
3445 // SIGSEGV, see 4355769.
3446 


3447 int os::sleep(Thread* thread, jlong millis, bool interruptible) {
3448   assert(thread == Thread::current(),  "thread consistency check");
3449 
3450   ParkEvent * const slp = thread->_SleepEvent ;
3451   slp->reset() ;
3452   OrderAccess::fence() ;
3453 
3454   if (interruptible) {
3455     jlong prevtime = javaTimeNanos();
3456 
3457     for (;;) {
3458       if (os::is_interrupted(thread, true)) {
3459         return OS_INTRPT;
3460       }
3461 
3462       jlong newtime = javaTimeNanos();
3463 
3464       if (newtime - prevtime < 0) {
3465         // time moving backwards, should only happen if no monotonic clock
3466         // not a guarantee() because JVM should not abort on kernel/glibc bugs
3467         assert(!Bsd::supports_monotonic_clock(), "time moving backwards");
3468       } else {
3469         millis -= (newtime - prevtime) / NANOSECS_PER_MILLISEC;
3470       }
3471 
3472       if(millis <= 0) {
3473         return OS_OK;
3474       }
3475 
3476       prevtime = newtime;
3477 
3478       {
3479         assert(thread->is_Java_thread(), "sanity check");
3480         JavaThread *jt = (JavaThread *) thread;
3481         ThreadBlockInVM tbivm(jt);
3482         OSThreadWaitState osts(jt->osthread(), false /* not Object.wait() */);
3483 
3484         jt->set_suspend_equivalent();
3485         // cleared by handle_special_suspend_equivalent_condition() or
3486         // java_suspend_self() via check_and_wait_while_suspended()
3487 
3488         slp->park(millis);
3489 
3490         // were we externally suspended while we were waiting?
3491         jt->check_and_wait_while_suspended();
3492       }
3493     }
3494   } else {
3495     OSThreadWaitState osts(thread->osthread(), false /* not Object.wait() */);
3496     jlong prevtime = javaTimeNanos();
3497 
3498     for (;;) {
3499       // It'd be nice to avoid the back-to-back javaTimeNanos() calls on
3500       // the 1st iteration ...
3501       jlong newtime = javaTimeNanos();
3502 
3503       if (newtime - prevtime < 0) {
3504         // time moving backwards, should only happen if no monotonic clock
3505         // not a guarantee() because JVM should not abort on kernel/glibc bugs
3506         assert(!Bsd::supports_monotonic_clock(), "time moving backwards");
3507       } else {
3508         millis -= (newtime - prevtime) / NANOSECS_PER_MILLISEC;
3509       }
3510 
3511       if(millis <= 0) break ;
3512 
3513       prevtime = newtime;
3514       slp->park(millis);
3515     }
3516     return OS_OK ;
3517   }
3518 }
3519 
3520 int os::naked_sleep() {
3521   // %% make the sleep time an integer flag. for now use 1 millisec.
3522   return os::sleep(Thread::current(), 1, false);
3523 }
3524 
3525 // Sleep forever; naked call to OS-specific sleep; use with CAUTION
3526 void os::infinite_sleep() {
3527   while (true) {    // sleep forever ...
3528     ::sleep(100);   // ... 100 seconds at a time


4177         tty->print_cr("Info: AllowUserSignalHandlers is activated, all active signal checking is disabled");
4178         check_signals = false;
4179       }
4180     }
4181   }
4182 }
4183 
4184 #ifndef _ALLBSD_SOURCE
4185 // This is the fastest way to get thread cpu time on Bsd.
4186 // Returns cpu time (user+sys) for any thread, not only for current.
4187 // POSIX compliant clocks are implemented in the kernels 2.6.16+.
4188 // It might work on 2.6.10+ with a special kernel/glibc patch.
4189 // For reference, please, see IEEE Std 1003.1-2004:
4190 //   http://www.unix.org/single_unix_specification
4191 
4192 jlong os::Bsd::fast_thread_cpu_time(clockid_t clockid) {
4193   struct timespec tp;
4194   int rc = os::Bsd::clock_gettime(clockid, &tp);
4195   assert(rc == 0, "clock_gettime is expected to return 0 code");
4196 
4197   return (tp.tv_sec * NANOSECS_PER_SEC) + tp.tv_nsec;
4198 }
4199 #endif
4200 
4201 /////
4202 // glibc on Bsd platform uses non-documented flag
4203 // to indicate, that some special sort of signal
4204 // trampoline is used.
4205 // We will never set this flag, and we should
4206 // ignore this flag in our diagnostic
4207 #ifdef SIGNIFICANT_SIGNAL_MASK
4208 #undef SIGNIFICANT_SIGNAL_MASK
4209 #endif
4210 #define SIGNIFICANT_SIGNAL_MASK (~0x04000000)
4211 
4212 static const char* get_signal_handler_name(address handler,
4213                                            char* buf, int buflen) {
4214   int offset;
4215   bool found = os::dll_address_to_library_name(handler, buf, buflen, &offset);
4216   if (found) {
4217     // skip directory names


5502   // circumstances this can cause a thread to return prematurely from
5503   // cond_{timed}wait() but the spurious wakeup is benign and the victim will
5504   // simply re-test the condition and re-park itself.
5505 }
5506 
5507 
5508 // JSR166
5509 // -------------------------------------------------------
5510 
5511 /*
5512  * The solaris and bsd implementations of park/unpark are fairly
5513  * conservative for now, but can be improved. They currently use a
5514  * mutex/condvar pair, plus a a count.
5515  * Park decrements count if > 0, else does a condvar wait.  Unpark
5516  * sets count to 1 and signals condvar.  Only one thread ever waits
5517  * on the condvar. Contention seen when trying to park implies that someone
5518  * is unparking you, so don't wait. And spurious returns are fine, so there
5519  * is no need to track notifications.
5520  */
5521 



5522 #define MAX_SECS 100000000
5523 /*
5524  * This code is common to bsd and solaris and will be moved to a
5525  * common place in dolphin.
5526  *
5527  * The passed in time value is either a relative time in nanoseconds
5528  * or an absolute time in milliseconds. Either way it has to be unpacked
5529  * into suitable seconds and nanoseconds components and stored in the
5530  * given timespec structure.
5531  * Given time is a 64-bit value and the time_t used in the timespec is only
5532  * a signed-32-bit value (except on 64-bit Bsd) we have to watch for
5533  * overflow if times way in the future are given. Further on Solaris versions
5534  * prior to 10 there is a restriction (see cond_timedwait) that the specified
5535  * number of seconds, in abstime, is less than current_time  + 100,000,000.
5536  * As it will be 28 years before "now + 100000000" will overflow we can
5537  * ignore overflow and just impose a hard-limit on seconds using the value
5538  * of "now + 100,000,000". This places a limit on the timeout of about 3.17
5539  * years from "now".
5540  */
5541