From c11ea7e78e41b09bb4b984dbc2b64c932884148b Mon Sep 17 00:00:00 2001 From: phh Date: Mon, 25 Feb 2019 21:38:45 +0000 Subject: [PATCH] 8027434: "-XX:OnOutOfMemoryError" uses fork instead of vfork Summary: On Linux, use vfork in case of an OOM. Reviewed-by: dholmes, iklam --- src/os/aix/vm/os_aix.cpp | 4 ++-- src/os/bsd/vm/os_bsd.cpp | 4 ++-- src/os/linux/vm/os_linux.cpp | 12 +++++++++--- src/os/solaris/vm/os_solaris.cpp | 4 ++-- src/os/windows/vm/os_windows.cpp | 4 ++-- src/share/vm/runtime/os.hpp | 4 ++-- src/share/vm/utilities/vmError.cpp | 4 ++-- 7 files changed, 21 insertions(+), 15 deletions(-) diff --git a/src/os/aix/vm/os_aix.cpp b/src/os/aix/vm/os_aix.cpp index 8cd1f4e8f..b03ce2458 100644 --- a/src/os/aix/vm/os_aix.cpp +++ b/src/os/aix/vm/os_aix.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 1999, 2018, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1999, 2019, Oracle and/or its affiliates. All rights reserved. * Copyright 2012, 2014 SAP AG. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * @@ -5142,7 +5142,7 @@ extern char** environ; // 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(); diff --git a/src/os/bsd/vm/os_bsd.cpp b/src/os/bsd/vm/os_bsd.cpp index c377c7d9f..99a039988 100644 --- a/src/os/bsd/vm/os_bsd.cpp +++ b/src/os/bsd/vm/os_bsd.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 1999, 2018, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1999, 2019, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -4716,7 +4716,7 @@ extern char** environ; // 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 diff --git a/src/os/linux/vm/os_linux.cpp b/src/os/linux/vm/os_linux.cpp index 4d4dad060..a7b12483c 100644 --- a/src/os/linux/vm/os_linux.cpp +++ b/src/os/linux/vm/os_linux.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 1999, 2018, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1999, 2019, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -6337,10 +6337,16 @@ extern char** environ; // 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 diff --git a/src/os/solaris/vm/os_solaris.cpp b/src/os/solaris/vm/os_solaris.cpp index 4a786cffb..2a733a75e 100644 --- a/src/os/solaris/vm/os_solaris.cpp +++ b/src/os/solaris/vm/os_solaris.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1997, 2019, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -6153,7 +6153,7 @@ extern char** environ; // 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"; diff --git a/src/os/windows/vm/os_windows.cpp b/src/os/windows/vm/os_windows.cpp index 4f550c90f..1baf45f2c 100644 --- a/src/os/windows/vm/os_windows.cpp +++ b/src/os/windows/vm/os_windows.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1997, 2019, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -5034,7 +5034,7 @@ void Parker::unpark() { // 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; diff --git a/src/share/vm/runtime/os.hpp b/src/share/vm/runtime/os.hpp index 92553ab47..931f4cb5b 100644 --- a/src/share/vm/runtime/os.hpp +++ b/src/share/vm/runtime/os.hpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1997, 2019, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -527,7 +527,7 @@ class os: AllStatic { 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); // os::exit() is merged with vm_exit() // static void exit(int num); diff --git a/src/share/vm/utilities/vmError.cpp b/src/share/vm/utilities/vmError.cpp index 8939da9bb..ef3bb5cee 100644 --- a/src/share/vm/utilities/vmError.cpp +++ b/src/share/vm/utilities/vmError.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -1060,7 +1060,7 @@ void VMError::report_and_die() { out.print_raw (cmd); out.print_raw_cr("\" ..."); - if (os::fork_and_exec(cmd) < 0) { + if (os::fork_and_exec(cmd, true) < 0) { out.print_cr("os::fork_and_exec failed: %s (%d)", strerror(errno), errno); } } -- GitLab