Skip to content

Commit bd2a203

Browse files
authored
Merge pull request #182 from bryanlatten/file-opcache
File-based opcache: ENV variable
2 parents d9d1600 + a9408c4 commit bd2a203

File tree

10 files changed

+115
-8
lines changed

10 files changed

+115
-8
lines changed

Dockerfile-7.0

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM behance/docker-nginx:8.8
1+
FROM behance/docker-nginx:8.9
22
LABEL maintainers="Behance Team <dev-behance@adobe.com>"
33

44
# Set TERM to suppress warning messages.
@@ -23,6 +23,7 @@ ENV CONF_PHPFPM=/etc/php/7.0/fpm/php-fpm.conf \
2323
PHP_OPCACHE_MEMORY_CONSUMPTION=128 \
2424
PHP_OPCACHE_INTERNED_STRINGS_BUFFER=16 \
2525
PHP_OPCACHE_MAX_WASTED_PERCENTAGE=5 \
26+
PHP_OPCACHE_ENABLE_CLI=1 \
2627
PHP_ENGINE_VERSION=20151012 \
2728
CFG_APP_DEBUG=1
2829

Dockerfile-7.1

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM behance/docker-nginx:8.8
1+
FROM behance/docker-nginx:8.9
22
LABEL maintainers="Behance Team <dev-behance@adobe.com>"
33

44
# Set TERM to suppress warning messages.
@@ -23,6 +23,7 @@ ENV CONF_PHPFPM=/etc/php/7.1/fpm/php-fpm.conf \
2323
PHP_OPCACHE_MEMORY_CONSUMPTION=128 \
2424
PHP_OPCACHE_INTERNED_STRINGS_BUFFER=16 \
2525
PHP_OPCACHE_MAX_WASTED_PERCENTAGE=5 \
26+
PHP_OPCACHE_ENABLE_CLI=1 \
2627
PHP_ENGINE_VERSION=20160303 \
2728
CFG_APP_DEBUG=1
2829

Dockerfile-7.2

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM behance/docker-nginx:8.8
1+
FROM behance/docker-nginx:8.9
22
LABEL maintainers="Behance Team <dev-behance@adobe.com>"
33

44
# Set TERM to suppress warning messages.
@@ -23,6 +23,7 @@ ENV CONF_PHPFPM=/etc/php/7.2/fpm/php-fpm.conf \
2323
PHP_OPCACHE_MEMORY_CONSUMPTION=128 \
2424
PHP_OPCACHE_INTERNED_STRINGS_BUFFER=16 \
2525
PHP_OPCACHE_MAX_WASTED_PERCENTAGE=5 \
26+
PHP_OPCACHE_ENABLE_CLI=1 \
2627
PHP_ENGINE_VERSION=20170718 \
2728
CFG_APP_DEBUG=1
2829

Dockerfile-7.3

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM behance/docker-nginx:8.8
1+
FROM behance/docker-nginx:8.9
22
LABEL maintainers="Behance Team <dev-behance@adobe.com>"
33

44
# Set TERM to suppress warning messages.
@@ -23,6 +23,7 @@ ENV CONF_PHPFPM=/etc/php/7.3/fpm/php-fpm.conf \
2323
PHP_OPCACHE_MEMORY_CONSUMPTION=128 \
2424
PHP_OPCACHE_INTERNED_STRINGS_BUFFER=16 \
2525
PHP_OPCACHE_MAX_WASTED_PERCENTAGE=5 \
26+
PHP_OPCACHE_ENABLE_CLI=1 \
2627
PHP_ENGINE_VERSION=20180731 \
2728
CFG_APP_DEBUG=1
2829

Dockerfile-7.3-alpine

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM behance/docker-nginx:8.8-alpine
1+
FROM behance/docker-nginx:8.9-alpine
22
LABEL maintainers="Behance Team <dev-behance@adobe.com>"
33

44
# Set TERM to suppress warning messages.
@@ -23,6 +23,7 @@ ENV CONF_PHPFPM=/etc/php7/php-fpm.conf \
2323
PHP_OPCACHE_MEMORY_CONSUMPTION=128 \
2424
PHP_OPCACHE_INTERNED_STRINGS_BUFFER=16 \
2525
PHP_OPCACHE_MAX_WASTED_PERCENTAGE=5 \
26+
PHP_OPCACHE_ENABLE_CLI=1 \
2627
PHP_ENGINE_VERSION=20180731 \
2728
CFG_APP_DEBUG=1
2829

Dockerfile-7.4

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM behance/docker-nginx:8.8
1+
FROM behance/docker-nginx:8.9
22
LABEL maintainers="Behance Team <dev-behance@adobe.com>"
33

44
# Set TERM to suppress warning messages.
@@ -23,6 +23,7 @@ ENV CONF_PHPFPM=/etc/php/7.4/fpm/php-fpm.conf \
2323
PHP_OPCACHE_MEMORY_CONSUMPTION=128 \
2424
PHP_OPCACHE_INTERNED_STRINGS_BUFFER=16 \
2525
PHP_OPCACHE_MAX_WASTED_PERCENTAGE=5 \
26+
PHP_OPCACHE_ENABLE_CLI=1 \
2627
PHP_ENGINE_VERSION=20190902 \
2728
CFG_APP_DEBUG=1
2829

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,11 @@ PHP_FPM_MIN_SPARE_SERVERS | PHP_FPM_MIN_SPARE_SERVERS=10 | 5 | [docs](http://php
203203
PHP_OPCACHE_MEMORY_CONSUMPTION | PHP_OPCACHE_MEMORY_CONSUMPTION=512 | 128 | [docs](http://php.net/manual/en/opcache.configuration.php#ini.opcache.memory-consumption)
204204
PHP_OPCACHE_MAX_WASTED_PERCENTAGE | PHP_OPCACHE_MAX_WASTED_PERCENTAGE=10 | 5 | [docs](http://php.net/manual/en/opcache.configuration.php#ini.opcache.max-wasted-percentage)
205205
PHP_OPCACHE_INTERNED_STRINGS_BUFFER | PHP_OPCACHE_INTERNED_STRINGS_BUFFER=64 | 16 | [docs](http://php.net/manual/en/opcache.configuration.php#ini.opcache.interned-strings-buffer)
206-
PHP_FPM_LOG_LIMIT | PHP_FPM_LOG_LIMIT=4096 | 1024 | PHP 7.3-only, allows configurable stdout message max length [docs](https://www.php.net/manual/en/install.fpm.configuration.php)
207-
PHP_FPM_LOG_BUFFERING | PHP_FPM_LOG_BUFFERING=no | yes | Experimental, PHP 7.3-only [docs](https://www.php.net/manual/en/install.fpm.configuration.php)
206+
PHP_OPCACHE_FILE_CACHE | PHP_OPCACHE_FILE_CACHE=/tmp | - | [docs](https://www.php.net/manual/en/opcache.configuration.php#ini.opcache.file-cache)
207+
PHP_OPCACHE_ENABLE_CLI | PHP_OPCACHE_ENABLE_CLI=0 | 1 | [docs](https://www.php.net/manual/en/opcache.configuration.php#ini.opcache.enable-cli)
208+
PHP_OPCACHE_PRELOAD | PHP_OPCACHE_PRELOAD=/etc/php/preload.php | - | [docs](https://www.php.net/manual/en/opcache.configuration.php#ini.opcache.preload)
209+
PHP_FPM_LOG_LIMIT | PHP_FPM_LOG_LIMIT=4096 | 1024 | PHP 7.3+ only, allows configurable stdout message max length [docs](https://www.php.net/manual/en/install.fpm.configuration.php)
210+
PHP_FPM_LOG_BUFFERING | PHP_FPM_LOG_BUFFERING=no | yes | PHP 7.3+ only [docs](https://www.php.net/manual/en/install.fpm.configuration.php)
208211

209212
### Testing
210213
---

container/root/etc/php/7.0/mods-available/opcache.ini

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,26 @@ opcache.interned_strings_buffer=${PHP_OPCACHE_INTERNED_STRINGS_BUFFER}
1313
opcache.fast_shutdown=0
1414
opcache.optimization_level=0xfffffff0
1515

16+
; Ignored when validate_timestamps is disabled
1617
opcache.revalidate_freq=0
18+
19+
; Since container deployments are atomic, there is no need to monitor for file updates
20+
; https://www.php.net/manual/en/opcache.configuration.php#ini.opcache.file_update_protection
21+
; https://www.php.net/manual/en/opcache.configuration.php#ini.opcache.validate-timestamps
22+
opcache.file_update_protection=${CFG_APP_DEBUG}
1723
opcache.validate_timestamps=${CFG_APP_DEBUG}
24+
25+
; Backup memory cache to file system
26+
; https://www.php.net/manual/en/opcache.configuration.php#ini.opcache.file-cache
27+
opcache.file_cache=${PHP_OPCACHE_FILE_CACHE}
28+
29+
; https://www.php.net/manual/en/opcache.configuration.php#ini.opcache.enable-cli
30+
opcache.enable_cli=${PHP_OPCACHE_ENABLE_CLI}
31+
32+
; https://www.php.net/manual/en/opcache.configuration.php#ini.opcache.preload-user
33+
opcache.preload_user=${NOT_ROOT_USER}
34+
35+
; https://www.php.net/manual/en/opcache.configuration.php#ini.opcache.preload
36+
opcache.preload=${PHP_OPCACHE_PRELOAD}
37+
38+

container/root/etc/php/preload.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
/**
3+
* Sends all eligible files into PHP's opcode cache before server startup
4+
*/
5+
$LOGPREFIX = '[opcache]';
6+
$MEMORY_USAGE_WARNING_PERCENTAGE = 85;
7+
$app_root = getenv('APP_ROOT');
8+
9+
if (!$app_root) {
10+
echo("${LOGPREFIX} cannot preload, APP_ROOT is empty");
11+
return;
12+
}
13+
14+
echo "${LOGPREFIX} preloading from ${app_root}\n";
15+
$start_time = microtime(true);
16+
17+
[ $count, $success, $failure ] = compileFiles($app_root);
18+
19+
$end_time = microtime(true);
20+
$elapsed = ($end_time - $start_time);
21+
$elapsed = round($elapsed, 3);
22+
23+
echo "${LOGPREFIX} preloaded: processed ${count} files - ${success} successful / ${failure} failed, in ${elapsed} seconds\n";
24+
25+
$opcache_status = opcache_get_status(false);
26+
$memory_usage = $opcache_status['memory_usage'] ?? [];
27+
28+
if (empty($memory_usage)) {
29+
echo "${LOGPREFIX} error: no memory reported\n";
30+
return;
31+
}
32+
33+
$free_memory = $memory_usage['free_memory'] ?? 0;
34+
$wasted_memory = $memory_usage['wasted_memory'] ?? 0;
35+
$used_memory = $memory_usage['used_memory'] ?? 0;
36+
37+
$memory_total = $wasted_memory + $free_memory + $used_memory;
38+
$memory_consumed = $used_memory + $wasted_memory;
39+
$percentage_used = ($memory_consumed / $memory_total) * 100;
40+
$percentage_used = round($percentage_used, 2);
41+
42+
if ($percentage_used >= $MEMORY_USAGE_WARNING_PERCENTAGE) {
43+
echo "${LOGPREFIX} warning: ${percentage_used}% memory usage, consider raising PHP_OPCACHE_MEMORY_CONSUMPTION env variable\n";
44+
} else {
45+
echo "${LOGPREFIX} complete: ${percentage_used}% memory utilized\n";
46+
}
47+
48+
function compileFiles($input) {
49+
$directory = new RecursiveDirectoryIterator($input);
50+
$fullTree = new RecursiveIteratorIterator($directory);
51+
52+
$phpunit_filter = '/^(?:(?!test).)*\.php$/i'; // Removes PHPUnit-style filenames (ie. calculatortest.php)
53+
54+
$all_files = new RegexIterator($fullTree, $phpunit_filter, RecursiveRegexIterator::GET_MATCH);
55+
$success = 0;
56+
$failure = 0;
57+
$results = [];
58+
59+
foreach($all_files as $file) {
60+
if (@opcache_compile_file($file[0])) {
61+
++$success;
62+
} else {
63+
++$failure;
64+
}
65+
}
66+
67+
$count = $success + $failure;
68+
69+
return [ $count, $success, $failure ];
70+
}

container/root/tests/php-fpm/base.goss.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,3 +198,10 @@ file:
198198
exists: true
199199
contains:
200200
- '/^error_log = \/dev\/stdout/'
201+
{{ .Env.CONF_PHPMODS }}/opcache.ini:
202+
exists: true
203+
contains:
204+
- '/^opcache.file_cache=\${PHP_OPCACHE_FILE_CACHE}/'
205+
- '/^opcache.enable_cli=\${PHP_OPCACHE_ENABLE_CLI}/'
206+
- '/^opcache.preload_user=\${NOT_ROOT_USER}/'
207+
- '/^opcache.preload=\${PHP_OPCACHE_PRELOAD}/'

0 commit comments

Comments
 (0)