Feature #9523
Updated by Jeff Lucovsky about 8 hours ago
When sizing a sensor or a container for Suricata, there is no way to find out how much memory a given configuration needs. Each subsystem has its own memcap, but nothing adds them up, and a large part of Suricata's memory has no memcap at all: * the detection engine. A rule reload builds and loads a second engine while the first is still in use, and frees the first only after the threads switch (src/detect-engine.c:5275-5329), so during a reload Suricata needs twice that memory. * the packet pool each packet thread preallocates: max-pending-packets packets per thread (src/tm-threads.c:218, src/tmqh-packetpool.c:253). * per-thread detection buffers, and capture buffers such as AF_PACKET rings. The number of threads is also hard to predict from the configuration. With AF_PACKET threads set to auto and cluster_flow, Suricata creates one thread per online CPU (src/runmode-af-packet.c:677). The threads setting under worker-cpu-set caps the worker count (src/tm-threads.c:2515-2518, src/util-runmodes.c:256), but the worker core list only decides which cores the threads run on, and the whole cpu-affinity section is ignored unless set-cpu-affinity is yes (src/runmodes.c:963-964). On a 16-CPU machine, an autofp run with a worker core list of [ "2-4" ] and no threads setting created 16 worker threads on those 3 cores. Some memcaps have no limit by default. app-layer.protocols.http.memcap is 0, meaning unlimited, unless it is set (src/app-layer-htp-mem.c:64, 102). While Suricata runs, the stats event has memuse and memcap counters per subsystem (for example src/app-layer.c:1170-1180), but no total for the process, and nothing that separates memory a memcap limits from memory that can keep growing. h3. Proposal PRs: suricata (to be opened from branch 9523/3), https://github.com/OISF/suricata-verify/pull/3642 1. A --memory-analysis option. It loads the configuration and rules, and when a capture source is given it also creates the threads and capture buffers the way a normal start would. It then prints reports the worst-case memory the configuration can reach reach, and exits before any packet is processed. Worst case means the memory in use after startup, every memcap full, and the detection engine counted twice and for a second set of reload. The report lists each part, the workers' detection contexts, since a rule reload builds both before it frees threads by type, and the old ones. memory per packet thread. The same data is written to memory-analysis.json. Example with a pcap, the ET Pro rules and 16 CPUs, with some notes shortened: CPUs: <pre> memory analysis (worst case, measured with heap statistics): process before setup 11.36 MiB setup outside memcaps 6.68 MiB detection engine 1.18 GiB (2 x 603.48 603.46 MiB, ...) a rule reload holds two engines) threads 219.96 219.02 MiB (17 packet threads: RX 1 W 16; ...) detection thread contexts 109.72 MiB (16 x 6.86 MiB, ...) 5 management threads: FM 1 FR 1 CW 1 CS 1 HB 1) capture buffers 0 B flow.memcap 128.00 memcaps (10) 772.00 MiB (6.82 MiB in use) ... headroom 226.27 221.60 MiB total 2.43 2.38 GiB per packet thread 12.94 12.88 MiB (...) </pre> 2. A resources.memory.limit setting (or --memory-limit) and a resources.memory.headroom setting, as a size or a percentage. At startup Suricata checks compares the estimate before with the rules load limit and again after the threads start, and exits, printing the estimate, when it is larger than the limit. does not fit. With a limit set, an a memcap left unlimited stream, stream.reassembly, http or ftp memcap also stops startup. resources.memory.report-only (or --memory-report) --memory-report turns both into warnings. -T prints the estimate as well. 3. A resources resources.memory section in the stats event. resources.memory reports the limit and estimate, RSS, cgroup v2 memory use and events, capture buffers, and in_use, peak and total for each memcap: event: <pre> "resources": { "memory": { "limit": 8589934592, 10737418240, "estimate": { "worst_case": 2557653168, "estimate_worst_case": 2555890894, "headroom": 232513920 }, 232353710, "cgroup_current": 35352342528, "cgroup_peak": 36486725632, "rss": { "current": 937623552, "peak": 937627648 }, 937508864, "cgroup": { "current": 1342177280, "peak": 1610612736, "limit": 4294967296, "events": { "high": 0, "max": 0, "oom_kill": 0 } "rss_peak": 937508864, }, "heap_in_use": 903793328, "heap": { "in_use": 904899008, "startup": 870803168, "uncapped": 397040, "uncapped_peak": 397040 "fixed": 869701712, }, "memcap_bound": 33698800, "memcaps": { "in_use": 33698800, "peak": 33698800, "total": "memcap_total": 809500672, "flow": { "in_use": 7154304, "peak": 7154304, "total": 134217728 }, "reassembly": { "in_use": 1835008, "peak": 1835008, "total": 268435456 } }, "capture_buffers": 0 "unbounded": 392816 } } </pre> heap.startup fixed is the memory allocated set up at startup outside the memcaps by memcaps, memcap_bound is the time memory under the threads start, memcaps, and heap.uncapped unbounded is what has grown outside the memcaps since then; heap.in_use is heap.startup plus memcaps.in_use plus heap.uncapped. startup. The peaks show whether a memcap is too large, and three add up to heap_in_use. Heap figures come from glibc, jemalloc or tcmalloc, whichever provides malloc; the memcap drop counters whether it counter is too small. resources.cpu reports the CPUs Suricata may use, the packet threads and the cores they may run on, the cores in use and the busiest packet thread's use with their peaks, user and system CPU time, and the cgroup's CPU quota, usage and throttling. resources.enabled (default yes) turns the whole section off. left out for other allocators. The heap cgroup counters need resources.memory.heap: yes, because reading the heap with glibc locks every malloc arena: 68 ms per stats write on a sensor with 16 worker threads. The limit check come from cgroup v2 memory.current and --memory-analysis work either way. In unix-socket mode memory.peak when the counters tied to process's cgroup has them. (These cgroup values are large because the startup check are left out. 4. A user guide page on sizing test run shared a sensor cgroup with these (performance/memory.rst). other processes.) Related: #845 added the per-subsystem memuse counters this builds on. #2316 (a single global memcap) is a different approach to the same problem. Reading the FTP memcap depends on the getter fixed in #8880. This is a feature for main.