How to monitor RAM for tuning pm.max_children

How to monitor RAM usage:

  1. free -h:

    • This command shows your system's total, used, and free memory in a human-readable format.
    • Key metrics:
      • total: Total RAM.
      • used: RAM currently in use.
      • free: Unused RAM.
      • buff/cache: RAM used for file system buffers and page cache. This is good; Linux uses free RAM for this and frees it when applications need it.
      • available: The most important metric. This estimates how much memory is available for starting new applications without swapping.
    • Run it before and after: Run free -h before you increase max_children and then after your server has been running for a while under typical load with the new settings. Compare the available memory.
  2. htop (recommended if installed):

    • htop (you might need to sudo apt install htop) provides a real-time, interactive view of processes and resource usage, including a visual bar for memory usage.
    • It's excellent for seeing how much RAM each PHP-FPM process (php-fpm: pool ... processes) is consuming. You can estimate the memory per process and multiply by your new max_children to get a rough idea of the total.
  3. top:

    • Less user-friendly than htop, but still effective.
    • Look at the %MEM column to see memory usage per process.

Practical Steps for Increasing pm.max_children and Monitoring RAM:

  1. Determine Current PHP Process Memory Usage:
    • Run htop (or top).
    • Find a few php-fpm processes from the kartscode pool. Look at their RES (Resident Set Size) or VIRT (Virtual Memory Size) columns in top/htop. This is the actual memory they are consuming. Let's say, on average, a PHP-FPM process uses 50MB.
  2. Calculate Potential Max Memory:
    • Current max_children = 16. At 50MB/process, that's 16 * 50MB = 800MB for PHP processes.
    • If you increase max_children to 30, that would be 30 * 50MB = 1500MB (1.5GB).
  3. Check free -h: Look at your available memory. If you only have 500MB available, increasing to 30 processes (requiring 1.5GB) will cause severe swapping.
  4. Increment Gradually: Instead of a big jump, increase pm.max_children in small steps (e.g., from 16 to 20, then 20 to 24).
  5. Monitor: After each increase, let the server run under load for an hour or two, then check free -h and htop to see how memory is being utilized. Look for sustained low available memory or increasing swap usage.

Your 17002705575366718.conf settings:

pm = ondemand
pm.max_children = 16
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 8

 

The fpm.txt output showed max-children-reached => 10, but pm.max_children is set to 16. This indicates that the max_children limit you hit might be determined by the system-wide limits or another PHP-FPM configuration, or that max-children-reached refers to the number of actively processing children at a specific moment, not necessarily the pm.max_children limit itself. Given the total-processes of 16 in fpm.txt, it seems like your max_children is indeed 16, and you're hitting that. This explains why new requests would queue or drop if more than 16 concurrent PHP processes were needed.

Action to take with pm.max_children: Based on the fpm.txt showing total-processes: 16 and max-children-reached: 10, it strongly suggests you are hitting your concurrent process limit for active requests. Since pm.max_children is 16, this means all 16 are being spawned, but perhaps only 10 were 'active' in the sense of processing requests at the very peak snapshot.

Let's increase pm.max_children for this pool. Given your server has 2 CPUs, you have some room.

  • Edit /etc/php/8.2/fpm/pool.d/17002705575366718.conf
  • Change pm.max_children = 16 to something higher. A common starting point for a server with 2 CPUs (assuming adequate RAM) might be 20 or 25.
  • Save the file.
  • Reload PHP-FPM: sudo systemctl reload php8.2-fpm (or sudo systemctl reload php-fpm if your distro uses a generic name).

Before and after this change, use free -h to monitor RAM. If you have 4GB or more RAM, increasing to 20-25 processes is generally safe to test. If you have less (e.g., 2GB), be more cautious and increase by smaller increments.