opcache: scripts loaded from file_cache report memory_consumption 0 (and add nothing to wasted_memory) if their .bin was written while SHM was full

未關閉
#25,555 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

維護者通常 1 天內回覆

還沒有人認領這個 Issue。

評估

難度
4/5
預估耗時
3-5 天
新手友好度
48/100
Issue 類型
缺陷
描述清晰度
基本清楚
活躍度
活躍
技術堆疊
c, php

研究方向

Start by reading zend_file_cache_script_load() and how dynamic_members.memory_consumption is restored when a cached script is loaded into SHM. Use the reproduction commands in the issue to create .bin files while SHM is full, then load them with more memory and check opcache_get_status() and invalidation's wasted_memory. Done means loaded scripts report their allocated memory and invalidation accounts for it.

由索引模型根據 Issue 內容生成。

描述

Bug Status: Needs Triage
Description
Description

With opcache.file_cache enabled, a script that cannot be stored in shared memory (SHM full) is compiled and written to the file cache only. Its .bin then carries memory_consumption = 0. When a later process with free SHM loads that .bin into shared memory, the entry keeps reporting memory_consumption: 0 in opcache_get_status(), although it occupies real shared memory.

Two effects:

  1. opcache_get_status()['scripts'] under-reports. On a production server (PHP 8.5.4, FPM, memory_consumption=2560), 42,535 of 54,864 cached scripts report 0, and the per-script sum is 419 MB while used_memory is 2,558 MB.
  2. opcache_invalidate() (and timestamp-based invalidation) of such a script adds 0 to wasted_memory. Waste is under-counted, max_wasted_percentage is never reached, and a full cache does not schedule its restart. On the server above, the cache stayed full at 0.07% wasted while files were being updated.

A .bin written with opcache.file_cache_only=1 has the same effect.

Reproduce
<?php
// repro.php
$dir = __DIR__ . '/src';
if (!is_dir($dir)) {
    mkdir($dir);
    for ($i = 0; $i < 200; $i++) {
        $body = "<?php\nfunction f$i() { return [" . implode(',', array_map(fn($n) => "'k{$i}_$n' => $n", range(1, 2000))) . "]; }\n";
        file_put_contents("$dir/s$i.php", $body);
    }
}
foreach (glob("$dir/*.php") as $f) opcache_compile_file($f);
$s = opcache_get_status(true);
$zero = array_keys(array_filter($s['scripts'], fn($x) => $x['memory_consumption'] === 0));
printf("cache_full=%s cached=%d zero=%d\n", var_export($s['cache_full'], true), count($s['scripts']), count($zero));
if ($zero) {
    $w0 = opcache_get_status(false)['memory_usage']['wasted_memory'];
    foreach ($zero as $k) opcache_invalidate($k, true);
    printf("invalidating the %d zero-size scripts added %d bytes to wasted_memory\n", count($zero), opcache_get_status(false)['memory_usage']['wasted_memory'] - $w0);
}
mkdir fc
O="-n -d opcache.enable_cli=1 -d opcache.file_cache=$PWD/fc -d opcache.interned_strings_buffer=1"
php $O -d opcache.memory_consumption=8   repro.php   # pass 1: SHM too small, all .bin files written
php $O -d opcache.memory_consumption=128 repro.php   # pass 2: everything fits, loaded from file_cache

Output:

cache_full=false cached=45 zero=0
cache_full=false cached=201 zero=156
invalidating the 156 zero-size scripts added 0 bytes to wasted_memory

Expected: pass 2 reports zero=0, and invalidation adds the scripts' real size to wasted_memory.

Control: running both passes with 128 MB and a fresh file_cache directory gives zero=0 both times, and invalidating a sized script adds its full size to wasted_memory.

Possible cause (not verified against the source)

It looks as if, when a .bin is loaded into SHM in zend_file_cache_script_load(), dynamic_members.memory_consumption is taken from the stored script and never set from the size actually allocated. A script that was never persisted to SHM was stored with 0. Setting it from the allocated size (e.g. ZEND_ALIGNED_SIZE(script->size)) on the SHM path would fix both effects.

PHP Version
PHP 8.5.4 (cli) (built: Oct 19 2026 14:35:18) (NTS)
Operating System

Ubuntu 26.04

主要語言
C
星號
41.8k
分支
8.6k
平均合併
2 天 15 小時
30 天內合併 PR
125

環境準備

  • 沒有 Dockerfile 或 Docker Compose 檔案
  • 沒有 Pull Request 範本
  • 閱讀貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

php/php-src 的其他 Issue

查看 php/php-src 的全部 Issue

相似的 Issue

更多 C Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。