-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathworkers.php
86 lines (73 loc) · 2.04 KB
/
workers.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
<?php
$units = array(
'k' => 1000,
'M' => 1000000,
'G' => 1000000000,
);
$memoryLimit = getenv('MEMORYLIMIT');
if ($memoryLimit == '') {
$memoryLimit = '128M';
}
$ml = substr($memoryLimit, 0, -1) * $units[substr($memoryLimit, -1)];
$workers = getenv('WORKERS');
if ($workers == '' ) {
// default mode (see https://gitlab.bearstech.com/bearstech/dockerfiles/-/issues/180#note_32085)
$workers = 'auto';
}
$max_spare_servers = getenv('MAX_SPARE_SERVERS');
if($max_spare_servers == '') {
$max_spare_servers = 3;
}
$start_servers = getenv('START_SERVERS');
if($start_servers == '') {
$start_servers = 2;
}
if(strtolower($workers) == 'auto') {
//which version for cgroup
$cgroupv2 = file_exists("/sys/fs/cgroup/cgroup.controllers");
$cg_mem_file_path = $cgroupv2 ? "/sys/fs/cgroup/memory.max" :
"/sys/fs/cgroup/memory/memory.limit_in_bytes";
$cg_proc_file_path = $cgroupv2 ? "/sys/fs/cgroup/cpu.max" :
"/sys/fs/cgroup/cpu/cpu.cfs_quota_us";
$procs = 0;
$f = trim(file_get_contents($cg_proc_file_path));
if($cgroupv2 && strstr($f, "max") === false) {
$t = explode($f, " ");
$procs = $t[0] / 100000; //need to be tested
}
else if (!$cgroupv2 && $f != "-1") {
$procs = $f / 100000;
} else {
$f = file_get_contents("/proc/cpuinfo");
foreach (explode("\n", $f) as $line) {
if (strstr($line, 'processor')) {
$procs++;
}
}
}
$memory = trim(file_get_contents($cg_mem_file_path));
if($cgroupv2 && $memory == "max") { // for cgroupv2
$f = file_get_contents("/proc/meminfo");
foreach (explode("\n", $f) as $line) {
if (strstr($line, 'MemTotal:')) {
preg_match('/\w+:\s+(\d+) (\w)B/', $line, $matches);
$memory = $matches[1] * $units[$matches[2]];
}
}
}
$workers = floor($memory / $ml);
// 2 is max worker per cpu
if ($workers > ($procs * 2)) {
$workers = $procs * 2;
}
if ($workers < 1) {
$workers = 1;
}
//workers should be greater than max_spare_servers
if($workers < $max_spare_servers) {
$max_spare_servers = 1;
$start_servers = 1;
}
}
print $workers." ".$max_spare_servers." ".$start_servers;
?>