Merge pull request #9860 from 9999years/set-stack-darwin

Increase stack size on macOS as well as Linux
This commit is contained in:
John Ericson 2024-01-26 13:35:10 -05:00 committed by GitHub
commit efb91d5979
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 18 additions and 10 deletions

View file

@ -1,3 +1,6 @@
#include <algorithm>
#include <cstring>
#include "current-process.hh" #include "current-process.hh"
#include "namespaces.hh" #include "namespaces.hh"
#include "util.hh" #include "util.hh"
@ -49,20 +52,27 @@ unsigned int getMaxCPU()
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
#if __linux__
rlim_t savedStackSize = 0; rlim_t savedStackSize = 0;
#endif
void setStackSize(size_t stackSize) void setStackSize(rlim_t stackSize)
{ {
#if __linux__
struct rlimit limit; struct rlimit limit;
if (getrlimit(RLIMIT_STACK, &limit) == 0 && limit.rlim_cur < stackSize) { if (getrlimit(RLIMIT_STACK, &limit) == 0 && limit.rlim_cur < stackSize) {
savedStackSize = limit.rlim_cur; savedStackSize = limit.rlim_cur;
limit.rlim_cur = stackSize; limit.rlim_cur = std::min(stackSize, limit.rlim_max);
setrlimit(RLIMIT_STACK, &limit); if (setrlimit(RLIMIT_STACK, &limit) != 0) {
logger->log(
lvlError,
hintfmt(
"Failed to increase stack size from %1% to %2% (maximum allowed stack size: %3%): %4%",
savedStackSize,
stackSize,
limit.rlim_max,
std::strerror(errno)
).str()
);
}
} }
#endif
} }
void restoreProcessContext(bool restoreMounts) void restoreProcessContext(bool restoreMounts)
@ -72,7 +82,6 @@ void restoreProcessContext(bool restoreMounts)
restoreMountNamespace(); restoreMountNamespace();
} }
#if __linux__
if (savedStackSize) { if (savedStackSize) {
struct rlimit limit; struct rlimit limit;
if (getrlimit(RLIMIT_STACK, &limit) == 0) { if (getrlimit(RLIMIT_STACK, &limit) == 0) {
@ -80,7 +89,6 @@ void restoreProcessContext(bool restoreMounts)
setrlimit(RLIMIT_STACK, &limit); setrlimit(RLIMIT_STACK, &limit);
} }
} }
#endif
} }

View file

@ -16,7 +16,7 @@ unsigned int getMaxCPU();
/** /**
* Change the stack size. * Change the stack size.
*/ */
void setStackSize(size_t stackSize); void setStackSize(rlim_t stackSize);
/** /**
* Restore the original inherited Unix process context (such as signal * Restore the original inherited Unix process context (such as signal