Path: blob/master/src/hotspot/os/posix/os_posix.cpp
64440 views
/*1* Copyright (c) 1999, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/232425#include "jvm.h"26#ifdef LINUX27#include "classfile/classLoader.hpp"28#endif29#include "jvmtifiles/jvmti.h"30#include "logging/log.hpp"31#include "memory/allocation.inline.hpp"32#include "os_posix.inline.hpp"33#include "runtime/globals_extension.hpp"34#include "runtime/osThread.hpp"35#include "utilities/globalDefinitions.hpp"36#include "runtime/frame.inline.hpp"37#include "runtime/interfaceSupport.inline.hpp"38#include "runtime/sharedRuntime.hpp"39#include "services/attachListener.hpp"40#include "services/memTracker.hpp"41#include "runtime/arguments.hpp"42#include "runtime/atomic.hpp"43#include "runtime/java.hpp"44#include "runtime/orderAccess.hpp"45#include "runtime/perfMemory.hpp"46#include "utilities/align.hpp"47#include "utilities/events.hpp"48#include "utilities/formatBuffer.hpp"49#include "utilities/macros.hpp"50#include "utilities/vmError.hpp"5152#include <dirent.h>53#include <dlfcn.h>54#include <grp.h>55#include <netdb.h>56#include <pwd.h>57#include <pthread.h>58#include <signal.h>59#include <sys/mman.h>60#include <sys/resource.h>61#include <sys/socket.h>62#include <sys/types.h>63#include <sys/utsname.h>64#include <sys/wait.h>65#include <time.h>66#include <unistd.h>67#include <utmpx.h>6869#ifdef __APPLE__70#include <crt_externs.h>71#endif7273#define ROOT_UID 07475#ifndef MAP_ANONYMOUS76#define MAP_ANONYMOUS MAP_ANON77#endif7879#define check_with_errno(check_type, cond, msg) \80do { \81int err = errno; \82check_type(cond, "%s; error='%s' (errno=%s)", msg, os::strerror(err), \83os::errno_name(err)); \84} while (false)8586#define assert_with_errno(cond, msg) check_with_errno(assert, cond, msg)87#define guarantee_with_errno(cond, msg) check_with_errno(guarantee, cond, msg)8889// Check core dump limit and report possible place where core can be found90void os::check_dump_limit(char* buffer, size_t bufferSize) {91if (!FLAG_IS_DEFAULT(CreateCoredumpOnCrash) && !CreateCoredumpOnCrash) {92jio_snprintf(buffer, bufferSize, "CreateCoredumpOnCrash is disabled from command line");93VMError::record_coredump_status(buffer, false);94return;95}9697int n;98struct rlimit rlim;99bool success;100101char core_path[PATH_MAX];102n = get_core_path(core_path, PATH_MAX);103104if (n <= 0) {105jio_snprintf(buffer, bufferSize, "core.%d (may not exist)", current_process_id());106success = true;107#ifdef LINUX108} else if (core_path[0] == '"') { // redirect to user process109jio_snprintf(buffer, bufferSize, "Core dumps may be processed with %s", core_path);110success = true;111#endif112} else if (getrlimit(RLIMIT_CORE, &rlim) != 0) {113jio_snprintf(buffer, bufferSize, "%s (may not exist)", core_path);114success = true;115} else {116switch(rlim.rlim_cur) {117case RLIM_INFINITY:118jio_snprintf(buffer, bufferSize, "%s", core_path);119success = true;120break;121case 0:122jio_snprintf(buffer, bufferSize, "Core dumps have been disabled. To enable core dumping, try \"ulimit -c unlimited\" before starting Java again");123success = false;124break;125default:126jio_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);127success = true;128break;129}130}131132VMError::record_coredump_status(buffer, success);133}134135int os::get_native_stack(address* stack, int frames, int toSkip) {136int frame_idx = 0;137int num_of_frames; // number of frames captured138frame fr = os::current_frame();139while (fr.pc() && frame_idx < frames) {140if (toSkip > 0) {141toSkip --;142} else {143stack[frame_idx ++] = fr.pc();144}145if (fr.fp() == NULL || fr.cb() != NULL ||146fr.sender_pc() == NULL || os::is_first_C_frame(&fr)) break;147148if (fr.sender_pc() && !os::is_first_C_frame(&fr)) {149fr = os::get_sender_for_C_frame(&fr);150} else {151break;152}153}154num_of_frames = frame_idx;155for (; frame_idx < frames; frame_idx ++) {156stack[frame_idx] = NULL;157}158159return num_of_frames;160}161162163bool os::unsetenv(const char* name) {164assert(name != NULL, "Null pointer");165return (::unsetenv(name) == 0);166}167168int os::get_last_error() {169return errno;170}171172size_t os::lasterror(char *buf, size_t len) {173if (errno == 0) return 0;174175const char *s = os::strerror(errno);176size_t n = ::strlen(s);177if (n >= len) {178n = len - 1;179}180::strncpy(buf, s, n);181buf[n] = '\0';182return n;183}184185void os::wait_for_keypress_at_exit(void) {186// don't do anything on posix platforms187return;188}189190int os::create_file_for_heap(const char* dir) {191int fd;192193#if defined(LINUX) && defined(O_TMPFILE)194char* native_dir = os::strdup(dir);195if (native_dir == NULL) {196vm_exit_during_initialization(err_msg("strdup failed during creation of backing file for heap (%s)", os::strerror(errno)));197return -1;198}199os::native_path(native_dir);200fd = os::open(dir, O_TMPFILE | O_RDWR, S_IRUSR | S_IWUSR);201os::free(native_dir);202203if (fd == -1)204#endif205{206const char name_template[] = "/jvmheap.XXXXXX";207208size_t fullname_len = strlen(dir) + strlen(name_template);209char *fullname = (char*)os::malloc(fullname_len + 1, mtInternal);210if (fullname == NULL) {211vm_exit_during_initialization(err_msg("Malloc failed during creation of backing file for heap (%s)", os::strerror(errno)));212return -1;213}214int n = snprintf(fullname, fullname_len + 1, "%s%s", dir, name_template);215assert((size_t)n == fullname_len, "Unexpected number of characters in string");216217os::native_path(fullname);218219// create a new file.220fd = mkstemp(fullname);221222if (fd < 0) {223warning("Could not create file for heap with template %s", fullname);224os::free(fullname);225return -1;226} else {227// delete the name from the filesystem. When 'fd' is closed, the file (and space) will be deleted.228int ret = unlink(fullname);229assert_with_errno(ret == 0, "unlink returned error");230}231232os::free(fullname);233}234235return fd;236}237238// Is a (classpath) directory empty?239bool os::dir_is_empty(const char* path) {240DIR *dir = NULL;241struct dirent *ptr;242243dir = ::opendir(path);244if (dir == NULL) return true;245246// Scan the directory247bool result = true;248while (result && (ptr = ::readdir(dir)) != NULL) {249if (strcmp(ptr->d_name, ".") != 0 && strcmp(ptr->d_name, "..") != 0) {250result = false;251}252}253::closedir(dir);254return result;255}256257static char* reserve_mmapped_memory(size_t bytes, char* requested_addr) {258char * addr;259int flags = MAP_PRIVATE NOT_AIX( | MAP_NORESERVE ) | MAP_ANONYMOUS;260if (requested_addr != NULL) {261assert((uintptr_t)requested_addr % os::vm_page_size() == 0, "Requested address should be aligned to OS page size");262flags |= MAP_FIXED;263}264265// Map reserved/uncommitted pages PROT_NONE so we fail early if we266// touch an uncommitted page. Otherwise, the read/write might267// succeed if we have enough swap space to back the physical page.268addr = (char*)::mmap(requested_addr, bytes, PROT_NONE,269flags, -1, 0);270271if (addr != MAP_FAILED) {272MemTracker::record_virtual_memory_reserve((address)addr, bytes, CALLER_PC);273return addr;274}275return NULL;276}277278static int util_posix_fallocate(int fd, off_t offset, off_t len) {279#ifdef __APPLE__280fstore_t store = { F_ALLOCATECONTIG, F_PEOFPOSMODE, 0, len };281// First we try to get a continuous chunk of disk space282int ret = fcntl(fd, F_PREALLOCATE, &store);283if (ret == -1) {284// Maybe we are too fragmented, try to allocate non-continuous range285store.fst_flags = F_ALLOCATEALL;286ret = fcntl(fd, F_PREALLOCATE, &store);287}288if(ret != -1) {289return ftruncate(fd, len);290}291return -1;292#else293return posix_fallocate(fd, offset, len);294#endif295}296297// Map the given address range to the provided file descriptor.298char* os::map_memory_to_file(char* base, size_t size, int fd) {299assert(fd != -1, "File descriptor is not valid");300301// allocate space for the file302int ret = util_posix_fallocate(fd, 0, (off_t)size);303if (ret != 0) {304vm_exit_during_initialization(err_msg("Error in mapping Java heap at the given filesystem directory. error(%d)", ret));305return NULL;306}307308int prot = PROT_READ | PROT_WRITE;309int flags = MAP_SHARED;310if (base != NULL) {311flags |= MAP_FIXED;312}313char* addr = (char*)mmap(base, size, prot, flags, fd, 0);314315if (addr == MAP_FAILED) {316warning("Failed mmap to file. (%s)", os::strerror(errno));317return NULL;318}319if (base != NULL && addr != base) {320if (!os::release_memory(addr, size)) {321warning("Could not release memory on unsuccessful file mapping");322}323return NULL;324}325return addr;326}327328char* os::replace_existing_mapping_with_file_mapping(char* base, size_t size, int fd) {329assert(fd != -1, "File descriptor is not valid");330assert(base != NULL, "Base cannot be NULL");331332return map_memory_to_file(base, size, fd);333}334335static size_t calculate_aligned_extra_size(size_t size, size_t alignment) {336assert((alignment & (os::vm_allocation_granularity() - 1)) == 0,337"Alignment must be a multiple of allocation granularity (page size)");338assert((size & (alignment -1)) == 0, "size must be 'alignment' aligned");339340size_t extra_size = size + alignment;341assert(extra_size >= size, "overflow, size is too large to allow alignment");342return extra_size;343}344345// After a bigger chunk was mapped, unmaps start and end parts to get the requested alignment.346static char* chop_extra_memory(size_t size, size_t alignment, char* extra_base, size_t extra_size) {347// Do manual alignment348char* aligned_base = align_up(extra_base, alignment);349350// [ | | ]351// ^ extra_base352// ^ extra_base + begin_offset == aligned_base353// extra_base + begin_offset + size ^354// extra_base + extra_size ^355// |<>| == begin_offset356// end_offset == |<>|357size_t begin_offset = aligned_base - extra_base;358size_t end_offset = (extra_base + extra_size) - (aligned_base + size);359360if (begin_offset > 0) {361os::release_memory(extra_base, begin_offset);362}363364if (end_offset > 0) {365os::release_memory(extra_base + begin_offset + size, end_offset);366}367368return aligned_base;369}370371// Multiple threads can race in this code, and can remap over each other with MAP_FIXED,372// so on posix, unmap the section at the start and at the end of the chunk that we mapped373// rather than unmapping and remapping the whole chunk to get requested alignment.374char* os::reserve_memory_aligned(size_t size, size_t alignment, bool exec) {375size_t extra_size = calculate_aligned_extra_size(size, alignment);376char* extra_base = os::reserve_memory(extra_size, exec);377if (extra_base == NULL) {378return NULL;379}380return chop_extra_memory(size, alignment, extra_base, extra_size);381}382383char* os::map_memory_to_file_aligned(size_t size, size_t alignment, int file_desc) {384size_t extra_size = calculate_aligned_extra_size(size, alignment);385// For file mapping, we do not call os:map_memory_to_file(size,fd) since:386// - we later chop away parts of the mapping using os::release_memory and that could fail if the387// original mmap call had been tied to an fd.388// - The memory API os::reserve_memory uses is an implementation detail. It may (and usually is)389// mmap but it also may System V shared memory which cannot be uncommitted as a whole, so390// chopping off and unmapping excess bits back and front (see below) would not work.391char* extra_base = reserve_mmapped_memory(extra_size, NULL);392if (extra_base == NULL) {393return NULL;394}395char* aligned_base = chop_extra_memory(size, alignment, extra_base, extra_size);396// After we have an aligned address, we can replace anonymous mapping with file mapping397if (replace_existing_mapping_with_file_mapping(aligned_base, size, file_desc) == NULL) {398vm_exit_during_initialization(err_msg("Error in mapping Java heap at the given filesystem directory"));399}400MemTracker::record_virtual_memory_commit((address)aligned_base, size, CALLER_PC);401return aligned_base;402}403404int os::vsnprintf(char* buf, size_t len, const char* fmt, va_list args) {405// All supported POSIX platforms provide C99 semantics.406int result = ::vsnprintf(buf, len, fmt, args);407// If an encoding error occurred (result < 0) then it's not clear408// whether the buffer is NUL terminated, so ensure it is.409if ((result < 0) && (len > 0)) {410buf[len - 1] = '\0';411}412return result;413}414415int os::get_fileno(FILE* fp) {416return NOT_AIX(::)fileno(fp);417}418419struct tm* os::gmtime_pd(const time_t* clock, struct tm* res) {420return gmtime_r(clock, res);421}422423void os::Posix::print_load_average(outputStream* st) {424st->print("load average: ");425double loadavg[3];426int res = os::loadavg(loadavg, 3);427if (res != -1) {428st->print("%0.02f %0.02f %0.02f", loadavg[0], loadavg[1], loadavg[2]);429} else {430st->print(" Unavailable");431}432st->cr();433}434435// boot/uptime information;436// unfortunately it does not work on macOS and Linux because the utx chain has no entry437// for reboot at least on my test machines438void os::Posix::print_uptime_info(outputStream* st) {439int bootsec = -1;440int currsec = time(NULL);441struct utmpx* ent;442setutxent();443while ((ent = getutxent())) {444if (!strcmp("system boot", ent->ut_line)) {445bootsec = ent->ut_tv.tv_sec;446break;447}448}449450if (bootsec != -1) {451os::print_dhm(st, "OS uptime:", (long) (currsec-bootsec));452}453}454455static void print_rlimit(outputStream* st, const char* msg,456int resource, bool output_k = false) {457struct rlimit rlim;458459st->print(" %s ", msg);460int res = getrlimit(resource, &rlim);461if (res == -1) {462st->print("could not obtain value");463} else {464// soft limit465if (rlim.rlim_cur == RLIM_INFINITY) { st->print("infinity"); }466else {467if (output_k) { st->print(UINT64_FORMAT "k", uint64_t(rlim.rlim_cur) / 1024); }468else { st->print(UINT64_FORMAT, uint64_t(rlim.rlim_cur)); }469}470// hard limit471st->print("/");472if (rlim.rlim_max == RLIM_INFINITY) { st->print("infinity"); }473else {474if (output_k) { st->print(UINT64_FORMAT "k", uint64_t(rlim.rlim_max) / 1024); }475else { st->print(UINT64_FORMAT, uint64_t(rlim.rlim_max)); }476}477}478}479480void os::Posix::print_rlimit_info(outputStream* st) {481st->print("rlimit (soft/hard):");482print_rlimit(st, "STACK", RLIMIT_STACK, true);483print_rlimit(st, ", CORE", RLIMIT_CORE, true);484485#if defined(AIX)486st->print(", NPROC ");487st->print("%d", sysconf(_SC_CHILD_MAX));488489print_rlimit(st, ", THREADS", RLIMIT_THREADS);490#else491print_rlimit(st, ", NPROC", RLIMIT_NPROC);492#endif493494print_rlimit(st, ", NOFILE", RLIMIT_NOFILE);495print_rlimit(st, ", AS", RLIMIT_AS, true);496print_rlimit(st, ", CPU", RLIMIT_CPU);497print_rlimit(st, ", DATA", RLIMIT_DATA, true);498499// maximum size of files that the process may create500print_rlimit(st, ", FSIZE", RLIMIT_FSIZE, true);501502#if defined(LINUX) || defined(__APPLE__)503// maximum number of bytes of memory that may be locked into RAM504// (rounded down to the nearest multiple of system pagesize)505print_rlimit(st, ", MEMLOCK", RLIMIT_MEMLOCK, true);506#endif507508// MacOS; The maximum size (in bytes) to which a process's resident set size may grow.509#if defined(__APPLE__)510print_rlimit(st, ", RSS", RLIMIT_RSS, true);511#endif512513st->cr();514}515516void os::Posix::print_uname_info(outputStream* st) {517// kernel518st->print("uname: ");519struct utsname name;520uname(&name);521st->print("%s ", name.sysname);522#ifdef ASSERT523st->print("%s ", name.nodename);524#endif525st->print("%s ", name.release);526st->print("%s ", name.version);527st->print("%s", name.machine);528st->cr();529}530531void os::Posix::print_umask(outputStream* st, mode_t umsk) {532st->print((umsk & S_IRUSR) ? "r" : "-");533st->print((umsk & S_IWUSR) ? "w" : "-");534st->print((umsk & S_IXUSR) ? "x" : "-");535st->print((umsk & S_IRGRP) ? "r" : "-");536st->print((umsk & S_IWGRP) ? "w" : "-");537st->print((umsk & S_IXGRP) ? "x" : "-");538st->print((umsk & S_IROTH) ? "r" : "-");539st->print((umsk & S_IWOTH) ? "w" : "-");540st->print((umsk & S_IXOTH) ? "x" : "-");541}542543void os::Posix::print_user_info(outputStream* st) {544unsigned id = (unsigned) ::getuid();545st->print("uid : %u ", id);546id = (unsigned) ::geteuid();547st->print("euid : %u ", id);548id = (unsigned) ::getgid();549st->print("gid : %u ", id);550id = (unsigned) ::getegid();551st->print_cr("egid : %u", id);552st->cr();553554mode_t umsk = ::umask(0);555::umask(umsk);556st->print("umask: %04o (", (unsigned) umsk);557print_umask(st, umsk);558st->print_cr(")");559st->cr();560}561562563bool os::get_host_name(char* buf, size_t buflen) {564struct utsname name;565uname(&name);566jio_snprintf(buf, buflen, "%s", name.nodename);567return true;568}569570#ifndef _LP64571// Helper, on 32bit, for os::has_allocatable_memory_limit572static bool is_allocatable(size_t s) {573if (s < 2 * G) {574return true;575}576// Use raw anonymous mmap here; no need to go through any577// of our reservation layers. We will unmap right away.578void* p = ::mmap(NULL, s, PROT_NONE,579MAP_PRIVATE | MAP_NORESERVE | MAP_ANONYMOUS, -1, 0);580if (p == MAP_FAILED) {581return false;582} else {583::munmap(p, s);584return true;585}586}587#endif // !_LP64588589590bool os::has_allocatable_memory_limit(size_t* limit) {591struct rlimit rlim;592int getrlimit_res = getrlimit(RLIMIT_AS, &rlim);593// if there was an error when calling getrlimit, assume that there is no limitation594// on virtual memory.595bool result;596if ((getrlimit_res != 0) || (rlim.rlim_cur == RLIM_INFINITY)) {597result = false;598} else {599*limit = (size_t)rlim.rlim_cur;600result = true;601}602#ifdef _LP64603return result;604#else605// arbitrary virtual space limit for 32 bit Unices found by testing. If606// getrlimit above returned a limit, bound it with this limit. Otherwise607// directly use it.608const size_t max_virtual_limit = 3800*M;609if (result) {610*limit = MIN2(*limit, max_virtual_limit);611} else {612*limit = max_virtual_limit;613}614615// bound by actually allocatable memory. The algorithm uses two bounds, an616// upper and a lower limit. The upper limit is the current highest amount of617// memory that could not be allocated, the lower limit is the current highest618// amount of memory that could be allocated.619// The algorithm iteratively refines the result by halving the difference620// between these limits, updating either the upper limit (if that value could621// not be allocated) or the lower limit (if the that value could be allocated)622// until the difference between these limits is "small".623624// the minimum amount of memory we care about allocating.625const size_t min_allocation_size = M;626627size_t upper_limit = *limit;628629// first check a few trivial cases630if (is_allocatable(upper_limit) || (upper_limit <= min_allocation_size)) {631*limit = upper_limit;632} else if (!is_allocatable(min_allocation_size)) {633// we found that not even min_allocation_size is allocatable. Return it634// anyway. There is no point to search for a better value any more.635*limit = min_allocation_size;636} else {637// perform the binary search.638size_t lower_limit = min_allocation_size;639while ((upper_limit - lower_limit) > min_allocation_size) {640size_t temp_limit = ((upper_limit - lower_limit) / 2) + lower_limit;641temp_limit = align_down(temp_limit, min_allocation_size);642if (is_allocatable(temp_limit)) {643lower_limit = temp_limit;644} else {645upper_limit = temp_limit;646}647}648*limit = lower_limit;649}650return true;651#endif652}653654void* os::get_default_process_handle() {655#ifdef __APPLE__656// MacOS X needs to use RTLD_FIRST instead of RTLD_LAZY657// to avoid finding unexpected symbols on second (or later)658// loads of a library.659return (void*)::dlopen(NULL, RTLD_FIRST);660#else661return (void*)::dlopen(NULL, RTLD_LAZY);662#endif663}664665void* os::dll_lookup(void* handle, const char* name) {666return dlsym(handle, name);667}668669void os::dll_unload(void *lib) {670const char* l_path = LINUX_ONLY(os::Linux::dll_path(lib))671NOT_LINUX("<not available>");672if (l_path == NULL) l_path = "<not available>";673int res = ::dlclose(lib);674675if (res == 0) {676Events::log_dll_message(NULL, "Unloaded shared library \"%s\" [" INTPTR_FORMAT "]",677l_path, p2i(lib));678log_info(os)("Unloaded shared library \"%s\" [" INTPTR_FORMAT "]", l_path, p2i(lib));679} else {680const char* error_report = ::dlerror();681if (error_report == NULL) {682error_report = "dlerror returned no error description";683}684685Events::log_dll_message(NULL, "Attempt to unload shared library \"%s\" [" INTPTR_FORMAT "] failed, %s",686l_path, p2i(lib), error_report);687log_info(os)("Attempt to unload shared library \"%s\" [" INTPTR_FORMAT "] failed, %s",688l_path, p2i(lib), error_report);689}690}691692jlong os::lseek(int fd, jlong offset, int whence) {693return (jlong) BSD_ONLY(::lseek) NOT_BSD(::lseek64)(fd, offset, whence);694}695696int os::fsync(int fd) {697return ::fsync(fd);698}699700int os::ftruncate(int fd, jlong length) {701return BSD_ONLY(::ftruncate) NOT_BSD(::ftruncate64)(fd, length);702}703704const char* os::get_current_directory(char *buf, size_t buflen) {705return getcwd(buf, buflen);706}707708FILE* os::open(int fd, const char* mode) {709return ::fdopen(fd, mode);710}711712size_t os::write(int fd, const void *buf, unsigned int nBytes) {713size_t res;714RESTARTABLE((size_t) ::write(fd, buf, (size_t) nBytes), res);715return res;716}717718ssize_t os::read_at(int fd, void *buf, unsigned int nBytes, jlong offset) {719return ::pread(fd, buf, nBytes, offset);720}721722int os::close(int fd) {723return ::close(fd);724}725726void os::flockfile(FILE* fp) {727::flockfile(fp);728}729730void os::funlockfile(FILE* fp) {731::funlockfile(fp);732}733734DIR* os::opendir(const char* dirname) {735assert(dirname != NULL, "just checking");736return ::opendir(dirname);737}738739struct dirent* os::readdir(DIR* dirp) {740assert(dirp != NULL, "just checking");741return ::readdir(dirp);742}743744int os::closedir(DIR *dirp) {745assert(dirp != NULL, "just checking");746return ::closedir(dirp);747}748749int os::socket_close(int fd) {750return ::close(fd);751}752753int os::socket(int domain, int type, int protocol) {754return ::socket(domain, type, protocol);755}756757int os::recv(int fd, char* buf, size_t nBytes, uint flags) {758RESTARTABLE_RETURN_INT(::recv(fd, buf, nBytes, flags));759}760761int os::send(int fd, char* buf, size_t nBytes, uint flags) {762RESTARTABLE_RETURN_INT(::send(fd, buf, nBytes, flags));763}764765int os::raw_send(int fd, char* buf, size_t nBytes, uint flags) {766return os::send(fd, buf, nBytes, flags);767}768769int os::connect(int fd, struct sockaddr* him, socklen_t len) {770RESTARTABLE_RETURN_INT(::connect(fd, him, len));771}772773struct hostent* os::get_host_by_name(char* name) {774return ::gethostbyname(name);775}776777void os::exit(int num) {778::exit(num);779}780781// Builds a platform dependent Agent_OnLoad_<lib_name> function name782// which is used to find statically linked in agents.783// Parameters:784// sym_name: Symbol in library we are looking for785// lib_name: Name of library to look in, NULL for shared libs.786// is_absolute_path == true if lib_name is absolute path to agent787// such as "/a/b/libL.so"788// == false if only the base name of the library is passed in789// such as "L"790char* os::build_agent_function_name(const char *sym_name, const char *lib_name,791bool is_absolute_path) {792char *agent_entry_name;793size_t len;794size_t name_len;795size_t prefix_len = strlen(JNI_LIB_PREFIX);796size_t suffix_len = strlen(JNI_LIB_SUFFIX);797const char *start;798799if (lib_name != NULL) {800name_len = strlen(lib_name);801if (is_absolute_path) {802// Need to strip path, prefix and suffix803if ((start = strrchr(lib_name, *os::file_separator())) != NULL) {804lib_name = ++start;805}806if (strlen(lib_name) <= (prefix_len + suffix_len)) {807return NULL;808}809lib_name += prefix_len;810name_len = strlen(lib_name) - suffix_len;811}812}813len = (lib_name != NULL ? name_len : 0) + strlen(sym_name) + 2;814agent_entry_name = NEW_C_HEAP_ARRAY_RETURN_NULL(char, len, mtThread);815if (agent_entry_name == NULL) {816return NULL;817}818strcpy(agent_entry_name, sym_name);819if (lib_name != NULL) {820strcat(agent_entry_name, "_");821strncat(agent_entry_name, lib_name, name_len);822}823return agent_entry_name;824}825826// Sleep forever; naked call to OS-specific sleep; use with CAUTION827void os::infinite_sleep() {828while (true) { // sleep forever ...829::sleep(100); // ... 100 seconds at a time830}831}832833void os::naked_short_nanosleep(jlong ns) {834struct timespec req;835assert(ns > -1 && ns < NANOUNITS, "Un-interruptable sleep, short time use only");836req.tv_sec = 0;837req.tv_nsec = ns;838::nanosleep(&req, NULL);839return;840}841842void os::naked_short_sleep(jlong ms) {843assert(ms < MILLIUNITS, "Un-interruptable sleep, short time use only");844os::naked_short_nanosleep(millis_to_nanos(ms));845return;846}847848char* os::Posix::describe_pthread_attr(char* buf, size_t buflen, const pthread_attr_t* attr) {849size_t stack_size = 0;850size_t guard_size = 0;851int detachstate = 0;852pthread_attr_getstacksize(attr, &stack_size);853pthread_attr_getguardsize(attr, &guard_size);854// Work around linux NPTL implementation error, see also os::create_thread() in os_linux.cpp.855LINUX_ONLY(stack_size -= guard_size);856pthread_attr_getdetachstate(attr, &detachstate);857jio_snprintf(buf, buflen, "stacksize: " SIZE_FORMAT "k, guardsize: " SIZE_FORMAT "k, %s",858stack_size / 1024, guard_size / 1024,859(detachstate == PTHREAD_CREATE_DETACHED ? "detached" : "joinable"));860return buf;861}862863char* os::Posix::realpath(const char* filename, char* outbuf, size_t outbuflen) {864865if (filename == NULL || outbuf == NULL || outbuflen < 1) {866assert(false, "os::Posix::realpath: invalid arguments.");867errno = EINVAL;868return NULL;869}870871char* result = NULL;872873// This assumes platform realpath() is implemented according to POSIX.1-2008.874// POSIX.1-2008 allows to specify NULL for the output buffer, in which case875// output buffer is dynamically allocated and must be ::free()'d by the caller.876char* p = ::realpath(filename, NULL);877if (p != NULL) {878if (strlen(p) < outbuflen) {879strcpy(outbuf, p);880result = outbuf;881} else {882errno = ENAMETOOLONG;883}884::free(p); // *not* os::free885} else {886// Fallback for platforms struggling with modern Posix standards (AIX 5.3, 6.1). If realpath887// returns EINVAL, this may indicate that realpath is not POSIX.1-2008 compatible and888// that it complains about the NULL we handed down as user buffer.889// In this case, use the user provided buffer but at least check whether realpath caused890// a memory overwrite.891if (errno == EINVAL) {892outbuf[outbuflen - 1] = '\0';893p = ::realpath(filename, outbuf);894if (p != NULL) {895guarantee(outbuf[outbuflen - 1] == '\0', "realpath buffer overwrite detected.");896result = p;897}898}899}900return result;901902}903904int os::stat(const char *path, struct stat *sbuf) {905return ::stat(path, sbuf);906}907908char * os::native_path(char *path) {909return path;910}911912bool os::same_files(const char* file1, const char* file2) {913if (file1 == nullptr && file2 == nullptr) {914return true;915}916917if (file1 == nullptr || file2 == nullptr) {918return false;919}920921if (strcmp(file1, file2) == 0) {922return true;923}924925bool is_same = false;926struct stat st1;927struct stat st2;928929if (os::stat(file1, &st1) < 0) {930return false;931}932933if (os::stat(file2, &st2) < 0) {934return false;935}936937if (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino) {938// same files939is_same = true;940}941return is_same;942}943944// Check minimum allowable stack sizes for thread creation and to initialize945// the java system classes, including StackOverflowError - depends on page946// size.947// The space needed for frames during startup is platform dependent. It948// depends on word size, platform calling conventions, C frame layout and949// interpreter/C1/C2 design decisions. Therefore this is given in a950// platform (os/cpu) dependent constant.951// To this, space for guard mechanisms is added, which depends on the952// page size which again depends on the concrete system the VM is running953// on. Space for libc guard pages is not included in this size.954jint os::Posix::set_minimum_stack_sizes() {955size_t os_min_stack_allowed = PTHREAD_STACK_MIN;956957_java_thread_min_stack_allowed = _java_thread_min_stack_allowed +958StackOverflow::stack_guard_zone_size() +959StackOverflow::stack_shadow_zone_size();960961_java_thread_min_stack_allowed = align_up(_java_thread_min_stack_allowed, vm_page_size());962_java_thread_min_stack_allowed = MAX2(_java_thread_min_stack_allowed, os_min_stack_allowed);963964size_t stack_size_in_bytes = ThreadStackSize * K;965if (stack_size_in_bytes != 0 &&966stack_size_in_bytes < _java_thread_min_stack_allowed) {967// The '-Xss' and '-XX:ThreadStackSize=N' options both set968// ThreadStackSize so we go with "Java thread stack size" instead969// of "ThreadStackSize" to be more friendly.970tty->print_cr("\nThe Java thread stack size specified is too small. "971"Specify at least " SIZE_FORMAT "k",972_java_thread_min_stack_allowed / K);973return JNI_ERR;974}975976// Make the stack size a multiple of the page size so that977// the yellow/red zones can be guarded.978JavaThread::set_stack_size_at_create(align_up(stack_size_in_bytes, vm_page_size()));979980// Reminder: a compiler thread is a Java thread.981_compiler_thread_min_stack_allowed = _compiler_thread_min_stack_allowed +982StackOverflow::stack_guard_zone_size() +983StackOverflow::stack_shadow_zone_size();984985_compiler_thread_min_stack_allowed = align_up(_compiler_thread_min_stack_allowed, vm_page_size());986_compiler_thread_min_stack_allowed = MAX2(_compiler_thread_min_stack_allowed, os_min_stack_allowed);987988stack_size_in_bytes = CompilerThreadStackSize * K;989if (stack_size_in_bytes != 0 &&990stack_size_in_bytes < _compiler_thread_min_stack_allowed) {991tty->print_cr("\nThe CompilerThreadStackSize specified is too small. "992"Specify at least " SIZE_FORMAT "k",993_compiler_thread_min_stack_allowed / K);994return JNI_ERR;995}996997_vm_internal_thread_min_stack_allowed = align_up(_vm_internal_thread_min_stack_allowed, vm_page_size());998_vm_internal_thread_min_stack_allowed = MAX2(_vm_internal_thread_min_stack_allowed, os_min_stack_allowed);9991000stack_size_in_bytes = VMThreadStackSize * K;1001if (stack_size_in_bytes != 0 &&1002stack_size_in_bytes < _vm_internal_thread_min_stack_allowed) {1003tty->print_cr("\nThe VMThreadStackSize specified is too small. "1004"Specify at least " SIZE_FORMAT "k",1005_vm_internal_thread_min_stack_allowed / K);1006return JNI_ERR;1007}1008return JNI_OK;1009}10101011// Called when creating the thread. The minimum stack sizes have already been calculated1012size_t os::Posix::get_initial_stack_size(ThreadType thr_type, size_t req_stack_size) {1013size_t stack_size;1014if (req_stack_size == 0) {1015stack_size = default_stack_size(thr_type);1016} else {1017stack_size = req_stack_size;1018}10191020switch (thr_type) {1021case os::java_thread:1022// Java threads use ThreadStackSize which default value can be1023// changed with the flag -Xss1024if (req_stack_size == 0 && JavaThread::stack_size_at_create() > 0) {1025// no requested size and we have a more specific default value1026stack_size = JavaThread::stack_size_at_create();1027}1028stack_size = MAX2(stack_size,1029_java_thread_min_stack_allowed);1030break;1031case os::compiler_thread:1032if (req_stack_size == 0 && CompilerThreadStackSize > 0) {1033// no requested size and we have a more specific default value1034stack_size = (size_t)(CompilerThreadStackSize * K);1035}1036stack_size = MAX2(stack_size,1037_compiler_thread_min_stack_allowed);1038break;1039case os::vm_thread:1040case os::pgc_thread:1041case os::cgc_thread:1042case os::watcher_thread:1043default: // presume the unknown thr_type is a VM internal1044if (req_stack_size == 0 && VMThreadStackSize > 0) {1045// no requested size and we have a more specific default value1046stack_size = (size_t)(VMThreadStackSize * K);1047}10481049stack_size = MAX2(stack_size,1050_vm_internal_thread_min_stack_allowed);1051break;1052}10531054// pthread_attr_setstacksize() may require that the size be rounded up to the OS page size.1055// Be careful not to round up to 0. Align down in that case.1056if (stack_size <= SIZE_MAX - vm_page_size()) {1057stack_size = align_up(stack_size, vm_page_size());1058} else {1059stack_size = align_down(stack_size, vm_page_size());1060}10611062return stack_size;1063}10641065#ifndef ZERO1066#ifndef ARM1067static bool get_frame_at_stack_banging_point(JavaThread* thread, address pc, const void* ucVoid, frame* fr) {1068if (Interpreter::contains(pc)) {1069// interpreter performs stack banging after the fixed frame header has1070// been generated while the compilers perform it before. To maintain1071// semantic consistency between interpreted and compiled frames, the1072// method returns the Java sender of the current frame.1073*fr = os::fetch_frame_from_context(ucVoid);1074if (!fr->is_first_java_frame()) {1075// get_frame_at_stack_banging_point() is only called when we1076// have well defined stacks so java_sender() calls do not need1077// to assert safe_for_sender() first.1078*fr = fr->java_sender();1079}1080} else {1081// more complex code with compiled code1082assert(!Interpreter::contains(pc), "Interpreted methods should have been handled above");1083CodeBlob* cb = CodeCache::find_blob(pc);1084if (cb == NULL || !cb->is_nmethod() || cb->is_frame_complete_at(pc)) {1085// Not sure where the pc points to, fallback to default1086// stack overflow handling1087return false;1088} else {1089// in compiled code, the stack banging is performed just after the return pc1090// has been pushed on the stack1091*fr = os::fetch_compiled_frame_from_context(ucVoid);1092if (!fr->is_java_frame()) {1093assert(!fr->is_first_frame(), "Safety check");1094// See java_sender() comment above.1095*fr = fr->java_sender();1096}1097}1098}1099assert(fr->is_java_frame(), "Safety check");1100return true;1101}1102#endif // ARM11031104// This return true if the signal handler should just continue, ie. return after calling this1105bool os::Posix::handle_stack_overflow(JavaThread* thread, address addr, address pc,1106const void* ucVoid, address* stub) {1107// stack overflow1108StackOverflow* overflow_state = thread->stack_overflow_state();1109if (overflow_state->in_stack_yellow_reserved_zone(addr)) {1110if (thread->thread_state() == _thread_in_Java) {1111#ifndef ARM1112// arm32 doesn't have this1113if (overflow_state->in_stack_reserved_zone(addr)) {1114frame fr;1115if (get_frame_at_stack_banging_point(thread, pc, ucVoid, &fr)) {1116assert(fr.is_java_frame(), "Must be a Java frame");1117frame activation =1118SharedRuntime::look_for_reserved_stack_annotated_method(thread, fr);1119if (activation.sp() != NULL) {1120overflow_state->disable_stack_reserved_zone();1121if (activation.is_interpreted_frame()) {1122overflow_state->set_reserved_stack_activation((address)(activation.fp()1123// Some platforms use frame pointers for interpreter frames, others use initial sp.1124#if !defined(PPC64) && !defined(S390)1125+ frame::interpreter_frame_initial_sp_offset1126#endif1127));1128} else {1129overflow_state->set_reserved_stack_activation((address)activation.unextended_sp());1130}1131return true; // just continue1132}1133}1134}1135#endif // ARM1136// Throw a stack overflow exception. Guard pages will be reenabled1137// while unwinding the stack.1138overflow_state->disable_stack_yellow_reserved_zone();1139*stub = SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::STACK_OVERFLOW);1140} else {1141// Thread was in the vm or native code. Return and try to finish.1142overflow_state->disable_stack_yellow_reserved_zone();1143return true; // just continue1144}1145} else if (overflow_state->in_stack_red_zone(addr)) {1146// Fatal red zone violation. Disable the guard pages and fall through1147// to handle_unexpected_exception way down below.1148overflow_state->disable_stack_red_zone();1149tty->print_raw_cr("An irrecoverable stack overflow has occurred.");11501151// This is a likely cause, but hard to verify. Let's just print1152// it as a hint.1153tty->print_raw_cr("Please check if any of your loaded .so files has "1154"enabled executable stack (see man page execstack(8))");11551156} else {1157#if !defined(AIX) && !defined(__APPLE__)1158// bsd and aix don't have this11591160// Accessing stack address below sp may cause SEGV if current1161// thread has MAP_GROWSDOWN stack. This should only happen when1162// current thread was created by user code with MAP_GROWSDOWN flag1163// and then attached to VM. See notes in os_linux.cpp.1164if (thread->osthread()->expanding_stack() == 0) {1165thread->osthread()->set_expanding_stack();1166if (os::Linux::manually_expand_stack(thread, addr)) {1167thread->osthread()->clear_expanding_stack();1168return true; // just continue1169}1170thread->osthread()->clear_expanding_stack();1171} else {1172fatal("recursive segv. expanding stack.");1173}1174#else1175tty->print_raw_cr("SIGSEGV happened inside stack but outside yellow and red zone.");1176#endif // AIX or BSD1177}1178return false;1179}1180#endif // ZERO11811182bool os::Posix::is_root(uid_t uid){1183return ROOT_UID == uid;1184}11851186bool os::Posix::matches_effective_uid_or_root(uid_t uid) {1187return is_root(uid) || geteuid() == uid;1188}11891190bool os::Posix::matches_effective_uid_and_gid_or_root(uid_t uid, gid_t gid) {1191return is_root(uid) || (geteuid() == uid && getegid() == gid);1192}11931194Thread* os::ThreadCrashProtection::_protected_thread = NULL;1195os::ThreadCrashProtection* os::ThreadCrashProtection::_crash_protection = NULL;11961197os::ThreadCrashProtection::ThreadCrashProtection() {1198_protected_thread = Thread::current();1199assert(_protected_thread->is_JfrSampler_thread(), "should be JFRSampler");1200}12011202/*1203* See the caveats for this class in os_posix.hpp1204* Protects the callback call so that SIGSEGV / SIGBUS jumps back into this1205* method and returns false. If none of the signals are raised, returns true.1206* The callback is supposed to provide the method that should be protected.1207*/1208bool os::ThreadCrashProtection::call(os::CrashProtectionCallback& cb) {1209sigset_t saved_sig_mask;12101211// we cannot rely on sigsetjmp/siglongjmp to save/restore the signal mask1212// since on at least some systems (OS X) siglongjmp will restore the mask1213// for the process, not the thread1214pthread_sigmask(0, NULL, &saved_sig_mask);1215if (sigsetjmp(_jmpbuf, 0) == 0) {1216// make sure we can see in the signal handler that we have crash protection1217// installed1218_crash_protection = this;1219cb.call();1220// and clear the crash protection1221_crash_protection = NULL;1222_protected_thread = NULL;1223return true;1224}1225// this happens when we siglongjmp() back1226pthread_sigmask(SIG_SETMASK, &saved_sig_mask, NULL);1227_crash_protection = NULL;1228_protected_thread = NULL;1229return false;1230}12311232void os::ThreadCrashProtection::restore() {1233assert(_crash_protection != NULL, "must have crash protection");1234siglongjmp(_jmpbuf, 1);1235}12361237void os::ThreadCrashProtection::check_crash_protection(int sig,1238Thread* thread) {12391240if (thread != NULL &&1241thread == _protected_thread &&1242_crash_protection != NULL) {12431244if (sig == SIGSEGV || sig == SIGBUS) {1245_crash_protection->restore();1246}1247}1248}12491250// Shared clock/time and other supporting routines for pthread_mutex/cond1251// initialization. This is enabled on Solaris but only some of the clock/time1252// functionality is actually used there.12531254// Shared condattr object for use with relative timed-waits. Will be associated1255// with CLOCK_MONOTONIC if available to avoid issues with time-of-day changes,1256// but otherwise whatever default is used by the platform - generally the1257// time-of-day clock.1258static pthread_condattr_t _condAttr[1];12591260// Shared mutexattr to explicitly set the type to PTHREAD_MUTEX_NORMAL as not1261// all systems (e.g. FreeBSD) map the default to "normal".1262static pthread_mutexattr_t _mutexAttr[1];12631264// common basic initialization that is always supported1265static void pthread_init_common(void) {1266int status;1267if ((status = pthread_condattr_init(_condAttr)) != 0) {1268fatal("pthread_condattr_init: %s", os::strerror(status));1269}1270if ((status = pthread_mutexattr_init(_mutexAttr)) != 0) {1271fatal("pthread_mutexattr_init: %s", os::strerror(status));1272}1273if ((status = pthread_mutexattr_settype(_mutexAttr, PTHREAD_MUTEX_NORMAL)) != 0) {1274fatal("pthread_mutexattr_settype: %s", os::strerror(status));1275}1276os::PlatformMutex::init();1277}12781279static int (*_pthread_condattr_setclock)(pthread_condattr_t *, clockid_t) = NULL;12801281static bool _use_clock_monotonic_condattr = false;12821283// Determine what POSIX API's are present and do appropriate1284// configuration.1285void os::Posix::init(void) {12861287// NOTE: no logging available when this is called. Put logging1288// statements in init_2().12891290// Check for pthread_condattr_setclock support.12911292// libpthread is already loaded.1293int (*condattr_setclock_func)(pthread_condattr_t*, clockid_t) =1294(int (*)(pthread_condattr_t*, clockid_t))dlsym(RTLD_DEFAULT,1295"pthread_condattr_setclock");1296if (condattr_setclock_func != NULL) {1297_pthread_condattr_setclock = condattr_setclock_func;1298}12991300// Now do general initialization.13011302pthread_init_common();13031304int status;1305if (_pthread_condattr_setclock != NULL) {1306if ((status = _pthread_condattr_setclock(_condAttr, CLOCK_MONOTONIC)) != 0) {1307if (status == EINVAL) {1308_use_clock_monotonic_condattr = false;1309warning("Unable to use monotonic clock with relative timed-waits" \1310" - changes to the time-of-day clock may have adverse affects");1311} else {1312fatal("pthread_condattr_setclock: %s", os::strerror(status));1313}1314} else {1315_use_clock_monotonic_condattr = true;1316}1317}1318}13191320void os::Posix::init_2(void) {1321log_info(os)("Use of CLOCK_MONOTONIC is supported");1322log_info(os)("Use of pthread_condattr_setclock is%s supported",1323(_pthread_condattr_setclock != NULL ? "" : " not"));1324log_info(os)("Relative timed-wait using pthread_cond_timedwait is associated with %s",1325_use_clock_monotonic_condattr ? "CLOCK_MONOTONIC" : "the default clock");1326}13271328// Utility to convert the given timeout to an absolute timespec1329// (based on the appropriate clock) to use with pthread_cond_timewait,1330// and sem_timedwait().1331// The clock queried here must be the clock used to manage the1332// timeout of the condition variable or semaphore.1333//1334// The passed in timeout value is either a relative time in nanoseconds1335// or an absolute time in milliseconds. A relative timeout will be1336// associated with CLOCK_MONOTONIC if available, unless the real-time clock1337// is explicitly requested; otherwise, or if absolute,1338// the default time-of-day clock will be used.13391340// Given time is a 64-bit value and the time_t used in the timespec is1341// sometimes a signed-32-bit value we have to watch for overflow if times1342// way in the future are given. Further on Solaris versions1343// prior to 10 there is a restriction (see cond_timedwait) that the specified1344// number of seconds, in abstime, is less than current_time + 100000000.1345// As it will be over 20 years before "now + 100000000" will overflow we can1346// ignore overflow and just impose a hard-limit on seconds using the value1347// of "now + 100000000". This places a limit on the timeout of about 3.171348// years from "now".1349//1350#define MAX_SECS 10000000013511352// Calculate a new absolute time that is "timeout" nanoseconds from "now".1353// "unit" indicates the unit of "now_part_sec" (may be nanos or micros depending1354// on which clock API is being used).1355static void calc_rel_time(timespec* abstime, jlong timeout, jlong now_sec,1356jlong now_part_sec, jlong unit) {1357time_t max_secs = now_sec + MAX_SECS;13581359jlong seconds = timeout / NANOUNITS;1360timeout %= NANOUNITS; // remaining nanos13611362if (seconds >= MAX_SECS) {1363// More seconds than we can add, so pin to max_secs.1364abstime->tv_sec = max_secs;1365abstime->tv_nsec = 0;1366} else {1367abstime->tv_sec = now_sec + seconds;1368long nanos = (now_part_sec * (NANOUNITS / unit)) + timeout;1369if (nanos >= NANOUNITS) { // overflow1370abstime->tv_sec += 1;1371nanos -= NANOUNITS;1372}1373abstime->tv_nsec = nanos;1374}1375}13761377// Unpack the given deadline in milliseconds since the epoch, into the given timespec.1378// The current time in seconds is also passed in to enforce an upper bound as discussed above.1379static void unpack_abs_time(timespec* abstime, jlong deadline, jlong now_sec) {1380time_t max_secs = now_sec + MAX_SECS;13811382jlong seconds = deadline / MILLIUNITS;1383jlong millis = deadline % MILLIUNITS;13841385if (seconds >= max_secs) {1386// Absolute seconds exceeds allowed max, so pin to max_secs.1387abstime->tv_sec = max_secs;1388abstime->tv_nsec = 0;1389} else {1390abstime->tv_sec = seconds;1391abstime->tv_nsec = millis_to_nanos(millis);1392}1393}13941395static jlong millis_to_nanos_bounded(jlong millis) {1396// We have to watch for overflow when converting millis to nanos,1397// but if millis is that large then we will end up limiting to1398// MAX_SECS anyway, so just do that here.1399if (millis / MILLIUNITS > MAX_SECS) {1400millis = jlong(MAX_SECS) * MILLIUNITS;1401}1402return millis_to_nanos(millis);1403}14041405static void to_abstime(timespec* abstime, jlong timeout,1406bool isAbsolute, bool isRealtime) {1407DEBUG_ONLY(int max_secs = MAX_SECS;)14081409if (timeout < 0) {1410timeout = 0;1411}14121413clockid_t clock = CLOCK_MONOTONIC;1414if (isAbsolute || (!_use_clock_monotonic_condattr || isRealtime)) {1415clock = CLOCK_REALTIME;1416}14171418struct timespec now;1419int status = clock_gettime(clock, &now);1420assert(status == 0, "clock_gettime error: %s", os::strerror(errno));14211422if (!isAbsolute) {1423calc_rel_time(abstime, timeout, now.tv_sec, now.tv_nsec, NANOUNITS);1424} else {1425unpack_abs_time(abstime, timeout, now.tv_sec);1426}1427DEBUG_ONLY(max_secs += now.tv_sec;)14281429assert(abstime->tv_sec >= 0, "tv_sec < 0");1430assert(abstime->tv_sec <= max_secs, "tv_sec > max_secs");1431assert(abstime->tv_nsec >= 0, "tv_nsec < 0");1432assert(abstime->tv_nsec < NANOUNITS, "tv_nsec >= NANOUNITS");1433}14341435// Create an absolute time 'millis' milliseconds in the future, using the1436// real-time (time-of-day) clock. Used by PosixSemaphore.1437void os::Posix::to_RTC_abstime(timespec* abstime, int64_t millis) {1438to_abstime(abstime, millis_to_nanos_bounded(millis),1439false /* not absolute */,1440true /* use real-time clock */);1441}14421443// Common (partly) shared time functions14441445jlong os::javaTimeMillis() {1446struct timespec ts;1447int status = clock_gettime(CLOCK_REALTIME, &ts);1448assert(status == 0, "clock_gettime error: %s", os::strerror(errno));1449return jlong(ts.tv_sec) * MILLIUNITS +1450jlong(ts.tv_nsec) / NANOUNITS_PER_MILLIUNIT;1451}14521453void os::javaTimeSystemUTC(jlong &seconds, jlong &nanos) {1454struct timespec ts;1455int status = clock_gettime(CLOCK_REALTIME, &ts);1456assert(status == 0, "clock_gettime error: %s", os::strerror(errno));1457seconds = jlong(ts.tv_sec);1458nanos = jlong(ts.tv_nsec);1459}14601461// macOS and AIX have platform specific implementations for javaTimeNanos()1462// using native clock/timer access APIs. These have historically worked well1463// for those platforms, but it may be possible for them to switch to the1464// generic clock_gettime mechanism in the future.1465#if !defined(__APPLE__) && !defined(AIX)14661467jlong os::javaTimeNanos() {1468struct timespec tp;1469int status = clock_gettime(CLOCK_MONOTONIC, &tp);1470assert(status == 0, "clock_gettime error: %s", os::strerror(errno));1471jlong result = jlong(tp.tv_sec) * NANOSECS_PER_SEC + jlong(tp.tv_nsec);1472return result;1473}14741475// for timer info max values which include all bits1476#define ALL_64_BITS CONST64(0xFFFFFFFFFFFFFFFF)14771478void os::javaTimeNanos_info(jvmtiTimerInfo *info_ptr) {1479// CLOCK_MONOTONIC - amount of time since some arbitrary point in the past1480info_ptr->max_value = ALL_64_BITS;1481info_ptr->may_skip_backward = false; // not subject to resetting or drifting1482info_ptr->may_skip_forward = false; // not subject to resetting or drifting1483info_ptr->kind = JVMTI_TIMER_ELAPSED; // elapsed not CPU time1484}14851486#endif // ! APPLE && !AIX14871488// Shared pthread_mutex/cond based PlatformEvent implementation.1489// Not currently usable by Solaris.149014911492// PlatformEvent1493//1494// Assumption:1495// Only one parker can exist on an event, which is why we allocate1496// them per-thread. Multiple unparkers can coexist.1497//1498// _event serves as a restricted-range semaphore.1499// -1 : thread is blocked, i.e. there is a waiter1500// 0 : neutral: thread is running or ready,1501// could have been signaled after a wait started1502// 1 : signaled - thread is running or ready1503//1504// Having three states allows for some detection of bad usage - see1505// comments on unpark().15061507os::PlatformEvent::PlatformEvent() {1508int status = pthread_cond_init(_cond, _condAttr);1509assert_status(status == 0, status, "cond_init");1510status = pthread_mutex_init(_mutex, _mutexAttr);1511assert_status(status == 0, status, "mutex_init");1512_event = 0;1513_nParked = 0;1514}15151516void os::PlatformEvent::park() { // AKA "down()"1517// Transitions for _event:1518// -1 => -1 : illegal1519// 1 => 0 : pass - return immediately1520// 0 => -1 : block; then set _event to 0 before returning15211522// Invariant: Only the thread associated with the PlatformEvent1523// may call park().1524assert(_nParked == 0, "invariant");15251526int v;15271528// atomically decrement _event1529for (;;) {1530v = _event;1531if (Atomic::cmpxchg(&_event, v, v - 1) == v) break;1532}1533guarantee(v >= 0, "invariant");15341535if (v == 0) { // Do this the hard way by blocking ...1536int status = pthread_mutex_lock(_mutex);1537assert_status(status == 0, status, "mutex_lock");1538guarantee(_nParked == 0, "invariant");1539++_nParked;1540while (_event < 0) {1541// OS-level "spurious wakeups" are ignored1542status = pthread_cond_wait(_cond, _mutex);1543assert_status(status == 0 MACOS_ONLY(|| status == ETIMEDOUT),1544status, "cond_wait");1545}1546--_nParked;15471548_event = 0;1549status = pthread_mutex_unlock(_mutex);1550assert_status(status == 0, status, "mutex_unlock");1551// Paranoia to ensure our locked and lock-free paths interact1552// correctly with each other.1553OrderAccess::fence();1554}1555guarantee(_event >= 0, "invariant");1556}15571558int os::PlatformEvent::park(jlong millis) {1559// Transitions for _event:1560// -1 => -1 : illegal1561// 1 => 0 : pass - return immediately1562// 0 => -1 : block; then set _event to 0 before returning15631564// Invariant: Only the thread associated with the Event/PlatformEvent1565// may call park().1566assert(_nParked == 0, "invariant");15671568int v;1569// atomically decrement _event1570for (;;) {1571v = _event;1572if (Atomic::cmpxchg(&_event, v, v - 1) == v) break;1573}1574guarantee(v >= 0, "invariant");15751576if (v == 0) { // Do this the hard way by blocking ...1577struct timespec abst;1578to_abstime(&abst, millis_to_nanos_bounded(millis), false, false);15791580int ret = OS_TIMEOUT;1581int status = pthread_mutex_lock(_mutex);1582assert_status(status == 0, status, "mutex_lock");1583guarantee(_nParked == 0, "invariant");1584++_nParked;15851586while (_event < 0) {1587status = pthread_cond_timedwait(_cond, _mutex, &abst);1588assert_status(status == 0 || status == ETIMEDOUT,1589status, "cond_timedwait");1590// OS-level "spurious wakeups" are ignored unless the archaic1591// FilterSpuriousWakeups is set false. That flag should be obsoleted.1592if (!FilterSpuriousWakeups) break;1593if (status == ETIMEDOUT) break;1594}1595--_nParked;15961597if (_event >= 0) {1598ret = OS_OK;1599}16001601_event = 0;1602status = pthread_mutex_unlock(_mutex);1603assert_status(status == 0, status, "mutex_unlock");1604// Paranoia to ensure our locked and lock-free paths interact1605// correctly with each other.1606OrderAccess::fence();1607return ret;1608}1609return OS_OK;1610}16111612void os::PlatformEvent::unpark() {1613// Transitions for _event:1614// 0 => 1 : just return1615// 1 => 1 : just return1616// -1 => either 0 or 1; must signal target thread1617// That is, we can safely transition _event from -1 to either1618// 0 or 1.1619// See also: "Semaphores in Plan 9" by Mullender & Cox1620//1621// Note: Forcing a transition from "-1" to "1" on an unpark() means1622// that it will take two back-to-back park() calls for the owning1623// thread to block. This has the benefit of forcing a spurious return1624// from the first park() call after an unpark() call which will help1625// shake out uses of park() and unpark() without checking state conditions1626// properly. This spurious return doesn't manifest itself in any user code1627// but only in the correctly written condition checking loops of ObjectMonitor,1628// Mutex/Monitor, and JavaThread::sleep16291630if (Atomic::xchg(&_event, 1) >= 0) return;16311632int status = pthread_mutex_lock(_mutex);1633assert_status(status == 0, status, "mutex_lock");1634int anyWaiters = _nParked;1635assert(anyWaiters == 0 || anyWaiters == 1, "invariant");1636status = pthread_mutex_unlock(_mutex);1637assert_status(status == 0, status, "mutex_unlock");16381639// Note that we signal() *after* dropping the lock for "immortal" Events.1640// This is safe and avoids a common class of futile wakeups. In rare1641// circumstances this can cause a thread to return prematurely from1642// cond_{timed}wait() but the spurious wakeup is benign and the victim1643// will simply re-test the condition and re-park itself.1644// This provides particular benefit if the underlying platform does not1645// provide wait morphing.16461647if (anyWaiters != 0) {1648status = pthread_cond_signal(_cond);1649assert_status(status == 0, status, "cond_signal");1650}1651}16521653// JSR166 support16541655os::PlatformParker::PlatformParker() : _counter(0), _cur_index(-1) {1656int status = pthread_cond_init(&_cond[REL_INDEX], _condAttr);1657assert_status(status == 0, status, "cond_init rel");1658status = pthread_cond_init(&_cond[ABS_INDEX], NULL);1659assert_status(status == 0, status, "cond_init abs");1660status = pthread_mutex_init(_mutex, _mutexAttr);1661assert_status(status == 0, status, "mutex_init");1662}16631664os::PlatformParker::~PlatformParker() {1665int status = pthread_cond_destroy(&_cond[REL_INDEX]);1666assert_status(status == 0, status, "cond_destroy rel");1667status = pthread_cond_destroy(&_cond[ABS_INDEX]);1668assert_status(status == 0, status, "cond_destroy abs");1669status = pthread_mutex_destroy(_mutex);1670assert_status(status == 0, status, "mutex_destroy");1671}16721673// Parker::park decrements count if > 0, else does a condvar wait. Unpark1674// sets count to 1 and signals condvar. Only one thread ever waits1675// on the condvar. Contention seen when trying to park implies that someone1676// is unparking you, so don't wait. And spurious returns are fine, so there1677// is no need to track notifications.16781679void Parker::park(bool isAbsolute, jlong time) {16801681// Optional fast-path check:1682// Return immediately if a permit is available.1683// We depend on Atomic::xchg() having full barrier semantics1684// since we are doing a lock-free update to _counter.1685if (Atomic::xchg(&_counter, 0) > 0) return;16861687JavaThread *jt = JavaThread::current();16881689// Optional optimization -- avoid state transitions if there's1690// an interrupt pending.1691if (jt->is_interrupted(false)) {1692return;1693}16941695// Next, demultiplex/decode time arguments1696struct timespec absTime;1697if (time < 0 || (isAbsolute && time == 0)) { // don't wait at all1698return;1699}1700if (time > 0) {1701to_abstime(&absTime, time, isAbsolute, false);1702}17031704// Enter safepoint region1705// Beware of deadlocks such as 6317397.1706// The per-thread Parker:: mutex is a classic leaf-lock.1707// In particular a thread must never block on the Threads_lock while1708// holding the Parker:: mutex. If safepoints are pending both the1709// the ThreadBlockInVM() CTOR and DTOR may grab Threads_lock.1710ThreadBlockInVM tbivm(jt);17111712// Can't access interrupt state now that we are _thread_blocked. If we've1713// been interrupted since we checked above then _counter will be > 0.17141715// Don't wait if cannot get lock since interference arises from1716// unparking.1717if (pthread_mutex_trylock(_mutex) != 0) {1718return;1719}17201721int status;1722if (_counter > 0) { // no wait needed1723_counter = 0;1724status = pthread_mutex_unlock(_mutex);1725assert_status(status == 0, status, "invariant");1726// Paranoia to ensure our locked and lock-free paths interact1727// correctly with each other and Java-level accesses.1728OrderAccess::fence();1729return;1730}17311732OSThreadWaitState osts(jt->osthread(), false /* not Object.wait() */);17331734assert(_cur_index == -1, "invariant");1735if (time == 0) {1736_cur_index = REL_INDEX; // arbitrary choice when not timed1737status = pthread_cond_wait(&_cond[_cur_index], _mutex);1738assert_status(status == 0 MACOS_ONLY(|| status == ETIMEDOUT),1739status, "cond_wait");1740}1741else {1742_cur_index = isAbsolute ? ABS_INDEX : REL_INDEX;1743status = pthread_cond_timedwait(&_cond[_cur_index], _mutex, &absTime);1744assert_status(status == 0 || status == ETIMEDOUT,1745status, "cond_timedwait");1746}1747_cur_index = -1;17481749_counter = 0;1750status = pthread_mutex_unlock(_mutex);1751assert_status(status == 0, status, "invariant");1752// Paranoia to ensure our locked and lock-free paths interact1753// correctly with each other and Java-level accesses.1754OrderAccess::fence();1755}17561757void Parker::unpark() {1758int status = pthread_mutex_lock(_mutex);1759assert_status(status == 0, status, "invariant");1760const int s = _counter;1761_counter = 1;1762// must capture correct index before unlocking1763int index = _cur_index;1764status = pthread_mutex_unlock(_mutex);1765assert_status(status == 0, status, "invariant");17661767// Note that we signal() *after* dropping the lock for "immortal" Events.1768// This is safe and avoids a common class of futile wakeups. In rare1769// circumstances this can cause a thread to return prematurely from1770// cond_{timed}wait() but the spurious wakeup is benign and the victim1771// will simply re-test the condition and re-park itself.1772// This provides particular benefit if the underlying platform does not1773// provide wait morphing.17741775if (s < 1 && index != -1) {1776// thread is definitely parked1777status = pthread_cond_signal(&_cond[index]);1778assert_status(status == 0, status, "invariant");1779}1780}17811782// Platform Mutex/Monitor implementation17831784#if PLATFORM_MONITOR_IMPL_INDIRECT17851786os::PlatformMutex::Mutex::Mutex() : _next(NULL) {1787int status = pthread_mutex_init(&_mutex, _mutexAttr);1788assert_status(status == 0, status, "mutex_init");1789}17901791os::PlatformMutex::Mutex::~Mutex() {1792int status = pthread_mutex_destroy(&_mutex);1793assert_status(status == 0, status, "mutex_destroy");1794}17951796pthread_mutex_t os::PlatformMutex::_freelist_lock;1797os::PlatformMutex::Mutex* os::PlatformMutex::_mutex_freelist = NULL;17981799void os::PlatformMutex::init() {1800int status = pthread_mutex_init(&_freelist_lock, _mutexAttr);1801assert_status(status == 0, status, "freelist lock init");1802}18031804struct os::PlatformMutex::WithFreeListLocked : public StackObj {1805WithFreeListLocked() {1806int status = pthread_mutex_lock(&_freelist_lock);1807assert_status(status == 0, status, "freelist lock");1808}18091810~WithFreeListLocked() {1811int status = pthread_mutex_unlock(&_freelist_lock);1812assert_status(status == 0, status, "freelist unlock");1813}1814};18151816os::PlatformMutex::PlatformMutex() {1817{1818WithFreeListLocked wfl;1819_impl = _mutex_freelist;1820if (_impl != NULL) {1821_mutex_freelist = _impl->_next;1822_impl->_next = NULL;1823return;1824}1825}1826_impl = new Mutex();1827}18281829os::PlatformMutex::~PlatformMutex() {1830WithFreeListLocked wfl;1831assert(_impl->_next == NULL, "invariant");1832_impl->_next = _mutex_freelist;1833_mutex_freelist = _impl;1834}18351836os::PlatformMonitor::Cond::Cond() : _next(NULL) {1837int status = pthread_cond_init(&_cond, _condAttr);1838assert_status(status == 0, status, "cond_init");1839}18401841os::PlatformMonitor::Cond::~Cond() {1842int status = pthread_cond_destroy(&_cond);1843assert_status(status == 0, status, "cond_destroy");1844}18451846os::PlatformMonitor::Cond* os::PlatformMonitor::_cond_freelist = NULL;18471848os::PlatformMonitor::PlatformMonitor() {1849{1850WithFreeListLocked wfl;1851_impl = _cond_freelist;1852if (_impl != NULL) {1853_cond_freelist = _impl->_next;1854_impl->_next = NULL;1855return;1856}1857}1858_impl = new Cond();1859}18601861os::PlatformMonitor::~PlatformMonitor() {1862WithFreeListLocked wfl;1863assert(_impl->_next == NULL, "invariant");1864_impl->_next = _cond_freelist;1865_cond_freelist = _impl;1866}18671868#else18691870os::PlatformMutex::PlatformMutex() {1871int status = pthread_mutex_init(&_mutex, _mutexAttr);1872assert_status(status == 0, status, "mutex_init");1873}18741875os::PlatformMutex::~PlatformMutex() {1876int status = pthread_mutex_destroy(&_mutex);1877assert_status(status == 0, status, "mutex_destroy");1878}18791880os::PlatformMonitor::PlatformMonitor() {1881int status = pthread_cond_init(&_cond, _condAttr);1882assert_status(status == 0, status, "cond_init");1883}18841885os::PlatformMonitor::~PlatformMonitor() {1886int status = pthread_cond_destroy(&_cond);1887assert_status(status == 0, status, "cond_destroy");1888}18891890#endif // PLATFORM_MONITOR_IMPL_INDIRECT18911892// Must already be locked1893int os::PlatformMonitor::wait(jlong millis) {1894assert(millis >= 0, "negative timeout");1895if (millis > 0) {1896struct timespec abst;1897// We have to watch for overflow when converting millis to nanos,1898// but if millis is that large then we will end up limiting to1899// MAX_SECS anyway, so just do that here.1900if (millis / MILLIUNITS > MAX_SECS) {1901millis = jlong(MAX_SECS) * MILLIUNITS;1902}1903to_abstime(&abst, millis_to_nanos(millis), false, false);19041905int ret = OS_TIMEOUT;1906int status = pthread_cond_timedwait(cond(), mutex(), &abst);1907assert_status(status == 0 || status == ETIMEDOUT,1908status, "cond_timedwait");1909if (status == 0) {1910ret = OS_OK;1911}1912return ret;1913} else {1914int status = pthread_cond_wait(cond(), mutex());1915assert_status(status == 0 MACOS_ONLY(|| status == ETIMEDOUT),1916status, "cond_wait");1917return OS_OK;1918}1919}19201921// Darwin has no "environ" in a dynamic library.1922#ifdef __APPLE__1923#define environ (*_NSGetEnviron())1924#else1925extern char** environ;1926#endif19271928char** os::get_environ() { return environ; }19291930// Run the specified command in a separate process. Return its exit value,1931// or -1 on failure (e.g. can't fork a new process).1932// Notes: -Unlike system(), this function can be called from signal handler. It1933// doesn't block SIGINT et al.1934// -this function is unsafe to use in non-error situations, mainly1935// because the child process will inherit all parent descriptors.1936int os::fork_and_exec(const char* cmd, bool prefer_vfork) {1937const char * argv[4] = {"sh", "-c", cmd, NULL};19381939pid_t pid ;19401941char** env = os::get_environ();19421943// Use always vfork on AIX, since its safe and helps with analyzing OOM situations.1944// Otherwise leave it up to the caller.1945AIX_ONLY(prefer_vfork = true;)1946#ifdef __APPLE__1947pid = ::fork();1948#else1949pid = prefer_vfork ? ::vfork() : ::fork();1950#endif19511952if (pid < 0) {1953// fork failed1954return -1;19551956} else if (pid == 0) {1957// child process19581959::execve("/bin/sh", (char* const*)argv, env);19601961// execve failed1962::_exit(-1);19631964} else {1965// copied from J2SE ..._waitForProcessExit() in UNIXProcess_md.c; we don't1966// care about the actual exit code, for now.19671968int status;19691970// Wait for the child process to exit. This returns immediately if1971// the child has already exited. */1972while (::waitpid(pid, &status, 0) < 0) {1973switch (errno) {1974case ECHILD: return 0;1975case EINTR: break;1976default: return -1;1977}1978}19791980if (WIFEXITED(status)) {1981// The child exited normally; get its exit code.1982return WEXITSTATUS(status);1983} else if (WIFSIGNALED(status)) {1984// The child exited because of a signal1985// The best value to return is 0x80 + signal number,1986// because that is what all Unix shells do, and because1987// it allows callers to distinguish between process exit and1988// process death by signal.1989return 0x80 + WTERMSIG(status);1990} else {1991// Unknown exit code; pass it through1992return status;1993}1994}1995}19961997////////////////////////////////////////////////////////////////////////////////1998// runtime exit support19992000// Note: os::shutdown() might be called very early during initialization, or2001// called from signal handler. Before adding something to os::shutdown(), make2002// sure it is async-safe and can handle partially initialized VM.2003void os::shutdown() {20042005// allow PerfMemory to attempt cleanup of any persistent resources2006perfMemory_exit();20072008// needs to remove object in file system2009AttachListener::abort();20102011// flush buffered output, finish log files2012ostream_abort();20132014// Check for abort hook2015abort_hook_t abort_hook = Arguments::abort_hook();2016if (abort_hook != NULL) {2017abort_hook();2018}20192020}20212022// Note: os::abort() might be called very early during initialization, or2023// called from signal handler. Before adding something to os::abort(), make2024// sure it is async-safe and can handle partially initialized VM.2025// Also note we can abort while other threads continue to run, so we can2026// easily trigger secondary faults in those threads. To reduce the likelihood2027// of that we use _exit rather than exit, so that no atexit hooks get run.2028// But note that os::shutdown() could also trigger secondary faults.2029void os::abort(bool dump_core, void* siginfo, const void* context) {2030os::shutdown();2031if (dump_core) {2032LINUX_ONLY(if (DumpPrivateMappingsInCore) ClassLoader::close_jrt_image();)2033::abort(); // dump core2034}2035::_exit(1);2036}20372038// Die immediately, no exit hook, no abort hook, no cleanup.2039// Dump a core file, if possible, for debugging.2040void os::die() {2041if (TestUnresponsiveErrorHandler && !CreateCoredumpOnCrash) {2042// For TimeoutInErrorHandlingTest.java, we just kill the VM2043// and don't take the time to generate a core file.2044os::signal_raise(SIGKILL);2045} else {2046::abort();2047}2048}204920502051