src/os/bsd/vm/os_bsd.cpp

Print this page


   1 /*
   2  * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *


 977 #endif
 978 
 979 #ifdef __APPLE__
 980 void os::Bsd::clock_init() {
 981         // XXXDARWIN: Investigate replacement monotonic clock
 982 }
 983 #else
 984 void os::Bsd::clock_init() {
 985   struct timespec res;
 986   struct timespec tp;
 987   if (::clock_getres(CLOCK_MONOTONIC, &res) == 0 &&
 988       ::clock_gettime(CLOCK_MONOTONIC, &tp)  == 0) {
 989     // yes, monotonic clock is supported
 990     _clock_gettime = ::clock_gettime;
 991   }
 992 }
 993 #endif
 994 
 995 
 996 jlong os::javaTimeNanos() {
 997   if (Bsd::supports_monotonic_clock()) {
 998     struct timespec tp;
 999     int status = Bsd::clock_gettime(CLOCK_MONOTONIC, &tp);
1000     assert(status == 0, "gettime error");
1001     jlong result = jlong(tp.tv_sec) * (1000 * 1000 * 1000) + jlong(tp.tv_nsec);
1002     return result;
1003   } else {
1004     timeval time;
1005     int status = gettimeofday(&time, NULL);
1006     assert(status != -1, "bsd error");
1007     jlong usecs = jlong(time.tv_sec) * (1000 * 1000) + jlong(time.tv_usec);
1008     return 1000 * usecs;
1009   }
1010 }
1011 
1012 void os::javaTimeNanos_info(jvmtiTimerInfo *info_ptr) {
1013   if (Bsd::supports_monotonic_clock()) {
1014     info_ptr->max_value = ALL_64_BITS;
1015 
1016     // CLOCK_MONOTONIC - amount of time since some arbitrary point in the past
1017     info_ptr->may_skip_backward = false;      // not subject to resetting or drifting
1018     info_ptr->may_skip_forward = false;       // not subject to resetting or drifting
1019   } else {
1020     // gettimeofday - based on time in seconds since the Epoch thus does not wrap
1021     info_ptr->max_value = ALL_64_BITS;
1022 
1023     // gettimeofday is a real time clock so it skips
1024     info_ptr->may_skip_backward = true;
1025     info_ptr->may_skip_forward = true;
1026   }
1027 
1028   info_ptr->kind = JVMTI_TIMER_ELAPSED;                // elapsed not CPU time
1029 }
1030 
1031 // Return the real, user, and system times in seconds from an
1032 // arbitrary fixed point in the past.
1033 bool os::getTimesSecs(double* process_real_time,


2542 
2543   for (int j = 0; j < i; ++j) {
2544     if (base[j] != NULL) {
2545       unmap_memory(base[j], size[j]);
2546     }
2547   }
2548 
2549   if (i < max_tries) {
2550     _highest_vm_reserved_address = MAX2(old_highest, (address)requested_addr + bytes);
2551     return requested_addr;
2552   } else {
2553     _highest_vm_reserved_address = old_highest;
2554     return NULL;
2555   }
2556 }
2557 
2558 size_t os::read(int fd, void *buf, unsigned int nBytes) {
2559   RESTARTABLE_RETURN_INT(::read(fd, buf, nBytes));
2560 }
2561 
2562 // TODO-FIXME: reconcile Solaris' os::sleep with the bsd variation.
2563 // Solaris uses poll(), bsd uses park().
2564 // Poll() is likely a better choice, assuming that Thread.interrupt()
2565 // generates a SIGUSRx signal. Note that SIGUSR1 can interfere with
2566 // SIGSEGV, see 4355769.
2567 
2568 int os::sleep(Thread* thread, jlong millis, bool interruptible) {
2569   assert(thread == Thread::current(),  "thread consistency check");
2570 
2571   ParkEvent * const slp = thread->_SleepEvent ;
2572   slp->reset() ;
2573   OrderAccess::fence() ;
2574 
2575   if (interruptible) {
2576     jlong prevtime = javaTimeNanos();
2577 
2578     for (;;) {
2579       if (os::is_interrupted(thread, true)) {
2580         return OS_INTRPT;
2581       }
2582 
2583       jlong newtime = javaTimeNanos();
2584 
2585       if (newtime - prevtime < 0) {
2586         // time moving backwards, should only happen if no monotonic clock
2587         // not a guarantee() because JVM should not abort on kernel/glibc bugs
2588         assert(!Bsd::supports_monotonic_clock(), "time moving backwards");
2589       } else {
2590         millis -= (newtime - prevtime) / NANOSECS_PER_MILLISEC;
2591       }
2592 
2593       if(millis <= 0) {
2594         return OS_OK;
2595       }
2596 
2597       prevtime = newtime;
2598 
2599       {
2600         assert(thread->is_Java_thread(), "sanity check");
2601         JavaThread *jt = (JavaThread *) thread;
2602         ThreadBlockInVM tbivm(jt);
2603         OSThreadWaitState osts(jt->osthread(), false /* not Object.wait() */);
2604 
2605         jt->set_suspend_equivalent();
2606         // cleared by handle_special_suspend_equivalent_condition() or
2607         // java_suspend_self() via check_and_wait_while_suspended()
2608 
2609         slp->park(millis);
2610 
2611         // were we externally suspended while we were waiting?
2612         jt->check_and_wait_while_suspended();
2613       }
2614     }
2615   } else {
2616     OSThreadWaitState osts(thread->osthread(), false /* not Object.wait() */);
2617     jlong prevtime = javaTimeNanos();
2618 
2619     for (;;) {
2620       // It'd be nice to avoid the back-to-back javaTimeNanos() calls on
2621       // the 1st iteration ...
2622       jlong newtime = javaTimeNanos();
2623 
2624       if (newtime - prevtime < 0) {
2625         // time moving backwards, should only happen if no monotonic clock
2626         // not a guarantee() because JVM should not abort on kernel/glibc bugs
2627         assert(!Bsd::supports_monotonic_clock(), "time moving backwards");
2628       } else {
2629         millis -= (newtime - prevtime) / NANOSECS_PER_MILLISEC;
2630       }
2631 
2632       if(millis <= 0) break ;
2633 
2634       prevtime = newtime;
2635       slp->park(millis);
2636     }
2637     return OS_OK ;
2638   }
2639 }
2640 
2641 void os::naked_short_sleep(jlong ms) {
2642   struct timespec req;
2643 
2644   assert(ms < 1000, "Un-interruptable sleep, short time use only");
2645   req.tv_sec = 0;
2646   if (ms > 0) {
2647     req.tv_nsec = (ms % 1000) * 1000000;
2648   }
2649   else {
2650     req.tv_nsec = 1;
2651   }
2652 
2653   nanosleep(&req, NULL);
2654 
2655   return;
2656 }
2657 
2658 // Sleep forever; naked call to OS-specific sleep; use with CAUTION
2659 void os::infinite_sleep() {
2660   while (true) {    // sleep forever ...


3016     // failed to switch to WAKEUP_REQUEST
3017     ShouldNotReachHere();
3018     return;
3019   }
3020 
3021   while (true) {
3022     if (sr_notify(osthread) == 0) {
3023       if (sr_semaphore.timedwait(0, 2 * NANOSECS_PER_MILLISEC)) {
3024         if (osthread->sr.is_running()) {
3025           return;
3026         }
3027       }
3028     } else {
3029       ShouldNotReachHere();
3030     }
3031   }
3032 
3033   guarantee(osthread->sr.is_running(), "Must be running!");
3034 }
3035 
3036 ////////////////////////////////////////////////////////////////////////////////
3037 // interrupt support
3038 
3039 void os::interrupt(Thread* thread) {
3040   assert(Thread::current() == thread || Threads_lock->owned_by_self(),
3041     "possibility of dangling Thread pointer");
3042 
3043   OSThread* osthread = thread->osthread();
3044 
3045   if (!osthread->interrupted()) {
3046     osthread->set_interrupted(true);
3047     // More than one thread can get here with the same value of osthread,
3048     // resulting in multiple notifications.  We do, however, want the store
3049     // to interrupted() to be visible to other threads before we execute unpark().
3050     OrderAccess::fence();
3051     ParkEvent * const slp = thread->_SleepEvent ;
3052     if (slp != NULL) slp->unpark() ;
3053   }
3054 
3055   // For JSR166. Unpark even if interrupt status already was set
3056   if (thread->is_Java_thread())
3057     ((JavaThread*)thread)->parker()->unpark();
3058 
3059   ParkEvent * ev = thread->_ParkEvent ;
3060   if (ev != NULL) ev->unpark() ;
3061 
3062 }
3063 
3064 bool os::is_interrupted(Thread* thread, bool clear_interrupted) {
3065   assert(Thread::current() == thread || Threads_lock->owned_by_self(),
3066     "possibility of dangling Thread pointer");
3067 
3068   OSThread* osthread = thread->osthread();
3069 
3070   bool interrupted = osthread->interrupted();
3071 
3072   if (interrupted && clear_interrupted) {
3073     osthread->set_interrupted(false);
3074     // consider thread->_SleepEvent->reset() ... optional optimization
3075   }
3076 
3077   return interrupted;
3078 }
3079 
3080 ///////////////////////////////////////////////////////////////////////////////////
3081 // signal handling (except suspend/resume)
3082 
3083 // This routine may be used by user applications as a "hook" to catch signals.
3084 // The user-defined signal handler must pass unrecognized signals to this
3085 // routine, and if it returns true (non-zero), then the signal handler must
3086 // return immediately.  If the flag "abort_if_unrecognized" is true, then this
3087 // routine will never retun false (zero), but instead will execute a VM panic
3088 // routine kill the process.
3089 //
3090 // If this routine returns false, it is OK to call it again.  This allows
3091 // the user-defined signal handler to perform checks either before or after
3092 // the VM performs its own checks.  Naturally, the user code would be making
3093 // a serious error if it tried to handle an exception (such as a null check
3094 // or breakpoint) that the VM was generating for its own correct operation.
3095 //
3096 // This routine may recognize any of the following kinds of signals:
3097 //    SIGBUS, SIGSEGV, SIGILL, SIGFPE, SIGQUIT, SIGPIPE, SIGXFSZ, SIGUSR1.
3098 // It should be consulted by handlers for any of those signals.
3099 //


   1 /*
   2  * Copyright (c) 1999, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *


 977 #endif
 978 
 979 #ifdef __APPLE__
 980 void os::Bsd::clock_init() {
 981         // XXXDARWIN: Investigate replacement monotonic clock
 982 }
 983 #else
 984 void os::Bsd::clock_init() {
 985   struct timespec res;
 986   struct timespec tp;
 987   if (::clock_getres(CLOCK_MONOTONIC, &res) == 0 &&
 988       ::clock_gettime(CLOCK_MONOTONIC, &tp)  == 0) {
 989     // yes, monotonic clock is supported
 990     _clock_gettime = ::clock_gettime;
 991   }
 992 }
 993 #endif
 994 
 995 
 996 jlong os::javaTimeNanos() {
 997   if (os::supports_monotonic_clock()) {
 998     struct timespec tp;
 999     int status = Bsd::clock_gettime(CLOCK_MONOTONIC, &tp);
1000     assert(status == 0, "gettime error");
1001     jlong result = jlong(tp.tv_sec) * (1000 * 1000 * 1000) + jlong(tp.tv_nsec);
1002     return result;
1003   } else {
1004     timeval time;
1005     int status = gettimeofday(&time, NULL);
1006     assert(status != -1, "bsd error");
1007     jlong usecs = jlong(time.tv_sec) * (1000 * 1000) + jlong(time.tv_usec);
1008     return 1000 * usecs;
1009   }
1010 }
1011 
1012 void os::javaTimeNanos_info(jvmtiTimerInfo *info_ptr) {
1013   if (os::supports_monotonic_clock()) {
1014     info_ptr->max_value = ALL_64_BITS;
1015 
1016     // CLOCK_MONOTONIC - amount of time since some arbitrary point in the past
1017     info_ptr->may_skip_backward = false;      // not subject to resetting or drifting
1018     info_ptr->may_skip_forward = false;       // not subject to resetting or drifting
1019   } else {
1020     // gettimeofday - based on time in seconds since the Epoch thus does not wrap
1021     info_ptr->max_value = ALL_64_BITS;
1022 
1023     // gettimeofday is a real time clock so it skips
1024     info_ptr->may_skip_backward = true;
1025     info_ptr->may_skip_forward = true;
1026   }
1027 
1028   info_ptr->kind = JVMTI_TIMER_ELAPSED;                // elapsed not CPU time
1029 }
1030 
1031 // Return the real, user, and system times in seconds from an
1032 // arbitrary fixed point in the past.
1033 bool os::getTimesSecs(double* process_real_time,


2542 
2543   for (int j = 0; j < i; ++j) {
2544     if (base[j] != NULL) {
2545       unmap_memory(base[j], size[j]);
2546     }
2547   }
2548 
2549   if (i < max_tries) {
2550     _highest_vm_reserved_address = MAX2(old_highest, (address)requested_addr + bytes);
2551     return requested_addr;
2552   } else {
2553     _highest_vm_reserved_address = old_highest;
2554     return NULL;
2555   }
2556 }
2557 
2558 size_t os::read(int fd, void *buf, unsigned int nBytes) {
2559   RESTARTABLE_RETURN_INT(::read(fd, buf, nBytes));
2560 }
2561 















































































2562 void os::naked_short_sleep(jlong ms) {
2563   struct timespec req;
2564 
2565   assert(ms < 1000, "Un-interruptable sleep, short time use only");
2566   req.tv_sec = 0;
2567   if (ms > 0) {
2568     req.tv_nsec = (ms % 1000) * 1000000;
2569   }
2570   else {
2571     req.tv_nsec = 1;
2572   }
2573 
2574   nanosleep(&req, NULL);
2575 
2576   return;
2577 }
2578 
2579 // Sleep forever; naked call to OS-specific sleep; use with CAUTION
2580 void os::infinite_sleep() {
2581   while (true) {    // sleep forever ...


2937     // failed to switch to WAKEUP_REQUEST
2938     ShouldNotReachHere();
2939     return;
2940   }
2941 
2942   while (true) {
2943     if (sr_notify(osthread) == 0) {
2944       if (sr_semaphore.timedwait(0, 2 * NANOSECS_PER_MILLISEC)) {
2945         if (osthread->sr.is_running()) {
2946           return;
2947         }
2948       }
2949     } else {
2950       ShouldNotReachHere();
2951     }
2952   }
2953 
2954   guarantee(osthread->sr.is_running(), "Must be running!");
2955 }
2956 












































2957 ///////////////////////////////////////////////////////////////////////////////////
2958 // signal handling (except suspend/resume)
2959 
2960 // This routine may be used by user applications as a "hook" to catch signals.
2961 // The user-defined signal handler must pass unrecognized signals to this
2962 // routine, and if it returns true (non-zero), then the signal handler must
2963 // return immediately.  If the flag "abort_if_unrecognized" is true, then this
2964 // routine will never retun false (zero), but instead will execute a VM panic
2965 // routine kill the process.
2966 //
2967 // If this routine returns false, it is OK to call it again.  This allows
2968 // the user-defined signal handler to perform checks either before or after
2969 // the VM performs its own checks.  Naturally, the user code would be making
2970 // a serious error if it tried to handle an exception (such as a null check
2971 // or breakpoint) that the VM was generating for its own correct operation.
2972 //
2973 // This routine may recognize any of the following kinds of signals:
2974 //    SIGBUS, SIGSEGV, SIGILL, SIGFPE, SIGQUIT, SIGPIPE, SIGXFSZ, SIGUSR1.
2975 // It should be consulted by handlers for any of those signals.
2976 //