1 /*- 2 * Copyright (c) 1980, 1991 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. 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 34 #ifndef lint 35 /*static char sccsid[] = "from: @(#)time.c 5.14 (Berkeley) 6/8/91";*/ 36 static char rcsid[] = "$Id: time.c,v 1.4 1993/08/01 19:00:28 mycroft Exp $"; 37 #endif /* not lint */ 38 39 #include <sys/types.h> 40 #if __STDC__ 41 # include <stdarg.h> 42 #else 43 # include <varargs.h> 44 #endif 45 46 #include "csh.h" 47 #include "extern.h" 48 49 /* 50 * C Shell - routines handling process timing and niceing 51 */ 52 static void pdeltat __P((struct timeval *, struct timeval *)); 53 54 void 55 settimes() 56 { 57 struct rusage ruch; 58 59 (void) gettimeofday(&time0, NULL); 60 (void) getrusage(RUSAGE_SELF, &ru0); 61 (void) getrusage(RUSAGE_CHILDREN, &ruch); 62 ruadd(&ru0, &ruch); 63 } 64 65 /* 66 * dotime is only called if it is truly a builtin function and not a 67 * prefix to another command 68 */ 69 void 70 dotime() 71 { 72 struct timeval timedol; 73 struct rusage ru1, ruch; 74 75 (void) getrusage(RUSAGE_SELF, &ru1); 76 (void) getrusage(RUSAGE_CHILDREN, &ruch); 77 ruadd(&ru1, &ruch); 78 (void) gettimeofday(&timedol, NULL); 79 prusage(&ru0, &ru1, &timedol, &time0); 80 } 81 82 /* 83 * donice is only called when it on the line by itself or with a +- value 84 */ 85 void 86 donice(v) 87 register Char **v; 88 { 89 register Char *cp; 90 int nval = 0; 91 92 v++, cp = *v++; 93 if (cp == 0) 94 nval = 4; 95 else if (*v == 0 && any("+-", cp[0])) 96 nval = getn(cp); 97 (void) setpriority(PRIO_PROCESS, 0, nval); 98 } 99 100 void 101 ruadd(ru, ru2) 102 register struct rusage *ru, *ru2; 103 { 104 tvadd(&ru->ru_utime, &ru2->ru_utime); 105 tvadd(&ru->ru_stime, &ru2->ru_stime); 106 if (ru2->ru_maxrss > ru->ru_maxrss) 107 ru->ru_maxrss = ru2->ru_maxrss; 108 109 ru->ru_ixrss += ru2->ru_ixrss; 110 ru->ru_idrss += ru2->ru_idrss; 111 ru->ru_isrss += ru2->ru_isrss; 112 ru->ru_minflt += ru2->ru_minflt; 113 ru->ru_majflt += ru2->ru_majflt; 114 ru->ru_nswap += ru2->ru_nswap; 115 ru->ru_inblock += ru2->ru_inblock; 116 ru->ru_oublock += ru2->ru_oublock; 117 ru->ru_msgsnd += ru2->ru_msgsnd; 118 ru->ru_msgrcv += ru2->ru_msgrcv; 119 ru->ru_nsignals += ru2->ru_nsignals; 120 ru->ru_nvcsw += ru2->ru_nvcsw; 121 ru->ru_nivcsw += ru2->ru_nivcsw; 122 } 123 124 void 125 prusage(r0, r1, e, b) 126 register struct rusage *r0, *r1; 127 struct timeval *e, *b; 128 { 129 register time_t t = 130 (r1->ru_utime.tv_sec - r0->ru_utime.tv_sec) * 100 + 131 (r1->ru_utime.tv_usec - r0->ru_utime.tv_usec) / 10000 + 132 (r1->ru_stime.tv_sec - r0->ru_stime.tv_sec) * 100 + 133 (r1->ru_stime.tv_usec - r0->ru_stime.tv_usec) / 10000; 134 register char *cp; 135 register long i; 136 register struct varent *vp = adrof(STRtime); 137 138 int ms = 139 (e->tv_sec - b->tv_sec) * 100 + (e->tv_usec - b->tv_usec) / 10000; 140 141 cp = "%Uu %Ss %E %P %X+%Dk %I+%Oio %Fpf+%Ww"; 142 143 if (vp && vp->vec[0] && vp->vec[1]) 144 cp = short2str(vp->vec[1]); 145 146 for (; *cp; cp++) 147 if (*cp != '%') 148 xputchar(*cp); 149 else if (cp[1]) 150 switch (*++cp) { 151 152 case 'U': /* user CPU time used */ 153 pdeltat(&r1->ru_utime, &r0->ru_utime); 154 break; 155 156 case 'S': /* system CPU time used */ 157 pdeltat(&r1->ru_stime, &r0->ru_stime); 158 break; 159 160 case 'E': /* elapsed (wall-clock) time */ 161 pcsecs((long) ms); 162 break; 163 164 case 'P': /* percent time spent running */ 165 /* check if it did not run at all */ 166 i = (ms == 0) ? 0 : (t * 1000 / ms); 167 xprintf("%ld.%01ld%%", i / 10, i % 10); /* nn.n% */ 168 break; 169 170 case 'W': /* number of swaps */ 171 i = r1->ru_nswap - r0->ru_nswap; 172 xprintf("%ld", i); 173 break; 174 175 case 'X': /* (average) shared text size */ 176 xprintf("%ld", t == 0 ? 0L : (r1->ru_ixrss - r0->ru_ixrss) / t); 177 break; 178 179 case 'D': /* (average) unshared data size */ 180 xprintf("%ld", t == 0 ? 0L : 181 (r1->ru_idrss + r1->ru_isrss - 182 (r0->ru_idrss + r0->ru_isrss)) / t); 183 break; 184 185 case 'K': /* (average) total data memory used */ 186 xprintf("%ld", t == 0 ? 0L : 187 ((r1->ru_ixrss + r1->ru_isrss + r1->ru_idrss) - 188 (r0->ru_ixrss + r0->ru_idrss + r0->ru_isrss)) / t); 189 break; 190 191 case 'M': /* max. Resident Set Size */ 192 xprintf("%ld", r1->ru_maxrss / 2L); 193 break; 194 195 case 'F': /* page faults */ 196 xprintf("%ld", r1->ru_majflt - r0->ru_majflt); 197 break; 198 199 case 'R': /* page reclaims */ 200 xprintf("%ld", r1->ru_minflt - r0->ru_minflt); 201 break; 202 203 case 'I': /* FS blocks in */ 204 xprintf("%ld", r1->ru_inblock - r0->ru_inblock); 205 break; 206 207 case 'O': /* FS blocks out */ 208 xprintf("%ld", r1->ru_oublock - r0->ru_oublock); 209 break; 210 211 case 'r': /* socket messages recieved */ 212 xprintf("%ld", r1->ru_msgrcv - r0->ru_msgrcv); 213 break; 214 215 case 's': /* socket messages sent */ 216 xprintf("%ld", r1->ru_msgsnd - r0->ru_msgsnd); 217 break; 218 219 case 'k': /* number of signals recieved */ 220 xprintf("%ld", r1->ru_nsignals - r0->ru_nsignals); 221 break; 222 223 case 'w': /* num. voluntary context switches (waits) */ 224 xprintf("%ld", r1->ru_nvcsw - r0->ru_nvcsw); 225 break; 226 227 case 'c': /* num. involuntary context switches */ 228 xprintf("%ld", r1->ru_nivcsw - r0->ru_nivcsw); 229 break; 230 } 231 xputchar('\n'); 232 } 233 234 static void 235 pdeltat(t1, t0) 236 struct timeval *t1, *t0; 237 { 238 struct timeval td; 239 240 tvsub(&td, t1, t0); 241 xprintf("%d.%01d", td.tv_sec, td.tv_usec / 100000); 242 } 243 244 void 245 tvadd(tsum, t0) 246 struct timeval *tsum, *t0; 247 { 248 249 tsum->tv_sec += t0->tv_sec; 250 tsum->tv_usec += t0->tv_usec; 251 if (tsum->tv_usec > 1000000) 252 tsum->tv_sec++, tsum->tv_usec -= 1000000; 253 } 254 255 void 256 tvsub(tdiff, t1, t0) 257 struct timeval *tdiff, *t1, *t0; 258 { 259 260 tdiff->tv_sec = t1->tv_sec - t0->tv_sec; 261 tdiff->tv_usec = t1->tv_usec - t0->tv_usec; 262 if (tdiff->tv_usec < 0) 263 tdiff->tv_sec--, tdiff->tv_usec += 1000000; 264 } 265