time.c revision 1.5
1/* $OpenBSD: time.c,v 1.5 1997/11/15 21:51:31 todd Exp $ */ 2/* $NetBSD: time.c,v 1.7 1995/03/21 13:55:25 mycroft Exp $ */ 3 4/*- 5 * Copyright (c) 1980, 1991, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by the University of 19 * California, Berkeley and its contributors. 20 * 4. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 */ 36 37#ifndef lint 38#if 0 39static char sccsid[] = "@(#)time.c 8.1 (Berkeley) 5/31/93"; 40#else 41static char rcsid[] = "$OpenBSD: time.c,v 1.5 1997/11/15 21:51:31 todd Exp $"; 42#endif 43#endif /* not lint */ 44 45#include <sys/types.h> 46#ifdef __STDC__ 47# include <stdarg.h> 48#else 49# include <varargs.h> 50#endif 51 52#include "csh.h" 53#include "extern.h" 54 55/* 56 * C Shell - routines handling process timing and niceing 57 */ 58static void pdeltat __P((struct timeval *, struct timeval *)); 59 60void 61settimes() 62{ 63 struct rusage ruch; 64 65 (void) gettimeofday(&time0, NULL); 66 (void) getrusage(RUSAGE_SELF, &ru0); 67 (void) getrusage(RUSAGE_CHILDREN, &ruch); 68 ruadd(&ru0, &ruch); 69} 70 71/* 72 * dotime is only called if it is truly a builtin function and not a 73 * prefix to another command 74 */ 75void 76/*ARGSUSED*/ 77dotime(v, t) 78 Char **v; 79 struct command *t; 80{ 81 struct timeval timedol; 82 struct rusage ru1, ruch; 83 84 (void) getrusage(RUSAGE_SELF, &ru1); 85 (void) getrusage(RUSAGE_CHILDREN, &ruch); 86 ruadd(&ru1, &ruch); 87 (void) gettimeofday(&timedol, NULL); 88 prusage(&ru0, &ru1, &timedol, &time0); 89} 90 91/* 92 * donice is only called when it on the line by itself or with a +- value 93 */ 94void 95/*ARGSUSED*/ 96donice(v, t) 97 Char **v; 98 struct command *t; 99{ 100 register Char *cp; 101 int nval = 0; 102 103 v++, cp = *v++; 104 if (cp == 0) 105 nval = 4; 106 else if (*v == 0 && any("+-", cp[0])) 107 nval = getn(cp); 108 (void) setpriority(PRIO_PROCESS, 0, nval); 109} 110 111void 112ruadd(ru, ru2) 113 register struct rusage *ru, *ru2; 114{ 115 timeradd(&ru->ru_utime, &ru2->ru_utime, &ru->ru_utime); 116 timeradd(&ru->ru_stime, &ru2->ru_stime, &ru->ru_stime); 117 if (ru2->ru_maxrss > ru->ru_maxrss) 118 ru->ru_maxrss = ru2->ru_maxrss; 119 120 ru->ru_ixrss += ru2->ru_ixrss; 121 ru->ru_idrss += ru2->ru_idrss; 122 ru->ru_isrss += ru2->ru_isrss; 123 ru->ru_minflt += ru2->ru_minflt; 124 ru->ru_majflt += ru2->ru_majflt; 125 ru->ru_nswap += ru2->ru_nswap; 126 ru->ru_inblock += ru2->ru_inblock; 127 ru->ru_oublock += ru2->ru_oublock; 128 ru->ru_msgsnd += ru2->ru_msgsnd; 129 ru->ru_msgrcv += ru2->ru_msgrcv; 130 ru->ru_nsignals += ru2->ru_nsignals; 131 ru->ru_nvcsw += ru2->ru_nvcsw; 132 ru->ru_nivcsw += ru2->ru_nivcsw; 133} 134 135void 136prusage(r0, r1, e, b) 137 register struct rusage *r0, *r1; 138 struct timeval *e, *b; 139{ 140 register time_t t = 141 (r1->ru_utime.tv_sec - r0->ru_utime.tv_sec) * 100 + 142 (r1->ru_utime.tv_usec - r0->ru_utime.tv_usec) / 10000 + 143 (r1->ru_stime.tv_sec - r0->ru_stime.tv_sec) * 100 + 144 (r1->ru_stime.tv_usec - r0->ru_stime.tv_usec) / 10000; 145 register char *cp; 146 register long i; 147 register struct varent *vp = adrof(STRtime); 148 149 int ms = 150 (e->tv_sec - b->tv_sec) * 100 + (e->tv_usec - b->tv_usec) / 10000; 151 152 cp = "%Uu %Ss %E %P %X+%Dk %I+%Oio %Fpf+%Ww"; 153 154 if (vp && vp->vec[0] && vp->vec[1]) 155 cp = short2str(vp->vec[1]); 156 157 for (; *cp; cp++) 158 if (*cp != '%') 159 (void) fputc(*cp, cshout); 160 else if (cp[1]) 161 switch (*++cp) { 162 163 case 'U': /* user CPU time used */ 164 pdeltat(&r1->ru_utime, &r0->ru_utime); 165 break; 166 167 case 'S': /* system CPU time used */ 168 pdeltat(&r1->ru_stime, &r0->ru_stime); 169 break; 170 171 case 'E': /* elapsed (wall-clock) time */ 172 pcsecs((long) ms); 173 break; 174 175 case 'P': /* percent time spent running */ 176 /* check if it did not run at all */ 177 i = (ms == 0) ? 0 : (t * 1000 / ms); 178 /* nn.n% */ 179 (void) fprintf(cshout, "%ld.%01ld%%", i / 10, i % 10); 180 break; 181 182 case 'W': /* number of swaps */ 183 i = r1->ru_nswap - r0->ru_nswap; 184 (void) fprintf(cshout, "%ld", i); 185 break; 186 187 case 'X': /* (average) shared text size */ 188 (void) fprintf(cshout, "%ld", t == 0 ? 0L : 189 (r1->ru_ixrss - r0->ru_ixrss) / t); 190 break; 191 192 case 'D': /* (average) unshared data size */ 193 (void) fprintf(cshout, "%ld", t == 0 ? 0L : 194 (r1->ru_idrss + r1->ru_isrss - 195 (r0->ru_idrss + r0->ru_isrss)) / t); 196 break; 197 198 case 'K': /* (average) total data memory used */ 199 (void) fprintf(cshout, "%ld", t == 0 ? 0L : 200 ((r1->ru_ixrss + r1->ru_isrss + r1->ru_idrss) - 201 (r0->ru_ixrss + r0->ru_idrss + r0->ru_isrss)) / t); 202 break; 203 204 case 'M': /* max. Resident Set Size */ 205 (void) fprintf(cshout, "%ld", r1->ru_maxrss / 2L); 206 break; 207 208 case 'F': /* page faults */ 209 (void) fprintf(cshout, "%ld", r1->ru_majflt - r0->ru_majflt); 210 break; 211 212 case 'R': /* page reclaims */ 213 (void) fprintf(cshout, "%ld", r1->ru_minflt - r0->ru_minflt); 214 break; 215 216 case 'I': /* FS blocks in */ 217 (void) fprintf(cshout, "%ld", r1->ru_inblock - r0->ru_inblock); 218 break; 219 220 case 'O': /* FS blocks out */ 221 (void) fprintf(cshout, "%ld", r1->ru_oublock - r0->ru_oublock); 222 break; 223 224 case 'r': /* socket messages received */ 225 (void) fprintf(cshout, "%ld", r1->ru_msgrcv - r0->ru_msgrcv); 226 break; 227 228 case 's': /* socket messages sent */ 229 (void) fprintf(cshout, "%ld", r1->ru_msgsnd - r0->ru_msgsnd); 230 break; 231 232 case 'k': /* number of signals received */ 233 (void) fprintf(cshout, "%ld", r1->ru_nsignals-r0->ru_nsignals); 234 break; 235 236 case 'w': /* num. voluntary context switches (waits) */ 237 (void) fprintf(cshout, "%ld", r1->ru_nvcsw - r0->ru_nvcsw); 238 break; 239 240 case 'c': /* num. involuntary context switches */ 241 (void) fprintf(cshout, "%ld", r1->ru_nivcsw - r0->ru_nivcsw); 242 break; 243 } 244 (void) fputc('\n', cshout); 245} 246 247static void 248pdeltat(t1, t0) 249 struct timeval *t1, *t0; 250{ 251 struct timeval td; 252 253 timersub(t1, t0, &td); 254 (void) fprintf(cshout, "%ld.%01ld", td.tv_sec, td.tv_usec / 100000); 255} 256 257#define P2DIG(i) (void) fprintf(cshout, "%d%d", (i) / 10, (i) % 10) 258 259void 260psecs(l) 261 long l; 262{ 263 register int i; 264 265 i = l / 3600; 266 if (i) { 267 (void) fprintf(cshout, "%d:", i); 268 i = l % 3600; 269 P2DIG(i / 60); 270 goto minsec; 271 } 272 i = l; 273 (void) fprintf(cshout, "%d", i / 60); 274minsec: 275 i %= 60; 276 (void) fputc(':', cshout); 277 P2DIG(i); 278} 279 280void 281pcsecs(l) /* PWP: print mm:ss.dd, l is in sec*100 */ 282 long l; 283{ 284 register int i; 285 286 i = l / 360000; 287 if (i) { 288 (void) fprintf(cshout, "%d:", i); 289 i = (l % 360000) / 100; 290 P2DIG(i / 60); 291 goto minsec; 292 } 293 i = l / 100; 294 (void) fprintf(cshout, "%d", i / 60); 295minsec: 296 i %= 60; 297 (void) fputc(':', cshout); 298 P2DIG(i); 299 (void) fputc('.', cshout); 300 P2DIG((int) (l % 100)); 301} 302