@@ -244,11 +244,12 @@ static void frankenphp_reset_session_state(void) {
244244}
245245#endif
246246
247- static __thread size_t thread_last_memory_usage = 0 ;
247+ static frankenphp_thread_metrics * thread_metrics = NULL ;
248248
249249/* Adapted from php_request_shutdown */
250250static void frankenphp_worker_request_shutdown () {
251- thread_last_memory_usage = zend_memory_usage (0 );
251+ __atomic_store_n (& thread_metrics [thread_index ].last_memory_usage ,
252+ zend_memory_usage (0 ), __ATOMIC_RELAXED );
252253
253254 /* Flush all output buffers */
254255 zend_try { php_output_end_all (); }
@@ -1236,7 +1237,8 @@ int frankenphp_execute_script(char *file_name) {
12361237 sandboxed_env = NULL ;
12371238 }
12381239
1239- thread_last_memory_usage = zend_memory_usage (0 );
1240+ __atomic_store_n (& thread_metrics [thread_index ].last_memory_usage ,
1241+ zend_memory_usage (0 ), __ATOMIC_RELAXED );
12401242 php_request_shutdown ((void * )0 );
12411243 frankenphp_free_request_context ();
12421244
@@ -1409,7 +1411,18 @@ int frankenphp_reset_opcache(void) {
14091411
14101412int frankenphp_get_current_memory_limit () { return PG (memory_limit ); }
14111413
1412- size_t frankenphp_get_current_memory_usage () { return thread_last_memory_usage ; }
1414+ void frankenphp_init_thread_metrics (int max_threads ) {
1415+ thread_metrics = calloc (max_threads , sizeof (frankenphp_thread_metrics ));
1416+ }
1417+
1418+ void frankenphp_destroy_thread_metrics (void ) {
1419+ free (thread_metrics );
1420+ thread_metrics = NULL ;
1421+ }
1422+
1423+ size_t frankenphp_get_thread_memory_usage (uintptr_t idx ) {
1424+ return __atomic_load_n (& thread_metrics [idx ].last_memory_usage , __ATOMIC_RELAXED );
1425+ }
14131426
14141427static zend_module_entry * * modules = NULL ;
14151428static int modules_len = 0 ;
0 commit comments