xref: /freebsd/contrib/tcsh/sh.time.c (revision 1e413cf93298b5b97441a21d9a50fdcd0ee9945e)
1 /* $Header: /p/tcsh/cvsroot/tcsh/sh.time.c,v 3.33 2006/03/02 18:46:44 christos Exp $ */
2 /*
3  * sh.time.c: Shell time keeping and printing.
4  */
5 /*-
6  * Copyright (c) 1980, 1991 The	Regents	of the University of California.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and	binary forms, with or without
10  * modification, are permitted provided	that the following conditions
11  * are met:
12  * 1. Redistributions of source	code must retain the above copyright
13  *    notice, this list	of conditions and the following	disclaimer.
14  * 2. Redistributions in binary	form must reproduce the	above copyright
15  *    notice, this list	of conditions and the following	disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or	promote	products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS	SOFTWARE IS PROVIDED BY	THE REGENTS AND	CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT	SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR	CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES;	LOSS OF	USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH	DAMAGE.
32  */
33 #include "sh.h"
34 
35 RCSID("$tcsh: sh.time.c,v 3.33 2006/03/02 18:46:44 christos Exp $")
36 
37 #ifdef SUNOS4
38 # include <machine/param.h>
39 #endif /* SUNOS4 */
40 
41 /*
42  * C Shell - routines handling process timing and niceing
43  */
44 #ifdef BSDTIMES
45 # ifndef RUSAGE_SELF
46 #  define	RUSAGE_SELF	0
47 #  define	RUSAGE_CHILDREN	-1
48 # endif	/* RUSAGE_SELF */
49 #else /* BSDTIMES */
50 struct tms times0;
51 #endif /* BSDTIMES */
52 
53 #if !defined(BSDTIMES) && !defined(_SEQUENT_)
54 # ifdef	POSIX
55 static	void	pdtimet	(clock_t, clock_t);
56 # else /* ! POSIX */
57 static	void	pdtimet	(time_t, time_t);
58 # endif	/* ! POSIX */
59 #else /* BSDTIMES || _SEQUENT_ */
60 static	void	tvadd	(timeval_t *, timeval_t *);
61 static	void	pdeltat	(timeval_t *, timeval_t *);
62 #endif /* BSDTIMES || _SEQUENT_	*/
63 
64 void
65 settimes(void)
66 {
67 #ifdef BSDTIMES
68     struct sysrusage ruch;
69 #ifdef convex
70     memset(&ru0, 0, sizeof(ru0));
71     memset(&ruch, 0, sizeof(ruch));
72 #endif /* convex */
73 
74     (void) gettimeofday(&time0,	NULL);
75     (void) getrusage(RUSAGE_SELF, (struct rusage *) &ru0);
76     (void) getrusage(RUSAGE_CHILDREN, (struct rusage *) &ruch);
77     ruadd(&ru0,	&ruch);
78 #else
79 # ifdef	_SEQUENT_
80     struct process_stats ruch;
81 
82     (void) get_process_stats(&time0, PS_SELF, &ru0, &ruch);
83     ruadd(&ru0,	&ruch);
84 # else	/* _SEQUENT_ */
85     seconds0 = time(NULL);
86     time0 = times(&times0);
87     times0.tms_stime +=	times0.tms_cstime;
88     times0.tms_utime +=	times0.tms_cutime;
89     times0.tms_cstime =	0;
90     times0.tms_cutime =	0;
91 # endif	/* _SEQUENT_ */
92 #endif /* BSDTIMES */
93 }
94 
95 /*
96  * dotime is only called if it is truly	a builtin function and not a
97  * prefix to another command
98  */
99 /*ARGSUSED*/
100 void
101 dotime(Char **v, struct command *c)
102 {
103 #ifdef BSDTIMES
104     timeval_t timedol;
105     struct sysrusage ru1, ruch;
106 #ifdef convex
107     memset(&ru1, 0, sizeof(ru1));
108     memset(&ruch, 0, sizeof(ruch));
109 #endif /* convex */
110 
111     (void) getrusage(RUSAGE_SELF, (struct rusage *) &ru1);
112     (void) getrusage(RUSAGE_CHILDREN, (struct rusage *) &ruch);
113     ruadd(&ru1,	&ruch);
114     (void) gettimeofday(&timedol, NULL);
115     prusage(&ru0, &ru1,	&timedol, &time0);
116 #else
117 # ifdef	_SEQUENT_
118     timeval_t timedol;
119     struct process_stats ru1, ruch;
120 
121     (void) get_process_stats(&timedol, PS_SELF,	&ru1, &ruch);
122     ruadd(&ru1,	&ruch);
123     prusage(&ru0, &ru1,	&timedol, &time0);
124 # else /* _SEQUENT_ */
125 #  ifndef POSIX
126     time_t  timedol;
127 #  else	/* POSIX */
128     clock_t timedol;
129 #  endif /* POSIX */
130 
131     struct tms times_dol;
132 
133     timedol = times(&times_dol);
134     times_dol.tms_stime	+= times_dol.tms_cstime;
135     times_dol.tms_utime	+= times_dol.tms_cutime;
136     times_dol.tms_cstime = 0;
137     times_dol.tms_cutime = 0;
138     prusage(&times0, &times_dol, timedol, time0);
139 # endif	/* _SEQUENT_ */
140 #endif /* BSDTIMES */
141     USE(c);
142     USE(v);
143 }
144 
145 /*
146  * donice is only called when it on the	line by	itself or with a +- value
147  */
148 /*ARGSUSED*/
149 void
150 donice(Char **v, struct command *c)
151 {
152     Char *cp;
153     int	    nval = 0;
154 
155     USE(c);
156     v++, cp = *v++;
157     if (cp == 0)
158 	nval = 4;
159     else if (*v	== 0 &&	any("+-", cp[0]))
160 	nval = getn(cp);
161 #ifdef HAVE_SETPRIORITY
162     if (setpriority(PRIO_PROCESS, 0, nval) == -1 && errno)
163 	stderror(ERR_SYSTEM, "setpriority", strerror(errno));
164 #else /* !HAVE_SETPRIORITY */
165     (void) nice(nval);
166 #endif /* HAVE_SETPRIORITY */
167 }
168 
169 #ifdef BSDTIMES
170 void
171 ruadd(struct sysrusage *ru, struct sysrusage *ru2)
172 {
173     tvadd(&ru->ru_utime, &ru2->ru_utime);
174     tvadd(&ru->ru_stime, &ru2->ru_stime);
175 #ifndef _OSD_POSIX
176     if (ru2->ru_maxrss > ru->ru_maxrss)
177 	ru->ru_maxrss =	ru2->ru_maxrss;
178 
179     ru->ru_ixrss += ru2->ru_ixrss;
180     ru->ru_idrss += ru2->ru_idrss;
181     ru->ru_isrss += ru2->ru_isrss;
182     ru->ru_minflt += ru2->ru_minflt;
183     ru->ru_majflt += ru2->ru_majflt;
184     ru->ru_nswap += ru2->ru_nswap;
185     ru->ru_inblock += ru2->ru_inblock;
186     ru->ru_oublock += ru2->ru_oublock;
187     ru->ru_msgsnd += ru2->ru_msgsnd;
188     ru->ru_msgrcv += ru2->ru_msgrcv;
189     ru->ru_nsignals += ru2->ru_nsignals;
190     ru->ru_nvcsw += ru2->ru_nvcsw;
191     ru->ru_nivcsw += ru2->ru_nivcsw;
192 #endif /*bs2000*/
193 
194 # ifdef	convex
195     tvadd(&ru->ru_exutime, &ru2->ru_exutime);
196     ru->ru_utotal += ru2->ru_utotal;
197     ru->ru_usamples += ru2->ru_usamples;
198     ru->ru_stotal += ru2->ru_stotal;
199     ru->ru_ssamples += ru2->ru_ssamples;
200 # endif	/* convex */
201 }
202 
203 #else /* BSDTIMES */
204 # ifdef	_SEQUENT_
205 void
206 ruadd(struct process_stats *ru, struct process_stats *ru2)
207 {
208     tvadd(&ru->ps_utime, &ru2->ps_utime);
209     tvadd(&ru->ps_stime, &ru2->ps_stime);
210     if (ru2->ps_maxrss > ru->ps_maxrss)
211 	ru->ps_maxrss =	ru2->ps_maxrss;
212 
213     ru->ps_pagein += ru2->ps_pagein;
214     ru->ps_reclaim += ru2->ps_reclaim;
215     ru->ps_zerofill += ru2->ps_zerofill;
216     ru->ps_pffincr += ru2->ps_pffincr;
217     ru->ps_pffdecr += ru2->ps_pffdecr;
218     ru->ps_swap	+= ru2->ps_swap;
219     ru->ps_syscall += ru2->ps_syscall;
220     ru->ps_volcsw += ru2->ps_volcsw;
221     ru->ps_involcsw += ru2->ps_involcsw;
222     ru->ps_signal += ru2->ps_signal;
223     ru->ps_lread += ru2->ps_lread;
224     ru->ps_lwrite += ru2->ps_lwrite;
225     ru->ps_bread += ru2->ps_bread;
226     ru->ps_bwrite += ru2->ps_bwrite;
227     ru->ps_phread += ru2->ps_phread;
228     ru->ps_phwrite += ru2->ps_phwrite;
229 }
230 
231 # endif	/* _SEQUENT_ */
232 #endif /* BSDTIMES */
233 
234 #ifdef BSDTIMES
235 
236 /*
237  * PWP:	the LOG1024 and	pagetok	stuff taken from the top command,
238  * written by William LeFebvre
239  */
240 /* Log base 2 of 1024 is 10 (2^10 == 1024) */
241 #define	LOG1024		10
242 
243 /* Convert clicks (kernel pages) to kbytes ... */
244 /* If there is no PGSHIFT defined, assume it is	11 */
245 /* Is this needed for compatability with some old flavor of 4.2	or 4.1?	*/
246 #ifdef SUNOS4
247 # ifndef PGSHIFT
248 #  define pagetok(size)	  ((size) << 1)
249 # else
250 #  if PGSHIFT>10
251 #   define pagetok(size)   ((size) << (PGSHIFT - LOG1024))
252 #  else
253 #   define pagetok(size)   ((size) >> (LOG1024 - PGSHIFT))
254 #  endif
255 # endif
256 #endif
257 
258 /*
259  * if any other	machines return	wierd values in	the ru_i* stuff, put
260  * the adjusting macro here:
261  */
262 #ifdef SUNOS4
263 # define IADJUST(i)	(pagetok(i)/2)
264 #else /* SUNOS4	*/
265 # ifdef	convex
266    /*
267     * convex has megabytes * CLK_TCK
268     * multiply by 100 since we use time	in 100ths of a second in prusage
269     */
270 #  define IADJUST(i) (((i) << 10) / CLK_TCK * 100)
271 # else /* convex */
272 #  define IADJUST(i)	(i)
273 # endif	/* convex */
274 #endif /* SUNOS4 */
275 
276 void
277 prusage(struct sysrusage *r0, struct sysrusage *r1, timeval_t *e, timeval_t *b)
278 
279 #else /* BSDTIMES */
280 # ifdef	_SEQUENT_
281 void
282 prusage(struct process_stats *r0, struct process_stats *r1, timeval_t e,
283 	timeval_t b)
284 
285 # else /* _SEQUENT_ */
286 #  ifndef POSIX
287 void
288 prusage(struct tms *bs, struct tms *es, time_t e, time_t b)
289 #  else	/* POSIX */
290 void
291 prusage(struct tms *bs, struct tms *es, clock_t e, clock_t b)
292 #  endif /* POSIX */
293 # endif	/* _SEQUENT_ */
294 #endif /* BSDTIMES */
295 {
296 #ifdef BSDTIMES
297     time_t t =
298     (r1->ru_utime.tv_sec - r0->ru_utime.tv_sec)	* 100 +
299     (r1->ru_utime.tv_usec - r0->ru_utime.tv_usec) / 10000 +
300     (r1->ru_stime.tv_sec - r0->ru_stime.tv_sec)	* 100 +
301     (r1->ru_stime.tv_usec - r0->ru_stime.tv_usec) / 10000;
302 
303 #else
304 # ifdef	_SEQUENT_
305     time_t t =
306     (r1->ps_utime.tv_sec - r0->ps_utime.tv_sec)	* 100 +
307     (r1->ps_utime.tv_usec - r0->ps_utime.tv_usec) / 10000 +
308     (r1->ps_stime.tv_sec - r0->ps_stime.tv_sec)	* 100 +
309     (r1->ps_stime.tv_usec - r0->ps_stime.tv_usec) / 10000;
310 
311 # else /* _SEQUENT_ */
312 #  ifndef POSIX
313     time_t t =	(es->tms_utime - bs->tms_utime +
314 			 es->tms_stime - bs->tms_stime)	* 100 /	HZ;
315 
316 #  else	/* POSIX */
317     clock_t t = (es->tms_utime	- bs->tms_utime	+
318 			  es->tms_stime	- bs->tms_stime) * 100 / clk_tck;
319 
320 #  endif /* POSIX */
321 # endif	/* _SEQUENT_ */
322 #endif /* BSDTIMES */
323 
324     const char *cp;
325     long i;
326     struct varent *vp = adrof(STRtime);
327 
328 #ifdef BSDTIMES
329 # ifdef	convex
330     static struct system_information sysinfo;
331     long long memtmp;	/* let memory calculations exceede 2Gb */
332 # endif	/* convex */
333     int	    ms = (int)
334     ((e->tv_sec	- b->tv_sec) * 100 + (e->tv_usec - b->tv_usec) / 10000);
335 
336     cp = "%Uu %Ss %E %P	%X+%Dk %I+%Oio %Fpf+%Ww";
337 #else /* !BSDTIMES */
338 # ifdef	_SEQUENT_
339     int	    ms = (int)
340     ((e->tv_sec	- b->tv_sec) * 100 + (e->tv_usec - b->tv_usec) / 10000);
341 
342     cp = "%Uu %Ss %E %P	%I+%Oio	%Fpf+%Ww";
343 # else /* !_SEQUENT_ */
344 #  ifndef POSIX
345     time_t ms = ((time_t)((e - b) / HZ) * 100) +
346 		 (time_t)(((e - b) % HZ) * 100) / HZ;
347 #  else	/* POSIX */
348     clock_t ms = ((clock_t)((e - b) / clk_tck) * 100) +
349 		  (clock_t)(((e - b) % clk_tck) * 100) / clk_tck;
350 #  endif /* POSIX */
351 
352     cp = "%Uu %Ss %E %P";
353 
354     /*
355      * the tms stuff is	not very precise, so we	fudge it.
356      * granularity fix:	can't be more than 100%
357      * this breaks in multi-processor systems...
358      * maybe I should take it out and let people see more then 100%
359      * utilizations.
360      */
361 #  if 0
362     if (ms < t && ms !=	0)
363 	ms = t;
364 #  endif
365 # endif	/*! _SEQUENT_ */
366 #endif /* !BSDTIMES */
367 #ifdef TDEBUG
368     xprintf("es->tms_utime %lu bs->tms_utime %lu\n",
369 	    es->tms_utime, bs->tms_utime);
370     xprintf("es->tms_stime %lu bs->tms_stime %lu\n",
371 	    es->tms_stime, bs->tms_stime);
372     xprintf("ms	%lu e %lu b %lu\n", ms,	e, b);
373     xprintf("t %lu\n", t);
374 #endif /* TDEBUG */
375 
376     if (vp && vp->vec && vp->vec[0] && vp->vec[1])
377 	cp = short2str(vp->vec[1]);
378     for	(; *cp;	cp++)
379 	if (*cp	!= '%')
380 	    xputchar(*cp);
381 	else if	(cp[1])
382 	    switch (*++cp) {
383 
384 	    case 'U':		/* user	CPU time used */
385 #ifdef BSDTIMES
386 		pdeltat(&r1->ru_utime, &r0->ru_utime);
387 #else
388 # ifdef	_SEQUENT_
389 		pdeltat(&r1->ps_utime, &r0->ps_utime);
390 # else /* _SEQUENT_ */
391 #  ifndef POSIX
392 		pdtimet(es->tms_utime, bs->tms_utime);
393 #  else	/* POSIX */
394 		pdtimet(es->tms_utime, bs->tms_utime);
395 #  endif /* POSIX */
396 # endif	/* _SEQUENT_ */
397 #endif /* BSDTIMES */
398 		break;
399 
400 	    case 'S':		/* system CPU time used	*/
401 #ifdef BSDTIMES
402 		pdeltat(&r1->ru_stime, &r0->ru_stime);
403 #else
404 # ifdef	_SEQUENT_
405 		pdeltat(&r1->ps_stime, &r0->ps_stime);
406 # else /* _SEQUENT_ */
407 #  ifndef POSIX
408 		pdtimet(es->tms_stime, bs->tms_stime);
409 #  else	/* POSIX */
410 		pdtimet(es->tms_stime, bs->tms_stime);
411 #  endif /* POSIX */
412 # endif	/* _SEQUENT_ */
413 #endif /* BSDTIMES */
414 		break;
415 
416 	    case 'E':		/* elapsed (wall-clock)	time */
417 #ifdef BSDTIMES
418 		pcsecs((long) ms);
419 #else /* BSDTIMES */
420 		pcsecs(ms);
421 #endif /* BSDTIMES */
422 		break;
423 
424 	    case 'P':		/* percent time	spent running */
425 		/* check if the	process	did not	run */
426 #ifdef convex
427 		/*
428 		 * scale the cpu %- ages by the	number of processors
429 		 * available on	this machine
430 		 */
431 		if ((sysinfo.cpu_count == 0) &&
432 		    (getsysinfo(SYSINFO_SIZE, &sysinfo)	< 0))
433 		    sysinfo.cpu_count =	1;
434 		    i =	(ms == 0) ? 0 :	(t * 1000.0 / (ms * sysinfo.cpu_count));
435 #else /* convex	*/
436 		i = (ms	== 0) ?	0 : (long)(t * 1000.0 / ms);
437 #endif /* convex */
438 		xprintf("%ld.%01ld%%", i / 10, i % 10);	/* nn.n% */
439 		break;
440 
441 #ifdef BSDTIMES
442 	    case 'W':		/* number of swaps */
443 #ifdef _OSD_POSIX
444 		i = 0;
445 #else
446 		i = r1->ru_nswap - r0->ru_nswap;
447 #endif
448 		xprintf("%ld", i);
449 		break;
450 
451 #ifdef convex
452 	    case 'X':		/* (average) shared text size */
453 		memtmp = (t == 0 ? 0LL : IADJUST((long long)r1->ru_ixrss -
454 				 (long long)r0->ru_ixrss) /
455 			 (long long)t);
456 		xprintf("%lu", (unsigned long)memtmp);
457 		break;
458 
459 	    case 'D':		/* (average) unshared data size	*/
460 		memtmp = (t == 0 ? 0LL : IADJUST((long long)r1->ru_idrss +
461 				 (long long)r1->ru_isrss -
462 				 ((long	long)r0->ru_idrss +
463 				  (long	long)r0->ru_isrss)) /
464 			 (long long)t);
465 		xprintf("%lu", (unsigned long)memtmp);
466 		break;
467 
468 	    case 'K':		/* (average) total data	memory used  */
469 		memtmp = (t == 0 ? 0LL : IADJUST(((long	long)r1->ru_ixrss +
470 				  (long	long)r1->ru_isrss +
471 				  (long	long)r1->ru_idrss) -
472 				  ((long long)r0->ru_ixrss +
473 				   (long long)r0->ru_idrss +
474 				   (long long)r0->ru_isrss)) /
475 			 (long long)t);
476 		xprintf("%lu", (unsigned long)memtmp);
477 		break;
478 #else /* !convex */
479 	    case 'X':		/* (average) shared text size */
480 #ifdef _OSD_POSIX
481 		xprintf("0",0);
482 #else
483 		xprintf("%ld", t == 0 ?	0L :
484 			IADJUST(r1->ru_ixrss - r0->ru_ixrss) / t);
485 #endif
486 		break;
487 
488 	    case 'D':		/* (average) unshared data size	*/
489 #ifdef _OSD_POSIX
490 		xprintf("0",0);
491 #else
492 		xprintf("%ld", t == 0 ?	0L :
493 			IADJUST(r1->ru_idrss + r1->ru_isrss -
494 				(r0->ru_idrss +	r0->ru_isrss)) / t);
495 #endif
496 		break;
497 
498 	    case 'K':		/* (average) total data	memory used  */
499 #ifdef _OSD_POSIX
500 		xprintf("0",0);
501 #else
502 		xprintf("%ld", t == 0 ?	0L :
503 			IADJUST((r1->ru_ixrss +	r1->ru_isrss + r1->ru_idrss) -
504 			   (r0->ru_ixrss + r0->ru_idrss	+ r0->ru_isrss)) / t);
505 #endif
506 		break;
507 #endif /* convex */
508 	    case 'M':		/* max.	Resident Set Size */
509 #ifdef SUNOS4
510 		xprintf("%ld", pagetok(r1->ru_maxrss));
511 #else
512 # ifdef	convex
513 		xprintf("%ld", r1->ru_maxrss * 4L);
514 # else /* !convex */
515 #  ifdef _OSD_POSIX
516 		xprintf("0",0);
517 #  else
518 		xprintf("%ld", r1->ru_maxrss / 2L);
519 #  endif
520 # endif	/* convex */
521 #endif /* SUNOS4 */
522 		break;
523 
524 	    case 'F':		/* page	faults */
525 #ifdef _OSD_POSIX
526 		xprintf("0",0);
527 #else
528 		xprintf("%ld", r1->ru_majflt - r0->ru_majflt);
529 #endif
530 		break;
531 
532 	    case 'R':		/* page	reclaims */
533 #ifdef _OSD_POSIX
534 		xprintf("0",0);
535 #else
536 		xprintf("%ld", r1->ru_minflt - r0->ru_minflt);
537 #endif
538 		break;
539 
540 	    case 'I':		/* FS blocks in	*/
541 #ifdef _OSD_POSIX
542 		xprintf("0",0);
543 #else
544 		xprintf("%ld", r1->ru_inblock -	r0->ru_inblock);
545 #endif
546 		break;
547 
548 	    case 'O':		/* FS blocks out */
549 #ifdef _OSD_POSIX
550 		xprintf("0",0);
551 #else
552 		xprintf("%ld", r1->ru_oublock -	r0->ru_oublock);
553 #endif
554 		break;
555 
556 # ifdef	convex
557 	    case 'C':			/*  CPU	parallelization	factor */
558 		if (r1->ru_usamples	!= 0LL)	{
559 		    long long parr = ((r1->ru_utotal * 100LL) /
560 				      r1->ru_usamples);
561 		    xprintf("%d.%02d", (int)(parr/100), (int)(parr%100));
562 		} else
563 		    xprintf("?");
564 		break;
565 # endif	/* convex */
566 	    case 'r':		/* PWP:	socket messages	recieved */
567 #ifdef _OSD_POSIX
568 		xprintf("0",0);
569 #else
570 		xprintf("%ld", r1->ru_msgrcv - r0->ru_msgrcv);
571 #endif
572 		break;
573 
574 	    case 's':		/* PWP:	socket messages	sent */
575 #ifdef _OSD_POSIX
576 		xprintf("0",0);
577 #else
578 		xprintf("%ld", r1->ru_msgsnd - r0->ru_msgsnd);
579 #endif
580 		break;
581 
582 	    case 'k':		/* PWP:	signals	received */
583 #ifdef _OSD_POSIX
584 		xprintf("0",0);
585 #else
586 		xprintf("%ld", r1->ru_nsignals - r0->ru_nsignals);
587 #endif
588 		break;
589 
590 	    case 'w':		/* PWP:	voluntary context switches (waits) */
591 #ifdef _OSD_POSIX
592 		xprintf("0",0);
593 #else
594 		xprintf("%ld", r1->ru_nvcsw - r0->ru_nvcsw);
595 #endif
596 		break;
597 
598 	    case 'c':		/* PWP:	involuntary context switches */
599 #ifdef _OSD_POSIX
600 		xprintf("0",0);
601 #else
602 		xprintf("%ld", r1->ru_nivcsw - r0->ru_nivcsw);
603 #endif
604 		break;
605 #else /* BSDTIMES */
606 # ifdef	_SEQUENT_
607 	    case 'W':		/* number of swaps */
608 		i = r1->ps_swap	- r0->ps_swap;
609 		xprintf("%ld", i);
610 		break;
611 
612 	    case 'M':
613 		xprintf("%ld", r1->ps_maxrss / 2);
614 		break;
615 
616 	    case 'F':
617 		xprintf("%ld", r1->ps_pagein - r0->ps_pagein);
618 		break;
619 
620 	    case 'R':
621 		xprintf("%ld", r1->ps_reclaim -	r0->ps_reclaim);
622 		break;
623 
624 	    case 'I':
625 		xprintf("%ld", r1->ps_bread - r0->ps_bread);
626 		break;
627 
628 	    case 'O':
629 		xprintf("%ld", r1->ps_bwrite - r0->ps_bwrite);
630 		break;
631 
632 	    case 'k':
633 		xprintf("%ld", r1->ps_signal - r0->ps_signal);
634 		break;
635 
636 	    case 'w':
637 		xprintf("%ld", r1->ps_volcsw - r0->ps_volcsw);
638 		break;
639 
640 	    case 'c':
641 		xprintf("%ld", r1->ps_involcsw - r0->ps_involcsw);
642 		break;
643 
644 	    case 'Z':
645 		xprintf("%ld", r1->ps_zerofill - r0->ps_zerofill);
646 		break;
647 
648 	    case 'i':
649 		xprintf("%ld", r1->ps_pffincr -	r0->ps_pffincr);
650 		break;
651 
652 	    case 'd':
653 		xprintf("%ld", r1->ps_pffdecr -	r0->ps_pffdecr);
654 		break;
655 
656 	    case 'Y':
657 		xprintf("%ld", r1->ps_syscall -	r0->ps_syscall);
658 		break;
659 
660 	    case 'l':
661 		xprintf("%ld", r1->ps_lread - r0->ps_lread);
662 		break;
663 
664 	    case 'm':
665 		xprintf("%ld", r1->ps_lwrite - r0->ps_lwrite);
666 		break;
667 
668 	    case 'p':
669 		xprintf("%ld", r1->ps_phread - r0->ps_phread);
670 		break;
671 
672 	    case 'q':
673 		xprintf("%ld", r1->ps_phwrite -	r0->ps_phwrite);
674 		break;
675 # endif	/* _SEQUENT_ */
676 #endif /* BSDTIMES */
677 	    default:
678 		break;
679 	    }
680     xputchar('\n');
681 }
682 
683 #if defined(BSDTIMES) || defined(_SEQUENT_)
684 static void
685 pdeltat(timeval_t *t1, timeval_t *t0)
686 {
687     timeval_t td;
688 
689     tvsub(&td, t1, t0);
690     xprintf("%ld.%03ld", td.tv_sec, td.tv_usec / 1000L);
691 }
692 
693 static void
694 tvadd(timeval_t *tsum, timeval_t *t0)
695 {
696 
697     tsum->tv_sec += t0->tv_sec;
698     tsum->tv_usec += t0->tv_usec;
699     if (tsum->tv_usec >= 1000000)
700 	tsum->tv_sec++,	tsum->tv_usec -= 1000000;
701 }
702 
703 void
704 tvsub(timeval_t *tdiff, timeval_t *t1, timeval_t *t0)
705 {
706 
707     tdiff->tv_sec = t1->tv_sec - t0->tv_sec;
708     tdiff->tv_usec = t1->tv_usec - t0->tv_usec;
709     if (tdiff->tv_usec < 0)
710 	tdiff->tv_sec--, tdiff->tv_usec	+= 1000000;
711 }
712 
713 #else /* !BSDTIMES && !_SEQUENT_ */
714 static void
715 #ifndef	POSIX
716 pdtimet(time_t eval, time_t bval)
717 
718 #else /* POSIX */
719 pdtimet(clock_t eval, clock_t bval)
720 
721 #endif /* POSIX	*/
722 {
723 #ifndef	POSIX
724     time_t  val;
725 
726 #else /* POSIX */
727     clock_t val;
728 
729 #endif /* POSIX	*/
730 
731 #ifndef	POSIX
732     val	= (eval	- bval)	* 100 /	HZ;
733 #else /* POSIX */
734     val	= (eval	- bval)	* 100 /	clk_tck;
735 #endif /* POSIX	*/
736 
737     xprintf("%ld.%02ld", val / 100, val	- (val / 100 * 100));
738 }
739 #endif /* BSDTIMES || _SEQUENT_	*/
740