10Sstevel@tonic-gate /*
2*356Smuffin * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
30Sstevel@tonic-gate * Use is subject to license terms.
40Sstevel@tonic-gate */
50Sstevel@tonic-gate
60Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
70Sstevel@tonic-gate /* All Rights Reserved */
80Sstevel@tonic-gate
90Sstevel@tonic-gate /*
100Sstevel@tonic-gate * Copyright (c) 1980 Regents of the University of California.
110Sstevel@tonic-gate * All rights reserved. The Berkeley Software License Agreement
120Sstevel@tonic-gate * specifies the terms and conditions for redistribution.
130Sstevel@tonic-gate */
140Sstevel@tonic-gate
150Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
160Sstevel@tonic-gate
170Sstevel@tonic-gate #include "sh.h"
180Sstevel@tonic-gate #include "sh.tconst.h"
190Sstevel@tonic-gate
200Sstevel@tonic-gate void ruadd(struct rusage *ru, struct rusage *ru2);
210Sstevel@tonic-gate void prusage(struct rusage *r0, struct rusage *r1, struct timeval *e,
220Sstevel@tonic-gate struct timeval *b);
230Sstevel@tonic-gate void pdeltat(struct timeval *t1, struct timeval *t0);
240Sstevel@tonic-gate void tvadd(struct timeval *tsum, struct timeval *t0);
250Sstevel@tonic-gate void tvsub(struct timeval *tdiff, struct timeval *t1, struct timeval *t0);
260Sstevel@tonic-gate
270Sstevel@tonic-gate /*
280Sstevel@tonic-gate * C Shell - routines handling process timing and niceing
290Sstevel@tonic-gate */
300Sstevel@tonic-gate
310Sstevel@tonic-gate void
settimes(void)32*356Smuffin settimes(void)
330Sstevel@tonic-gate {
340Sstevel@tonic-gate struct rusage ruch;
350Sstevel@tonic-gate
360Sstevel@tonic-gate #ifdef TRACE
370Sstevel@tonic-gate tprintf("TRACE- settimes()\n");
380Sstevel@tonic-gate #endif
390Sstevel@tonic-gate (void) gettimeofday(&time0, (struct timezone *)0);
400Sstevel@tonic-gate (void) getrusage(RUSAGE_SELF, &ru0);
410Sstevel@tonic-gate (void) getrusage(RUSAGE_CHILDREN, &ruch);
420Sstevel@tonic-gate ruadd(&ru0, &ruch);
430Sstevel@tonic-gate }
440Sstevel@tonic-gate
450Sstevel@tonic-gate /*
460Sstevel@tonic-gate * dotime is only called if it is truly a builtin function and not a
470Sstevel@tonic-gate * prefix to another command
480Sstevel@tonic-gate */
490Sstevel@tonic-gate void
dotime(void)50*356Smuffin dotime(void)
510Sstevel@tonic-gate {
520Sstevel@tonic-gate struct timeval timedol;
530Sstevel@tonic-gate struct rusage ru1, ruch;
540Sstevel@tonic-gate
550Sstevel@tonic-gate #ifdef TRACE
560Sstevel@tonic-gate tprintf("TRACE- dotime()\n");
570Sstevel@tonic-gate #endif
580Sstevel@tonic-gate (void) getrusage(RUSAGE_SELF, &ru1);
590Sstevel@tonic-gate (void) getrusage(RUSAGE_CHILDREN, &ruch);
600Sstevel@tonic-gate ruadd(&ru1, &ruch);
610Sstevel@tonic-gate (void) gettimeofday(&timedol, (struct timezone *)0);
620Sstevel@tonic-gate prusage(&ru0, &ru1, &timedol, &time0);
630Sstevel@tonic-gate }
640Sstevel@tonic-gate
650Sstevel@tonic-gate /*
660Sstevel@tonic-gate * donice is only called when it's on the line by itself or with a +- value
670Sstevel@tonic-gate */
680Sstevel@tonic-gate void
donice(tchar ** v)690Sstevel@tonic-gate donice(tchar **v)
700Sstevel@tonic-gate {
710Sstevel@tonic-gate tchar *cp;
720Sstevel@tonic-gate int nval;
730Sstevel@tonic-gate
740Sstevel@tonic-gate #ifdef TRACE
750Sstevel@tonic-gate tprintf("TRACE- donice()\n");
760Sstevel@tonic-gate #endif
770Sstevel@tonic-gate v++;
780Sstevel@tonic-gate cp = *v++;
790Sstevel@tonic-gate if (cp == 0) {
800Sstevel@tonic-gate nval = 4;
810Sstevel@tonic-gate } else if (*v == 0 && (cp[0] == '+' || cp[0] == '-')) {
820Sstevel@tonic-gate nval = getn(cp);
830Sstevel@tonic-gate }
840Sstevel@tonic-gate (void) setpriority(PRIO_PROCESS, 0, nval);
850Sstevel@tonic-gate }
860Sstevel@tonic-gate
870Sstevel@tonic-gate void
ruadd(struct rusage * ru,struct rusage * ru2)880Sstevel@tonic-gate ruadd(struct rusage *ru, struct rusage *ru2)
890Sstevel@tonic-gate {
900Sstevel@tonic-gate long *lp, *lp2;
910Sstevel@tonic-gate int cnt;
920Sstevel@tonic-gate /*
930Sstevel@tonic-gate * The SunOS 4.x <sys/rusage.h> has ru_first and ru_last #defines
940Sstevel@tonic-gate * as below.
950Sstevel@tonic-gate * The SVR4/POSIX <sys/resource.h> does not have these defined for
960Sstevel@tonic-gate * struct rusage
970Sstevel@tonic-gate * The #defines below are here so that the original csh logic
980Sstevel@tonic-gate * for ruadd remains clear now that there is no longer a private copy
990Sstevel@tonic-gate * of the old <sys/resource.h>
1000Sstevel@tonic-gate */
1010Sstevel@tonic-gate #define ru_first ru_ixrss
1020Sstevel@tonic-gate #define ru_last ru_nivcsw
1030Sstevel@tonic-gate
1040Sstevel@tonic-gate #ifdef TRACE
1050Sstevel@tonic-gate tprintf("TRACE- ruadd()\n");
1060Sstevel@tonic-gate #endif
1070Sstevel@tonic-gate tvadd(&ru->ru_utime, &ru2->ru_utime);
1080Sstevel@tonic-gate tvadd(&ru->ru_stime, &ru2->ru_stime);
1090Sstevel@tonic-gate if (ru2->ru_maxrss > ru->ru_maxrss) {
1100Sstevel@tonic-gate ru->ru_maxrss = ru2->ru_maxrss;
1110Sstevel@tonic-gate }
1120Sstevel@tonic-gate cnt = &ru->ru_last - &ru->ru_first + 1;
1130Sstevel@tonic-gate lp = &ru->ru_first;
1140Sstevel@tonic-gate lp2 = &ru2->ru_first;
1150Sstevel@tonic-gate do {
1160Sstevel@tonic-gate *lp++ += *lp2++;
1170Sstevel@tonic-gate } while (--cnt > 0);
1180Sstevel@tonic-gate }
1190Sstevel@tonic-gate
1200Sstevel@tonic-gate void
prusage(struct rusage * r0,struct rusage * r1,struct timeval * e,struct timeval * b)1210Sstevel@tonic-gate prusage(struct rusage *r0, struct rusage *r1, struct timeval *e,
1220Sstevel@tonic-gate struct timeval *b)
1230Sstevel@tonic-gate {
1240Sstevel@tonic-gate #define pgtok(p) ((p * pgsize) / 1024)
1250Sstevel@tonic-gate static int pgsize;
1260Sstevel@tonic-gate
1270Sstevel@tonic-gate time_t t =
1280Sstevel@tonic-gate (r1->ru_utime.tv_sec - r0->ru_utime.tv_sec) * 100 +
1290Sstevel@tonic-gate (r1->ru_utime.tv_usec - r0->ru_utime.tv_usec) / 10000 +
1300Sstevel@tonic-gate (r1->ru_stime.tv_sec - r0->ru_stime.tv_sec) * 100 +
1310Sstevel@tonic-gate (r1->ru_stime.tv_usec - r0->ru_stime.tv_usec) / 10000;
1320Sstevel@tonic-gate tchar *cp;
1330Sstevel@tonic-gate int i;
1340Sstevel@tonic-gate struct varent *vp = adrof(S_time);
1350Sstevel@tonic-gate int ms =
1360Sstevel@tonic-gate (e->tv_sec - b->tv_sec) * 100 + (e->tv_usec - b->tv_usec) / 10000;
1370Sstevel@tonic-gate
1380Sstevel@tonic-gate #ifdef TRACE
1390Sstevel@tonic-gate tprintf("TRACE- prusage()\n");
1400Sstevel@tonic-gate #endif
1410Sstevel@tonic-gate if (pgsize == 0) {
1420Sstevel@tonic-gate pgsize = getpagesize();
1430Sstevel@tonic-gate }
1440Sstevel@tonic-gate
1450Sstevel@tonic-gate cp = S_USAGEFORMAT; /* "%Uu %Ss %E %P %X+%Dk %I+%Oio %Fpf+%Ww" */
1460Sstevel@tonic-gate if (vp && vp->vec[0] && vp->vec[1]) {
1470Sstevel@tonic-gate cp = vp->vec[1];
1480Sstevel@tonic-gate }
1490Sstevel@tonic-gate for (; *cp; cp++) {
1500Sstevel@tonic-gate if (*cp != '%') {
1510Sstevel@tonic-gate Putchar(*cp);
1520Sstevel@tonic-gate } else if (cp[1]) {
1530Sstevel@tonic-gate switch (*++cp) {
1540Sstevel@tonic-gate
1550Sstevel@tonic-gate case 'U':
1560Sstevel@tonic-gate pdeltat(&r1->ru_utime, &r0->ru_utime);
1570Sstevel@tonic-gate break;
1580Sstevel@tonic-gate
1590Sstevel@tonic-gate case 'S':
1600Sstevel@tonic-gate pdeltat(&r1->ru_stime, &r0->ru_stime);
1610Sstevel@tonic-gate break;
1620Sstevel@tonic-gate
1630Sstevel@tonic-gate case 'E':
1640Sstevel@tonic-gate psecs_int(ms / 100);
1650Sstevel@tonic-gate break;
1660Sstevel@tonic-gate
1670Sstevel@tonic-gate case 'P':
1680Sstevel@tonic-gate printf("%d%%", (int)(t * 100 /
1690Sstevel@tonic-gate ((ms ? ms : 1))));
1700Sstevel@tonic-gate break;
1710Sstevel@tonic-gate
1720Sstevel@tonic-gate case 'W':
1730Sstevel@tonic-gate i = r1->ru_nswap - r0->ru_nswap;
1740Sstevel@tonic-gate printf("%d", i);
1750Sstevel@tonic-gate break;
1760Sstevel@tonic-gate
1770Sstevel@tonic-gate case 'X':
1780Sstevel@tonic-gate printf("%d", t == 0 ? 0 :
1790Sstevel@tonic-gate pgtok((r1->ru_ixrss - r0->ru_ixrss) / t));
1800Sstevel@tonic-gate break;
1810Sstevel@tonic-gate
1820Sstevel@tonic-gate case 'D':
1830Sstevel@tonic-gate printf("%d", t == 0 ? 0 :
1840Sstevel@tonic-gate pgtok((r1->ru_idrss + r1->ru_isrss-
1850Sstevel@tonic-gate (r0->ru_idrss + r0->ru_isrss)) / t));
1860Sstevel@tonic-gate break;
1870Sstevel@tonic-gate
1880Sstevel@tonic-gate case 'K':
1890Sstevel@tonic-gate printf("%d", t == 0 ? 0 :
1900Sstevel@tonic-gate pgtok(((r1->ru_ixrss + r1->ru_isrss +
1910Sstevel@tonic-gate r1->ru_idrss) - (r0->ru_ixrss +
1920Sstevel@tonic-gate r0->ru_idrss + r0->ru_isrss)) / t));
1930Sstevel@tonic-gate break;
1940Sstevel@tonic-gate
1950Sstevel@tonic-gate case 'M':
1960Sstevel@tonic-gate printf("%d", r1->ru_maxrss / 2);
1970Sstevel@tonic-gate break;
1980Sstevel@tonic-gate
1990Sstevel@tonic-gate case 'F':
2000Sstevel@tonic-gate printf("%d", r1->ru_majflt - r0->ru_majflt);
2010Sstevel@tonic-gate break;
2020Sstevel@tonic-gate
2030Sstevel@tonic-gate case 'R':
2040Sstevel@tonic-gate printf("%d", r1->ru_minflt - r0->ru_minflt);
2050Sstevel@tonic-gate break;
2060Sstevel@tonic-gate
2070Sstevel@tonic-gate case 'I':
2080Sstevel@tonic-gate printf("%d", r1->ru_inblock - r0->ru_inblock);
2090Sstevel@tonic-gate break;
2100Sstevel@tonic-gate
2110Sstevel@tonic-gate case 'O':
2120Sstevel@tonic-gate printf("%d", r1->ru_oublock - r0->ru_oublock);
2130Sstevel@tonic-gate break;
2140Sstevel@tonic-gate }
2150Sstevel@tonic-gate }
2160Sstevel@tonic-gate }
2170Sstevel@tonic-gate Putchar('\n');
2180Sstevel@tonic-gate #undef pgtok
2190Sstevel@tonic-gate }
2200Sstevel@tonic-gate
2210Sstevel@tonic-gate void
pdeltat(struct timeval * t1,struct timeval * t0)2220Sstevel@tonic-gate pdeltat(struct timeval *t1, struct timeval *t0)
2230Sstevel@tonic-gate {
2240Sstevel@tonic-gate struct timeval td;
2250Sstevel@tonic-gate
2260Sstevel@tonic-gate #ifdef TRACE
2270Sstevel@tonic-gate tprintf("TRACE- pdeltat()\n");
2280Sstevel@tonic-gate #endif
2290Sstevel@tonic-gate tvsub(&td, t1, t0);
2300Sstevel@tonic-gate /* change printf formats */
2310Sstevel@tonic-gate printf("%d.%01d", td.tv_sec, td.tv_usec / 100000);
2320Sstevel@tonic-gate }
2330Sstevel@tonic-gate
2340Sstevel@tonic-gate void
tvadd(struct timeval * tsum,struct timeval * t0)2350Sstevel@tonic-gate tvadd(struct timeval *tsum, struct timeval *t0)
2360Sstevel@tonic-gate {
2370Sstevel@tonic-gate
2380Sstevel@tonic-gate #ifdef TRACE
2390Sstevel@tonic-gate tprintf("TRACE- tvadd()\n");
2400Sstevel@tonic-gate #endif
2410Sstevel@tonic-gate tsum->tv_sec += t0->tv_sec;
2420Sstevel@tonic-gate tsum->tv_usec += t0->tv_usec;
2430Sstevel@tonic-gate if (tsum->tv_usec > 1000000) {
2440Sstevel@tonic-gate tsum->tv_sec++;
2450Sstevel@tonic-gate tsum->tv_usec -= 1000000;
2460Sstevel@tonic-gate }
2470Sstevel@tonic-gate }
2480Sstevel@tonic-gate
2490Sstevel@tonic-gate void
tvsub(struct timeval * tdiff,struct timeval * t1,struct timeval * t0)2500Sstevel@tonic-gate tvsub(struct timeval *tdiff, struct timeval *t1, struct timeval *t0)
2510Sstevel@tonic-gate {
2520Sstevel@tonic-gate
2530Sstevel@tonic-gate #ifdef TRACE
2540Sstevel@tonic-gate tprintf("TRACE- tvsub()\n");
2550Sstevel@tonic-gate #endif
2560Sstevel@tonic-gate tdiff->tv_sec = t1->tv_sec - t0->tv_sec;
2570Sstevel@tonic-gate tdiff->tv_usec = t1->tv_usec - t0->tv_usec;
2580Sstevel@tonic-gate if (tdiff->tv_usec < 0) {
2590Sstevel@tonic-gate tdiff->tv_sec--;
2600Sstevel@tonic-gate tdiff->tv_usec += 1000000;
2610Sstevel@tonic-gate }
2620Sstevel@tonic-gate }
263