1: /* 2: * Copyright (c) 1980 Regents of the University of California. 3: * All rights reserved. The Berkeley Software License Agreement 4: * specifies the terms and conditions for redistribution. 5: */ 6: 7: #if !defined(lint) && defined(DOSCCS) 8: static char *sccsid = "@(#)sh.time.c 5.4.1 (2.11BSD GTE) 12/31/93"; 9: #endif 10: 11: #include "sh.h" 12: 13: /* 14: * C Shell - routines handling process timing and niceing 15: */ 16: 17: settimes() 18: { 19: struct rusage ruch; 20: 21: (void) gettimeofday(&time0, (struct timezone *)0); 22: (void) getrusage(RUSAGE_SELF, &ru0); 23: (void) getrusage(RUSAGE_CHILDREN, &ruch); 24: ruadd(&ru0, &ruch); 25: } 26: 27: /* 28: * dotime is only called if it is truly a builtin function and not a 29: * prefix to another command 30: */ 31: dotime() 32: { 33: struct timeval timedol; 34: struct rusage ru1, ruch; 35: 36: (void) getrusage(RUSAGE_SELF, &ru1); 37: (void) getrusage(RUSAGE_CHILDREN, &ruch); 38: ruadd(&ru1, &ruch); 39: (void) gettimeofday(&timedol, (struct timezone *)0); 40: prusage(&ru0, &ru1, &timedol, &time0); 41: } 42: 43: /* 44: * donice is only called when it on the line by itself or with a +- value 45: */ 46: donice(v) 47: register char **v; 48: { 49: register char *cp; 50: int nval; 51: 52: v++, cp = *v++; 53: if (cp == 0) 54: nval = 4; 55: else if (*v == 0 && any(cp[0], "+-")) 56: nval = getn(cp); 57: (void) setpriority(PRIO_PROCESS, 0, nval); 58: } 59: 60: ruadd(ru, ru2) 61: register struct rusage *ru, *ru2; 62: { 63: register long *lp, *lp2; 64: register int cnt; 65: 66: tvadd(&ru->ru_utime, &ru2->ru_utime); 67: tvadd(&ru->ru_stime, &ru2->ru_stime); 68: if (ru2->ru_maxrss > ru->ru_maxrss) 69: ru->ru_maxrss = ru2->ru_maxrss; 70: cnt = &ru->ru_last - &ru->ru_first + 1; 71: lp = &ru->ru_first; lp2 = &ru2->ru_first; 72: do 73: *lp++ += *lp2++; 74: while (--cnt > 0); 75: } 76: 77: prusage(r0, r1, e, b) 78: register struct rusage *r0, *r1; 79: struct timeval *e, *b; 80: { 81: register time_t t = 82: (r1->ru_utime.tv_sec-r0->ru_utime.tv_sec)*100+ 83: (r1->ru_utime.tv_usec-r0->ru_utime.tv_usec)/10000+ 84: (r1->ru_stime.tv_sec-r0->ru_stime.tv_sec)*100+ 85: (r1->ru_stime.tv_usec-r0->ru_stime.tv_usec)/10000; 86: register char *cp; 87: register long i; 88: register struct varent *vp = adrof("time"); 89: long ms = 90: (e->tv_sec-b->tv_sec)*100 + (e->tv_usec-b->tv_usec)/10000; 91: 92: #ifdef pdp11 93: cp = "%Uu %Ss %E %P %I+%Oio %Vov %Wsw"; 94: #else 95: cp = "%Uu %Ss %E %P %X+%Dk %I+%Oio %Fpf+%Ww"; 96: #endif 97: if (vp && vp->vec[0] && vp->vec[1]) 98: cp = vp->vec[1]; 99: for (; *cp; cp++) 100: if (*cp != '%') 101: putchar(*cp); 102: else if (cp[1]) switch(*++cp) { 103: 104: case 'U': 105: pdeltat(&r1->ru_utime, &r0->ru_utime); 106: break; 107: 108: case 'S': 109: pdeltat(&r1->ru_stime, &r0->ru_stime); 110: break; 111: 112: case 'E': 113: psecs(ms / 100); 114: break; 115: 116: case 'P': 117: printf("%d%%", (int) (t*100 / ((ms ? ms : 1)))); 118: break; 119: 120: #ifdef pdp11 121: case 'V': 122: printf("%ld", r1->ru_ovly - r0->ru_ovly); 123: break; 124: #endif 125: 126: case 'W': 127: i = r1->ru_nswap - r0->ru_nswap; 128: printf("%ld", i); 129: break; 130: 131: case 'X': 132: printf("%ld", t == 0 ? 0L : (r1->ru_ixrss-r0->ru_ixrss)/t); 133: break; 134: 135: case 'D': 136: printf("%ld", t == 0 ? 0L : 137: (r1->ru_idrss+r1->ru_isrss-(r0->ru_idrss+r0->ru_isrss))/t); 138: break; 139: 140: case 'K': 141: printf("%ld", t == 0 ? 0L : 142: ((r1->ru_ixrss+r1->ru_isrss+r1->ru_idrss) - 143: (r0->ru_ixrss+r0->ru_idrss+r0->ru_isrss))/t); 144: break; 145: 146: case 'M': 147: printf("%ld", r1->ru_maxrss/2); 148: break; 149: 150: case 'F': 151: printf("%ld", r1->ru_majflt-r0->ru_majflt); 152: break; 153: 154: case 'R': 155: printf("%ld", r1->ru_minflt-r0->ru_minflt); 156: break; 157: 158: case 'I': 159: printf("%ld", r1->ru_inblock-r0->ru_inblock); 160: break; 161: 162: case 'O': 163: printf("%ld", r1->ru_oublock-r0->ru_oublock); 164: break; 165: } 166: putchar('\n'); 167: } 168: 169: pdeltat(t1, t0) 170: struct timeval *t1, *t0; 171: { 172: struct timeval td; 173: 174: tvsub(&td, t1, t0); 175: printf("%ld.%01ld", td.tv_sec, td.tv_usec/100000); 176: } 177: 178: tvadd(tsum, t0) 179: struct timeval *tsum, *t0; 180: { 181: 182: tsum->tv_sec += t0->tv_sec; 183: tsum->tv_usec += t0->tv_usec; 184: if (tsum->tv_usec > 1000000) 185: tsum->tv_sec++, tsum->tv_usec -= 1000000; 186: } 187: 188: tvsub(tdiff, t1, t0) 189: struct timeval *tdiff, *t1, *t0; 190: { 191: 192: tdiff->tv_sec = t1->tv_sec - t0->tv_sec; 193: tdiff->tv_usec = t1->tv_usec - t0->tv_usec; 194: if (tdiff->tv_usec < 0) 195: tdiff->tv_sec--, tdiff->tv_usec += 1000000; 196: }