Skip to content

Commit d5191e5

Browse files
TimPushkinRadim Vansa
andcommitted
8357641: [CRaC] Support PID change during checkpoint-restore
Co-authored-by: Radim Vansa <rvansa@openjdk.org> Reviewed-by: rvansa
1 parent 2eb2197 commit d5191e5

File tree

15 files changed

+94
-9
lines changed

15 files changed

+94
-9
lines changed

src/hotspot/os/aix/os_aix.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -916,6 +916,9 @@ int os::current_process_id() {
916916
return getpid();
917917
}
918918

919+
void os::reset_cached_process_id() {
920+
}
921+
919922
// DLL functions
920923

921924
// This must be hard coded because it's the system's temporary

src/hotspot/os/bsd/os_bsd.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -869,6 +869,9 @@ int os::current_process_id() {
869869
return (int)(getpid());
870870
}
871871

872+
void os::reset_cached_process_id() {
873+
}
874+
872875
// DLL functions
873876
static int local_dladdr(const void* addr, Dl_info* info) {
874877
#ifdef __APPLE__

src/hotspot/os/linux/os_linux.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,8 @@ typedef int (*malloc_info_func_t)(int options, FILE *stream);
220220
static malloc_info_func_t g_malloc_info = nullptr;
221221
#endif // __GLIBC__
222222

223+
static int cached_pid = 0;
224+
223225
static int clock_tics_per_sec = 100;
224226

225227
// If the VM might have been created on the primordial thread, we need to resolve the
@@ -1516,8 +1518,16 @@ void os::Linux::fast_thread_clock_init() {
15161518

15171519
// thread_id is kernel thread id (similar to Solaris LWP id)
15181520
intx os::current_thread_id() { return os::Linux::gettid(); }
1521+
15191522
int os::current_process_id() {
1520-
return ::getpid();
1523+
// GLIBC < 2.25 caches pid in ::getpid(); we need to be able to reset this
1524+
// on CRaC restore, therefore we will use our own caching.
1525+
return cached_pid ? cached_pid : syscall(SYS_getpid);
1526+
}
1527+
1528+
void os::reset_cached_process_id() {
1529+
cached_pid = 0;
1530+
cached_pid = current_process_id();
15211531
}
15221532

15231533
// DLL functions
@@ -4415,6 +4425,7 @@ static void check_pax(void) {
44154425
// this is called _before_ most of the global arguments have been parsed
44164426
void os::init(void) {
44174427
char dummy; // used to get a guess on initial stack address
4428+
cached_pid = current_process_id();
44184429

44194430
clock_tics_per_sec = checked_cast<int>(sysconf(_SC_CLK_TCK));
44204431
int sys_pg_size = checked_cast<int>(sysconf(_SC_PAGESIZE));

src/hotspot/os/posix/signals_posix.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1169,7 +1169,7 @@ void os::print_siginfo(outputStream* os, const void* si0) {
11691169
const pid_t pid = si->si_pid;
11701170
os->print(", si_pid: %ld", (long) pid);
11711171
if (IS_VALID_PID(pid)) {
1172-
const pid_t me = getpid();
1172+
const pid_t me = os::current_process_id();
11731173
if (me == pid) {
11741174
os->print(" (current process)");
11751175
}

src/hotspot/os/windows/os_windows.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3951,6 +3951,9 @@ int os::current_process_id() {
39513951
return (_initial_pid ? _initial_pid : _getpid());
39523952
}
39533953

3954+
void os::reset_cached_process_id() {
3955+
}
3956+
39543957
int os::win32::_processor_type = 0;
39553958
// Processor level is not available on non-NT systems, use vm_version instead
39563959
int os::win32::_processor_level = 0;

src/hotspot/share/runtime/crac.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@ void VM_Crac::doit() {
280280
}
281281

282282
Arguments::reset_for_crac_restore();
283+
os::reset_cached_process_id();
283284

284285
if (shmid == 0) { // E.g. engine does not support restore data
285286
log_debug(crac)("Restore parameters (JVM flags, env vars, system properties, arguments...) not provided");

src/hotspot/share/runtime/os.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,7 @@ class os: AllStatic {
608608
// thread id on Linux/64bit is 64bit, on Windows it's 32bit
609609
static intx current_thread_id();
610610
static int current_process_id();
611+
static void reset_cached_process_id();
611612

612613
// Short standalone OS sleep routines suitable for slow path spin loop.
613614
// Ignores safepoints/suspension/Thread.interrupt() (so keep it short).

src/java.base/share/classes/java/lang/ProcessHandleImpl.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@
2424
*/
2525
package java.lang;
2626

27+
import jdk.internal.crac.Core;
28+
import jdk.internal.crac.JDKResource;
29+
import jdk.internal.crac.mirror.Context;
30+
import jdk.internal.crac.mirror.Resource;
2731
import jdk.internal.misc.InnocuousThread;
2832

2933
import java.lang.annotation.Native;
@@ -73,10 +77,22 @@ final class ProcessHandleImpl implements ProcessHandle {
7377
private static final ConcurrentMap<Long, ExitCompletion>
7478
completions = new ConcurrentHashMap<>();
7579

80+
private static JDKResource cracResource = new JDKResource() {
81+
@Override
82+
public void beforeCheckpoint(Context<? extends Resource> context) {
83+
}
84+
85+
@Override
86+
public void afterRestore(Context<? extends Resource> context) {
87+
current.pid = getCurrentPid0();
88+
}
89+
};
90+
7691
static {
7792
initNative();
7893
long pid = getCurrentPid0();
7994
current = new ProcessHandleImpl(pid, isAlive0(pid));
95+
Core.Priority.NORMAL.getContext().register(cracResource);
8096
}
8197

8298
private static native void initNative();
@@ -206,7 +222,7 @@ public CompletableFuture<ProcessHandle> onExit() {
206222
/**
207223
* The pid of this ProcessHandle.
208224
*/
209-
private final long pid;
225+
private long pid;
210226

211227
/**
212228
* The start time of this process.

src/java.base/unix/native/libjava/ProcessHandleImpl_unix.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@
4444
#include <sys/stat.h>
4545
#include <sys/wait.h>
4646

47+
#ifdef LINUX
48+
#include <sys/syscall.h>
49+
#endif
50+
4751
#include <pwd.h>
4852

4953
#if defined(_AIX)
@@ -274,7 +278,11 @@ Java_java_lang_ProcessHandleImpl_waitForProcessExit0(JNIEnv* env,
274278
*/
275279
JNIEXPORT jlong JNICALL
276280
Java_java_lang_ProcessHandleImpl_getCurrentPid0(JNIEnv *env, jclass clazz) {
281+
#ifdef LINUX
282+
pid_t pid = syscall(SYS_getpid);
283+
#else
277284
pid_t pid = getpid();
285+
#endif
278286
return (jlong) pid;
279287
}
280288

@@ -383,7 +391,12 @@ Java_java_lang_ProcessHandleImpl_parent0(JNIEnv *env,
383391
pid_t pid = (pid_t) jpid;
384392
pid_t ppid;
385393

386-
if (pid == getpid()) {
394+
#ifdef LINUX
395+
pid_t mypid = syscall(SYS_getpid);
396+
#else
397+
pid_t mypid = getpid();
398+
#endif
399+
if (pid == mypid) {
387400
ppid = getppid();
388401
} else {
389402
jlong start = 0L;

src/java.base/unix/native/libnet/Inet4AddressImpl.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131
#include <netinet/ip_icmp.h>
3232
#include <stdlib.h>
3333
#include <string.h>
34+
#ifdef LINUX
35+
#include <sys/syscall.h>
36+
#endif
3437
#include <sys/time.h>
3538

3639
#include "net_util.h"
@@ -379,7 +382,11 @@ ping4(JNIEnv *env, jint fd, SOCKETADDRESS *sa, SOCKETADDRESS *netif,
379382
}
380383

381384
// icmp_id is a 16 bit data type, therefore down cast the pid
385+
#ifdef LINUX
386+
pid = (jchar)syscall(SYS_getpid);
387+
#else
382388
pid = (jchar)getpid();
389+
#endif // !LINUX
383390

384391
// Make the socket non blocking so we can use select
385392
SET_NONBLOCKING(fd);

0 commit comments

Comments
 (0)