0
|
1 #include<stdio.h>
|
|
2 #include<stdlib.h>
|
|
3 #include<string.h>
|
|
4 #include<sys/time.h>
|
|
5 #include<unistd.h>
|
|
6 #include"profile.h"
|
|
7
|
|
8 static unsigned long timeprof_overhead;
|
|
9
|
|
10 #define timeprof_get_time(t) {\
|
|
11 struct timeval t0;\
|
|
12 gettimeofday(&t0,NULL);\
|
|
13 (t).sec=t0.tv_sec;\
|
|
14 (t).usec=t0.tv_usec;}
|
|
15
|
|
16 void timeprof_begin(Timeprof t)
|
|
17 {
|
|
18 timeprof_get_time(t->begin);
|
|
19 }
|
|
20
|
|
21 int timeprof_end(Timeprof t)
|
|
22 {
|
|
23 unsigned int etime;
|
|
24 timeprof_get_time(t->end);
|
|
25 if (t->begin.usec > t->end.usec) {
|
|
26 t->end.usec += 1000000;
|
|
27 t->end.sec--;
|
|
28 }
|
|
29 etime = ((t->end.sec - t->begin.sec) * 1000000 +
|
|
30 (t->end.usec - t->begin.usec)) - timeprof_overhead;
|
|
31 if (etime > t->peak) {
|
|
32 t->peak = etime;
|
|
33 }
|
|
34 if (t->average > 0) {
|
|
35 t->average += etime;
|
|
36 t->average /= 2;
|
|
37 } else {
|
|
38 t->average = etime;
|
|
39 }
|
|
40 return etime;
|
|
41 }
|
|
42
|
|
43 void timeprof_sprint(char *s, const char *profname, Timeprof t)
|
|
44 {
|
|
45 sprintf(s, "%s: average:%dusec, peak:%dusec", profname, t->average,
|
|
46 t->peak);
|
|
47 }
|
|
48
|
|
49 void timeprof_init()
|
|
50 {
|
|
51 struct time_profile t;
|
|
52 timeprof_begin(&t);
|
|
53 timeprof_overhead = timeprof_end(&t);
|
|
54 }
|
|
55
|
|
56 Timeprof timeprof_new()
|
|
57 {
|
|
58 Timeprof t = (Timeprof) malloc(sizeof(struct time_profile));
|
|
59 memset(t, 0, sizeof(struct time_profile));
|
|
60 return t;
|
|
61 }
|