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