-
Notifications
You must be signed in to change notification settings - Fork 445
Expand file tree
/
Copy pathdebugstate.go
More file actions
96 lines (80 loc) · 2.53 KB
/
debugstate.go
File metadata and controls
96 lines (80 loc) · 2.53 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
package frankenphp
// #include "frankenphp.h"
import "C"
import (
"github.com/dunglas/frankenphp/internal/state"
)
// EXPERIMENTAL: ThreadDebugState prints the state of a single PHP thread - debugging purposes only
type ThreadDebugState struct {
Index int
Name string
State string
IsWaiting bool
IsBusy bool
WaitingSinceMilliseconds int64
CurrentURI string
CurrentMethod string
RequestStartedAt int64
RequestCount int64
MemoryUsage int64
}
// EXPERIMENTAL: FrankenPHPDebugState prints the state of all PHP threads - debugging purposes only
type FrankenPHPDebugState struct {
ThreadDebugStates []ThreadDebugState
ReservedThreadCount int
}
// EXPERIMENTAL: DebugState prints the state of all PHP threads - debugging purposes only
func DebugState() FrankenPHPDebugState {
fullState := FrankenPHPDebugState{
ThreadDebugStates: make([]ThreadDebugState, 0, len(phpThreads)),
ReservedThreadCount: 0,
}
for _, thread := range phpThreads {
if thread.state.Is(state.Reserved) {
fullState.ReservedThreadCount++
continue
}
fullState.ThreadDebugStates = append(fullState.ThreadDebugStates, threadDebugState(thread))
}
return fullState
}
// threadDebugState creates a small jsonable status message for debugging purposes
func threadDebugState(thread *phpThread) ThreadDebugState {
isBusy := !thread.state.IsInWaitingState()
s := ThreadDebugState{
Index: thread.threadIndex,
Name: thread.name(),
State: thread.state.Name(),
IsWaiting: thread.state.IsInWaitingState(),
IsBusy: isBusy,
WaitingSinceMilliseconds: thread.state.WaitTime(),
}
s.RequestCount = thread.requestCount.Load()
s.MemoryUsage = int64(C.frankenphp_get_thread_memory_usage(C.uintptr_t(thread.threadIndex)))
if !isBusy {
return s
}
thread.handlerMu.RLock()
handler := thread.handler
thread.handlerMu.RUnlock()
if handler == nil {
return s
}
thread.contextMu.RLock()
defer thread.contextMu.RUnlock()
fc := handler.frankenPHPContext()
if fc == nil || fc.request == nil || fc.responseWriter == nil {
return s
}
if fc.originalRequest == nil {
s.CurrentURI = fc.requestURI
s.CurrentMethod = fc.request.Method
} else {
s.CurrentURI = fc.originalRequest.URL.RequestURI()
s.CurrentMethod = fc.originalRequest.Method
}
if !fc.startedAt.IsZero() {
s.RequestStartedAt = fc.startedAt.UnixMilli()
}
return s
}