8027434: "-XX:OnOutOfMemoryError" uses fork instead of vfork
authormchinnathamb
Tue, 09 Oct 2018 16:08:07 +0530
changeset 52109 101c2b6eacbe
parent 52108 9c84227836d4
child 52110 65efb9c57fef
8027434: "-XX:OnOutOfMemoryError" uses fork instead of vfork Reviewed-by: dholmes, iklam
src/hotspot/os/aix/os_aix.cpp
src/hotspot/os/bsd/os_bsd.cpp
src/hotspot/os/linux/os_linux.cpp
src/hotspot/os/solaris/os_solaris.cpp
src/hotspot/os/windows/os_windows.cpp
src/hotspot/share/runtime/os.hpp
src/hotspot/share/utilities/vmError.cpp
--- a/src/hotspot/os/aix/os_aix.cpp	Fri Oct 12 10:58:06 2018 +0200
+++ b/src/hotspot/os/aix/os_aix.cpp	Tue Oct 09 16:08:07 2018 +0530
@@ -4259,7 +4259,7 @@
 // or -1 on failure (e.g. can't fork a new process).
 // Unlike system(), this function can be called from signal handler. It
 // doesn't block SIGINT et al.
-int os::fork_and_exec(char* cmd) {
+int os::fork_and_exec(char* cmd, bool use_vfork_if_available) {
   char * argv[4] = {"sh", "-c", cmd, NULL};
 
   pid_t pid = fork();
--- a/src/hotspot/os/bsd/os_bsd.cpp	Fri Oct 12 10:58:06 2018 +0200
+++ b/src/hotspot/os/bsd/os_bsd.cpp	Tue Oct 09 16:08:07 2018 +0530
@@ -3785,7 +3785,7 @@
 // or -1 on failure (e.g. can't fork a new process).
 // Unlike system(), this function can be called from signal handler. It
 // doesn't block SIGINT et al.
-int os::fork_and_exec(char* cmd) {
+int os::fork_and_exec(char* cmd, bool use_vfork_if_available) {
   const char * argv[4] = {"sh", "-c", cmd, NULL};
 
   // fork() in BsdThreads/NPTL is not async-safe. It needs to run
--- a/src/hotspot/os/linux/os_linux.cpp	Fri Oct 12 10:58:06 2018 +0200
+++ b/src/hotspot/os/linux/os_linux.cpp	Tue Oct 09 16:08:07 2018 +0530
@@ -5676,10 +5676,16 @@
 // or -1 on failure (e.g. can't fork a new process).
 // Unlike system(), this function can be called from signal handler. It
 // doesn't block SIGINT et al.
-int os::fork_and_exec(char* cmd) {
+int os::fork_and_exec(char* cmd, bool use_vfork_if_available) {
   const char * argv[4] = {"sh", "-c", cmd, NULL};
 
-  pid_t pid = fork();
+  pid_t pid ;
+
+  if (use_vfork_if_available) {
+    pid = vfork();
+  } else {
+    pid = fork();
+  }
 
   if (pid < 0) {
     // fork failed
--- a/src/hotspot/os/solaris/os_solaris.cpp	Fri Oct 12 10:58:06 2018 +0200
+++ b/src/hotspot/os/solaris/os_solaris.cpp	Tue Oct 09 16:08:07 2018 +0530
@@ -5252,7 +5252,7 @@
 // or -1 on failure (e.g. can't fork a new process).
 // Unlike system(), this function can be called from signal handler. It
 // doesn't block SIGINT et al.
-int os::fork_and_exec(char* cmd) {
+int os::fork_and_exec(char* cmd, bool use_vfork_if_available) {
   char * argv[4];
   argv[0] = (char *)"sh";
   argv[1] = (char *)"-c";
--- a/src/hotspot/os/windows/os_windows.cpp	Fri Oct 12 10:58:06 2018 +0200
+++ b/src/hotspot/os/windows/os_windows.cpp	Tue Oct 09 16:08:07 2018 +0530
@@ -5254,7 +5254,7 @@
 
 // Run the specified command in a separate process. Return its exit value,
 // or -1 on failure (e.g. can't create a new process).
-int os::fork_and_exec(char* cmd) {
+int os::fork_and_exec(char* cmd, bool use_vfork_if_available) {
   STARTUPINFO si;
   PROCESS_INFORMATION pi;
   DWORD exit_code;
--- a/src/hotspot/share/runtime/os.hpp	Fri Oct 12 10:58:06 2018 +0200
+++ b/src/hotspot/share/runtime/os.hpp	Tue Oct 09 16:08:07 2018 +0530
@@ -543,7 +543,7 @@
   static char* do_you_want_to_debug(const char* message);
 
   // run cmd in a separate process and return its exit code; or -1 on failures
-  static int fork_and_exec(char *cmd);
+  static int fork_and_exec(char *cmd, bool use_vfork_if_available = false);
 
   // Call ::exit() on all platforms but Windows
   static void exit(int num);
--- a/src/hotspot/share/utilities/vmError.cpp	Fri Oct 12 10:58:06 2018 +0200
+++ b/src/hotspot/share/utilities/vmError.cpp	Tue Oct 09 16:08:07 2018 +0530
@@ -1565,7 +1565,7 @@
 #endif
     tty->print_cr("\"%s\"...", cmd);
 
-    if (os::fork_and_exec(cmd) < 0) {
+    if (os::fork_and_exec(cmd, true) < 0) {
       tty->print_cr("os::fork_and_exec failed: %s (%s=%d)",
                      os::strerror(errno), os::errno_name(errno), errno);
     }