From ea77538991102dd9b18166209c40d654e4e8f8b2 Mon Sep 17 00:00:00 2001 From: Sam Leffler Date: Fri, 31 Dec 1982 01:35:50 -0800 Subject: [PATCH] finally works (updated for 4.1c and merged with sun) SCCS-vsn: bin/csh/exec.c 4.5 SCCS-vsn: bin/csh/func.c 4.7 SCCS-vsn: bin/csh/csh.h 4.5 SCCS-vsn: bin/csh/hist.c 4.4 SCCS-vsn: bin/csh/proc.c 4.7 SCCS-vsn: bin/csh/proc.h 4.2 SCCS-vsn: bin/csh/time.c 4.2 --- usr/src/bin/csh/csh.h | 16 ++--- usr/src/bin/csh/exec.c | 2 +- usr/src/bin/csh/func.c | 55 ++++++++-------- usr/src/bin/csh/hist.c | 4 +- usr/src/bin/csh/proc.c | 51 ++++----------- usr/src/bin/csh/proc.h | 6 +- usr/src/bin/csh/time.c | 143 +++++++++++++++++++++-------------------- 7 files changed, 122 insertions(+), 155 deletions(-) diff --git a/usr/src/bin/csh/csh.h b/usr/src/bin/csh/csh.h index 5bd3859b4e..3f40060752 100644 --- a/usr/src/bin/csh/csh.h +++ b/usr/src/bin/csh/csh.h @@ -1,9 +1,8 @@ -/* csh.h 4.4 %G% */ +/* csh.h 4.5 82/12/30 */ #include "sh.local.h" -#ifdef VMUNIX -#include -#endif +#include +#include /* * C shell @@ -59,6 +58,7 @@ char *err; /* Error message from scanner/parser */ int errno; /* Error from C library routines */ char *shtemp; /* Temp name for << shell files in /tmp */ time_t time0; /* Time at which the shell started */ +struct rusage ru0; /* * Miscellany @@ -199,11 +199,7 @@ struct wordent { * process id's from `$$', and modified variable values (from qualifiers * during expansion in sh.dol.c) here. */ -#ifdef VMUNIX char labuf[BUFSIZ]; -#else -char labuf[256]; -#endif char *lap; @@ -437,10 +433,6 @@ char **Vav; char *Vdp; #endif -#ifdef VMUNIX -struct vtimes zvms; -#endif - char **evalvec; char *evalp; diff --git a/usr/src/bin/csh/exec.c b/usr/src/bin/csh/exec.c index 488dbc3ee6..1040e3e3cd 100644 --- a/usr/src/bin/csh/exec.c +++ b/usr/src/bin/csh/exec.c @@ -1,4 +1,4 @@ -static char *sccsid = "@(#)exec.c 4.4 %G%"; +static char *sccsid = "@(#)exec.c 4.5 %G%"; #include "sh.h" #include diff --git a/usr/src/bin/csh/func.c b/usr/src/bin/csh/func.c index 87f35cd860..0646e0980a 100644 --- a/usr/src/bin/csh/func.c +++ b/usr/src/bin/csh/func.c @@ -1,4 +1,4 @@ -static char *sccsid = "@(#)func.c 4.6 81/08/20"; +static char *sccsid = "@(#)func.c 4.7 82/12/30"; #include "sh.h" #include @@ -820,7 +820,6 @@ doumask(v) umask(i); } -#include struct limits { int limconst; @@ -828,13 +827,12 @@ struct limits { int limdiv; char *limscale; } limits[] = { - LIM_NORAISE, "noraise", 1, "", - LIM_CPU, "cputime", 1, "seconds", - LIM_FSIZE, "filesize", 1024, "kbytes", - LIM_DATA, "datasize", 1024, "kbytes", - LIM_STACK, "stacksize", 1024, "kbytes", - LIM_CORE, "coredumpsize", 1024, "kbytes", - LIM_MAXRSS, "memoryuse", 1024, "kbytes", + RLIMIT_CPU, "cputime", 1, "seconds", + RLIMIT_FSIZE, "filesize", 1024, "kbytes", + RLIMIT_DATA, "datasize", 1024, "kbytes", + RLIMIT_STACK, "stacksize", 1024, "kbytes", + RLIMIT_CORE, "coredumpsize", 1024, "kbytes", + RLIMIT_RSS, "memoryuse", 1024, "kbytes", -1, 0, }; @@ -866,8 +864,6 @@ dolimit(v) if (*v == 0) { for (lp = limits+1; lp->limconst >= 0; lp++) plim(lp); - if (vlimit(LIM_NORAISE, -1) && getuid()) - printf("Limits cannot be raised\n"); return; } lp = findlim(v[0]); @@ -895,30 +891,28 @@ getval(lp, v) return ((int)(f+0.5) * lp->limdiv); cp = *v; } - if (lp->limconst == LIM_NORAISE) - goto badscal; switch (*cp) { case ':': - if (lp->limconst != LIM_CPU) + if (lp->limconst != RLIMIT_CPU) goto badscal; return ((int)(f * 60.0 + atof(cp+1))); case 'h': - if (lp->limconst != LIM_CPU) + if (lp->limconst != RLIMIT_CPU) goto badscal; limtail(cp, "hours"); f *= 3600.; break; case 'm': - if (lp->limconst == LIM_CPU) { + if (lp->limconst == RLIMIT_CPU) { limtail(cp, "minutes"); f *= 60.; break; } case 'M': - if (lp->limconst == LIM_CPU) + if (lp->limconst == RLIMIT_CPU) goto badscal; *cp = 'm'; limtail(cp, "megabytes"); @@ -926,13 +920,13 @@ getval(lp, v) break; case 's': - if (lp->limconst != LIM_CPU) + if (lp->limconst != RLIMIT_CPU) goto badscal; limtail(cp, "seconds"); break; case 'k': - if (lp->limconst == LIM_CPU) + if (lp->limconst == RLIMIT_CPU) goto badscal; limtail(cp, "kbytes"); f *= 1024; @@ -940,7 +934,7 @@ getval(lp, v) case 'u': limtail(cp, "unlimited"); - return (INFINITY); + return (RLIM_INFINITY); default: badscal: @@ -963,16 +957,16 @@ limtail(cp, str0) plim(lp) register struct limits *lp; { - register int lim; + struct rlimit rlim; printf("%s \t", lp->limname); - lim = vlimit(lp->limconst, -1); - if (lim == INFINITY) + getrlimit(lp->limconst, &rlim); + if (rlim.rlim_cur == RLIM_INFINITY) printf("unlimited"); - else if (lp->limconst == LIM_CPU) - psecs((long)lim); + else if (lp->limconst == RLIMIT_CPU) + psecs((long)rlim.rlim_cur); else - printf("%d %s", lim / lp->limdiv, lp->limscale); + printf("%d %s", rlim.rlim_cur / lp->limdiv, lp->limscale); printf("\n"); } @@ -984,20 +978,23 @@ dounlimit(v) v++; if (*v == 0) { for (lp = limits+1; lp->limconst >= 0; lp++) - setlim(lp, INFINITY); + setlim(lp, RLIM_INFINITY); return; } while (*v) { lp = findlim(*v++); - setlim(lp, INFINITY); + setlim(lp, RLIM_INFINITY); } } setlim(lp, limit) register struct limits *lp; { + struct rlimit rlim; - if (vlimit(lp->limconst, limit) < 0) + getrlimit(lp->limconst, &rlim); + rlim.rlim_cur = limit; + if (setrlimit(lp->limconst, &rlim) < 0) Perror(bname); } diff --git a/usr/src/bin/csh/hist.c b/usr/src/bin/csh/hist.c index 5d8a379217..8a68351558 100644 --- a/usr/src/bin/csh/hist.c +++ b/usr/src/bin/csh/hist.c @@ -1,4 +1,4 @@ -static char *sccsid = "@(#)hist.c 4.3 %G%"; +static char *sccsid = "@(#)hist.c 4.4 %G%"; #include "sh.h" @@ -75,7 +75,7 @@ dohist(vp) if (setintr) sigrelse(SIGINT); vp++; - while (*vp[0] == '-') { + while (*vp && *vp[0] == '-') { if (*vp && eq(*vp, "-h")) { hflg++; vp++; diff --git a/usr/src/bin/csh/proc.c b/usr/src/bin/csh/proc.c index fdf3b69401..b92a066f83 100644 --- a/usr/src/bin/csh/proc.c +++ b/usr/src/bin/csh/proc.c @@ -1,4 +1,4 @@ -static char *sccsid = "@(#)proc.c 4.6 (Berkeley) 81/05/03"; +static char *sccsid = "@(#)proc.c 4.7 (Berkeley) 82/12/30"; #include "sh.h" #include "sh.dir.h" @@ -26,19 +26,13 @@ pchild() register int pid; union wait w; int jobflags; -#ifdef VMUNIX - struct vtimes vt; -#endif + struct rusage ru; if (!timesdone) timesdone++, times(&shtimes); loop: pid = wait3(&w.w_status, (setintr ? WNOHANG|WUNTRACED:WNOHANG), -#ifndef VMUNIX - 0); -#else - &vt); -#endif + &ru); if (pid <= 0) { if (errno == EINTR) { errno = 0; @@ -69,9 +63,7 @@ found: pp->p_stime = shtimes.tms_cstime - oldcstimes; } else times(&shtimes); -#ifdef VMUNIX - pp->p_vtimes = vt; -#endif + pp->p_rusage = ru; if (WIFSIGNALED(w)) { if (w.w_termsig == SIGINT) pp->p_flags |= PINTERRUPTED; @@ -82,11 +74,7 @@ found: pp->p_reason = w.w_termsig; } else { pp->p_reason = w.w_retcode; -#ifdef IIASA - if (pp->p_reason >= 3) -#else if (pp->p_reason != 0) -#endif pp->p_flags |= PAEXITED; else pp->p_flags |= PNEXITED; @@ -632,11 +620,10 @@ prcomd: if (pp->p_flags&PPTIME && !(status&(PSTOPPED|PRUNNING))) { if (linp != linbuf) printf("\n\t"); -#ifndef VMUNIX - ptimes(pp->p_utime, pp->p_stime, pp->p_etime-pp->p_btime); -#else - pvtimes(&zvms, &pp->p_vtimes, pp->p_etime - pp->p_btime); -#endif + { static struct rusage zru; + prusage(&zru, &pp->p_rusage, + pp->p_etime - pp->p_btime); + } } if (tp == pp->p_friends) { if (linp != linbuf) @@ -660,29 +647,17 @@ ptprint(tp) register struct process *tp; { time_t tetime = 0; -#ifdef VMUNIX - struct vtimes vmt; -#else - time_t tutime = 0, tstime = 0; -#endif + struct rusage ru; + static struct rusage zru; register struct process *pp = tp; - vmt = zvms; + ru = zru; do { -#ifdef VMUNIX - vmsadd(&vmt, &pp->p_vtimes); -#else - tutime += pp->p_utime; - tstime += pp->p_stime; -#endif + ruadd(&ru, &pp->p_rusage); if (pp->p_etime - pp->p_btime > tetime) tetime = pp->p_etime - pp->p_btime; } while ((pp = pp->p_friends) != tp); -#ifdef VMUNIX - pvtimes(&zvms, &vmt, tetime); -#else - ptimes(tutime, tstime, tetime); -#endif + prusage(&zru, &ru, tetime); } /* diff --git a/usr/src/bin/csh/proc.h b/usr/src/bin/csh/proc.h index 8522eaa810..0cb12a4902 100644 --- a/usr/src/bin/csh/proc.h +++ b/usr/src/bin/csh/proc.h @@ -1,4 +1,4 @@ -/* proc.h 4.1 %G% */ +/* proc.h 4.2 %G% */ /* * C shell - process structure declarations @@ -26,9 +26,7 @@ struct process { time_t p_etime; /* end time */ long p_stime; /* system cpu time */ long p_utime; /* user cpu time */ -#ifdef VMUNIX - struct vtimes p_vtimes; -#endif + struct rusage p_rusage; char *p_command; /* first PMAXLEN chars of command */ }; diff --git a/usr/src/bin/csh/time.c b/usr/src/bin/csh/time.c index f53da34a5b..e9ed8b1373 100644 --- a/usr/src/bin/csh/time.c +++ b/usr/src/bin/csh/time.c @@ -1,26 +1,21 @@ -static char *sccsid = "@(#)time.c 4.1 %G%"; +/* time.c 4.2 82/12/30 */ #include "sh.h" /* * C Shell - routines handling process timing and niceing */ -#ifdef VMUNIX -struct vtimes vm0; -#else struct tms times0; struct tms timesdol; -#endif settimes() { + struct rusage ruch; time(&time0); -#ifdef VMUNIX - vtimes(&vm0, 0); -#else - times(×0); -#endif + getrusage(RUSAGE_SELF, &ru0); + getrusage(RUSAGE_CHILDREN, &ruch); + ruadd(&ru0, &ruch); } /* @@ -30,20 +25,13 @@ settimes() dotime() { time_t timedol; -#ifdef VMUNIX - struct vtimes vm1, vmch; - - vtimes(&vm1, &vmch); - vmsadd(&vm1, &vmch); -#endif + struct rusage ru1, ruch; + getrusage(RUSAGE_SELF, &ru1); + getrusage(RUSAGE_CHILDREN, &ruch); + ruadd(&ru1, &ruch); time(&timedol); -#ifdef VMUNIX - pvtimes(&vm0, &vm1, timedol - time0); -#else - times(×dol); - ptimes(timedol - time0, ×0, ×dol); -#endif + prusage(&ru0, &ru1, timedol - time0); } /* @@ -70,44 +58,32 @@ donice(v) } } -#ifndef VMUNIX -ptimes(utime, stime, etime) - register time_t utime, stime, etime; -{ - - p60ths(utime); - printf("u "); - p60ths(stime); - printf("s "); - psecs(etime); - printf(" %d%%\n", (int) (100 * (utime+stime) / - (60 * (etime ? etime : 1)))); -} - -#else -vmsadd(vp, wp) - register struct vtimes *vp, *wp; +ruadd(ru, ru2) + register struct rusage *ru, *ru2; { - - vp->vm_utime += wp->vm_utime; - vp->vm_stime += wp->vm_stime; - vp->vm_nswap += wp->vm_nswap; - vp->vm_idsrss += wp->vm_idsrss; - vp->vm_ixrss += wp->vm_ixrss; - if (vp->vm_maxrss < wp->vm_maxrss) - vp->vm_maxrss = wp->vm_maxrss; - vp->vm_majflt += wp->vm_majflt; - vp->vm_minflt += wp->vm_minflt; - vp->vm_inblk += wp->vm_inblk; - vp->vm_oublk += wp->vm_oublk; + register long *lp, *lp2; + register int cnt; + + tvadd(&ru->ru_utime, &ru2->ru_utime); + tvadd(&ru->ru_stime, &ru2->ru_stime); + if (ru2->ru_maxrss > ru->ru_maxrss) + ru->ru_maxrss = ru2->ru_maxrss; + cnt = &ru->ru_last - &ru->ru_first + 1; + lp = &ru->ru_first; lp2 = &ru2->ru_first; + do + *lp++ += *lp2++; + while (--cnt > 0); } -pvtimes(v0, v1, sec) - register struct vtimes *v0, *v1; +prusage(r0, r1, sec) + register struct rusage *r0, *r1; time_t sec; { register time_t t = - (v1->vm_utime-v0->vm_utime)+(v1->vm_stime-v0->vm_stime); + (r1->ru_utime.tv_sec-r0->ru_utime.tv_sec)*100+ + (r1->ru_utime.tv_usec-r0->ru_utime.tv_usec)/10000+ + (r1->ru_stime.tv_sec-r0->ru_stime.tv_sec)*100+ + (r1->ru_stime.tv_usec-r0->ru_stime.tv_usec)/10000; register char *cp; register int i; register struct varent *vp = adrof("time"); @@ -121,11 +97,11 @@ pvtimes(v0, v1, sec) else if (cp[1]) switch(*++cp) { case 'U': - p60ths(v1->vm_utime - v0->vm_utime); + pdeltat(&r1->ru_utime, &r0->ru_utime); break; case 'S': - p60ths(v1->vm_stime - v0->vm_stime); + pdeltat(&r1->ru_stime, &r0->ru_stime); break; case 'E': @@ -133,48 +109,77 @@ pvtimes(v0, v1, sec) break; case 'P': - printf("%d%%", (int) ((100 * t) / (60 * (sec ? sec : 1)))); + printf("%d%%", (int) (t / ((sec ? sec : 1)))); break; case 'W': - i = v1->vm_nswap - v0->vm_nswap; + i = r1->ru_nswap - r0->ru_nswap; printf("%d", i); break; case 'X': - printf("%d", t == 0 ? 0 : (v1->vm_ixrss-v0->vm_ixrss)/(2*t)); + printf("%d", t == 0 ? 0 : (r1->ru_ixrss-r0->ru_ixrss)/t); break; case 'D': - printf("%d", t == 0 ? 0 : (v1->vm_idsrss-v0->vm_idsrss)/(2*t)); + printf("%d", t == 0 ? 0 : + (r1->ru_idrss+r1->ru_isrss-(r0->ru_idrss+r0->ru_isrss))/t); break; case 'K': - printf("%d", t == 0 ? 0 : ((v1->vm_ixrss+v1->vm_idsrss) - - (v0->vm_ixrss+v0->vm_idsrss))/(2*t)); + printf("%d", t == 0 ? 0 : + ((r1->ru_ixrss+r1->ru_isrss+r1->ru_idrss) - + (r0->ru_ixrss+r0->ru_idrss+r0->ru_isrss))/t); break; case 'M': - printf("%d", v1->vm_maxrss/2); + printf("%d", r1->ru_maxrss/2); break; case 'F': - printf("%d", v1->vm_majflt-v0->vm_majflt); + printf("%d", r1->ru_majflt-r0->ru_majflt); break; case 'R': - printf("%d", v1->vm_minflt-v0->vm_minflt); + printf("%d", r1->ru_minflt-r0->ru_minflt); break; case 'I': - printf("%d", v1->vm_inblk-v0->vm_inblk); + printf("%d", r1->ru_inblock-r0->ru_inblock); break; case 'O': - printf("%d", v1->vm_oublk-v0->vm_oublk); + printf("%d", r1->ru_oublock-r0->ru_oublock); break; - } putchar('\n'); } -#endif + +pdeltat(t1, t0) + struct timeval *t1, *t0; +{ + struct timeval td; + + tvsub(&td, t1, t0); + printf("%d.%01d", td.tv_sec, td.tv_usec/100000); +} + +tvadd(tsum, t0) + struct timeval *tsum, *t0; +{ + + tsum->tv_sec += t0->tv_sec; + tsum->tv_usec += t0->tv_usec; + if (tsum->tv_usec > 1000000) + tsum->tv_sec++, tsum->tv_usec -= 1000000; +} + +tvsub(tdiff, t1, t0) + struct timeval *tdiff, *t1, *t0; +{ + + tdiff->tv_sec = t1->tv_sec - t0->tv_sec; + tdiff->tv_usec = t1->tv_usec - t0->tv_usec; + if (tdiff->tv_usec < 0) + tdiff->tv_sec--, tdiff->tv_usec += 1000000; +} -- 2.20.1