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 "ucw/profile.h"
20 * printf("%s\n", PROF_STR(cnt));
23 #ifndef _UCW_PROFILE_H
24 #define _UCW_PROFILE_H
26 /* PROFILE_TOD: gettimeofday() profiler */
29 u32 start_sec, start_usec;
33 void prof_tod_init(struct prof_tod *);
34 void prof_tod_switch(struct prof_tod *, struct prof_tod *);
35 int prof_tod_format(char *, struct prof_tod *);
37 /* PROFILE_TSC: i386 TSC profiler */
46 void prof_tsc_init(struct prof_tsc *);
47 int prof_tsc_format(char *, struct prof_tsc *);
51 /* PROFILE_KTSC: Linux kernel TSC profiler */
56 u64 start_user, start_sys;
57 u64 ticks_user, ticks_sys;
60 void prof_ktsc_init(struct prof_ktsc *);
61 void prof_ktsc_switch(struct prof_ktsc *, struct prof_ktsc *);
62 int prof_ktsc_format(char *, struct prof_ktsc *);
66 /* Select the right profiler */
68 #if defined(PROFILE_TOD)
71 #define PROF_STR_SIZE 21
72 typedef struct prof_tod prof_t;
73 #define prof_init prof_tod_init
74 #define prof_switch prof_tod_switch
75 #define prof_format prof_tod_format
77 #elif defined(PROFILE_TSC)
80 #define PROFILER_INLINE
81 #define PROF_STR_SIZE 24
83 typedef struct prof_tsc prof_t;
84 #define prof_init prof_tsc_init
85 #define prof_format prof_tsc_format
87 #define rdtscll(val) __asm__ __volatile__("rdtsc" : "=A" (val))
89 static inline void prof_start(prof_t *c)
91 rdtscll(c->start_tsc);
94 static inline void prof_stop(prof_t *c)
102 static inline void prof_switch(prof_t *o, prof_t *n)
111 #elif defined(PROFILE_KTSC)
114 #define PROF_STR_SIZE 50
115 typedef struct prof_ktsc prof_t;
116 #define prof_init prof_ktsc_init
117 #define prof_switch prof_ktsc_switch
118 #define prof_format prof_ktsc_format
124 /* Stuff common for all profilers */
125 #ifndef PROFILER_INLINE
126 static inline void prof_start(prof_t *c) { prof_switch(NULL, c); }
127 static inline void prof_stop(prof_t *c) { prof_switch(c, NULL); }
129 #define PROF_STR(C) ({ static char _x[PROF_STR_SIZE]; prof_format(_x, &(C)); _x; })
133 /* Dummy profiler with no output */
134 typedef struct { } prof_t;
135 static inline void prof_init(prof_t *c UNUSED) { }
136 static inline void prof_start(prof_t *c UNUSED) { }
137 static inline void prof_stop(prof_t *c UNUSED) { }
138 static inline void prof_switch(prof_t *c UNUSED, prof_t *d UNUSED) { }
139 static inline void prof_format(char *b, prof_t *c UNUSED) { b[0]='?'; b[1]=0; }
140 #define PROF_STR_SIZE 2
141 #define PROF_STR(C) "?"