9

Let say I am using the PHP 5.5 opcode cache, and set

opcache.memory_consumption=128

, if I have 4 pools in php-fpm, will each of the 4 pool share 128MB of cache, or will they own 128M opcache for each pool?

hakre
  • 193,403
  • 52
  • 435
  • 836
Ryan
  • 10,041
  • 27
  • 91
  • 156

3 Answers3

6

If you have any doubts about how cache memory used between pools make a simple test.

Technique is quite simple. Create 2 fpm-pools on different www-dir listening for example 8081 and 8082 ports and 2 files index.php and check.php with identical content:

<?php
echo "<pre>\n";
var_dump(opcache_get_status());

Firstly restart your php-fpm service, then run first pool localhost:8081/index.php, then localhost:8082/check.php. After this check ["scripts"] section in output. I've got next results:

localhost:8081/index.php

["scripts"]=>
  array(1) {
    ["/usr/share/nginx/html/index.php"]=>
    array(6) {
      ["full_path"]=>
      string(31) "/usr/share/nginx/html/index.php"
      ["hits"]=>
      int(0)
      ["memory_consumption"]=>
      int(1032)
      ["last_used"]=>
      string(24) "Mon Dec 23 23:38:35 2013"
      ["last_used_timestamp"]=>
      int(1387827515)
      ["timestamp"]=>
      int(1387825100)
    }
  }

localhost:8082/check.php

["scripts"]=>
  array(2) {
    ["/usr/share/nginx/html1/check.php"]=>
    array(6) {
      ["full_path"]=>
      string(32) "/usr/share/nginx/html1/check.php"
      ["hits"]=>
      int(0)
      ["memory_consumption"]=>
      int(1056)
      ["last_used"]=>
      string(24) "Mon Dec 23 23:38:47 2013"
      ["last_used_timestamp"]=>
      int(1387827527)
      ["timestamp"]=>
      int(1387825174)
    }
    ["/usr/share/nginx/html/index.php"]=>
    array(6) {
      ["full_path"]=>
      string(31) "/usr/share/nginx/html/index.php"
      ["hits"]=>
      int(0)
      ["memory_consumption"]=>
      int(1032)
      ["last_used"]=>
      string(24) "Mon Dec 23 23:38:35 2013"
      ["last_used_timestamp"]=>
      int(1387827515)
      ["timestamp"]=>
      int(1387825100)
    }
  }

As you see second pool already has index.php in cache, so answer is all 4 pools will share 128MB of cache.

Alexander Yancharuk
  • 13,817
  • 5
  • 55
  • 55
1

As mentioned by raina77ow through link that 128 MB will be shared betweeen 4 Pools

Adding to that, as mentioned in official documentation

; Sets how much memory to use
opcache.memory_consumption=128

opcache.memory_consumption sets the memory limit that will be used no matter how many pools you use, it will get shared accordingly.

Abhishek Goel
  • 18,785
  • 11
  • 87
  • 65
  • 1
    Let me add something I learned today, which wasn't obvious to me and is a consequence of this. I tend to set per-pool PHP options via `php_admin_value`/`php_value` etc. in pool configs. If you do this for `opcache.memory_consumption` or `opcache.interned_strings_usage`, the actually usable memory won't increase - instead the difference between the respective value in FPM's `php.ini` and in the pool config will just be added to the `used_memory` count by `opcache_get_status()`. This confused me quite a lot and it took me a while to realize that I actually have to change the value in `php.ini`. – David Feb 12 '21 at 13:59
0

As OpCache essentially works the same way as APC does (by storing precompiled script bytecode in shared memory), and it's confirmed that APC opcode cache is shared between php-fpm pools if they're started by the same master process, 128 MB will be shared between 4 pools as well.

raina77ow
  • 103,633
  • 15
  • 192
  • 229