xref: /original-bsd/bin/csh/time.c (revision 9d44dd09)
1 static	char *sccsid = "@(#)time.c 4.1 10/09/80";
2 
3 #include "sh.h"
4 
5 /*
6  * C Shell - routines handling process timing and niceing
7  */
8 #ifdef VMUNIX
9 struct	vtimes vm0;
10 #else
11 struct	tms times0;
12 struct	tms timesdol;
13 #endif
14 
15 settimes()
16 {
17 
18 	time(&time0);
19 #ifdef VMUNIX
20 	vtimes(&vm0, 0);
21 #else
22 	times(&times0);
23 #endif
24 }
25 
26 /*
27  * dotime is only called if it is truly a builtin function and not a
28  * prefix to another command
29  */
30 dotime()
31 {
32 	time_t timedol;
33 #ifdef VMUNIX
34 	struct vtimes vm1, vmch;
35 
36 	vtimes(&vm1, &vmch);
37 	vmsadd(&vm1, &vmch);
38 #endif
39 
40 	time(&timedol);
41 #ifdef VMUNIX
42 	pvtimes(&vm0, &vm1, timedol - time0);
43 #else
44 	times(&timesdol);
45 	ptimes(timedol - time0, &times0, &timesdol);
46 #endif
47 }
48 
49 /*
50  * donice is only called when it on the line by itself or with a +- value
51  */
52 donice(v)
53 	register char **v;
54 {
55 	register char *cp;
56 
57 	v++, cp = *v++;
58 	if (cp == 0) {
59 #ifndef V6
60 		nice(20);
61 		nice(-10);
62 #endif
63 		nice(4);
64 	} else if (*v == 0 && any(cp[0], "+-")) {
65 #ifndef V6
66 		nice(20);
67 		nice(-10);
68 #endif
69 		nice(getn(cp));
70 	}
71 }
72 
73 #ifndef VMUNIX
74 ptimes(utime, stime, etime)
75 	register time_t utime, stime, etime;
76 {
77 
78 	p60ths(utime);
79 	printf("u ");
80 	p60ths(stime);
81 	printf("s ");
82 	psecs(etime);
83 	printf(" %d%%\n", (int) (100 * (utime+stime) /
84 		(60 * (etime ? etime : 1))));
85 }
86 
87 #else
88 vmsadd(vp, wp)
89 	register struct vtimes *vp, *wp;
90 {
91 
92 	vp->vm_utime += wp->vm_utime;
93 	vp->vm_stime += wp->vm_stime;
94 	vp->vm_nswap += wp->vm_nswap;
95 	vp->vm_idsrss += wp->vm_idsrss;
96 	vp->vm_ixrss += wp->vm_ixrss;
97 	if (vp->vm_maxrss < wp->vm_maxrss)
98 		vp->vm_maxrss = wp->vm_maxrss;
99 	vp->vm_majflt += wp->vm_majflt;
100 	vp->vm_minflt += wp->vm_minflt;
101 	vp->vm_inblk += wp->vm_inblk;
102 	vp->vm_oublk += wp->vm_oublk;
103 }
104 
105 pvtimes(v0, v1, sec)
106 	register struct vtimes *v0, *v1;
107 	time_t sec;
108 {
109 	register time_t t =
110 	    (v1->vm_utime-v0->vm_utime)+(v1->vm_stime-v0->vm_stime);
111 	register char *cp;
112 	register int i;
113 	register struct varent *vp = adrof("time");
114 
115 	cp = "%Uu %Ss %E %P %X+%Dk %I+%Oio %Fpf+%Ww";
116 	if (vp && vp->vec[0] && vp->vec[1])
117 		cp = vp->vec[1];
118 	for (; *cp; cp++)
119 	if (*cp != '%')
120 		putchar(*cp);
121 	else if (cp[1]) switch(*++cp) {
122 
123 	case 'U':
124 		p60ths(v1->vm_utime - v0->vm_utime);
125 		break;
126 
127 	case 'S':
128 		p60ths(v1->vm_stime - v0->vm_stime);
129 		break;
130 
131 	case 'E':
132 		psecs(sec);
133 		break;
134 
135 	case 'P':
136 		printf("%d%%", (int) ((100 * t) / (60 * (sec ? sec : 1))));
137 		break;
138 
139 	case 'W':
140 		i = v1->vm_nswap - v0->vm_nswap;
141 		printf("%d", i);
142 		break;
143 
144 	case 'X':
145 		printf("%d", t == 0 ? 0 : (v1->vm_ixrss-v0->vm_ixrss)/(2*t));
146 		break;
147 
148 	case 'D':
149 		printf("%d", t == 0 ? 0 : (v1->vm_idsrss-v0->vm_idsrss)/(2*t));
150 		break;
151 
152 	case 'K':
153 		printf("%d", t == 0 ? 0 : ((v1->vm_ixrss+v1->vm_idsrss) -
154 		   (v0->vm_ixrss+v0->vm_idsrss))/(2*t));
155 		break;
156 
157 	case 'M':
158 		printf("%d", v1->vm_maxrss/2);
159 		break;
160 
161 	case 'F':
162 		printf("%d", v1->vm_majflt-v0->vm_majflt);
163 		break;
164 
165 	case 'R':
166 		printf("%d", v1->vm_minflt-v0->vm_minflt);
167 		break;
168 
169 	case 'I':
170 		printf("%d", v1->vm_inblk-v0->vm_inblk);
171 		break;
172 
173 	case 'O':
174 		printf("%d", v1->vm_oublk-v0->vm_oublk);
175 		break;
176 
177 	}
178 	putchar('\n');
179 }
180 #endif
181