|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Innmind\ProvisionerBundle; |
|
4
|
|
|
|
|
5
|
|
|
use Innmind\ProvisionerBundle\Server\ServerInterface; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Holds informations about a process througthout a symfony runtime |
|
9
|
|
|
*/ |
|
10
|
|
|
class ProcessStatusHandler |
|
11
|
|
|
{ |
|
12
|
|
|
const COMPUTE_ITERATIONS = 3; |
|
13
|
|
|
const ITERATIONS_SLEEP = 2; |
|
14
|
|
|
|
|
15
|
|
|
protected $informations = []; |
|
16
|
|
|
protected $server; |
|
17
|
|
|
protected $usePrecision = true; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Set the server helper |
|
21
|
|
|
* |
|
22
|
|
|
* @param ServerInterface $server |
|
23
|
|
|
*/ |
|
24
|
39 |
|
public function setServer(ServerInterface $server) |
|
25
|
|
|
{ |
|
26
|
39 |
|
$this->server = $server; |
|
27
|
39 |
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Whether to run compute multiple times cpu usage |
|
31
|
|
|
* to get a better approximation or not |
|
32
|
|
|
* |
|
33
|
|
|
* @param bool $use |
|
34
|
|
|
*/ |
|
35
|
39 |
|
public function setUsePrecision($use) |
|
36
|
|
|
{ |
|
37
|
39 |
|
$this->usePrecision = (bool) $use; |
|
38
|
39 |
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Return the number of process running for the givent command |
|
42
|
|
|
* |
|
43
|
|
|
* @param string $command |
|
44
|
|
|
* |
|
45
|
|
|
* @return int |
|
46
|
|
|
*/ |
|
47
|
27 |
View Code Duplication |
public function getProcessCount($command) |
|
48
|
|
|
{ |
|
49
|
27 |
|
$hash = $this->getKey($command); |
|
50
|
|
|
|
|
51
|
27 |
|
if (!isset($this->informations[$hash])) { |
|
52
|
24 |
|
$this->computeProcessInformations($command); |
|
53
|
24 |
|
} |
|
54
|
|
|
|
|
55
|
27 |
|
return $this->informations[$hash]['number']; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Return the average CPU usage for the given command |
|
60
|
|
|
* |
|
61
|
|
|
* @param string $command |
|
62
|
|
|
* |
|
63
|
|
|
* @return float |
|
64
|
|
|
*/ |
|
65
|
6 |
View Code Duplication |
public function getProcessUsage($command) |
|
66
|
|
|
{ |
|
67
|
6 |
|
$hash = $this->getKey($command); |
|
68
|
|
|
|
|
69
|
6 |
|
if (!isset($this->informations[$hash])) { |
|
70
|
6 |
|
$this->computeProcessInformations($command); |
|
71
|
6 |
|
} |
|
72
|
|
|
|
|
73
|
6 |
|
return $this->informations[$hash]['usage']; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Compute the process usage/number for the given command |
|
78
|
|
|
* |
|
79
|
|
|
* @param string $command |
|
80
|
|
|
*/ |
|
81
|
30 |
|
protected function computeProcessInformations($command) |
|
82
|
|
|
{ |
|
83
|
30 |
|
$averageProcessUsage = 0; |
|
84
|
30 |
|
$averageProcessNumber = 0; |
|
85
|
30 |
|
$iterations = $this->usePrecision ? self::COMPUTE_ITERATIONS : 1; |
|
86
|
|
|
|
|
87
|
30 |
|
for ($i = 0; $i < $iterations; $i++) { |
|
88
|
30 |
|
$averageProcessUsage += $this->server->getProcessUsage($command); |
|
89
|
30 |
|
$averageProcessNumber += $this->server->getProcessCount($command); |
|
90
|
|
|
|
|
91
|
30 |
|
if ($this->usePrecision === true) { |
|
92
|
|
|
sleep(self::ITERATIONS_SLEEP); |
|
93
|
|
|
} |
|
94
|
30 |
|
} |
|
95
|
|
|
|
|
96
|
30 |
|
$averageProcessUsage = $averageProcessUsage / $iterations; |
|
97
|
30 |
|
$averageProcessNumber = floor($averageProcessNumber / $iterations); |
|
98
|
|
|
|
|
99
|
30 |
|
$this->informations[$this->getKey($command)] = [ |
|
100
|
30 |
|
'usage' => (float) $averageProcessUsage, |
|
101
|
|
|
'number' => (int) $averageProcessNumber |
|
102
|
30 |
|
]; |
|
103
|
30 |
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Return a unique key for the given command |
|
107
|
|
|
* |
|
108
|
|
|
* @param string $command |
|
109
|
|
|
* |
|
110
|
|
|
* @return string |
|
111
|
|
|
*/ |
|
112
|
30 |
|
protected function getKey($command) |
|
113
|
|
|
{ |
|
114
|
30 |
|
return md5($command); |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|