JPRED-2 Add alscript to the Git repository
[jpred.git] / sources / alscript / gjutil / gjtimes.c
1 #include <stdio.h>
2 #include <stdarg.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <ctype.h>
6 #include <errno.h>
7 #include <time.h>
8
9 #include <gjutil.h>
10 #include <gjtimes.h>
11
12 #ifndef CLK_TCK
13 #include <unistd.h>
14 #define CLK_TCK sysconf(_SC_CLK_TCK)
15 #endif
16
17
18 /* timing routines  */
19
20 struct tms *t_init = NULL;
21 struct tms *t_last = NULL;
22 struct tms *t_new = NULL;
23
24 clock_t init_times,last_times, new_times;
25
26 void GJ_init_times(void)
27 {
28
29   extern struct tms *t_init, *t_last, *t_new;
30   extern clock_t init_times, last_times, new_times;
31
32   if(t_init == NULL){
33     t_init = (struct tms *) GJmalloc(sizeof(struct tms));
34     t_new = (struct tms *) GJmalloc(sizeof(struct tms));
35     t_last = (struct tms *) GJmalloc(sizeof(struct tms));
36
37     init_times = times(t_init);
38     new_times = init_times;
39     last_times = init_times;
40   
41     copy_tms(t_last,t_init);
42     copy_tms(t_new,t_init);
43   }
44 }
45
46 void copy_tms(struct tms *l,struct tms *r)
47 {
48   l->tms_utime = r->tms_utime;
49   l->tms_stime = r->tms_stime;
50   l->tms_cutime = r->tms_cutime;
51   l->tms_cstime = r->tms_cstime;
52 }
53
54
55 void GJ_show_times(FILE *fp,int action)
56 {
57   /* show the time(s) since init_times was called
58      if action = 0, print the time since the last call to GJ_init_times
59                     or GJ_show_times.
60      if action = 1, print as in 0, but add the total time since the
61                     first call to GJ_init_times.
62         action = 2, print elapsed and total time +
63                     tms_utime, tms_stime, tms_cutime and tms_cstime
64   */
65
66   clock_t init_calling, new_calling, last_calling;
67   clock_t init_child, new_child, last_child;
68
69   extern struct tms *t_init, *t_last, *t_new;
70   extern clock_t init_times, last_times, new_times;
71
72   new_times = times(t_new);
73    
74   if(action == 0){
75     fprintf(fp,"Elapsed time: %*6.2f\n",
76             (float)(new_times-last_times)/CLK_TCK);
77   }
78
79   if(action == 1){
80     /* compressed output - shows summary of user + sys for parent and child */
81      init_calling = t_init->tms_utime + t_init->tms_stime;
82      init_child = t_init->tms_cutime + t_init->tms_cstime;
83
84      last_calling = t_last->tms_utime + t_last->tms_stime;
85      new_calling = t_new->tms_utime + t_new->tms_stime;
86
87      last_child = t_last->tms_cutime + t_last->tms_cstime;
88      new_child = t_new->tms_cutime + t_new->tms_cstime;
89
90
91
92      fprintf(fp,"CPU(P%7.1f%7.1f)(C%7.1f%7.1f)(T%7.1f%7.1f)(E%7.1f%7.1f)\n",
93              (float)(new_calling - last_calling)/CLK_TCK,
94              (float)(new_calling - init_calling)/CLK_TCK,
95              (float)(new_child - last_child)/CLK_TCK,
96              (float)(new_child - init_child)/CLK_TCK,
97              (float)(new_calling-last_calling+new_child-last_child)/CLK_TCK,
98              (float)(new_calling-init_calling+new_child-init_child)/CLK_TCK,
99              (float)(new_times-last_times)/CLK_TCK,
100              (float)(new_times-init_times)/CLK_TCK);
101   }
102
103
104   if(action == 2){
105     /* full output */
106      init_calling = t_init->tms_utime + t_init->tms_stime;
107      init_child = t_init->tms_cutime + t_init->tms_cstime;
108
109      last_calling = t_last->tms_utime + t_last->tms_stime;
110      new_calling = t_new->tms_utime + t_new->tms_stime;
111
112      last_child = t_last->tms_cutime + t_last->tms_cstime;
113      new_child = t_new->tms_cutime + t_new->tms_cstime;
114
115
116
117      fprintf(fp,"         %7s:%7s:%7s|%7s:%7s:%7s:%7s:%7s\n",
118              "user","sys","total","c user","c sys","ctotal","el/tcpu","cum");
119      fprintf(fp,"Master  :%7.1f:%7.1f:%7.1f|%7.1f:%7.1f:%7.1f|%7.1f:%7.1f\n",
120              (float)(t_new->tms_utime - t_last->tms_utime)/CLK_TCK,
121              (float)(t_new->tms_stime - t_last->tms_stime)/CLK_TCK,
122              (float)(new_calling - last_calling)/CLK_TCK,
123              (float)(t_new->tms_utime - t_init->tms_utime)/CLK_TCK,
124              (float)(t_new->tms_stime - t_init->tms_stime)/CLK_TCK,
125              (float)(new_calling - init_calling)/CLK_TCK,
126              (float)(new_times-last_times)/CLK_TCK,
127              (float)(new_times-init_times)/CLK_TCK);
128
129      fprintf(fp,"Children:%7.1f:%7.1f:%7.1f|%7.1f:%7.1f:%7.1f|%7.1f:%7.1f\n",
130              (float)(t_new->tms_cutime - t_last->tms_cutime)/CLK_TCK,
131              (float)(t_new->tms_cstime - t_last->tms_cstime)/CLK_TCK,
132              (float)(new_child - last_child)/CLK_TCK,
133              (float)(t_new->tms_cutime - t_init->tms_cutime)/CLK_TCK,
134              (float)(t_new->tms_cstime - t_init->tms_cstime)/CLK_TCK,
135              (float)(new_child - init_child)/CLK_TCK,
136              (float)(new_calling-last_calling+new_child-last_child)/CLK_TCK,
137              (float)(new_calling-init_calling+new_child-init_child)/CLK_TCK);
138   }
139   if(action == 3){
140     /* very compressed output - shows cumparent, cumchild + sum of the two  and elapsed time summary*/
141      init_calling = t_init->tms_utime + t_init->tms_stime;
142      init_child = t_init->tms_cutime + t_init->tms_cstime;
143
144      last_calling = t_last->tms_utime + t_last->tms_stime;
145      new_calling = t_new->tms_utime + t_new->tms_stime;
146
147      last_child = t_last->tms_cutime + t_last->tms_cstime;
148      new_child = t_new->tms_cutime + t_new->tms_cstime;
149
150      fprintf(fp,"Cum CPU Time: Parent: %*7.1f Child: %*7.1f Total: %*7.1f Elapsed: %7.1f\n",
151              (float)(new_calling - init_calling)/CLK_TCK,
152              (float)(new_child - init_child)/CLK_TCK,
153              (float)(new_calling-init_calling+new_child-init_child)/CLK_TCK,
154              (float)(new_times-init_times)/CLK_TCK);
155   }
156
157   copy_tms(t_last,t_new);
158   last_times = new_times;
159 }
160
161
162 char *GJ_get_times(int action)
163 {
164   /* show the time(s) since init_times was called
165      if action = 0, print the time since the last call to GJ_init_times
166                     or GJ_show_times.
167      if action = 1, print as in 0, but add the total time since the
168                     first call to GJ_init_times.
169
170   */
171
172   clock_t init_calling, new_calling, last_calling;
173   clock_t init_child, new_child, last_child;
174
175   char *outbuf;
176
177   extern struct tms *t_init, *t_last, *t_new;
178   extern clock_t init_times, last_times, new_times;
179
180   new_times = times(t_new);
181
182   outbuf = (char *) GJmalloc(sizeof(char) *200);
183    
184   if(action == 0){
185     sprintf(outbuf,"%7.1f\0",
186             (float)(new_times-last_times)/CLK_TCK);
187   }
188 #ifdef KEEP_THIS
189   if(action == 1){
190     /* compressed output - shows summary of user + sys for parent and child */
191      init_calling = t_init->tms_utime + t_init->tms_stime;
192      init_child = t_init->tms_cutime + t_init->tms_cstime;
193
194      last_calling = t_last->tms_utime + t_last->tms_stime;
195      new_calling = t_new->tms_utime + t_new->tms_stime;
196
197      last_child = t_last->tms_cutime + t_last->tms_cstime;
198      new_child = t_new->tms_cutime + t_new->tms_cstime;
199
200      sprintf(outbuf,"%7.1f %7.1f %7.1f %7.1f %7.1f %7.1f %7.1f %7.1f\0",
201              (float)(new_calling - last_calling)/CLK_TCK,
202              (float)(new_calling - init_calling)/CLK_TCK,
203              (float)(new_child - last_child)/CLK_TCK,
204              (float)(new_child - init_child)/CLK_TCK,
205              (float)(new_calling-last_calling+new_child-last_child)/CLK_TCK,
206              (float)(new_calling-init_calling+new_child-init_child)/CLK_TCK,
207              (float)(new_times-last_times)/CLK_TCK,
208              (float)(new_times-init_times)/CLK_TCK);
209   }
210 #else
211   /* this is a version that ignores the child processes since these 
212      appear not to be reported on the SGI */
213   if(action == 1){
214     /* compressed output - shows summary of user + sys for parent and child */
215      init_calling = t_init->tms_utime + t_init->tms_stime;
216      init_child = t_init->tms_cutime + t_init->tms_cstime;
217
218      last_calling = t_last->tms_utime + t_last->tms_stime;
219      new_calling = t_new->tms_utime + t_new->tms_stime;
220
221      last_child = t_last->tms_cutime + t_last->tms_cstime;
222      new_child = t_new->tms_cutime + t_new->tms_cstime;
223
224      sprintf(outbuf,"%7.1f %7.1f %7.1f %7.1f\0",
225              (float)(new_calling - last_calling)/CLK_TCK,
226              (float)(new_calling - init_calling)/CLK_TCK,
227              (float)(new_times-last_times)/CLK_TCK,
228              (float)(new_times-init_times)/CLK_TCK);
229   }
230 #endif
231
232   if(action == 2){
233     /* full output */
234      init_calling = t_init->tms_utime + t_init->tms_stime;
235      init_child = t_init->tms_cutime + t_init->tms_cstime;
236
237      last_calling = t_last->tms_utime + t_last->tms_stime;
238      new_calling = t_new->tms_utime + t_new->tms_stime;
239
240      last_child = t_last->tms_cutime + t_last->tms_cstime;
241      new_child = t_new->tms_cutime + t_new->tms_cstime;
242
243
244
245      sprintf(outbuf,
246      "%7.1f %7.1f %7.1f %7.1f %7.1f %7.1f %7.1f %7.1f %7.1f %7.1f %7.1f %7.1f %7.1f %7.1f %7.1f %7.1f",
247              (float)(t_new->tms_utime - t_last->tms_utime)/CLK_TCK,
248              (float)(t_new->tms_stime - t_last->tms_stime)/CLK_TCK,
249              (float)(new_calling - last_calling)/CLK_TCK,
250              (float)(t_new->tms_utime - t_init->tms_utime)/CLK_TCK,
251              (float)(t_new->tms_stime - t_init->tms_stime)/CLK_TCK,
252              (float)(new_calling - init_calling)/CLK_TCK,
253              (float)(new_times-last_times)/CLK_TCK,
254              (float)(new_times-init_times)/CLK_TCK,
255              (float)(t_new->tms_cutime - t_last->tms_cutime)/CLK_TCK,
256              (float)(t_new->tms_cstime - t_last->tms_cstime)/CLK_TCK,
257              (float)(new_child - last_child)/CLK_TCK,
258              (float)(t_new->tms_cutime - t_init->tms_cutime)/CLK_TCK,
259              (float)(t_new->tms_cstime - t_init->tms_cstime)/CLK_TCK,
260              (float)(new_child - init_child)/CLK_TCK,
261              (float)(new_calling-last_calling+new_child-last_child)/CLK_TCK,
262              (float)(new_calling-init_calling+new_child-init_child)/CLK_TCK);
263   }
264
265   if(action == 3){
266     /* very compressed output - shows cumparent, cumchild + sum of the two  and elapsed time summary*/
267      init_calling = t_init->tms_utime + t_init->tms_stime;
268      init_child = t_init->tms_cutime + t_init->tms_cstime;
269
270      last_calling = t_last->tms_utime + t_last->tms_stime;
271      new_calling = t_new->tms_utime + t_new->tms_stime;
272
273      last_child = t_last->tms_cutime + t_last->tms_cstime;
274      new_child = t_new->tms_cutime + t_new->tms_cstime;
275
276      sprintf(outbuf,"%7.1f %7.1f %7.1f %7.1f\0",
277              (float)(new_calling - init_calling)/CLK_TCK,
278              (float)(new_child - init_child)/CLK_TCK,
279              (float)(new_calling-init_calling+new_child-init_child)/CLK_TCK,
280              (float)(new_times-init_times)/CLK_TCK);
281   }
282
283   copy_tms(t_last,t_new);
284   last_times = new_times;
285   outbuf = (char *) GJrealloc(outbuf,sizeof(char) * (strlen(outbuf) + 1));
286   return outbuf;
287 }
288
289 void GJ_reset_times()
290 {
291     extern struct tms *t_init, *t_new, *t_last;
292
293     if(t_init != NULL){
294       GJfree(t_init);
295       t_init = NULL;
296     }
297     if(t_new != NULL){
298       GJfree(t_new);
299       t_new = NULL;
300     }
301     if(t_last != NULL){
302       GJfree(t_last);
303       t_last = NULL;
304     }
305 }
306 #define DELETE
307 #ifndef DELETE
308 main(int argc,char **argv)
309 {
310   int i,j;
311   float l;
312   GJ_init_times();
313   /*  fprintf(stderr,"(0)%s\n",GJ_get_times(0));*/
314   /*  fprintf(stderr,"(1)%s\n",GJ_get_times(1));*/
315   /*  fprintf(stderr,"(2)%s\n",GJ_get_times(2));*/
316   fprintf(stderr,"(3)%s\n",GJ_get_times(3));
317
318   for(i=0;i<5000;++i){
319     for(j=0;j<5000;++j){
320       l = i*j;
321     }
322   }
323   /*  fprintf(stderr,"(0)%s\n",GJ_get_times(0));*/
324   /*  fprintf(stderr,"(1)%s\n",GJ_get_times(1));*/
325   /*  fprintf(stderr,"(2)%s\n",GJ_get_times(2));*/
326   fprintf(stderr,"(3)%s\n",GJ_get_times(3));
327   for(i=0;i<10000;++i){
328     for(j=0;j<5000;++j){
329       l = i*j;
330     }
331   }
332   /*  fprintf(stderr,"(0)%s\n",GJ_get_times(0));*/
333   /*  fprintf(stderr,"(1)%s\n",GJ_get_times(1));*/
334   /*  fprintf(stderr,"(2)%s\n",GJ_get_times(2));*/
335   fprintf(stderr,"(3)%s\n",GJ_get_times(3));
336   GJ_reset_times();
337 }
338 #endif
339
340
341
342
343