From 448ea39ab99c343afd3b4700ed8e037afbf69a15 Mon Sep 17 00:00:00 2001 From: Hraban Luyat Date: Sat, 13 Apr 2024 14:15:23 -0400 Subject: [PATCH 1/2] sbcl: custom patch to read memory from envvar --- pkgs/development/compilers/sbcl/default.nix | 8 +++ .../dynamic-space-size-envvar-feature.patch | 63 +++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 pkgs/development/compilers/sbcl/dynamic-space-size-envvar-feature.patch diff --git a/pkgs/development/compilers/sbcl/default.nix b/pkgs/development/compilers/sbcl/default.nix index 4116952199719..cdde06cbca03b 100644 --- a/pkgs/development/compilers/sbcl/default.nix +++ b/pkgs/development/compilers/sbcl/default.nix @@ -113,6 +113,14 @@ stdenv.mkDerivation (self: rec { # have it block a release. "futex-wait.test.sh" ]; + patches = [ + # Support the NIX_SBCL_DYNAMIC_SPACE_SIZE envvar. Upstream SBCL didn’t want + # to include this (see + # "https://sourceforge.net/p/sbcl/mailman/sbcl-devel/thread/2cf20df7-01d0-44f2-8551-0df01fe55f1a%400brg.net/"), + # but for Nix envvars are sufficiently useful that it’s worth maintaining + # this functionality downstream. + ./dynamic-space-size-envvar-feature.patch + ]; postPatch = lib.optionalString (self.disabledTestFiles != [ ]) '' (cd tests ; rm -f ${lib.concatStringsSep " " self.disabledTestFiles}) '' diff --git a/pkgs/development/compilers/sbcl/dynamic-space-size-envvar-feature.patch b/pkgs/development/compilers/sbcl/dynamic-space-size-envvar-feature.patch new file mode 100644 index 0000000000000..f1596958a6f8f --- /dev/null +++ b/pkgs/development/compilers/sbcl/dynamic-space-size-envvar-feature.patch @@ -0,0 +1,63 @@ +From ac15f9f7c75c1fb5767514e64b609e2a75e6fe9d Mon Sep 17 00:00:00 2001 +From: Hraban Luyat +Date: Sat, 13 Apr 2024 14:04:57 -0400 +Subject: [PATCH] feat: NIX_SBCL_DYNAMIC_SPACE_SIZE envvar + +Read SBCL dynamic space size configuration from env if available. +--- + src/runtime/runtime.c | 25 +++++++++++++++++++++++++ + 1 file changed, 25 insertions(+) + +diff --git a/src/runtime/runtime.c b/src/runtime/runtime.c +index 274687c8f..970caa8f4 100644 +--- a/src/runtime/runtime.c ++++ b/src/runtime/runtime.c +@@ -422,6 +422,29 @@ static int is_memsize_arg(char *argv[], int argi, int argc, int *merge_core_page + return 0; + } + ++/** ++ * Read memory options from the environment, if present. ++ * ++ * Memory settings are read in the following priority: ++ * ++ * 1. command line arguments ++ * 2. environment variable ++ * 3. embedded options in core ++ * 4. default ++ */ ++static void ++read_memsize_from_env(void) { ++ const char *val = getenv("NIX_SBCL_DYNAMIC_SPACE_SIZE"); ++ // The distinction is blurry between setting an envvar to the empty string and ++ // unsetting it entirely. Depending on the calling environment it can even be ++ // tricky to properly unset an envvar in the first place. An empty envvar is ++ // practically always intended to just mean “unset”, so let’s interpret it ++ // that way. ++ if (val != NULL && (strcmp(val, "") != 0)) { ++ dynamic_space_size = parse_size_arg(val, "NIX_SBCL_DYNAMIC_SPACE_SIZE"); ++ } ++} ++ + static struct cmdline_options + parse_argv(struct memsize_options memsize_options, + int argc, char *argv[], char *envp[], char *core) +@@ -462,6 +485,7 @@ parse_argv(struct memsize_options memsize_options, + dynamic_space_size = memsize_options.dynamic_space_size; + thread_control_stack_size = memsize_options.thread_control_stack_size; + dynamic_values_bytes = memsize_options.thread_tls_bytes; ++ read_memsize_from_env(); + int stop_parsing = 0; // have we seen '--' + int output_index = 1; + +@@ -488,6 +512,7 @@ parse_argv(struct memsize_options memsize_options, + } + sbcl_argv[output_index] = 0; + } else { ++ read_memsize_from_env(); + bool end_runtime_options = 0; + /* Parse our any of the command-line options that we handle from C, + * stopping at the first one that we don't, and leave the rest */ +-- +2.44.0 + From 2712181ce37ddc8375343b42ced2e42722876661 Mon Sep 17 00:00:00 2001 From: Hraban Luyat Date: Fri, 10 May 2024 14:41:28 -0400 Subject: [PATCH 2/2] sbcl: test for memory envvar read --- pkgs/development/compilers/sbcl/default.nix | 1 + .../dynamic-space-size-envvar-tests.patch | 104 ++++++++++++++++++ 2 files changed, 105 insertions(+) create mode 100644 pkgs/development/compilers/sbcl/dynamic-space-size-envvar-tests.patch diff --git a/pkgs/development/compilers/sbcl/default.nix b/pkgs/development/compilers/sbcl/default.nix index cdde06cbca03b..75ead6184aa88 100644 --- a/pkgs/development/compilers/sbcl/default.nix +++ b/pkgs/development/compilers/sbcl/default.nix @@ -120,6 +120,7 @@ stdenv.mkDerivation (self: rec { # but for Nix envvars are sufficiently useful that it’s worth maintaining # this functionality downstream. ./dynamic-space-size-envvar-feature.patch + ./dynamic-space-size-envvar-tests.patch ]; postPatch = lib.optionalString (self.disabledTestFiles != [ ]) '' (cd tests ; rm -f ${lib.concatStringsSep " " self.disabledTestFiles}) diff --git a/pkgs/development/compilers/sbcl/dynamic-space-size-envvar-tests.patch b/pkgs/development/compilers/sbcl/dynamic-space-size-envvar-tests.patch new file mode 100644 index 0000000000000..1a507cc90381a --- /dev/null +++ b/pkgs/development/compilers/sbcl/dynamic-space-size-envvar-tests.patch @@ -0,0 +1,104 @@ +From 9d4a886a8a76ea8be51bcf754cefacdf30986f46 Mon Sep 17 00:00:00 2001 +From: Hraban Luyat +Date: Sat, 13 Apr 2024 15:39:58 -0400 +Subject: [PATCH 2/2] test: dynamic space size envvar and precedence + +--- + tests/memory-args.test.sh | 22 ++++++++++++++++++++++ + tests/save7.test.sh | 37 ++++++++++++++++++++++++++++++++----- + 2 files changed, 54 insertions(+), 5 deletions(-) + create mode 100755 tests/memory-args.test.sh + +diff --git a/tests/memory-args.test.sh b/tests/memory-args.test.sh +new file mode 100755 +index 000000000..72ef0cc79 +--- /dev/null ++++ b/tests/memory-args.test.sh +@@ -0,0 +1,22 @@ ++#!/bin/sh ++ ++. ./subr.sh ++ ++use_test_subdirectory ++ ++set -e ++ ++# Allow slight shrinkage if heap relocation has to adjust for alignment ++NIX_SBCL_DYNAMIC_SPACE_SIZE=234mb run_sbcl_with_args --script <