Completed
Push — master ( 050fb9...428fac )
by Matias
16s queued 13s
created

MemoryLimits::getTotalMemoryLinux()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

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