Completed
Push — master ( bd4a48...a13ca6 )
by Branko
12s queued 10s
created

MemoryLimits::getTotalMemoryLinux()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

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