medfall

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit 33167a818ac845f110321eaa2f5bca8f5c04098f
parent e59f45bf4ff1dd0f11a14c51e25ff29a3e6ade0a
Author: Michael Savage <mikejsavage@gmail.com>
Date:   Tue Dec 27 17:44:45 +0200

Make PROFILE_BLOCK behave like printf

Diffstat:
profiler.cc | 14++++++++++----
profiler.h | 21+++++++++++++++------
2 files changed, 25 insertions(+), 10 deletions(-)
diff --git a/profiler.cc b/profiler.cc @@ -6,6 +6,7 @@ #include "profiler.h" #include "stats.h" #include "str.h" +#include "strlcpy.h" struct Timer { const char * name; @@ -78,7 +79,7 @@ static void print_frame( double frame_dt ) { printf( "\n" ); } -void profiler_pop() { +void profiler_pop( const char * name ) { ASSERT( timer_stack_top > 0 ); double now = glfwGetTime(); @@ -90,9 +91,13 @@ void profiler_pop() { Timer * timer = &all_timers[ idx ]; stats_record( &timer->stats, dt ); + if( name == NULL ) { + name = timer->name; + } + ASSERT( num_log_entries < TIMER_LOG_SIZE ); LogEntry * entry = &logs[ num_log_entries ]; - entry->message.sprintf( "%*s%s (%s:%d) - %.3fms", timer_stack_top * 2, "", timer->name, timer->file, timer->line, dt * 1000.0 ); + entry->message.sprintf( "%*s%s (%s:%d) - %.3fms", timer_stack_top * 2, "", name, timer->file, timer->line, dt * 1000.0 ); entry->dt = dt; entry->first_child = last_at_depth[ timer_stack_top + 1 ]; entry->prev_sibling = last_at_depth[ timer_stack_top ]; @@ -119,10 +124,11 @@ void profiler_print_all() { } } -ScopedTimer::ScopedTimer( u32 timer_idx ) { +ScopedTimer::ScopedTimer( u32 timer_idx, const char * n ) { + strlcpy( name, n, sizeof( name ) ); profiler_push( timer_idx ); } ScopedTimer::~ScopedTimer() { - profiler_pop(); + profiler_pop( name ); } diff --git a/profiler.h b/profiler.h @@ -1,18 +1,27 @@ #pragma once #include "intrinsics.h" +#include "platform_io.h" u32 profiler_new_timer( const char * name, const char * file, int line ); void profiler_push( u32 timer_idx ); -void profiler_pop(); +void profiler_pop( const char * name = NULL ); void profiler_print_all(); +#define PROFILE_BLOCK_NAME_LENGTH 128 + struct ScopedTimer { - ScopedTimer( u32 timer_idx ); + ScopedTimer( u32 timer_idx, const char * n ); ~ScopedTimer(); + + char name[ PROFILE_BLOCK_NAME_LENGTH ]; }; -#define PROFILE_BLOCK( name ) \ - static u32 CONCAT( scoped_timer_idx, __LINE__ ) = profiler_new_timer( name, __FILE__, __LINE__ ); \ - ScopedTimer CONCAT( scoped_timer, __LINE__ )( CONCAT( scoped_timer_idx, __LINE__ ) ); -#define PROFILE_FUNCTION() PROFILE_BLOCK( __FUNCTION__ ) +#define PROFILE_BLOCK( fmt, ... ) \ + static u32 CONCAT( scoped_timer_idx, __LINE__ ) = profiler_new_timer( fmt, __FILE__, __LINE__ ); \ + char CONCAT( scoped_timer_name, __LINE__ )[ PROFILE_BLOCK_NAME_LENGTH ]; \ + snprintf( CONCAT( scoped_timer_name, __LINE__ ), PROFILE_BLOCK_NAME_LENGTH, fmt, ##__VA_ARGS__ ); \ + ScopedTimer CONCAT( scoped_timer, __LINE__ )( CONCAT( scoped_timer_idx, __LINE__ ), CONCAT( scoped_timer_name, __LINE__ ) ); +#define PROFILE_FUNCTION() \ + static u32 CONCAT( scoped_timer_idx, __LINE__ ) = profiler_new_timer( __FUNCTION__, __FILE__, __LINE__ ); \ + ScopedTimer CONCAT( scoped_timer, __LINE__ )( CONCAT( scoped_timer_idx, __LINE__ ), __FUNCTION__ );