@@ -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 (); }
@@ -1237,7 +1238,8 @@ int frankenphp_execute_script(char *file_name) {
12371238 sandboxed_env = NULL ;
12381239 }
12391240
1240- thread_last_memory_usage = zend_memory_usage (0 );
1241+ __atomic_store_n (& thread_metrics [thread_index ].last_memory_usage ,
1242+ zend_memory_usage (0 ), __ATOMIC_RELAXED );
12411243 php_request_shutdown ((void * )0 );
12421244 frankenphp_free_request_context ();
12431245
@@ -1410,7 +1412,19 @@ int frankenphp_reset_opcache(void) {
14101412
14111413int frankenphp_get_current_memory_limit () { return PG (memory_limit ); }
14121414
1413- size_t frankenphp_get_current_memory_usage () { return thread_last_memory_usage ; }
1415+ void frankenphp_init_thread_metrics (int max_threads ) {
1416+ thread_metrics = calloc (max_threads , sizeof (frankenphp_thread_metrics ));
1417+ }
1418+
1419+ void frankenphp_destroy_thread_metrics (void ) {
1420+ free (thread_metrics );
1421+ thread_metrics = NULL ;
1422+ }
1423+
1424+ size_t frankenphp_get_thread_memory_usage (uintptr_t idx ) {
1425+ return __atomic_load_n (& thread_metrics [idx ].last_memory_usage ,
1426+ __ATOMIC_RELAXED );
1427+ }
14141428
14151429static zend_module_entry * * modules = NULL ;
14161430static int modules_len = 0 ;
0 commit comments