Feature #9523
Updated by Jeff Lucovsky about 7 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: https://github.com/OISF/suricata/pull/16867, 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 the worst-case memory the configuration can reach and exits before any packet is processed. Worst case means the memory in use after startup, every memcap full, the detection engine counted twice and a second set of the workers' detection contexts, since a rule reload builds both before it frees the old ones. The same data is written to memory-analysis.json. Example with a pcap, the ET Pro rules and 16 CPUs, with some notes shortened: <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 MiB, ...) threads 219.96 MiB (17 packet threads: RX 1 W 16; ...) detection thread contexts 109.72 MiB (16 x 6.86 MiB, ...) capture buffers 0 B flow.memcap 128.00 MiB (6.82 MiB in use) ... headroom 226.27 MiB total 2.43 GiB per packet thread 12.94 MiB (...) </pre> 2. A resources.memory.limit setting (or --memory-limit) and a resources.memory.headroom setting, as a size or a percentage. Suricata checks the estimate before the rules load and again after the threads start, and exits, printing the estimate, when it is larger than the limit. With a limit set, an unlimited stream, stream.reassembly, http or ftp memcap also stops startup. resources.memory.report-only (or --memory-report) turns both into warnings. -T prints the estimate as well. 3. A resources 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: <pre> "resources": { "memory": { "limit": 8589934592, "estimate": { "worst_case": 2557653168, "headroom": 232513920 }, "rss": { "current": 937623552, "peak": 937627648 }, "cgroup": { "current": 1342177280, "peak": 1610612736, "limit": 4294967296, "events": { "high": 0, "max": 0, "oom_kill": 0 } }, "heap": { "in_use": 904899008, "startup": 870803168, "uncapped": 397040, "uncapped_peak": 397040 }, "memcaps": { "in_use": 33698800, "peak": 33698800, "total": 809500672, "flow": { "in_use": 7154304, "peak": 7154304, "total": 134217728 }, "reassembly": { "in_use": 1835008, "peak": 1835008, "total": 268435456 } }, "capture_buffers": 0 } } </pre> heap.startup is the memory allocated outside the memcaps by the time the threads start, and heap.uncapped is what has grown outside the memcaps since then; heap.in_use is heap.startup plus memcaps.in_use plus heap.uncapped. The peaks show whether a memcap is too large, and the memcap drop counters whether it 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. The heap 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 and --memory-analysis work either way. In unix-socket mode the counters tied to the startup check are left out. 4. A user guide page on sizing a sensor with these (performance/memory.rst). 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.