00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055 #ifndef _profile_h
00056 #define _profile_h
00057
00058 #include <string.h>
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071 #if defined __CPU_i386 && ! defined __CPU_x86
00072 #define __CPU_x86
00073 #endif
00074
00075 #ifdef __CPU_x86
00076 typedef unsigned long long cycles_t;
00077
00078 inline static cycles_t get_cpu_cycles()
00079 {
00080 cycles_t r;
00081 asm volatile( "rdtsc \n\t" : "=A"(r));
00082 return r;
00083 }
00084
00085 #define get_cpu_cycles_uint(u1, u2) \
00086 do{ \
00087 \
00088 asm volatile( "rdtsc \n\t" : "=a"(*(u1)), "=d"(*(u2))); \
00089 }while(0)
00090
00091 #elif defined __CPU_x86_64
00092 typedef unsigned long long cycles_t;
00093
00094 inline static cycles_t get_cpu_cycles()
00095 {
00096 unsigned int u1, u2;
00097 asm volatile( "rdtsc \n\t" : "=a"(u1), "=d"(u2));
00098 return ((cycles_t)u2<<32ULL)|u1;
00099 }
00100
00101
00102 #define get_cpu_cycles_uint(u1, u2) \
00103 do{ \
00104 \
00105 asm volatile( "rdtsc \n\t" : "=a"(*(u1)), "=d"(*(u2))); \
00106 }while(0)
00107
00108 #elif defined __CPU_sparc64
00109
00110 typedef unsigned long long cycles_t;
00111
00112 inline static cycles_t get_cpu_cycles()
00113 {
00114 #if ! defined(_LP64)
00115 #warning "ilp32 mode "
00116 struct uint_64{
00117 unsigned int u2;
00118 unsigned int u1;
00119 };
00120 union{
00121 cycles_t c;
00122 struct uint_64 u;
00123 }r;
00124
00125 asm volatile("rd %%tick, %0 \n\t"
00126 "srlx %0, 32, %1 \n\t"
00127 : "=r"(r.u.u1), "=r"(r.u.u2));
00128 return r.c;
00129 #else
00130 cycles_t r;
00131
00132 asm volatile("rd %%tick, %0" : "=r"(r));
00133 return r;
00134 #endif
00135 }
00136 inline static void get_cpu_cycles_uint(unsigned int* u1, unsigned int* u2)
00137 {
00138 cycles_t r;
00139 asm volatile("rd %%tick, %0" : "=r"(r));
00140 *u1=(unsigned int)r;
00141 *u2=(unsigned int)(r>>32);
00142 }
00143
00144 #else
00145 #error "no get_cycles support for this CPU"
00146 #endif
00147
00148
00149 union profile_cycles{
00150 cycles_t c;
00151 struct{
00152 unsigned int u1;
00153 unsigned int u2;
00154 }uint;
00155 };
00156
00157 struct profile_data{
00158 cycles_t cycles;
00159 cycles_t total_cycles;
00160 cycles_t max_cycles;
00161 unsigned long entries;
00162 unsigned long exits;
00163 char * name;
00164
00165
00166 union profile_cycles init_rdtsc;
00167 };
00168
00169 inline static void profile_init(struct profile_data* pd, char *name)
00170 {
00171 memset(pd, 0, sizeof(*pd));
00172 pd->name=name;
00173 }
00174
00175
00176 inline static void profile_start(struct profile_data* pd)
00177 {
00178 pd->entries++;
00179 pd->init_rdtsc.c=get_cpu_cycles();
00180 }
00181
00182
00183 inline static void profile_end(struct profile_data* pd)
00184 {
00185 pd->cycles=get_cpu_cycles()-pd->init_rdtsc.c;
00186 if (pd->max_cycles<pd->cycles) pd->max_cycles=pd->cycles;
00187 pd->total_cycles+=pd->cycles;
00188 pd->exits++;
00189 }
00190
00191
00192 #endif