【问题标题】:How to detect if PHP JIT is enabled如何检测是否启用了 PHP JIT
【发布时间】:2020-10-20 13:50:38
【问题描述】:

检测 PHP 是否使用 JIT 编译并从运行脚本启用 JIT 的最简单方法是什么?

【问题讨论】:

  • 所以您想要一种方法来检查您的脚本是否已被编译或解释?
  • 理想情况下两者都有,但至少在被解释的情况下。

标签: php jit php-8


【解决方案1】:

可以直接调用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

来源:https://wiki.php.net/rfc/jit#phpini_defaults

【讨论】:

  • 它似乎不起作用 - var_dump(ini_get("opcache.jit")); 返回 string(4) "1205" 用于 JIT 和没有 JIT,使用 3v4l.org/Q366F 进行测试
  • @mvorisek 我已经更新了我的答案。我猜 opcache_get_status() 函数就是你要找的。​​span>
  • 谢谢,下面的作品:opcache_get_status()["jit"]['enabled'] ?? false
【解决方案2】:

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

【讨论】:

    【解决方案3】:
    php -i | grep "opcache"
    
    checking:
    opcache.enable => On => On
    opcache.enable_cli => On => On
    opcache.jit => tracing => tracing
    

    【讨论】:

    • 请更新您的答案,提供有关您的代码的一些详细信息以及它如何回答问题。
    猜你喜欢
    • 2011-08-02
    • 2012-03-15
    • 2013-01-30
    • 1970-01-01
    • 2011-03-09
    • 1970-01-01
    • 1970-01-01
    • 2012-11-27
    相关资源
    最近更新 更多