Skip to content

Commit

Permalink
Enhance and complement OPcache setup checks
Browse files Browse the repository at this point in the history
The current OPcache recommendations match the PHP defaults, but the values are much higher than required to run Nextcloud, even with a high number of installed apps. On the other hand, when other applications use the same OPcache instance, the recommended values might not be sufficient. Accurate recommendations need to take into account actual OPcache usage.

With this commit, recommendations are shown to raise the config value if more than 90% of max cache size or number of keys is used.

The checks whether the module is loaded and whether the OPcache is properly configured have been merged into a single function. This allowed to reduce the overhead of OPcache configuration checks when the module is not loaded.

A check has been added whether Nextcloud is permitted to use the OPcache API. Without this, inconsistencies during core or app upgrades may cause errors and OPcache usage cannot be determined for the new usage based checks.

OPcache usage based checks are skipped when Nextcloud is not permitted to use the API.

Signed-off-by: MichaIng <[email protected]>
  • Loading branch information
MichaIng committed Sep 23, 2021
1 parent dad8949 commit 4115c8d
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 73 deletions.
67 changes: 43 additions & 24 deletions apps/settings/lib/Controller/CheckSetupController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
* @author timm2k <[email protected]>
* @author Timo Förster <[email protected]>
* @author Valdnet <[email protected]>
* @author MichaIng <[email protected]>
*
* @license AGPL-3.0
*
Expand Down Expand Up @@ -225,7 +226,7 @@ protected function getCurlVersion() {
}

/**
* Check if the used SSL lib is outdated. Older OpenSSL and NSS versions do
* Check if the used SSL lib is outdated. Older OpenSSL and NSS versions do
* have multiple bugs which likely lead to problems in combination with
* functionality required by ownCloud such as SNI.
*
Expand Down Expand Up @@ -431,31 +432,55 @@ public function getFailedIntegrityCheckFiles(): DataDisplayResponse {
}

/**
* Checks whether a PHP opcache is properly set up
* @return bool
* Checks whether a PHP OPcache is properly set up
* @return string[] The list of OPcache setup recommendations
*/
protected function isOpcacheProperlySetup() {
if (!$this->iniGetWrapper->getBool('opcache.enable')) {
return false;
protected function getOpcacheSetupRecommendations(): array {
// If the module is not loaded, return directly to skip inapplicable checks
if (!extension_loaded('Zend OPcache')) {
return array('The PHP OPcache module is not loaded. <a target="_blank" rel="noreferrer noopener" class="external" href="' + $this->urlGenerator->linkToDocs('admin-php-opcache') + '">For better performance it is recommended</a> to load it into your PHP installation.');
}

if (!$this->iniGetWrapper->getBool('opcache.save_comments')) {
return false;
}
$recommendations = [];

if ($this->iniGetWrapper->getNumeric('opcache.max_accelerated_files') < 10000) {
return false;
// Check whether Nextcloud is allowed to use the OPcache API
$isPermitted = true;
$permittedPath = $this->iniGetWrapper->getString('opcache.restrict_api');
if ($permittedPath && !str_starts_with(\OC::$SERVERROOT, $permittedPath)) {
$isPermitted = false;
}

if ($this->iniGetWrapper->getNumeric('opcache.memory_consumption') < 128) {
return false;
}
if (!$this->iniGetWrapper->getBool('opcache.enable')) {
$recommendations[] = 'OPcache is disabled. For better performance, it is recommended to apply <code>opcache.enable=1</code> to your PHP configuration.';

if ($this->iniGetWrapper->getNumeric('opcache.interned_strings_buffer') < 8) {
return false;
// Check for saved comments only when OPcache is currently disabled. If it was enabled, opcache.save_comments=0 would break Nextcloud in the first place.
if (!$this->iniGetWrapper->getBool('opcache.save_comments')) {
$recommendations[] = 'OPcache is configured to remove code comments. With OPcache enabled, <code>opcache.save_comments=1</code> must be set for Nextcloud to function.';
}

if (!$isPermitted) {
$recommendations[] = 'Nextcloud is not allowed to use the OPcache API. With OPcache enabled, it is highly recommended to include all Nextcloud directories with <code>opcache.restrict_api</code> or unset this setting to disable OPcache API restrictions, to prevent errors during Nextcloud core or app upgrades.';
}
} elseif (!$isPermitted) {
$recommendations[] = 'Nextcloud is not allowed to use the OPcache API. It is highly recommended to include all Nextcloud directories with <code>opcache.restrict_api</code> or unset this setting to disable OPcache API restrictions, to prevent errors during Nextcloud core or app upgrades.';
} else {
$status = opcache_get_status(false);

// Recommend to raise value, if more than 90% of max value is reached
if ($status['opcache_statistics']['num_cached_keys'] / $status['opcache_statistics']['max_cached_keys'] > 0.9) {
$recommendations[] = 'The maximum number of OPcache keys is nearly exceeded. To assure that all scripts can be hold in cache, it is recommended to apply <code>opcache.max_accelerated_files</code> to your PHP configuration with a value higher than <code>' . ($this->iniGetWrapper->getNumeric('opcache.max_accelerated_files') ?: 'currently') . '</code>.';
}

if ($status['memory_usage']['used_memory'] / $status['memory_usage']['free_memory'] > 9) {
$recommendations[] = 'The OPcache buffer is nearly full. To assure that all scripts can be hold in cache, it is recommended to apply <code>opcache.memory_consumption</code> to your PHP configuration with a value higher than <code>' . ($this->iniGetWrapper->getNumeric('opcache.memory_consumption') ?: 'currently') . '</code>.';
}

if ($status['interned_strings_usage']['used_memory'] / $status['interned_strings_usage']['free_memory'] > 9) {
$recommendations[] = 'The OPcache interned strings buffer is nearly full. To assure that repeating strings can be effectively cached, it is recommended to apply <code>opcache.interned_strings_buffer</code> to your PHP configuration with a value higher than <code>' . ($this->iniGetWrapper->getNumeric('opcache.interned_strings_buffer') ?: 'currently') . '</code>.';
}
}

return true;
return $recommendations;
}

/**
Expand Down Expand Up @@ -555,10 +580,6 @@ protected function getCronErrors() {
return [];
}

protected function hasOpcacheLoaded(): bool {
return extension_loaded('Zend OPcache');
}

private function isTemporaryDirectoryWritable(): bool {
try {
if (!empty($this->tempManager->getTempBaseDir())) {
Expand Down Expand Up @@ -770,9 +791,7 @@ public function check() {
'isCorrectMemcachedPHPModuleInstalled' => $this->isCorrectMemcachedPHPModuleInstalled(),
'hasPassedCodeIntegrityCheck' => $this->checker->hasPassedCheck(),
'codeIntegrityCheckerDocumentation' => $this->urlGenerator->linkToDocs('admin-code-integrity'),
'isOpcacheProperlySetup' => $this->isOpcacheProperlySetup(),
'hasOpcacheLoaded' => $this->hasOpcacheLoaded(),
'phpOpcacheDocumentation' => $this->urlGenerator->linkToDocs('admin-php-opcache'),
'OpcacheSetupRecommendations' => $this->getOpcacheSetupRecommendations(),
'isSettimelimitAvailable' => $this->isSettimelimitAvailable(),
'hasFreeTypeSupport' => $this->hasFreeTypeSupport(),
'missingPrimaryKeys' => $this->hasMissingPrimaryKeys(),
Expand Down
18 changes: 8 additions & 10 deletions core/js/setupchecks.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,18 +322,16 @@
type: OC.SetupChecks.MESSAGE_TYPE_ERROR
});
}
if(!data.hasOpcacheLoaded) {
messages.push({
msg: t('core', 'The PHP OPcache module is not loaded. {linkstart}For better performance it is recommended ↗{linkend} to load it into your PHP installation.')
.replace('{linkstart}', '<a target="_blank" rel="noreferrer noopener" class="external" href="' + data.phpOpcacheDocumentation + '">')
.replace('{linkend}', '</a>'),
type: OC.SetupChecks.MESSAGE_TYPE_INFO
if(data.OpcacheSetupRecommendations.length > 0) {
var listOfOPcacheRecommendations = "";
data.OpcacheSetupRecommendations.forEach(function(element){
listOfOPcacheRecommendations += "<li>" + element + "</li>";
});
} else if(!data.isOpcacheProperlySetup) {
messages.push({
msg: t('core', 'The PHP OPcache module is not properly configured. {linkstart}For better performance it is recommended ↗{linkend} to use the following settings in the <code>php.ini</code>:')
.replace('{linkstart}', '<a target="_blank" rel="noreferrer noopener" class="external" href="' + data.phpOpcacheDocumentation + '">')
.replace('{linkend}', '</a>') + "<pre><code>opcache.enable=1\nopcache.interned_strings_buffer=8\nopcache.max_accelerated_files=10000\nopcache.memory_consumption=128\nopcache.save_comments=1\nopcache.revalidate_freq=1</code></pre>",
msg: t(
'core',
'The PHP OPcache module is not properly configured:'
) + "<ul>" + listOfOPcacheRecommendations + "</ul>",
type: OC.SetupChecks.MESSAGE_TYPE_INFO
});
}
Expand Down
59 changes: 20 additions & 39 deletions core/js/tests/specs/setupchecksSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,7 @@ describe('OC.SetupChecks tests', function() {
forwardedForHeadersWorking: true,
isCorrectMemcachedPHPModuleInstalled: true,
hasPassedCodeIntegrityCheck: true,
isOpcacheProperlySetup: true,
hasOpcacheLoaded: true,
OpcacheSetupRecommendations: [],
isSettimelimitAvailable: true,
hasFreeTypeSupport: true,
missingIndexes: [],
Expand Down Expand Up @@ -292,8 +291,7 @@ describe('OC.SetupChecks tests', function() {
forwardedForHeadersWorking: true,
isCorrectMemcachedPHPModuleInstalled: true,
hasPassedCodeIntegrityCheck: true,
isOpcacheProperlySetup: true,
hasOpcacheLoaded: true,
OpcacheSetupRecommendations: [],
isSettimelimitAvailable: true,
hasFreeTypeSupport: true,
missingIndexes: [],
Expand Down Expand Up @@ -350,8 +348,7 @@ describe('OC.SetupChecks tests', function() {
forwardedForHeadersWorking: true,
isCorrectMemcachedPHPModuleInstalled: true,
hasPassedCodeIntegrityCheck: true,
isOpcacheProperlySetup: true,
hasOpcacheLoaded: true,
OpcacheSetupRecommendations: [],
isSettimelimitAvailable: true,
hasFreeTypeSupport: true,
missingIndexes: [],
Expand Down Expand Up @@ -406,8 +403,7 @@ describe('OC.SetupChecks tests', function() {
forwardedForHeadersWorking: true,
isCorrectMemcachedPHPModuleInstalled: true,
hasPassedCodeIntegrityCheck: true,
isOpcacheProperlySetup: true,
hasOpcacheLoaded: true,
OpcacheSetupRecommendations: [],
isSettimelimitAvailable: true,
hasFreeTypeSupport: true,
missingIndexes: [],
Expand Down Expand Up @@ -460,8 +456,7 @@ describe('OC.SetupChecks tests', function() {
forwardedForHeadersWorking: true,
isCorrectMemcachedPHPModuleInstalled: false,
hasPassedCodeIntegrityCheck: true,
isOpcacheProperlySetup: true,
hasOpcacheLoaded: true,
OpcacheSetupRecommendations: [],
isSettimelimitAvailable: true,
hasFreeTypeSupport: true,
missingIndexes: [],
Expand Down Expand Up @@ -514,8 +509,7 @@ describe('OC.SetupChecks tests', function() {
forwardedForHeadersWorking: true,
isCorrectMemcachedPHPModuleInstalled: true,
hasPassedCodeIntegrityCheck: true,
isOpcacheProperlySetup: true,
hasOpcacheLoaded: true,
OpcacheSetupRecommendations: [],
isSettimelimitAvailable: true,
hasFreeTypeSupport: true,
missingIndexes: [],
Expand Down Expand Up @@ -570,8 +564,7 @@ describe('OC.SetupChecks tests', function() {
reverseProxyDocs: 'https://docs.nextcloud.com/foo/bar.html',
isCorrectMemcachedPHPModuleInstalled: true,
hasPassedCodeIntegrityCheck: true,
isOpcacheProperlySetup: true,
hasOpcacheLoaded: true,
OpcacheSetupRecommendations: [],
isSettimelimitAvailable: true,
hasFreeTypeSupport: true,
missingIndexes: [],
Expand Down Expand Up @@ -624,8 +617,7 @@ describe('OC.SetupChecks tests', function() {
reverseProxyDocs: 'https://docs.nextcloud.com/foo/bar.html',
isCorrectMemcachedPHPModuleInstalled: true,
hasPassedCodeIntegrityCheck: true,
isOpcacheProperlySetup: true,
hasOpcacheLoaded: true,
OpcacheSetupRecommendations: [],
isSettimelimitAvailable: false,
hasFreeTypeSupport: true,
missingIndexes: [],
Expand Down Expand Up @@ -678,8 +670,7 @@ describe('OC.SetupChecks tests', function() {
reverseProxyDocs: 'https://docs.nextcloud.com/foo/bar.html',
isCorrectMemcachedPHPModuleInstalled: true,
hasPassedCodeIntegrityCheck: true,
isOpcacheProperlySetup: true,
hasOpcacheLoaded: true,
OpcacheSetupRecommendations: [],
isSettimelimitAvailable: true,
hasFreeTypeSupport: true,
missingIndexes: [],
Expand Down Expand Up @@ -753,8 +744,7 @@ describe('OC.SetupChecks tests', function() {
phpSupported: {eol: true, version: '5.4.0'},
isCorrectMemcachedPHPModuleInstalled: true,
hasPassedCodeIntegrityCheck: true,
isOpcacheProperlySetup: true,
hasOpcacheLoaded: true,
OpcacheSetupRecommendations: [],
isSettimelimitAvailable: true,
hasFreeTypeSupport: true,
missingIndexes: [],
Expand Down Expand Up @@ -807,8 +797,7 @@ describe('OC.SetupChecks tests', function() {
forwardedForHeadersWorking: true,
isCorrectMemcachedPHPModuleInstalled: true,
hasPassedCodeIntegrityCheck: true,
isOpcacheProperlySetup: false,
hasOpcacheLoaded: true,
OpcacheSetupRecommendations: ['recommendation1', 'recommendation2'],
phpOpcacheDocumentation: 'https://example.org/link/to/doc',
isSettimelimitAvailable: true,
hasFreeTypeSupport: true,
Expand All @@ -833,7 +822,7 @@ describe('OC.SetupChecks tests', function() {

async.done(function( data, s, x ){
expect(data).toEqual([{
msg: 'The PHP OPcache module is not properly configured. <a target="_blank" rel="noreferrer noopener" class="external" href="https://example.org/link/to/doc">For better performance it is recommended ↗</a> to use the following settings in the <code>php.ini</code>:' + "<pre><code>opcache.enable=1\nopcache.interned_strings_buffer=8\nopcache.max_accelerated_files=10000\nopcache.memory_consumption=128\nopcache.save_comments=1\nopcache.revalidate_freq=1</code></pre>",
msg: 'The PHP OPcache module is not properly configured:<ul><li>recommendation1</li><li>recommendation2</li></ul>',
type: OC.SetupChecks.MESSAGE_TYPE_INFO
}]);
done();
Expand Down Expand Up @@ -862,8 +851,7 @@ describe('OC.SetupChecks tests', function() {
forwardedForHeadersWorking: true,
isCorrectMemcachedPHPModuleInstalled: true,
hasPassedCodeIntegrityCheck: true,
isOpcacheProperlySetup: true,
hasOpcacheLoaded: false,
OpcacheSetupRecommendations: [],
phpOpcacheDocumentation: 'https://example.org/link/to/doc',
isSettimelimitAvailable: true,
hasFreeTypeSupport: true,
Expand Down Expand Up @@ -917,8 +905,7 @@ describe('OC.SetupChecks tests', function() {
forwardedForHeadersWorking: true,
isCorrectMemcachedPHPModuleInstalled: true,
hasPassedCodeIntegrityCheck: true,
isOpcacheProperlySetup: true,
hasOpcacheLoaded: true,
OpcacheSetupRecommendations: [],
phpOpcacheDocumentation: 'https://example.org/link/to/doc',
isSettimelimitAvailable: true,
hasFreeTypeSupport: false,
Expand Down Expand Up @@ -972,8 +959,7 @@ describe('OC.SetupChecks tests', function() {
forwardedForHeadersWorking: true,
isCorrectMemcachedPHPModuleInstalled: true,
hasPassedCodeIntegrityCheck: true,
isOpcacheProperlySetup: true,
hasOpcacheLoaded: true,
OpcacheSetupRecommendations: [],
isSettimelimitAvailable: true,
hasFreeTypeSupport: true,
missingIndexes: [],
Expand Down Expand Up @@ -1030,8 +1016,7 @@ describe('OC.SetupChecks tests', function() {
forwardedForHeadersWorking: true,
isCorrectMemcachedPHPModuleInstalled: true,
hasPassedCodeIntegrityCheck: true,
isOpcacheProperlySetup: true,
hasOpcacheLoaded: true,
OpcacheSetupRecommendations: [],
isSettimelimitAvailable: true,
hasFreeTypeSupport: true,
missingIndexes: [],
Expand Down Expand Up @@ -1085,8 +1070,7 @@ describe('OC.SetupChecks tests', function() {
forwardedForHeadersWorking: true,
isCorrectMemcachedPHPModuleInstalled: true,
hasPassedCodeIntegrityCheck: true,
isOpcacheProperlySetup: true,
hasOpcacheLoaded: true,
OpcacheSetupRecommendations: [],
isSettimelimitAvailable: true,
hasFreeTypeSupport: true,
missingIndexes: [],
Expand Down Expand Up @@ -1137,8 +1121,7 @@ describe('OC.SetupChecks tests', function() {
forwardedForHeadersWorking: true,
isCorrectMemcachedPHPModuleInstalled: true,
hasPassedCodeIntegrityCheck: true,
isOpcacheProperlySetup: true,
hasOpcacheLoaded: true,
OpcacheSetupRecommendations: [],
isSettimelimitAvailable: true,
hasFreeTypeSupport: true,
missingIndexes: [],
Expand Down Expand Up @@ -1191,8 +1174,7 @@ describe('OC.SetupChecks tests', function() {
forwardedForHeadersWorking: true,
isCorrectMemcachedPHPModuleInstalled: true,
hasPassedCodeIntegrityCheck: true,
isOpcacheProperlySetup: true,
hasOpcacheLoaded: true,
OpcacheSetupRecommendations: [],
isSettimelimitAvailable: true,
hasFreeTypeSupport: true,
missingIndexes: [],
Expand Down Expand Up @@ -1245,8 +1227,7 @@ describe('OC.SetupChecks tests', function() {
forwardedForHeadersWorking: true,
isCorrectMemcachedPHPModuleInstalled: true,
hasPassedCodeIntegrityCheck: true,
isOpcacheProperlySetup: true,
hasOpcacheLoaded: true,
OpcacheSetupRecommendations: [],
isSettimelimitAvailable: true,
hasFreeTypeSupport: true,
missingIndexes: [],
Expand Down

0 comments on commit 4115c8d

Please sign in to comment.