-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathphp-fpm-tuner.php
More file actions
48 lines (38 loc) · 1.56 KB
/
php-fpm-tuner.php
File metadata and controls
48 lines (38 loc) · 1.56 KB
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
<?php
function getCpuCores() {
if (PHP_OS_FAMILY == 'Windows') {
$cores = shell_exec('echo %NUMBER_OF_PROCESSORS%');
} else {
$cores = shell_exec('nproc');
}
return (int) $cores;
}
function getFreeMemory() {
if (PHP_OS_FAMILY == 'Windows' && preg_match('~(\d+)~', shell_exec('wmic OS get FreePhysicalMemory'), $matches)) {
$freeMemory = round((int) $matches[1] / 1024);
} else {
if (preg_match('~MemFree:\s+(\d+)\s+~', shell_exec('cat /proc/meminfo'), $matches)) {
$freeMemory = $matches[1] / 1024;
}
}
return (int) $freeMemory;
}
function getWorkerMemory() {
if (PHP_OS_FAMILY !== 'Windows' && preg_match_all('~(\d+).*php-fpm: pool~', shell_exec('ps -eo size,command'), $matches, PREG_PATTERN_ORDER)) {
$processMemory = round(array_sum($matches[1]) / count($matches[1]) / 1024);
}
if (!isset($processMemory)) {
$processMemory = round(ini_parse_quantity(ini_get('memory_limit')) / 1048576);
}
return (int) $processMemory;
}
$cpuCores = getCpuCores();
$freeMemory = getFreeMemory();
$workerMemory = getWorkerMemory();
// reserve 10% for system use
$memoryReserve = round(0.1 * $freeMemory);
$maxChildren = floor(($freeMemory - $memoryReserve) / $workerMemory);
echo "pm.max_children = " . $maxChildren . "\n";
echo "pm.start_servers = " . min(round(0.25 * $maxChildren), $cpuCores * 4) . "\n";
echo "pm.min_spare_servers = " . min(round(0.25 * $maxChildren), $cpuCores * 2) . "\n";
echo "pm.max_spare_servers = " . min(round(0.75 * $maxChildren), $cpuCores * 4) . "\n";