From: richard@atheist.tamu.edu (Richard Henderson) Newsgroups: comp.unix.aux Subject: New top patch Date: 2 May 1995 04:48:21 GMT Organization: Texas A&M University, College Station, TX Message-ID: <3o4dil$qbh@news.tamu.edu> With the aid of a few kind souls, I've been able to produce a version of top that runs under A/UX 3.0.2. In the process I discovered a few bugs that affected all systems. In any event, I'd people with the systems that have, in the past, had the most problems with top, 3.0.x and AWS95, to give this patch a shot. The reference distribution can be found at ftp://eecs.nwu.edu/pub/top/top-3.3.tar.gz Thanks, r~ --- display.c.orig Wed Jan 4 18:11:08 1995 +++ display.c Mon May 1 23:11:49 1995 @@ -880,7 +880,7 @@ register char **names; if (thisname[0] == 'K' && num >= 10000) { /* yes: format it as megabytes, rounded */ - p = strecpy(p, itoa((num+500)/1000)); + p = strecpy(p, itoa((num+512)/1024)); /* replace 'K' with 'M' in string to display */ *p++ = 'M'; --- utils.c.orig Tue Feb 7 23:14:49 1995 +++ utils.c Mon May 1 23:12:46 1995 @@ -405,7 +405,7 @@ int amt; if (amt >= 10000) { - amt = (amt + 500) / 1000; + amt = (amt + 512) / 1024; tag = 'M'; } --- machine/m_aux31.c.orig Fri Feb 3 15:18:26 1995 +++ machine/m_aux31.c Mon May 1 23:13:14 1995 @@ -1,11 +1,10 @@ /* * top - a top users display for Unix * - * SYNOPSIS: a Mac running A/UX version 3.1 + * SYNOPSIS: a Mac running A/UX version 3.x * * DESCRIPTION: - * This is the machine-dependent module for A/UX 3.1. - * It might work on A/UX 3.0. + * This is the machine-dependent module for A/UX 3.0 and 3.1 * == * Although AUX does not generally have a renice systemcall, it can be * implemented by tweeking kernel memory. While such a simple hack should @@ -25,6 +24,7 @@ #include #include #include +#include #include #include #include @@ -32,6 +32,7 @@ #include #include #include +#include #define FSCALE 65536.0 @@ -47,22 +48,13 @@ #define X_MAXMEM 3 #define X_FREEMEM 4 #define X_AVAILRMEM 5 -#define X_AVAILSMEM 6 - -static struct nlist nlst[] = { - {"v"}, - {"sysinfo"}, - {"avenrun"}, - {"maxmem"}, - {"freemem"}, - {0}, /* "availrmem" */ - {0}, /* "availsmem" */ - {0} -}; +#define NUMNAMES 6 static int kmem; static int mem; +static struct nlist nlst[NUMNAMES]; + static struct var v; static int maxmem; @@ -72,7 +64,6 @@ static int maxmem; #define MAXMEM_OFS (nlst[X_MAXMEM].n_value) #define FREEMEM_OFS (nlst[X_FREEMEM].n_value) #define AVAILRMEM_OFS (nlst[X_AVAILRMEM].n_value) -#define AVAILSMEM_OFS (nlst[X_AVAILSMEM].n_value) /*=SYSTEM STATE INFO====================================================*/ @@ -128,7 +119,7 @@ static char *cpustatenames[] = static char *memorynames[] = { - "K real, ", "K free, ", "K free swap, ", "K locked", + "K mem avail, ", "K free, ", "K locked, ", "K swap free, ", "K used", NULL }; @@ -155,8 +146,12 @@ machine_init(statics) } /* get the list of symbols we want to access in the kernel */ + strcpy(nlst[X_V].n_name, "v"); + strcpy(nlst[X_SYSINFO].n_name, "sysinfo"); + strcpy(nlst[X_AVENRUN].n_name, "avenrun"); + strcpy(nlst[X_MAXMEM].n_name, "maxmem"); + strcpy(nlst[X_FREEMEM].n_name, "freemem"); nlst[X_AVAILRMEM].n_nptr = "availrmem"; - nlst[X_AVAILSMEM].n_nptr = "availsmem"; if (nlist("/unix", nlst) < 0) { @@ -202,7 +197,7 @@ get_system_info(info) { load_avg ar[3]; - (void)getkval(AVENRUN_OFS, (char *)&ar, sizeof(ar), "avenrun"); + (void)getkval(AVENRUN_OFS, (char *)&ar[0], sizeof(ar), "avenrun"); /* convert load averages to doubles */ info->load_avg[0] = loaddouble(ar[0]); @@ -222,19 +217,35 @@ get_system_info(info) /* get memory usage information */ { - int freemem, availrmem, availsmem; + int freemem, availrmem, totalswap, freeswap; (void)getkval(FREEMEM_OFS, (char *)&freemem, sizeof(freemem), "freemem"); (void)getkval(AVAILRMEM_OFS, (char *)&availrmem, sizeof(availrmem), "availrmem"); - (void)getkval(AVAILSMEM_OFS, (char *)&availsmem, sizeof(availsmem), - "availsmem"); + + { + swpi_t swpi; + swpt_t swpt[MSFILES]; + int i; + + swpi.si_cmd = SI_LIST; + swpi.si_buf = (char *)&swpt[0]; + (void)syscall(SYS_sysm68k_1, 3, &swpi); + + for (i = totalswap = freeswap = 0; i < MSFILES; i++) + if (swpt[i].st_dev != 0 && !(swpt[i].st_flags & ST_INDEL)) + { + totalswap += swpt[i].st_npgs; + freeswap += swpt[i].st_nfpgs; + } + } memory_stats[0] = pagetok(availrmem - freemem); memory_stats[1] = pagetok(freemem); - memory_stats[2] = pagetok(availsmem - availrmem); - memory_stats[3] = pagetok(maxmem - availrmem); + memory_stats[2] = pagetok(maxmem - availrmem); + memory_stats[3] = pagetok(freeswap); + memory_stats[4] = pagetok(totalswap - freeswap); } info->last_pid = -1; @@ -259,7 +270,7 @@ get_process_info(si, sel, compare) int show_idle, show_system, show_uid, show_command; /* calculate the time difference since our last check */ - gettimeofday(&thistime); + gettimeofday(&thistime, NULL); if (lasttime.tv_sec) timediff = ((thistime.tv_sec - lasttime.tv_sec) + (thistime.tv_usec - lasttime.tv_usec) * 1e-6); @@ -327,7 +338,7 @@ get_process_info(si, sel, compare) /* calculate relevant metrics */ percent_cpu(pp1) = 0.0; if (pp1->p_pid == pp2->p_pid) - weighted_cpu(pp1) = percent_cpu(pp2) * beta; + weighted_cpu(pp1) = weighted_cpu(pp2) * beta; else weighted_cpu(pp1) = 0.0; } @@ -348,15 +359,19 @@ get_process_info(si, sel, compare) if (pp1->p_pid == pp2->p_pid) { percent_cpu(pp1) = - (pp1->p_utime - pp2->p_utime + - pp1->p_stime - pp2->p_stime) / (v.v_hz * timediff); + (double)(pp1->p_utime - pp2->p_utime + + pp1->p_stime - pp2->p_stime) / + (double)(v.v_hz * timediff); weighted_cpu(pp1) = - percent_cpu(pp2) * beta + percent_cpu(pp1) * alpha; + percent_cpu(pp1) * alpha + weighted_cpu(pp2) * beta; } else { - weighted_cpu(pp1) = percent_cpu(pp1) = - (pp1->p_utime + pp1->p_stime) / (v.v_hz * timediff); + percent_cpu(pp1) = + (double)(pp1->p_utime + pp1->p_stime) / + (double)(v.v_hz * timediff); + weighted_cpu(pp1) = + percent_cpu(pp1) * alpha; } } @@ -600,7 +615,7 @@ setpriority(which, pid, val) errno = ENOSYS; return -1; #else - int ofs, uid; + int ofs, euid; struct proc *pp; /* sanity check arguments */ @@ -621,10 +636,10 @@ setpriority(which, pid, val) } /* make sure we don't allow nasty people to do nasty things */ - uid = getuid(); - if (uid != 0) + euid = geteuid(); + if (euid != 0) { - if (uid != pp->p_uid || val < pp->p_nice) + if ((euid != pp->p_uid && getuid() != pp->p_uid) || val < pp->p_nice) { errno = EACCES; return -1;