Passed
Push — dependabot/npm_and_yarn/decode... ( a4b439 )
by
unknown
11:31
created

MemoryLimits::returnBytes()   A

Complexity

Conditions 6
Paths 10

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
eloc 15
nc 10
nop 1
dl 0
loc 24
ccs 15
cts 15
cp 1
crap 6
rs 9.2222
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2017,2020, Matias De lellis <[email protected]>
4
 * @copyright Copyright (c) 2018-2019, Branko Kokanovic <[email protected]>
5
 *
6
 * @author Branko Kokanovic <[email protected]>
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\FaceRecognition\Helper;
25
26
use OCA\FaceRecognition\Service\SettingsService;
27
28
/**
29
 * Tries to get total amount of memory on the host, given to PHP.
30
 */
31
class MemoryLimits {
32
33
	/**
34
	 * Tries to get memory available to PHP. This is highly speculative business.
35
	 * It will first try reading value of "memory_limit" and if it is -1, it will
36
	 * try to get 1/2 memory of host system. In case of any error, it will return
37
	 * negative value. Note that negative here doesn't mean "unlimited"! This
38
	 * function doesn't care if PHP is being used in CLI or FPM mode.
39
	 *
40
	 * @return float Total memory available to PHP, in bytes, or negative if
41
	 * we don't know any better
42
	 */
43
	public static function getAvailableMemory(): float {
44
		// Try first to get from php.ini
45
		$availableMemory = MemoryLimits::getPhpMemory();
46
47
		// php.ini says that memory_limit is -1, which means unlimited.
48
		// We need to get memory from system (if OS is supported here).
49
		// Only linux is currently supported.
50
		if ($availableMemory < 0) {
51
			$systemMemory = MemoryLimits::getSystemMemory();
52
			if ($systemMemory < 0)
53
				return -1;
54
			$availableMemory = ($systemMemory * 2 / 3);
55
			$availableMemory = min($availableMemory, SettingsService::MAXIMUN_ASSIGNED_MEMORY);
56
		}
57
		return $availableMemory;
58
	}
59
60
	/**
61
	 * Tries to get memory available to PHP reading value of "memory_limit".
62
	 *
63
	 * @return float Total memory available to PHP, in bytes, or negative if
64
	 * we don't know any better or it is unlimited.
65
	 */
66
	public static function getPhpMemory(): float {
67
		// Get from php.ini
68
		try {
69
			$ini_value = ini_get('memory_limit');
70
			$availableMemory = MemoryLimits::returnBytes($ini_value);
71
		} catch (\Exception $e) {
72
			$availableMemory = -1;
73
		}
74
		return $availableMemory;
75
	}
76
77
	/**
78
	 * @return float Total memory available on system, in bytes, or negative if
79
	 * we don't know any better
80
	 * Only linux is currently supported.
81
	 */
82 1
	public static function getSystemMemory(): float {
83 1
		if (php_uname("s") !== "Linux")
84
			return -1;
85
86 1
		$linuxMemory = MemoryLimits::getTotalMemoryLinux();
87 1
		if ($linuxMemory <= 0) {
88
			return -2;
89
		}
90
91
92 1
		return $linuxMemory;
93
	}
94
95
	/**
96
	 * @return float Total memory available on linux system, in bytes, or
97
	 * zero if we don't know any better.
98
	 */
99 1
	private static function getTotalMemoryLinux(): float {
100 1
		$fh = fopen('/proc/meminfo','r');
101 1
		if ($fh === false) {
102
			return 0;
103
		}
104 1
		$mem = 0;
105 1
		while ($line = fgets($fh)) {
106 1
			$pieces = array();
107 1
			if (preg_match('/^MemTotal:\s+(\d+)\skB$/', $line, $pieces)) {
108 1
				$mem = $pieces[1];
109 1
				break;
110
			}
111
112
		}
113 1
		fclose($fh);
114
115 1
		return $mem * 1024;
116
	}
117
118
	/**
119
	 * Converts shorthand memory notation value to bytes
120
	 * From http://php.net/manual/en/function.ini-get.php
121
	 *
122
	 * @param string $val Memory size shorthand notation string
123
	 *
124
	 * @return int Value in integers (bytes)
125
	 */
126 1
	public static function returnBytes(string $val): int {
127 1
		$val = trim($val);
128 1
		if ($val === "") {
129 1
			return 0;
130
		}
131
132 1
		$valInt = intval($val);
133 1
		if ($valInt === 0) {
134 1
			return 0;
135
		}
136
137 1
		$last = strtolower($val[strlen($val)-1]);
138
		switch($last) {
139 1
			case 'g':
140 1
				$valInt *= 1024;
141
				// Fallthrough on purpose
142 1
			case 'm':
143 1
				$valInt *= 1024;
144
				// Fallthrough on purpose
145 1
			case 'k':
146 1
				$valInt *= 1024;
147
		}
148
149 1
		return $valInt;
150
	}
151
152
}