2 * UCW Library -- Poor Man's Profiler
4 * (c) 2001 Martin Mares <mj@ucw.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU Lesser General Public License.
13 * #include "lib/profile.h"
20 * printf("%s\n", PROF_STR(cnt));
23 /* PROFILE_TOD: gettimeofday() profiler */
26 u32 start_sec, start_usec;
30 void prof_tod_init(struct prof_tod *);
31 void prof_tod_switch(struct prof_tod *, struct prof_tod *);
32 int prof_tod_format(char *, struct prof_tod *);
34 /* PROFILE_TSC: i386 TSC profiler */
43 void prof_tsc_init(struct prof_tsc *);
44 int prof_tsc_format(char *, struct prof_tsc *);
48 /* PROFILE_KTSC: Linux kernel TSC profiler */
53 u64 start_user, start_sys;
54 u64 ticks_user, ticks_sys;
57 void prof_ktsc_init(struct prof_ktsc *);
58 void prof_ktsc_switch(struct prof_ktsc *, struct prof_ktsc *);
59 int prof_ktsc_format(char *, struct prof_ktsc *);
63 /* Select the right profiler */
65 #if defined(PROFILE_TOD)
68 #define PROF_STR_SIZE 21
69 typedef struct prof_tod prof_t;
70 #define prof_init prof_tod_init
71 #define prof_switch prof_tod_switch
72 #define prof_format prof_tod_format
74 #elif defined(PROFILE_TSC)
77 #define PROFILER_INLINE
78 #define PROF_STR_SIZE 24
80 typedef struct prof_tsc prof_t;
81 #define prof_init prof_tsc_init
82 #define prof_format prof_tsc_format
84 #define rdtscll(val) __asm__ __volatile__("rdtsc" : "=A" (val))
86 static inline void prof_start(prof_t *c)
88 rdtscll(c->start_tsc);
91 static inline void prof_stop(prof_t *c)
99 static inline void prof_switch(prof_t *o, prof_t *n)
108 #elif defined(PROFILE_KTSC)
111 #define PROF_STR_SIZE 50
112 typedef struct prof_ktsc prof_t;
113 #define prof_init prof_ktsc_init
114 #define prof_switch prof_ktsc_switch
115 #define prof_format prof_ktsc_format
121 /* Stuff common for all profilers */
122 #ifndef PROFILER_INLINE
123 static inline void prof_start(prof_t *c) { prof_switch(NULL, c); }
124 static inline void prof_stop(prof_t *c) { prof_switch(c, NULL); }
126 #define PROF_STR(C) ({ static char _x[PROF_STR_SIZE]; prof_format(_x, &(C)); _x; })
130 /* Dummy profiler with no output */
131 typedef struct { } prof_t;
132 static inline void prof_init(prof_t *c UNUSED) { }
133 static inline void prof_start(prof_t *c UNUSED) { }
134 static inline void prof_stop(prof_t *c UNUSED) { }
135 static inline void prof_switch(prof_t *c UNUSED, prof_t *d UNUSED) { }
136 static inline void prof_format(char *b, prof_t *c UNUSED) { b[0]='?'; b[1]=0; }
137 #define PROF_STR_SIZE 2
138 #define PROF_STR(C) "?"