Related posts
- Plain, BOLT, PGO or both on an AMD EPYC (Zen 1)
A second run on the Zen 1 machine, with four builds of PHP 8.6.0 and the first run's two caveats fixed. PGO alone was fastest at +6.1 to +8.4 % and won all 20 comparisons. BOLT from plain samples gave +4.3 to +5.0 % on its own but made PGO slower, and the CLI still lost 4.8 % on bench.php, so the CLI gets built without the profile.
- BOLT, PGO or both for PHP on a Xeon
I built PHP 8.5.13 four ways on the Xeon test host: plain, with BOLT, with clang PGO, and with both. BOLT and PGO each give about +7 % on uncached XenForo pages, but PGO on top of the BOLT build my setup script already ships adds only -1 to +2.4 %, so it stays out where BOLT runs.
- PGO where BOLT cannot run: PHP 8.6 on an AMD EPYC (Zen 1)
My setup script optimises PHP's code layout with BOLT, but BOLT needs branch records that older AMD CPUs such as Zen 1 don't have, so on those machines PHP gets no layout optimisation at all. Profile-guided optimisation trained on XenForo traffic filled the gap: +6 to +9 % requests per second on an EPYC 7401P, with one catch for the PHP CLI.
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:
bench.phpwith 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
--optimizechecks whether the active PHP version is due for a PGO pass. If it is, the pass runs after the normal build and before BOLT:-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).llvm-profdata mergeof the raw profiles.opcache.so) with-fprofile-use.php-fpm -ton the live config.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/phpis 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:
php.iniandconf.d. One worker, because twelve workers merging into one profile corrupted it in the Xeon experiment.127.0.0.1ports, 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.auth_basic,auth_request,deny allorsatisfyis 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
/runand is stopped and deleted afterwards. Nothing under/etc/php,/etc/nginxor/run/phpis 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#
--domainhas nothing real to train on, so it recordspgo=skipped:noappand the next--optimizetries again. No try is counted.pgo=failed:<stage>with the reason. The running build keeps serving, and the pass is retried at most twice per build.SETUP_PHP_PGO=0. It’s persisted, and the next--optimizerebuilds 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 ofllvm-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/phpstayed 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 --optimizeran 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/phpbyte-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:
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 numbers sit next to the earlier PGO runs in
tests/stress-site/RESULTS.mdin ubuntu-setup-script.