]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
maint: use adaptive approach for `ulimit -v` based tests
authorBernhard Voelker <mail@bernhard-voelker.de>
Tue, 22 Sep 2015 21:23:26 +0000 (23:23 +0200)
committerBernhard Voelker <mail@bernhard-voelker.de>
Tue, 22 Sep 2015 21:23:26 +0000 (23:23 +0200)
When configured with either 'symlinks' or 'shebangs' as value for
the --enable-single-binary option, tests based on `ulimit -v` are
skipped.  The reason is that the multicall 'coreutils' binary requires
much more memory due to shared libraries being loaded, and the size of
the 'date' binary (~290KiB) compared to the multicall binary (~5MiB),
of course.  Finally, in the case of 'shebangs', the starting shell
requires more memory, too

Instead of using hard-coded values for the memory limit, use an
adaptive approach: first determine the amount of memory for a similar,
yet more trivial invocation of the command, and then do the real test
run using that limit (plus some buffer in some cases).

* init.cfg (require_ulimit_v_): Remove function.
(get_min_ulimit_v_): Add function to determine the minimum memory limit
required for a given command in an adaptive way.
* cfg.mk (sc_prohibit_test_ulimit_without_require_): Change the name
of the above function in the syntax-check rule.
* tests/cp/link-heap.sh: Use the above function to determine the
minimum memory required to run a command simpler than in the real test
run.  Use that limit plus a buffer there.  While at it, change to list
of commands in the subshell to fail also if the beginning `ulimit -v`
fails.
* tests/dd/no-allocate.sh: Likewise.
* tests/misc/csplit-heap.sh: Likewise.
* tests/misc/cut-huge-range.sh: Likewise.
* tests/misc/head-c.sh: Likewise.
* tests/misc/printf-surprise.sh: Likewise.
* tests/split/line-bytes.sh: Likewise.
* tests/rm/many-dir-entries-vs-OOM.sh: Likewise - doing it separately
for each program under test.

cfg.mk
init.cfg
tests/cp/link-heap.sh
tests/dd/no-allocate.sh
tests/misc/csplit-heap.sh
tests/misc/cut-huge-range.sh
tests/misc/head-c.sh
tests/misc/printf-surprise.sh
tests/rm/many-dir-entries-vs-OOM.sh
tests/split/line-bytes.sh

diff --git a/cfg.mk b/cfg.mk
index ace3f5a84eb648a89863ce64f79b4d79fa8ab755..d38a8dd6debef385e5ec80e62530275169dd6b34 100644 (file)
--- a/cfg.mk
+++ b/cfg.mk
@@ -503,12 +503,12 @@ sc_some_programs_must_avoid_exit_failure:
            && { echo '$(ME): do not use EXIT_FAILURE in the above'     \
                  1>&2; exit 1; } || :
 
-# Ensure that tests call the require_ulimit_v_ function if using ulimit -v
+# Ensure that tests call the get_min_ulimit_v_ function if using ulimit -v
 sc_prohibit_test_ulimit_without_require_:
-       @(git grep -l require_ulimit_v_ $(srcdir)/tests;                \
+       @(git grep -l get_min_ulimit_v_ $(srcdir)/tests;                \
          git grep -l 'ulimit -v' $(srcdir)/tests)                      \
          | sort | uniq -u | grep . && { echo "$(ME): the above test(s)"\
-         " should match require_ulimit_v_ with ulimit -v" 1>&2; exit 1; } || :
+         " should match get_min_ulimit_v_ with ulimit -v" 1>&2; exit 1; } || :
 
 # Ensure that tests call the cleanup_ function if using background processes
 sc_prohibit_test_background_without_cleanup_:
index 3beba5a9a1f426e963e735ac6b6b169589cf7359..f71f94c7a796add956574193381c3e38ad4a29bf 100644 (file)
--- a/init.cfg
+++ b/init.cfg
@@ -144,24 +144,26 @@ require_openat_support_()
   fi
 }
 
-require_ulimit_v_()
-{
-  local ulimit_works=yes
-  # Expect to be able to exec a program in 10MiB of virtual memory,
-  # (10MiB is usually plenty, but valgrind-wrapped date requires 19000KiB,
-  # so allow more in that case)
-  # but not in 20KiB.  I chose "date".  It must not be a shell built-in
-  # function, so you can't use echo, printf, true, etc.
-  # Of course, in coreutils, I could use $top_builddir/src/true,
-  # but this should be able to work for other projects, too.
+# Determine the minimum required VM limit to run the given command.
+# Output that value to stdout ... to be used by the caller.
+# Return 0 in case of success, and a non-Zero value otherwise.
+get_min_ulimit_v_()
+{
   local vm
-  case $(printenv LD_PRELOAD) in */valgrind/*) vm=22000;; *) vm=10000;; esac
-
-  ( ulimit -v $vm; date ) > /dev/null 2>&1 || ulimit_works=no
-  ( ulimit -v 20;  date ) > /dev/null 2>&1 && ulimit_works=no
-
-  test $ulimit_works = no \
-    && skip_ "this shell lacks ulimit support"
+  for v in $( seq 5000 5000 50000 ); do
+    if ( ulimit -v $v && "$@" ) >/dev/null; then
+      local vm_prev
+      prev_v=$v
+      for v in $( seq $(($prev_v-1000)) -1000 1000 ); do
+        ( ulimit -v $v && "$@" ) >/dev/null \
+          || { echo $prev_v; return 0; }
+        prev_v=$v
+      done
+    fi
+  done
+  # The above did not find a working limit.  Echo a very small number - just
+  # in case the caller does not handle the non-Zero return value.
+  echo 1; return 1
 }
 
 require_readable_root_()
index 901a92fc70d7f440bb5e9b1ebc45e51f73be858a..b65223a59b02174f01b358c0c6d653292f24d36b 100755 (executable)
 . "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
 print_ver_ cp
 expensive_
-require_ulimit_v_
+
+# Determine basic amount of memory needed for 'cp -al'.
+touch f || framework_failure_
+vm=$(get_min_ulimit_v_ cp -al f f2) \
+  || skip_ "this shell lacks ulimit support"
+rm f f2 || framework_failure_
 
 a=$(printf %031d 0)
 b=$(printf %031d 1)
@@ -31,7 +36,7 @@ cp -al $a $b || framework_failure_
 mkdir e || framework_failure_
 mv $a $b e || framework_failure_
 
-# Increased from 20000 to 22000 in 2012, for pre-F18 rawhide.
-(ulimit -v 22000; cp -al e f) || fail=1
+# Allow cp(1) to use 4MiB more virtual memory than for the above trivial case.
+(ulimit -v $(($vm+4000)) && cp -al e f) || fail=1
 
 Exit $fail
index 99e0542ac9b52927052e2df413f8ee982d698eb4..d122e354459441e6ab38fec59da783ca2bb27bca 100755 (executable)
 
 . "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
 print_ver_ dd
-require_ulimit_v_
+
+# Determine basic amount of memory needed.
+echo . > f || framework_failure_
+vm=$(get_min_ulimit_v_ dd if=f of=f2 status=none) \
+  || skip_ "this shell lacks ulimit support"
+rm -f f || framework_failure_
 
 # count and skip are zero, we don't need to allocate memory
-(ulimit -v 20000; dd  bs=30M count=0) || fail=1
-(ulimit -v 20000; dd ibs=30M count=0) || fail=1
-(ulimit -v 20000; dd obs=30M count=0) || fail=1
+(ulimit -v $vm && dd  bs=30M count=0) || fail=1
+(ulimit -v $vm && dd ibs=30M count=0) || fail=1
+(ulimit -v $vm && dd obs=30M count=0) || fail=1
 
 check_dd_seek_alloc() {
   local file="$1"
@@ -38,7 +43,8 @@ check_dd_seek_alloc() {
   timeout 10 dd count=1 if=/dev/zero of=tape&
 
   # Allocate buffer and read from the "tape"
-  (ulimit -v 20000; timeout 10 dd $dd_buf=30M $dd_op=1 count=0 $dd_file=tape)
+  (ulimit -v $(($vm+4000)) \
+    && timeout 10 dd $dd_buf=30M $dd_op=1 count=0 $dd_file=tape)
   local ret=$?
 
   # Be defensive in case the tape reader is blocked for some reason
index 9e51cb15fa4dfeb083cee62c5b5445703dc126d1..0ffe70ad42ec0f804b04e4e60bc7ec5f9cea95bf 100755 (executable)
 . "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
 print_ver_ csplit
 
-require_ulimit_v_
+# Determine basic amount of memory needed.
+{ echo y; echo n; } > f || framework_failure_
+vm=$(get_min_ulimit_v_ csplit -z f %n%1) \
+  || skip_ "this shell lacks ulimit support"
 
 (
- ulimit -v 20000
- { yes | head -n2500000; echo n; } | csplit -z - %n%1
+ ulimit -v $vm \
  && { yes | head -n2500000; echo n; } | csplit -z - %n%1
 ) || fail=1
 
 Exit $fail
index 035d8d0875224566ed8b262d472309cf0b870537..4df2fc06634b2a5740928c7f5a89cfb8d974626a 100755 (executable)
 
 . "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
 print_ver_ cut
-require_ulimit_v_
 getlimits_
 
+vm=$(get_min_ulimit_v_ cut -b1 /dev/null) \
+  || skip_ "this shell lacks ulimit support"
+
 # sed script to subtract one from the input.
 # Each input line should consist of a positive decimal number.
 # Each output line's number is one less than the input's.
@@ -49,15 +51,15 @@ CUT_MAX=$(echo $SIZE_MAX | sed "$subtract_one")
 
 # From coreutils-8.10 through 8.20, this would make cut try to allocate
 # a 256MiB bit vector.  With a 20MB limit on VM, the following would fail.
-(ulimit -v 20000; : | cut -b$CUT_MAX- > err 2>&1) || fail=1
+(ulimit -v $vm && : | cut -b$CUT_MAX- > err 2>&1) || fail=1
 
 # Up to and including coreutils-8.21, cut would allocate possibly needed
 # memory upfront.  Subsequently extra memory is no longer needed.
-(ulimit -v 20000; : | cut -b1-$CUT_MAX >> err 2>&1) || fail=1
+(ulimit -v $vm && : | cut -b1-$CUT_MAX >> err 2>&1) || fail=1
 
 # Explicitly disallow values above CUT_MAX
-(ulimit -v 20000; : | returns_ 1 cut -b$SIZE_MAX 2>/dev/null) || fail=1
-(ulimit -v 20000; : | returns_ 1 cut -b$SIZE_OFLOW 2>/dev/null) || fail=1
+(ulimit -v $vm && : | returns_ 1 cut -b$SIZE_MAX 2>/dev/null) || fail=1
+(ulimit -v $vm && : | returns_ 1 cut -b$SIZE_OFLOW 2>/dev/null) || fail=1
 
 compare /dev/null err || fail=1
 
index a553a55bcf16aa036b90f00bb00dbd95e8e0bb7f..ab821ac61a51ee2b5b767d0296003098a26e72ec 100755 (executable)
 
 . "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
 print_ver_ head
-require_ulimit_v_
 getlimits_
 
+vm=$(get_min_ulimit_v_ head -c1 /dev/null) \
+  || skip_ "this shell lacks ulimit support"
+
 # exercise the fix of 2001-08-18, based on test case from Ian Bruce
 echo abc > in || framework_failure_
 (head -c1; head -c1) < in > out || fail=1
@@ -40,7 +42,7 @@ esac
 # Only allocate memory as needed.
 # Coreutils <= 8.21 would allocate memory up front
 # based on the value passed to -c
-(ulimit -v 20000; head --bytes=-$SSIZE_MAX < /dev/null) || fail=1
+(ulimit -v $vm && head --bytes=-$SSIZE_MAX < /dev/null) || fail=1
 
 # Make sure it works on funny files in /proc and /sys.
 
index 20e6b0988ceaa345f00ec36df30236de2ad6e406..8480693fc4ea1081275f523cdfce661c48b56dae 100755 (executable)
@@ -20,8 +20,9 @@ prog=printf
 
 . "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
 print_ver_ printf
-require_ulimit_v_
 
+vm=$(get_min_ulimit_v_ env $prog %20f 0) \
+  || skip_ "this shell lacks ulimit support"
 
 # Up to coreutils-6.9, "printf %.Nf 0" would encounter an ENOMEM internal
 # error from glibc's printf(3) function whenever N was large relative to
@@ -60,7 +61,7 @@ head -c 10 fifo > out & pid=$!
 
 # Choosing the virtual memory limit, 11000 is enough, but 10000 is too
 # little and provokes a "memory exhausted" diagnostic on FreeBSD 9.0-p3.
-( ulimit -v 15000; env $prog %20000000f 0 2>err-msg > fifo )
+( ulimit -v $vm && env $prog %20000000f 0 2>err-msg > fifo )
 exit=$?
 
 # Map this longer, and rarer, diagnostic to the common one.
index e9c95d5d5a535024bfe7c7c663e9c0764a3ca1e6..8cc839bd1113654f22eec04f76863acc1ceeed72 100755 (executable)
 
 . "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
 print_ver_ rm du chmod
-require_ulimit_v_
-
 expensive_
 
+mkdir d2 \
+  && touch d2/f || framework_failure_
+
+# Restrict memory.  Each of these coreutils-8.12 programs would fail
+# with a diagnostic like "rm: fts_read failed: Cannot allocate memory".
+vm=$(get_min_ulimit_v_ du -sh d2) \
+  || skip_ "this shell lacks ulimit support"
+
 # With many files in a single directory...
-mkdir d && cd d || framework_failure_
-seq 200000|xargs touch || framework_failure_
+mkdir d || framework_failure_
+seq --format="d/%06g" 200000 | xargs touch || framework_failure_
 
-cd ..
+# Allow 35MiB more memory as for the trivial case above.
+(ulimit -v $(($vm + 35000)) && du -sh d) || fail=1
 
-# Restricted to 40MB, each of these coreutils-8.12 programs would fail
-# with a diagnostic like "rm: fts_read failed: Cannot allocate memory".
-ulimit -v 40000
-du -sh d || fail=1
-chmod -R 700 d || fail=1
-rm -rf d || fail=1
+vm=$(get_min_ulimit_v_ chmod -R 700 d2) \
+  || skip_ "this shell lacks ulimit support"
+(ulimit -v $(($vm + 35000)) && chmod -R 700 d) || fail=1
+
+vm=$(get_min_ulimit_v_ rm -rf d2) \
+  || skip_ "this shell lacks ulimit support"
+(ulimit -v $(($vm + 35000)) && rm -rf d) || fail=1
 
 Exit $fail
index 6e2f1376538620a5c89f168df24ea9c8c0847416..f2f0f8fd0d954e532620f110bfcc09198dd68f86 100755 (executable)
 
 . "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
 print_ver_ split
-require_ulimit_v_
 
+vm=$(get_min_ulimit_v_ split -C 'K' /dev/null) \
+  || skip_ "this shell lacks ulimit support"
 
 # Ensure memory is not allocated up front
-(ulimit -v 20000; split -C 'G' /dev/null) || fail=1
+(ulimit -v $vm && split -C 'G' /dev/null) || fail=1
 
 
 # Ensure correct operation with various split and buffer size combinations