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