You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PhpOpcacheSetup.php 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2023 Côme Chilliet <come.chilliet@nextcloud.com>
  5. *
  6. * @author Côme Chilliet <come.chilliet@nextcloud.com>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\Settings\SetupChecks;
  25. use bantu\IniGetWrapper\IniGetWrapper;
  26. use OCP\IL10N;
  27. use OCP\IURLGenerator;
  28. use OCP\SetupCheck\ISetupCheck;
  29. use OCP\SetupCheck\SetupResult;
  30. class PhpOpcacheSetup implements ISetupCheck {
  31. public function __construct(
  32. private IL10N $l10n,
  33. private IURLGenerator $urlGenerator,
  34. private IniGetWrapper $iniGetWrapper,
  35. ) {
  36. }
  37. public function getName(): string {
  38. return $this->l10n->t('PHP opcache');
  39. }
  40. public function getCategory(): string {
  41. return 'php';
  42. }
  43. /**
  44. * Checks whether a PHP OPcache is properly set up
  45. * @return array{'warning'|'error',list<string>} The level and the list of OPcache setup recommendations
  46. */
  47. protected function getOpcacheSetupRecommendations(): array {
  48. $level = 'warning';
  49. // If the module is not loaded, return directly to skip inapplicable checks
  50. if (!extension_loaded('Zend OPcache')) {
  51. 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.')]];
  52. }
  53. $recommendations = [];
  54. // Check whether Nextcloud is allowed to use the OPcache API
  55. $isPermitted = true;
  56. $permittedPath = (string)$this->iniGetWrapper->getString('opcache.restrict_api');
  57. if ($permittedPath !== '' && !str_starts_with(\OC::$SERVERROOT, rtrim($permittedPath, '/'))) {
  58. $isPermitted = false;
  59. }
  60. if (!$this->iniGetWrapper->getBool('opcache.enable')) {
  61. $recommendations[] = $this->l10n->t('OPcache is disabled. For better performance, it is recommended to apply "opcache.enable=1" to your PHP configuration.');
  62. $level = 'error';
  63. } elseif ($this->iniGetWrapper->getBool('opcache.file_cache_only')) {
  64. $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.');
  65. } else {
  66. // Check whether opcache_get_status has been explicitly disabled an in case skip usage based checks
  67. $disabledFunctions = $this->iniGetWrapper->getString('disable_functions');
  68. if (isset($disabledFunctions) && str_contains($disabledFunctions, 'opcache_get_status')) {
  69. return [$level, $recommendations];
  70. }
  71. $status = opcache_get_status(false);
  72. if ($status === false) {
  73. $recommendations[] = $this->l10n->t('OPcache is not working as it should, opcache_get_status() returns false, please check configuration.');
  74. $level = 'error';
  75. }
  76. // Recommend to raise value, if more than 90% of max value is reached
  77. if (
  78. empty($status['opcache_statistics']['max_cached_keys']) ||
  79. ($status['opcache_statistics']['num_cached_keys'] / $status['opcache_statistics']['max_cached_keys'] > 0.9)
  80. ) {
  81. $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')]);
  82. }
  83. if (
  84. empty($status['memory_usage']['free_memory']) ||
  85. ($status['memory_usage']['used_memory'] / $status['memory_usage']['free_memory'] > 9)
  86. ) {
  87. $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')]);
  88. }
  89. if (
  90. // Do not recommend to raise the interned strings buffer size above a quarter of the total OPcache size
  91. ($this->iniGetWrapper->getNumeric('opcache.interned_strings_buffer') ?? $this->iniGetWrapper->getNumeric('opcache.memory_consumption') > 0 ?? 0 / 4) &&
  92. (
  93. empty($status['interned_strings_usage']['free_memory']) ||
  94. ($status['interned_strings_usage']['used_memory'] / $status['interned_strings_usage']['free_memory'] > 9)
  95. )
  96. ) {
  97. $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')]);
  98. }
  99. }
  100. // 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.
  101. if (!$this->iniGetWrapper->getBool('opcache.save_comments')) {
  102. $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.');
  103. $level = 'error';
  104. }
  105. if (!$isPermitted) {
  106. $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.');
  107. $level = 'error';
  108. }
  109. return [$level, $recommendations];
  110. }
  111. public function run(): SetupResult {
  112. [$level,$recommendations] = $this->getOpcacheSetupRecommendations();
  113. if (!empty($recommendations)) {
  114. return match($level) {
  115. 'error' => SetupResult::error(
  116. $this->l10n->t('The PHP OPcache module is not properly configured. %s.', implode("\n", $recommendations)),
  117. $this->urlGenerator->linkToDocs('admin-php-opcache')
  118. ),
  119. default => SetupResult::warning(
  120. $this->l10n->t('The PHP OPcache module is not properly configured. %s.', implode("\n", $recommendations)),
  121. $this->urlGenerator->linkToDocs('admin-php-opcache')
  122. ),
  123. };
  124. } else {
  125. return SetupResult::success($this->l10n->t('Correctly configured'));
  126. }
  127. }
  128. }