medfall

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

commit ee347c1eba9eda1686b89316ea13a58c758b454f
parent 046e695924a7f12912966391906a0115ac166f91
Author: Michael Savage <mikejsavage@gmail.com>
Date:   Sun Jan 17 18:00:48 +0000

Add benchmarking code

Diffstat:
benchmark.cc | 58++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
benchmark.h | 23+++++++++++++++++++++++
btt.cc | 4++++
game.h | 2++
4 files changed, 87 insertions(+), 0 deletions(-)
diff --git a/benchmark.cc b/benchmark.cc @@ -0,0 +1,58 @@ +// TODO: this header doesn't exist on windows +#include <x86intrin.h> + +#include "intrinsics.h" +#include "benchmark.h" +#include "platform_atomic.h" + +#define MAX_TIMERS 4096 + +struct Timer { + const char * fn; + const char * file; + int line; + volatile u64 total_clocks; + volatile u64 num_calls; +}; + +static Timer timers[ 4096 ]; +static volatile u32 num_timers = 0; + +ScopedTimer::ScopedTimer( u32 idx ) { + initial_clock = __rdtsc(); + timer_idx = idx; +} + +ScopedTimer::~ScopedTimer() { + u64 dt = __rdtsc() - initial_clock; + atomic_add_u64( &timers[ timer_idx ].total_clocks, dt ); + atomic_add_u64( &timers[ timer_idx ].num_calls, 1 ); +} + +u32 benchmark_new_timer( const char * fn, const char * file, int line ) { + u32 idx = atomic_add_u32( &num_timers, 1 ) - 1; + assert( idx < MAX_TIMERS ); + + Timer info; + info.fn = fn; + info.file = file; + info.line = line; + info.total_clocks = 0; + info.num_calls = 0; + + timers[ idx ] = info; + + return idx; +} + +void benchmark_print_timers() { + for( u64 i = 0; i < num_timers; i++ ) { + u64 clocks = timers[ i ].total_clocks; + u64 calls = timers[ i ].num_calls; + + printf( "%s (%s:%d) called %llu times, %llu clocks, %llu avg\n", + timers[ i ].fn, timers[ i ].file, timers[ i ].line, + calls, clocks, clocks / calls + ); + } +} diff --git a/benchmark.h b/benchmark.h @@ -0,0 +1,23 @@ +#ifndef _BENCHMARK_H_ +#define _BENCHMARK_H_ + +#include "intrinsics.h" + +struct ScopedTimer { + u64 initial_clock; + u32 timer_idx; + + ScopedTimer( u32 idx ); + ~ScopedTimer(); +}; + +u32 benchmark_new_timer( const char * fn, const char * file, int line ); +void benchmark_print_timers(); + +// TODO: i think this works but double check to make sure it really does. +#define TIMED_BLOCK( fn, file, line ) \ + static u32 benchmark_timer_idx = benchmark_new_timer( fn, file, line ); \ + ScopedTimer benchmark_timer( benchmark_timer_idx ); +#define TIMED_FUNCTION() TIMED_BLOCK( __FUNCTION__, __FILE__, __LINE__ ) + +#endif // _BENCHMARK_H_ diff --git a/btt.cc b/btt.cc @@ -176,6 +176,8 @@ static void btt_build( BTT * const node, const glm::ivec2 v0, const glm::ivec2 v1, const glm::ivec2 v2 ) { + TIMED_FUNCTION(); + const glm::ivec2 mid = ( v0 + v2 ) / 2; if( !node->left ) { @@ -329,4 +331,6 @@ extern "C" GAME_FRAME( game_frame ) { glUniformMatrix4fv( state->test_outline_un_vp, 1, GL_FALSE, glm::value_ptr( VP ) ); immediate_render( &imm, state->test_outline_at_position, state->test_outline_at_colour ); glUseProgram( 0 ); + + benchmark_print_timers(); } diff --git a/game.h b/game.h @@ -73,7 +73,9 @@ #include <glm/glm.hpp> +// TODO: this whole file blows #include "intrinsics.h" +#include "benchmark.h" #include "assets.h" #include "terrain_manager.h" #include "btt.h"