src/os/solaris/vm/os_solaris.cpp

Print this page


   1 /*
   2  * Copyright (c) 1997, 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  *


 315 
 316 // setup_interruptible saves the thread state before going into an
 317 // interruptible system call.
 318 // The saved state is used to restore the thread to
 319 // its former state whether or not an interrupt is received.
 320 // Used by classloader os::read
 321 // os::restartable_read calls skip this layer and stay in _thread_in_native
 322 
 323 void os::Solaris::setup_interruptible(JavaThread* thread) {
 324 
 325   JavaThreadState thread_state = thread->thread_state();
 326 
 327   assert(thread_state != _thread_blocked, "Coming from the wrong thread");
 328   assert(thread_state != _thread_in_native, "Native threads skip setup_interruptible");
 329   OSThread* osthread = thread->osthread();
 330   osthread->set_saved_interrupt_thread_state(thread_state);
 331   thread->frame_anchor()->make_walkable(thread);
 332   ThreadStateTransition::transition(thread, thread_state, _thread_blocked);
 333 }
 334 
 335 // Version of setup_interruptible() for threads that are already in
 336 // _thread_blocked. Used by os_sleep().
 337 void os::Solaris::setup_interruptible_already_blocked(JavaThread* thread) {
 338   thread->frame_anchor()->make_walkable(thread);
 339 }
 340 
 341 JavaThread* os::Solaris::setup_interruptible() {
 342   JavaThread* thread = (JavaThread*)ThreadLocalStorage::thread();
 343   setup_interruptible(thread);
 344   return thread;
 345 }
 346 
 347 void os::Solaris::try_enable_extended_io() {
 348   typedef int (*enable_extended_FILE_stdio_t)(int, int);
 349 
 350   if (!UseExtendedFileIO) {
 351     return;
 352   }
 353 
 354   enable_extended_FILE_stdio_t enabler =
 355     (enable_extended_FILE_stdio_t) dlsym(RTLD_DEFAULT,
 356                                          "enable_extended_FILE_stdio");
 357   if (enabler) {
 358     enabler(-1, -1);
 359   }
 360 }


3396 
3397 bool os::release_memory_special(char* base, size_t bytes) {
3398   fatal("os::release_memory_special should not be called on Solaris.");
3399   return false;
3400 }
3401 
3402 size_t os::large_page_size() {
3403   return _large_page_size;
3404 }
3405 
3406 // MPSS allows application to commit large page memory on demand; with ISM
3407 // the entire memory region must be allocated as shared memory.
3408 bool os::can_commit_large_page_memory() {
3409   return true;
3410 }
3411 
3412 bool os::can_execute_large_page_memory() {
3413   return true;
3414 }
3415 
3416 static int os_sleep(jlong millis, bool interruptible) {
3417   const jlong limit = INT_MAX;
3418   jlong prevtime;
3419   int res;
3420 
3421   while (millis > limit) {
3422     if ((res = os_sleep(limit, interruptible)) != OS_OK)
3423       return res;
3424     millis -= limit;
3425   }
3426 
3427   // Restart interrupted polls with new parameters until the proper delay
3428   // has been completed.
3429 
3430   prevtime = getTimeMillis();
3431 
3432   while (millis > 0) {
3433     jlong newtime;
3434 
3435     if (!interruptible) {
3436       // Following assert fails for os::yield_all:
3437       // assert(!thread->is_Java_thread(), "must not be java thread");
3438       res = poll(NULL, 0, millis);
3439     } else {
3440       JavaThread *jt = JavaThread::current();
3441 
3442       INTERRUPTIBLE_NORESTART_VM_ALWAYS(poll(NULL, 0, millis), res, jt,
3443         os::Solaris::clear_interrupted);
3444     }
3445 
3446     // INTERRUPTIBLE_NORESTART_VM_ALWAYS returns res == OS_INTRPT for
3447     // thread.Interrupt.
3448 
3449     // See c/r 6751923. Poll can return 0 before time
3450     // has elapsed if time is set via clock_settime (as NTP does).
3451     // res == 0 if poll timed out (see man poll RETURN VALUES)
3452     // using the logic below checks that we really did
3453     // sleep at least "millis" if not we'll sleep again.
3454     if( ( res == 0 ) || ((res == OS_ERR) && (errno == EINTR))) {
3455       newtime = getTimeMillis();
3456       assert(newtime >= prevtime, "time moving backwards");
3457     /* Doing prevtime and newtime in microseconds doesn't help precision,
3458        and trying to round up to avoid lost milliseconds can result in a
3459        too-short delay. */
3460       millis -= newtime - prevtime;
3461       if(millis <= 0)
3462         return OS_OK;
3463       prevtime = newtime;
3464     } else
3465       return res;
3466   }
3467 
3468   return OS_OK;
3469 }
3470 
3471 // Read calls from inside the vm need to perform state transitions
3472 size_t os::read(int fd, void *buf, unsigned int nBytes) {
3473   INTERRUPTIBLE_RETURN_INT_VM(::read(fd, buf, nBytes), os::Solaris::clear_interrupted);
3474 }
3475 
3476 size_t os::restartable_read(int fd, void *buf, unsigned int nBytes) {
3477   INTERRUPTIBLE_RETURN_INT(::read(fd, buf, nBytes), os::Solaris::clear_interrupted);
3478 }
3479 
3480 int os::sleep(Thread* thread, jlong millis, bool interruptible) {
3481   assert(thread == Thread::current(),  "thread consistency check");
3482 
3483   // TODO-FIXME: this should be removed.
3484   // On Solaris machines (especially 2.5.1) we found that sometimes the VM gets into a live lock
3485   // situation with a JavaThread being starved out of a lwp. The kernel doesn't seem to generate
3486   // a SIGWAITING signal which would enable the threads library to create a new lwp for the starving
3487   // thread. We suspect that because the Watcher thread keeps waking up at periodic intervals the kernel
3488   // is fooled into believing that the system is making progress. In the code below we block the
3489   // the watcher thread while safepoint is in progress so that it would not appear as though the
3490   // system is making progress.
3491   if (!Solaris::T2_libthread() &&
3492       thread->is_Watcher_thread() && SafepointSynchronize::is_synchronizing() && !Arguments::has_profile()) {
3493     // We now try to acquire the threads lock. Since this lock is held by the VM thread during
3494     // the entire safepoint, the watcher thread will  line up here during the safepoint.
3495     Threads_lock->lock_without_safepoint_check();
3496     Threads_lock->unlock();
3497   }
3498 
3499   if (thread->is_Java_thread()) {
3500     // This is a JavaThread so we honor the _thread_blocked protocol
3501     // even for sleeps of 0 milliseconds. This was originally done
3502     // as a workaround for bug 4338139. However, now we also do it
3503     // to honor the suspend-equivalent protocol.
3504 
3505     JavaThread *jt = (JavaThread *) thread;
3506     ThreadBlockInVM tbivm(jt);
3507 
3508     jt->set_suspend_equivalent();
3509     // cleared by handle_special_suspend_equivalent_condition() or
3510     // java_suspend_self() via check_and_wait_while_suspended()
3511 
3512     int ret_code;
3513     if (millis <= 0) {
3514       thr_yield();
3515       ret_code = 0;
3516     } else {
3517       // The original sleep() implementation did not create an
3518       // OSThreadWaitState helper for sleeps of 0 milliseconds.
3519       // I'm preserving that decision for now.
3520       OSThreadWaitState osts(jt->osthread(), false /* not Object.wait() */);
3521 
3522       ret_code = os_sleep(millis, interruptible);
3523     }
3524 
3525     // were we externally suspended while we were waiting?
3526     jt->check_and_wait_while_suspended();
3527 
3528     return ret_code;
3529   }
3530 
3531   // non-JavaThread from this point on:
3532 
3533   if (millis <= 0) {
3534     thr_yield();
3535     return 0;
3536   }
3537 
3538   OSThreadWaitState osts(thread->osthread(), false /* not Object.wait() */);
3539 
3540   return os_sleep(millis, interruptible);
3541 }
3542 
3543 int os::naked_sleep() {
3544   // %% make the sleep time an integer flag. for now use 1 millisec.
3545   return os_sleep(1, false);
3546 }
3547 
3548 // Sleep forever; naked call to OS-specific sleep; use with CAUTION
3549 void os::infinite_sleep() {
3550   while (true) {    // sleep forever ...
3551     ::sleep(100);   // ... 100 seconds at a time
3552   }
3553 }
3554 
3555 // Used to convert frequent JVM_Yield() to nops
3556 bool os::dont_yield() {
3557   if (DontYieldALot) {
3558     static hrtime_t last_time = 0;
3559     hrtime_t diff = getTimeNanos() - last_time;
3560 
3561     if (diff < DontYieldALotInterval * 1000000)
3562       return true;
3563 
3564     last_time += diff;
3565 
3566     return false;
3567   }


4160       }
4161 
4162     } else if (state == os::SuspendResume::SR_RUNNING) {
4163       // request was cancelled, continue
4164     } else {
4165       ShouldNotReachHere();
4166     }
4167 
4168     resume_clear_context(osthread);
4169   } else if (current == os::SuspendResume::SR_RUNNING) {
4170     // request was cancelled, continue
4171   } else if (current == os::SuspendResume::SR_WAKEUP_REQUEST) {
4172     // ignore
4173   } else {
4174     // ignore
4175   }
4176 
4177   errno = old_errno;
4178 }
4179 
4180 
4181 void os::interrupt(Thread* thread) {
4182   assert(Thread::current() == thread || Threads_lock->owned_by_self(), "possibility of dangling Thread pointer");
4183 
4184   OSThread* osthread = thread->osthread();
4185 
4186   int isInterrupted = osthread->interrupted();
4187   if (!isInterrupted) {
4188       osthread->set_interrupted(true);
4189       OrderAccess::fence();
4190       // os::sleep() is implemented with either poll (NULL,0,timeout) or
4191       // by parking on _SleepEvent.  If the former, thr_kill will unwedge
4192       // the sleeper by SIGINTR, otherwise the unpark() will wake the sleeper.
4193       ParkEvent * const slp = thread->_SleepEvent ;
4194       if (slp != NULL) slp->unpark() ;
4195   }
4196 
4197   // For JSR166:  unpark after setting status but before thr_kill -dl
4198   if (thread->is_Java_thread()) {
4199     ((JavaThread*)thread)->parker()->unpark();
4200   }
4201 
4202   // Handle interruptible wait() ...
4203   ParkEvent * const ev = thread->_ParkEvent ;
4204   if (ev != NULL) ev->unpark() ;
4205 
4206   // When events are used everywhere for os::sleep, then this thr_kill
4207   // will only be needed if UseVMInterruptibleIO is true.
4208 
4209   if (!isInterrupted) {
4210     int status = thr_kill(osthread->thread_id(), os::Solaris::SIGinterrupt());
4211     assert_status(status == 0, status, "thr_kill");
4212 
4213     // Bump thread interruption counter
4214     RuntimeService::record_thread_interrupt_signaled_count();
4215   }
4216 }
4217 
4218 
4219 bool os::is_interrupted(Thread* thread, bool clear_interrupted) {
4220   assert(Thread::current() == thread || Threads_lock->owned_by_self(), "possibility of dangling Thread pointer");
4221 
4222   OSThread* osthread = thread->osthread();
4223 
4224   bool res = osthread->interrupted();
4225 
4226   // NOTE that since there is no "lock" around these two operations,
4227   // there is the possibility that the interrupted flag will be
4228   // "false" but that the interrupt event will be set. This is
4229   // intentional. The effect of this is that Object.wait() will appear
4230   // to have a spurious wakeup, which is not harmful, and the
4231   // possibility is so rare that it is not worth the added complexity
4232   // to add yet another lock. It has also been recommended not to put
4233   // the interrupted flag into the os::Solaris::Event structure,
4234   // because it hides the issue.
4235   if (res && clear_interrupted) {
4236     osthread->set_interrupted(false);
4237   }
4238   return res;
4239 }
4240 
4241 
4242 void os::print_statistics() {
4243 }
4244 
4245 int os::message_box(const char* title, const char* message) {
4246   int i;
4247   fdStream err(defaultStream::error_fd());
4248   for (i = 0; i < 78; i++) err.print_raw("=");
4249   err.cr();
4250   err.print_raw_cr(title);
4251   for (i = 0; i < 78; i++) err.print_raw("-");
4252   err.cr();
4253   err.print_raw_cr(message);
4254   for (i = 0; i < 78; i++) err.print_raw("=");
4255   err.cr();
4256 
4257   char buf[16];
4258   // Prevent process from exiting upon "read error" without consuming all CPU
4259   while (::read(0, buf, sizeof(buf)) <= 0) { ::sleep(100); }
4260 
4261   return buf[0] == 'y' || buf[0] == 'Y';


   1 /*
   2  * Copyright (c) 1997, 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  *


 315 
 316 // setup_interruptible saves the thread state before going into an
 317 // interruptible system call.
 318 // The saved state is used to restore the thread to
 319 // its former state whether or not an interrupt is received.
 320 // Used by classloader os::read
 321 // os::restartable_read calls skip this layer and stay in _thread_in_native
 322 
 323 void os::Solaris::setup_interruptible(JavaThread* thread) {
 324 
 325   JavaThreadState thread_state = thread->thread_state();
 326 
 327   assert(thread_state != _thread_blocked, "Coming from the wrong thread");
 328   assert(thread_state != _thread_in_native, "Native threads skip setup_interruptible");
 329   OSThread* osthread = thread->osthread();
 330   osthread->set_saved_interrupt_thread_state(thread_state);
 331   thread->frame_anchor()->make_walkable(thread);
 332   ThreadStateTransition::transition(thread, thread_state, _thread_blocked);
 333 }
 334 






 335 JavaThread* os::Solaris::setup_interruptible() {
 336   JavaThread* thread = (JavaThread*)ThreadLocalStorage::thread();
 337   setup_interruptible(thread);
 338   return thread;
 339 }
 340 
 341 void os::Solaris::try_enable_extended_io() {
 342   typedef int (*enable_extended_FILE_stdio_t)(int, int);
 343 
 344   if (!UseExtendedFileIO) {
 345     return;
 346   }
 347 
 348   enable_extended_FILE_stdio_t enabler =
 349     (enable_extended_FILE_stdio_t) dlsym(RTLD_DEFAULT,
 350                                          "enable_extended_FILE_stdio");
 351   if (enabler) {
 352     enabler(-1, -1);
 353   }
 354 }


3390 
3391 bool os::release_memory_special(char* base, size_t bytes) {
3392   fatal("os::release_memory_special should not be called on Solaris.");
3393   return false;
3394 }
3395 
3396 size_t os::large_page_size() {
3397   return _large_page_size;
3398 }
3399 
3400 // MPSS allows application to commit large page memory on demand; with ISM
3401 // the entire memory region must be allocated as shared memory.
3402 bool os::can_commit_large_page_memory() {
3403   return true;
3404 }
3405 
3406 bool os::can_execute_large_page_memory() {
3407   return true;
3408 }
3409 























































3410 // Read calls from inside the vm need to perform state transitions
3411 size_t os::read(int fd, void *buf, unsigned int nBytes) {
3412   INTERRUPTIBLE_RETURN_INT_VM(::read(fd, buf, nBytes), os::Solaris::clear_interrupted);
3413 }
3414 
3415 size_t os::restartable_read(int fd, void *buf, unsigned int nBytes) {
3416   INTERRUPTIBLE_RETURN_INT(::read(fd, buf, nBytes), os::Solaris::clear_interrupted);
3417 }
3418 




































































3419 // Sleep forever; naked call to OS-specific sleep; use with CAUTION
3420 void os::infinite_sleep() {
3421   while (true) {    // sleep forever ...
3422     ::sleep(100);   // ... 100 seconds at a time
3423   }
3424 }
3425 
3426 // Used to convert frequent JVM_Yield() to nops
3427 bool os::dont_yield() {
3428   if (DontYieldALot) {
3429     static hrtime_t last_time = 0;
3430     hrtime_t diff = getTimeNanos() - last_time;
3431 
3432     if (diff < DontYieldALotInterval * 1000000)
3433       return true;
3434 
3435     last_time += diff;
3436 
3437     return false;
3438   }


4031       }
4032 
4033     } else if (state == os::SuspendResume::SR_RUNNING) {
4034       // request was cancelled, continue
4035     } else {
4036       ShouldNotReachHere();
4037     }
4038 
4039     resume_clear_context(osthread);
4040   } else if (current == os::SuspendResume::SR_RUNNING) {
4041     // request was cancelled, continue
4042   } else if (current == os::SuspendResume::SR_WAKEUP_REQUEST) {
4043     // ignore
4044   } else {
4045     // ignore
4046   }
4047 
4048   errno = old_errno;
4049 }
4050 






























































4051 void os::print_statistics() {
4052 }
4053 
4054 int os::message_box(const char* title, const char* message) {
4055   int i;
4056   fdStream err(defaultStream::error_fd());
4057   for (i = 0; i < 78; i++) err.print_raw("=");
4058   err.cr();
4059   err.print_raw_cr(title);
4060   for (i = 0; i < 78; i++) err.print_raw("-");
4061   err.cr();
4062   err.print_raw_cr(message);
4063   for (i = 0; i < 78; i++) err.print_raw("=");
4064   err.cr();
4065 
4066   char buf[16];
4067   // Prevent process from exiting upon "read error" without consuming all CPU
4068   while (::read(0, buf, sizeof(buf)) <= 0) { ::sleep(100); }
4069 
4070   return buf[0] == 'y' || buf[0] == 'Y';