1 /* 2 * Copyright (c) 1999, 2017, 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 * 23 */ 24 25 #include "jvm.h" 26 #include "utilities/globalDefinitions.hpp" 27 #include "runtime/frame.inline.hpp" 28 #include "runtime/interfaceSupport.hpp" 29 #include "runtime/os.hpp" 30 #include "utilities/align.hpp" 31 #include "utilities/macros.hpp" 32 #include "utilities/vmError.hpp" 33 34 #ifndef __APPLE__ 35 // POSIX unamed semaphores are not supported on OS X. 36 #include "semaphore_posix.hpp" 37 #endif 38 39 #include <dlfcn.h> 40 #include <pthread.h> 41 #include <semaphore.h> 42 #include <signal.h> 43 #include <sys/mman.h> 44 #include <sys/resource.h> 45 #include <sys/utsname.h> 46 #include <time.h> 47 #include <unistd.h> 48 49 // Todo: provide a os::get_max_process_id() or similar. Number of processes 50 // may have been configured, can be read more accurately from proc fs etc. 51 #ifndef MAX_PID 52 #define MAX_PID INT_MAX 53 #endif 54 #define IS_VALID_PID(p) (p > 0 && p < MAX_PID) 55 56 #ifndef MAP_ANONYMOUS 57 #define MAP_ANONYMOUS MAP_ANON 58 #endif 59 60 #define check_with_errno(check_type, cond, msg) \ 61 do { \ 62 int err = errno; \ 63 check_type(cond, "%s; error='%s' (errno=%s)", msg, os::strerror(err), \ 64 os::errno_name(err)); \ 65 } while (false) 66 67 #define assert_with_errno(cond, msg) check_with_errno(assert, cond, msg) 68 #define guarantee_with_errno(cond, msg) check_with_errno(guarantee, cond, msg) 69 70 // Check core dump limit and report possible place where core can be found 71 void os::check_dump_limit(char* buffer, size_t bufferSize) { 72 if (!FLAG_IS_DEFAULT(CreateCoredumpOnCrash) && !CreateCoredumpOnCrash) { 73 jio_snprintf(buffer, bufferSize, "CreateCoredumpOnCrash is disabled from command line"); 74 VMError::record_coredump_status(buffer, false); 75 return; 76 } 77 78 int n; 79 struct rlimit rlim; 80 bool success; 81 82 char core_path[PATH_MAX]; 83 n = get_core_path(core_path, PATH_MAX); 84 85 if (n <= 0) { 86 jio_snprintf(buffer, bufferSize, "core.%d (may not exist)", current_process_id()); 87 success = true; 88 #ifdef LINUX 89 } else if (core_path[0] == '"') { // redirect to user process 90 jio_snprintf(buffer, bufferSize, "Core dumps may be processed with %s", core_path); 91 success = true; 92 #endif 93 } else if (getrlimit(RLIMIT_CORE, &rlim) != 0) { 94 jio_snprintf(buffer, bufferSize, "%s (may not exist)", core_path); 95 success = true; 96 } else { 97 switch(rlim.rlim_cur) { 98 case RLIM_INFINITY: 99 jio_snprintf(buffer, bufferSize, "%s", core_path); 100 success = true; 101 break; 102 case 0: 103 jio_snprintf(buffer, bufferSize, "Core dumps have been disabled. To enable core dumping, try \"ulimit -c unlimited\" before starting Java again"); 104 success = false; 105 break; 106 default: 107 jio_snprintf(buffer, bufferSize, "%s (max size " UINT64_FORMAT " kB). To ensure a full core dump, try \"ulimit -c unlimited\" before starting Java again", core_path, uint64_t(rlim.rlim_cur) / 1024); 108 success = true; 109 break; 110 } 111 } 112 113 VMError::record_coredump_status(buffer, success); 114 } 115 116 int os::get_native_stack(address* stack, int frames, int toSkip) { 117 int frame_idx = 0; 118 int num_of_frames; // number of frames captured 119 frame fr = os::current_frame(); 120 while (fr.pc() && frame_idx < frames) { 121 if (toSkip > 0) { 122 toSkip --; 123 } else { 124 stack[frame_idx ++] = fr.pc(); 125 } 126 if (fr.fp() == NULL || fr.cb() != NULL || 127 fr.sender_pc() == NULL || os::is_first_C_frame(&fr)) break; 128 129 if (fr.sender_pc() && !os::is_first_C_frame(&fr)) { 130 fr = os::get_sender_for_C_frame(&fr); 131 } else { 132 break; 133 } 134 } 135 num_of_frames = frame_idx; 136 for (; frame_idx < frames; frame_idx ++) { 137 stack[frame_idx] = NULL; 138 } 139 140 return num_of_frames; 141 } 142 143 144 bool os::unsetenv(const char* name) { 145 assert(name != NULL, "Null pointer"); 146 return (::unsetenv(name) == 0); 147 } 148 149 int os::get_last_error() { 150 return errno; 151 } 152 153 bool os::is_debugger_attached() { 154 // not implemented 155 return false; 156 } 157 158 void os::wait_for_keypress_at_exit(void) { 159 // don't do anything on posix platforms 160 return; 161 } 162 163 int os::create_file_for_heap(const char* dir) { 164 165 const char name_template[] = "/jvmheap.XXXXXX"; 166 167 char *fullname = (char*)os::malloc((strlen(dir) + strlen(name_template) + 1), mtInternal); 168 if (fullname == NULL) { 169 vm_exit_during_initialization(err_msg("Malloc failed during creation of backing file for heap (%s)", os::strerror(errno))); 170 return -1; 171 } 172 (void)strncpy(fullname, dir, strlen(dir)+1); 173 (void)strncat(fullname, name_template, strlen(name_template)); 174 175 os::native_path(fullname); 176 177 sigset_t set, oldset; 178 int ret = sigfillset(&set); 179 assert_with_errno(ret == 0, "sigfillset returned error"); 180 181 // set the file creation mask. 182 mode_t file_mode = S_IRUSR | S_IWUSR; 183 184 // create a new file. 185 int fd = mkstemp(fullname); 186 187 if (fd < 0) { 188 warning("Could not create file for heap with template %s", fullname); 189 os::free(fullname); 190 return -1; 191 } 192 193 // delete the name from the filesystem. When 'fd' is closed, the file (and space) will be deleted. 194 ret = unlink(fullname); 195 assert_with_errno(ret == 0, "unlink returned error"); 196 197 os::free(fullname); 198 return fd; 199 } 200 201 static char* reserve_mmapped_memory(size_t bytes, char* requested_addr) { 202 char * addr; 203 int flags = MAP_PRIVATE NOT_AIX( | MAP_NORESERVE ) | MAP_ANONYMOUS; 204 if (requested_addr != NULL) { 205 assert((uintptr_t)requested_addr % os::vm_page_size() == 0, "Requested address should be aligned to OS page size"); 206 flags |= MAP_FIXED; 207 } 208 209 // Map reserved/uncommitted pages PROT_NONE so we fail early if we 210 // touch an uncommitted page. Otherwise, the read/write might 211 // succeed if we have enough swap space to back the physical page. 212 addr = (char*)::mmap(requested_addr, bytes, PROT_NONE, 213 flags, -1, 0); 214 215 if (addr != MAP_FAILED) { 216 MemTracker::record_virtual_memory_reserve((address)addr, bytes, CALLER_PC); 217 return addr; 218 } 219 return NULL; 220 } 221 222 static int util_posix_fallocate(int fd, off_t offset, off_t len) { 223 #ifdef __APPLE__ 224 fstore_t store = { F_ALLOCATECONTIG, F_PEOFPOSMODE, 0, len }; 225 // First we try to get a continuous chunk of disk space 226 int ret = fcntl(fd, F_PREALLOCATE, &store); 227 if (ret == -1) { 228 // Maybe we are too fragmented, try to allocate non-continuous range 229 store.fst_flags = F_ALLOCATEALL; 230 ret = fcntl(fd, F_PREALLOCATE, &store); 231 } 232 if(ret != -1) { 233 return ftruncate(fd, len); 234 } 235 return -1; 236 #else 237 return posix_fallocate(fd, offset, len); 238 #endif 239 } 240 241 // Map the given address range to the provided file descriptor. 242 char* os::map_memory_to_file(char* base, size_t size, int fd) { 243 assert(fd != -1, "File descriptor is not valid"); 244 245 // allocate space for the file 246 int ret = util_posix_fallocate(fd, 0, (off_t)size); 247 if (ret != 0) { 248 vm_exit_during_initialization(err_msg("Error in mapping Java heap at the given filesystem directory. error(%d)", ret)); 249 return NULL; 250 } 251 252 int prot = PROT_READ | PROT_WRITE; 253 int flags = MAP_SHARED; 254 if (base != NULL) { 255 flags |= MAP_FIXED; 256 } 257 char* addr = (char*)mmap(base, size, prot, flags, fd, 0); 258 259 if (addr == MAP_FAILED) { 260 warning("Failed mmap to file. (%s)", os::strerror(errno)); 261 return NULL; 262 } 263 if (base != NULL && addr != base) { 264 if (!os::release_memory(addr, size)) { 265 warning("Could not release memory on unsuccessful file mapping"); 266 } 267 return NULL; 268 } 269 return addr; 270 } 271 272 char* os::replace_existing_mapping_with_file_mapping(char* base, size_t size, int fd) { 273 assert(fd != -1, "File descriptor is not valid"); 274 assert(base != NULL, "Base cannot be NULL"); 275 276 return map_memory_to_file(base, size, fd); 277 } 278 279 // Multiple threads can race in this code, and can remap over each other with MAP_FIXED, 280 // so on posix, unmap the section at the start and at the end of the chunk that we mapped 281 // rather than unmapping and remapping the whole chunk to get requested alignment. 282 char* os::reserve_memory_aligned(size_t size, size_t alignment, int file_desc) { 283 assert((alignment & (os::vm_allocation_granularity() - 1)) == 0, 284 "Alignment must be a multiple of allocation granularity (page size)"); 285 assert((size & (alignment -1)) == 0, "size must be 'alignment' aligned"); 286 287 size_t extra_size = size + alignment; 288 assert(extra_size >= size, "overflow, size is too large to allow alignment"); 289 290 char* extra_base; 291 if (file_desc != -1) { 292 // For file mapping, we do not call os:reserve_memory(extra_size, NULL, alignment, file_desc) because 293 // we need to deal with shrinking of the file space later when we release extra memory after alignment. 294 // We also cannot called os:reserve_memory() with file_desc set to -1 because on aix we might get SHM memory. 295 // So here to call a helper function while reserve memory for us. After we have a aligned base, 296 // we will replace anonymous mapping with file mapping. 297 extra_base = reserve_mmapped_memory(extra_size, NULL); 298 if (extra_base != NULL) { 299 MemTracker::record_virtual_memory_reserve((address)extra_base, extra_size, CALLER_PC); 300 } 301 } else { 302 extra_base = os::reserve_memory(extra_size, NULL, alignment); 303 } 304 305 if (extra_base == NULL) { 306 return NULL; 307 } 308 309 // Do manual alignment 310 char* aligned_base = align_up(extra_base, alignment); 311 312 // [ | | ] 313 // ^ extra_base 314 // ^ extra_base + begin_offset == aligned_base 315 // extra_base + begin_offset + size ^ 316 // extra_base + extra_size ^ 317 // |<>| == begin_offset 318 // end_offset == |<>| 319 size_t begin_offset = aligned_base - extra_base; 320 size_t end_offset = (extra_base + extra_size) - (aligned_base + size); 321 322 if (begin_offset > 0) { 323 os::release_memory(extra_base, begin_offset); 324 } 325 326 if (end_offset > 0) { 327 os::release_memory(extra_base + begin_offset + size, end_offset); 328 } 329 330 if (file_desc != -1) { 331 // After we have an aligned address, we can replace anonymous mapping with file mapping 332 if (replace_existing_mapping_with_file_mapping(aligned_base, size, file_desc) == NULL) { 333 vm_exit_during_initialization(err_msg("Error in mapping Java heap at the given filesystem directory")); 334 } 335 MemTracker::record_virtual_memory_commit((address)aligned_base, size, CALLER_PC); 336 } 337 return aligned_base; 338 } 339 340 int os::log_vsnprintf(char* buf, size_t len, const char* fmt, va_list args) { 341 return vsnprintf(buf, len, fmt, args); 342 } 343 344 int os::get_fileno(FILE* fp) { 345 return NOT_AIX(::)fileno(fp); 346 } 347 348 struct tm* os::gmtime_pd(const time_t* clock, struct tm* res) { 349 return gmtime_r(clock, res); 350 } 351 352 void os::Posix::print_load_average(outputStream* st) { 353 st->print("load average:"); 354 double loadavg[3]; 355 os::loadavg(loadavg, 3); 356 st->print("%0.02f %0.02f %0.02f", loadavg[0], loadavg[1], loadavg[2]); 357 st->cr(); 358 } 359 360 void os::Posix::print_rlimit_info(outputStream* st) { 361 st->print("rlimit:"); 362 struct rlimit rlim; 363 364 st->print(" STACK "); 365 getrlimit(RLIMIT_STACK, &rlim); 366 if (rlim.rlim_cur == RLIM_INFINITY) st->print("infinity"); 367 else st->print(UINT64_FORMAT "k", uint64_t(rlim.rlim_cur) / 1024); 368 369 st->print(", CORE "); 370 getrlimit(RLIMIT_CORE, &rlim); 371 if (rlim.rlim_cur == RLIM_INFINITY) st->print("infinity"); 372 else st->print(UINT64_FORMAT "k", uint64_t(rlim.rlim_cur) / 1024); 373 374 // Isn't there on solaris 375 #if defined(AIX) 376 st->print(", NPROC "); 377 st->print("%d", sysconf(_SC_CHILD_MAX)); 378 #elif !defined(SOLARIS) 379 st->print(", NPROC "); 380 getrlimit(RLIMIT_NPROC, &rlim); 381 if (rlim.rlim_cur == RLIM_INFINITY) st->print("infinity"); 382 else st->print(UINT64_FORMAT, uint64_t(rlim.rlim_cur)); 383 #endif 384 385 st->print(", NOFILE "); 386 getrlimit(RLIMIT_NOFILE, &rlim); 387 if (rlim.rlim_cur == RLIM_INFINITY) st->print("infinity"); 388 else st->print(UINT64_FORMAT, uint64_t(rlim.rlim_cur)); 389 390 st->print(", AS "); 391 getrlimit(RLIMIT_AS, &rlim); 392 if (rlim.rlim_cur == RLIM_INFINITY) st->print("infinity"); 393 else st->print(UINT64_FORMAT "k", uint64_t(rlim.rlim_cur) / 1024); 394 395 st->print(", DATA "); 396 getrlimit(RLIMIT_DATA, &rlim); 397 if (rlim.rlim_cur == RLIM_INFINITY) st->print("infinity"); 398 else st->print(UINT64_FORMAT "k", uint64_t(rlim.rlim_cur) / 1024); 399 400 st->print(", FSIZE "); 401 getrlimit(RLIMIT_FSIZE, &rlim); 402 if (rlim.rlim_cur == RLIM_INFINITY) st->print("infinity"); 403 else st->print(UINT64_FORMAT "k", uint64_t(rlim.rlim_cur) / 1024); 404 405 st->cr(); 406 } 407 408 void os::Posix::print_uname_info(outputStream* st) { 409 // kernel 410 st->print("uname:"); 411 struct utsname name; 412 uname(&name); 413 st->print("%s ", name.sysname); 414 #ifdef ASSERT 415 st->print("%s ", name.nodename); 416 #endif 417 st->print("%s ", name.release); 418 st->print("%s ", name.version); 419 st->print("%s", name.machine); 420 st->cr(); 421 } 422 423 bool os::get_host_name(char* buf, size_t buflen) { 424 struct utsname name; 425 uname(&name); 426 jio_snprintf(buf, buflen, "%s", name.nodename); 427 return true; 428 } 429 430 bool os::has_allocatable_memory_limit(julong* limit) { 431 struct rlimit rlim; 432 int getrlimit_res = getrlimit(RLIMIT_AS, &rlim); 433 // if there was an error when calling getrlimit, assume that there is no limitation 434 // on virtual memory. 435 bool result; 436 if ((getrlimit_res != 0) || (rlim.rlim_cur == RLIM_INFINITY)) { 437 result = false; 438 } else { 439 *limit = (julong)rlim.rlim_cur; 440 result = true; 441 } 442 #ifdef _LP64 443 return result; 444 #else 445 // arbitrary virtual space limit for 32 bit Unices found by testing. If 446 // getrlimit above returned a limit, bound it with this limit. Otherwise 447 // directly use it. 448 const julong max_virtual_limit = (julong)3800*M; 449 if (result) { 450 *limit = MIN2(*limit, max_virtual_limit); 451 } else { 452 *limit = max_virtual_limit; 453 } 454 455 // bound by actually allocatable memory. The algorithm uses two bounds, an 456 // upper and a lower limit. The upper limit is the current highest amount of 457 // memory that could not be allocated, the lower limit is the current highest 458 // amount of memory that could be allocated. 459 // The algorithm iteratively refines the result by halving the difference 460 // between these limits, updating either the upper limit (if that value could 461 // not be allocated) or the lower limit (if the that value could be allocated) 462 // until the difference between these limits is "small". 463 464 // the minimum amount of memory we care about allocating. 465 const julong min_allocation_size = M; 466 467 julong upper_limit = *limit; 468 469 // first check a few trivial cases 470 if (is_allocatable(upper_limit) || (upper_limit <= min_allocation_size)) { 471 *limit = upper_limit; 472 } else if (!is_allocatable(min_allocation_size)) { 473 // we found that not even min_allocation_size is allocatable. Return it 474 // anyway. There is no point to search for a better value any more. 475 *limit = min_allocation_size; 476 } else { 477 // perform the binary search. 478 julong lower_limit = min_allocation_size; 479 while ((upper_limit - lower_limit) > min_allocation_size) { 480 julong temp_limit = ((upper_limit - lower_limit) / 2) + lower_limit; 481 temp_limit = align_down(temp_limit, min_allocation_size); 482 if (is_allocatable(temp_limit)) { 483 lower_limit = temp_limit; 484 } else { 485 upper_limit = temp_limit; 486 } 487 } 488 *limit = lower_limit; 489 } 490 return true; 491 #endif 492 } 493 494 const char* os::get_current_directory(char *buf, size_t buflen) { 495 return getcwd(buf, buflen); 496 } 497 498 FILE* os::open(int fd, const char* mode) { 499 return ::fdopen(fd, mode); 500 } 501 502 void os::flockfile(FILE* fp) { 503 ::flockfile(fp); 504 } 505 506 void os::funlockfile(FILE* fp) { 507 ::funlockfile(fp); 508 } 509 510 // Builds a platform dependent Agent_OnLoad_<lib_name> function name 511 // which is used to find statically linked in agents. 512 // Parameters: 513 // sym_name: Symbol in library we are looking for 514 // lib_name: Name of library to look in, NULL for shared libs. 515 // is_absolute_path == true if lib_name is absolute path to agent 516 // such as "/a/b/libL.so" 517 // == false if only the base name of the library is passed in 518 // such as "L" 519 char* os::build_agent_function_name(const char *sym_name, const char *lib_name, 520 bool is_absolute_path) { 521 char *agent_entry_name; 522 size_t len; 523 size_t name_len; 524 size_t prefix_len = strlen(JNI_LIB_PREFIX); 525 size_t suffix_len = strlen(JNI_LIB_SUFFIX); 526 const char *start; 527 528 if (lib_name != NULL) { 529 name_len = strlen(lib_name); 530 if (is_absolute_path) { 531 // Need to strip path, prefix and suffix 532 if ((start = strrchr(lib_name, *os::file_separator())) != NULL) { 533 lib_name = ++start; 534 } 535 if (strlen(lib_name) <= (prefix_len + suffix_len)) { 536 return NULL; 537 } 538 lib_name += prefix_len; 539 name_len = strlen(lib_name) - suffix_len; 540 } 541 } 542 len = (lib_name != NULL ? name_len : 0) + strlen(sym_name) + 2; 543 agent_entry_name = NEW_C_HEAP_ARRAY_RETURN_NULL(char, len, mtThread); 544 if (agent_entry_name == NULL) { 545 return NULL; 546 } 547 strcpy(agent_entry_name, sym_name); 548 if (lib_name != NULL) { 549 strcat(agent_entry_name, "_"); 550 strncat(agent_entry_name, lib_name, name_len); 551 } 552 return agent_entry_name; 553 } 554 555 int os::sleep(Thread* thread, jlong millis, bool interruptible) { 556 assert(thread == Thread::current(), "thread consistency check"); 557 558 ParkEvent * const slp = thread->_SleepEvent ; 559 slp->reset() ; 560 OrderAccess::fence() ; 561 562 if (interruptible) { 563 jlong prevtime = javaTimeNanos(); 564 565 for (;;) { 566 if (os::is_interrupted(thread, true)) { 567 return OS_INTRPT; 568 } 569 570 jlong newtime = javaTimeNanos(); 571 572 if (newtime - prevtime < 0) { 573 // time moving backwards, should only happen if no monotonic clock 574 // not a guarantee() because JVM should not abort on kernel/glibc bugs 575 assert(!os::supports_monotonic_clock(), "unexpected time moving backwards detected in os::sleep(interruptible)"); 576 } else { 577 millis -= (newtime - prevtime) / NANOSECS_PER_MILLISEC; 578 } 579 580 if (millis <= 0) { 581 return OS_OK; 582 } 583 584 prevtime = newtime; 585 586 { 587 assert(thread->is_Java_thread(), "sanity check"); 588 JavaThread *jt = (JavaThread *) thread; 589 ThreadBlockInVM tbivm(jt); 590 OSThreadWaitState osts(jt->osthread(), false /* not Object.wait() */); 591 592 jt->set_suspend_equivalent(); 593 // cleared by handle_special_suspend_equivalent_condition() or 594 // java_suspend_self() via check_and_wait_while_suspended() 595 596 slp->park(millis); 597 598 // were we externally suspended while we were waiting? 599 jt->check_and_wait_while_suspended(); 600 } 601 } 602 } else { 603 OSThreadWaitState osts(thread->osthread(), false /* not Object.wait() */); 604 jlong prevtime = javaTimeNanos(); 605 606 for (;;) { 607 // It'd be nice to avoid the back-to-back javaTimeNanos() calls on 608 // the 1st iteration ... 609 jlong newtime = javaTimeNanos(); 610 611 if (newtime - prevtime < 0) { 612 // time moving backwards, should only happen if no monotonic clock 613 // not a guarantee() because JVM should not abort on kernel/glibc bugs 614 assert(!os::supports_monotonic_clock(), "unexpected time moving backwards detected on os::sleep(!interruptible)"); 615 } else { 616 millis -= (newtime - prevtime) / NANOSECS_PER_MILLISEC; 617 } 618 619 if (millis <= 0) break ; 620 621 prevtime = newtime; 622 slp->park(millis); 623 } 624 return OS_OK ; 625 } 626 } 627 628 //////////////////////////////////////////////////////////////////////////////// 629 // interrupt support 630 631 void os::interrupt(Thread* thread) { 632 debug_only(Thread::check_for_dangling_thread_pointer(thread);) 633 634 OSThread* osthread = thread->osthread(); 635 636 if (!osthread->interrupted()) { 637 osthread->set_interrupted(true); 638 // More than one thread can get here with the same value of osthread, 639 // resulting in multiple notifications. We do, however, want the store 640 // to interrupted() to be visible to other threads before we execute unpark(). 641 OrderAccess::fence(); 642 ParkEvent * const slp = thread->_SleepEvent ; 643 if (slp != NULL) slp->unpark() ; 644 } 645 646 // For JSR166. Unpark even if interrupt status already was set 647 if (thread->is_Java_thread()) 648 ((JavaThread*)thread)->parker()->unpark(); 649 650 ParkEvent * ev = thread->_ParkEvent ; 651 if (ev != NULL) ev->unpark() ; 652 } 653 654 bool os::is_interrupted(Thread* thread, bool clear_interrupted) { 655 debug_only(Thread::check_for_dangling_thread_pointer(thread);) 656 657 OSThread* osthread = thread->osthread(); 658 659 bool interrupted = osthread->interrupted(); 660 661 // NOTE that since there is no "lock" around the interrupt and 662 // is_interrupted operations, there is the possibility that the 663 // interrupted flag (in osThread) will be "false" but that the 664 // low-level events will be in the signaled state. This is 665 // intentional. The effect of this is that Object.wait() and 666 // LockSupport.park() will appear to have a spurious wakeup, which 667 // is allowed and not harmful, and the possibility is so rare that 668 // it is not worth the added complexity to add yet another lock. 669 // For the sleep event an explicit reset is performed on entry 670 // to os::sleep, so there is no early return. It has also been 671 // recommended not to put the interrupted flag into the "event" 672 // structure because it hides the issue. 673 if (interrupted && clear_interrupted) { 674 osthread->set_interrupted(false); 675 // consider thread->_SleepEvent->reset() ... optional optimization 676 } 677 678 return interrupted; 679 } 680 681 682 683 static const struct { 684 int sig; const char* name; 685 } 686 g_signal_info[] = 687 { 688 { SIGABRT, "SIGABRT" }, 689 #ifdef SIGAIO 690 { SIGAIO, "SIGAIO" }, 691 #endif 692 { SIGALRM, "SIGALRM" }, 693 #ifdef SIGALRM1 694 { SIGALRM1, "SIGALRM1" }, 695 #endif 696 { SIGBUS, "SIGBUS" }, 697 #ifdef SIGCANCEL 698 { SIGCANCEL, "SIGCANCEL" }, 699 #endif 700 { SIGCHLD, "SIGCHLD" }, 701 #ifdef SIGCLD 702 { SIGCLD, "SIGCLD" }, 703 #endif 704 { SIGCONT, "SIGCONT" }, 705 #ifdef SIGCPUFAIL 706 { SIGCPUFAIL, "SIGCPUFAIL" }, 707 #endif 708 #ifdef SIGDANGER 709 { SIGDANGER, "SIGDANGER" }, 710 #endif 711 #ifdef SIGDIL 712 { SIGDIL, "SIGDIL" }, 713 #endif 714 #ifdef SIGEMT 715 { SIGEMT, "SIGEMT" }, 716 #endif 717 { SIGFPE, "SIGFPE" }, 718 #ifdef SIGFREEZE 719 { SIGFREEZE, "SIGFREEZE" }, 720 #endif 721 #ifdef SIGGFAULT 722 { SIGGFAULT, "SIGGFAULT" }, 723 #endif 724 #ifdef SIGGRANT 725 { SIGGRANT, "SIGGRANT" }, 726 #endif 727 { SIGHUP, "SIGHUP" }, 728 { SIGILL, "SIGILL" }, 729 { SIGINT, "SIGINT" }, 730 #ifdef SIGIO 731 { SIGIO, "SIGIO" }, 732 #endif 733 #ifdef SIGIOINT 734 { SIGIOINT, "SIGIOINT" }, 735 #endif 736 #ifdef SIGIOT 737 // SIGIOT is there for BSD compatibility, but on most Unices just a 738 // synonym for SIGABRT. The result should be "SIGABRT", not 739 // "SIGIOT". 740 #if (SIGIOT != SIGABRT ) 741 { SIGIOT, "SIGIOT" }, 742 #endif 743 #endif 744 #ifdef SIGKAP 745 { SIGKAP, "SIGKAP" }, 746 #endif 747 { SIGKILL, "SIGKILL" }, 748 #ifdef SIGLOST 749 { SIGLOST, "SIGLOST" }, 750 #endif 751 #ifdef SIGLWP 752 { SIGLWP, "SIGLWP" }, 753 #endif 754 #ifdef SIGLWPTIMER 755 { SIGLWPTIMER, "SIGLWPTIMER" }, 756 #endif 757 #ifdef SIGMIGRATE 758 { SIGMIGRATE, "SIGMIGRATE" }, 759 #endif 760 #ifdef SIGMSG 761 { SIGMSG, "SIGMSG" }, 762 #endif 763 { SIGPIPE, "SIGPIPE" }, 764 #ifdef SIGPOLL 765 { SIGPOLL, "SIGPOLL" }, 766 #endif 767 #ifdef SIGPRE 768 { SIGPRE, "SIGPRE" }, 769 #endif 770 { SIGPROF, "SIGPROF" }, 771 #ifdef SIGPTY 772 { SIGPTY, "SIGPTY" }, 773 #endif 774 #ifdef SIGPWR 775 { SIGPWR, "SIGPWR" }, 776 #endif 777 { SIGQUIT, "SIGQUIT" }, 778 #ifdef SIGRECONFIG 779 { SIGRECONFIG, "SIGRECONFIG" }, 780 #endif 781 #ifdef SIGRECOVERY 782 { SIGRECOVERY, "SIGRECOVERY" }, 783 #endif 784 #ifdef SIGRESERVE 785 { SIGRESERVE, "SIGRESERVE" }, 786 #endif 787 #ifdef SIGRETRACT 788 { SIGRETRACT, "SIGRETRACT" }, 789 #endif 790 #ifdef SIGSAK 791 { SIGSAK, "SIGSAK" }, 792 #endif 793 { SIGSEGV, "SIGSEGV" }, 794 #ifdef SIGSOUND 795 { SIGSOUND, "SIGSOUND" }, 796 #endif 797 #ifdef SIGSTKFLT 798 { SIGSTKFLT, "SIGSTKFLT" }, 799 #endif 800 { SIGSTOP, "SIGSTOP" }, 801 { SIGSYS, "SIGSYS" }, 802 #ifdef SIGSYSERROR 803 { SIGSYSERROR, "SIGSYSERROR" }, 804 #endif 805 #ifdef SIGTALRM 806 { SIGTALRM, "SIGTALRM" }, 807 #endif 808 { SIGTERM, "SIGTERM" }, 809 #ifdef SIGTHAW 810 { SIGTHAW, "SIGTHAW" }, 811 #endif 812 { SIGTRAP, "SIGTRAP" }, 813 #ifdef SIGTSTP 814 { SIGTSTP, "SIGTSTP" }, 815 #endif 816 { SIGTTIN, "SIGTTIN" }, 817 { SIGTTOU, "SIGTTOU" }, 818 #ifdef SIGURG 819 { SIGURG, "SIGURG" }, 820 #endif 821 { SIGUSR1, "SIGUSR1" }, 822 { SIGUSR2, "SIGUSR2" }, 823 #ifdef SIGVIRT 824 { SIGVIRT, "SIGVIRT" }, 825 #endif 826 { SIGVTALRM, "SIGVTALRM" }, 827 #ifdef SIGWAITING 828 { SIGWAITING, "SIGWAITING" }, 829 #endif 830 #ifdef SIGWINCH 831 { SIGWINCH, "SIGWINCH" }, 832 #endif 833 #ifdef SIGWINDOW 834 { SIGWINDOW, "SIGWINDOW" }, 835 #endif 836 { SIGXCPU, "SIGXCPU" }, 837 { SIGXFSZ, "SIGXFSZ" }, 838 #ifdef SIGXRES 839 { SIGXRES, "SIGXRES" }, 840 #endif 841 { -1, NULL } 842 }; 843 844 // Returned string is a constant. For unknown signals "UNKNOWN" is returned. 845 const char* os::Posix::get_signal_name(int sig, char* out, size_t outlen) { 846 847 const char* ret = NULL; 848 849 #ifdef SIGRTMIN 850 if (sig >= SIGRTMIN && sig <= SIGRTMAX) { 851 if (sig == SIGRTMIN) { 852 ret = "SIGRTMIN"; 853 } else if (sig == SIGRTMAX) { 854 ret = "SIGRTMAX"; 855 } else { 856 jio_snprintf(out, outlen, "SIGRTMIN+%d", sig - SIGRTMIN); 857 return out; 858 } 859 } 860 #endif 861 862 if (sig > 0) { 863 for (int idx = 0; g_signal_info[idx].sig != -1; idx ++) { 864 if (g_signal_info[idx].sig == sig) { 865 ret = g_signal_info[idx].name; 866 break; 867 } 868 } 869 } 870 871 if (!ret) { 872 if (!is_valid_signal(sig)) { 873 ret = "INVALID"; 874 } else { 875 ret = "UNKNOWN"; 876 } 877 } 878 879 if (out && outlen > 0) { 880 strncpy(out, ret, outlen); 881 out[outlen - 1] = '\0'; 882 } 883 return out; 884 } 885 886 int os::Posix::get_signal_number(const char* signal_name) { 887 char tmp[30]; 888 const char* s = signal_name; 889 if (s[0] != 'S' || s[1] != 'I' || s[2] != 'G') { 890 jio_snprintf(tmp, sizeof(tmp), "SIG%s", signal_name); 891 s = tmp; 892 } 893 for (int idx = 0; g_signal_info[idx].sig != -1; idx ++) { 894 if (strcmp(g_signal_info[idx].name, s) == 0) { 895 return g_signal_info[idx].sig; 896 } 897 } 898 return -1; 899 } 900 901 int os::get_signal_number(const char* signal_name) { 902 return os::Posix::get_signal_number(signal_name); 903 } 904 905 // Returns true if signal number is valid. 906 bool os::Posix::is_valid_signal(int sig) { 907 // MacOS not really POSIX compliant: sigaddset does not return 908 // an error for invalid signal numbers. However, MacOS does not 909 // support real time signals and simply seems to have just 33 910 // signals with no holes in the signal range. 911 #ifdef __APPLE__ 912 return sig >= 1 && sig < NSIG; 913 #else 914 // Use sigaddset to check for signal validity. 915 sigset_t set; 916 sigemptyset(&set); 917 if (sigaddset(&set, sig) == -1 && errno == EINVAL) { 918 return false; 919 } 920 return true; 921 #endif 922 } 923 924 // Returns: 925 // NULL for an invalid signal number 926 // "SIG<num>" for a valid but unknown signal number 927 // signal name otherwise. 928 const char* os::exception_name(int sig, char* buf, size_t size) { 929 if (!os::Posix::is_valid_signal(sig)) { 930 return NULL; 931 } 932 const char* const name = os::Posix::get_signal_name(sig, buf, size); 933 if (strcmp(name, "UNKNOWN") == 0) { 934 jio_snprintf(buf, size, "SIG%d", sig); 935 } 936 return buf; 937 } 938 939 #define NUM_IMPORTANT_SIGS 32 940 // Returns one-line short description of a signal set in a user provided buffer. 941 const char* os::Posix::describe_signal_set_short(const sigset_t* set, char* buffer, size_t buf_size) { 942 assert(buf_size == (NUM_IMPORTANT_SIGS + 1), "wrong buffer size"); 943 // Note: for shortness, just print out the first 32. That should 944 // cover most of the useful ones, apart from realtime signals. 945 for (int sig = 1; sig <= NUM_IMPORTANT_SIGS; sig++) { 946 const int rc = sigismember(set, sig); 947 if (rc == -1 && errno == EINVAL) { 948 buffer[sig-1] = '?'; 949 } else { 950 buffer[sig-1] = rc == 0 ? '0' : '1'; 951 } 952 } 953 buffer[NUM_IMPORTANT_SIGS] = 0; 954 return buffer; 955 } 956 957 // Prints one-line description of a signal set. 958 void os::Posix::print_signal_set_short(outputStream* st, const sigset_t* set) { 959 char buf[NUM_IMPORTANT_SIGS + 1]; 960 os::Posix::describe_signal_set_short(set, buf, sizeof(buf)); 961 st->print("%s", buf); 962 } 963 964 // Writes one-line description of a combination of sigaction.sa_flags into a user 965 // provided buffer. Returns that buffer. 966 const char* os::Posix::describe_sa_flags(int flags, char* buffer, size_t size) { 967 char* p = buffer; 968 size_t remaining = size; 969 bool first = true; 970 int idx = 0; 971 972 assert(buffer, "invalid argument"); 973 974 if (size == 0) { 975 return buffer; 976 } 977 978 strncpy(buffer, "none", size); 979 980 const struct { 981 // NB: i is an unsigned int here because SA_RESETHAND is on some 982 // systems 0x80000000, which is implicitly unsigned. Assignining 983 // it to an int field would be an overflow in unsigned-to-signed 984 // conversion. 985 unsigned int i; 986 const char* s; 987 } flaginfo [] = { 988 { SA_NOCLDSTOP, "SA_NOCLDSTOP" }, 989 { SA_ONSTACK, "SA_ONSTACK" }, 990 { SA_RESETHAND, "SA_RESETHAND" }, 991 { SA_RESTART, "SA_RESTART" }, 992 { SA_SIGINFO, "SA_SIGINFO" }, 993 { SA_NOCLDWAIT, "SA_NOCLDWAIT" }, 994 { SA_NODEFER, "SA_NODEFER" }, 995 #ifdef AIX 996 { SA_ONSTACK, "SA_ONSTACK" }, 997 { SA_OLDSTYLE, "SA_OLDSTYLE" }, 998 #endif 999 { 0, NULL } 1000 }; 1001 1002 for (idx = 0; flaginfo[idx].s && remaining > 1; idx++) { 1003 if (flags & flaginfo[idx].i) { 1004 if (first) { 1005 jio_snprintf(p, remaining, "%s", flaginfo[idx].s); 1006 first = false; 1007 } else { 1008 jio_snprintf(p, remaining, "|%s", flaginfo[idx].s); 1009 } 1010 const size_t len = strlen(p); 1011 p += len; 1012 remaining -= len; 1013 } 1014 } 1015 1016 buffer[size - 1] = '\0'; 1017 1018 return buffer; 1019 } 1020 1021 // Prints one-line description of a combination of sigaction.sa_flags. 1022 void os::Posix::print_sa_flags(outputStream* st, int flags) { 1023 char buffer[0x100]; 1024 os::Posix::describe_sa_flags(flags, buffer, sizeof(buffer)); 1025 st->print("%s", buffer); 1026 } 1027 1028 // Helper function for os::Posix::print_siginfo_...(): 1029 // return a textual description for signal code. 1030 struct enum_sigcode_desc_t { 1031 const char* s_name; 1032 const char* s_desc; 1033 }; 1034 1035 static bool get_signal_code_description(const siginfo_t* si, enum_sigcode_desc_t* out) { 1036 1037 const struct { 1038 int sig; int code; const char* s_code; const char* s_desc; 1039 } t1 [] = { 1040 { SIGILL, ILL_ILLOPC, "ILL_ILLOPC", "Illegal opcode." }, 1041 { SIGILL, ILL_ILLOPN, "ILL_ILLOPN", "Illegal operand." }, 1042 { SIGILL, ILL_ILLADR, "ILL_ILLADR", "Illegal addressing mode." }, 1043 { SIGILL, ILL_ILLTRP, "ILL_ILLTRP", "Illegal trap." }, 1044 { SIGILL, ILL_PRVOPC, "ILL_PRVOPC", "Privileged opcode." }, 1045 { SIGILL, ILL_PRVREG, "ILL_PRVREG", "Privileged register." }, 1046 { SIGILL, ILL_COPROC, "ILL_COPROC", "Coprocessor error." }, 1047 { SIGILL, ILL_BADSTK, "ILL_BADSTK", "Internal stack error." }, 1048 #if defined(IA64) && defined(LINUX) 1049 { SIGILL, ILL_BADIADDR, "ILL_BADIADDR", "Unimplemented instruction address" }, 1050 { SIGILL, ILL_BREAK, "ILL_BREAK", "Application Break instruction" }, 1051 #endif 1052 { SIGFPE, FPE_INTDIV, "FPE_INTDIV", "Integer divide by zero." }, 1053 { SIGFPE, FPE_INTOVF, "FPE_INTOVF", "Integer overflow." }, 1054 { SIGFPE, FPE_FLTDIV, "FPE_FLTDIV", "Floating-point divide by zero." }, 1055 { SIGFPE, FPE_FLTOVF, "FPE_FLTOVF", "Floating-point overflow." }, 1056 { SIGFPE, FPE_FLTUND, "FPE_FLTUND", "Floating-point underflow." }, 1057 { SIGFPE, FPE_FLTRES, "FPE_FLTRES", "Floating-point inexact result." }, 1058 { SIGFPE, FPE_FLTINV, "FPE_FLTINV", "Invalid floating-point operation." }, 1059 { SIGFPE, FPE_FLTSUB, "FPE_FLTSUB", "Subscript out of range." }, 1060 { SIGSEGV, SEGV_MAPERR, "SEGV_MAPERR", "Address not mapped to object." }, 1061 { SIGSEGV, SEGV_ACCERR, "SEGV_ACCERR", "Invalid permissions for mapped object." }, 1062 #ifdef AIX 1063 // no explanation found what keyerr would be 1064 { SIGSEGV, SEGV_KEYERR, "SEGV_KEYERR", "key error" }, 1065 #endif 1066 #if defined(IA64) && !defined(AIX) 1067 { SIGSEGV, SEGV_PSTKOVF, "SEGV_PSTKOVF", "Paragraph stack overflow" }, 1068 #endif 1069 #if defined(__sparc) && defined(SOLARIS) 1070 // define Solaris Sparc M7 ADI SEGV signals 1071 #if !defined(SEGV_ACCADI) 1072 #define SEGV_ACCADI 3 1073 #endif 1074 { SIGSEGV, SEGV_ACCADI, "SEGV_ACCADI", "ADI not enabled for mapped object." }, 1075 #if !defined(SEGV_ACCDERR) 1076 #define SEGV_ACCDERR 4 1077 #endif 1078 { SIGSEGV, SEGV_ACCDERR, "SEGV_ACCDERR", "ADI disrupting exception." }, 1079 #if !defined(SEGV_ACCPERR) 1080 #define SEGV_ACCPERR 5 1081 #endif 1082 { SIGSEGV, SEGV_ACCPERR, "SEGV_ACCPERR", "ADI precise exception." }, 1083 #endif // defined(__sparc) && defined(SOLARIS) 1084 { SIGBUS, BUS_ADRALN, "BUS_ADRALN", "Invalid address alignment." }, 1085 { SIGBUS, BUS_ADRERR, "BUS_ADRERR", "Nonexistent physical address." }, 1086 { SIGBUS, BUS_OBJERR, "BUS_OBJERR", "Object-specific hardware error." }, 1087 { SIGTRAP, TRAP_BRKPT, "TRAP_BRKPT", "Process breakpoint." }, 1088 { SIGTRAP, TRAP_TRACE, "TRAP_TRACE", "Process trace trap." }, 1089 { SIGCHLD, CLD_EXITED, "CLD_EXITED", "Child has exited." }, 1090 { SIGCHLD, CLD_KILLED, "CLD_KILLED", "Child has terminated abnormally and did not create a core file." }, 1091 { SIGCHLD, CLD_DUMPED, "CLD_DUMPED", "Child has terminated abnormally and created a core file." }, 1092 { SIGCHLD, CLD_TRAPPED, "CLD_TRAPPED", "Traced child has trapped." }, 1093 { SIGCHLD, CLD_STOPPED, "CLD_STOPPED", "Child has stopped." }, 1094 { SIGCHLD, CLD_CONTINUED,"CLD_CONTINUED","Stopped child has continued." }, 1095 #ifdef SIGPOLL 1096 { SIGPOLL, POLL_OUT, "POLL_OUT", "Output buffers available." }, 1097 { SIGPOLL, POLL_MSG, "POLL_MSG", "Input message available." }, 1098 { SIGPOLL, POLL_ERR, "POLL_ERR", "I/O error." }, 1099 { SIGPOLL, POLL_PRI, "POLL_PRI", "High priority input available." }, 1100 { SIGPOLL, POLL_HUP, "POLL_HUP", "Device disconnected. [Option End]" }, 1101 #endif 1102 { -1, -1, NULL, NULL } 1103 }; 1104 1105 // Codes valid in any signal context. 1106 const struct { 1107 int code; const char* s_code; const char* s_desc; 1108 } t2 [] = { 1109 { SI_USER, "SI_USER", "Signal sent by kill()." }, 1110 { SI_QUEUE, "SI_QUEUE", "Signal sent by the sigqueue()." }, 1111 { SI_TIMER, "SI_TIMER", "Signal generated by expiration of a timer set by timer_settime()." }, 1112 { SI_ASYNCIO, "SI_ASYNCIO", "Signal generated by completion of an asynchronous I/O request." }, 1113 { SI_MESGQ, "SI_MESGQ", "Signal generated by arrival of a message on an empty message queue." }, 1114 // Linux specific 1115 #ifdef SI_TKILL 1116 { SI_TKILL, "SI_TKILL", "Signal sent by tkill (pthread_kill)" }, 1117 #endif 1118 #ifdef SI_DETHREAD 1119 { SI_DETHREAD, "SI_DETHREAD", "Signal sent by execve() killing subsidiary threads" }, 1120 #endif 1121 #ifdef SI_KERNEL 1122 { SI_KERNEL, "SI_KERNEL", "Signal sent by kernel." }, 1123 #endif 1124 #ifdef SI_SIGIO 1125 { SI_SIGIO, "SI_SIGIO", "Signal sent by queued SIGIO" }, 1126 #endif 1127 1128 #ifdef AIX 1129 { SI_UNDEFINED, "SI_UNDEFINED","siginfo contains partial information" }, 1130 { SI_EMPTY, "SI_EMPTY", "siginfo contains no useful information" }, 1131 #endif 1132 1133 #ifdef __sun 1134 { SI_NOINFO, "SI_NOINFO", "No signal information" }, 1135 { SI_RCTL, "SI_RCTL", "kernel generated signal via rctl action" }, 1136 { SI_LWP, "SI_LWP", "Signal sent via lwp_kill" }, 1137 #endif 1138 1139 { -1, NULL, NULL } 1140 }; 1141 1142 const char* s_code = NULL; 1143 const char* s_desc = NULL; 1144 1145 for (int i = 0; t1[i].sig != -1; i ++) { 1146 if (t1[i].sig == si->si_signo && t1[i].code == si->si_code) { 1147 s_code = t1[i].s_code; 1148 s_desc = t1[i].s_desc; 1149 break; 1150 } 1151 } 1152 1153 if (s_code == NULL) { 1154 for (int i = 0; t2[i].s_code != NULL; i ++) { 1155 if (t2[i].code == si->si_code) { 1156 s_code = t2[i].s_code; 1157 s_desc = t2[i].s_desc; 1158 } 1159 } 1160 } 1161 1162 if (s_code == NULL) { 1163 out->s_name = "unknown"; 1164 out->s_desc = "unknown"; 1165 return false; 1166 } 1167 1168 out->s_name = s_code; 1169 out->s_desc = s_desc; 1170 1171 return true; 1172 } 1173 1174 void os::print_siginfo(outputStream* os, const void* si0) { 1175 1176 const siginfo_t* const si = (const siginfo_t*) si0; 1177 1178 char buf[20]; 1179 os->print("siginfo:"); 1180 1181 if (!si) { 1182 os->print(" <null>"); 1183 return; 1184 } 1185 1186 const int sig = si->si_signo; 1187 1188 os->print(" si_signo: %d (%s)", sig, os::Posix::get_signal_name(sig, buf, sizeof(buf))); 1189 1190 enum_sigcode_desc_t ed; 1191 get_signal_code_description(si, &ed); 1192 os->print(", si_code: %d (%s)", si->si_code, ed.s_name); 1193 1194 if (si->si_errno) { 1195 os->print(", si_errno: %d", si->si_errno); 1196 } 1197 1198 // Output additional information depending on the signal code. 1199 1200 // Note: Many implementations lump si_addr, si_pid, si_uid etc. together as unions, 1201 // so it depends on the context which member to use. For synchronous error signals, 1202 // we print si_addr, unless the signal was sent by another process or thread, in 1203 // which case we print out pid or tid of the sender. 1204 if (si->si_code == SI_USER || si->si_code == SI_QUEUE) { 1205 const pid_t pid = si->si_pid; 1206 os->print(", si_pid: %ld", (long) pid); 1207 if (IS_VALID_PID(pid)) { 1208 const pid_t me = getpid(); 1209 if (me == pid) { 1210 os->print(" (current process)"); 1211 } 1212 } else { 1213 os->print(" (invalid)"); 1214 } 1215 os->print(", si_uid: %ld", (long) si->si_uid); 1216 if (sig == SIGCHLD) { 1217 os->print(", si_status: %d", si->si_status); 1218 } 1219 } else if (sig == SIGSEGV || sig == SIGBUS || sig == SIGILL || 1220 sig == SIGTRAP || sig == SIGFPE) { 1221 os->print(", si_addr: " PTR_FORMAT, p2i(si->si_addr)); 1222 #ifdef SIGPOLL 1223 } else if (sig == SIGPOLL) { 1224 os->print(", si_band: %ld", si->si_band); 1225 #endif 1226 } 1227 1228 } 1229 1230 int os::Posix::unblock_thread_signal_mask(const sigset_t *set) { 1231 return pthread_sigmask(SIG_UNBLOCK, set, NULL); 1232 } 1233 1234 address os::Posix::ucontext_get_pc(const ucontext_t* ctx) { 1235 #if defined(AIX) 1236 return Aix::ucontext_get_pc(ctx); 1237 #elif defined(BSD) 1238 return Bsd::ucontext_get_pc(ctx); 1239 #elif defined(LINUX) 1240 return Linux::ucontext_get_pc(ctx); 1241 #elif defined(SOLARIS) 1242 return Solaris::ucontext_get_pc(ctx); 1243 #else 1244 VMError::report_and_die("unimplemented ucontext_get_pc"); 1245 #endif 1246 } 1247 1248 void os::Posix::ucontext_set_pc(ucontext_t* ctx, address pc) { 1249 #if defined(AIX) 1250 Aix::ucontext_set_pc(ctx, pc); 1251 #elif defined(BSD) 1252 Bsd::ucontext_set_pc(ctx, pc); 1253 #elif defined(LINUX) 1254 Linux::ucontext_set_pc(ctx, pc); 1255 #elif defined(SOLARIS) 1256 Solaris::ucontext_set_pc(ctx, pc); 1257 #else 1258 VMError::report_and_die("unimplemented ucontext_get_pc"); 1259 #endif 1260 } 1261 1262 char* os::Posix::describe_pthread_attr(char* buf, size_t buflen, const pthread_attr_t* attr) { 1263 size_t stack_size = 0; 1264 size_t guard_size = 0; 1265 int detachstate = 0; 1266 pthread_attr_getstacksize(attr, &stack_size); 1267 pthread_attr_getguardsize(attr, &guard_size); 1268 // Work around linux NPTL implementation error, see also os::create_thread() in os_linux.cpp. 1269 LINUX_ONLY(stack_size -= guard_size); 1270 pthread_attr_getdetachstate(attr, &detachstate); 1271 jio_snprintf(buf, buflen, "stacksize: " SIZE_FORMAT "k, guardsize: " SIZE_FORMAT "k, %s", 1272 stack_size / 1024, guard_size / 1024, 1273 (detachstate == PTHREAD_CREATE_DETACHED ? "detached" : "joinable")); 1274 return buf; 1275 } 1276 1277 char* os::Posix::realpath(const char* filename, char* outbuf, size_t outbuflen) { 1278 1279 if (filename == NULL || outbuf == NULL || outbuflen < 1) { 1280 assert(false, "os::Posix::realpath: invalid arguments."); 1281 errno = EINVAL; 1282 return NULL; 1283 } 1284 1285 char* result = NULL; 1286 1287 // This assumes platform realpath() is implemented according to POSIX.1-2008. 1288 // POSIX.1-2008 allows to specify NULL for the output buffer, in which case 1289 // output buffer is dynamically allocated and must be ::free()'d by the caller. 1290 char* p = ::realpath(filename, NULL); 1291 if (p != NULL) { 1292 if (strlen(p) < outbuflen) { 1293 strcpy(outbuf, p); 1294 result = outbuf; 1295 } else { 1296 errno = ENAMETOOLONG; 1297 } 1298 ::free(p); // *not* os::free 1299 } else { 1300 // Fallback for platforms struggling with modern Posix standards (AIX 5.3, 6.1). If realpath 1301 // returns EINVAL, this may indicate that realpath is not POSIX.1-2008 compatible and 1302 // that it complains about the NULL we handed down as user buffer. 1303 // In this case, use the user provided buffer but at least check whether realpath caused 1304 // a memory overwrite. 1305 if (errno == EINVAL) { 1306 outbuf[outbuflen - 1] = '\0'; 1307 p = ::realpath(filename, outbuf); 1308 if (p != NULL) { 1309 guarantee(outbuf[outbuflen - 1] == '\0', "realpath buffer overwrite detected."); 1310 result = p; 1311 } 1312 } 1313 } 1314 return result; 1315 1316 } 1317 1318 1319 // Check minimum allowable stack sizes for thread creation and to initialize 1320 // the java system classes, including StackOverflowError - depends on page 1321 // size. 1322 // The space needed for frames during startup is platform dependent. It 1323 // depends on word size, platform calling conventions, C frame layout and 1324 // interpreter/C1/C2 design decisions. Therefore this is given in a 1325 // platform (os/cpu) dependent constant. 1326 // To this, space for guard mechanisms is added, which depends on the 1327 // page size which again depends on the concrete system the VM is running 1328 // on. Space for libc guard pages is not included in this size. 1329 jint os::Posix::set_minimum_stack_sizes() { 1330 size_t os_min_stack_allowed = SOLARIS_ONLY(thr_min_stack()) NOT_SOLARIS(PTHREAD_STACK_MIN); 1331 1332 _java_thread_min_stack_allowed = _java_thread_min_stack_allowed + 1333 JavaThread::stack_guard_zone_size() + 1334 JavaThread::stack_shadow_zone_size(); 1335 1336 _java_thread_min_stack_allowed = align_up(_java_thread_min_stack_allowed, vm_page_size()); 1337 _java_thread_min_stack_allowed = MAX2(_java_thread_min_stack_allowed, os_min_stack_allowed); 1338 1339 size_t stack_size_in_bytes = ThreadStackSize * K; 1340 if (stack_size_in_bytes != 0 && 1341 stack_size_in_bytes < _java_thread_min_stack_allowed) { 1342 // The '-Xss' and '-XX:ThreadStackSize=N' options both set 1343 // ThreadStackSize so we go with "Java thread stack size" instead 1344 // of "ThreadStackSize" to be more friendly. 1345 tty->print_cr("\nThe Java thread stack size specified is too small. " 1346 "Specify at least " SIZE_FORMAT "k", 1347 _java_thread_min_stack_allowed / K); 1348 return JNI_ERR; 1349 } 1350 1351 // Make the stack size a multiple of the page size so that 1352 // the yellow/red zones can be guarded. 1353 JavaThread::set_stack_size_at_create(align_up(stack_size_in_bytes, vm_page_size())); 1354 1355 // Reminder: a compiler thread is a Java thread. 1356 _compiler_thread_min_stack_allowed = _compiler_thread_min_stack_allowed + 1357 JavaThread::stack_guard_zone_size() + 1358 JavaThread::stack_shadow_zone_size(); 1359 1360 _compiler_thread_min_stack_allowed = align_up(_compiler_thread_min_stack_allowed, vm_page_size()); 1361 _compiler_thread_min_stack_allowed = MAX2(_compiler_thread_min_stack_allowed, os_min_stack_allowed); 1362 1363 stack_size_in_bytes = CompilerThreadStackSize * K; 1364 if (stack_size_in_bytes != 0 && 1365 stack_size_in_bytes < _compiler_thread_min_stack_allowed) { 1366 tty->print_cr("\nThe CompilerThreadStackSize specified is too small. " 1367 "Specify at least " SIZE_FORMAT "k", 1368 _compiler_thread_min_stack_allowed / K); 1369 return JNI_ERR; 1370 } 1371 1372 _vm_internal_thread_min_stack_allowed = align_up(_vm_internal_thread_min_stack_allowed, vm_page_size()); 1373 _vm_internal_thread_min_stack_allowed = MAX2(_vm_internal_thread_min_stack_allowed, os_min_stack_allowed); 1374 1375 stack_size_in_bytes = VMThreadStackSize * K; 1376 if (stack_size_in_bytes != 0 && 1377 stack_size_in_bytes < _vm_internal_thread_min_stack_allowed) { 1378 tty->print_cr("\nThe VMThreadStackSize specified is too small. " 1379 "Specify at least " SIZE_FORMAT "k", 1380 _vm_internal_thread_min_stack_allowed / K); 1381 return JNI_ERR; 1382 } 1383 return JNI_OK; 1384 } 1385 1386 // Called when creating the thread. The minimum stack sizes have already been calculated 1387 size_t os::Posix::get_initial_stack_size(ThreadType thr_type, size_t req_stack_size) { 1388 size_t stack_size; 1389 if (req_stack_size == 0) { 1390 stack_size = default_stack_size(thr_type); 1391 } else { 1392 stack_size = req_stack_size; 1393 } 1394 1395 switch (thr_type) { 1396 case os::java_thread: 1397 // Java threads use ThreadStackSize which default value can be 1398 // changed with the flag -Xss 1399 if (req_stack_size == 0 && JavaThread::stack_size_at_create() > 0) { 1400 // no requested size and we have a more specific default value 1401 stack_size = JavaThread::stack_size_at_create(); 1402 } 1403 stack_size = MAX2(stack_size, 1404 _java_thread_min_stack_allowed); 1405 break; 1406 case os::compiler_thread: 1407 if (req_stack_size == 0 && CompilerThreadStackSize > 0) { 1408 // no requested size and we have a more specific default value 1409 stack_size = (size_t)(CompilerThreadStackSize * K); 1410 } 1411 stack_size = MAX2(stack_size, 1412 _compiler_thread_min_stack_allowed); 1413 break; 1414 case os::vm_thread: 1415 case os::pgc_thread: 1416 case os::cgc_thread: 1417 case os::watcher_thread: 1418 default: // presume the unknown thr_type is a VM internal 1419 if (req_stack_size == 0 && VMThreadStackSize > 0) { 1420 // no requested size and we have a more specific default value 1421 stack_size = (size_t)(VMThreadStackSize * K); 1422 } 1423 1424 stack_size = MAX2(stack_size, 1425 _vm_internal_thread_min_stack_allowed); 1426 break; 1427 } 1428 1429 // pthread_attr_setstacksize() may require that the size be rounded up to the OS page size. 1430 // Be careful not to round up to 0. Align down in that case. 1431 if (stack_size <= SIZE_MAX - vm_page_size()) { 1432 stack_size = align_up(stack_size, vm_page_size()); 1433 } else { 1434 stack_size = align_down(stack_size, vm_page_size()); 1435 } 1436 1437 return stack_size; 1438 } 1439 1440 Thread* os::ThreadCrashProtection::_protected_thread = NULL; 1441 os::ThreadCrashProtection* os::ThreadCrashProtection::_crash_protection = NULL; 1442 volatile intptr_t os::ThreadCrashProtection::_crash_mux = 0; 1443 1444 os::ThreadCrashProtection::ThreadCrashProtection() { 1445 } 1446 1447 /* 1448 * See the caveats for this class in os_posix.hpp 1449 * Protects the callback call so that SIGSEGV / SIGBUS jumps back into this 1450 * method and returns false. If none of the signals are raised, returns true. 1451 * The callback is supposed to provide the method that should be protected. 1452 */ 1453 bool os::ThreadCrashProtection::call(os::CrashProtectionCallback& cb) { 1454 sigset_t saved_sig_mask; 1455 1456 Thread::muxAcquire(&_crash_mux, "CrashProtection"); 1457 1458 _protected_thread = Thread::current_or_null(); 1459 assert(_protected_thread != NULL, "Cannot crash protect a NULL thread"); 1460 1461 // we cannot rely on sigsetjmp/siglongjmp to save/restore the signal mask 1462 // since on at least some systems (OS X) siglongjmp will restore the mask 1463 // for the process, not the thread 1464 pthread_sigmask(0, NULL, &saved_sig_mask); 1465 if (sigsetjmp(_jmpbuf, 0) == 0) { 1466 // make sure we can see in the signal handler that we have crash protection 1467 // installed 1468 _crash_protection = this; 1469 cb.call(); 1470 // and clear the crash protection 1471 _crash_protection = NULL; 1472 _protected_thread = NULL; 1473 Thread::muxRelease(&_crash_mux); 1474 return true; 1475 } 1476 // this happens when we siglongjmp() back 1477 pthread_sigmask(SIG_SETMASK, &saved_sig_mask, NULL); 1478 _crash_protection = NULL; 1479 _protected_thread = NULL; 1480 Thread::muxRelease(&_crash_mux); 1481 return false; 1482 } 1483 1484 void os::ThreadCrashProtection::restore() { 1485 assert(_crash_protection != NULL, "must have crash protection"); 1486 siglongjmp(_jmpbuf, 1); 1487 } 1488 1489 void os::ThreadCrashProtection::check_crash_protection(int sig, 1490 Thread* thread) { 1491 1492 if (thread != NULL && 1493 thread == _protected_thread && 1494 _crash_protection != NULL) { 1495 1496 if (sig == SIGSEGV || sig == SIGBUS) { 1497 _crash_protection->restore(); 1498 } 1499 } 1500 } 1501 1502 // POSIX unamed semaphores are not supported on OS X. 1503 #ifndef __APPLE__ 1504 1505 PosixSemaphore::PosixSemaphore(uint value) { 1506 int ret = sem_init(&_semaphore, 0, value); 1507 1508 guarantee_with_errno(ret == 0, "Failed to initialize semaphore"); 1509 } 1510 1511 PosixSemaphore::~PosixSemaphore() { 1512 sem_destroy(&_semaphore); 1513 } 1514 1515 void PosixSemaphore::signal(uint count) { 1516 for (uint i = 0; i < count; i++) { 1517 int ret = sem_post(&_semaphore); 1518 1519 assert_with_errno(ret == 0, "sem_post failed"); 1520 } 1521 } 1522 1523 void PosixSemaphore::wait() { 1524 int ret; 1525 1526 do { 1527 ret = sem_wait(&_semaphore); 1528 } while (ret != 0 && errno == EINTR); 1529 1530 assert_with_errno(ret == 0, "sem_wait failed"); 1531 } 1532 1533 bool PosixSemaphore::trywait() { 1534 int ret; 1535 1536 do { 1537 ret = sem_trywait(&_semaphore); 1538 } while (ret != 0 && errno == EINTR); 1539 1540 assert_with_errno(ret == 0 || errno == EAGAIN, "trywait failed"); 1541 1542 return ret == 0; 1543 } 1544 1545 bool PosixSemaphore::timedwait(struct timespec ts) { 1546 while (true) { 1547 int result = sem_timedwait(&_semaphore, &ts); 1548 if (result == 0) { 1549 return true; 1550 } else if (errno == EINTR) { 1551 continue; 1552 } else if (errno == ETIMEDOUT) { 1553 return false; 1554 } else { 1555 assert_with_errno(false, "timedwait failed"); 1556 return false; 1557 } 1558 } 1559 } 1560 1561 #endif // __APPLE__ 1562 1563 1564 // Shared pthread_mutex/cond based PlatformEvent implementation. 1565 // Not currently usable by Solaris. 1566 1567 #ifndef SOLARIS 1568 1569 // Shared condattr object for use with relative timed-waits. Will be associated 1570 // with CLOCK_MONOTONIC if available to avoid issues with time-of-day changes, 1571 // but otherwise whatever default is used by the platform - generally the 1572 // time-of-day clock. 1573 static pthread_condattr_t _condAttr[1]; 1574 1575 // Shared mutexattr to explicitly set the type to PTHREAD_MUTEX_NORMAL as not 1576 // all systems (e.g. FreeBSD) map the default to "normal". 1577 static pthread_mutexattr_t _mutexAttr[1]; 1578 1579 // common basic initialization that is always supported 1580 static void pthread_init_common(void) { 1581 int status; 1582 if ((status = pthread_condattr_init(_condAttr)) != 0) { 1583 fatal("pthread_condattr_init: %s", os::strerror(status)); 1584 } 1585 if ((status = pthread_mutexattr_init(_mutexAttr)) != 0) { 1586 fatal("pthread_mutexattr_init: %s", os::strerror(status)); 1587 } 1588 if ((status = pthread_mutexattr_settype(_mutexAttr, PTHREAD_MUTEX_NORMAL)) != 0) { 1589 fatal("pthread_mutexattr_settype: %s", os::strerror(status)); 1590 } 1591 } 1592 1593 // Not all POSIX types and API's are available on all notionally "posix" 1594 // platforms. If we have build-time support then we will check for actual 1595 // runtime support via dlopen/dlsym lookup. This allows for running on an 1596 // older OS version compared to the build platform. But if there is no 1597 // build time support then there cannot be any runtime support as we do not 1598 // know what the runtime types would be (for example clockid_t might be an 1599 // int or int64_t). 1600 // 1601 #ifdef SUPPORTS_CLOCK_MONOTONIC 1602 1603 // This means we have clockid_t, clock_gettime et al and CLOCK_MONOTONIC 1604 1605 static int (*_clock_gettime)(clockid_t, struct timespec *); 1606 static int (*_pthread_condattr_setclock)(pthread_condattr_t *, clockid_t); 1607 1608 static bool _use_clock_monotonic_condattr; 1609 1610 // Determine what POSIX API's are present and do appropriate 1611 // configuration. 1612 void os::Posix::init(void) { 1613 1614 // NOTE: no logging available when this is called. Put logging 1615 // statements in init_2(). 1616 1617 // Copied from os::Linux::clock_init(). The duplication is temporary. 1618 1619 // 1. Check for CLOCK_MONOTONIC support. 1620 1621 void* handle = NULL; 1622 1623 // For linux we need librt, for other OS we can find 1624 // this function in regular libc. 1625 #ifdef NEEDS_LIBRT 1626 // We do dlopen's in this particular order due to bug in linux 1627 // dynamic loader (see 6348968) leading to crash on exit. 1628 handle = dlopen("librt.so.1", RTLD_LAZY); 1629 if (handle == NULL) { 1630 handle = dlopen("librt.so", RTLD_LAZY); 1631 } 1632 #endif 1633 1634 if (handle == NULL) { 1635 handle = RTLD_DEFAULT; 1636 } 1637 1638 _clock_gettime = NULL; 1639 1640 int (*clock_getres_func)(clockid_t, struct timespec*) = 1641 (int(*)(clockid_t, struct timespec*))dlsym(handle, "clock_getres"); 1642 int (*clock_gettime_func)(clockid_t, struct timespec*) = 1643 (int(*)(clockid_t, struct timespec*))dlsym(handle, "clock_gettime"); 1644 if (clock_getres_func != NULL && clock_gettime_func != NULL) { 1645 // We assume that if both clock_gettime and clock_getres support 1646 // CLOCK_MONOTONIC then the OS provides true high-res monotonic clock. 1647 struct timespec res; 1648 struct timespec tp; 1649 if (clock_getres_func(CLOCK_MONOTONIC, &res) == 0 && 1650 clock_gettime_func(CLOCK_MONOTONIC, &tp) == 0) { 1651 // Yes, monotonic clock is supported. 1652 _clock_gettime = clock_gettime_func; 1653 } else { 1654 #ifdef NEEDS_LIBRT 1655 // Close librt if there is no monotonic clock. 1656 if (handle != RTLD_DEFAULT) { 1657 dlclose(handle); 1658 } 1659 #endif 1660 } 1661 } 1662 1663 // 2. Check for pthread_condattr_setclock support. 1664 1665 _pthread_condattr_setclock = NULL; 1666 1667 // libpthread is already loaded. 1668 int (*condattr_setclock_func)(pthread_condattr_t*, clockid_t) = 1669 (int (*)(pthread_condattr_t*, clockid_t))dlsym(RTLD_DEFAULT, 1670 "pthread_condattr_setclock"); 1671 if (condattr_setclock_func != NULL) { 1672 _pthread_condattr_setclock = condattr_setclock_func; 1673 } 1674 1675 // Now do general initialization. 1676 1677 pthread_init_common(); 1678 1679 int status; 1680 if (_pthread_condattr_setclock != NULL && _clock_gettime != NULL) { 1681 if ((status = _pthread_condattr_setclock(_condAttr, CLOCK_MONOTONIC)) != 0) { 1682 if (status == EINVAL) { 1683 _use_clock_monotonic_condattr = false; 1684 warning("Unable to use monotonic clock with relative timed-waits" \ 1685 " - changes to the time-of-day clock may have adverse affects"); 1686 } else { 1687 fatal("pthread_condattr_setclock: %s", os::strerror(status)); 1688 } 1689 } else { 1690 _use_clock_monotonic_condattr = true; 1691 } 1692 } else { 1693 _use_clock_monotonic_condattr = false; 1694 } 1695 } 1696 1697 void os::Posix::init_2(void) { 1698 log_info(os)("Use of CLOCK_MONOTONIC is%s supported", 1699 (_clock_gettime != NULL ? "" : " not")); 1700 log_info(os)("Use of pthread_condattr_setclock is%s supported", 1701 (_pthread_condattr_setclock != NULL ? "" : " not")); 1702 log_info(os)("Relative timed-wait using pthread_cond_timedwait is associated with %s", 1703 _use_clock_monotonic_condattr ? "CLOCK_MONOTONIC" : "the default clock"); 1704 } 1705 1706 #else // !SUPPORTS_CLOCK_MONOTONIC 1707 1708 void os::Posix::init(void) { 1709 pthread_init_common(); 1710 } 1711 1712 void os::Posix::init_2(void) { 1713 log_info(os)("Use of CLOCK_MONOTONIC is not supported"); 1714 log_info(os)("Use of pthread_condattr_setclock is not supported"); 1715 log_info(os)("Relative timed-wait using pthread_cond_timedwait is associated with the default clock"); 1716 } 1717 1718 #endif // SUPPORTS_CLOCK_MONOTONIC 1719 1720 os::PlatformEvent::PlatformEvent() { 1721 int status = pthread_cond_init(_cond, _condAttr); 1722 assert_status(status == 0, status, "cond_init"); 1723 status = pthread_mutex_init(_mutex, _mutexAttr); 1724 assert_status(status == 0, status, "mutex_init"); 1725 _event = 0; 1726 _nParked = 0; 1727 } 1728 1729 // Utility to convert the given timeout to an absolute timespec 1730 // (based on the appropriate clock) to use with pthread_cond_timewait. 1731 // The clock queried here must be the clock used to manage the 1732 // timeout of the condition variable. 1733 // 1734 // The passed in timeout value is either a relative time in nanoseconds 1735 // or an absolute time in milliseconds. A relative timeout will be 1736 // associated with CLOCK_MONOTONIC if available; otherwise, or if absolute, 1737 // the default time-of-day clock will be used. 1738 1739 // Given time is a 64-bit value and the time_t used in the timespec is 1740 // sometimes a signed-32-bit value we have to watch for overflow if times 1741 // way in the future are given. Further on Solaris versions 1742 // prior to 10 there is a restriction (see cond_timedwait) that the specified 1743 // number of seconds, in abstime, is less than current_time + 100000000. 1744 // As it will be over 20 years before "now + 100000000" will overflow we can 1745 // ignore overflow and just impose a hard-limit on seconds using the value 1746 // of "now + 100000000". This places a limit on the timeout of about 3.17 1747 // years from "now". 1748 // 1749 #define MAX_SECS 100000000 1750 1751 // Calculate a new absolute time that is "timeout" nanoseconds from "now". 1752 // "unit" indicates the unit of "now_part_sec" (may be nanos or micros depending 1753 // on which clock is being used). 1754 static void calc_rel_time(timespec* abstime, jlong timeout, jlong now_sec, 1755 jlong now_part_sec, jlong unit) { 1756 time_t max_secs = now_sec + MAX_SECS; 1757 1758 jlong seconds = timeout / NANOUNITS; 1759 timeout %= NANOUNITS; // remaining nanos 1760 1761 if (seconds >= MAX_SECS) { 1762 // More seconds than we can add, so pin to max_secs. 1763 abstime->tv_sec = max_secs; 1764 abstime->tv_nsec = 0; 1765 } else { 1766 abstime->tv_sec = now_sec + seconds; 1767 long nanos = (now_part_sec * (NANOUNITS / unit)) + timeout; 1768 if (nanos >= NANOUNITS) { // overflow 1769 abstime->tv_sec += 1; 1770 nanos -= NANOUNITS; 1771 } 1772 abstime->tv_nsec = nanos; 1773 } 1774 } 1775 1776 // Unpack the given deadline in milliseconds since the epoch, into the given timespec. 1777 // The current time in seconds is also passed in to enforce an upper bound as discussed above. 1778 static void unpack_abs_time(timespec* abstime, jlong deadline, jlong now_sec) { 1779 time_t max_secs = now_sec + MAX_SECS; 1780 1781 jlong seconds = deadline / MILLIUNITS; 1782 jlong millis = deadline % MILLIUNITS; 1783 1784 if (seconds >= max_secs) { 1785 // Absolute seconds exceeds allowed max, so pin to max_secs. 1786 abstime->tv_sec = max_secs; 1787 abstime->tv_nsec = 0; 1788 } else { 1789 abstime->tv_sec = seconds; 1790 abstime->tv_nsec = millis * (NANOUNITS / MILLIUNITS); 1791 } 1792 } 1793 1794 static void to_abstime(timespec* abstime, jlong timeout, bool isAbsolute) { 1795 DEBUG_ONLY(int max_secs = MAX_SECS;) 1796 1797 if (timeout < 0) { 1798 timeout = 0; 1799 } 1800 1801 #ifdef SUPPORTS_CLOCK_MONOTONIC 1802 1803 if (_use_clock_monotonic_condattr && !isAbsolute) { 1804 struct timespec now; 1805 int status = _clock_gettime(CLOCK_MONOTONIC, &now); 1806 assert_status(status == 0, status, "clock_gettime"); 1807 calc_rel_time(abstime, timeout, now.tv_sec, now.tv_nsec, NANOUNITS); 1808 DEBUG_ONLY(max_secs += now.tv_sec;) 1809 } else { 1810 1811 #else 1812 1813 { // Match the block scope. 1814 1815 #endif // SUPPORTS_CLOCK_MONOTONIC 1816 1817 // Time-of-day clock is all we can reliably use. 1818 struct timeval now; 1819 int status = gettimeofday(&now, NULL); 1820 assert_status(status == 0, errno, "gettimeofday"); 1821 if (isAbsolute) { 1822 unpack_abs_time(abstime, timeout, now.tv_sec); 1823 } else { 1824 calc_rel_time(abstime, timeout, now.tv_sec, now.tv_usec, MICROUNITS); 1825 } 1826 DEBUG_ONLY(max_secs += now.tv_sec;) 1827 } 1828 1829 assert(abstime->tv_sec >= 0, "tv_sec < 0"); 1830 assert(abstime->tv_sec <= max_secs, "tv_sec > max_secs"); 1831 assert(abstime->tv_nsec >= 0, "tv_nsec < 0"); 1832 assert(abstime->tv_nsec < NANOUNITS, "tv_nsec >= NANOUNITS"); 1833 } 1834 1835 // PlatformEvent 1836 // 1837 // Assumption: 1838 // Only one parker can exist on an event, which is why we allocate 1839 // them per-thread. Multiple unparkers can coexist. 1840 // 1841 // _event serves as a restricted-range semaphore. 1842 // -1 : thread is blocked, i.e. there is a waiter 1843 // 0 : neutral: thread is running or ready, 1844 // could have been signaled after a wait started 1845 // 1 : signaled - thread is running or ready 1846 // 1847 // Having three states allows for some detection of bad usage - see 1848 // comments on unpark(). 1849 1850 void os::PlatformEvent::park() { // AKA "down()" 1851 // Transitions for _event: 1852 // -1 => -1 : illegal 1853 // 1 => 0 : pass - return immediately 1854 // 0 => -1 : block; then set _event to 0 before returning 1855 1856 // Invariant: Only the thread associated with the PlatformEvent 1857 // may call park(). 1858 assert(_nParked == 0, "invariant"); 1859 1860 int v; 1861 1862 // atomically decrement _event 1863 for (;;) { 1864 v = _event; 1865 if (Atomic::cmpxchg(v - 1, &_event, v) == v) break; 1866 } 1867 guarantee(v >= 0, "invariant"); 1868 1869 if (v == 0) { // Do this the hard way by blocking ... 1870 int status = pthread_mutex_lock(_mutex); 1871 assert_status(status == 0, status, "mutex_lock"); 1872 guarantee(_nParked == 0, "invariant"); 1873 ++_nParked; 1874 while (_event < 0) { 1875 // OS-level "spurious wakeups" are ignored 1876 status = pthread_cond_wait(_cond, _mutex); 1877 assert_status(status == 0, status, "cond_wait"); 1878 } 1879 --_nParked; 1880 1881 _event = 0; 1882 status = pthread_mutex_unlock(_mutex); 1883 assert_status(status == 0, status, "mutex_unlock"); 1884 // Paranoia to ensure our locked and lock-free paths interact 1885 // correctly with each other. 1886 OrderAccess::fence(); 1887 } 1888 guarantee(_event >= 0, "invariant"); 1889 } 1890 1891 int os::PlatformEvent::park(jlong millis) { 1892 // Transitions for _event: 1893 // -1 => -1 : illegal 1894 // 1 => 0 : pass - return immediately 1895 // 0 => -1 : block; then set _event to 0 before returning 1896 1897 // Invariant: Only the thread associated with the Event/PlatformEvent 1898 // may call park(). 1899 assert(_nParked == 0, "invariant"); 1900 1901 int v; 1902 // atomically decrement _event 1903 for (;;) { 1904 v = _event; 1905 if (Atomic::cmpxchg(v - 1, &_event, v) == v) break; 1906 } 1907 guarantee(v >= 0, "invariant"); 1908 1909 if (v == 0) { // Do this the hard way by blocking ... 1910 struct timespec abst; 1911 // We have to watch for overflow when converting millis to nanos, 1912 // but if millis is that large then we will end up limiting to 1913 // MAX_SECS anyway, so just do that here. 1914 if (millis / MILLIUNITS > MAX_SECS) { 1915 millis = jlong(MAX_SECS) * MILLIUNITS; 1916 } 1917 to_abstime(&abst, millis * (NANOUNITS / MILLIUNITS), false); 1918 1919 int ret = OS_TIMEOUT; 1920 int status = pthread_mutex_lock(_mutex); 1921 assert_status(status == 0, status, "mutex_lock"); 1922 guarantee(_nParked == 0, "invariant"); 1923 ++_nParked; 1924 1925 while (_event < 0) { 1926 status = pthread_cond_timedwait(_cond, _mutex, &abst); 1927 assert_status(status == 0 || status == ETIMEDOUT, 1928 status, "cond_timedwait"); 1929 // OS-level "spurious wakeups" are ignored unless the archaic 1930 // FilterSpuriousWakeups is set false. That flag should be obsoleted. 1931 if (!FilterSpuriousWakeups) break; 1932 if (status == ETIMEDOUT) break; 1933 } 1934 --_nParked; 1935 1936 if (_event >= 0) { 1937 ret = OS_OK; 1938 } 1939 1940 _event = 0; 1941 status = pthread_mutex_unlock(_mutex); 1942 assert_status(status == 0, status, "mutex_unlock"); 1943 // Paranoia to ensure our locked and lock-free paths interact 1944 // correctly with each other. 1945 OrderAccess::fence(); 1946 return ret; 1947 } 1948 return OS_OK; 1949 } 1950 1951 void os::PlatformEvent::unpark() { 1952 // Transitions for _event: 1953 // 0 => 1 : just return 1954 // 1 => 1 : just return 1955 // -1 => either 0 or 1; must signal target thread 1956 // That is, we can safely transition _event from -1 to either 1957 // 0 or 1. 1958 // See also: "Semaphores in Plan 9" by Mullender & Cox 1959 // 1960 // Note: Forcing a transition from "-1" to "1" on an unpark() means 1961 // that it will take two back-to-back park() calls for the owning 1962 // thread to block. This has the benefit of forcing a spurious return 1963 // from the first park() call after an unpark() call which will help 1964 // shake out uses of park() and unpark() without checking state conditions 1965 // properly. This spurious return doesn't manifest itself in any user code 1966 // but only in the correctly written condition checking loops of ObjectMonitor, 1967 // Mutex/Monitor, Thread::muxAcquire and os::sleep 1968 1969 if (Atomic::xchg(1, &_event) >= 0) return; 1970 1971 int status = pthread_mutex_lock(_mutex); 1972 assert_status(status == 0, status, "mutex_lock"); 1973 int anyWaiters = _nParked; 1974 assert(anyWaiters == 0 || anyWaiters == 1, "invariant"); 1975 status = pthread_mutex_unlock(_mutex); 1976 assert_status(status == 0, status, "mutex_unlock"); 1977 1978 // Note that we signal() *after* dropping the lock for "immortal" Events. 1979 // This is safe and avoids a common class of futile wakeups. In rare 1980 // circumstances this can cause a thread to return prematurely from 1981 // cond_{timed}wait() but the spurious wakeup is benign and the victim 1982 // will simply re-test the condition and re-park itself. 1983 // This provides particular benefit if the underlying platform does not 1984 // provide wait morphing. 1985 1986 if (anyWaiters != 0) { 1987 status = pthread_cond_signal(_cond); 1988 assert_status(status == 0, status, "cond_signal"); 1989 } 1990 } 1991 1992 // JSR166 support 1993 1994 os::PlatformParker::PlatformParker() { 1995 int status; 1996 status = pthread_cond_init(&_cond[REL_INDEX], _condAttr); 1997 assert_status(status == 0, status, "cond_init rel"); 1998 status = pthread_cond_init(&_cond[ABS_INDEX], NULL); 1999 assert_status(status == 0, status, "cond_init abs"); 2000 status = pthread_mutex_init(_mutex, _mutexAttr); 2001 assert_status(status == 0, status, "mutex_init"); 2002 _cur_index = -1; // mark as unused 2003 } 2004 2005 // Parker::park decrements count if > 0, else does a condvar wait. Unpark 2006 // sets count to 1 and signals condvar. Only one thread ever waits 2007 // on the condvar. Contention seen when trying to park implies that someone 2008 // is unparking you, so don't wait. And spurious returns are fine, so there 2009 // is no need to track notifications. 2010 2011 void Parker::park(bool isAbsolute, jlong time) { 2012 2013 // Optional fast-path check: 2014 // Return immediately if a permit is available. 2015 // We depend on Atomic::xchg() having full barrier semantics 2016 // since we are doing a lock-free update to _counter. 2017 if (Atomic::xchg(0, &_counter) > 0) return; 2018 2019 Thread* thread = Thread::current(); 2020 assert(thread->is_Java_thread(), "Must be JavaThread"); 2021 JavaThread *jt = (JavaThread *)thread; 2022 2023 // Optional optimization -- avoid state transitions if there's 2024 // an interrupt pending. 2025 if (Thread::is_interrupted(thread, false)) { 2026 return; 2027 } 2028 2029 // Next, demultiplex/decode time arguments 2030 struct timespec absTime; 2031 if (time < 0 || (isAbsolute && time == 0)) { // don't wait at all 2032 return; 2033 } 2034 if (time > 0) { 2035 to_abstime(&absTime, time, isAbsolute); 2036 } 2037 2038 // Enter safepoint region 2039 // Beware of deadlocks such as 6317397. 2040 // The per-thread Parker:: mutex is a classic leaf-lock. 2041 // In particular a thread must never block on the Threads_lock while 2042 // holding the Parker:: mutex. If safepoints are pending both the 2043 // the ThreadBlockInVM() CTOR and DTOR may grab Threads_lock. 2044 ThreadBlockInVM tbivm(jt); 2045 2046 // Don't wait if cannot get lock since interference arises from 2047 // unparking. Also re-check interrupt before trying wait. 2048 if (Thread::is_interrupted(thread, false) || 2049 pthread_mutex_trylock(_mutex) != 0) { 2050 return; 2051 } 2052 2053 int status; 2054 if (_counter > 0) { // no wait needed 2055 _counter = 0; 2056 status = pthread_mutex_unlock(_mutex); 2057 assert_status(status == 0, status, "invariant"); 2058 // Paranoia to ensure our locked and lock-free paths interact 2059 // correctly with each other and Java-level accesses. 2060 OrderAccess::fence(); 2061 return; 2062 } 2063 2064 OSThreadWaitState osts(thread->osthread(), false /* not Object.wait() */); 2065 jt->set_suspend_equivalent(); 2066 // cleared by handle_special_suspend_equivalent_condition() or java_suspend_self() 2067 2068 assert(_cur_index == -1, "invariant"); 2069 if (time == 0) { 2070 _cur_index = REL_INDEX; // arbitrary choice when not timed 2071 status = pthread_cond_wait(&_cond[_cur_index], _mutex); 2072 assert_status(status == 0, status, "cond_timedwait"); 2073 } 2074 else { 2075 _cur_index = isAbsolute ? ABS_INDEX : REL_INDEX; 2076 status = pthread_cond_timedwait(&_cond[_cur_index], _mutex, &absTime); 2077 assert_status(status == 0 || status == ETIMEDOUT, 2078 status, "cond_timedwait"); 2079 } 2080 _cur_index = -1; 2081 2082 _counter = 0; 2083 status = pthread_mutex_unlock(_mutex); 2084 assert_status(status == 0, status, "invariant"); 2085 // Paranoia to ensure our locked and lock-free paths interact 2086 // correctly with each other and Java-level accesses. 2087 OrderAccess::fence(); 2088 2089 // If externally suspended while waiting, re-suspend 2090 if (jt->handle_special_suspend_equivalent_condition()) { 2091 jt->java_suspend_self(); 2092 } 2093 } 2094 2095 void Parker::unpark() { 2096 int status = pthread_mutex_lock(_mutex); 2097 assert_status(status == 0, status, "invariant"); 2098 const int s = _counter; 2099 _counter = 1; 2100 // must capture correct index before unlocking 2101 int index = _cur_index; 2102 status = pthread_mutex_unlock(_mutex); 2103 assert_status(status == 0, status, "invariant"); 2104 2105 // Note that we signal() *after* dropping the lock for "immortal" Events. 2106 // This is safe and avoids a common class of futile wakeups. In rare 2107 // circumstances this can cause a thread to return prematurely from 2108 // cond_{timed}wait() but the spurious wakeup is benign and the victim 2109 // will simply re-test the condition and re-park itself. 2110 // This provides particular benefit if the underlying platform does not 2111 // provide wait morphing. 2112 2113 if (s < 1 && index != -1) { 2114 // thread is definitely parked 2115 status = pthread_cond_signal(&_cond[index]); 2116 assert_status(status == 0, status, "invariant"); 2117 } 2118 } 2119 2120 2121 #endif // !SOLARIS