【发布时间】:2020-10-20 13:50:38
【问题描述】:
检测 PHP 是否使用 JIT 编译并从运行脚本启用 JIT 的最简单方法是什么?
【问题讨论】:
-
所以您想要一种方法来检查您的脚本是否已被编译或解释?
-
理想情况下两者都有,但至少在被解释的情况下。
检测 PHP 是否使用 JIT 编译并从运行脚本启用 JIT 的最简单方法是什么?
【问题讨论】:
可以直接调用opcache_get_status()查询opcache设置:
opcache_get_status()["jit"]["enabled"];
或在php.ini中执行查找:
ini_get("opcache.jit")
这是一个整数(以字符串形式返回),其中最后一位数字表示 JIT 的状态:
0 - don't JIT
1 - minimal JIT (call standard VM handlers)
2 - selective VM handler inlining
3 - optimized JIT based on static type inference of individual function
4 - optimized JIT based on static type inference and call tree
5 - optimized JIT based on static type inference and inner procedure analyses
【讨论】:
var_dump(ini_get("opcache.jit")); 返回 string(4) "1205" 用于 JIT 和没有 JIT,使用 3v4l.org/Q366F 进行测试
opcache_get_status()["jit"]['enabled'] ?? false
opcache_get_status() 在禁用 JIT 时将无法工作,并会引发致命错误。
echo (function_exists('opcache_get_status')
&& opcache_get_status()['jit']['enabled']) ? 'JIT enabled' : 'JIT disabled';
php.ini 文件中的 JIT 必须有以下设置。
zend_extension=opcache.so
opcache.enable=1
opcache.enable_cli=1 //optional, for CLI interpreter
opcache.jit_buffer_size=32M //default is 0, with value 0 JIT doesn't work
opcache.jit=1235 //optional, default is "tracing" which is equal to 1254
【讨论】:
php -i | grep "opcache"
checking:
opcache.enable => On => On
opcache.enable_cli => On => On
opcache.jit => tracing => tracing
【讨论】: