Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/hotspot/os/aix/os_aix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@

// put OS-includes here (sorted alphabetically)
#include <alloca.h>
#include <ctype.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
Expand Down Expand Up @@ -2717,3 +2719,22 @@ void os::print_memory_mappings(char* addr, size_t bytes, outputStream* st) {}
void os::jfr_report_memory_info() {}

#endif // INCLUDE_JFR

void os::print_open_file_descriptors(outputStream* st) {
char fd_dir[32];
snprintf(fd_dir, sizeof(fd_dir), "/proc/%d/fd", getpid());
DIR* dirp = opendir(fd_dir);
int fds = 0;
if (dirp != nullptr) {
struct dirent* dentp;
while ((dentp = readdir(dirp)) != nullptr) {
if (isdigit(dentp->d_name[0])) {
fds++;
}
}
closedir(dirp);
st->print_cr("OpenFileDescriptorCount = %d", fds - 1); // minus the opendir fd itself
} else {
st->print_cr("OpenFileDescriptorCount = unknown");
}
}
41 changes: 41 additions & 0 deletions src/hotspot/os/bsd/os_bsd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
# include <fcntl.h>
# include <fenv.h>
# include <inttypes.h>
# include <mach/mach.h>
# include <poll.h>
# include <pthread.h>
# include <pwd.h>
Expand All @@ -102,6 +103,7 @@
#endif

#ifdef __APPLE__
#include <libproc.h>
#include <mach/task_info.h>
#include <mach-o/dyld.h>
#endif
Expand Down Expand Up @@ -2501,3 +2503,42 @@ bool os::pd_dll_unload(void* libhandle, char* ebuf, int ebuflen) {

return res;
} // end: os::pd_dll_unload()

#ifdef __APPLE__
void os::print_open_file_descriptors(outputStream* st) {
const int MAX_SAFE_FDS = 1024;
struct proc_fdinfo fds[MAX_SAFE_FDS];
struct proc_bsdinfo bsdinfo;
int nfiles;
kern_return_t kres;
int res;
size_t fds_size;
pid_t my_pid;

kres = pid_for_task(mach_task_self(), &my_pid);
if (kres != KERN_SUCCESS) {
st->print_cr("OpenFileDescriptorCount = unknown");
return;
}

res = proc_pidinfo(my_pid, PROC_PIDLISTFDS, 0, fds, MAX_SAFE_FDS * sizeof(struct proc_fdinfo));
if (res <= 0) {
st->print_cr("OpenFileDescriptorCount = unknown");
return;
}

nfiles = res / sizeof(struct proc_fdinfo);
if (nfiles >= MAX_SAFE_FDS) {
st->print_cr("OpenFileDescriptorCount = unknown");
return;
}

st->print_cr("OpenFileDescriptorCount = %d", nfiles);
}
#endif // __APPLE__

#if defined(_ALLBSD_SOURCE) && !defined(__APPLE__)
long os::get_open_file_descriptor_count() {
st->print_cr("OpenFileDescriptorCount = unknown");
}
#endif
Comment on lines +2507 to +2544
Copy link
Member

Choose a reason for hiding this comment

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

Is defined(_ALLBSD_SOURCE) needed? The return type for the non-apple variant is incorrect. Maybe we could do something like the following here instead:

void os::print_open_file_descriptors(outputStream* st) {
#ifdef __APPLE__
// do Apple stuff
#else
  st->print_cr("OpenFileDescriptorCount = unknown");
#endif
}

17 changes: 17 additions & 0 deletions src/hotspot/os/linux/os_linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5449,3 +5449,20 @@ bool os::pd_dll_unload(void* libhandle, char* ebuf, int ebuflen) {

return res;
} // end: os::pd_dll_unload()

void os::print_open_file_descriptors(outputStream* st) {
DIR* dirp = opendir("/proc/self/fd");
int fds = 0;
if (dirp != nullptr) {
struct dirent* dentp;
while ((dentp = readdir(dirp)) != nullptr) {
if (isdigit(dentp->d_name[0])) {
fds++;
}
}
closedir(dirp);
st->print_cr("OpenFileDescriptorCount = %d", fds - 1);
Copy link
Member

Choose a reason for hiding this comment

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

Maybe a comment on why we do -1 here, like you have for the AIX implementation.

} else {
st->print_cr("OpenFileDescriptorCount = unknown");
}
}
4 changes: 4 additions & 0 deletions src/hotspot/os/windows/os_windows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6260,6 +6260,10 @@ const void* os::get_saved_assert_context(const void** sigInfo) {
return nullptr;
}

void os::print_open_file_descriptors(outputStream* st) {
// File descriptor counting not supported on Windows.
}

/*
* Windows/x64 does not use stack frames the way expected by Java:
* [1] in most cases, there is no frame pointer. All locals are addressed via RSP
Expand Down
3 changes: 3 additions & 0 deletions src/hotspot/share/runtime/os.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -842,6 +842,9 @@ class os: AllStatic {
static void print_date_and_time(outputStream* st, char* buf, size_t buflen);
static void print_elapsed_time(outputStream* st, double time);

// Prints the number of open file descriptors for the current process
static void print_open_file_descriptors(outputStream* st);

static void print_user_info(outputStream* st);
static void print_active_locale(outputStream* st);

Expand Down
6 changes: 6 additions & 0 deletions src/hotspot/share/utilities/vmError.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1432,6 +1432,12 @@ void VMError::print_vm_info(outputStream* st) {
st->cr();
}

// STEP("printing number of open file descriptors")
#ifndef _WIN32
os::print_open_file_descriptors(st);
st->cr();
#endif

Comment on lines +1435 to +1440
Copy link
Member

@jsikstro jsikstro Oct 27, 2025

Choose a reason for hiding this comment

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

This is in VMError::print_vm_info which is only printed via the VM.info jcmd and the -XX:+PrintVMInfoAtExit flag. To add it to hs_err files, you should also add the code to VMError::report.

// Take heap lock over heap, GC and metaspace printing so that information
// is consistent.
if (Universe::is_fully_initialized()) {
Expand Down