Guest User

Pafy

a guest
Jul 31st, 2009
353
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.32 KB | None | 0 0
  1. ---cycle_bench.h---
  2. #ifndef CYCLE_BENCH_H
  3. #define CYCLE_BENCH_H
  4.  
  5. #include <asm/msr.h>
  6.  
  7. #define BENCH_LOOP_NUM 1000
  8.  
  9. typedef long long int int64;
  10. typedef struct _cycle_bench_context
  11. {
  12.     // start is declared as volatile to prevent the compilator to optimize code.
  13.     // The calculated overhead execution time is slightly more accurate.
  14.     volatile int64 start;
  15.     int64 cycles[ BENCH_LOOP_NUM ];
  16.     int index;
  17.  
  18.     int64 total_cycles_mean;
  19.     int64 total_cycles_std_dev;
  20.     int64 overhead_cycles_mean;
  21.     int64 overhead_cycles_std_dev;
  22.     int64 section_cycles_mean;
  23.     int64 section_cycles_std_dev;
  24. } cycle_bench_context;
  25.  
  26. void init_cycle_bench( cycle_bench_context *ctx );
  27. void calc_cycle_bench( cycle_bench_context *ctx );
  28.  
  29. static inline int64 getcpucycles()
  30. {
  31.     unsigned long low;
  32.     long high;
  33.     rdtsc( low, high );
  34.     return ( (int64)high << 32) | low;
  35. }
  36.  
  37. #define BEGIN_BENCH_LOOP( ctx ) \
  38.     for( (ctx)->index = 0; (ctx)->index < BENCH_LOOP_NUM; (ctx)->index++ ) \
  39. { \
  40.         (ctx)->start = getcpucycles();
  41. #define END_BENCH_LOOP( ctx ) \
  42.         (ctx)->cycles[ (ctx)->index ] = getcpucycles() - (ctx)->start; \
  43. }
  44.  
  45. #endif
  46.  
  47.  
  48. --- cycle_bench.c ---
  49. #include "cycle_bench.h"
  50. #include <string.h>
  51. #include <math.h>
  52.  
  53. void init_cycle_bench( cycle_bench_context *ctx )
  54. {
  55.     memset( ctx, 0, sizeof( cycle_bench_context ));
  56.     BEGIN_BENCH_LOOP( ctx );
  57.     END_BENCH_LOOP( ctx );
  58.     calc_cycle_bench( ctx );
  59.     ctx->overhead_cycles_mean = ctx->total_cycles_mean;
  60.     ctx->overhead_cycles_std_dev = ctx->total_cycles_std_dev;
  61. }
  62.  
  63. void calc_cycle_bench( cycle_bench_context *ctx )
  64. {
  65.     int i;
  66.  
  67.     int64 tmp=0;
  68.     for( i = 0; i < BENCH_LOOP_NUM; i++ )
  69.         tmp += ctx->cycles[i];
  70.     ctx->total_cycles_mean = tmp / BENCH_LOOP_NUM;
  71.  
  72.     tmp = 0;
  73.     for( i = 0; i < BENCH_LOOP_NUM; i++ )
  74.         tmp += pow( ctx->total_cycles_mean - ctx->cycles[i], 2 );
  75.     ctx->total_cycles_std_dev = sqrt( tmp / BENCH_LOOP_NUM );
  76.  
  77.     ctx->section_cycles_mean = ctx->total_cycles_mean - \
  78.             ctx->overhead_cycles_mean;
  79.     ctx->section_cycles_std_dev = ctx->total_cycles_std_dev + \
  80.             ctx->overhead_cycles_std_dev;
  81. }
  82.  
  83.  
  84. --- how to use it ---
  85.     cycle_bench_context ctx;
  86.     init_cycle_bench( &ctx );
  87.     BEGIN_BENCH_LOOP( &ctx );
  88.         struct timeval tv;
  89.         gettimeofday( &tv, NULL );
  90.     END_BENCH_LOOP( &ctx );
  91.     calc_cycle_bench( &ctx );
  92.     // ctx.section_cycles_mean and ctx.section_cycles_std_dev contains the data
Advertisement
Add Comment
Please, Sign In to add comment