-
Notifications
You must be signed in to change notification settings - Fork 462
Expand file tree
/
Copy pathphpmainthread.go
More file actions
298 lines (249 loc) · 8.3 KB
/
Copy pathphpmainthread.go
File metadata and controls
298 lines (249 loc) · 8.3 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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
package frankenphp
// #cgo nocallback frankenphp_new_main_thread
// #cgo noescape frankenphp_new_main_thread
// #include "frankenphp.h"
// #include <php_variables.h>
import "C"
import (
"log/slog"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/dunglas/frankenphp/internal/memory"
"github.com/dunglas/frankenphp/internal/phpheaders"
"github.com/dunglas/frankenphp/internal/state"
)
// represents the main PHP thread
// the thread needs to keep running as long as all other threads are running
type phpMainThread struct {
state *state.ThreadState
done chan struct{}
numThreads int
maxThreads int
phpIni map[string]string
isRebooting atomic.Bool
}
var (
phpThreads []*phpThread
mainThread *phpMainThread
commonHeaders map[string]*C.zend_string
// timeouts to wait for threads to yield before arming force-kill
shutDownGracePeriod = 30 * time.Second
rebootGracePeriod = 6 * time.Second
)
// initPHPThreads starts the main PHP thread,
// a fixed number of inactive PHP threads
// and reserves a fixed number of possible PHP threads
func initPHPThreads(numThreads int, numMaxThreads int, phpIni map[string]string) (*phpMainThread, error) {
mainThread = &phpMainThread{
state: state.NewThreadState(),
done: make(chan struct{}),
numThreads: numThreads,
maxThreads: numMaxThreads,
phpIni: phpIni,
}
// initialize the first thread
// this needs to happen before starting the main thread
// since some extensions access environment variables on startup
// the threadIndex on the main thread defaults to 0 -> phpThreads[0].Pin(...)
initialThread := newPHPThread(0)
phpThreads = []*phpThread{initialThread}
if err := mainThread.start(); err != nil {
return nil, err
}
// Must follow start(): maxThreads is only final once
// setAutomaticMaxThreads runs on the main PHP thread (before Ready).
C.frankenphp_init_thread_metrics(C.int(mainThread.maxThreads))
// initialize all other threads
phpThreads = make([]*phpThread, mainThread.maxThreads)
phpThreads[0] = initialThread
for i := 1; i < mainThread.maxThreads; i++ {
phpThreads[i] = newPHPThread(i)
}
// start the underlying C threads
var ready sync.WaitGroup
for i := 0; i < numThreads; i++ {
ready.Go(phpThreads[i].boot)
}
ready.Wait()
return mainThread, nil
}
func drainPHPThreads() {
// disallow any scaling or restarting threads while draining
scalingMu.Lock()
defer scalingMu.Unlock()
if mainThread == nil || !mainThread.state.Is(state.Ready) {
return // mainThread was never initialized
}
doneWG := sync.WaitGroup{}
doneWG.Add(len(phpThreads))
mainThread.state.Set(state.ShuttingDown)
close(mainThread.done)
for _, thread := range phpThreads {
go func(thread *phpThread) {
thread.shutdown()
doneWG.Done()
}(thread)
}
doneWG.Wait()
mainThread.state.Set(state.Done)
mainThread.state.WaitFor(state.Reserved)
C.frankenphp_destroy_thread_metrics()
phpThreads = nil
}
func (mainThread *phpMainThread) start() error {
if C.frankenphp_new_main_thread(C.int(mainThread.numThreads)) != 0 {
return ErrMainThreadCreation
}
mainThread.state.WaitFor(state.Ready)
// cache common request headers as zend_strings (HTTP_ACCEPT, HTTP_USER_AGENT, etc.)
if commonHeaders == nil {
commonHeaders = make(map[string]*C.zend_string, len(phpheaders.CommonRequestHeaders))
for key, phpKey := range phpheaders.CommonRequestHeaders {
commonHeaders[key] = newPersistentZendString(phpKey)
}
}
return nil
}
// rebootAllThreads reboots all underlying C threads, but keeps the go side alive
func (mainThread *phpMainThread) rebootAllThreads() bool {
if !mainThread.isRebooting.CompareAndSwap(false, true) {
// if already rebooting, ignore the call
return false
}
defer mainThread.isRebooting.Store(false)
// allow no scaling or shutdown while rebooting
scalingMu.Lock()
defer scalingMu.Unlock()
if !mainThread.state.Is(state.Ready) {
// mainThread has shut down in the meantime, no need to reboot
return false
}
rebootStart := time.Now()
rebootWg := sync.WaitGroup{}
rebootingThreads := []*phpThread{}
globalLogger.LogAttrs(globalCtx, slog.LevelInfo, "rebooting all PHP threads")
for _, thread := range phpThreads {
if thread.forceReboot() {
rebootingThreads = append(rebootingThreads, thread)
}
}
for _, thread := range rebootingThreads {
rebootWg.Go(func() {
close(thread.drainChan)
if thread.state.WaitForStateWithTimeout(rebootGracePeriod, state.YieldingForReboot) {
return
}
// thread has not shut down in time, force kill the thread on the PHP side
globalLogger.LogAttrs(
globalCtx,
slog.LevelWarn,
"force-killing thread on reboot timeout",
slog.String("name", thread.name()),
slog.String("state", thread.state.Name()),
slog.String("timeout", rebootGracePeriod.String()),
)
thread.sendKillSignal()
thread.state.WaitFor(state.YieldingForReboot)
})
}
rebootWg.Wait()
mainThread.state.Set(state.Rebooting)
mainThread.state.WaitFor(state.YieldingForReboot)
if C.frankenphp_new_main_thread(C.int(mainThread.numThreads)) != 0 {
panic("unable to recreate main thread after reboot")
}
mainThread.state.WaitFor(state.Ready)
for _, thread := range rebootingThreads {
thread.drainChan = make(chan struct{})
if thread.state.CompareAndSwap(state.YieldingForReboot, state.RebootReady) {
// wait for any of the stable states
thread.state.WaitFor(state.Ready, state.Inactive, state.Reserved)
} else {
panic("unexpected state on reboot: " + thread.state.Name() + " for thread " + thread.name())
}
}
globalLogger.LogAttrs(
globalCtx,
slog.LevelInfo,
"thread reboot finished",
slog.String("duration", time.Since(rebootStart).String()),
slog.Int("num_threads", len(rebootingThreads)),
)
return true
}
func getInactivePHPThread() *phpThread {
for _, thread := range phpThreads {
if thread.state.Is(state.Inactive) {
return thread
}
}
for _, thread := range phpThreads {
if thread.state.CompareAndSwap(state.Reserved, state.BootRequested) {
thread.boot()
return thread
}
}
return nil
}
//export go_frankenphp_main_thread_is_ready
func go_frankenphp_main_thread_is_ready() {
mainThread.setAutomaticMaxThreads()
if mainThread.maxThreads < mainThread.numThreads {
mainThread.maxThreads = mainThread.numThreads
}
mainThread.state.Set(state.Ready)
mainThread.state.WaitFor(state.Done, state.Rebooting)
}
// max_threads = auto
// setAutomaticMaxThreads estimates the amount of threads based on php.ini and system memory_limit
// If unable to get the system's memory limit, simply double num_threads
func (mainThread *phpMainThread) setAutomaticMaxThreads() {
if mainThread.maxThreads >= 0 {
return
}
perThreadMemoryLimit := int64(C.frankenphp_get_current_memory_limit())
totalSysMemory := memory.TotalSysMemory()
if perThreadMemoryLimit <= 0 || totalSysMemory == 0 {
mainThread.maxThreads = mainThread.numThreads * 2
return
}
maxAllowedThreads := totalSysMemory / uint64(perThreadMemoryLimit)
mainThread.maxThreads = int(maxAllowedThreads)
if globalLogger.Enabled(globalCtx, slog.LevelDebug) {
globalLogger.LogAttrs(globalCtx, slog.LevelDebug, "Automatic thread limit", slog.Int("perThreadMemoryLimitMB", int(perThreadMemoryLimit/1024/1024)), slog.Int("maxThreads", mainThread.maxThreads))
}
}
//export go_frankenphp_shutdown_main_thread
func go_frankenphp_shutdown_main_thread() {
if mainThread.state.CompareAndSwap(state.Rebooting, state.YieldingForReboot) {
return
}
mainThread.state.Set(state.Reserved)
}
//export go_get_custom_php_ini
func go_get_custom_php_ini(disableTimeouts C.bool) *C.char {
if mainThread.phpIni == nil {
mainThread.phpIni = make(map[string]string)
}
// Timeouts are currently fundamentally broken
// with ZTS except on Linux and FreeBSD: https://bugs.php.net/bug.php?id=79464
// Disable timeouts if ZEND_MAX_EXECUTION_TIMERS is not supported
if disableTimeouts {
mainThread.phpIni["max_execution_time"] = "0"
mainThread.phpIni["max_input_time"] = "-1"
}
// Pass the php.ini overrides to PHP before startup
// TODO: if needed this would also be possible on a per-thread basis
var overrides strings.Builder
// 32 is an over-estimate for php.ini settings
overrides.Grow(len(mainThread.phpIni) * 32)
for k, v := range mainThread.phpIni {
overrides.WriteString(k)
overrides.WriteByte('=')
overrides.WriteString(v)
overrides.WriteByte('\n')
}
return C.CString(overrides.String())
}