client/runtime/browser_profile.c (1364B)
1 #include "browser_profile.h" 2 #include <emscripten.h> 3 #include <pthread.h> 4 #include <stdatomic.h> 5 #include <stdlib.h> 6 7 static char *configuration; 8 static uint32_t phase; 9 static _Thread_local _Atomic uint32_t *thread_sample; 10 char *ao_profile_start(uint32_t *current_function, uint32_t *phase, uintptr_t thread); 11 void ao_profile_thread_start(uint32_t *sample, uint32_t entry); 12 void ao_profile_thread_stop(uint32_t *sample); 13 14 EMSCRIPTEN_KEEPALIVE const char *browser_profile_configuration(void) 15 { 16 return configuration; 17 } 18 19 void browser_profile_initialize(uint32_t *current_function) 20 { 21 configuration = ao_profile_start(current_function, &phase, (uintptr_t)pthread_self()); 22 } 23 24 void browser_profile_thread_initialize(uint32_t *current_function, uint32_t entry) 25 { 26 if (!configuration) return; 27 thread_sample = calloc(2, sizeof(*thread_sample)); 28 if (!thread_sample) abort(); 29 atomic_store(thread_sample, (uint32_t)current_function); 30 ao_profile_thread_start((uint32_t *)thread_sample, entry); 31 } 32 33 void browser_profile_thread_finish(void) 34 { 35 if (!thread_sample) return; 36 /* Invalidate before TLS is reclaimed. The collector releases this record 37 * after removing it from its sampling list, without delaying thread exit. */ 38 atomic_store(thread_sample, 0); 39 ao_profile_thread_stop((uint32_t *)thread_sample); 40 thread_sample = NULL; 41 }