PhpOpcacheSetup.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\Settings\SetupChecks;
  8. use bantu\IniGetWrapper\IniGetWrapper;
  9. use OCP\IL10N;
  10. use OCP\IURLGenerator;
  11. use OCP\SetupCheck\ISetupCheck;
  12. use OCP\SetupCheck\SetupResult;
  13. class PhpOpcacheSetup implements ISetupCheck {
  14. public function __construct(
  15. private IL10N $l10n,
  16. private IURLGenerator $urlGenerator,
  17. private IniGetWrapper $iniGetWrapper,
  18. ) {
  19. }
  20. public function getName(): string {
  21. return $this->l10n->t('PHP opcache');
  22. }
  23. public function getCategory(): string {
  24. return 'php';
  25. }
  26. /**
  27. * Checks whether a PHP OPcache is properly set up
  28. * @return array{'warning'|'error',list<string>} The level and the list of OPcache setup recommendations
  29. */
  30. protected function getOpcacheSetupRecommendations(): array {
  31. $level = 'warning';
  32. // If the module is not loaded, return directly to skip inapplicable checks
  33. if (!extension_loaded('Zend OPcache')) {
  34. return ['error',[$this->l10n->t('The PHP OPcache module is not loaded. For better performance it is recommended to load it into your PHP installation.')]];
  35. }
  36. $recommendations = [];
  37. // Check whether Nextcloud is allowed to use the OPcache API
  38. $isPermitted = true;
  39. $permittedPath = (string)$this->iniGetWrapper->getString('opcache.restrict_api');
  40. if ($permittedPath !== '' && !str_starts_with(\OC::$SERVERROOT, rtrim($permittedPath, '/'))) {
  41. $isPermitted = false;
  42. }
  43. if (!$this->iniGetWrapper->getBool('opcache.enable')) {
  44. $recommendations[] = $this->l10n->t('OPcache is disabled. For better performance, it is recommended to apply "opcache.enable=1" to your PHP configuration.');
  45. $level = 'error';
  46. } elseif ($this->iniGetWrapper->getBool('opcache.file_cache_only')) {
  47. $recommendations[] = $this->l10n->t('The shared memory based OPcache is disabled. For better performance, it is recommended to apply "opcache.file_cache_only=0" to your PHP configuration and use the file cache as second level cache only.');
  48. } else {
  49. // Check whether opcache_get_status has been explicitly disabled an in case skip usage based checks
  50. $disabledFunctions = $this->iniGetWrapper->getString('disable_functions');
  51. if (isset($disabledFunctions) && str_contains($disabledFunctions, 'opcache_get_status')) {
  52. return [$level, $recommendations];
  53. }
  54. $status = opcache_get_status(false);
  55. if ($status === false) {
  56. $recommendations[] = $this->l10n->t('OPcache is not working as it should, opcache_get_status() returns false, please check configuration.');
  57. $level = 'error';
  58. }
  59. // Recommend to raise value, if more than 90% of max value is reached
  60. if (
  61. empty($status['opcache_statistics']['max_cached_keys']) ||
  62. ($status['opcache_statistics']['num_cached_keys'] / $status['opcache_statistics']['max_cached_keys'] > 0.9)
  63. ) {
  64. $recommendations[] = $this->l10n->t('The maximum number of OPcache keys is nearly exceeded. To assure that all scripts can be kept in the cache, it is recommended to apply "opcache.max_accelerated_files" to your PHP configuration with a value higher than "%s".', [($this->iniGetWrapper->getNumeric('opcache.max_accelerated_files') ?: 'currently')]);
  65. }
  66. if (
  67. empty($status['memory_usage']['free_memory']) ||
  68. ($status['memory_usage']['used_memory'] / $status['memory_usage']['free_memory'] > 9)
  69. ) {
  70. $recommendations[] = $this->l10n->t('The OPcache buffer is nearly full. To assure that all scripts can be hold in cache, it is recommended to apply "opcache.memory_consumption" to your PHP configuration with a value higher than "%s".', [($this->iniGetWrapper->getNumeric('opcache.memory_consumption') ?: 'currently')]);
  71. }
  72. $interned_strings_buffer = $this->iniGetWrapper->getNumeric('opcache.interned_strings_buffer') ?? 0;
  73. $memory_consumption = $this->iniGetWrapper->getNumeric('opcache.memory_consumption') ?? 0;
  74. if (
  75. // Do not recommend to raise the interned strings buffer size above a quarter of the total OPcache size
  76. ($interned_strings_buffer < ($memory_consumption / 4)) &&
  77. (
  78. empty($status['interned_strings_usage']['free_memory']) ||
  79. ($status['interned_strings_usage']['used_memory'] / $status['interned_strings_usage']['free_memory'] > 9)
  80. )
  81. ) {
  82. $recommendations[] = $this->l10n->t('The OPcache interned strings buffer is nearly full. To assure that repeating strings can be effectively cached, it is recommended to apply "opcache.interned_strings_buffer" to your PHP configuration with a value higher than "%s".', [($this->iniGetWrapper->getNumeric('opcache.interned_strings_buffer') ?: 'currently')]);
  83. }
  84. }
  85. // 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.
  86. if (!$this->iniGetWrapper->getBool('opcache.save_comments')) {
  87. $recommendations[] = $this->l10n->t('OPcache is configured to remove code comments. With OPcache enabled, "opcache.save_comments=1" must be set for Nextcloud to function.');
  88. $level = 'error';
  89. }
  90. if (!$isPermitted) {
  91. $recommendations[] = $this->l10n->t('Nextcloud is not allowed to use the OPcache API. With OPcache enabled, it is highly recommended to include all Nextcloud directories with "opcache.restrict_api" or unset this setting to disable OPcache API restrictions, to prevent errors during Nextcloud core or app upgrades.');
  92. $level = 'error';
  93. }
  94. return [$level, $recommendations];
  95. }
  96. public function run(): SetupResult {
  97. [$level,$recommendations] = $this->getOpcacheSetupRecommendations();
  98. if (!empty($recommendations)) {
  99. return match($level) {
  100. 'error' => SetupResult::error(
  101. $this->l10n->t('The PHP OPcache module is not properly configured. %s.', implode("\n", $recommendations)),
  102. $this->urlGenerator->linkToDocs('admin-php-opcache')
  103. ),
  104. default => SetupResult::warning(
  105. $this->l10n->t('The PHP OPcache module is not properly configured. %s.', implode("\n", $recommendations)),
  106. $this->urlGenerator->linkToDocs('admin-php-opcache')
  107. ),
  108. };
  109. } else {
  110. return SetupResult::success($this->l10n->t('Correctly configured'));
  111. }
  112. }
  113. }