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 ***
19 #include "rfs_assert.h"
20 //#include "rfs_c_def.h"
21 extern FILE * profile_fp;
23 static struct timezone tz;
24 inline void calculate_interval
25 (struct timeval * ts, struct timeval * te, struct timeval * interval)
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);
32 RFS_ASSERT (te->tv_sec > ts->tv_sec);
33 te->tv_usec += 1000000;
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);
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);
52 inline void normalize_profile (int pos, struct timeval * time)
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);
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;
64 inline void start_real_profile (profile_t * profile)
66 start_profile(profile);
69 inline void end_real_profile (profile_t * profile)
74 inline void start_profile (profile_t * profile)
77 if (strlen(profile->about) < 3) {
78 printf ("total_profile address: %x %x\n", &total_profile, profile);
82 gettimeofday(&(profile->ts), &tz);
83 normalize_profile (1, &(profile->ts));
86 inline void end_profile (profile_t * profile)
88 struct timeval te, teorg;
94 //printf ("end_profile %s\n", profile->about);
96 if (strlen(profile->about) < 3) {
97 printf ("total_profile address: %x %x\n", &total_profile, profile);
105 gettimeofday(&te, &tz);
106 normalize_profile (2, &te);
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);
116 RFS_ASSERT (te.tv_sec > ts->tv_sec);
117 te.tv_usec += 1000000;
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);
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);
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);
140 inline void init_profile (char * string, profile_t * profile)
142 RFS_ASSERT (strlen(string) < sizeof (profile->about));
143 memset (profile, 0, sizeof(profile_t));
144 strcpy (profile->about, string);
147 inline int calculate_avg_timeval (struct timeval * in, int num)
149 unsigned long long i;
152 if (in->tv_sec < 2000) {
153 return ((in->tv_sec*1000000+in->tv_usec)/num );
155 i = ((unsigned long long)in->tv_sec)*1000000 + in->tv_usec;
157 RFS_ASSERT (i<2000000000);
163 inline void print_profile (char * string, profile_t * profile)
165 struct timeval * ts = &(profile->ts);
166 struct timeval * in = &(profile->in);
169 if (strcmp (string, profile->about)) {
170 printf ("print_profile string %s about %s\n", string, profile->about);
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);
179 RFS_ASSERT (in->tv_usec>=0 && in->tv_usec<1000000);
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);
185 RFS_ASSERT (in->tv_sec >=0 && in->tv_sec <864000); /* it's about 10 days */
187 if (profile->num == 0) {
188 printf("... %40s %3d.%06d num %d \n", profile->about, in->tv_sec, in->tv_usec,
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");
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,
203 fprintf(profile_fp, "... %40s %3d.%06d num %d avg %d \n", profile->about, in->tv_sec,
204 in->tv_usec, profile->num, avg);
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 );
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 );