1 /* 2 * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 3 * Use is subject to license terms. 4 */ 5 6 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */ 7 /* All Rights Reserved */ 8 9 /* 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. 13 */ 14 15 #include "sh.h" 16 #include "sh.tconst.h" 17 18 struct timeval time0; 19 static struct rusage ru0; 20 21 void ruadd(struct rusage *ru, struct rusage *ru2); 22 void prusage(struct rusage *r0, struct rusage *r1, struct timeval *e, 23 struct timeval *b); 24 void pdeltat(struct timeval *t1, struct timeval *t0); 25 void tvadd(struct timeval *tsum, struct timeval *t0); 26 void tvsub(struct timeval *tdiff, struct timeval *t1, struct timeval *t0); 27 28 /* 29 * C Shell - routines handling process timing and niceing 30 */ 31 32 void 33 settimes(void) 34 { 35 struct rusage ruch; 36 37 #ifdef TRACE 38 tprintf("TRACE- settimes()\n"); 39 #endif 40 (void) gettimeofday(&time0, (struct timezone *)0); 41 (void) getrusage(RUSAGE_SELF, &ru0); 42 (void) getrusage(RUSAGE_CHILDREN, &ruch); 43 ruadd(&ru0, &ruch); 44 } 45 46 /* 47 * dotime is only called if it is truly a builtin function and not a 48 * prefix to another command 49 */ 50 void 51 dotime(void) 52 { 53 struct timeval timedol; 54 struct rusage ru1, ruch; 55 56 #ifdef TRACE 57 tprintf("TRACE- dotime()\n"); 58 #endif 59 (void) getrusage(RUSAGE_SELF, &ru1); 60 (void) getrusage(RUSAGE_CHILDREN, &ruch); 61 ruadd(&ru1, &ruch); 62 (void) gettimeofday(&timedol, (struct timezone *)0); 63 prusage(&ru0, &ru1, &timedol, &time0); 64 } 65 66 /* 67 * donice is only called when it's on the line by itself or with a +- value 68 */ 69 void 70 donice(tchar **v) 71 { 72 tchar *cp; 73 int nval; 74 75 #ifdef TRACE 76 tprintf("TRACE- donice()\n"); 77 #endif 78 v++; 79 cp = *v++; 80 if (cp == 0) { 81 nval = 4; 82 } else if (*v == 0 && (cp[0] == '+' || cp[0] == '-')) { 83 nval = getn(cp); 84 } 85 (void) setpriority(PRIO_PROCESS, 0, nval); 86 } 87 88 void 89 ruadd(struct rusage *ru, struct rusage *ru2) 90 { 91 long *lp, *lp2; 92 int cnt; 93 /* 94 * The SunOS 4.x <sys/rusage.h> has ru_first and ru_last #defines 95 * as below. 96 * The SVR4/POSIX <sys/resource.h> does not have these defined for 97 * struct rusage 98 * The #defines below are here so that the original csh logic 99 * for ruadd remains clear now that there is no longer a private copy 100 * of the old <sys/resource.h> 101 */ 102 #define ru_first ru_ixrss 103 #define ru_last ru_nivcsw 104 105 #ifdef TRACE 106 tprintf("TRACE- ruadd()\n"); 107 #endif 108 tvadd(&ru->ru_utime, &ru2->ru_utime); 109 tvadd(&ru->ru_stime, &ru2->ru_stime); 110 if (ru2->ru_maxrss > ru->ru_maxrss) { 111 ru->ru_maxrss = ru2->ru_maxrss; 112 } 113 cnt = &ru->ru_last - &ru->ru_first + 1; 114 lp = &ru->ru_first; 115 lp2 = &ru2->ru_first; 116 do { 117 *lp++ += *lp2++; 118 } while (--cnt > 0); 119 } 120 121 void 122 prusage(struct rusage *r0, struct rusage *r1, struct timeval *e, 123 struct timeval *b) 124 { 125 #define pgtok(p) ((p * pgsize) / 1024) 126 static int pgsize; 127 128 time_t t = 129 (r1->ru_utime.tv_sec - r0->ru_utime.tv_sec) * 100 + 130 (r1->ru_utime.tv_usec - r0->ru_utime.tv_usec) / 10000 + 131 (r1->ru_stime.tv_sec - r0->ru_stime.tv_sec) * 100 + 132 (r1->ru_stime.tv_usec - r0->ru_stime.tv_usec) / 10000; 133 tchar *cp; 134 int i; 135 struct varent *vp = adrof(S_time); 136 int ms = 137 (e->tv_sec - b->tv_sec) * 100 + (e->tv_usec - b->tv_usec) / 10000; 138 139 #ifdef TRACE 140 tprintf("TRACE- prusage()\n"); 141 #endif 142 if (pgsize == 0) { 143 pgsize = getpagesize(); 144 } 145 146 cp = S_USAGEFORMAT; /* "%Uu %Ss %E %P %X+%Dk %I+%Oio %Fpf+%Ww" */ 147 if (vp && vp->vec[0] && vp->vec[1]) { 148 cp = vp->vec[1]; 149 } 150 for (; *cp; cp++) { 151 if (*cp != '%') { 152 Putchar(*cp); 153 } else if (cp[1]) { 154 switch (*++cp) { 155 156 case 'U': 157 pdeltat(&r1->ru_utime, &r0->ru_utime); 158 break; 159 160 case 'S': 161 pdeltat(&r1->ru_stime, &r0->ru_stime); 162 break; 163 164 case 'E': 165 psecs_int(ms / 100); 166 break; 167 168 case 'P': 169 printf("%d%%", (int)(t * 100 / 170 ((ms ? ms : 1)))); 171 break; 172 173 case 'W': 174 i = r1->ru_nswap - r0->ru_nswap; 175 printf("%d", i); 176 break; 177 178 case 'X': 179 printf("%d", t == 0 ? 0 : 180 pgtok((r1->ru_ixrss - r0->ru_ixrss) / t)); 181 break; 182 183 case 'D': 184 printf("%d", t == 0 ? 0 : 185 pgtok((r1->ru_idrss + r1->ru_isrss- 186 (r0->ru_idrss + r0->ru_isrss)) / t)); 187 break; 188 189 case 'K': 190 printf("%d", t == 0 ? 0 : 191 pgtok(((r1->ru_ixrss + r1->ru_isrss + 192 r1->ru_idrss) - (r0->ru_ixrss + 193 r0->ru_idrss + r0->ru_isrss)) / t)); 194 break; 195 196 case 'M': 197 printf("%d", r1->ru_maxrss / 2); 198 break; 199 200 case 'F': 201 printf("%d", r1->ru_majflt - r0->ru_majflt); 202 break; 203 204 case 'R': 205 printf("%d", r1->ru_minflt - r0->ru_minflt); 206 break; 207 208 case 'I': 209 printf("%d", r1->ru_inblock - r0->ru_inblock); 210 break; 211 212 case 'O': 213 printf("%d", r1->ru_oublock - r0->ru_oublock); 214 break; 215 } 216 } 217 } 218 Putchar('\n'); 219 #undef pgtok 220 } 221 222 void 223 pdeltat(struct timeval *t1, struct timeval *t0) 224 { 225 struct timeval td; 226 227 #ifdef TRACE 228 tprintf("TRACE- pdeltat()\n"); 229 #endif 230 tvsub(&td, t1, t0); 231 /* change printf formats */ 232 printf("%d.%01d", td.tv_sec, td.tv_usec / 100000); 233 } 234 235 void 236 tvadd(struct timeval *tsum, struct timeval *t0) 237 { 238 239 #ifdef TRACE 240 tprintf("TRACE- tvadd()\n"); 241 #endif 242 tsum->tv_sec += t0->tv_sec; 243 tsum->tv_usec += t0->tv_usec; 244 if (tsum->tv_usec > 1000000) { 245 tsum->tv_sec++; 246 tsum->tv_usec -= 1000000; 247 } 248 } 249 250 void 251 tvsub(struct timeval *tdiff, struct timeval *t1, struct timeval *t0) 252 { 253 254 #ifdef TRACE 255 tprintf("TRACE- tvsub()\n"); 256 #endif 257 tdiff->tv_sec = t1->tv_sec - t0->tv_sec; 258 tdiff->tv_usec = t1->tv_usec - t0->tv_usec; 259 if (tdiff->tv_usec < 0) { 260 tdiff->tv_sec--; 261 tdiff->tv_usec += 1000000; 262 } 263 } 264