2 * Sherlock 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.
11 #include "lib/profile.h"
20 prof_tod_init(struct prof_tod *c)
26 prof_tod_switch(struct prof_tod *o, struct prof_tod *n)
29 gettimeofday(&tv, NULL);
32 n->start_sec = tv.tv_sec;
33 n->start_usec = tv.tv_usec;
37 o->sec += tv.tv_sec - o->start_sec;
38 o->usec += tv.tv_usec - o->start_usec;
44 else while (o->usec >= 1000000)
53 prof_tod_format(char *buf, struct prof_tod *c)
55 return sprintf(buf, "%d.%06d", c->sec, c->usec);
63 prof_tsc_init(struct prof_tsc *c)
69 prof_tsc_format(char *buf, struct prof_tsc *c)
71 return sprintf(buf, "%Ld", c->ticks);
82 static int self_prof_fd = -1;
85 prof_ktsc_init(struct prof_ktsc *c)
89 self_prof_fd = open("/proc/self/profile", O_RDONLY, 0);
91 die("Unable to open /proc/self/profile: %m");
98 prof_ktsc_switch(struct prof_ktsc *o, struct prof_ktsc *n)
103 int l = pread(self_prof_fd, buf, sizeof(buf)-1, 0);
104 ASSERT(l > 0 && l < (int)sizeof(buf)-1);
106 l = sscanf(buf, "%Ld%Ld", &u, &s);
124 prof_ktsc_format(char *buf, struct prof_ktsc *c)
126 return sprintf(buf, "%Ld+%Ld", c->ticks_user, c->ticks_sys);