xref: /titanic_52/usr/src/cmd/csh/sh.time.c (revision bdfc6d18da790deeec2e0eb09c625902defe2498)
1 /*
2  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
3  * Use is subject to license terms.
4  */
5 
6 /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
7 /*	  All Rights Reserved  	*/
8 
9 /*
10  * Copyright (c) 1980 Regents of the University of California.
11  * All rights reserved. The Berkeley Software License Agreement
12  * specifies the terms and conditions for redistribution.
13  */
14 
15 #pragma ident	"%Z%%M%	%I%	%E% SMI"
16 
17 #include "sh.h"
18 #include "sh.tconst.h"
19 
20 void ruadd(struct rusage *ru, struct rusage *ru2);
21 void prusage(struct rusage *r0, struct rusage *r1, struct timeval *e,
22 	struct timeval *b);
23 void pdeltat(struct timeval *t1, struct timeval *t0);
24 void tvadd(struct timeval *tsum, struct timeval *t0);
25 void tvsub(struct timeval *tdiff, struct timeval *t1, struct timeval *t0);
26 
27 /*
28  * C Shell - routines handling process timing and niceing
29  */
30 
31 void
32 settimes(void)
33 {
34 	struct rusage ruch;
35 
36 #ifdef TRACE
37 	tprintf("TRACE- settimes()\n");
38 #endif
39 	(void) gettimeofday(&time0, (struct timezone *)0);
40 	(void) getrusage(RUSAGE_SELF, &ru0);
41 	(void) getrusage(RUSAGE_CHILDREN, &ruch);
42 	ruadd(&ru0, &ruch);
43 }
44 
45 /*
46  * dotime is only called if it is truly a builtin function and not a
47  * prefix to another command
48  */
49 void
50 dotime(void)
51 {
52 	struct timeval timedol;
53 	struct rusage ru1, ruch;
54 
55 #ifdef TRACE
56 	tprintf("TRACE- dotime()\n");
57 #endif
58 	(void) getrusage(RUSAGE_SELF, &ru1);
59 	(void) getrusage(RUSAGE_CHILDREN, &ruch);
60 	ruadd(&ru1, &ruch);
61 	(void) gettimeofday(&timedol, (struct timezone *)0);
62 	prusage(&ru0, &ru1, &timedol, &time0);
63 }
64 
65 /*
66  * donice is only called when it's on the line by itself or with a +- value
67  */
68 void
69 donice(tchar **v)
70 {
71 	tchar *cp;
72 	int nval;
73 
74 #ifdef TRACE
75 	tprintf("TRACE- donice()\n");
76 #endif
77 	v++;
78 	cp = *v++;
79 	if (cp == 0) {
80 		nval = 4;
81 	} else if (*v == 0 && (cp[0] == '+' || cp[0] == '-')) {
82 		nval = getn(cp);
83 	}
84 	(void) setpriority(PRIO_PROCESS, 0, nval);
85 }
86 
87 void
88 ruadd(struct rusage *ru, struct rusage *ru2)
89 {
90 	long *lp, *lp2;
91 	int cnt;
92 	/*
93 	 * The SunOS 4.x <sys/rusage.h> has ru_first and ru_last #defines
94 	 * as below.
95 	 * The SVR4/POSIX <sys/resource.h> does not have these defined for
96 	 * struct rusage
97 	 * The #defines below are here so that the original csh logic
98 	 * for ruadd remains clear now that there is no longer a private copy
99 	 * of the old <sys/resource.h>
100 	 */
101 #define	ru_first ru_ixrss
102 #define	ru_last ru_nivcsw
103 
104 #ifdef TRACE
105 	tprintf("TRACE- ruadd()\n");
106 #endif
107 	tvadd(&ru->ru_utime, &ru2->ru_utime);
108 	tvadd(&ru->ru_stime, &ru2->ru_stime);
109 	if (ru2->ru_maxrss > ru->ru_maxrss) {
110 		ru->ru_maxrss = ru2->ru_maxrss;
111 	}
112 	cnt = &ru->ru_last - &ru->ru_first + 1;
113 	lp = &ru->ru_first;
114 	lp2 = &ru2->ru_first;
115 	do {
116 		*lp++ += *lp2++;
117 	} while (--cnt > 0);
118 }
119 
120 void
121 prusage(struct rusage *r0, struct rusage *r1, struct timeval *e,
122 	struct timeval *b)
123 {
124 #define	pgtok(p)	((p * pgsize) / 1024)
125 	static int	pgsize;
126 
127 	time_t t =
128 	    (r1->ru_utime.tv_sec - r0->ru_utime.tv_sec) * 100 +
129 	    (r1->ru_utime.tv_usec - r0->ru_utime.tv_usec) / 10000 +
130 	    (r1->ru_stime.tv_sec - r0->ru_stime.tv_sec) * 100 +
131 	    (r1->ru_stime.tv_usec - r0->ru_stime.tv_usec) / 10000;
132 	tchar *cp;
133 	int i;
134 	struct varent *vp = adrof(S_time);
135 	int ms =
136 	    (e->tv_sec - b->tv_sec) * 100 + (e->tv_usec - b->tv_usec) / 10000;
137 
138 #ifdef TRACE
139 	tprintf("TRACE- prusage()\n");
140 #endif
141 	if (pgsize == 0) {
142 		pgsize = getpagesize();
143 	}
144 
145 	cp = S_USAGEFORMAT;	/* "%Uu %Ss %E %P %X+%Dk %I+%Oio %Fpf+%Ww" */
146 	if (vp && vp->vec[0] && vp->vec[1]) {
147 		cp = vp->vec[1];
148 	}
149 	for (; *cp; cp++) {
150 		if (*cp != '%') {
151 			Putchar(*cp);
152 		} else if (cp[1]) {
153 			switch (*++cp) {
154 
155 			case 'U':
156 				pdeltat(&r1->ru_utime, &r0->ru_utime);
157 				break;
158 
159 			case 'S':
160 				pdeltat(&r1->ru_stime, &r0->ru_stime);
161 				break;
162 
163 			case 'E':
164 				psecs_int(ms / 100);
165 				break;
166 
167 			case 'P':
168 				printf("%d%%", (int)(t * 100 /
169 				    ((ms ? ms : 1))));
170 				break;
171 
172 			case 'W':
173 				i = r1->ru_nswap - r0->ru_nswap;
174 				printf("%d", i);
175 				break;
176 
177 			case 'X':
178 				printf("%d", t == 0 ? 0 :
179 				    pgtok((r1->ru_ixrss - r0->ru_ixrss) / t));
180 				break;
181 
182 			case 'D':
183 				printf("%d", t == 0 ? 0 :
184 				    pgtok((r1->ru_idrss + r1->ru_isrss-
185 				    (r0->ru_idrss + r0->ru_isrss)) / t));
186 				break;
187 
188 			case 'K':
189 				printf("%d", t == 0 ? 0 :
190 				    pgtok(((r1->ru_ixrss + r1->ru_isrss +
191 				    r1->ru_idrss) - (r0->ru_ixrss +
192 				    r0->ru_idrss + r0->ru_isrss)) / t));
193 				break;
194 
195 			case 'M':
196 				printf("%d", r1->ru_maxrss / 2);
197 				break;
198 
199 			case 'F':
200 				printf("%d", r1->ru_majflt - r0->ru_majflt);
201 				break;
202 
203 			case 'R':
204 				printf("%d", r1->ru_minflt - r0->ru_minflt);
205 				break;
206 
207 			case 'I':
208 				printf("%d", r1->ru_inblock - r0->ru_inblock);
209 				break;
210 
211 			case 'O':
212 				printf("%d", r1->ru_oublock - r0->ru_oublock);
213 				break;
214 			}
215 		}
216 	}
217 	Putchar('\n');
218 #undef	pgtok
219 }
220 
221 void
222 pdeltat(struct timeval *t1, struct timeval *t0)
223 {
224 	struct timeval td;
225 
226 #ifdef TRACE
227 	tprintf("TRACE- pdeltat()\n");
228 #endif
229 	tvsub(&td, t1, t0);
230 	/* change printf formats */
231 	printf("%d.%01d", td.tv_sec, td.tv_usec / 100000);
232 }
233 
234 void
235 tvadd(struct timeval *tsum, struct timeval *t0)
236 {
237 
238 #ifdef TRACE
239 	tprintf("TRACE- tvadd()\n");
240 #endif
241 	tsum->tv_sec += t0->tv_sec;
242 	tsum->tv_usec += t0->tv_usec;
243 	if (tsum->tv_usec > 1000000) {
244 		tsum->tv_sec++;
245 		tsum->tv_usec -= 1000000;
246 	}
247 }
248 
249 void
250 tvsub(struct timeval *tdiff, struct timeval *t1, struct timeval *t0)
251 {
252 
253 #ifdef TRACE
254 	tprintf("TRACE- tvsub()\n");
255 #endif
256 	tdiff->tv_sec = t1->tv_sec - t0->tv_sec;
257 	tdiff->tv_usec = t1->tv_usec - t0->tv_usec;
258 	if (tdiff->tv_usec < 0) {
259 		tdiff->tv_sec--;
260 		tdiff->tv_usec += 1000000;
261 	}
262 }
263