2 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
3 * Use is subject to license terms.
6 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
7 /* All Rights Reserved */
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.
15 #pragma ident "%Z%%M% %I% %E% SMI"
18 #include "sh.tconst.h"
20 void ruadd(struct rusage
*ru
, struct rusage
*ru2
);
21 void prusage(struct rusage
*r0
, struct rusage
*r1
, struct timeval
*e
,
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
);
28 * C Shell - routines handling process timing and niceing
37 tprintf("TRACE- settimes()\n");
39 (void) gettimeofday(&time0
, NULL
);
40 (void) getrusage(RUSAGE_SELF
, &ru0
);
41 (void) getrusage(RUSAGE_CHILDREN
, &ruch
);
46 * dotime is only called if it is truly a builtin function and not a
47 * prefix to another command
52 struct timeval timedol
;
53 struct rusage ru1
, ruch
;
56 tprintf("TRACE- dotime()\n");
58 (void) getrusage(RUSAGE_SELF
, &ru1
);
59 (void) getrusage(RUSAGE_CHILDREN
, &ruch
);
61 (void) gettimeofday(&timedol
, NULL
);
62 prusage(&ru0
, &ru1
, &timedol
, &time0
);
66 * donice is only called when it's on the line by itself or with a +- value
75 tprintf("TRACE- donice()\n");
81 } else if (*v
== 0 && (cp
[0] == '+' || cp
[0] == '-')) {
84 (void) setpriority(PRIO_PROCESS
, 0, nval
);
88 ruadd(struct rusage
*ru
, struct rusage
*ru2
)
93 * The SunOS 4.x <sys/rusage.h> has ru_first and ru_last #defines
95 * The SVR4/POSIX <sys/resource.h> does not have these defined for
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>
101 #define ru_first ru_ixrss
102 #define ru_last ru_nivcsw
105 tprintf("TRACE- ruadd()\n");
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
;
112 cnt
= &ru
->ru_last
- &ru
->ru_first
+ 1;
114 lp2
= &ru2
->ru_first
;
121 prusage(struct rusage
*r0
, struct rusage
*r1
, struct timeval
*e
,
124 #define pgtok(p) ((p * pgsize) / 1024)
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;
134 struct varent
*vp
= adrof(S_time
);
136 (e
->tv_sec
- b
->tv_sec
) * 100 + (e
->tv_usec
- b
->tv_usec
) / 10000;
139 tprintf("TRACE- prusage()\n");
142 pgsize
= getpagesize();
145 cp
= S_USAGEFORMAT
; /* "%Uu %Ss %E %P %X+%Dk %I+%Oio %Fpf+%Ww" */
146 if (vp
&& vp
->vec
[0] && vp
->vec
[1]) {
156 pdeltat(&r1
->ru_utime
, &r0
->ru_utime
);
160 pdeltat(&r1
->ru_stime
, &r0
->ru_stime
);
168 printf("%d%%", (int)(t
* 100 /
173 i
= r1
->ru_nswap
- r0
->ru_nswap
;
178 printf("%d", t
== 0 ? 0 :
179 pgtok((r1
->ru_ixrss
- r0
->ru_ixrss
) / t
));
183 printf("%d", t
== 0 ? 0 :
184 pgtok((r1
->ru_idrss
+ r1
->ru_isrss
-
185 (r0
->ru_idrss
+ r0
->ru_isrss
)) / t
));
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
));
196 printf("%d", r1
->ru_maxrss
/ 2);
200 printf("%d", r1
->ru_majflt
- r0
->ru_majflt
);
204 printf("%d", r1
->ru_minflt
- r0
->ru_minflt
);
208 printf("%d", r1
->ru_inblock
- r0
->ru_inblock
);
212 printf("%d", r1
->ru_oublock
- r0
->ru_oublock
);
222 pdeltat(struct timeval
*t1
, struct timeval
*t0
)
227 tprintf("TRACE- pdeltat()\n");
230 /* change printf formats */
231 printf("%d.%01d", td
.tv_sec
, td
.tv_usec
/ 100000);
235 tvadd(struct timeval
*tsum
, struct timeval
*t0
)
239 tprintf("TRACE- tvadd()\n");
241 tsum
->tv_sec
+= t0
->tv_sec
;
242 tsum
->tv_usec
+= t0
->tv_usec
;
243 if (tsum
->tv_usec
> 1000000) {
245 tsum
->tv_usec
-= 1000000;
250 tvsub(struct timeval
*tdiff
, struct timeval
*t1
, struct timeval
*t0
)
254 tprintf("TRACE- tvsub()\n");
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) {
260 tdiff
->tv_usec
+= 1000000;