]> mj.ucw.cz Git - libucw.git/commitdiff
Rewrote the profiler. Each module can now choose its own profiling method
authorMartin Mares <mj@ucw.cz>
Sun, 2 Dec 2001 12:03:39 +0000 (12:03 +0000)
committerMartin Mares <mj@ucw.cz>
Sun, 2 Dec 2001 12:03:39 +0000 (12:03 +0000)
without recompiling the library.

lib/profile.c
lib/profile.h

index dea6b93240a36c0beda8b6737f4f756360c64e04..f78eae746af4fbe5c4bbf7edc9e338a8cdde9c75 100644 (file)
@@ -9,17 +9,18 @@
 
 #include <stdio.h>
 
-#ifdef CONFIG_PROFILE_TOD
+/* PROFILE_TOD */
+
 #include <sys/time.h>
 
 void
-prof_init(prof_t *c)
+prof_tod_init(struct prof_tod *c)
 {
   c->sec = c->usec = 0;
 }
 
 void
-prof_switch(prof_t *o, prof_t *n)
+prof_tod_switch(struct prof_tod *o, struct prof_tod *n)
 {
   struct timeval tv;
   gettimeofday(&tv, NULL);
@@ -46,33 +47,39 @@ prof_switch(prof_t *o, prof_t *n)
 }
 
 int
-prof_format(char *buf, prof_t *c)
+prof_tod_format(char *buf, struct prof_tod *c)
 {
   return sprintf(buf, "%d.%06d", c->sec, c->usec);
 }
-#endif
 
-#ifdef CONFIG_PROFILE_TSC
+/* PROFILE_TSC */
+
+#ifdef CPU_I386
+
 void
-prof_init(prof_t *c)
+prof_tsc_init(struct prof_tsc *c)
 {
   c->ticks = 0;
 }
 
 int
-prof_format(char *buf, prof_t *c)
+prof_tsc_format(char *buf, struct prof_tsc *c)
 {
   return sprintf(buf, "%Ld", c->ticks);
 }
+
 #endif
 
-#ifdef CONFIG_PROFILE_KTSC
+/* PROFILE_KTSC */
+
+#ifdef CONFIG_LINUX
+
 #include <fcntl.h>
 #include <unistd.h>
 static int self_prof_fd = -1;
 
 void
-prof_init(prof_t *c)
+prof_ktsc_init(struct prof_ktsc *c)
 {
   if (self_prof_fd < 0)
     {
@@ -85,7 +92,7 @@ prof_init(prof_t *c)
 }
 
 void
-prof_switch(prof_t *o, prof_t *n)
+prof_ktsc_switch(struct prof_ktsc *o, struct prof_ktsc *n)
 {
   u64 u, s;
   byte buf[256];
@@ -111,8 +118,9 @@ prof_switch(prof_t *o, prof_t *n)
 }
 
 int
-prof_format(char *buf, prof_t *c)
+prof_ktsc_format(char *buf, struct prof_ktsc *c)
 {
   return sprintf(buf, "%Ld+%Ld", c->ticks_user, c->ticks_sys);
 }
+
 #endif
index e3e51a28401902ef343dc368cb65e8ce6d5c2b22..426b3a30512fdeba73d56071e3568e114cb994ee 100644 (file)
@@ -6,6 +6,8 @@
 
 /*
  *  Usage:
+ *             #define PROFILE_xxx
+ *             #include "lib/profile.h"
  *             prof_t cnt;
  *             prof_init(&cnt);
  *             ...
  *             printf("%s\n", PROF_STRING(&cnt));
  */
 
-/* Profiling method to use */
-#define CONFIG_PROFILE_TOD             /* gettimeofday() */
-#undef CONFIG_PROFILE_TSC              /* i386 TSC */
-#undef CONFIG_PROFILE_KTSC             /* kernel TSC profiler */
+/* PROFILE_TOD: gettimeofday() profiler */
 
-#ifdef CONFIG_PROFILE_TOD
-#define CONFIG_PROFILE
-#define PROF_STR_SIZE 21
-
-typedef struct {
+struct prof_tod {
   u32 start_sec, start_usec;
   s32 sec, usec;
-} prof_t;
+};
 
-#endif
+void prof_tod_init(struct prof_tod *);
+void prof_tod_switch(struct prof_tod *, struct prof_tod *);
+int prof_tod_format(char *, struct prof_tod *);
 
-#ifdef CONFIG_PROFILE_TSC
-#define CONFIG_PROFILE
-#define CONFIG_PROFILE_INLINE
-#define PROF_STR_SIZE 24
+/* PROFILE_TSC: i386 TSC profiler */
 
-typedef struct {
+#ifdef CPU_I386
+
+struct prof_tsc {
   u64 start_tsc;
   u64 ticks;
-} prof_t;
+};
+
+void prof_tsc_init(struct prof_tsc *);
+int prof_tsc_format(char *, struct prof_tsc *);
+
+#endif
+
+/* PROFILE_KTSC: Linux kernel TSC profiler */
+
+#ifdef CONFIG_LINUX
+
+struct prof_ktsc {
+  u64 start_user, start_sys;
+  u64 ticks_user, ticks_sys;
+};
+
+void prof_ktsc_init(struct prof_ktsc *);
+void prof_ktsc_switch(struct prof_ktsc *, struct prof_ktsc *);
+int prof_ktsc_format(char *, struct prof_ktsc *);
+
+#endif
+
+/* Select the right profiler */
+
+#if defined(PROFILE_TOD)
+
+#define PROFILER
+#define PROF_STR_SIZE 21
+typedef struct prof_tod prof_t;
+#define prof_init prof_tod_init
+#define prof_switch prof_tod_switch
+#define prof_format prof_tod_format
+
+#elif defined(PROFILE_TSC)
+
+#define PROFILER
+#define PROFILER_INLINE
+#define PROF_STR_SIZE 24
+
+typedef struct prof_tsc prof_t;
+#define prof_init prof_tsc_init
+#define prof_format prof_tsc_format
 
 #define rdtscll(val) __asm__ __volatile__("rdtsc" : "=A" (val))
 
@@ -64,28 +101,26 @@ static inline void prof_switch(prof_t *o, prof_t *n)
   tsc -= o->start_tsc;
   o->ticks += tsc;
 }
-#endif
 
-#ifdef CONFIG_PROFILE_KTSC
-#define CONFIG_PROFILE
+#elif defined(PROFILE_KTSC)
+
+#define PROFILER
 #define PROF_STR_SIZE 50
+typedef struct prof_ktsc prof_t;
+#define prof_init prof_ktsc_init
+#define prof_switch prof_ktsc_switch
+#define prof_format prof_ktsc_format
 
-typedef struct {
-  u64 start_user, start_sys;
-  u64 ticks_user, ticks_sys;
-} prof_t;
 #endif
 
-#ifdef CONFIG_PROFILE
+#ifdef PROFILER
 
 /* Stuff common for all profilers */
-#ifndef CONFIG_PROFILE_INLINE
-void prof_switch(prof_t *, prof_t *);
+#ifndef PROFILER_INLINE
 static inline void prof_start(prof_t *c) { prof_switch(NULL, c); }
 static inline void prof_stop(prof_t *c) { prof_switch(c, NULL); }
 #endif
-int prof_format(char *, prof_t *);
-void prof_init(prof_t *);
+#define PROF_STR(c) ({ static byte _x[PROF_STR_SIZE]; prof_format(_x, &(C)); _x })
 
 #else