phpmainthread.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. package frankenphp
  2. // #include "frankenphp.h"
  3. import "C"
  4. import (
  5. "fmt"
  6. "sync"
  7. "github.com/dunglas/frankenphp/internal/memory"
  8. "github.com/dunglas/frankenphp/internal/phpheaders"
  9. "go.uber.org/zap"
  10. )
  11. // represents the main PHP thread
  12. // the thread needs to keep running as long as all other threads are running
  13. type phpMainThread struct {
  14. state *threadState
  15. done chan struct{}
  16. numThreads int
  17. maxThreads int
  18. phpIni map[string]string
  19. commonHeaders map[string]*C.zend_string
  20. knownServerKeys map[string]*C.zend_string
  21. }
  22. var (
  23. phpThreads []*phpThread
  24. mainThread *phpMainThread
  25. )
  26. // start the main PHP thread
  27. // start a fixed number of inactive PHP threads
  28. // reserve a fixed number of possible PHP threads
  29. func initPHPThreads(numThreads int, numMaxThreads int, phpIni map[string]string) (*phpMainThread, error) {
  30. mainThread = &phpMainThread{
  31. state: newThreadState(),
  32. done: make(chan struct{}),
  33. numThreads: numThreads,
  34. maxThreads: numMaxThreads,
  35. phpIni: phpIni,
  36. }
  37. // initialize the first thread
  38. // this needs to happen before starting the main thread
  39. // since some extensions access environment variables on startup
  40. // the threadIndex on the main thread defaults to 0 -> phpThreads[0].Pin(...)
  41. initialThread := newPHPThread(0)
  42. phpThreads = []*phpThread{initialThread}
  43. if err := mainThread.start(); err != nil {
  44. return nil, err
  45. }
  46. // initialize all other threads
  47. phpThreads = make([]*phpThread, mainThread.maxThreads)
  48. phpThreads[0] = initialThread
  49. for i := 1; i < mainThread.maxThreads; i++ {
  50. phpThreads[i] = newPHPThread(i)
  51. }
  52. // start the underlying C threads
  53. ready := sync.WaitGroup{}
  54. ready.Add(numThreads)
  55. for i := 0; i < numThreads; i++ {
  56. thread := phpThreads[i]
  57. go func() {
  58. thread.boot()
  59. ready.Done()
  60. }()
  61. }
  62. ready.Wait()
  63. return mainThread, nil
  64. }
  65. // ThreadDebugStatus prints the state of all PHP threads - debugging purposes only
  66. func ThreadDebugStatus() string {
  67. statusMessage := ""
  68. reservedThreadCount := 0
  69. for _, thread := range phpThreads {
  70. if thread.state.is(stateReserved) {
  71. reservedThreadCount++
  72. continue
  73. }
  74. statusMessage += thread.debugStatus() + "\n"
  75. }
  76. statusMessage += fmt.Sprintf("%d additional threads can be started at runtime\n", reservedThreadCount)
  77. return statusMessage
  78. }
  79. func drainPHPThreads() {
  80. doneWG := sync.WaitGroup{}
  81. doneWG.Add(len(phpThreads))
  82. mainThread.state.set(stateShuttingDown)
  83. close(mainThread.done)
  84. for _, thread := range phpThreads {
  85. // shut down all reserved threads
  86. if thread.state.compareAndSwap(stateReserved, stateDone) {
  87. doneWG.Done()
  88. continue
  89. }
  90. // shut down all active threads
  91. go func(thread *phpThread) {
  92. thread.shutdown()
  93. doneWG.Done()
  94. }(thread)
  95. }
  96. doneWG.Wait()
  97. mainThread.state.set(stateDone)
  98. mainThread.state.waitFor(stateReserved)
  99. phpThreads = nil
  100. }
  101. func (mainThread *phpMainThread) start() error {
  102. if C.frankenphp_new_main_thread(C.int(mainThread.numThreads)) != 0 {
  103. return MainThreadCreationError
  104. }
  105. mainThread.state.waitFor(stateReady)
  106. // cache common request headers as zend_strings (HTTP_ACCEPT, HTTP_USER_AGENT, etc.)
  107. mainThread.commonHeaders = make(map[string]*C.zend_string, len(phpheaders.CommonRequestHeaders))
  108. for key, phpKey := range phpheaders.CommonRequestHeaders {
  109. mainThread.commonHeaders[key] = C.frankenphp_init_persistent_string(C.CString(phpKey), C.size_t(len(phpKey)))
  110. }
  111. // cache $_SERVER keys as zend_strings (SERVER_PROTOCOL, SERVER_SOFTWARE, etc.)
  112. mainThread.knownServerKeys = make(map[string]*C.zend_string, len(knownServerKeys))
  113. for _, phpKey := range knownServerKeys {
  114. mainThread.knownServerKeys[phpKey] = C.frankenphp_init_persistent_string(toUnsafeChar(phpKey), C.size_t(len(phpKey)))
  115. }
  116. return nil
  117. }
  118. func getInactivePHPThread() *phpThread {
  119. thread := getPHPThreadAtState(stateInactive)
  120. if thread != nil {
  121. return thread
  122. }
  123. thread = getPHPThreadAtState(stateReserved)
  124. if thread == nil {
  125. return nil
  126. }
  127. thread.boot()
  128. return thread
  129. }
  130. func getPHPThreadAtState(state stateID) *phpThread {
  131. for _, thread := range phpThreads {
  132. if thread.state.is(state) {
  133. return thread
  134. }
  135. }
  136. return nil
  137. }
  138. //export go_frankenphp_main_thread_is_ready
  139. func go_frankenphp_main_thread_is_ready() {
  140. mainThread.setAutomaticMaxThreads()
  141. if mainThread.maxThreads < mainThread.numThreads {
  142. mainThread.maxThreads = mainThread.numThreads
  143. }
  144. mainThread.state.set(stateReady)
  145. mainThread.state.waitFor(stateDone)
  146. }
  147. // max_threads = auto
  148. // Estimate the amount of threads based on php.ini and system memory_limit
  149. // If unable to get the system's memory limit, simply double num_threads
  150. func (mainThread *phpMainThread) setAutomaticMaxThreads() {
  151. if mainThread.maxThreads >= 0 {
  152. return
  153. }
  154. perThreadMemoryLimit := int64(C.frankenphp_get_current_memory_limit())
  155. totalSysMemory := memory.TotalSysMemory()
  156. if perThreadMemoryLimit <= 0 || totalSysMemory == 0 {
  157. mainThread.maxThreads = mainThread.numThreads * 2
  158. return
  159. }
  160. maxAllowedThreads := totalSysMemory / uint64(perThreadMemoryLimit)
  161. mainThread.maxThreads = int(maxAllowedThreads)
  162. logger.Debug("Automatic thread limit", zap.Int("perThreadMemoryLimitMB", int(perThreadMemoryLimit/1024/1024)), zap.Int("maxThreads", mainThread.maxThreads))
  163. }
  164. //export go_frankenphp_shutdown_main_thread
  165. func go_frankenphp_shutdown_main_thread() {
  166. mainThread.state.set(stateReserved)
  167. }
  168. //export go_get_custom_php_ini
  169. func go_get_custom_php_ini() *C.char {
  170. if mainThread.phpIni == nil {
  171. return nil
  172. }
  173. // pass the php.ini overrides to PHP before startup
  174. // TODO: if needed this would also be possible on a per-thread basis
  175. overrides := ""
  176. for k, v := range mainThread.phpIni {
  177. overrides += fmt.Sprintf("%s=%s\n", k, v)
  178. }
  179. return C.CString(overrides)
  180. }