This repository has been archived by the owner on Jan 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathPHP5Daemon.php
173 lines (158 loc) · 4.62 KB
/
PHP5Daemon.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
<?hh
/*
* Copyright (c) 2014-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/
final class PHP5Daemon extends PHPEngine {
private PerfTarget $target;
public function __construct(private PerfOptions $options) {
$this->target = $options->getTarget();
parent::__construct((string) $options->php5);
if ($options->fpm) {
$output = [];
$check_command = implode(
' ',
(Vector {
$options->php5,
'-i',
'-c',
OSS_PERFORMANCE_ROOT.'/conf',
})->map($x ==> escapeshellarg($x)),
);
// Basic check for opcode caching.
if ($options->traceSubProcess) {
fprintf(STDERR, "%s\n", $check_command);
}
exec($check_command, &$output);
$check = array_search('Opcode Caching => Up and Running', $output, true);
invariant($check, 'Got invalid output from php-fpm -i');
} else {
$output = [];
$check_command = implode(
' ',
(Vector {
$options->php5,
'-q',
'-c',
OSS_PERFORMANCE_ROOT.'/conf',
__dir__.'/php-src_config_check.php',
})->map($x ==> escapeshellarg($x)),
);
if ($options->traceSubProcess) {
fprintf(STDERR, "%s\n", $check_command);
}
exec($check_command, &$output);
$checks = json_decode(implode("\n", $output), /* as array = */ true);
invariant($checks, 'Got invalid output from php-src_config_check.php');
BuildChecker::Check(
$options,
(string) $options->php5,
$checks,
Set {'PHP_VERSION', 'PHP_VERSION_ID'},
);
}
}
public function start(): void {
parent::startWorker(
$this->options->daemonOutputFileName('php5'),
$this->options->delayProcessLaunch,
$this->options->traceSubProcess,
);
}
protected function getArguments(): Vector<string> {
if ($this->options->cpuBind) {
$this->cpuRange = $this->options->daemonProcessors;
}
if ($this->options->fpm) {
echo 'Creating PHP FPM config';
$path = $this->options->tempDir.'/php-fpm.conf';
$config = file_get_contents(OSS_PERFORMANCE_ROOT.'/conf/php-fpm.conf.in');
$config = str_replace(
"__FASTCGI_PORT__",
PerfSettings::BackendPort(),
$config
);
$config = str_replace(
"__CHILDREN__",
$this->options->serverThreads,
$config
);
$config = str_replace(
"__TMP_DIR__",
$this->options->tempDir,
$config
);
file_put_contents($path, $config);
$args = Vector {
'-F',
'--fpm-config',
$path,
'-c',
OSS_PERFORMANCE_ROOT.'/conf/',
};
} else {
$args = Vector {
'-b',
'localhost:'.PerfSettings::BackendPort(),
'-c',
OSS_PERFORMANCE_ROOT.'/conf/',
};
}
if (count($this->options->phpExtraArguments) > 0) {
$args->addAll($this->options->phpExtraArguments);
}
return $args;
}
public function getPid(): ?int {
$pid = parent::getPid();
if ($pid === null) {
return null;
}
// proc_open() uses the shell to spawn PHP.
// Bash just does an exec() in simple cases, so we already have the right
// PID.
if ($this->isPHPCGIProcess($pid)) {
return $pid;
}
// Dash always does a fork() + exec(), so we actually want the PID of one
// of the children.
foreach (glob('/proc/*') as $candidate) {
if (!preg_match(',^/proc/[0-9]+(/|$),', $candidate)) {
continue;
}
if (!file_exists($candidate.'/stat')) {
continue;
}
$stat = explode(' ', file_get_contents($candidate.'/stat'));
$cpid = (int) $stat[0];
$ppid = (int) $stat[3];
if ($ppid === $pid && $this->isPHPCGIProcess($cpid)) {
return $cpid;
}
}
return null;
}
private function isPHPCGIProcess(int $pid): bool {
// Allow 'php-cgi', 'php5-cgi', 'php7-cgi-20141209', etc
$exe = '/proc/'.$pid.'/exe';
if (!file_exists($exe)) {
return false;
}
return (bool) preg_match('/php.*-cgi/', readlink($exe)) ||
(bool) preg_match('/php.*-fpm/', readlink($exe));
}
protected function getEnvironmentVariables(): Map<string, string> {
return Map {
'OSS_PERF_TARGET' => (string) $this->target,
'PHP_FCGI_CHILDREN' => (string) $this->options->phpFCGIChildren,
'PHP_FCGI_MAX_REQUESTS' => '0',
};
}
public function __toString(): string {
return (string) $this->options->php5;
}
}