14

What is the simplest way to detect if PHP is compiled with JIT and JIT is enabled from the running script?

mvorisek
  • 3,290
  • 2
  • 18
  • 53

3 Answers3

8

You can query the opcache settings directly by calling opcache_get_status():

opcache_get_status()["jit"]["enabled"];

or perform a lookup in the php.ini:

ini_get("opcache.jit")

which is an integer (returned as string) where the last digit states the status of the 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

Source: https://wiki.php.net/rfc/jit#phpini_defaults

maio290
  • 6,440
  • 1
  • 21
  • 38
  • It seems it does not work - `var_dump(ini_get("opcache.jit"));` returns `string(4) "1205"` for with JIT and without JIT, tested using https://3v4l.org/Q366F – mvorisek Jun 30 '20 at 12:01
  • @mvorisek I've updated my answer. I guess the opcache_get_status() function is what you were looking for. – maio290 Jun 30 '20 at 12:23
  • 1
    Thanks, the following works: `opcache_get_status()["jit"]['enabled'] ?? false` – mvorisek Jun 30 '20 at 12:32
  • Wrap the answer in a `var_dump()` to get it to print out the boolean value. e.g. `var_dump(opcache_get_status()["jit"]["enabled"]);`.... returns e.g. `bool(false)` or `bool(true)`. – Elijah Lynn Nov 01 '22 at 03:13
  • `opcache_get_status` is working, but it's terribly slow, ~40ms, if someone have a fastest way... – luigifab Jun 23 '23 at 14:02
  • @luigifab I cannot reproduce it, the method executes on my server in around 6 to 8 ms. I wouldn't even call 40 ms that long, if this time is an issue to you, you maybe shouldn't query it that often, sounds more like an issue with the algorithm you're trying to implement. – maio290 Jun 23 '23 at 14:13
3

opcache_get_status() will not work when JIT is disabled and will throw Fatal error.

echo (function_exists('opcache_get_status') 
      && opcache_get_status()['jit']['enabled']) ? 'JIT enabled' : 'JIT disabled';

We must have following settings for JIT in php.ini file.

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
Jsowa
  • 9,104
  • 5
  • 56
  • 60
3
php -i | grep "opcache"

checking:
opcache.enable => On => On
opcache.enable_cli => On => On
opcache.jit => tracing => tracing
Leo Yuee
  • 31
  • 2