From e0ea80006ff47e85e53e4ced4f0961a99e91768b Mon Sep 17 00:00:00 2001 From: Lukas Winkler Date: Mon, 12 Apr 2021 17:28:42 +0200 Subject: [PATCH] add OPcache check --- Diagnostic/OpcacheCheck.php | 111 ++++++++++++++++++++++++++++++++++++ Diagnostic/PhpIniCheck.php | 26 +-------- Utils.php | 44 ++++++++++++++ config/config.php | 1 + lang/en.json | 8 +++ 5 files changed, 166 insertions(+), 24 deletions(-) create mode 100644 Diagnostic/OpcacheCheck.php create mode 100644 Utils.php diff --git a/Diagnostic/OpcacheCheck.php b/Diagnostic/OpcacheCheck.php new file mode 100644 index 0000000..0136071 --- /dev/null +++ b/Diagnostic/OpcacheCheck.php @@ -0,0 +1,111 @@ +logger = $logger; + $this->label = "🧪 " . "OPcache"; + } + + /** + * @return DiagnosticResult[] + */ + public function execute() + { + $result = new DiagnosticResult($this->label); + if (!Utils::booleanIni("opcache.enable")) { + $result->addItem(new DiagnosticResultItem( + DiagnosticResult::STATUS_WARNING, + Piwik::translate("DiagnosticsExtended_OpcacheCheckOpcacheDisabled") + )); + return [$result]; + } else { + $status = opcache_get_status(); + $memoryUsage = $status["memory_usage"]; + $interned = $status["interned_strings_usage"]; + $statistics = $status["opcache_statistics"]; + $result->addItem(new DiagnosticResultItem( + DiagnosticResult::STATUS_OK, + Piwik::translate("DiagnosticsExtended_OpcacheCheckOpcacheEnabled", [ + round($memoryUsage["used_memory"] / self::MEGABYTE), + round(($memoryUsage["used_memory"] + $memoryUsage["free_memory"]) / self::MEGABYTE), + round($memoryUsage["current_wasted_percentage"] * 100, 2), + round($interned["used_memory"] / self::MEGABYTE), + round($interned["buffer_size"] / self::MEGABYTE), + round($statistics["opcache_hit_rate"]) + ]) + )); + } + if (!Utils::booleanIni("opcache.save_comments")) { + $result->addItem(new DiagnosticResultItem( + DiagnosticResult::STATUS_ERROR, + Piwik::translate("DiagnosticsExtended_OpcacheCheckSaveComments") + )); + } + $minimum_files = 7963; + if (Utils::intIni("opcache.max_accelerated_files") <= $minimum_files) { + $result->addItem(new DiagnosticResultItem( + DiagnosticResult::STATUS_WARNING, + Piwik::translate("DiagnosticsExtended_OpcacheCheckMaxFiles", [$minimum_files]) + )); + } + if (Utils::intIni("opcache.memory_consumption") < 128) { + $result->addItem(new DiagnosticResultItem( + DiagnosticResult::STATUS_WARNING, + Piwik::translate("DiagnosticsExtended_OpcacheCheckMemory") + )); + } + if (Utils::intIni("opcache.interned_strings_buffer") < 8) { + $result->addItem(new DiagnosticResultItem( + DiagnosticResult::STATUS_WARNING, + Piwik::translate("DiagnosticsExtended_OpcacheCheckInternedStrings") + )); + } + if (!Utils::intIni("opcache.validate_timestamps")) { + $result->addItem(new DiagnosticResultItem( + DiagnosticResult::STATUS_INFORMATIONAL, + Piwik::translate("DiagnosticsExtended_OpcacheCheckValidateTimestamps") + )); + + } + $jit = ini_get("opcache.jit"); + if (PHP_MAJOR_VERSION >= 8 && (!$jit || $jit == "0" || $jit == "off")) { + $result->addItem(new DiagnosticResultItem( + DiagnosticResult::STATUS_INFORMATIONAL, + Piwik::translate("DiagnosticsExtended_OpcacheCheckJIT") + )); + + } + return [$result]; + } + + +} diff --git a/Diagnostic/PhpIniCheck.php b/Diagnostic/PhpIniCheck.php index b95cc73..0b1e7eb 100644 --- a/Diagnostic/PhpIniCheck.php +++ b/Diagnostic/PhpIniCheck.php @@ -13,6 +13,7 @@ use Piwik\Plugins\Diagnostics\Diagnostic\Diagnostic; use Piwik\Plugins\Diagnostics\Diagnostic\DiagnosticResult; use Piwik\Plugins\Diagnostics\Diagnostic\DiagnosticResultItem; use Piwik\Plugins\DiagnosticsExtended\Diagnostic\IniSettings\IniSetting; +use Piwik\Plugins\DiagnosticsExtended\Utils; use Psr\Log\LoggerInterface; class PhpIniCheck implements Diagnostic @@ -42,7 +43,7 @@ class PhpIniCheck implements Diagnostic $result = new DiagnosticResult($this->label); foreach ($this->iniSettings as $setting) { $key = $setting::$key; - if ($this->booleanIni($key) === $setting::$targetValue) { + if (Utils::booleanIni($key) === $setting::$targetValue) { $item = new DiagnosticResultItem( DiagnosticResult::STATUS_OK, $setting::$targetValue @@ -65,29 +66,6 @@ class PhpIniCheck implements Diagnostic } - private function booleanIni(string $key): bool - { - return $this->IniValueToBoolean(ini_get($key)); - } - - private function IniValueToBoolean(string $iniValue): bool - { - switch (strtolower($iniValue)) { - case "on": - case "true": - case "yes": - case "1": - return true; - case "off": - case "false": - case "no": - case "0": - case "": - return false; - default: - return $iniValue; - } - } } diff --git a/Utils.php b/Utils.php new file mode 100644 index 0000000..98472d4 --- /dev/null +++ b/Utils.php @@ -0,0 +1,44 @@ +opcache.interned_strings_buffer below 8MB which might be a bit low. Please increase the value to 8 (Megabyte) or more depending on how many PHP files you serve and how much memory your server has available.", + "OpcacheCheckJIT": "As you are using PHP 8 or newer, you might want to look into enabling the JIT compiler. It might be able to speed up Matomo by a bit. It would also be great if you could report back any experiences after enabling PHP JIT.", + "OpcacheCheckMaxFiles": "It seems like you have set opcache.max_accelerated_files to or below %s which might be too low to keep all Matomo files in the cache, especially if you are also hosting other PHP applications. Please set the option to 7963, 16229 or 32531 depending on the number of PHP files your webserver uses.", + "OpcacheCheckMemory": "It seems like you have set opcache.memory_consumption below 128MB which might be too low to keep all Matomo files in the cache, especially if you are also hosting other PHP applications. Please increase the value to 128 or 256 (Megabyte) depending on how many PHP files you serve and how much memory your server has available.", + "OpcacheCheckOpcacheDisabled": "It seems like OPcache is disabled in your PHP setup. OPcache stores bytecode of PHP scripts in memory and is therefore able to speed up PHP by quite a bit as it avoids loading and parsing PHP files on every request. Check the opcache.enable php.ini setting for more information.", + "OpcacheCheckOpcacheEnabled": "It seems like OPcache is enabled. %1$sMB of %2$sMB are used for PHP scripts (%3$s% wasted) and %4$sMB of %5$sMB are used for interned strings. %6$s% of requests hit the OPcache.", + "OpcacheCheckSaveComments": "It seems like you have disabled the opcache.save_comments setting. This removes comments from the OPcache making it smaller, but breaks Matomo. Please set opcache.save_comments to 0", + "OpcacheCheckValidateTimestamps": "It seems like you have disabled opcache.validate_timestamps which means that PHP will never check if the actual files on disk have changed, but continue to use the cached ones. In theory, this should be no issue as Matomo invalidates the OPcache on updates and installs, but you might still want to look into this if you encounter weird issues.", "OpensslVersionCheckLabel": "OpenSSL version check", "OpensslVersionCheckNoOpenssl": "Your PHP setup doesn't use OpenSSL or curl, so there is nothing to check.", "OpensslVersionCheckNotOutdated": "Your OpenSSL version (%s) is not really old. Nevertheless, check if there are known vulnerabilities for it and update it if necessary.",