Skip to content
Open
Changes from 4 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
59 changes: 53 additions & 6 deletions src/hotspot/os/bsd/os_bsd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@
# include <fcntl.h>
# include <fenv.h>
# include <inttypes.h>
# include <mach/mach.h>
# include <poll.h>
# include <pthread.h>
# include <pwd.h>
Expand Down Expand Up @@ -112,6 +111,7 @@

#ifdef __APPLE__
#include <libproc.h>
#include <mach/mach.h>
#include <mach/task_info.h>
#include <mach-o/dyld.h>

Expand All @@ -124,6 +124,32 @@
#include <pthread_np.h>
#endif

#if defined(__APPLE__)
#define KERN_PROC_MIB KERN_PROC
#define KINFO_PROC_T kinfo_proc
#define KI_UID kp_eproc.e_ucred.cr_uid
#define KI_PID kp_proc.p_pid
#elif defined(__OpenBSD__)
#define KERN_PROC_MIB KERN_PROC
#define KINFO_PROC_T kinfo_proc
#define KI_RSS p_vm_rssize
#define KI_UID p_uid
#define KI_PID p_pid
#elif defined(__FreeBSD__)
#include <sys/user.h>
#define KERN_PROC_MIB KERN_PROC
#define KINFO_PROC_T kinfo_proc
#define KI_RSS ki_rssize
#define KI_UID ki_uid
#define KI_PID ki_pid
#elif defined(__NetBSD__)
#define KERN_PROC_MIB KERN_PROC2
#define KINFO_PROC_T kinfo_proc2
#define KI_RSS p_vm_rssize
#define KI_UID p_uid
#define KI_PID p_pid
#endif

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is an eyesore. Can this be shortened, and/or moved into a helper include?


#ifndef MAP_ANONYMOUS
#define MAP_ANONYMOUS MAP_ANON
#endif
Expand Down Expand Up @@ -261,6 +287,20 @@ size_t os::rss() {
if (ret == KERN_SUCCESS) {
rss = info.resident_size;
}
#else
pid_t pid = getpid();
struct KINFO_PROC_T kp;
size_t bufSize = sizeof kp;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

we use brackets with sizeof

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Fixed in latest commit.

#ifndef __FreeBSD__
u_int namelen = 6;
int mib[6] = {CTL_KERN, KERN_PROC_MIB, KERN_PROC_PID, pid, bufSize, 1};
#else
u_int namelen = 4;
int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PID, pid};
#endif

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Make namelen const, move it out of the ifdefs, and calculated it based on the array like this

const int namelen = sizeof(mib)/sizeof(mib[0]);

also mib can probably be constexpr as well.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Updated, but a few comments:

I could not use the NOT_FREEBSD() macro as it would require it to be a variadic macro. That works and is doable, but since the other similar macros are not variadic, I though it best to keep to the same pattern. Still reduced to a single conditional using plain #ifndef makes the code nicer I think.

mib can not be constexpr since it contains the pid which is by definition not const :)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

haha right, sorry, my bad

if (sysctl(mib, namelen, &kp, &bufSize, nullptr, 0) != -1) {
return kp.KI_RSS * getpagesize();
}
#endif // __APPLE__

return rss;
Expand Down Expand Up @@ -899,12 +939,19 @@ pid_t os::Bsd::gettid() {

// Returns the uid of a process or -1 on error.
uid_t os::Bsd::get_process_uid(pid_t pid) {
struct kinfo_proc kp;
struct KINFO_PROC_T kp;
size_t size = sizeof kp;
int mib_kern[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PID, pid};
if (sysctl(mib_kern, 4, &kp, &size, nullptr, 0) == 0) {
if (size > 0 && kp.kp_proc.p_pid == pid) {
return kp.kp_eproc.e_ucred.cr_uid;
#if defined(__FreeBSD__) || defined(__APPLE__)
u_int namelen = 4;
int mib_kern[4] = {CTL_KERN, KERN_PROC_MIB, KERN_PROC_PID, pid};
#else
u_int namelen = 6;
int mib_kern[6] = {CTL_KERN, KERN_PROC_MIB, KERN_PROC_PID, pid,
static_cast<int>(size), 1};
#endif
if (sysctl(mib_kern, namelen, &kp, &size, nullptr, 0) == 0) {
if (size > 0 && kp.KI_PID == pid) {
return kp.KI_UID;
}
}
return (uid_t)-1;
Expand Down