2 * Sherlock Library -- Poor Man's Profiler
4 * (c) 2001 Martin Mares <mj@ucw.cz>
10 * #include "lib/profile.h"
17 * printf("%s\n", PROF_STRING(&cnt));
22 /* PROFILE_TOD: gettimeofday() profiler */
25 u32 start_sec, start_usec;
29 void prof_tod_init(struct prof_tod *);
30 void prof_tod_switch(struct prof_tod *, struct prof_tod *);
31 int prof_tod_format(char *, struct prof_tod *);
33 /* PROFILE_TSC: i386 TSC profiler */
42 void prof_tsc_init(struct prof_tsc *);
43 int prof_tsc_format(char *, struct prof_tsc *);
47 /* PROFILE_KTSC: Linux kernel TSC profiler */
52 u64 start_user, start_sys;
53 u64 ticks_user, ticks_sys;
56 void prof_ktsc_init(struct prof_ktsc *);
57 void prof_ktsc_switch(struct prof_ktsc *, struct prof_ktsc *);
58 int prof_ktsc_format(char *, struct prof_ktsc *);
62 /* Select the right profiler */
64 #if defined(PROFILE_TOD)
67 #define PROF_STR_SIZE 21
68 typedef struct prof_tod prof_t;
69 #define prof_init prof_tod_init
70 #define prof_switch prof_tod_switch
71 #define prof_format prof_tod_format
73 #elif defined(PROFILE_TSC)
76 #define PROFILER_INLINE
77 #define PROF_STR_SIZE 24
79 typedef struct prof_tsc prof_t;
80 #define prof_init prof_tsc_init
81 #define prof_format prof_tsc_format
83 #define rdtscll(val) __asm__ __volatile__("rdtsc" : "=A" (val))
85 static inline void prof_start(prof_t *c)
87 rdtscll(c->start_tsc);
90 static inline void prof_stop(prof_t *c)
98 static inline void prof_switch(prof_t *o, prof_t *n)
107 #elif defined(PROFILE_KTSC)
110 #define PROF_STR_SIZE 50
111 typedef struct prof_ktsc prof_t;
112 #define prof_init prof_ktsc_init
113 #define prof_switch prof_ktsc_switch
114 #define prof_format prof_ktsc_format
120 /* Stuff common for all profilers */
121 #ifndef PROFILER_INLINE
122 static inline void prof_start(prof_t *c) { prof_switch(NULL, c); }
123 static inline void prof_stop(prof_t *c) { prof_switch(c, NULL); }
125 #define PROF_STR(c) ({ static byte _x[PROF_STR_SIZE]; prof_format(_x, &(C)); _x })
129 /* Dummy profiler with no output */
130 typedef struct { } prof_t;
131 static inline void prof_init(prof_t *c UNUSED) { }
132 static inline void prof_start(prof_t *c UNUSED) { }
133 static inline void prof_stop(prof_t *c UNUSED) { }
134 static inline void prof_switch(prof_t *c UNUSED, prof_t *d UNUSED) { }
135 static inline void prof_format(char *b, prof_t *c UNUSED) { strcpy(b, "?"); }
136 #define PROF_STR_SIZE 2