]> mj.ucw.cz Git - libucw.git/blob - lib/profile.h
e3e51a28401902ef343dc368cb65e8ce6d5c2b22
[libucw.git] / lib / profile.h
1 /*
2  *      Sherlock Library -- Poor Man's Profiler
3  *
4  *      (c) 2001 Martin Mares <mj@ucw.cz>
5  */
6
7 /*
8  *  Usage:
9  *              prof_t cnt;
10  *              prof_init(&cnt);
11  *              ...
12  *              prof_start(&cnt);
13  *              ...
14  *              prof_stop(&cnt);
15  *              printf("%s\n", PROF_STRING(&cnt));
16  */
17
18 /* Profiling method to use */
19 #define CONFIG_PROFILE_TOD              /* gettimeofday() */
20 #undef CONFIG_PROFILE_TSC               /* i386 TSC */
21 #undef CONFIG_PROFILE_KTSC              /* kernel TSC profiler */
22
23 #ifdef CONFIG_PROFILE_TOD
24 #define CONFIG_PROFILE
25 #define PROF_STR_SIZE 21
26
27 typedef struct {
28   u32 start_sec, start_usec;
29   s32 sec, usec;
30 } prof_t;
31
32 #endif
33
34 #ifdef CONFIG_PROFILE_TSC
35 #define CONFIG_PROFILE
36 #define CONFIG_PROFILE_INLINE
37 #define PROF_STR_SIZE 24
38
39 typedef struct {
40   u64 start_tsc;
41   u64 ticks;
42 } prof_t;
43
44 #define rdtscll(val) __asm__ __volatile__("rdtsc" : "=A" (val))
45
46 static inline void prof_start(prof_t *c)
47 {
48   rdtscll(c->start_tsc);
49 }
50
51 static inline void prof_stop(prof_t *c)
52 {
53   u64 tsc;
54   rdtscll(tsc);
55   tsc -= c->start_tsc;
56   c->ticks += tsc;
57 }
58
59 static inline void prof_switch(prof_t *o, prof_t *n)
60 {
61   u64 tsc;
62   rdtscll(tsc);
63   n->start_tsc = tsc;
64   tsc -= o->start_tsc;
65   o->ticks += tsc;
66 }
67 #endif
68
69 #ifdef CONFIG_PROFILE_KTSC
70 #define CONFIG_PROFILE
71 #define PROF_STR_SIZE 50
72
73 typedef struct {
74   u64 start_user, start_sys;
75   u64 ticks_user, ticks_sys;
76 } prof_t;
77 #endif
78
79 #ifdef CONFIG_PROFILE
80
81 /* Stuff common for all profilers */
82 #ifndef CONFIG_PROFILE_INLINE
83 void prof_switch(prof_t *, prof_t *);
84 static inline void prof_start(prof_t *c) { prof_switch(NULL, c); }
85 static inline void prof_stop(prof_t *c) { prof_switch(c, NULL); }
86 #endif
87 int prof_format(char *, prof_t *);
88 void prof_init(prof_t *);
89
90 #else
91
92 /* Dummy profiler with no output */
93 typedef struct { } prof_t;
94 static inline void prof_init(prof_t *c UNUSED) { }
95 static inline void prof_start(prof_t *c UNUSED) { }
96 static inline void prof_stop(prof_t *c UNUSED) { }
97 static inline void prof_switch(prof_t *c UNUSED, prof_t *d UNUSED) { }
98 static inline void prof_format(char *b, prof_t *c UNUSED) { strcpy(b, "?"); }
99 #define PROF_STR_SIZE 2
100
101 #endif