medfall

A super great game engine
Log | Files | Refs

commit fdb946a5de01a3bbf38d4779baebf7756ae51e7b
parent 2124510c91023c4b1afdf85cab96d3b5bcc10385
Author: Michael Savage <mikejsavage@gmail.com>
Date:   Sat Jun  3 01:18:41 +0300

Use rdtsc in the profiler

Diffstat:
platform_rdtsc.h | 16++++++++++++++++
profiler.cc | 32++++++++++++++++----------------
stats.cc | 10++++++----
3 files changed, 38 insertions(+), 20 deletions(-)
diff --git a/platform_rdtsc.h b/platform_rdtsc.h @@ -0,0 +1,16 @@ +#pragma once + +#include "platform.h" +#include "platform_inline.h" + +#if COMPILER_MSVC +#include <intrin.h> +#elif COMPILER_GCCORCLANG +#include <x86intrin.h> +#else +#error new compiler +#endif + +forceinline u64 rdtsc() { + return __rdtsc(); +} diff --git a/profiler.cc b/profiler.cc @@ -8,7 +8,7 @@ #include "strlcat.h" #include "ggformat.h" #include "log.h" -#include "platform_time.h" +#include "platform_rdtsc.h" struct Timer { const char * name; @@ -19,12 +19,12 @@ struct Timer { struct StackTimer { u32 timer_idx; - double pushed_at; + u64 pushed_at; }; struct LogEntry { str< 256 > message; - double dt; + u64 clocks; u32 first_child; u32 prev_sibling; }; @@ -59,7 +59,7 @@ void profiler_push( u32 timer_idx ) { ASSERT( timer_stack_top < TIMER_STACK_SIZE ); timer_stack[ timer_stack_top ].timer_idx = timer_idx; - timer_stack[ timer_stack_top ].pushed_at = get_time(); + timer_stack[ timer_stack_top ].pushed_at = rdtsc(); timer_stack_top++; } @@ -72,22 +72,22 @@ static void profiler_logger_init() { log_file = fopen( log_path.c_str(), "w" ); } -static void write_node( u32 idx, double frame_dt ) { +static void write_node( u32 idx, u64 frame_clocks ) { const LogEntry & entry = logs[ idx ]; if( entry.prev_sibling != 0 ) { - write_node( entry.prev_sibling, frame_dt ); + write_node( entry.prev_sibling, frame_clocks ); } - ggprint_to_file( log_file, "{} ({.2}%)\n", entry.message.c_str(), entry.dt * 100.0 / frame_dt ); + ggprint_to_file( log_file, "{} ({.2}%)\n", entry.message.c_str(), entry.clocks * 100.0 / frame_clocks ); if( entry.first_child != 0 ) { - write_node( entry.first_child, frame_dt ); + write_node( entry.first_child, frame_clocks ); } } -static void write_frame( double frame_dt ) { +static void write_frame( u64 frame_clocks ) { profiler_logger_init(); - write_node( num_log_entries - 1, frame_dt ); + write_node( num_log_entries - 1, frame_clocks ); ggprint_to_file( log_file, "\n" ); fflush( log_file ); } @@ -95,14 +95,14 @@ static void write_frame( double frame_dt ) { void profiler_pop( const char * name ) { ASSERT( timer_stack_top > 0 ); - double now = get_time(); + u64 now = rdtsc(); timer_stack_top--; u32 idx = timer_stack[ timer_stack_top ].timer_idx; - double dt = now - timer_stack[ timer_stack_top ].pushed_at; + u64 clocks = now - timer_stack[ timer_stack_top ].pushed_at; Timer * timer = &all_timers[ idx ]; - stats_record( &timer->stats, dt ); + stats_record( &timer->stats, clocks ); if( name == NULL ) { name = timer->name; @@ -115,8 +115,8 @@ void profiler_pop( const char * name ) { ASSERT( num_log_entries < TIMER_LOG_SIZE ); LogEntry * entry = &logs[ num_log_entries ]; - entry->message.sprintf( "{}{} ({}:{}) - {.3}ms", spaces, name, timer->file, timer->line, dt * 1000.0 ); - entry->dt = dt; + entry->message.sprintf( "{}{} ({}:{}) - {.3} Kclocks", spaces, name, timer->file, timer->line, clocks / 1024 ); + entry->clocks = clocks; entry->first_child = last_at_depth[ timer_stack_top + 1 ]; entry->prev_sibling = last_at_depth[ timer_stack_top ]; @@ -126,7 +126,7 @@ void profiler_pop( const char * name ) { num_log_entries++; if( timer_stack_top == 0 ) { - write_frame( dt ); + write_frame( clocks ); num_log_entries = 1; memset( last_at_depth, 0, sizeof( last_at_depth ) ); } diff --git a/stats.cc b/stats.cc @@ -53,10 +53,12 @@ void format( FormatBuffer * fb, const Stats & stats, const FormatOpts & opts ) { u32 num_samples = checked_cast< u32 >( min( stats.num_records, u64( STATS_NUM_QUART_SAMPLES ) ) ); std::sort( stats.samples, stats.samples + num_samples ); - ggformat_impl( fb, "n = {}, mean = {}, stddev = {}, range = {} to {}, 25% = {}, 75% = {}", - stats.num_records, + ggformat_impl( fb, "total = {}, n = {}, mean = {}, stddev = {}, min = {}, 25% = {}, 75% = {}, max = {}", + stats.sum, stats.num_records, stats_mean( &stats ), stats_stddev( &stats ), - stats.min, stats.max, - stats.samples[ num_samples / 4 ], stats.samples[ num_samples * 3 / 4 ] + stats.min, + stats.samples[ num_samples / 4 ], + stats.samples[ num_samples * 3 / 4 ], + stats.max ); }