2 * Sherlock Library -- Poor Man's Profiler
4 * (c) 2001 Martin Mares <mj@ucw.cz>
8 #include "lib/profile.h"
17 prof_tod_init(struct prof_tod *c)
23 prof_tod_switch(struct prof_tod *o, struct prof_tod *n)
26 gettimeofday(&tv, NULL);
29 n->start_sec = tv.tv_sec;
30 n->start_usec = tv.tv_usec;
34 o->sec += tv.tv_sec - o->start_sec;
35 o->usec += tv.tv_usec - o->start_usec;
41 else while (o->usec >= 1000000)
50 prof_tod_format(char *buf, struct prof_tod *c)
52 return sprintf(buf, "%d.%06d", c->sec, c->usec);
60 prof_tsc_init(struct prof_tsc *c)
66 prof_tsc_format(char *buf, struct prof_tsc *c)
68 return sprintf(buf, "%Ld", c->ticks);
79 static int self_prof_fd = -1;
82 prof_ktsc_init(struct prof_ktsc *c)
86 self_prof_fd = open("/proc/self/profile", O_RDONLY, 0);
88 die("Unable to open /proc/self/profile: %m");
95 prof_ktsc_switch(struct prof_ktsc *o, struct prof_ktsc *n)
100 int l = pread(self_prof_fd, buf, sizeof(buf)-1, 0);
101 ASSERT(l > 0 && l < (int)sizeof(buf)-1);
103 l = sscanf(buf, "%Ld%Ld", &u, &s);
121 prof_ktsc_format(char *buf, struct prof_ktsc *c)
123 return sprintf(buf, "%Ld+%Ld", c->ticks_user, c->ticks_sys);