11package frankenphp
22
3+ // #include "frankenphp.h"
4+ import "C"
35import (
46 "github.com/dunglas/frankenphp/internal/state"
57)
@@ -12,6 +14,11 @@ type ThreadDebugState struct {
1214 IsWaiting bool
1315 IsBusy bool
1416 WaitingSinceMilliseconds int64
17+ CurrentURI string
18+ CurrentMethod string
19+ RequestStartedAt int64
20+ RequestCount int64
21+ MemoryUsage int64
1522}
1623
1724// EXPERIMENTAL: FrankenPHPDebugState prints the state of all PHP threads - debugging purposes only
@@ -39,12 +46,51 @@ func DebugState() FrankenPHPDebugState {
3946
4047// threadDebugState creates a small jsonable status message for debugging purposes
4148func threadDebugState (thread * phpThread ) ThreadDebugState {
42- return ThreadDebugState {
49+ isBusy := ! thread .state .IsInWaitingState ()
50+
51+ s := ThreadDebugState {
4352 Index : thread .threadIndex ,
4453 Name : thread .name (),
4554 State : thread .state .Name (),
4655 IsWaiting : thread .state .IsInWaitingState (),
47- IsBusy : ! thread . state . IsInWaitingState () ,
56+ IsBusy : isBusy ,
4857 WaitingSinceMilliseconds : thread .state .WaitTime (),
4958 }
59+
60+ s .RequestCount = thread .requestCount .Load ()
61+ s .MemoryUsage = int64 (C .frankenphp_get_thread_memory_usage (C .uintptr_t (thread .threadIndex )))
62+
63+ if ! isBusy {
64+ return s
65+ }
66+
67+ thread .handlerMu .RLock ()
68+ handler := thread .handler
69+ thread .handlerMu .RUnlock ()
70+
71+ if handler == nil {
72+ return s
73+ }
74+
75+ thread .contextMu .RLock ()
76+ defer thread .contextMu .RUnlock ()
77+
78+ fc := handler .frankenPHPContext ()
79+ if fc == nil || fc .request == nil || fc .responseWriter == nil {
80+ return s
81+ }
82+
83+ if fc .originalRequest == nil {
84+ s .CurrentURI = fc .requestURI
85+ s .CurrentMethod = fc .request .Method
86+ } else {
87+ s .CurrentURI = fc .originalRequest .URL .RequestURI ()
88+ s .CurrentMethod = fc .originalRequest .Method
89+ }
90+
91+ if ! fc .startedAt .IsZero () {
92+ s .RequestStartedAt = fc .startedAt .UnixMilli ()
93+ }
94+
95+ return s
5096}
0 commit comments