11 /* CLK_TCK is officially obsolete so may not be defined in time.h. If not,
15 #define CLK_TCK sysconf(_SC_CLK_TCK)
19 struct tms *t_init = NULL;
20 struct tms *t_last = NULL;
21 struct tms *t_new = NULL;
23 clock_t init_times,last_times, new_times;
25 void GJ_init_times(void)
28 extern struct tms *t_init, *t_last, *t_new;
29 extern clock_t init_times, last_times, new_times;
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));
36 init_times = times(t_init);
37 new_times = init_times;
38 last_times = init_times;
40 copy_tms(t_last,t_init);
41 copy_tms(t_new,t_init);
45 void copy_tms(struct tms *l,struct tms *r)
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;
54 void GJ_show_times(FILE *fp,int action)
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
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
65 clock_t init_calling, new_calling, last_calling;
66 clock_t init_child, new_child, last_child;
68 extern struct tms *t_init, *t_last, *t_new;
69 extern clock_t init_times, last_times, new_times;
71 new_times = times(t_new);
74 fprintf(fp,"Elapsed time: %6.2f\n",
75 (float)(new_times-last_times)/CLK_TCK);
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;
83 last_calling = t_last->tms_utime + t_last->tms_stime;
84 new_calling = t_new->tms_utime + t_new->tms_stime;
86 last_child = t_last->tms_cutime + t_last->tms_cstime;
87 new_child = t_new->tms_cutime + t_new->tms_cstime;
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);
105 init_calling = t_init->tms_utime + t_init->tms_stime;
106 init_child = t_init->tms_cutime + t_init->tms_cstime;
108 last_calling = t_last->tms_utime + t_last->tms_stime;
109 new_calling = t_new->tms_utime + t_new->tms_stime;
111 last_child = t_last->tms_cutime + t_last->tms_cstime;
112 new_child = t_new->tms_cutime + t_new->tms_cstime;
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);
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);
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;
143 last_calling = t_last->tms_utime + t_last->tms_stime;
144 new_calling = t_new->tms_utime + t_new->tms_stime;
146 last_child = t_last->tms_cutime + t_last->tms_cstime;
147 new_child = t_new->tms_cutime + t_new->tms_cstime;
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);
156 copy_tms(t_last,t_new);
157 last_times = new_times;
161 char *GJ_get_times(int action)
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
166 if action = 1, print as in 0, but add the total time since the
167 first call to GJ_init_times.
171 clock_t init_calling, new_calling, last_calling;
172 clock_t init_child, new_child, last_child;
176 extern struct tms *t_init, *t_last, *t_new;
177 extern clock_t init_times, last_times, new_times;
179 new_times = times(t_new);
181 outbuf = (char *) GJmalloc(sizeof(char) *200);
184 sprintf(outbuf,"%7.1f\0",
185 (float)(new_times-last_times)/CLK_TCK);
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;
193 last_calling = t_last->tms_utime + t_last->tms_stime;
194 new_calling = t_new->tms_utime + t_new->tms_stime;
196 last_child = t_last->tms_cutime + t_last->tms_cstime;
197 new_child = t_new->tms_cutime + t_new->tms_cstime;
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);
210 /* this is a version that ignores the child processes since these
211 appear not to be reported on the SGI */
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;
217 last_calling = t_last->tms_utime + t_last->tms_stime;
218 new_calling = t_new->tms_utime + t_new->tms_stime;
220 last_child = t_last->tms_cutime + t_last->tms_cstime;
221 new_child = t_new->tms_cutime + t_new->tms_cstime;
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);
233 init_calling = t_init->tms_utime + t_init->tms_stime;
234 init_child = t_init->tms_cutime + t_init->tms_cstime;
236 last_calling = t_last->tms_utime + t_last->tms_stime;
237 new_calling = t_new->tms_utime + t_new->tms_stime;
239 last_child = t_last->tms_cutime + t_last->tms_cstime;
240 new_child = t_new->tms_cutime + t_new->tms_cstime;
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);
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;
269 last_calling = t_last->tms_utime + t_last->tms_stime;
270 new_calling = t_new->tms_utime + t_new->tms_stime;
272 last_child = t_last->tms_cutime + t_last->tms_cstime;
273 new_child = t_new->tms_cutime + t_new->tms_cstime;
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);
282 copy_tms(t_last,t_new);
283 last_times = new_times;
284 outbuf = (char *) GJrealloc(outbuf,sizeof(char) * (strlen(outbuf) + 1));
288 void GJ_reset_times()
290 extern struct tms *t_init, *t_new, *t_last;
307 main(int argc,char **argv)
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));
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){
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));