ZEEK-ONLY: avoid initialization of every calloc'd int in fd_map#2
ZEEK-ONLY: avoid initialization of every calloc'd int in fd_map#2ckreibich wants to merge 1 commit intozeek-5.2.0-dev.51from
Conversation
libkqueue allocates the the number of available file descriptors for the fd_map array in linux/platform.c to. That limit is vast in Docker, for complicated reasons: # ulimit -n 1073741816 The library allocates arrays of that size in several places, but in only one does it then also _initialize_ every member of the array (to -1). Since calloc/mmap are optimized to allocate lazily, this means that only that instance winds up immediately actually using ~4GB in memory. The initialization of the int array uses -1 to indicate that a field isn't in use. The patch changes this to use 0 as unused, simply bumping the stored fds by one upon insert and decrementing by one on access, via helpers.
It should just point at I'm confused by how this works though. It's still calling |
|
Will do, Tim. The The +1/-1 is just so the surrounding checks for -1 (there's only one) keep working. I think one could go to town on all of this code a bunch more (given there's a separate array implementing a use count, but thankfully keeping the default to 0), and I actually started out implementing an abstraction that initially allocates a small chunk and then reallocs as needed as larger fds come in. But this had me thinking it's not actually needed. |
|
Closing in favor of #3 so it's an isolated change on top of master. |
I keep running into the problem of Zeek gobbling up gigabytes of memory in Docker due to libkqueue's over-eager array allocations of the maximum number of file descriptors. This change is the smallest I could come up with to avoid that problem, simply by avoiding initialization of every int in a
calloc()'d array.Memory use of a regular Zeek in Docker, per htop (note resident memory):
Memory use of a build with this patch in Docker:
Memory use outside of Docker, on the same system (with ulimit of 1024 fds):
See commit for details. @timwoj I wasn't quite sure where to point the merge at, does this work for you?