ubuntips

PGO for php-fpm is now on by default

Draft posted on · 8 min read

On this page
Draft commented on Dec 2, 2026

Today started with three PGO posts. On the Xeon, PGO on top of the BOLT build my setup script already ships added about 1.5 %, too little on its own to pass my default rule. On an AMD EPYC with Zen 1, where BOLT can’t run, PGO was the only layout optimisation and was worth +6 to +9 %. The follow-up run on the same CPU confirmed +6.1 to +8.4 % with clean training, and showed that the PHP CLI must not get the profile.

Taken together, that was enough for the decision: PGO is now on by default for php-fpm, on every CPU. It ships in 1.22.48. This post covers how it works, the bug the first live run found, and what it measured when the real code path ran on the Xeon.

Short version: +1.0 to +1.5 % on every PHP tier over the BOLT build it replaced, no tier slower, and member pages gained even though training only ever saw guest pages.

Why it’s a default#

My rule for a default is at least 2 % faster somewhere and slower nowhere, measured on Intel and AMD. Neither machine alone made a clean case:

  • Xeon E-2236 (has LBR): PGO + BOLT was about +1.5 % over BOLT alone in the earlier manual runs (+0.9 to +2.0 % in one five-round run, -1.1 to +2.4 % in the four-arm run, where the member-home dip sat inside overlapping ranges). Below the bar, and not cleanly slower-nowhere either.
  • EPYC 7401P (Zen 1, no LBR): PGO alone was +6.1 to +8.4 % and won all 20 tier/round comparisons. Far above the bar, but only for php-fpm: the no-JIT CLI lost 4.8 % on bench.php with a profile.

So the default is shaped by both results: PGO for php-fpm everywhere, never for the CLI, and BOLT on top only where the CPU has branch records. In the manual runs that combination was clearly faster on Zen 1 and a small, mostly positive step on the Xeon. Whether it is slower nowhere on the Xeon is what the shipped code path had to show, below.

How it works#

Each --optimize checks whether the active PHP version is due for a PGO pass. If it is, the pass runs after the normal build and before BOLT:

  1. An instrumented build of PHP with -fprofile-generate, in a fixed directory (static functions are named in the profile by their source path, so the training build and the final build must compile in the same place).
  2. A training run on the host’s own sites, described below.
  3. llvm-profdata merge of the raw profiles.
  4. A profile-use build of php-fpm (and, before PHP 8.5, opcache.so) with -fprofile-use.
  5. The same checks as every build, plus a pair check against the live CLI: one PHP version, no profile counters left in the binary, and php-fpm -t on the live config.
  6. A swap of php-fpm (and, before 8.5, opcache.so), file by file with the old ones kept as .prev. A stop signal waits until every file is in place, and if php-fpm doesn’t come back up, the previous build is put back. bin/php is never touched.

Then, where the CPU has LBR, BOLT rewrites the PGO php-fpm from 60 s of live traffic, as it did before. Where it doesn’t (AMD Zen 1 to 3 and most VMs), PGO is the only layout optimisation. Sampled BOLT without branch records is never used: on Zen 1 it made the PGO build 1.9 to 4.4 % slower.

The cost is about two PHP-only compiles plus the crawl: 10-15 minutes on the Xeon, more on a small VM. The builds run at low CPU and I/O priority, and the running php-fpm serves throughout.

The training never touches the live stack#

Training needs traffic, and I didn’t want a synthetic workload. The Zen 1 runs showed that training on the wrong work makes the code it misses slower. So the script trains on the host’s own sites, but through a private copy of the serving path:

  • A temporary php-fpm master per live pool, running as that pool’s user, with one static worker and copies of the live php.ini and conf.d. One worker, because twelve workers merging into one profile corrupted it in the Xeon experiment.
  • A temporary nginx (the script’s own binary) on random 127.0.0.1 ports, plain HTTP, no cache. Each site gets its own port, so a home page that redirects to a proxy’s public name is still crawled on the right docroot.
  • A bounded guest crawl: the pages each home page links to, two levels deep, at most 300 URLs per site, GET only, no cookies, one request at a time, about 3 minutes in all. It never requests a login, admin, account, install, search, upload, API, token, cart, checkout or other action URL.
  • Only sites guests can reach. A vhost that keeps anonymous visitors out with auth_basic, auth_request, deny all or satisfy is skipped, because the temporary nginx carries none of those rules. XenForo, WordPress and Chevereto sites are trained; any other PHP root only when I opt in, because a small admin tool’s GET links can act.

Everything lives under one directory in /run and is stopped and deleted afterwards. Nothing under /etc/php, /etc/nginx or /run/php is written, and a test checks that. The one visible trace is the one any guest leaves: the crawl bumps view counters and the online list in the live database.

When it doesn’t run, or fails#

  • No PHP site yet. A fresh server before --domain has nothing real to train on, so it records pgo=skipped:noapp and the next --optimize tries again. No try is counted.
  • Not enough memory or disk, or no nginx from the script: skipped and retried, no try counted.
  • Any failure in the builds, the training, the merge or the swap records pgo=failed:<stage> with the reason. The running build keeps serving, and the pass is retried at most twice per build.
  • A thin profile (under 1,000 functions or 200 good pages) isn’t used.
  • The opt-out is SETUP_PHP_PGO=0. It’s persisted, and the next --optimize rebuilds a plain php-fpm.

Existing hosts don’t need a rebuild to get it. A build without a PGO record is simply due, so each host trains at its next --optimize.

The bug the first live run found#

The unit tests passed and the first real pass on the Xeon test host looked fine: it crawled both test forums, about 87 % of the requests came back 2xx, and it wrote a 3.7 MB profile. Then it threw the profile away as “0 of 16462 functions profiled”.

The coverage check counted Function count: lines in the output of llvm-profdata show. LLVM 23 doesn’t print that line for an IR profile, so every profile read as empty. The check now counts functions with a non-zero block count, which every LLVM release prints, and the test stub prints LLVM 23’s real format instead of the one I assumed.

The fallback did what it was built for in the meantime. php-fpm kept the BOLT build, bin/php stayed byte-identical, the skip was recorded, and the retry cap held. A broken profile check cost one wasted pass, not a slower server.

The numbers#

After the fix, setup.sh --optimize ran the 1.22.48 code on the test host from start to finish. It did the instrumented build and a 184 s guest crawl of both test forums (11,853 of 13,550 requests 2xx). 4,233 of 16,462 functions were profiled. The profile-use build changed php-fpm only (bin/php byte-identical), and BOLT then ran from 60 s of LBR samples under the mixed load (2,153 functions).

The setup is the same as the earlier Xeon runs: a Xeon E-2236 with the clock pinned at 3.6 GHz, XenForo 2.3.14 on seeded data, 5 rounds with the order rotated. The baseline is the previous production build, BOLT only. Medians, with the slowest and fastest round in brackets:

Tierbefore (BOLT) req/sPGO + BOLT
guest home (uncached)407 [391-408]413 (+1.5 %) [411-418]
guest thread (uncached)307 [305-308]310 (+1.0 %) [308-311]
member home338 [336-340]343 (+1.5 %) [340-346]
member thread225 [223-229]228 (+1.3 %) [226-229]

The cached guest tiers never reach PHP. nginx serves them, and they moved -1.5 % and +0.6 % with overlapping ranges, which is noise.

Two things stand out. No PHP tier got slower, and the slowest PGO round beat the median of the old build on all four tiers. And the member pages gained as much as the guest pages, although the crawl never logged in. My guess is that a logged-in XenForo page runs mostly the same template, string and allocator code as a guest page, and that’s the code PGO lays out. I didn’t profile that.

The gain matches the earlier manual runs on this CPU, about +1.5 % on top of BOLT. On an Intel host, PGO is a small extra. Its real weight is on CPUs where BOLT can’t run.

What I didn’t measure#

  • The shipped code path on AMD. The +6.1 to +8.4 % on Zen 1 comes from the earlier manual runs, not from this feature. The EPYC is someone else’s development machine, and this feature crawls a host’s live sites, so I didn’t run it there. The method is the same (one worker, guest-only training, php-fpm only), but it’s a different harness.
  • Zen 2, 3 and 4. Not measured at all. Zen 2 and 3 take the PGO-only path. Zen 4 takes the PGO + BOLT path on bare metal with kernel 6.1 or later, and the PGO-only path otherwise.
  • WordPress and Chevereto. The crawl trains on them, but the benchmark is XenForo only.
  • The CLI. It’s the plain build, byte-identical to before, so cron jobs and imports run as they did. I didn’t time them again.
  • Long-term drift. The profile is recorded once per build. If a site’s traffic changes shape, the profile doesn’t follow it until the next build.

The numbers sit next to the earlier PGO runs in tests/stress-site/RESULTS.md in ubuntu-setup-script.

Back to top ↑