https://bugs.kde.org/show_bug.cgi?id=529105

            Bug ID: 529105
           Summary: Large RLIMIT_NOFILE makes startup and every fork slow
                    (FreeBSD default with much RAM)
    Classification: Developer tools
           Product: valgrind
      Version First unspecified
       Reported In:
          Platform: Compiled Sources
                OS: FreeBSD
            Status: REPORTED
          Severity: normal
          Priority: NOR
         Component: general
          Assignee: [email protected]
          Reporter: [email protected]
  Target Milestone: ---

As mentioned in bug 528993: Valgrind moves its own file descriptors to
just below the RLIMIT_NOFILE soft limit (setup_file_descriptors() in
coregrind/m_main.c, VG_(safe_fd)() in coregrind/m_libcfile.c).  When
that limit is very large, every process running under Valgrind gets a
file descriptor table of that size.  Creating it makes startup slow,
every fork has to set up a table of the same size for the child, and it
takes a lot of kernel memory.

On FreeBSD the default limit is very large on any machine with a lot of
RAM.  kern.maxfiles defaults to 32 files per MB of physical memory and
kern.maxfilesperproc to 90% of that (sys/kern/subr_param.c), and the
default login class (openfiles=unlimited) gives every process
kern.maxfilesperproc as its RLIMIT_NOFILE.  That is about 470,000
descriptors per 16 GB of RAM; on the 512 GB POWER9 machine I measured
on, it is 15,098,238.

The F_DUPFD in VG_(safe_fd)() grows the descriptor table to about 15
million entries, and fork sizes the child's table by the highest open
descriptor (fdcopy() in sys/kern/kern_descrip.c), so each fork under
Valgrind (vfork too, since Valgrind turns it into fork) allocates the
same again.  While a process runs under Valgrind, vmstat -m shows about
690 MB in the "filedesc" malloc type, 48 bytes per descriptor.

Measured on FreeBSD 15.2-BETA1 powerpc64 (POWER9), with the FreeBSD
powerpc64 port from bug 528993, using the attached program (10 times
vfork + execl("/usr/bin/true") + waitpid):

                                         ulimit -n 15098238  ulimit -n 4096
  native, 10 x vfork+exec                     0.054 s           0.054 s
  valgrind --tool=none /usr/bin/true          2.80 s            0.47 s
  valgrind --tool=none, 10 x vfork+exec      11.66 s            0.13 s
  valgrind (memcheck), 10 x vfork+exec       10.06 s            0.17 s

So every fork takes about a second under Valgrind.  In the regression
tests, none/tests/async-sigs, which forks a child for each of its eight
cases, timed out.  Lowering the limit, e.g. "ulimit -n 4096", avoids
all of this.

Nothing here is specific to powerpc64: the table size, and with it the
cost, grows with the RAM of the machine on every FreeBSD architecture.
On a smaller machine it should reproduce after raising the limits
(kern.maxfiles and kern.maxfilesperproc can be set with sysctl at run
time, then ulimit -n).  Paul mentioned seeing similar problems with
Docker, where the limit is often very high as well; the "Private file
creation failed" message in VG_(safe_fd)() already suggests lowering it
there.

A possible fix would be to cap the descriptor range Valgrind reserves
for itself (at some fixed value, or with a command line option), and
lower the soft limit the client sees to match; the client already sees
it lowered by N_RESERVED_FDS.  Valgrind could also at least warn when
the limit is very large.

-- 
You are receiving this mail because:
You are watching all bug changes.

Reply via email to