Passed
Push — scaling-calculation ( b4cbb7 )
by Branko
02:10
created

MemoryLimits   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 36
dl 0
loc 78
ccs 0
cts 48
cp 0
rs 10
c 0
b 0
f 0
wmc 13

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getTotalMemoryLinux() 0 13 3
A returnBytes() 0 18 5
A getAvailableMemory() 0 22 5
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 system 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
		$mem = 0;
69
		while ($line = fgets($fh)) {
0 ignored issues
show
Bug introduced by
It seems like $fh can also be of type false; however, parameter $handle of fgets() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

69
		while ($line = fgets(/** @scrutinizer ignore-type */ $fh)) {
Loading history...
70
			$pieces = array();
71
			if (preg_match('/^MemTotal:\s+(\d+)\skB$/', $line, $pieces)) {
72
				$mem = $pieces[1];
73
				break;
74
			}
75
		}
76
		fclose($fh);
0 ignored issues
show
Bug introduced by
It seems like $fh can also be of type false; however, parameter $handle of fclose() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

76
		fclose(/** @scrutinizer ignore-type */ $fh);
Loading history...
77
		$memKb = intval($mem);
78
		return $memKb * 1024;
79
	}
80
81
	/**
82
	 * Converts shorthand memory notation value to bytes
83
	 * From http://php.net/manual/en/function.ini-get.php
84
	 *
85
	 * @param string val Memory size shorthand notation string
0 ignored issues
show
Bug introduced by
The type OCA\FaceRecognition\Helper\val was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
86
	 *
87
	 * @return int Value in integers (bytes)
88
	 */
89
	private static function returnBytes(string $val): int {
90
		$val = trim($val);
91
		if ($val === "") {
92
			return 0;
93
		}
94
95
		$last = strtolower($val[strlen($val)-1]);
96
		switch($last) {
97
			// Fallthrough on purpose
98
			case 'g':
99
				$val *= 1024;
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment if this fall-through is intended.
Loading history...
100
			case 'm':
101
				$val *= 1024;
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment if this fall-through is intended.
Loading history...
102
			case 'k':
103
				$val *= 1024;
104
		}
105
106
		return $val;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $val returns the type string which is incompatible with the type-hinted return integer.
Loading history...
107
	}
108
}