|
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 OSXFacade |
|
15
|
|
|
{ |
|
16
|
3 |
|
public function __invoke(): Memory |
|
17
|
|
|
{ |
|
18
|
|
|
$total = $this |
|
19
|
3 |
|
->run('sysctl hw.memsize') |
|
20
|
|
|
->trim() |
|
21
|
|
|
->capture('~^hw.memsize: (?P<total>\d+)$~') |
|
22
|
|
|
->get('total'); |
|
23
|
|
|
$swap = $this |
|
24
|
|
|
->run('sysctl vm.swapusage') |
|
25
|
|
|
->trim() |
|
26
|
|
|
->capture('~used = (?P<swap>\d+[\.,]?\d*[KMGTP])~') |
|
27
|
|
|
->get('swap'); |
|
28
|
|
|
$amounts = $this |
|
29
|
|
|
->run('top -l 1 -s 0 | grep PhysMem') |
|
30
|
|
|
->trim() |
|
31
|
|
|
->capture( |
|
32
|
|
|
'~^PhysMem: (?P<used>\d+[KMGTP]) used \((?P<wired>\d+[KMGTP]) wired\), (?P<unused>\d+[KMGTP]) unused.$~' |
|
33
|
|
|
); |
|
34
|
|
|
$active = $this |
|
35
|
|
|
->run('vm_stat | grep \'Pages active\'') |
|
36
|
|
|
->trim() |
|
37
|
|
|
->capture('~(?P<active>\d+)~') |
|
38
|
|
|
->get('active'); |
|
39
|
|
|
|
|
40
|
|
|
return new Memory( |
|
41
|
|
|
new Bytes((int) $total->toString()), |
|
42
|
|
|
Bytes::of($amounts->get('wired')->toString()), |
|
43
|
|
|
new Bytes(((int) $active->toString()) * 4096), |
|
44
|
|
|
Bytes::of($amounts->get('unused')->toString()), |
|
45
|
|
|
Bytes::of($swap->toString()), |
|
46
|
|
|
Bytes::of($amounts->get('used')->toString()), |
|
47
|
|
|
); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
3 |
|
private function run(string $command): Str |
|
51
|
|
|
{ |
|
52
|
3 |
|
$process = Process::fromShellCommandline($command); |
|
53
|
3 |
|
$process->run(); |
|
54
|
|
|
|
|
55
|
3 |
|
if (!$process->isSuccessful()) { |
|
56
|
3 |
|
throw new MemoryUsageNotAccessible; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
return Str::of($process->getOutput()); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|