Skip to content

Commit 3d82363

Browse files
author
Gerard Ziemski
committed
8300088: [IMPROVE] OPEN_MAX is no longer the max limit on macOS >= 10.6 for RLIMIT_NOFILE
Reviewed-by: dholmes, fparain, dcubed
1 parent 2a01c79 commit 3d82363

File tree

1 file changed

+29
-20
lines changed

1 file changed

+29
-20
lines changed

src/hotspot/os/bsd/os_bsd.cpp

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1999, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1999, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -2008,15 +2008,15 @@ void os::init(void) {
20082008

20092009
Bsd::initialize_system_info();
20102010

2011-
// _main_thread points to the thread that created/loaded the JVM.
2011+
// _main_thread points to the thread that created/loaded the JVM
20122012
Bsd::_main_thread = pthread_self();
20132013

20142014
Bsd::clock_init();
20152015

20162016
os::Posix::init();
20172017
}
20182018

2019-
// To install functions for atexit system call
2019+
// to install functions for atexit system call
20202020
extern "C" {
20212021
static void perfMemory_exit_helper() {
20222022
perfMemory_exit();
@@ -2036,51 +2036,60 @@ jint os::init_2(void) {
20362036
return JNI_ERR;
20372037
}
20382038

2039-
// Check and sets minimum stack sizes against command line options
2039+
// check and sets minimum stack sizes against command line options
20402040
if (set_minimum_stack_sizes() == JNI_ERR) {
20412041
return JNI_ERR;
20422042
}
20432043

2044-
// Not supported.
2044+
// not supported
20452045
FLAG_SET_ERGO(UseNUMA, false);
20462046
FLAG_SET_ERGO(UseNUMAInterleaving, false);
20472047

20482048
if (MaxFDLimit) {
2049-
// set the number of file descriptors to max. print out error
2049+
// Set the number of file descriptors to max. Print out error
20502050
// if getrlimit/setrlimit fails but continue regardless.
20512051
struct rlimit nbr_files;
20522052
int status = getrlimit(RLIMIT_NOFILE, &nbr_files);
20532053
if (status != 0) {
20542054
log_info(os)("os::init_2 getrlimit failed: %s", os::strerror(errno));
20552055
} else {
2056-
nbr_files.rlim_cur = nbr_files.rlim_max;
2057-
2058-
#ifdef __APPLE__
2059-
// Darwin returns RLIM_INFINITY for rlim_max, but fails with EINVAL if
2060-
// you attempt to use RLIM_INFINITY. As per setrlimit(2), OPEN_MAX must
2061-
// be used instead
2062-
nbr_files.rlim_cur = MIN(OPEN_MAX, nbr_files.rlim_cur);
2063-
#endif
2056+
rlim_t rlim_original = nbr_files.rlim_cur;
2057+
2058+
// On macOS according to setrlimit(2), OPEN_MAX must be used instead
2059+
// of RLIM_INFINITY, but testing on macOS >= 10.6, reveals that
2060+
// we can, in fact, use even RLIM_INFINITY, so try the max value
2061+
// that the system claims can be used first, same as other BSD OSes.
2062+
// However, some terminals (ksh) will internally use "int" type
2063+
// to store this value and since RLIM_INFINITY overflows an "int"
2064+
// we might end up with a negative value, so cap the system limit max
2065+
// at INT_MAX instead, just in case, for everyone.
2066+
nbr_files.rlim_cur = MIN(INT_MAX, nbr_files.rlim_max);
20642067

20652068
status = setrlimit(RLIMIT_NOFILE, &nbr_files);
2069+
if (status != 0) {
2070+
// If that fails then try lowering the limit to either OPEN_MAX
2071+
// (which is safe) or the original limit, whichever was greater.
2072+
nbr_files.rlim_cur = MAX(OPEN_MAX, rlim_original);
2073+
status = setrlimit(RLIMIT_NOFILE, &nbr_files);
2074+
}
20662075
if (status != 0) {
20672076
log_info(os)("os::init_2 setrlimit failed: %s", os::strerror(errno));
20682077
}
20692078
}
20702079
}
20712080

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

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

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

21052114
int os::active_processor_count() {
2106-
// User has overridden the number of active processors
2115+
// user has overridden the number of active processors
21072116
if (ActiveProcessorCount > 0) {
21082117
log_trace(os)("active_processor_count: "
21092118
"active processor count set by user : %d",
@@ -2156,9 +2165,9 @@ uint os::processor_id() {
21562165

21572166
void os::set_native_thread_name(const char *name) {
21582167
#if defined(__APPLE__) && MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_5
2159-
// This is only supported in Snow Leopard and beyond
2168+
// this is only supported in Snow Leopard and beyond
21602169
if (name != nullptr) {
2161-
// Add a "Java: " prefix to the name
2170+
// add a "Java: " prefix to the name
21622171
char buf[MAXTHREADNAMESIZE];
21632172
snprintf(buf, sizeof(buf), "Java: %s", name);
21642173
pthread_setname_np(buf);

0 commit comments

Comments
 (0)