[php-src] PR #18960: fpm: calculate accurate process counts from states during status generation
| From: | sylvesterdamgaard | Date: | Fri, 27 Jun 2025 12:45:39 +0000 |
| Subject: | [php-src] PR #18960: fpm: calculate accurate process counts from states during status generation | ||
| Groups: | php.git-pulls | ||
| Request: | Send a blank email to git-pulls+get-32219@lists.php.net to get a copy of this message | ||
Pull Request: https://github.com/php/php-src/pull/18960
Author: sylvesterdamgaard
# Fix PHP-FPM process count discrepancy in status endpoint
## Problem
The PHP-FPM status endpoint reports inaccurate
active processes and idle
processes counts that don't match the actual process states. This causes significant
issues for:
- **Monitoring tools** receiving incorrect metrics
- **Autoscaling systems** making poor decisions based on false data
- **Load balancers** routing incorrectly due to inaccurate capacity information
- **Operations teams** unable to trust FPM status for diagnostics
### Example Discrepancy
Replace unreliable scoreboard counters with counts calculated from actual process states. Since
status endpoint already locks and reads all processes, this adds zero performance overhead while
ensuring 100% accurate metrics.
Resolves monitoring discrepancies where active processes were incorrectly reported as significantly
lower than actual non-idle processes under load.
## Root Cause Analysis
The issue stems from **race conditions in scoreboard counter updates**, not the process states
themselves:
1. **Scoreboard counters are derived data** - updated via fpm_scoreboard_update() from
multiple locations
2. **Process states are source of truth** - maintained individually per process under locks
3. **Race conditions occur** when concurrent processes update shared counters during state
transitions
4. **Timing windows exist** where counters become inconsistent with actual states
Investigation of fpm_scoreboard.c shows that scoreboard counters are **not calculated
from process states** but rather maintained as separate values that can drift due to concurrent
updates.
## Solution
Calculate active processes and idle processes directly from actual process
states during status generation, instead of relying on potentially inconsistent scoreboard counters.
### Implementation Details
The fix leverages the existing process acquisition loop in fpm_status_export_to_zval():
```c
int calculated_active = 0, calculated_idle = 0;
for(i=0; i<scoreboard.nprocs; i++) {
proc_p = fpm_scoreboard_proc_acquire(scoreboard_p, i, 1);
if (!proc_p) continue;
// Count states while process is locked
if (proc_p->used) {
if (proc_p->request_stage == FPM_REQUEST_ACCEPTING) {
calculated_idle++;
} else {
calculated_active++;
}
}
procs[i] = *proc_p;
fpm_scoreboard_proc_release(proc_p);
}
// Use calculated values instead of scoreboard counters
add_assoc_long(status, "idle-processes", calculated_idle);
add_assoc_long(status, "active-processes", calculated_active);
add_assoc_long(status, "total-processes", calculated_idle + calculated_active);
```
Fixes #18956