1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace Innmind\Server\Status\Facade\Memory; |
5
|
|
|
|
6
|
|
|
use Innmind\Server\Status\{ |
7
|
|
|
Server\Memory, |
8
|
|
|
Server\Memory\Bytes, |
9
|
|
|
Exception\MemoryUsageNotAccessible |
10
|
|
|
}; |
11
|
|
|
use Innmind\Immutable\Str; |
12
|
|
|
use Symfony\Component\Process\Process; |
13
|
|
|
|
14
|
|
|
final class LinuxFacade |
15
|
|
|
{ |
16
|
|
|
private $entries = [ |
17
|
|
|
'MemTotal' => 'total', |
18
|
|
|
'Active' => 'active', |
19
|
|
|
'Inactive' => 'inactive', |
20
|
|
|
'MemFree' => 'free', |
21
|
|
|
'SwapCached' => 'swap', |
22
|
|
|
]; |
23
|
|
|
|
24
|
|
|
public function __invoke(): Memory |
25
|
|
|
{ |
26
|
|
|
$process = new Process('cat /proc/meminfo'); |
27
|
|
|
$process->run(); |
28
|
|
|
|
29
|
|
|
if (!$process->isSuccessful()) { |
30
|
|
|
throw new MemoryUsageNotAccessible; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
$amounts = (new Str($process->getOutput())) |
34
|
|
|
->trim() |
35
|
|
|
->split("\n") |
36
|
|
|
->filter(static function(Str $line): bool { |
37
|
|
|
return $line->matches( |
38
|
|
|
'('.implode('|', array_keys($this->entries)).')' |
39
|
|
|
); |
40
|
|
|
}) |
41
|
|
|
->reduce( |
42
|
|
|
new Map('string', 'int'), |
43
|
|
|
static function(Map $map, Str $line): Map { |
44
|
|
|
$elements = $line->capture('~^(?P<key>\s+): +(?P<value>\d+) kB$~'); |
45
|
|
|
|
46
|
|
|
return $map->put( |
47
|
|
|
$this->entries[(string) $elements->get('key')], |
|
|
|
|
48
|
|
|
((int) (string) $elements->get('value')) * Bytes::BYTES |
|
|
|
|
49
|
|
|
); |
50
|
|
|
} |
51
|
|
|
); |
52
|
|
|
|
53
|
|
|
$used = $amounts->get('total') - $amounts->get('free'); |
54
|
|
|
$wired = $used - $amounts->get('active') - $amounts->get('inactive'); |
55
|
|
|
|
56
|
|
|
return new Memory( |
57
|
|
|
new Bytes($amounts->get('total')), |
58
|
|
|
new Bytes($wired), |
59
|
|
|
new Bytes($amounts->get('active')), |
60
|
|
|
new Bytes($amounts->get('free')), |
61
|
|
|
new Bytes($amounts->get('swap')), |
62
|
|
|
new Bytes($used) |
63
|
|
|
); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: