Path: blob/master/src/hotspot/os/posix/os_posix.cpp
40930 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#ifndef __ANDROID__68#include <utmpx.h>69#endif7071#ifdef __APPLE__72#include <crt_externs.h>73#endif7475#define ROOT_UID 07677#ifndef MAP_ANONYMOUS78#define MAP_ANONYMOUS MAP_ANON79#endif8081#define check_with_errno(check_type, cond, msg) \82do { \83int err = errno; \84check_type(cond, "%s; error='%s' (errno=%s)", msg, os::strerror(err), \85os::errno_name(err)); \86} while (false)8788#define assert_with_errno(cond, msg) check_with_errno(assert, cond, msg)89#define guarantee_with_errno(cond, msg) check_with_errno(guarantee, cond, msg)9091// Check core dump limit and report possible place where core can be found92void os::check_dump_limit(char* buffer, size_t bufferSize) {93if (!FLAG_IS_DEFAULT(CreateCoredumpOnCrash) && !CreateCoredumpOnCrash) {94jio_snprintf(buffer, bufferSize, "CreateCoredumpOnCrash is disabled from command line");95VMError::record_coredump_status(buffer, false);96return;97}9899int n;100struct rlimit rlim;101bool success;102103char core_path[PATH_MAX];104n = get_core_path(core_path, PATH_MAX);105106if (n <= 0) {107jio_snprintf(buffer, bufferSize, "core.%d (may not exist)", current_process_id());108success = true;109#ifdef LINUX110} else if (core_path[0] == '"') { // redirect to user process111jio_snprintf(buffer, bufferSize, "Core dumps may be processed with %s", core_path);112success = true;113#endif114} else if (getrlimit(RLIMIT_CORE, &rlim) != 0) {115jio_snprintf(buffer, bufferSize, "%s (may not exist)", core_path);116success = true;117} else {118switch(rlim.rlim_cur) {119case RLIM_INFINITY:120jio_snprintf(buffer, bufferSize, "%s", core_path);121success = true;122break;123case 0:124jio_snprintf(buffer, bufferSize, "Core dumps have been disabled. To enable core dumping, try \"ulimit -c unlimited\" before starting Java again");125success = false;126break;127default:128jio_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);129success = true;130break;131}132}133134VMError::record_coredump_status(buffer, success);135}136137int os::get_native_stack(address* stack, int frames, int toSkip) {138int frame_idx = 0;139int num_of_frames; // number of frames captured140frame fr = os::current_frame();141while (fr.pc() && frame_idx < frames) {142if (toSkip > 0) {143toSkip --;144} else {145stack[frame_idx ++] = fr.pc();146}147if (fr.fp() == NULL || fr.cb() != NULL ||148fr.sender_pc() == NULL || os::is_first_C_frame(&fr)) break;149150if (fr.sender_pc() && !os::is_first_C_frame(&fr)) {151fr = os::get_sender_for_C_frame(&fr);152} else {153break;154}155}156num_of_frames = frame_idx;157for (; frame_idx < frames; frame_idx ++) {158stack[frame_idx] = NULL;159}160161return num_of_frames;162}163164165bool os::unsetenv(const char* name) {166assert(name != NULL, "Null pointer");167return (::unsetenv(name) == 0);168}169170int os::get_last_error() {171return errno;172}173174size_t os::lasterror(char *buf, size_t len) {175if (errno == 0) return 0;176177const char *s = os::strerror(errno);178size_t n = ::strlen(s);179if (n >= len) {180n = len - 1;181}182::strncpy(buf, s, n);183buf[n] = '\0';184return n;185}186187void os::wait_for_keypress_at_exit(void) {188// don't do anything on posix platforms189return;190}191192int os::create_file_for_heap(const char* dir) {193int fd;194195#if defined(LINUX) && defined(O_TMPFILE)196char* native_dir = os::strdup(dir);197if (native_dir == NULL) {198vm_exit_during_initialization(err_msg("strdup failed during creation of backing file for heap (%s)", os::strerror(errno)));199return -1;200}201os::native_path(native_dir);202fd = os::open(dir, O_TMPFILE | O_RDWR, S_IRUSR | S_IWUSR);203os::free(native_dir);204205if (fd == -1)206#endif207{208const char name_template[] = "/jvmheap.XXXXXX";209210size_t fullname_len = strlen(dir) + strlen(name_template);211char *fullname = (char*)os::malloc(fullname_len + 1, mtInternal);212if (fullname == NULL) {213vm_exit_during_initialization(err_msg("Malloc failed during creation of backing file for heap (%s)", os::strerror(errno)));214return -1;215}216int n = snprintf(fullname, fullname_len + 1, "%s%s", dir, name_template);217assert((size_t)n == fullname_len, "Unexpected number of characters in string");218219os::native_path(fullname);220221// create a new file.222fd = mkstemp(fullname);223224if (fd < 0) {225warning("Could not create file for heap with template %s", fullname);226os::free(fullname);227return -1;228} else {229// delete the name from the filesystem. When 'fd' is closed, the file (and space) will be deleted.230int ret = unlink(fullname);231assert_with_errno(ret == 0, "unlink returned error");232}233234os::free(fullname);235}236237return fd;238}239240static char* reserve_mmapped_memory(size_t bytes, char* requested_addr) {241char * addr;242int flags = MAP_PRIVATE NOT_AIX( | MAP_NORESERVE ) | MAP_ANONYMOUS;243if (requested_addr != NULL) {244assert((uintptr_t)requested_addr % os::vm_page_size() == 0, "Requested address should be aligned to OS page size");245flags |= MAP_FIXED;246}247248// Map reserved/uncommitted pages PROT_NONE so we fail early if we249// touch an uncommitted page. Otherwise, the read/write might250// succeed if we have enough swap space to back the physical page.251addr = (char*)::mmap(requested_addr, bytes, PROT_NONE,252flags, -1, 0);253254if (addr != MAP_FAILED) {255MemTracker::record_virtual_memory_reserve((address)addr, bytes, CALLER_PC);256return addr;257}258return NULL;259}260261static int util_posix_fallocate(int fd, off_t offset, off_t len) {262#ifdef __APPLE__263fstore_t store = { F_ALLOCATECONTIG, F_PEOFPOSMODE, 0, len };264// First we try to get a continuous chunk of disk space265int ret = fcntl(fd, F_PREALLOCATE, &store);266if (ret == -1) {267// Maybe we are too fragmented, try to allocate non-continuous range268store.fst_flags = F_ALLOCATEALL;269ret = fcntl(fd, F_PREALLOCATE, &store);270}271if(ret != -1) {272return ftruncate(fd, len);273}274return -1;275#else276return posix_fallocate(fd, offset, len);277#endif278}279280// Map the given address range to the provided file descriptor.281char* os::map_memory_to_file(char* base, size_t size, int fd) {282assert(fd != -1, "File descriptor is not valid");283284// allocate space for the file285int ret = util_posix_fallocate(fd, 0, (off_t)size);286if (ret != 0) {287vm_exit_during_initialization(err_msg("Error in mapping Java heap at the given filesystem directory. error(%d)", ret));288return NULL;289}290291int prot = PROT_READ | PROT_WRITE;292int flags = MAP_SHARED;293if (base != NULL) {294flags |= MAP_FIXED;295}296char* addr = (char*)mmap(base, size, prot, flags, fd, 0);297298if (addr == MAP_FAILED) {299warning("Failed mmap to file. (%s)", os::strerror(errno));300return NULL;301}302if (base != NULL && addr != base) {303if (!os::release_memory(addr, size)) {304warning("Could not release memory on unsuccessful file mapping");305}306return NULL;307}308return addr;309}310311char* os::replace_existing_mapping_with_file_mapping(char* base, size_t size, int fd) {312assert(fd != -1, "File descriptor is not valid");313assert(base != NULL, "Base cannot be NULL");314315return map_memory_to_file(base, size, fd);316}317318static size_t calculate_aligned_extra_size(size_t size, size_t alignment) {319assert((alignment & (os::vm_allocation_granularity() - 1)) == 0,320"Alignment must be a multiple of allocation granularity (page size)");321assert((size & (alignment -1)) == 0, "size must be 'alignment' aligned");322323size_t extra_size = size + alignment;324assert(extra_size >= size, "overflow, size is too large to allow alignment");325return extra_size;326}327328// After a bigger chunk was mapped, unmaps start and end parts to get the requested alignment.329static char* chop_extra_memory(size_t size, size_t alignment, char* extra_base, size_t extra_size) {330// Do manual alignment331char* aligned_base = align_up(extra_base, alignment);332333// [ | | ]334// ^ extra_base335// ^ extra_base + begin_offset == aligned_base336// extra_base + begin_offset + size ^337// extra_base + extra_size ^338// |<>| == begin_offset339// end_offset == |<>|340size_t begin_offset = aligned_base - extra_base;341size_t end_offset = (extra_base + extra_size) - (aligned_base + size);342343if (begin_offset > 0) {344os::release_memory(extra_base, begin_offset);345}346347if (end_offset > 0) {348os::release_memory(extra_base + begin_offset + size, end_offset);349}350351return aligned_base;352}353354// Multiple threads can race in this code, and can remap over each other with MAP_FIXED,355// so on posix, unmap the section at the start and at the end of the chunk that we mapped356// rather than unmapping and remapping the whole chunk to get requested alignment.357char* os::reserve_memory_aligned(size_t size, size_t alignment, bool exec) {358size_t extra_size = calculate_aligned_extra_size(size, alignment);359char* extra_base = os::reserve_memory(extra_size, exec);360if (extra_base == NULL) {361return NULL;362}363return chop_extra_memory(size, alignment, extra_base, extra_size);364}365366char* os::map_memory_to_file_aligned(size_t size, size_t alignment, int file_desc) {367size_t extra_size = calculate_aligned_extra_size(size, alignment);368// For file mapping, we do not call os:map_memory_to_file(size,fd) since:369// - we later chop away parts of the mapping using os::release_memory and that could fail if the370// original mmap call had been tied to an fd.371// - The memory API os::reserve_memory uses is an implementation detail. It may (and usually is)372// mmap but it also may System V shared memory which cannot be uncommitted as a whole, so373// chopping off and unmapping excess bits back and front (see below) would not work.374char* extra_base = reserve_mmapped_memory(extra_size, NULL);375if (extra_base == NULL) {376return NULL;377}378char* aligned_base = chop_extra_memory(size, alignment, extra_base, extra_size);379// After we have an aligned address, we can replace anonymous mapping with file mapping380if (replace_existing_mapping_with_file_mapping(aligned_base, size, file_desc) == NULL) {381vm_exit_during_initialization(err_msg("Error in mapping Java heap at the given filesystem directory"));382}383MemTracker::record_virtual_memory_commit((address)aligned_base, size, CALLER_PC);384return aligned_base;385}386387int os::vsnprintf(char* buf, size_t len, const char* fmt, va_list args) {388// All supported POSIX platforms provide C99 semantics.389int result = ::vsnprintf(buf, len, fmt, args);390// If an encoding error occurred (result < 0) then it's not clear391// whether the buffer is NUL terminated, so ensure it is.392if ((result < 0) && (len > 0)) {393buf[len - 1] = '\0';394}395return result;396}397398int os::get_fileno(FILE* fp) {399return NOT_AIX(::)fileno(fp);400}401402struct tm* os::gmtime_pd(const time_t* clock, struct tm* res) {403return gmtime_r(clock, res);404}405406void os::Posix::print_load_average(outputStream* st) {407st->print("load average: ");408double loadavg[3];409int res = os::loadavg(loadavg, 3);410if (res != -1) {411st->print("%0.02f %0.02f %0.02f", loadavg[0], loadavg[1], loadavg[2]);412} else {413st->print(" Unavailable");414}415st->cr();416}417418// boot/uptime information;419// unfortunately it does not work on macOS and Linux because the utx chain has no entry420// for reboot at least on my test machines421void os::Posix::print_uptime_info(outputStream* st) {422#ifndef __ANDROID__423int bootsec = -1;424int currsec = time(NULL);425struct utmpx* ent;426setutxent();427while ((ent = getutxent())) {428if (!strcmp("system boot", ent->ut_line)) {429bootsec = ent->ut_tv.tv_sec;430break;431}432}433434if (bootsec != -1) {435os::print_dhm(st, "OS uptime:", (long) (currsec-bootsec));436}437#endif438}439440static void print_rlimit(outputStream* st, const char* msg,441int resource, bool output_k = false) {442struct rlimit rlim;443444st->print(" %s ", msg);445int res = getrlimit(resource, &rlim);446if (res == -1) {447st->print("could not obtain value");448} else {449// soft limit450if (rlim.rlim_cur == RLIM_INFINITY) { st->print("infinity"); }451else {452if (output_k) { st->print(UINT64_FORMAT "k", uint64_t(rlim.rlim_cur) / 1024); }453else { st->print(UINT64_FORMAT, uint64_t(rlim.rlim_cur)); }454}455// hard limit456st->print("/");457if (rlim.rlim_max == RLIM_INFINITY) { st->print("infinity"); }458else {459if (output_k) { st->print(UINT64_FORMAT "k", uint64_t(rlim.rlim_max) / 1024); }460else { st->print(UINT64_FORMAT, uint64_t(rlim.rlim_max)); }461}462}463}464465void os::Posix::print_rlimit_info(outputStream* st) {466st->print("rlimit (soft/hard):");467print_rlimit(st, "STACK", RLIMIT_STACK, true);468print_rlimit(st, ", CORE", RLIMIT_CORE, true);469470#if defined(AIX)471st->print(", NPROC ");472st->print("%d", sysconf(_SC_CHILD_MAX));473474print_rlimit(st, ", THREADS", RLIMIT_THREADS);475#else476print_rlimit(st, ", NPROC", RLIMIT_NPROC);477#endif478479print_rlimit(st, ", NOFILE", RLIMIT_NOFILE);480print_rlimit(st, ", AS", RLIMIT_AS, true);481print_rlimit(st, ", CPU", RLIMIT_CPU);482print_rlimit(st, ", DATA", RLIMIT_DATA, true);483484// maximum size of files that the process may create485print_rlimit(st, ", FSIZE", RLIMIT_FSIZE, true);486487#if defined(LINUX) || defined(__APPLE__)488// maximum number of bytes of memory that may be locked into RAM489// (rounded down to the nearest multiple of system pagesize)490print_rlimit(st, ", MEMLOCK", RLIMIT_MEMLOCK, true);491#endif492493// MacOS; The maximum size (in bytes) to which a process's resident set size may grow.494#if defined(__APPLE__)495print_rlimit(st, ", RSS", RLIMIT_RSS, true);496#endif497498st->cr();499}500501void os::Posix::print_uname_info(outputStream* st) {502// kernel503st->print("uname: ");504struct utsname name;505uname(&name);506st->print("%s ", name.sysname);507#ifdef ASSERT508st->print("%s ", name.nodename);509#endif510st->print("%s ", name.release);511st->print("%s ", name.version);512st->print("%s", name.machine);513st->cr();514}515516void os::Posix::print_umask(outputStream* st, mode_t umsk) {517st->print((umsk & S_IRUSR) ? "r" : "-");518st->print((umsk & S_IWUSR) ? "w" : "-");519st->print((umsk & S_IXUSR) ? "x" : "-");520st->print((umsk & S_IRGRP) ? "r" : "-");521st->print((umsk & S_IWGRP) ? "w" : "-");522st->print((umsk & S_IXGRP) ? "x" : "-");523st->print((umsk & S_IROTH) ? "r" : "-");524st->print((umsk & S_IWOTH) ? "w" : "-");525st->print((umsk & S_IXOTH) ? "x" : "-");526}527528void os::Posix::print_user_info(outputStream* st) {529unsigned id = (unsigned) ::getuid();530st->print("uid : %u ", id);531id = (unsigned) ::geteuid();532st->print("euid : %u ", id);533id = (unsigned) ::getgid();534st->print("gid : %u ", id);535id = (unsigned) ::getegid();536st->print_cr("egid : %u", id);537st->cr();538539mode_t umsk = ::umask(0);540::umask(umsk);541st->print("umask: %04o (", (unsigned) umsk);542print_umask(st, umsk);543st->print_cr(")");544st->cr();545}546547548bool os::get_host_name(char* buf, size_t buflen) {549struct utsname name;550uname(&name);551jio_snprintf(buf, buflen, "%s", name.nodename);552return true;553}554555#ifndef _LP64556// Helper, on 32bit, for os::has_allocatable_memory_limit557static bool is_allocatable(size_t s) {558if (s < 2 * G) {559return true;560}561// Use raw anonymous mmap here; no need to go through any562// of our reservation layers. We will unmap right away.563void* p = ::mmap(NULL, s, PROT_NONE,564MAP_PRIVATE | MAP_NORESERVE | MAP_ANONYMOUS, -1, 0);565if (p == MAP_FAILED) {566return false;567} else {568::munmap(p, s);569return true;570}571}572#endif // !_LP64573574575bool os::has_allocatable_memory_limit(size_t* limit) {576struct rlimit rlim;577int getrlimit_res = getrlimit(RLIMIT_AS, &rlim);578// if there was an error when calling getrlimit, assume that there is no limitation579// on virtual memory.580bool result;581if ((getrlimit_res != 0) || (rlim.rlim_cur == RLIM_INFINITY)) {582result = false;583} else {584*limit = (size_t)rlim.rlim_cur;585result = true;586}587#ifdef _LP64588return result;589#else590// arbitrary virtual space limit for 32 bit Unices found by testing. If591// getrlimit above returned a limit, bound it with this limit. Otherwise592// directly use it.593const size_t max_virtual_limit = 3800*M;594if (result) {595*limit = MIN2(*limit, max_virtual_limit);596} else {597*limit = max_virtual_limit;598}599600// bound by actually allocatable memory. The algorithm uses two bounds, an601// upper and a lower limit. The upper limit is the current highest amount of602// memory that could not be allocated, the lower limit is the current highest603// amount of memory that could be allocated.604// The algorithm iteratively refines the result by halving the difference605// between these limits, updating either the upper limit (if that value could606// not be allocated) or the lower limit (if the that value could be allocated)607// until the difference between these limits is "small".608609// the minimum amount of memory we care about allocating.610const size_t min_allocation_size = M;611612size_t upper_limit = *limit;613614// first check a few trivial cases615if (is_allocatable(upper_limit) || (upper_limit <= min_allocation_size)) {616*limit = upper_limit;617} else if (!is_allocatable(min_allocation_size)) {618// we found that not even min_allocation_size is allocatable. Return it619// anyway. There is no point to search for a better value any more.620*limit = min_allocation_size;621} else {622// perform the binary search.623size_t lower_limit = min_allocation_size;624while ((upper_limit - lower_limit) > min_allocation_size) {625size_t temp_limit = ((upper_limit - lower_limit) / 2) + lower_limit;626temp_limit = align_down(temp_limit, min_allocation_size);627if (is_allocatable(temp_limit)) {628lower_limit = temp_limit;629} else {630upper_limit = temp_limit;631}632}633*limit = lower_limit;634}635return true;636#endif637}638639void os::dll_unload(void *lib) {640::dlclose(lib);641}642643jlong os::lseek(int fd, jlong offset, int whence) {644return (jlong) BSD_ONLY(::lseek) NOT_BSD(::lseek64)(fd, offset, whence);645}646647int os::fsync(int fd) {648return ::fsync(fd);649}650651int os::ftruncate(int fd, jlong length) {652return BSD_ONLY(::ftruncate) NOT_BSD(::ftruncate64)(fd, length);653}654655const char* os::get_current_directory(char *buf, size_t buflen) {656return getcwd(buf, buflen);657}658659FILE* os::open(int fd, const char* mode) {660return ::fdopen(fd, mode);661}662663size_t os::write(int fd, const void *buf, unsigned int nBytes) {664size_t res;665RESTARTABLE((size_t) ::write(fd, buf, (size_t) nBytes), res);666return res;667}668669ssize_t os::read_at(int fd, void *buf, unsigned int nBytes, jlong offset) {670return ::pread(fd, buf, nBytes, offset);671}672673int os::close(int fd) {674return ::close(fd);675}676677void os::flockfile(FILE* fp) {678::flockfile(fp);679}680681void os::funlockfile(FILE* fp) {682::funlockfile(fp);683}684685DIR* os::opendir(const char* dirname) {686assert(dirname != NULL, "just checking");687return ::opendir(dirname);688}689690struct dirent* os::readdir(DIR* dirp) {691assert(dirp != NULL, "just checking");692return ::readdir(dirp);693}694695int os::closedir(DIR *dirp) {696assert(dirp != NULL, "just checking");697return ::closedir(dirp);698}699700int os::socket_close(int fd) {701return ::close(fd);702}703704int os::socket(int domain, int type, int protocol) {705return ::socket(domain, type, protocol);706}707708int os::recv(int fd, char* buf, size_t nBytes, uint flags) {709RESTARTABLE_RETURN_INT(::recv(fd, buf, nBytes, flags));710}711712int os::send(int fd, char* buf, size_t nBytes, uint flags) {713RESTARTABLE_RETURN_INT(::send(fd, buf, nBytes, flags));714}715716int os::raw_send(int fd, char* buf, size_t nBytes, uint flags) {717return os::send(fd, buf, nBytes, flags);718}719720int os::connect(int fd, struct sockaddr* him, socklen_t len) {721RESTARTABLE_RETURN_INT(::connect(fd, him, len));722}723724struct hostent* os::get_host_by_name(char* name) {725return ::gethostbyname(name);726}727728void os::exit(int num) {729::exit(num);730}731732// Builds a platform dependent Agent_OnLoad_<lib_name> function name733// which is used to find statically linked in agents.734// Parameters:735// sym_name: Symbol in library we are looking for736// lib_name: Name of library to look in, NULL for shared libs.737// is_absolute_path == true if lib_name is absolute path to agent738// such as "/a/b/libL.so"739// == false if only the base name of the library is passed in740// such as "L"741char* os::build_agent_function_name(const char *sym_name, const char *lib_name,742bool is_absolute_path) {743char *agent_entry_name;744size_t len;745size_t name_len;746size_t prefix_len = strlen(JNI_LIB_PREFIX);747size_t suffix_len = strlen(JNI_LIB_SUFFIX);748const char *start;749750if (lib_name != NULL) {751name_len = strlen(lib_name);752if (is_absolute_path) {753// Need to strip path, prefix and suffix754if ((start = strrchr(lib_name, *os::file_separator())) != NULL) {755lib_name = ++start;756}757if (strlen(lib_name) <= (prefix_len + suffix_len)) {758return NULL;759}760lib_name += prefix_len;761name_len = strlen(lib_name) - suffix_len;762}763}764len = (lib_name != NULL ? name_len : 0) + strlen(sym_name) + 2;765agent_entry_name = NEW_C_HEAP_ARRAY_RETURN_NULL(char, len, mtThread);766if (agent_entry_name == NULL) {767return NULL;768}769strcpy(agent_entry_name, sym_name);770if (lib_name != NULL) {771strcat(agent_entry_name, "_");772strncat(agent_entry_name, lib_name, name_len);773}774return agent_entry_name;775}776777778void os::naked_short_nanosleep(jlong ns) {779struct timespec req;780assert(ns > -1 && ns < NANOUNITS, "Un-interruptable sleep, short time use only");781req.tv_sec = 0;782req.tv_nsec = ns;783::nanosleep(&req, NULL);784return;785}786787void os::naked_short_sleep(jlong ms) {788assert(ms < MILLIUNITS, "Un-interruptable sleep, short time use only");789os::naked_short_nanosleep(millis_to_nanos(ms));790return;791}792793char* os::Posix::describe_pthread_attr(char* buf, size_t buflen, const pthread_attr_t* attr) {794size_t stack_size = 0;795size_t guard_size = 0;796int detachstate = 0;797pthread_attr_getstacksize(attr, &stack_size);798pthread_attr_getguardsize(attr, &guard_size);799// Work around linux NPTL implementation error, see also os::create_thread() in os_linux.cpp.800LINUX_ONLY(stack_size -= guard_size);801pthread_attr_getdetachstate(attr, &detachstate);802jio_snprintf(buf, buflen, "stacksize: " SIZE_FORMAT "k, guardsize: " SIZE_FORMAT "k, %s",803stack_size / 1024, guard_size / 1024,804(detachstate == PTHREAD_CREATE_DETACHED ? "detached" : "joinable"));805return buf;806}807808char* os::Posix::realpath(const char* filename, char* outbuf, size_t outbuflen) {809810if (filename == NULL || outbuf == NULL || outbuflen < 1) {811assert(false, "os::Posix::realpath: invalid arguments.");812errno = EINVAL;813return NULL;814}815816char* result = NULL;817818// This assumes platform realpath() is implemented according to POSIX.1-2008.819// POSIX.1-2008 allows to specify NULL for the output buffer, in which case820// output buffer is dynamically allocated and must be ::free()'d by the caller.821char* p = ::realpath(filename, NULL);822if (p != NULL) {823if (strlen(p) < outbuflen) {824strcpy(outbuf, p);825result = outbuf;826} else {827errno = ENAMETOOLONG;828}829::free(p); // *not* os::free830} else {831// Fallback for platforms struggling with modern Posix standards (AIX 5.3, 6.1). If realpath832// returns EINVAL, this may indicate that realpath is not POSIX.1-2008 compatible and833// that it complains about the NULL we handed down as user buffer.834// In this case, use the user provided buffer but at least check whether realpath caused835// a memory overwrite.836if (errno == EINVAL) {837outbuf[outbuflen - 1] = '\0';838p = ::realpath(filename, outbuf);839if (p != NULL) {840guarantee(outbuf[outbuflen - 1] == '\0', "realpath buffer overwrite detected.");841result = p;842}843}844}845return result;846847}848849int os::stat(const char *path, struct stat *sbuf) {850return ::stat(path, sbuf);851}852853char * os::native_path(char *path) {854return path;855}856857bool os::same_files(const char* file1, const char* file2) {858if (file1 == nullptr && file2 == nullptr) {859return true;860}861862if (file1 == nullptr || file2 == nullptr) {863return false;864}865866if (strcmp(file1, file2) == 0) {867return true;868}869870bool is_same = false;871struct stat st1;872struct stat st2;873874if (os::stat(file1, &st1) < 0) {875return false;876}877878if (os::stat(file2, &st2) < 0) {879return false;880}881882if (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino) {883// same files884is_same = true;885}886return is_same;887}888889// Check minimum allowable stack sizes for thread creation and to initialize890// the java system classes, including StackOverflowError - depends on page891// size.892// The space needed for frames during startup is platform dependent. It893// depends on word size, platform calling conventions, C frame layout and894// interpreter/C1/C2 design decisions. Therefore this is given in a895// platform (os/cpu) dependent constant.896// To this, space for guard mechanisms is added, which depends on the897// page size which again depends on the concrete system the VM is running898// on. Space for libc guard pages is not included in this size.899jint os::Posix::set_minimum_stack_sizes() {900size_t os_min_stack_allowed = PTHREAD_STACK_MIN;901902_java_thread_min_stack_allowed = _java_thread_min_stack_allowed +903StackOverflow::stack_guard_zone_size() +904StackOverflow::stack_shadow_zone_size();905906_java_thread_min_stack_allowed = align_up(_java_thread_min_stack_allowed, vm_page_size());907_java_thread_min_stack_allowed = MAX2(_java_thread_min_stack_allowed, os_min_stack_allowed);908909size_t stack_size_in_bytes = ThreadStackSize * K;910if (stack_size_in_bytes != 0 &&911stack_size_in_bytes < _java_thread_min_stack_allowed) {912// The '-Xss' and '-XX:ThreadStackSize=N' options both set913// ThreadStackSize so we go with "Java thread stack size" instead914// of "ThreadStackSize" to be more friendly.915tty->print_cr("\nThe Java thread stack size specified is too small. "916"Specify at least " SIZE_FORMAT "k",917_java_thread_min_stack_allowed / K);918return JNI_ERR;919}920921// Make the stack size a multiple of the page size so that922// the yellow/red zones can be guarded.923JavaThread::set_stack_size_at_create(align_up(stack_size_in_bytes, vm_page_size()));924925// Reminder: a compiler thread is a Java thread.926_compiler_thread_min_stack_allowed = _compiler_thread_min_stack_allowed +927StackOverflow::stack_guard_zone_size() +928StackOverflow::stack_shadow_zone_size();929930_compiler_thread_min_stack_allowed = align_up(_compiler_thread_min_stack_allowed, vm_page_size());931_compiler_thread_min_stack_allowed = MAX2(_compiler_thread_min_stack_allowed, os_min_stack_allowed);932933stack_size_in_bytes = CompilerThreadStackSize * K;934if (stack_size_in_bytes != 0 &&935stack_size_in_bytes < _compiler_thread_min_stack_allowed) {936tty->print_cr("\nThe CompilerThreadStackSize specified is too small. "937"Specify at least " SIZE_FORMAT "k",938_compiler_thread_min_stack_allowed / K);939return JNI_ERR;940}941942_vm_internal_thread_min_stack_allowed = align_up(_vm_internal_thread_min_stack_allowed, vm_page_size());943_vm_internal_thread_min_stack_allowed = MAX2(_vm_internal_thread_min_stack_allowed, os_min_stack_allowed);944945stack_size_in_bytes = VMThreadStackSize * K;946if (stack_size_in_bytes != 0 &&947stack_size_in_bytes < _vm_internal_thread_min_stack_allowed) {948tty->print_cr("\nThe VMThreadStackSize specified is too small. "949"Specify at least " SIZE_FORMAT "k",950_vm_internal_thread_min_stack_allowed / K);951return JNI_ERR;952}953return JNI_OK;954}955956// Called when creating the thread. The minimum stack sizes have already been calculated957size_t os::Posix::get_initial_stack_size(ThreadType thr_type, size_t req_stack_size) {958size_t stack_size;959if (req_stack_size == 0) {960stack_size = default_stack_size(thr_type);961} else {962stack_size = req_stack_size;963}964965switch (thr_type) {966case os::java_thread:967// Java threads use ThreadStackSize which default value can be968// changed with the flag -Xss969if (req_stack_size == 0 && JavaThread::stack_size_at_create() > 0) {970// no requested size and we have a more specific default value971stack_size = JavaThread::stack_size_at_create();972}973stack_size = MAX2(stack_size,974_java_thread_min_stack_allowed);975break;976case os::compiler_thread:977if (req_stack_size == 0 && CompilerThreadStackSize > 0) {978// no requested size and we have a more specific default value979stack_size = (size_t)(CompilerThreadStackSize * K);980}981stack_size = MAX2(stack_size,982_compiler_thread_min_stack_allowed);983break;984case os::vm_thread:985case os::pgc_thread:986case os::cgc_thread:987case os::watcher_thread:988default: // presume the unknown thr_type is a VM internal989if (req_stack_size == 0 && VMThreadStackSize > 0) {990// no requested size and we have a more specific default value991stack_size = (size_t)(VMThreadStackSize * K);992}993994stack_size = MAX2(stack_size,995_vm_internal_thread_min_stack_allowed);996break;997}998999// pthread_attr_setstacksize() may require that the size be rounded up to the OS page size.1000// Be careful not to round up to 0. Align down in that case.1001if (stack_size <= SIZE_MAX - vm_page_size()) {1002stack_size = align_up(stack_size, vm_page_size());1003} else {1004stack_size = align_down(stack_size, vm_page_size());1005}10061007return stack_size;1008}10091010#ifndef ZERO1011#ifndef ARM1012static bool get_frame_at_stack_banging_point(JavaThread* thread, address pc, const void* ucVoid, frame* fr) {1013if (Interpreter::contains(pc)) {1014// interpreter performs stack banging after the fixed frame header has1015// been generated while the compilers perform it before. To maintain1016// semantic consistency between interpreted and compiled frames, the1017// method returns the Java sender of the current frame.1018*fr = os::fetch_frame_from_context(ucVoid);1019if (!fr->is_first_java_frame()) {1020// get_frame_at_stack_banging_point() is only called when we1021// have well defined stacks so java_sender() calls do not need1022// to assert safe_for_sender() first.1023*fr = fr->java_sender();1024}1025} else {1026// more complex code with compiled code1027assert(!Interpreter::contains(pc), "Interpreted methods should have been handled above");1028CodeBlob* cb = CodeCache::find_blob(pc);1029if (cb == NULL || !cb->is_nmethod() || cb->is_frame_complete_at(pc)) {1030// Not sure where the pc points to, fallback to default1031// stack overflow handling1032return false;1033} else {1034// in compiled code, the stack banging is performed just after the return pc1035// has been pushed on the stack1036*fr = os::fetch_compiled_frame_from_context(ucVoid);1037if (!fr->is_java_frame()) {1038assert(!fr->is_first_frame(), "Safety check");1039// See java_sender() comment above.1040*fr = fr->java_sender();1041}1042}1043}1044assert(fr->is_java_frame(), "Safety check");1045return true;1046}1047#endif // ARM10481049// This return true if the signal handler should just continue, ie. return after calling this1050bool os::Posix::handle_stack_overflow(JavaThread* thread, address addr, address pc,1051const void* ucVoid, address* stub) {1052// stack overflow1053StackOverflow* overflow_state = thread->stack_overflow_state();1054if (overflow_state->in_stack_yellow_reserved_zone(addr)) {1055if (thread->thread_state() == _thread_in_Java) {1056#ifndef ARM1057// arm32 doesn't have this1058if (overflow_state->in_stack_reserved_zone(addr)) {1059frame fr;1060if (get_frame_at_stack_banging_point(thread, pc, ucVoid, &fr)) {1061assert(fr.is_java_frame(), "Must be a Java frame");1062frame activation =1063SharedRuntime::look_for_reserved_stack_annotated_method(thread, fr);1064if (activation.sp() != NULL) {1065overflow_state->disable_stack_reserved_zone();1066if (activation.is_interpreted_frame()) {1067overflow_state->set_reserved_stack_activation((address)(activation.fp()1068// Some platforms use frame pointers for interpreter frames, others use initial sp.1069#if !defined(PPC64) && !defined(S390)1070+ frame::interpreter_frame_initial_sp_offset1071#endif1072));1073} else {1074overflow_state->set_reserved_stack_activation((address)activation.unextended_sp());1075}1076return true; // just continue1077}1078}1079}1080#endif // ARM1081// Throw a stack overflow exception. Guard pages will be reenabled1082// while unwinding the stack.1083overflow_state->disable_stack_yellow_reserved_zone();1084*stub = SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::STACK_OVERFLOW);1085} else {1086// Thread was in the vm or native code. Return and try to finish.1087overflow_state->disable_stack_yellow_reserved_zone();1088return true; // just continue1089}1090} else if (overflow_state->in_stack_red_zone(addr)) {1091// Fatal red zone violation. Disable the guard pages and fall through1092// to handle_unexpected_exception way down below.1093overflow_state->disable_stack_red_zone();1094tty->print_raw_cr("An irrecoverable stack overflow has occurred.");10951096// This is a likely cause, but hard to verify. Let's just print1097// it as a hint.1098tty->print_raw_cr("Please check if any of your loaded .so files has "1099"enabled executable stack (see man page execstack(8))");11001101} else {1102#if !defined(AIX) && !defined(__APPLE__)1103// bsd and aix don't have this11041105// Accessing stack address below sp may cause SEGV if current1106// thread has MAP_GROWSDOWN stack. This should only happen when1107// current thread was created by user code with MAP_GROWSDOWN flag1108// and then attached to VM. See notes in os_linux.cpp.1109if (thread->osthread()->expanding_stack() == 0) {1110thread->osthread()->set_expanding_stack();1111if (os::Linux::manually_expand_stack(thread, addr)) {1112thread->osthread()->clear_expanding_stack();1113return true; // just continue1114}1115thread->osthread()->clear_expanding_stack();1116} else {1117fatal("recursive segv. expanding stack.");1118}1119#else1120tty->print_raw_cr("SIGSEGV happened inside stack but outside yellow and red zone.");1121#endif // AIX or BSD1122}1123return false;1124}1125#endif // ZERO11261127bool os::Posix::is_root(uid_t uid){1128return ROOT_UID == uid;1129}11301131bool os::Posix::matches_effective_uid_or_root(uid_t uid) {1132return is_root(uid) || geteuid() == uid;1133}11341135bool os::Posix::matches_effective_uid_and_gid_or_root(uid_t uid, gid_t gid) {1136return is_root(uid) || (geteuid() == uid && getegid() == gid);1137}11381139Thread* os::ThreadCrashProtection::_protected_thread = NULL;1140os::ThreadCrashProtection* os::ThreadCrashProtection::_crash_protection = NULL;11411142os::ThreadCrashProtection::ThreadCrashProtection() {1143_protected_thread = Thread::current();1144assert(_protected_thread->is_JfrSampler_thread(), "should be JFRSampler");1145}11461147/*1148* See the caveats for this class in os_posix.hpp1149* Protects the callback call so that SIGSEGV / SIGBUS jumps back into this1150* method and returns false. If none of the signals are raised, returns true.1151* The callback is supposed to provide the method that should be protected.1152*/1153bool os::ThreadCrashProtection::call(os::CrashProtectionCallback& cb) {1154sigset_t saved_sig_mask;11551156// we cannot rely on sigsetjmp/siglongjmp to save/restore the signal mask1157// since on at least some systems (OS X) siglongjmp will restore the mask1158// for the process, not the thread1159pthread_sigmask(0, NULL, &saved_sig_mask);1160if (sigsetjmp(_jmpbuf, 0) == 0) {1161// make sure we can see in the signal handler that we have crash protection1162// installed1163_crash_protection = this;1164cb.call();1165// and clear the crash protection1166_crash_protection = NULL;1167_protected_thread = NULL;1168return true;1169}1170// this happens when we siglongjmp() back1171pthread_sigmask(SIG_SETMASK, &saved_sig_mask, NULL);1172_crash_protection = NULL;1173_protected_thread = NULL;1174return false;1175}11761177void os::ThreadCrashProtection::restore() {1178assert(_crash_protection != NULL, "must have crash protection");1179siglongjmp(_jmpbuf, 1);1180}11811182void os::ThreadCrashProtection::check_crash_protection(int sig,1183Thread* thread) {11841185if (thread != NULL &&1186thread == _protected_thread &&1187_crash_protection != NULL) {11881189if (sig == SIGSEGV || sig == SIGBUS) {1190_crash_protection->restore();1191}1192}1193}11941195// Shared clock/time and other supporting routines for pthread_mutex/cond1196// initialization. This is enabled on Solaris but only some of the clock/time1197// functionality is actually used there.11981199// Shared condattr object for use with relative timed-waits. Will be associated1200// with CLOCK_MONOTONIC if available to avoid issues with time-of-day changes,1201// but otherwise whatever default is used by the platform - generally the1202// time-of-day clock.1203static pthread_condattr_t _condAttr[1];12041205// Shared mutexattr to explicitly set the type to PTHREAD_MUTEX_NORMAL as not1206// all systems (e.g. FreeBSD) map the default to "normal".1207static pthread_mutexattr_t _mutexAttr[1];12081209// common basic initialization that is always supported1210static void pthread_init_common(void) {1211int status;1212if ((status = pthread_condattr_init(_condAttr)) != 0) {1213fatal("pthread_condattr_init: %s", os::strerror(status));1214}1215if ((status = pthread_mutexattr_init(_mutexAttr)) != 0) {1216fatal("pthread_mutexattr_init: %s", os::strerror(status));1217}1218if ((status = pthread_mutexattr_settype(_mutexAttr, PTHREAD_MUTEX_NORMAL)) != 0) {1219fatal("pthread_mutexattr_settype: %s", os::strerror(status));1220}1221os::PlatformMutex::init();1222}12231224static int (*_pthread_condattr_setclock)(pthread_condattr_t *, clockid_t) = NULL;12251226static bool _use_clock_monotonic_condattr = false;12271228// Determine what POSIX API's are present and do appropriate1229// configuration.1230void os::Posix::init(void) {12311232// NOTE: no logging available when this is called. Put logging1233// statements in init_2().12341235// Check for pthread_condattr_setclock support.12361237// libpthread is already loaded.1238int (*condattr_setclock_func)(pthread_condattr_t*, clockid_t) =1239(int (*)(pthread_condattr_t*, clockid_t))dlsym(RTLD_DEFAULT,1240"pthread_condattr_setclock");1241if (condattr_setclock_func != NULL) {1242_pthread_condattr_setclock = condattr_setclock_func;1243}12441245// Now do general initialization.12461247pthread_init_common();12481249int status;1250if (_pthread_condattr_setclock != NULL) {1251if ((status = _pthread_condattr_setclock(_condAttr, CLOCK_MONOTONIC)) != 0) {1252if (status == EINVAL) {1253_use_clock_monotonic_condattr = false;1254warning("Unable to use monotonic clock with relative timed-waits" \1255" - changes to the time-of-day clock may have adverse affects");1256} else {1257fatal("pthread_condattr_setclock: %s", os::strerror(status));1258}1259} else {1260_use_clock_monotonic_condattr = true;1261}1262}1263}12641265void os::Posix::init_2(void) {1266log_info(os)("Use of CLOCK_MONOTONIC is supported");1267log_info(os)("Use of pthread_condattr_setclock is%s supported",1268(_pthread_condattr_setclock != NULL ? "" : " not"));1269log_info(os)("Relative timed-wait using pthread_cond_timedwait is associated with %s",1270_use_clock_monotonic_condattr ? "CLOCK_MONOTONIC" : "the default clock");1271}12721273// Utility to convert the given timeout to an absolute timespec1274// (based on the appropriate clock) to use with pthread_cond_timewait,1275// and sem_timedwait().1276// The clock queried here must be the clock used to manage the1277// timeout of the condition variable or semaphore.1278//1279// The passed in timeout value is either a relative time in nanoseconds1280// or an absolute time in milliseconds. A relative timeout will be1281// associated with CLOCK_MONOTONIC if available, unless the real-time clock1282// is explicitly requested; otherwise, or if absolute,1283// the default time-of-day clock will be used.12841285// Given time is a 64-bit value and the time_t used in the timespec is1286// sometimes a signed-32-bit value we have to watch for overflow if times1287// way in the future are given. Further on Solaris versions1288// prior to 10 there is a restriction (see cond_timedwait) that the specified1289// number of seconds, in abstime, is less than current_time + 100000000.1290// As it will be over 20 years before "now + 100000000" will overflow we can1291// ignore overflow and just impose a hard-limit on seconds using the value1292// of "now + 100000000". This places a limit on the timeout of about 3.171293// years from "now".1294//1295#define MAX_SECS 10000000012961297// Calculate a new absolute time that is "timeout" nanoseconds from "now".1298// "unit" indicates the unit of "now_part_sec" (may be nanos or micros depending1299// on which clock API is being used).1300static void calc_rel_time(timespec* abstime, jlong timeout, jlong now_sec,1301jlong now_part_sec, jlong unit) {1302time_t max_secs = now_sec + MAX_SECS;13031304jlong seconds = timeout / NANOUNITS;1305timeout %= NANOUNITS; // remaining nanos13061307if (seconds >= MAX_SECS) {1308// More seconds than we can add, so pin to max_secs.1309abstime->tv_sec = max_secs;1310abstime->tv_nsec = 0;1311} else {1312abstime->tv_sec = now_sec + seconds;1313long nanos = (now_part_sec * (NANOUNITS / unit)) + timeout;1314if (nanos >= NANOUNITS) { // overflow1315abstime->tv_sec += 1;1316nanos -= NANOUNITS;1317}1318abstime->tv_nsec = nanos;1319}1320}13211322// Unpack the given deadline in milliseconds since the epoch, into the given timespec.1323// The current time in seconds is also passed in to enforce an upper bound as discussed above.1324static void unpack_abs_time(timespec* abstime, jlong deadline, jlong now_sec) {1325time_t max_secs = now_sec + MAX_SECS;13261327jlong seconds = deadline / MILLIUNITS;1328jlong millis = deadline % MILLIUNITS;13291330if (seconds >= max_secs) {1331// Absolute seconds exceeds allowed max, so pin to max_secs.1332abstime->tv_sec = max_secs;1333abstime->tv_nsec = 0;1334} else {1335abstime->tv_sec = seconds;1336abstime->tv_nsec = millis_to_nanos(millis);1337}1338}13391340static jlong millis_to_nanos_bounded(jlong millis) {1341// We have to watch for overflow when converting millis to nanos,1342// but if millis is that large then we will end up limiting to1343// MAX_SECS anyway, so just do that here.1344if (millis / MILLIUNITS > MAX_SECS) {1345millis = jlong(MAX_SECS) * MILLIUNITS;1346}1347return millis_to_nanos(millis);1348}13491350static void to_abstime(timespec* abstime, jlong timeout,1351bool isAbsolute, bool isRealtime) {1352DEBUG_ONLY(int max_secs = MAX_SECS;)13531354if (timeout < 0) {1355timeout = 0;1356}13571358clockid_t clock = CLOCK_MONOTONIC;1359if (isAbsolute || (!_use_clock_monotonic_condattr || isRealtime)) {1360clock = CLOCK_REALTIME;1361}13621363struct timespec now;1364int status = clock_gettime(clock, &now);1365assert(status == 0, "clock_gettime error: %s", os::strerror(errno));13661367if (!isAbsolute) {1368calc_rel_time(abstime, timeout, now.tv_sec, now.tv_nsec, NANOUNITS);1369} else {1370unpack_abs_time(abstime, timeout, now.tv_sec);1371}1372DEBUG_ONLY(max_secs += now.tv_sec;)13731374assert(abstime->tv_sec >= 0, "tv_sec < 0");1375assert(abstime->tv_sec <= max_secs, "tv_sec > max_secs");1376assert(abstime->tv_nsec >= 0, "tv_nsec < 0");1377assert(abstime->tv_nsec < NANOUNITS, "tv_nsec >= NANOUNITS");1378}13791380// Create an absolute time 'millis' milliseconds in the future, using the1381// real-time (time-of-day) clock. Used by PosixSemaphore.1382void os::Posix::to_RTC_abstime(timespec* abstime, int64_t millis) {1383to_abstime(abstime, millis_to_nanos_bounded(millis),1384false /* not absolute */,1385true /* use real-time clock */);1386}13871388// Common (partly) shared time functions13891390jlong os::javaTimeMillis() {1391struct timespec ts;1392int status = clock_gettime(CLOCK_REALTIME, &ts);1393assert(status == 0, "clock_gettime error: %s", os::strerror(errno));1394return jlong(ts.tv_sec) * MILLIUNITS +1395jlong(ts.tv_nsec) / NANOUNITS_PER_MILLIUNIT;1396}13971398void os::javaTimeSystemUTC(jlong &seconds, jlong &nanos) {1399struct timespec ts;1400int status = clock_gettime(CLOCK_REALTIME, &ts);1401assert(status == 0, "clock_gettime error: %s", os::strerror(errno));1402seconds = jlong(ts.tv_sec);1403nanos = jlong(ts.tv_nsec);1404}14051406// macOS and AIX have platform specific implementations for javaTimeNanos()1407// using native clock/timer access APIs. These have historically worked well1408// for those platforms, but it may be possible for them to switch to the1409// generic clock_gettime mechanism in the future.1410#if !defined(__APPLE__) && !defined(AIX)14111412jlong os::javaTimeNanos() {1413struct timespec tp;1414int status = clock_gettime(CLOCK_MONOTONIC, &tp);1415assert(status == 0, "clock_gettime error: %s", os::strerror(errno));1416jlong result = jlong(tp.tv_sec) * NANOSECS_PER_SEC + jlong(tp.tv_nsec);1417return result;1418}14191420// for timer info max values which include all bits1421#define ALL_64_BITS CONST64(0xFFFFFFFFFFFFFFFF)14221423void os::javaTimeNanos_info(jvmtiTimerInfo *info_ptr) {1424// CLOCK_MONOTONIC - amount of time since some arbitrary point in the past1425info_ptr->max_value = ALL_64_BITS;1426info_ptr->may_skip_backward = false; // not subject to resetting or drifting1427info_ptr->may_skip_forward = false; // not subject to resetting or drifting1428info_ptr->kind = JVMTI_TIMER_ELAPSED; // elapsed not CPU time1429}14301431#endif // ! APPLE && !AIX14321433// Shared pthread_mutex/cond based PlatformEvent implementation.1434// Not currently usable by Solaris.143514361437// PlatformEvent1438//1439// Assumption:1440// Only one parker can exist on an event, which is why we allocate1441// them per-thread. Multiple unparkers can coexist.1442//1443// _event serves as a restricted-range semaphore.1444// -1 : thread is blocked, i.e. there is a waiter1445// 0 : neutral: thread is running or ready,1446// could have been signaled after a wait started1447// 1 : signaled - thread is running or ready1448//1449// Having three states allows for some detection of bad usage - see1450// comments on unpark().14511452os::PlatformEvent::PlatformEvent() {1453int status = pthread_cond_init(_cond, _condAttr);1454assert_status(status == 0, status, "cond_init");1455status = pthread_mutex_init(_mutex, _mutexAttr);1456assert_status(status == 0, status, "mutex_init");1457_event = 0;1458_nParked = 0;1459}14601461void os::PlatformEvent::park() { // AKA "down()"1462// Transitions for _event:1463// -1 => -1 : illegal1464// 1 => 0 : pass - return immediately1465// 0 => -1 : block; then set _event to 0 before returning14661467// Invariant: Only the thread associated with the PlatformEvent1468// may call park().1469assert(_nParked == 0, "invariant");14701471int v;14721473// atomically decrement _event1474for (;;) {1475v = _event;1476if (Atomic::cmpxchg(&_event, v, v - 1) == v) break;1477}1478guarantee(v >= 0, "invariant");14791480if (v == 0) { // Do this the hard way by blocking ...1481int status = pthread_mutex_lock(_mutex);1482assert_status(status == 0, status, "mutex_lock");1483guarantee(_nParked == 0, "invariant");1484++_nParked;1485while (_event < 0) {1486// OS-level "spurious wakeups" are ignored1487status = pthread_cond_wait(_cond, _mutex);1488assert_status(status == 0 MACOS_ONLY(|| status == ETIMEDOUT),1489status, "cond_wait");1490}1491--_nParked;14921493_event = 0;1494status = pthread_mutex_unlock(_mutex);1495assert_status(status == 0, status, "mutex_unlock");1496// Paranoia to ensure our locked and lock-free paths interact1497// correctly with each other.1498OrderAccess::fence();1499}1500guarantee(_event >= 0, "invariant");1501}15021503int os::PlatformEvent::park(jlong millis) {1504// Transitions for _event:1505// -1 => -1 : illegal1506// 1 => 0 : pass - return immediately1507// 0 => -1 : block; then set _event to 0 before returning15081509// Invariant: Only the thread associated with the Event/PlatformEvent1510// may call park().1511assert(_nParked == 0, "invariant");15121513int v;1514// atomically decrement _event1515for (;;) {1516v = _event;1517if (Atomic::cmpxchg(&_event, v, v - 1) == v) break;1518}1519guarantee(v >= 0, "invariant");15201521if (v == 0) { // Do this the hard way by blocking ...1522struct timespec abst;1523to_abstime(&abst, millis_to_nanos_bounded(millis), false, false);15241525int ret = OS_TIMEOUT;1526int status = pthread_mutex_lock(_mutex);1527assert_status(status == 0, status, "mutex_lock");1528guarantee(_nParked == 0, "invariant");1529++_nParked;15301531while (_event < 0) {1532status = pthread_cond_timedwait(_cond, _mutex, &abst);1533assert_status(status == 0 || status == ETIMEDOUT,1534status, "cond_timedwait");1535// OS-level "spurious wakeups" are ignored unless the archaic1536// FilterSpuriousWakeups is set false. That flag should be obsoleted.1537if (!FilterSpuriousWakeups) break;1538if (status == ETIMEDOUT) break;1539}1540--_nParked;15411542if (_event >= 0) {1543ret = OS_OK;1544}15451546_event = 0;1547status = pthread_mutex_unlock(_mutex);1548assert_status(status == 0, status, "mutex_unlock");1549// Paranoia to ensure our locked and lock-free paths interact1550// correctly with each other.1551OrderAccess::fence();1552return ret;1553}1554return OS_OK;1555}15561557void os::PlatformEvent::unpark() {1558// Transitions for _event:1559// 0 => 1 : just return1560// 1 => 1 : just return1561// -1 => either 0 or 1; must signal target thread1562// That is, we can safely transition _event from -1 to either1563// 0 or 1.1564// See also: "Semaphores in Plan 9" by Mullender & Cox1565//1566// Note: Forcing a transition from "-1" to "1" on an unpark() means1567// that it will take two back-to-back park() calls for the owning1568// thread to block. This has the benefit of forcing a spurious return1569// from the first park() call after an unpark() call which will help1570// shake out uses of park() and unpark() without checking state conditions1571// properly. This spurious return doesn't manifest itself in any user code1572// but only in the correctly written condition checking loops of ObjectMonitor,1573// Mutex/Monitor, and JavaThread::sleep15741575if (Atomic::xchg(&_event, 1) >= 0) return;15761577int status = pthread_mutex_lock(_mutex);1578assert_status(status == 0, status, "mutex_lock");1579int anyWaiters = _nParked;1580assert(anyWaiters == 0 || anyWaiters == 1, "invariant");1581status = pthread_mutex_unlock(_mutex);1582assert_status(status == 0, status, "mutex_unlock");15831584// Note that we signal() *after* dropping the lock for "immortal" Events.1585// This is safe and avoids a common class of futile wakeups. In rare1586// circumstances this can cause a thread to return prematurely from1587// cond_{timed}wait() but the spurious wakeup is benign and the victim1588// will simply re-test the condition and re-park itself.1589// This provides particular benefit if the underlying platform does not1590// provide wait morphing.15911592if (anyWaiters != 0) {1593status = pthread_cond_signal(_cond);1594assert_status(status == 0, status, "cond_signal");1595}1596}15971598// JSR166 support15991600os::PlatformParker::PlatformParker() : _counter(0), _cur_index(-1) {1601int status = pthread_cond_init(&_cond[REL_INDEX], _condAttr);1602assert_status(status == 0, status, "cond_init rel");1603status = pthread_cond_init(&_cond[ABS_INDEX], NULL);1604assert_status(status == 0, status, "cond_init abs");1605status = pthread_mutex_init(_mutex, _mutexAttr);1606assert_status(status == 0, status, "mutex_init");1607}16081609os::PlatformParker::~PlatformParker() {1610int status = pthread_cond_destroy(&_cond[REL_INDEX]);1611assert_status(status == 0, status, "cond_destroy rel");1612status = pthread_cond_destroy(&_cond[ABS_INDEX]);1613assert_status(status == 0, status, "cond_destroy abs");1614status = pthread_mutex_destroy(_mutex);1615assert_status(status == 0, status, "mutex_destroy");1616}16171618// Parker::park decrements count if > 0, else does a condvar wait. Unpark1619// sets count to 1 and signals condvar. Only one thread ever waits1620// on the condvar. Contention seen when trying to park implies that someone1621// is unparking you, so don't wait. And spurious returns are fine, so there1622// is no need to track notifications.16231624void Parker::park(bool isAbsolute, jlong time) {16251626// Optional fast-path check:1627// Return immediately if a permit is available.1628// We depend on Atomic::xchg() having full barrier semantics1629// since we are doing a lock-free update to _counter.1630if (Atomic::xchg(&_counter, 0) > 0) return;16311632JavaThread *jt = JavaThread::current();16331634// Optional optimization -- avoid state transitions if there's1635// an interrupt pending.1636if (jt->is_interrupted(false)) {1637return;1638}16391640// Next, demultiplex/decode time arguments1641struct timespec absTime;1642if (time < 0 || (isAbsolute && time == 0)) { // don't wait at all1643return;1644}1645if (time > 0) {1646to_abstime(&absTime, time, isAbsolute, false);1647}16481649// Enter safepoint region1650// Beware of deadlocks such as 6317397.1651// The per-thread Parker:: mutex is a classic leaf-lock.1652// In particular a thread must never block on the Threads_lock while1653// holding the Parker:: mutex. If safepoints are pending both the1654// the ThreadBlockInVM() CTOR and DTOR may grab Threads_lock.1655ThreadBlockInVM tbivm(jt);16561657// Can't access interrupt state now that we are _thread_blocked. If we've1658// been interrupted since we checked above then _counter will be > 0.16591660// Don't wait if cannot get lock since interference arises from1661// unparking.1662if (pthread_mutex_trylock(_mutex) != 0) {1663return;1664}16651666int status;1667if (_counter > 0) { // no wait needed1668_counter = 0;1669status = pthread_mutex_unlock(_mutex);1670assert_status(status == 0, status, "invariant");1671// Paranoia to ensure our locked and lock-free paths interact1672// correctly with each other and Java-level accesses.1673OrderAccess::fence();1674return;1675}16761677OSThreadWaitState osts(jt->osthread(), false /* not Object.wait() */);16781679assert(_cur_index == -1, "invariant");1680if (time == 0) {1681_cur_index = REL_INDEX; // arbitrary choice when not timed1682status = pthread_cond_wait(&_cond[_cur_index], _mutex);1683assert_status(status == 0 MACOS_ONLY(|| status == ETIMEDOUT),1684status, "cond_wait");1685}1686else {1687_cur_index = isAbsolute ? ABS_INDEX : REL_INDEX;1688status = pthread_cond_timedwait(&_cond[_cur_index], _mutex, &absTime);1689assert_status(status == 0 || status == ETIMEDOUT,1690status, "cond_timedwait");1691}1692_cur_index = -1;16931694_counter = 0;1695status = pthread_mutex_unlock(_mutex);1696assert_status(status == 0, status, "invariant");1697// Paranoia to ensure our locked and lock-free paths interact1698// correctly with each other and Java-level accesses.1699OrderAccess::fence();1700}17011702void Parker::unpark() {1703int status = pthread_mutex_lock(_mutex);1704assert_status(status == 0, status, "invariant");1705const int s = _counter;1706_counter = 1;1707// must capture correct index before unlocking1708int index = _cur_index;1709status = pthread_mutex_unlock(_mutex);1710assert_status(status == 0, status, "invariant");17111712// Note that we signal() *after* dropping the lock for "immortal" Events.1713// This is safe and avoids a common class of futile wakeups. In rare1714// circumstances this can cause a thread to return prematurely from1715// cond_{timed}wait() but the spurious wakeup is benign and the victim1716// will simply re-test the condition and re-park itself.1717// This provides particular benefit if the underlying platform does not1718// provide wait morphing.17191720if (s < 1 && index != -1) {1721// thread is definitely parked1722status = pthread_cond_signal(&_cond[index]);1723assert_status(status == 0, status, "invariant");1724}1725}17261727// Platform Mutex/Monitor implementation17281729#if PLATFORM_MONITOR_IMPL_INDIRECT17301731os::PlatformMutex::Mutex::Mutex() : _next(NULL) {1732int status = pthread_mutex_init(&_mutex, _mutexAttr);1733assert_status(status == 0, status, "mutex_init");1734}17351736os::PlatformMutex::Mutex::~Mutex() {1737int status = pthread_mutex_destroy(&_mutex);1738assert_status(status == 0, status, "mutex_destroy");1739}17401741pthread_mutex_t os::PlatformMutex::_freelist_lock;1742os::PlatformMutex::Mutex* os::PlatformMutex::_mutex_freelist = NULL;17431744void os::PlatformMutex::init() {1745int status = pthread_mutex_init(&_freelist_lock, _mutexAttr);1746assert_status(status == 0, status, "freelist lock init");1747}17481749struct os::PlatformMutex::WithFreeListLocked : public StackObj {1750WithFreeListLocked() {1751int status = pthread_mutex_lock(&_freelist_lock);1752assert_status(status == 0, status, "freelist lock");1753}17541755~WithFreeListLocked() {1756int status = pthread_mutex_unlock(&_freelist_lock);1757assert_status(status == 0, status, "freelist unlock");1758}1759};17601761os::PlatformMutex::PlatformMutex() {1762{1763WithFreeListLocked wfl;1764_impl = _mutex_freelist;1765if (_impl != NULL) {1766_mutex_freelist = _impl->_next;1767_impl->_next = NULL;1768return;1769}1770}1771_impl = new Mutex();1772}17731774os::PlatformMutex::~PlatformMutex() {1775WithFreeListLocked wfl;1776assert(_impl->_next == NULL, "invariant");1777_impl->_next = _mutex_freelist;1778_mutex_freelist = _impl;1779}17801781os::PlatformMonitor::Cond::Cond() : _next(NULL) {1782int status = pthread_cond_init(&_cond, _condAttr);1783assert_status(status == 0, status, "cond_init");1784}17851786os::PlatformMonitor::Cond::~Cond() {1787int status = pthread_cond_destroy(&_cond);1788assert_status(status == 0, status, "cond_destroy");1789}17901791os::PlatformMonitor::Cond* os::PlatformMonitor::_cond_freelist = NULL;17921793os::PlatformMonitor::PlatformMonitor() {1794{1795WithFreeListLocked wfl;1796_impl = _cond_freelist;1797if (_impl != NULL) {1798_cond_freelist = _impl->_next;1799_impl->_next = NULL;1800return;1801}1802}1803_impl = new Cond();1804}18051806os::PlatformMonitor::~PlatformMonitor() {1807WithFreeListLocked wfl;1808assert(_impl->_next == NULL, "invariant");1809_impl->_next = _cond_freelist;1810_cond_freelist = _impl;1811}18121813#else18141815os::PlatformMutex::PlatformMutex() {1816int status = pthread_mutex_init(&_mutex, _mutexAttr);1817assert_status(status == 0, status, "mutex_init");1818}18191820os::PlatformMutex::~PlatformMutex() {1821int status = pthread_mutex_destroy(&_mutex);1822assert_status(status == 0, status, "mutex_destroy");1823}18241825os::PlatformMonitor::PlatformMonitor() {1826int status = pthread_cond_init(&_cond, _condAttr);1827assert_status(status == 0, status, "cond_init");1828}18291830os::PlatformMonitor::~PlatformMonitor() {1831int status = pthread_cond_destroy(&_cond);1832assert_status(status == 0, status, "cond_destroy");1833}18341835#endif // PLATFORM_MONITOR_IMPL_INDIRECT18361837// Must already be locked1838int os::PlatformMonitor::wait(jlong millis) {1839assert(millis >= 0, "negative timeout");1840if (millis > 0) {1841struct timespec abst;1842// We have to watch for overflow when converting millis to nanos,1843// but if millis is that large then we will end up limiting to1844// MAX_SECS anyway, so just do that here.1845if (millis / MILLIUNITS > MAX_SECS) {1846millis = jlong(MAX_SECS) * MILLIUNITS;1847}1848to_abstime(&abst, millis_to_nanos(millis), false, false);18491850int ret = OS_TIMEOUT;1851int status = pthread_cond_timedwait(cond(), mutex(), &abst);1852assert_status(status == 0 || status == ETIMEDOUT,1853status, "cond_timedwait");1854if (status == 0) {1855ret = OS_OK;1856}1857return ret;1858} else {1859int status = pthread_cond_wait(cond(), mutex());1860assert_status(status == 0 MACOS_ONLY(|| status == ETIMEDOUT),1861status, "cond_wait");1862return OS_OK;1863}1864}18651866// Darwin has no "environ" in a dynamic library.1867#ifdef __APPLE__1868#define environ (*_NSGetEnviron())1869#else1870extern char** environ;1871#endif18721873char** os::get_environ() { return environ; }18741875// Run the specified command in a separate process. Return its exit value,1876// or -1 on failure (e.g. can't fork a new process).1877// Notes: -Unlike system(), this function can be called from signal handler. It1878// doesn't block SIGINT et al.1879// -this function is unsafe to use in non-error situations, mainly1880// because the child process will inherit all parent descriptors.1881int os::fork_and_exec(const char* cmd, bool prefer_vfork) {1882const char * argv[4] = {"sh", "-c", cmd, NULL};18831884pid_t pid ;18851886char** env = os::get_environ();18871888// Use always vfork on AIX, since its safe and helps with analyzing OOM situations.1889// Otherwise leave it up to the caller.1890AIX_ONLY(prefer_vfork = true;)1891pid = prefer_vfork ? ::vfork() : ::fork();18921893if (pid < 0) {1894// fork failed1895return -1;18961897} else if (pid == 0) {1898// child process18991900::execve("/bin/sh", (char* const*)argv, env);19011902// execve failed1903::_exit(-1);19041905} else {1906// copied from J2SE ..._waitForProcessExit() in UNIXProcess_md.c; we don't1907// care about the actual exit code, for now.19081909int status;19101911// Wait for the child process to exit. This returns immediately if1912// the child has already exited. */1913while (::waitpid(pid, &status, 0) < 0) {1914switch (errno) {1915case ECHILD: return 0;1916case EINTR: break;1917default: return -1;1918}1919}19201921if (WIFEXITED(status)) {1922// The child exited normally; get its exit code.1923return WEXITSTATUS(status);1924} else if (WIFSIGNALED(status)) {1925// The child exited because of a signal1926// The best value to return is 0x80 + signal number,1927// because that is what all Unix shells do, and because1928// it allows callers to distinguish between process exit and1929// process death by signal.1930return 0x80 + WTERMSIG(status);1931} else {1932// Unknown exit code; pass it through1933return status;1934}1935}1936}19371938////////////////////////////////////////////////////////////////////////////////1939// runtime exit support19401941// Note: os::shutdown() might be called very early during initialization, or1942// called from signal handler. Before adding something to os::shutdown(), make1943// sure it is async-safe and can handle partially initialized VM.1944void os::shutdown() {19451946// allow PerfMemory to attempt cleanup of any persistent resources1947perfMemory_exit();19481949// needs to remove object in file system1950AttachListener::abort();19511952// flush buffered output, finish log files1953ostream_abort();19541955// Check for abort hook1956abort_hook_t abort_hook = Arguments::abort_hook();1957if (abort_hook != NULL) {1958abort_hook();1959}19601961}19621963// Note: os::abort() might be called very early during initialization, or1964// called from signal handler. Before adding something to os::abort(), make1965// sure it is async-safe and can handle partially initialized VM.1966// Also note we can abort while other threads continue to run, so we can1967// easily trigger secondary faults in those threads. To reduce the likelihood1968// of that we use _exit rather than exit, so that no atexit hooks get run.1969// But note that os::shutdown() could also trigger secondary faults.1970void os::abort(bool dump_core, void* siginfo, const void* context) {1971os::shutdown();1972if (dump_core) {1973LINUX_ONLY(if (DumpPrivateMappingsInCore) ClassLoader::close_jrt_image();)1974::abort(); // dump core1975}1976::_exit(1);1977}19781979// Die immediately, no exit hook, no abort hook, no cleanup.1980// Dump a core file, if possible, for debugging.1981void os::die() {1982if (TestUnresponsiveErrorHandler && !CreateCoredumpOnCrash) {1983// For TimeoutInErrorHandlingTest.java, we just kill the VM1984// and don't take the time to generate a core file.1985os::signal_raise(SIGKILL);1986} else {1987::abort();1988}1989}199019911992