4 * $Id: profile.c,v 1.1 2002/08/21 22:08:01 ningning Exp $
7 * Revision 1.1 2002/08/21 22:08:01 ningning
8 * *** empty log message ***
17 #include "rfs_assert.h"
18 //#include "rfs_c_def.h"
19 extern FILE * profile_fp;
21 static struct timezone tz;
22 inline void calculate_interval
23 (struct timeval * ts, struct timeval * te, struct timeval * interval)
25 if (te->tv_usec < ts->tv_usec) {
26 if (te->tv_sec <= ts->tv_sec) {
27 printf ("te->tv_sec %d ts->tv_sec %d\n", te->tv_sec, ts->tv_sec);
28 printf ("te->tv_usec %d ts->tv_usec %d\n", te->tv_usec, ts->tv_usec);
30 RFS_ASSERT (te->tv_sec > ts->tv_sec);
31 te->tv_usec += 1000000;
35 interval->tv_sec = te->tv_sec - ts->tv_sec;
36 interval->tv_usec = te->tv_usec - ts->tv_usec;
37 if (interval->tv_usec > 1000000) {
38 if (interval->tv_usec > 2000000) {
39 printf ("interval->tv_sec %d interval->tv_usec %d \n", interval->tv_sec, interval->tv_usec);
40 printf ("ts->tv_sec %d ts->tv_usec %d \n", ts->tv_sec, ts->tv_usec);
41 printf ("te->tv_sec %d te->tv_usec %d \n", te->tv_sec, te->tv_usec);
43 /* Sometimes it can happend that te->tv_usec > 1000000 */
44 interval->tv_sec += 1;
45 interval->tv_usec -= 1000000;
46 RFS_ASSERT (interval->tv_usec < 1000000);
50 inline void normalize_profile (int pos, struct timeval * time)
52 if (!(time->tv_sec >=0 && time->tv_usec >=0 && time->tv_usec < 2000000)) {
53 printf ("pos %d tv_sec %d tv_usec %d\n", pos, time->tv_sec, time->tv_usec);
55 RFS_ASSERT (time->tv_sec >=0 && time->tv_usec >=0 && time->tv_usec < 2000000);
56 while (time->tv_usec >= 1000000) {
57 time->tv_usec -= 1000000;
62 inline void start_real_profile (profile_t * profile)
64 start_profile(profile);
67 inline void end_real_profile (profile_t * profile)
72 inline void start_profile (profile_t * profile)
75 if (strlen(profile->about) < 3) {
76 printf ("total_profile address: %x %x\n", &total_profile, profile);
80 gettimeofday(&(profile->ts), &tz);
81 normalize_profile (1, &(profile->ts));
84 inline void end_profile (profile_t * profile)
86 struct timeval te, teorg;
92 //printf ("end_profile %s\n", profile->about);
94 if (strlen(profile->about) < 3) {
95 printf ("total_profile address: %x %x\n", &total_profile, profile);
103 gettimeofday(&te, &tz);
104 normalize_profile (2, &te);
107 RFS_ASSERT (te.tv_sec >= ts->tv_sec);
108 RFS_ASSERT (te.tv_usec >=0 && ts->tv_usec >=0);
109 while (te.tv_usec < ts->tv_usec) {
110 if (te.tv_sec <= ts->tv_sec) {
111 printf ("%s ts.tv_sec %d ts.tv_usec %d\n", profile->about, ts->tv_sec, ts->tv_usec);
112 printf ("teorg.tv_sec %d teorg.tv_usec %d\n", teorg.tv_sec, teorg.tv_usec);
114 RFS_ASSERT (te.tv_sec > ts->tv_sec);
115 te.tv_usec += 1000000;
119 if (!(in->tv_sec >=0 && in->tv_usec >=0)) {
120 printf ("in->tv_sec %d, in->tv_usec %d\n", in->tv_sec, in->tv_usec);
122 RFS_ASSERT (in->tv_sec >=0 && in->tv_usec >=0);
123 in->tv_sec += te.tv_sec - ts->tv_sec;
124 in->tv_usec += te.tv_usec - ts->tv_usec;
125 normalize_profile (3, in);
127 if (!(in->tv_sec >=0 && in->tv_sec <864000)) {
128 printf (" ts.tv_sec %d ts.tv_usec %d\n", ts->tv_sec, ts->tv_usec);
129 printf (" te.tv_sec %d te.tv_usec %d\n", te.tv_sec, te.tv_usec);
130 printf (" in.tv_sec %d in.tv_usec %d\n", in->tv_sec, in->tv_usec);
131 printf (" oldin.tv_sec %d oldin.tv_usec %d\n", oldin.tv_sec, oldin.tv_usec);
138 inline void init_profile (char * string, profile_t * profile)
140 RFS_ASSERT (strlen(string) < sizeof (profile->about));
141 memset (profile, 0, sizeof(profile_t));
142 strcpy (profile->about, string);
145 inline int calculate_avg_timeval (struct timeval * in, int num)
147 unsigned long long i;
150 if (in->tv_sec < 2000) {
151 return ((in->tv_sec*1000000+in->tv_usec)/num );
153 i = ((unsigned long long)in->tv_sec)*1000000 + in->tv_usec;
155 RFS_ASSERT (i<2000000000);
161 inline void print_profile (char * string, profile_t * profile)
163 struct timeval * ts = &(profile->ts);
164 struct timeval * in = &(profile->in);
167 if (strcmp (string, profile->about)) {
168 printf ("print_profile string %s about %s\n", string, profile->about);
172 //RFS_ASSERT (!strcmp (string, profile->about));
173 if (in->tv_usec<0 || in->tv_usec>1000000) {
174 printf ("%s in->tv_usec %d, in->tv_sec %d num %d\n", profile->about, in->tv_usec, in->tv_sec, profile->num);
177 RFS_ASSERT (in->tv_usec>=0 && in->tv_usec<1000000);
179 if (!(in->tv_sec >=0 && in->tv_sec <864000)) {
180 printf ("%s ts.tv_sec %d ts.tv_usec %d\n", profile->about, ts->tv_sec, ts->tv_usec);
181 printf ("%s in.tv_sec %d in.tv_usec %d\n", profile->about, in->tv_sec, in->tv_usec);
183 RFS_ASSERT (in->tv_sec >=0 && in->tv_sec <864000); /* it's about 10 days */
185 if (profile->num == 0) {
186 printf("... %40s %3d.%06d num %d \n", profile->about, in->tv_sec, in->tv_usec,
190 fprintf(profile_fp, "... %40s %3d.%06d num %d \n", profile->about, in->tv_sec,
191 in->tv_usec, profile->num );
192 //perror("print_profile_1");
196 int avg = calculate_avg_timeval (in, profile->num);
197 printf("... %40s %3d.%06d num %d avg %d \n", profile->about, in->tv_sec, in->tv_usec,
201 fprintf(profile_fp, "... %40s %3d.%06d num %d avg %d \n", profile->about, in->tv_sec,
202 in->tv_usec, profile->num, avg);
206 printf("... %40s %3d.%06d num %d avg %d \n", string, in->tv_sec, in->tv_usec,
207 profile->num, (in->tv_sec*1000000+in->tv_usec)/profile->num );
210 fprintf(profile_fp, "... %40s %3d.%06d num %d avg %d \n", string, in->tv_sec,
211 in->tv_usec, profile->num, (in->tv_sec*1000000+in->tv_usec)/profile->num );