1
0

MemoryInfo.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2018, Michael Weimann (<mail@michael-weimann.eu>)
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Michael Weimann <mail@michael-weimann.eu>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OC;
  26. use OCP\Util;
  27. /**
  28. * Helper class that covers memory info.
  29. */
  30. class MemoryInfo {
  31. public const RECOMMENDED_MEMORY_LIMIT = 512 * 1024 * 1024;
  32. /**
  33. * Tests if the memory limit is greater or equal the recommended value.
  34. *
  35. * @return bool
  36. */
  37. public function isMemoryLimitSufficient(): bool {
  38. $memoryLimit = $this->getMemoryLimit();
  39. return $memoryLimit === -1 || $memoryLimit >= self::RECOMMENDED_MEMORY_LIMIT;
  40. }
  41. /**
  42. * Returns the php memory limit.
  43. *
  44. * @return int|float The memory limit in bytes.
  45. */
  46. public function getMemoryLimit(): int|float {
  47. $iniValue = trim(ini_get('memory_limit'));
  48. if ($iniValue === '-1') {
  49. return -1;
  50. } elseif (is_numeric($iniValue)) {
  51. return Util::numericToNumber($iniValue);
  52. } else {
  53. return $this->memoryLimitToBytes($iniValue);
  54. }
  55. }
  56. /**
  57. * Converts the ini memory limit to bytes.
  58. *
  59. * @param string $memoryLimit The "memory_limit" ini value
  60. */
  61. private function memoryLimitToBytes(string $memoryLimit): int|float {
  62. $last = strtolower(substr($memoryLimit, -1));
  63. $number = substr($memoryLimit, 0, -1);
  64. if (is_numeric($number)) {
  65. $memoryLimit = Util::numericToNumber($number);
  66. } else {
  67. throw new \InvalidArgumentException($number.' is not a valid numeric string (in memory_limit ini directive)');
  68. }
  69. // intended fall through
  70. switch ($last) {
  71. case 'g':
  72. $memoryLimit *= 1024;
  73. // no break
  74. case 'm':
  75. $memoryLimit *= 1024;
  76. // no break
  77. case 'k':
  78. $memoryLimit *= 1024;
  79. }
  80. return $memoryLimit;
  81. }
  82. }