spkgdeveloper's documentation

bench.h

00001 /*----------------------------------------------------------------------*\
00002 |* spkg - The Unofficial Slackware Linux Package Manager                *|
00003 |*                                      designed by Ondøej Jirman, 2005 *|
00004 |*----------------------------------------------------------------------*|
00005 |*          No copy/usage restrictions are imposed on anybody.          *|
00006 \*----------------------------------------------------------------------*/
00007 #if __BENCH == 1
00008 
00009 #include <stdio.h>
00010 #include <asm/msr.h>
00011 
00012 /* tics per second (this is constant for my 1GHz athlon) */
00013 #define TPS 1000000000ull
00014 #define CNT 16
00015 
00016 /* [0] = start tics, [1] = accumulated tics, [2] = times stopped/continued */
00017 static unsigned long long timers[CNT][3];
00018 
00019 static __inline__ void reset_timer(int t)
00020 {
00021   timers[t][1] = 0;
00022   timers[t][2] = 0;
00023 }
00024 
00025 static __inline__ void reset_timers()
00026 {
00027   int i;
00028   for (i=0;i<CNT;i++)
00029     reset_timer(i);
00030 }
00031 
00032 static __inline__ void start_timer(int t)
00033 {
00034   reset_timer(t);
00035   rdtscll(timers[t][0]);
00036 }
00037 
00038 static __inline__ void continue_timer(int t)
00039 {
00040   rdtscll(timers[t][0]);
00041 }
00042 
00043 static __inline__ void stop_timer(int t)
00044 {
00045   unsigned long long stop;
00046   rdtscll(stop);
00047   timers[t][2] += 1;
00048   timers[t][1] += stop - timers[t][0];
00049 }
00050 
00051 static __inline__ double get_timer(int t)
00052 {
00053   return (double)(timers[t][1])/TPS;
00054 }
00055 
00056 static __inline__ void get_time_str(double d, char* buf)
00057 {
00058   if (d < 1e-6)
00059     sprintf(buf, "%1.1lf ns", d*1e9);
00060   else if (d < 1e-3)
00061     sprintf(buf, "%1.4lf us", d*1e6);
00062   else if (d < 1)
00063     sprintf(buf, "%1.7lf ms", d*1e3);
00064   else
00065     sprintf(buf, "%1.10lf s", d);
00066 }
00067 
00068 
00069 static __inline__ void print_timer(int t, char* msg)
00070 {
00071   char buf1[64];
00072   char buf2[64];
00073   unsigned int c = (unsigned int)timers[t][2];
00074   double d1 = get_timer(t);
00075   double d2 = get_timer(t)/c;
00076   get_time_str(d1,buf1);
00077   get_time_str(d2,buf2);
00078   printf("** timer: %-30s : %-15s : %-15s per cycle (%u cycles)\n", msg?msg:"", c?buf1:"---", c?buf2:"---", c);
00079 }
00080 
00081 #undef CNT
00082 #undef TPS
00083 
00084 #else
00085 
00086 static __inline__ void start_timer(int t) { }
00087 static __inline__ void reset_timers() { }
00088 static __inline__ void reset_timer(int t) { }
00089 static __inline__ void continue_timer(int t) { }
00090 static __inline__ void stop_timer(int t) { }
00091 static __inline__ double get_timer(int t) { return 0; }
00092 static __inline__ void print_timer(int t, char* msg) { }
00093 
00094 #endif
Documentation for spkg, Sun Jul 23 13:12:53 2006.