I'm trying to benchmark and figure out the correct PHP-FPM pm.max_children
settings.
I'm using the following formula to work out the average size of a PHP-FPM process:
ps -ylC php-fpm --sort:rss | awk '{sum+=$8; ++n} END {print "Tot="sum"("n")";print "Avg="sum"/"n"="sum/n/1024"MB"}'
This returns me 31MB. I've only got about 2GB to play with on the system in question - so theorotically if I change pm.max_children
inside the PHP-FPM config file to be 2048MB / 31MB = 66.
I then used ab
to benchmark it - I quickly hit the pm.max_children
limit ([06-Oct-2021 10:32:37] WARNING: [pool www] server reached pm.max_children setting (66), consider raising it
) but I can see I've got plenty of memory to spare.
When checking with free:
# free -m
total used free shared buff/cache available
Mem: 1954 914 756 15 284 869
Swap: 0 0 0
So then I pm.max_children
to 200 - to test further and again hit the limit:
[06-Oct-2021 10:34:46] WARNING: [pool www] server reached pm.max_children setting (200), consider raising it
Memory usage via free is lower but still not what I'd expect based on the calculation:
# free -m
total used free shared buff/cache available
Mem: 1954 1595 74 15 285 188
Swap: 0 0 0
Going off the calculation above, if each process was using 31MB as shown in the RSS column:
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
apache 17558 0.3 1.5 859324 31820 ? R 10:16 0:02 php-fpm: pool www
apache 17577 0.3 1.5 859468 31848 ? R 10:16 0:02 php-fpm: pool www
apache 17603 0.3 1.5 859464 31836 ? R 10:16 0:02 php-fpm: pool www
The number of PHP-FPM processes is:
ps aux | grep php-fpm | grep -v grep | wc -l
= 200
And if I multiply the value in RSS (which is average 31000KB) * 200 = ~ 6GB memory.
However if I use the ps_mem.py
utility, the overall usage of PHP-FPM is a lot lower:
882.4 MiB + 26.9 MiB = 909.4 MiB php-fpm (201)
What am I missing here in terms of working out the correct value?
It seems as though the advice to utilise available memory / average php-fpm process memory (as calculated by the ps
command above) gives inconsistent results..
For reference here are some articles recommending this method to calculate pm.max_children
: