Add proper per-file copyright notices/licenses and top-level license.
[bluesky.git] / TBBT / trace_play / profile.c
1 /*
2  * profile.c
3  *
4  * $Id: profile.c,v 1.1 2002/08/21 22:08:01 ningning Exp $
5  * Changes:
6  *      $Log: profile.c,v $
7  *      Revision 1.1  2002/08/21 22:08:01  ningning
8  *      *** empty log message ***
9  *      
10  */
11
12 #include <sys/time.h>
13 #include <sys/sem.h>
14 #include <unistd.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include "profile.h"
19 #include "rfs_assert.h"
20 //#include "rfs_c_def.h"
21 extern FILE * profile_fp;
22
23 static struct timezone tz;
24 inline void calculate_interval 
25         (struct timeval * ts, struct timeval * te, struct timeval * interval)
26 {
27         if (te->tv_usec < ts->tv_usec) {
28                 if (te->tv_sec <= ts->tv_sec) {
29                         printf ("te->tv_sec %d ts->tv_sec %d\n", te->tv_sec, ts->tv_sec);
30                         printf ("te->tv_usec %d ts->tv_usec %d\n", te->tv_usec, ts->tv_usec);
31                 }
32                 RFS_ASSERT (te->tv_sec > ts->tv_sec);
33                 te->tv_usec += 1000000;
34                 te->tv_sec -= 1;
35         }
36
37         interval->tv_sec  = te->tv_sec - ts->tv_sec;
38         interval->tv_usec = te->tv_usec - ts->tv_usec;
39         if (interval->tv_usec > 1000000) {
40                 if (interval->tv_usec > 2000000) {
41                         printf ("interval->tv_sec %d interval->tv_usec %d \n", interval->tv_sec, interval->tv_usec);
42                         printf ("ts->tv_sec %d ts->tv_usec %d \n", ts->tv_sec, ts->tv_usec);
43                         printf ("te->tv_sec %d te->tv_usec %d \n", te->tv_sec, te->tv_usec);
44                 }
45                 /* Sometimes it can happend that te->tv_usec > 1000000 */
46                 interval->tv_sec += 1;
47                 interval->tv_usec -= 1000000;
48                 RFS_ASSERT (interval->tv_usec < 1000000);
49         }
50 }
51
52 inline void normalize_profile (int pos, struct timeval * time)
53 {
54         if (!(time->tv_sec >=0 && time->tv_usec >=0 && time->tv_usec < 2000000)) {
55                 printf ("pos %d tv_sec %d tv_usec %d\n", pos, time->tv_sec, time->tv_usec);
56         };
57         RFS_ASSERT (time->tv_sec >=0 && time->tv_usec >=0 && time->tv_usec < 2000000);
58         while (time->tv_usec >= 1000000) {
59                 time->tv_usec -= 1000000;
60                 time->tv_sec += 1;
61         }
62 }
63
64 inline void start_real_profile (profile_t * profile)
65 {
66         start_profile(profile);
67 }
68
69 inline void end_real_profile (profile_t * profile)
70 {
71         end_profile(profile);
72 }
73
74 inline void start_profile (profile_t * profile)
75 {
76 /*
77         if (strlen(profile->about) < 3) {
78                 printf ("total_profile address: %x %x\n", &total_profile, profile);
79         }
80 */
81
82         gettimeofday(&(profile->ts), &tz);
83         normalize_profile (1, &(profile->ts));
84 }
85
86 inline void end_profile (profile_t * profile)
87 {
88         struct timeval te, teorg;
89         struct timeval * ts; 
90         struct timeval * in;
91         struct timeval oldin;
92         
93 /*
94         //printf ("end_profile %s\n", profile->about);
95
96         if (strlen(profile->about) < 3) {
97                 printf ("total_profile address: %x %x\n", &total_profile, profile);
98         }
99 */
100
101         oldin = profile->in;
102         in = &(profile->in);
103         ts = &(profile->ts);
104
105         gettimeofday(&te, &tz);
106         normalize_profile (2, &te);
107         teorg = te;
108
109         RFS_ASSERT (te.tv_sec >= ts->tv_sec);
110         RFS_ASSERT (te.tv_usec >=0 && ts->tv_usec >=0);
111         while (te.tv_usec < ts->tv_usec) {
112                 if (te.tv_sec <= ts->tv_sec) {
113                         printf ("%s ts.tv_sec %d ts.tv_usec %d\n", profile->about, ts->tv_sec, ts->tv_usec);
114                         printf ("teorg.tv_sec %d teorg.tv_usec %d\n", teorg.tv_sec, teorg.tv_usec);
115                 }
116                 RFS_ASSERT (te.tv_sec > ts->tv_sec);
117                 te.tv_usec += 1000000;
118                 te.tv_sec -= 1;
119         }
120
121         if (!(in->tv_sec >=0 && in->tv_usec >=0)) {
122                 printf ("in->tv_sec %d, in->tv_usec %d\n", in->tv_sec, in->tv_usec);
123         };
124         RFS_ASSERT (in->tv_sec >=0 && in->tv_usec >=0);
125         in->tv_sec  += te.tv_sec - ts->tv_sec;
126         in->tv_usec += te.tv_usec - ts->tv_usec;
127         normalize_profile (3, in);
128
129         if (!(in->tv_sec >=0 && in->tv_sec <864000)) {
130                  printf (" ts.tv_sec %d ts.tv_usec %d\n", ts->tv_sec, ts->tv_usec);
131                  printf (" te.tv_sec %d te.tv_usec %d\n", te.tv_sec, te.tv_usec);
132                  printf (" in.tv_sec %d in.tv_usec %d\n", in->tv_sec, in->tv_usec);
133                  printf (" oldin.tv_sec %d oldin.tv_usec %d\n", oldin.tv_sec, oldin.tv_usec);
134         }
135
136         profile->num ++;
137         profile->ts = teorg;
138 }
139
140 inline void init_profile (char * string, profile_t * profile)
141 {
142         RFS_ASSERT (strlen(string) < sizeof (profile->about));
143         memset (profile, 0, sizeof(profile_t));
144         strcpy (profile->about, string);
145 }
146
147 inline int calculate_avg_timeval (struct timeval * in, int num)
148 {
149         unsigned long long i;
150         int ret;
151
152         if (in->tv_sec < 2000) {
153                 return ((in->tv_sec*1000000+in->tv_usec)/num );
154         } else {
155                 i = ((unsigned long long)in->tv_sec)*1000000 + in->tv_usec;
156                 i/= num;
157                 RFS_ASSERT (i<2000000000);
158                 ret = i;
159                 return ret;
160         }
161 }
162
163 inline void print_profile (char * string, profile_t * profile)
164 {
165         struct timeval * ts = &(profile->ts);
166         struct timeval * in = &(profile->in);
167
168 /*
169         if (strcmp (string, profile->about)) {
170                 printf ("print_profile string %s about %s\n", string, profile->about);
171         }
172 */
173
174         //RFS_ASSERT (!strcmp (string, profile->about));
175         if (in->tv_usec<0 || in->tv_usec>1000000) {
176                 printf ("%s in->tv_usec %d, in->tv_sec %d num %d\n", profile->about, in->tv_usec, in->tv_sec, profile->num);
177         }
178
179         RFS_ASSERT (in->tv_usec>=0 && in->tv_usec<1000000);
180         
181         if (!(in->tv_sec >=0 && in->tv_sec <864000)) {
182                  printf ("%s ts.tv_sec %d ts.tv_usec %d\n", profile->about, ts->tv_sec, ts->tv_usec);
183                  printf ("%s in.tv_sec %d in.tv_usec %d\n", profile->about, in->tv_sec, in->tv_usec);
184         }
185         RFS_ASSERT (in->tv_sec >=0 && in->tv_sec <864000);      /* it's about 10 days */
186
187         if (profile->num == 0) {
188                 printf("... %40s %3d.%06d num %d \n", profile->about, in->tv_sec, in->tv_usec, 
189                         profile->num);
190
191                 if (profile_fp) {
192                         fprintf(profile_fp, "... %40s %3d.%06d num %d \n", profile->about, in->tv_sec,
193                                 in->tv_usec, profile->num );
194                         //perror("print_profile_1");
195                 }
196         } else {
197
198                 int avg = calculate_avg_timeval (in, profile->num);
199                 printf("... %40s %3d.%06d num %d avg %d \n", profile->about, in->tv_sec, in->tv_usec, 
200                         profile->num, avg);
201
202                 if (profile_fp) {
203                         fprintf(profile_fp, "... %40s %3d.%06d num %d avg %d \n", profile->about, in->tv_sec,
204                                 in->tv_usec, profile->num, avg);
205                 }
206
207 /*
208                 printf("... %40s %3d.%06d num %d avg %d \n", string, in->tv_sec, in->tv_usec, 
209                         profile->num, (in->tv_sec*1000000+in->tv_usec)/profile->num );
210
211                 if (profile_fp) {
212                         fprintf(profile_fp, "... %40s %3d.%06d num %d avg %d \n", string, in->tv_sec,
213                                 in->tv_usec, profile->num, (in->tv_sec*1000000+in->tv_usec)/profile->num );
214                 }
215 */
216         }
217 }