Skip to content

Commit

Permalink
8324578: [BACKOUT] [IMPROVE] OPEN_MAX is no longer the max limit on m…
Browse files Browse the repository at this point in the history
…acOS >= 10.6 for RLIMIT_NOFILE

Reviewed-by: darcy, kvn
  • Loading branch information
David Holmes committed Jan 24, 2024
1 parent c17059d commit 6d2f640
Showing 1 changed file with 20 additions and 29 deletions.
49 changes: 20 additions & 29 deletions src/hotspot/os/bsd/os_bsd.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1999, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1999, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -2008,15 +2008,15 @@ void os::init(void) {

Bsd::initialize_system_info();

// _main_thread points to the thread that created/loaded the JVM
// _main_thread points to the thread that created/loaded the JVM.
Bsd::_main_thread = pthread_self();

Bsd::clock_init();

os::Posix::init();
}

// to install functions for atexit system call
// To install functions for atexit system call
extern "C" {
static void perfMemory_exit_helper() {
perfMemory_exit();
Expand All @@ -2036,60 +2036,51 @@ jint os::init_2(void) {
return JNI_ERR;
}

// check and sets minimum stack sizes against command line options
// Check and sets minimum stack sizes against command line options
if (set_minimum_stack_sizes() == JNI_ERR) {
return JNI_ERR;
}

// not supported
// Not supported.
FLAG_SET_ERGO(UseNUMA, false);
FLAG_SET_ERGO(UseNUMAInterleaving, false);

if (MaxFDLimit) {
// Set the number of file descriptors to max. Print out error
// set the number of file descriptors to max. print out error
// if getrlimit/setrlimit fails but continue regardless.
struct rlimit nbr_files;
int status = getrlimit(RLIMIT_NOFILE, &nbr_files);
if (status != 0) {
log_info(os)("os::init_2 getrlimit failed: %s", os::strerror(errno));
} else {
rlim_t rlim_original = nbr_files.rlim_cur;

// On macOS according to setrlimit(2), OPEN_MAX must be used instead
// of RLIM_INFINITY, but testing on macOS >= 10.6, reveals that
// we can, in fact, use even RLIM_INFINITY, so try the max value
// that the system claims can be used first, same as other BSD OSes.
// However, some terminals (ksh) will internally use "int" type
// to store this value and since RLIM_INFINITY overflows an "int"
// we might end up with a negative value, so cap the system limit max
// at INT_MAX instead, just in case, for everyone.
nbr_files.rlim_cur = MIN(INT_MAX, nbr_files.rlim_max);
nbr_files.rlim_cur = nbr_files.rlim_max;

#ifdef __APPLE__
// Darwin returns RLIM_INFINITY for rlim_max, but fails with EINVAL if
// you attempt to use RLIM_INFINITY. As per setrlimit(2), OPEN_MAX must
// be used instead
nbr_files.rlim_cur = MIN(OPEN_MAX, nbr_files.rlim_cur);
#endif

status = setrlimit(RLIMIT_NOFILE, &nbr_files);
if (status != 0) {
// If that fails then try lowering the limit to either OPEN_MAX
// (which is safe) or the original limit, whichever was greater.
nbr_files.rlim_cur = MAX(OPEN_MAX, rlim_original);
status = setrlimit(RLIMIT_NOFILE, &nbr_files);
}
if (status != 0) {
log_info(os)("os::init_2 setrlimit failed: %s", os::strerror(errno));
}
}
}

// At-exit methods are called in the reverse order of their registration.
// at-exit methods are called in the reverse order of their registration.
// atexit functions are called on return from main or as a result of a
// call to exit(3C). There can be only 32 of these functions registered
// and atexit() does not set errno.

if (PerfAllowAtExitRegistration) {
// Only register atexit functions if PerfAllowAtExitRegistration is set.
// only register atexit functions if PerfAllowAtExitRegistration is set.
// atexit functions can be delayed until process exit time, which
// can be problematic for embedded VM situations. Embedded VMs should
// call DestroyJavaVM() to assure that VM resources are released.

// Note: perfMemory_exit_helper atexit function may be removed in
// note: perfMemory_exit_helper atexit function may be removed in
// the future if the appropriate cleanup code can be added to the
// VM_Exit VMOperation's doit method.
if (atexit(perfMemory_exit_helper) != 0) {
Expand All @@ -2112,7 +2103,7 @@ jint os::init_2(void) {
}

int os::active_processor_count() {
// user has overridden the number of active processors
// User has overridden the number of active processors
if (ActiveProcessorCount > 0) {
log_trace(os)("active_processor_count: "
"active processor count set by user : %d",
Expand Down Expand Up @@ -2165,9 +2156,9 @@ uint os::processor_id() {

void os::set_native_thread_name(const char *name) {
#if defined(__APPLE__) && MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_5
// this is only supported in Snow Leopard and beyond
// This is only supported in Snow Leopard and beyond
if (name != nullptr) {
// add a "Java: " prefix to the name
// Add a "Java: " prefix to the name
char buf[MAXTHREADNAMESIZE];
snprintf(buf, sizeof(buf), "Java: %s", name);
pthread_setname_np(buf);
Expand Down

1 comment on commit 6d2f640

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.