forked from ryoppy/Get-MaxClients.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_max_client.php
211 lines (185 loc) · 4.87 KB
/
get_max_client.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<?php
//--------------------------------------------------------------------------------------
// Usage: php get_max_client.php
// root only.
//
// This program is used to get MaxClient of apache.
// Following step.
// 1、get all pid of httpd.
// 2、get average Rss of "/proc/$pid/smaps".
// 3、get average Shared_Clean of "/proc/$pid/smaps".
// 4、get average Shared_Dirty of "/proc/$pid/smaps".
// 5、get memory size by free command.
// 6、math max client by "memorySize / (rssAverage - (Shared_Clean + Shared_Dirty))".
// 7、print max client.
//--------------------------------------------------------------------------------------
$GLOBALS['debug'] = false;
/**
* Dump data.
* If $GLOBALS['debug'] is true, dump.
*
* @param mixd $value
* @param string $label
*/
function debugDump ($value, $label = null) {
if ($GLOBALS['debug']) {
if ($label) {
$value = $label . ' : ' . $value;
}
var_dump($value);
}
}
/**
* command execute. error handling and logging.
* if command is error, it will end. (exit(1))
*
* @param string $arg
* @return string
*/
function executeCommand ($arg)
{
$result = `$arg 2>&1`;
if (substr(`echo $?`, 0, 1) === '0') {
// success
return rtrim($result);
} else {
// failure
error_log(sprintf('command failure [%s][%s]', $arg, $result));
echo 0;
exit(1);
}
}
/**
* This method is run.
* If you want to see debug data, see this page top.
*
*/
function run ()
{
$httpdPidArr = getHttpdPidArr();
$rss = getRssAverage($httpdPidArr);
$shr = getShrAverage($httpdPidArr);
$memorySize = getMemorySize();
$maxClient = mathMaxClient($shr, $rss, $memorySize);
$usedMemory = $rss - $shr;
debugDump($rss, '$rss');
debugDump($shr, '$shr');
debugDump($memorySize, '$memorySize');
debugDump($maxClient, '$maxClient');
echo sprintf('--------------------------------') . PHP_EOL;
echo sprintf('rssAverage - shrAverage = (%dKB - %dKB) = %dKB', $rss, $shr, $usedMemory) . PHP_EOL;
echo sprintf('memorySize / (rssAverage - shrAverage) = %dKB / %dKB = %d', $memorySize, $usedMemory, $maxClient) . PHP_EOL;
echo sprintf('MaxClient maximum value is %d.', $maxClient) . PHP_EOL;
echo sprintf('--------------------------------') . PHP_EOL;
}
/**
* Get httpd pid.
*
* @return array
*/
function getHttpdPidArr ()
{
$httpdPids = executeCommand('pgrep httpd');
if (empty($httpdPids)) {
throw new Exception("Not found httpdPids. failure pgrep httpd.");
}
return explode(PHP_EOL, $httpdPids);
}
/**
* Get rss.
* This method is browse /proc/$pid/smaps.
*
* @return int
*/
function getRss ($pid)
{
$rss = executeCommand("cat /proc/$pid/smaps | grep Rss | awk '{rss += $2} END {print rss;};'");
if (empty($rss)) {
throw new Exception("Rss not found. /proc/$pid/smaps not found?.");
}
return (int) $rss;
}
/**
* Get rss average.
*
* @return int
*/
function getRssAverage ($httpdPidArr)
{
foreach ($httpdPidArr as $httpdPid) {
$rssArr[] = $debug = getRss($httpdPid);
}
return (int) round(array_sum($rssArr) / count($rssArr));
}
/**
* Get rss.
* This method is browse /proc/$pid/smaps.
*
* @return int
*/
function getShrClean ($pid)
{
$shrClean = executeCommand("cat /proc/$pid/smaps | grep Shared_Clean | awk '{shrc += $2} END {print shrc;};'");
if (empty($shrClean)) {
throw new Exception("Shared_Clean not found. /proc/$pid/smaps not found?.");
}
return (int) $shrClean;
}
/**
* Get rss.
* This method is browse /proc/$pid/smaps.
*
* @return int
*/
function getShrDirty ($pid)
{
$shrDirty = executeCommand("cat /proc/$pid/smaps | grep Shared_Dirty | awk '{shrd += $2} END {print shrd;};'");
if (empty($shrDirty)) {
throw new Exception("Shared_Dirty not found. /proc/$pid/smaps not found?.");
}
return (int) $shrDirty;
}
/**
* Get shr average.
* (Shared_Clean + Shared_Dirty) / count
*
* @return int
*/
function getShrAverage ($httpdPidArr)
{
foreach ($httpdPidArr as $httpdPid) {
$shrClean = getShrClean($httpdPid);
$shrDirty = getShrDirty($httpdPid);
debugDump("pid=$httpdPid : shrClean $shrClean");
debugDump($shrDirty, '$shrDirty');
$shrArr[] = $shrClean + $shrDirty;
}
return (int) round(array_sum($shrArr) / count($shrArr));
}
/**
* Get memory size.
* This method is execute free command.
*
* @return int
*/
function getMemorySize ()
{
$result = executeCommand("free | grep Mem | awk '{print $2;};'");
if (empty($result)) {
throw new Exception('memory size not found. failure free command...');
}
return $result;
}
/**
* Math MaxClient.
*
* @param int $shr
* @param int $rss
* @param int $memorySize
* @return int MaxClient
*/
function mathMaxClient ($shr, $rss, $memorySize)
{
return (int) round($memorySize / ($rss - $shr));
}
run();