This repository has been archived on 2025-02-04. You can view files and clone it, but cannot push or open issues or pull requests.
helpless/oogabooga/profiling.c
Charlie b37652e1ce v0.00.004 - Custom logging, more concurrency & bugfixing
Concurrency:
	- Refactored spinlock out of OS api (deprecated old procs)
	- Concurrency utilites:
		void spinlock_init(Spinlock *l);
		void spinlock_acquire_or_wait(Spinlock* l);
		bool spinlock_acquire_or_wait_timeout(Spinlock* l, f64 timeout_seconds);
		void spinlock_release(Spinlock* l);
		void mutex_init(Mutex *m);
		void mutex_destroy(Mutex *m);
		void mutex_acquire_or_wait(Mutex *m);
		void mutex_release(Mutex *m);
		void binary_semaphore_init(Binary_Semaphore *sem, bool initial_state);
		void binary_semaphore_destroy(Binary_Semaphore *sem);
		void binary_semaphore_wait(Binary_Semaphore *sem);
		void binary_semaphore_signal(Binary_Semaphore *sem);
		Macro MEMORY_BARRIER
	- Concurrency tests

Docs:
	- custom_logger.c example to show how one can make a custom logger and have logs displayed in-game

Utility:
	- draw_text_and_measure() which is just an overload of draw_text but it also does a measure and returns it

Misc:
	- Added u64 thread_id to global context. This is set in main() for main thread and in thread startup when you dispatch a Thread
	- Fixed a bug where plain rects would be drawn with the incorrect color
	- Fixed a bug where quads from earlier frames would be drawn
2024-07-09 18:38:06 +02:00

48 lines
No EOL
1.9 KiB
C

String_Builder _profile_output = {0};
bool profiler_initted = false;
Spinlock _profiler_lock;
void dump_profile_result() {
File file = os_file_open("google_trace.json", O_CREATE | O_WRITE);
os_file_write_string(file, STR("["));
os_file_write_string(file, _profile_output.result);
os_file_write_string(file, STR("{}]"));
os_file_close(file);
log_verbose("Wrote profiling result to google_trace.json");
}
void _profiler_report_time_cycles(string name, u64 count, u64 start) {
if (!profiler_initted) {
spinlock_init(&_profiler_lock);
profiler_initted = true;
string_builder_init_reserve(&_profile_output, 1024*1000, get_heap_allocator());
}
spinlock_acquire_or_wait(&_profiler_lock);
string fmt = STR("{\"cat\":\"function\",\"dur\":%.3f,\"name\":\"%s\",\"ph\":\"X\",\"pid\":0,\"tid\":%zu,\"ts\":%lld},");
string_builder_print(&_profile_output, fmt, (float64)count*1000, name, GetCurrentThreadId(), start*1000);
spinlock_release(&_profiler_lock);
}
#if ENABLE_PROFILING
#define tm_scope_cycles(name) \
for (u64 start_time = os_get_current_cycle_count(), end_time = start_time, elapsed_time = 0; \
elapsed_time == 0; \
elapsed_time = (end_time = os_get_current_cycle_count()) - start_time, _profiler_report_time_cycles(STR(name), elapsed_time, start_time))
#define tm_scope_cycles_var(name, var) \
for (u64 start_time = os_get_current_cycle_count(), end_time = start_time, elapsed_time = 0; \
elapsed_time == 0; \
elapsed_time = (end_time = os_get_current_cycle_count()) - start_time, var=elapsed_time)
#define tm_scope_cycles_accum(name, var) \
for (u64 start_time = os_get_current_cycle_count(), end_time = start_time, elapsed_time = 0; \
elapsed_time == 0; \
elapsed_time = (end_time = os_get_current_cycle_count()) - start_time, var+=elapsed_time)
#else
#define tm_scope_cycles(...)
#define tm_scope_cycles_var(...)
#define tm_scope_cycles_accum(...)
#endif