Merge pull request #10737 from poweredbypie/mingw-stackSize

Implement `setStackSize` on Windows
This commit is contained in:
John Ericson 2024-05-25 09:56:02 -04:00 committed by GitHub
commit 5cfa75ea16
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 54 additions and 10 deletions

View file

@ -59,15 +59,15 @@ unsigned int getMaxCPU()
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
#ifndef _WIN32 size_t savedStackSize = 0;
rlim_t savedStackSize = 0;
void setStackSize(rlim_t stackSize) void setStackSize(size_t stackSize)
{ {
#ifndef _WIN32
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 = std::min(stackSize, limit.rlim_max); limit.rlim_cur = std::min(static_cast<rlim_t>(stackSize), limit.rlim_max);
if (setrlimit(RLIMIT_STACK, &limit) != 0) { if (setrlimit(RLIMIT_STACK, &limit) != 0) {
logger->log( logger->log(
lvlError, lvlError,
@ -81,8 +81,31 @@ void setStackSize(rlim_t stackSize)
); );
} }
} }
#else
ULONG_PTR stackLow, stackHigh;
GetCurrentThreadStackLimits(&stackLow, &stackHigh);
ULONG maxStackSize = stackHigh - stackLow;
ULONG currStackSize = 0;
// This retrieves the current promised stack size
SetThreadStackGuarantee(&currStackSize);
if (currStackSize < stackSize) {
savedStackSize = currStackSize;
ULONG newStackSize = std::min(static_cast<ULONG>(stackSize), maxStackSize);
if (SetThreadStackGuarantee(&newStackSize) == 0) {
logger->log(
lvlError,
HintFmt(
"Failed to increase stack size from %1% to %2% (maximum allowed stack size: %3%): %4%",
savedStackSize,
stackSize,
maxStackSize,
std::to_string(GetLastError())
).str()
);
}
}
#endif
} }
#endif
void restoreProcessContext(bool restoreMounts) void restoreProcessContext(bool restoreMounts)
{ {

View file

@ -17,12 +17,10 @@ namespace nix {
*/ */
unsigned int getMaxCPU(); unsigned int getMaxCPU();
#ifndef _WIN32 // TODO implement on Windows, if needed.
/** /**
* Change the stack size. * Change the stack size.
*/ */
void setStackSize(rlim_t stackSize); void setStackSize(size_t stackSize);
#endif
/** /**
* Restore the original inherited Unix process context (such as signal * Restore the original inherited Unix process context (such as signal

View file

@ -30,6 +30,11 @@ nix_LIBS = libexpr libmain libfetchers libstore libutil libcmd
nix_LDFLAGS = $(THREAD_LDFLAGS) $(SODIUM_LIBS) $(EDITLINE_LIBS) $(BOOST_LDFLAGS) $(LOWDOWN_LIBS) nix_LDFLAGS = $(THREAD_LDFLAGS) $(SODIUM_LIBS) $(EDITLINE_LIBS) $(BOOST_LDFLAGS) $(LOWDOWN_LIBS)
ifdef HOST_WINDOWS
# Increase the default reserved stack size to 65 MB so Nix doesn't run out of space
nix_LDFLAGS += -Wl,--stack,$(shell echo $$((65 * 1024 * 1024)))
endif
$(foreach name, \ $(foreach name, \
nix-build nix-channel nix-collect-garbage nix-copy-closure nix-daemon nix-env nix-hash nix-instantiate nix-prefetch-url nix-shell nix-store, \ nix-build nix-channel nix-collect-garbage nix-copy-closure nix-daemon nix-env nix-hash nix-instantiate nix-prefetch-url nix-shell nix-store, \
$(eval $(call install-symlink, nix, $(bindir)/$(name)))) $(eval $(call install-symlink, nix, $(bindir)/$(name))))

View file

@ -522,11 +522,9 @@ void mainWrapped(int argc, char * * argv)
int main(int argc, char * * argv) int main(int argc, char * * argv)
{ {
#ifndef _WIN32 // TODO implement on Windows
// Increase the default stack size for the evaluator and for // Increase the default stack size for the evaluator and for
// libstdc++'s std::regex. // libstdc++'s std::regex.
nix::setStackSize(64 * 1024 * 1024); nix::setStackSize(64 * 1024 * 1024);
#endif
return nix::handleExceptions(argv[0], [&]() { return nix::handleExceptions(argv[0], [&]() {
nix::mainWrapped(argc, argv); nix::mainWrapped(argc, argv);

View file

@ -38,3 +38,8 @@ libexpr-tests_LIBS = \
libexpr libexprc libfetchers libstore libstorec libutil libutilc libexpr libexprc libfetchers libstore libstorec libutil libutilc
libexpr-tests_LDFLAGS := -lrapidcheck $(GTEST_LIBS) -lgmock libexpr-tests_LDFLAGS := -lrapidcheck $(GTEST_LIBS) -lgmock
ifdef HOST_WINDOWS
# Increase the default reserved stack size to 65 MB so Nix doesn't run out of space
libexpr-tests_LDFLAGS += -Wl,--stack,$(shell echo $$((65 * 1024 * 1024)))
endif

View file

@ -30,3 +30,8 @@ libfetchers-tests_LIBS = \
libfetchers libstore libutil libfetchers libstore libutil
libfetchers-tests_LDFLAGS := -lrapidcheck $(GTEST_LIBS) libfetchers-tests_LDFLAGS := -lrapidcheck $(GTEST_LIBS)
ifdef HOST_WINDOWS
# Increase the default reserved stack size to 65 MB so Nix doesn't run out of space
libfetchers-tests_LDFLAGS += -Wl,--stack,$(shell echo $$((65 * 1024 * 1024)))
endif

View file

@ -31,3 +31,8 @@ libstore-tests_LIBS = \
libstore libstorec libutil libutilc libstore libstorec libutil libutilc
libstore-tests_LDFLAGS := -lrapidcheck $(GTEST_LIBS) libstore-tests_LDFLAGS := -lrapidcheck $(GTEST_LIBS)
ifdef HOST_WINDOWS
# Increase the default reserved stack size to 65 MB so Nix doesn't run out of space
libstore-tests_LDFLAGS += -Wl,--stack,$(shell echo $$((65 * 1024 * 1024)))
endif

View file

@ -27,6 +27,11 @@ libutil-tests_LIBS = libutil-test-support libutil libutilc
libutil-tests_LDFLAGS := -lrapidcheck $(GTEST_LIBS) libutil-tests_LDFLAGS := -lrapidcheck $(GTEST_LIBS)
ifdef HOST_WINDOWS
# Increase the default reserved stack size to 65 MB so Nix doesn't run out of space
libutil-tests_LDFLAGS += -Wl,--stack,$(shell echo $$((65 * 1024 * 1024)))
endif
check: $(d)/data/git/check-data.sh.test check: $(d)/data/git/check-data.sh.test
$(eval $(call run-test,$(d)/data/git/check-data.sh)) $(eval $(call run-test,$(d)/data/git/check-data.sh))