14 #define CLK_TCK sysconf(_SC_CLK_TCK)
20 struct tms *t_init = NULL;
21 struct tms *t_last = NULL;
22 struct tms *t_new = NULL;
24 clock_t init_times,last_times, new_times;
26 void GJ_init_times(void)
29 extern struct tms *t_init, *t_last, *t_new;
30 extern clock_t init_times, last_times, new_times;
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));
37 init_times = times(t_init);
38 new_times = init_times;
39 last_times = init_times;
41 copy_tms(t_last,t_init);
42 copy_tms(t_new,t_init);
46 void copy_tms(struct tms *l,struct tms *r)
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;
55 void GJ_show_times(FILE *fp,int action)
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
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
66 clock_t init_calling, new_calling, last_calling;
67 clock_t init_child, new_child, last_child;
69 extern struct tms *t_init, *t_last, *t_new;
70 extern clock_t init_times, last_times, new_times;
72 new_times = times(t_new);
75 fprintf(fp,"Elapsed time: %*6.2f\n",
76 (float)(new_times-last_times)/CLK_TCK);
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;
84 last_calling = t_last->tms_utime + t_last->tms_stime;
85 new_calling = t_new->tms_utime + t_new->tms_stime;
87 last_child = t_last->tms_cutime + t_last->tms_cstime;
88 new_child = t_new->tms_cutime + t_new->tms_cstime;
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);
106 init_calling = t_init->tms_utime + t_init->tms_stime;
107 init_child = t_init->tms_cutime + t_init->tms_cstime;
109 last_calling = t_last->tms_utime + t_last->tms_stime;
110 new_calling = t_new->tms_utime + t_new->tms_stime;
112 last_child = t_last->tms_cutime + t_last->tms_cstime;
113 new_child = t_new->tms_cutime + t_new->tms_cstime;
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);
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);
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;
144 last_calling = t_last->tms_utime + t_last->tms_stime;
145 new_calling = t_new->tms_utime + t_new->tms_stime;
147 last_child = t_last->tms_cutime + t_last->tms_cstime;
148 new_child = t_new->tms_cutime + t_new->tms_cstime;
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);
157 copy_tms(t_last,t_new);
158 last_times = new_times;
162 char *GJ_get_times(int action)
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
167 if action = 1, print as in 0, but add the total time since the
168 first call to GJ_init_times.
172 clock_t init_calling, new_calling, last_calling;
173 clock_t init_child, new_child, last_child;
177 extern struct tms *t_init, *t_last, *t_new;
178 extern clock_t init_times, last_times, new_times;
180 new_times = times(t_new);
182 outbuf = (char *) GJmalloc(sizeof(char) *200);
185 sprintf(outbuf,"%7.1f\0",
186 (float)(new_times-last_times)/CLK_TCK);
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;
194 last_calling = t_last->tms_utime + t_last->tms_stime;
195 new_calling = t_new->tms_utime + t_new->tms_stime;
197 last_child = t_last->tms_cutime + t_last->tms_cstime;
198 new_child = t_new->tms_cutime + t_new->tms_cstime;
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);
211 /* this is a version that ignores the child processes since these
212 appear not to be reported on the SGI */
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;
218 last_calling = t_last->tms_utime + t_last->tms_stime;
219 new_calling = t_new->tms_utime + t_new->tms_stime;
221 last_child = t_last->tms_cutime + t_last->tms_cstime;
222 new_child = t_new->tms_cutime + t_new->tms_cstime;
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);
234 init_calling = t_init->tms_utime + t_init->tms_stime;
235 init_child = t_init->tms_cutime + t_init->tms_cstime;
237 last_calling = t_last->tms_utime + t_last->tms_stime;
238 new_calling = t_new->tms_utime + t_new->tms_stime;
240 last_child = t_last->tms_cutime + t_last->tms_cstime;
241 new_child = t_new->tms_cutime + t_new->tms_cstime;
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);
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;
270 last_calling = t_last->tms_utime + t_last->tms_stime;
271 new_calling = t_new->tms_utime + t_new->tms_stime;
273 last_child = t_last->tms_cutime + t_last->tms_cstime;
274 new_child = t_new->tms_cutime + t_new->tms_cstime;
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);
283 copy_tms(t_last,t_new);
284 last_times = new_times;
285 outbuf = (char *) GJrealloc(outbuf,sizeof(char) * (strlen(outbuf) + 1));
289 void GJ_reset_times()
291 extern struct tms *t_init, *t_new, *t_last;
308 main(int argc,char **argv)
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));
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){
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));