|
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
|
|
|
// 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 = intval($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 int 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(): int { |
|
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 int 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(): int { |
|
80
|
|
|
if (php_uname("s") !== "Linux") |
|
81
|
|
|
return -1; |
|
82
|
|
|
|
|
83
|
|
|
$linuxMemory = MemoryLimits::getTotalMemoryLinux(); |
|
84
|
|
|
if ($linuxMemory <= 0) { |
|
85
|
|
|
return -2; |
|
86
|
|
|
} |
|
87
|
|
|
return $linuxMemory; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @return int Total memory available on linux system, in bytes, or |
|
92
|
|
|
* zero if we don't know any better. |
|
93
|
|
|
*/ |
|
94
|
|
|
private static function getTotalMemoryLinux(): int { |
|
95
|
|
|
$fh = fopen('/proc/meminfo','r'); |
|
96
|
|
|
if ($fh === false) { |
|
97
|
|
|
return 0; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
$mem = 0; |
|
101
|
|
|
while ($line = fgets($fh)) { |
|
102
|
|
|
$pieces = array(); |
|
103
|
|
|
if (preg_match('/^MemTotal:\s+(\d+)\skB$/', $line, $pieces)) { |
|
104
|
|
|
$mem = $pieces[1]; |
|
105
|
|
|
break; |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
fclose($fh); |
|
109
|
|
|
$memKb = intval($mem); |
|
110
|
|
|
return intval($memKb * 1024); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Converts shorthand memory notation value to bytes |
|
115
|
|
|
* From http://php.net/manual/en/function.ini-get.php |
|
116
|
|
|
* |
|
117
|
|
|
* @param string $val Memory size shorthand notation string |
|
118
|
|
|
* |
|
119
|
|
|
* @return int Value in integers (bytes) |
|
120
|
|
|
*/ |
|
121
|
1 |
|
public static function returnBytes(string $val): int { |
|
122
|
1 |
|
$val = trim($val); |
|
123
|
1 |
|
if ($val === "") { |
|
124
|
1 |
|
return 0; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
1 |
|
$valInt = intval($val); |
|
128
|
1 |
|
if ($valInt === 0) { |
|
129
|
1 |
|
return 0; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
1 |
|
$last = strtolower($val[strlen($val)-1]); |
|
133
|
|
|
switch($last) { |
|
134
|
1 |
|
case 'g': |
|
135
|
1 |
|
$valInt *= 1024; |
|
136
|
|
|
// Fallthrough on purpose |
|
137
|
1 |
|
case 'm': |
|
138
|
1 |
|
$valInt *= 1024; |
|
139
|
|
|
// Fallthrough on purpose |
|
140
|
1 |
|
case 'k': |
|
141
|
1 |
|
$valInt *= 1024; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
1 |
|
return $valInt; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
} |