Passed
Push — memory-logic ( 482239 )
by Matias
04:06
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 2
Bugs 0 Features 0
Metric Value
cc 4
eloc 12
c 2
b 0
f 0
nc 4
nop 0
dl 0
loc 17
ccs 0
cts 13
cp 0
crap 20
rs 9.8666
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
/**
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
		$availableMemory = -1;
0 ignored issues
show
Unused Code introduced by
The assignment to $availableMemory is dead and can be removed.
Loading history...
43
		// Try first to get from php.ini
44
		try {
45
			$ini_value = ini_get('memory_limit');
46
			$availableMemory = MemoryLimits::returnBytes($ini_value);
47
		} catch (\Exception $e) {
48
			return -1;
49
		}
50
51
		// php.ini says that memory_limit is -1, which means unlimited.
52
		// We need to get memory from system (if OS is supported here).
53
		// Only linux is currently supported.
54
		if ($availableMemory < 0) {
55
			$systemMemory = MemoryLimits::getSystemMemory();
56
			if ($systemMemory < 0)
57
				return -1;
58
			$availableMemory = intval($systemMemory / 2);
59
		}
60
61
		return $availableMemory;
62
	}
63
64
	/**
65
	 * @return int Total memory available on system, in bytes, or negative if
66
	 * we don't know any better
67
	 * Only linux is currently supported.
68
	 */
69
	public static function getSystemMemory(): int {
70
		if (php_uname("s") !== "Linux")
71
			return -1;
72
73
		$linuxMemory = MemoryLimits::getTotalMemoryLinux();
74
		if ($linuxMemory <= 0) {
75
			return -2;
76
		}
77
		return $linuxMemory;
78
	}
79
80
	/**
81
	 * @return int Total memory available on linux system, in bytes, or
82
	 * zero if we don't know any better.
83
	 */
84
	private static function getTotalMemoryLinux(): int {
85
		$fh = fopen('/proc/meminfo','r');
86
		if ($fh === false) {
87
			return 0;
88
		}
89
90
		$mem = 0;
91
		while ($line = fgets($fh)) {
92
			$pieces = array();
93
			if (preg_match('/^MemTotal:\s+(\d+)\skB$/', $line, $pieces)) {
94
				$mem = $pieces[1];
95
				break;
96
			}
97
		}
98
		fclose($fh);
99
		$memKb = intval($mem);
100
		return intval($memKb * 1024);
101
	}
102
103
	/**
104
	 * Converts shorthand memory notation value to bytes
105
	 * From http://php.net/manual/en/function.ini-get.php
106
	 *
107
	 * @param string $val Memory size shorthand notation string
108
	 *
109
	 * @return int Value in integers (bytes)
110
	 */
111 1
	public static function returnBytes(string $val): int {
112 1
		$val = trim($val);
113 1
		if ($val === "") {
114 1
			return 0;
115
		}
116
117 1
		$valInt = intval($val);
118 1
		if ($valInt === 0) {
119 1
			return 0;
120
		}
121
122 1
		$last = strtolower($val[strlen($val)-1]);
123
		switch($last) {
124 1
			case 'g':
125 1
				$valInt *= 1024;
126
				// Fallthrough on purpose
127 1
			case 'm':
128 1
				$valInt *= 1024;
129
				// Fallthrough on purpose
130 1
			case 'k':
131 1
				$valInt *= 1024;
132
		}
133
134 1
		return $valInt;
135
	}
136
137
}