Subject: Re: xosview with NetBSD 1.2G
To: Matthias Scheler <tron@lyssa.owl.de>
From: Scott Stevens <s.k.stevens@ic.ac.uk>
List: current-users
Date: 06/21/1997 17:46:35
Matthias Scheler writes:
 > 	Hi,
 > 
 > since I updated from 1.2F to 1.2G "xosview" (a nice X11 system monitor)
 > stopped working. It's probably the same problem that broke "top". Has
 > anybody patched it for use with 1.2G already?

Erm, I patched xosview-1.4.1 a little while ago... The patch was based
on the patch to top posted to this list (unfortunately I've forgotten
who).

 > 	Thanks in advance

Cheers Scott

-- 
Scott Stevens, Network Services Group, Computer Centre, Imperial College
"And did they get you to trade, Your heroes for ghosts? Hot ashes for
 trees? Hot air for a cool breeze? Cold comfort for change? And did you
 exchange a walk on part in the war for lead role in a cage ?"
                                         - Wish You Were Here, Pink Floyd

*** netbsd/swapinternal.cc.orig	Tue Jun 17 13:42:21 1997
--- netbsd/swapinternal.cc	Tue Jun 17 14:24:39 1997
***************
*** 60,70 ****
  
  #include <sys/param.h>
  #include <sys/buf.h>
- #include <sys/conf.h>
  #include <sys/ioctl.h>
- #include <sys/map.h>
  #include <sys/stat.h>
  
  #include <kvm.h>
  #include <nlist.h>
  #include <stdio.h>
--- 60,70 ----
  
  #include <sys/param.h>
  #include <sys/buf.h>
  #include <sys/ioctl.h>
  #include <sys/stat.h>
  
+ #include <vm/vm_swap.h>
+ 
  #include <kvm.h>
  #include <nlist.h>
  #include <stdio.h>
***************
*** 81,248 ****
  
  extern char *getbsize __P((int *headerlenp, long *printoutblocksizep));
  
- static kvm_t   *swap_kd;
- 
- struct nlist syms[] = {
-         { "_swapmap" }, /* list of free swap areas */
- #define VM_SWAPMAP      0
-         { "_nswapmap" },/* size of the swap map */
- #define VM_NSWAPMAP     1
-         { "_swdevt" },  /* list of swap devices and sizes */
- #define VM_SWDEVT       2
-         { "_nswap" },   /* size of largest swap device */
- #define VM_NSWAP        3
-         { "_nswdev" },  /* number of swap devices */
- #define VM_NSWDEV       4
-         { "_dmmax" },   /* maximum size of a swap block */
- #define VM_DMMAX        5
-         {0}		/* End-of-list (need {} to avoid gcc warning) */
- };
- 
- static int nswap, nswdev, dmmax, nswapmap;
- static struct swdevt *sw;
- static long *perdev;
- static struct map *swapmap, *kswapmap;
- static struct mapent *mp;
- static int nfree;
- 
- #define SVAR(var) __STRING(var) /* to force expansion */
- #define KGET(idx, var) \
-         KGET1(idx, &var, sizeof(var), SVAR(var))
- #define KGET1(idx, p, s, msg) \
-         KGET2(syms[idx].n_value, p, s, msg)
- #define KGET2(addr, p, s, msg) \
-         if (kvm_read(swap_kd, addr, p, s) != s) { \
-                 printf("cannot read %s: %s", msg, kvm_geterr(swap_kd)); \
-                 return (0); \
-         }
- 
  int
  NetBSDInitSwapInfo()
  {
!         int i;
!         char msgbuf[BUFSIZ];
!         static int once = 0;
! 	int stringlen;
! 
! 	getbsize (&stringlen, &netbsdSwapBlockSize);
!         swap_kd = kvm_open (NULL, NULL, NULL, O_RDONLY, NULL);
! 	if (!swap_kd)
! 	  err (-1, "NetBSDInitSwapInfo(): kvm_open()");
!         if (once)
!                 return (1);
!         if (kvm_nlist(swap_kd, syms)) {
!                 strcpy(msgbuf, "systat: swap: cannot find");
!                 for (i = 0; syms[i].n_name != NULL; i++) {
!                         if (syms[i].n_value == 0) {
!                                 strcat(msgbuf, " ");
!                                 strcat(msgbuf, syms[i].n_name);
!                         }
!                 }
!                 printf(msgbuf);
!                 return (0);
!         }
!         KGET(VM_NSWAP, nswap);
!         KGET(VM_NSWDEV, nswdev);
!         KGET(VM_DMMAX, dmmax);
!         KGET(VM_NSWAPMAP, nswapmap);
!         KGET(VM_SWAPMAP, kswapmap);     /* kernel `swapmap' is a pointer */
!         if ((sw = (struct swdevt*) malloc(nswdev * sizeof(*sw))) == NULL ||
!             (perdev = (long*) malloc(nswdev * sizeof(*perdev))) == NULL ||
!             (mp = (struct mapent*) malloc(nswapmap * sizeof(*mp))) == NULL) {
!                 printf("swap malloc");
!                 return (0);
!         }
!         KGET1(VM_SWDEVT, sw, (signed) (nswdev * sizeof(*sw)), "swdevt");
!         once = 1;
!         return (1);
! }
! 
! void
! fetchswap()
! {
!         int s, e, i;
!         int elast;
! 	struct mapent* localmp;
! 
! 	localmp = mp;
!         s = nswapmap * sizeof(*localmp);
!         if (kvm_read(swap_kd, (long)kswapmap, localmp, s) != s)
!                 printf("cannot read swapmap: %s", kvm_geterr(swap_kd));
! 
!         /* first entry in map is `struct map'; rest are mapent's */
!         swapmap = (struct map *)localmp;
!         if (nswapmap != swapmap->m_limit - (struct mapent *)kswapmap)
!                 printf("panic: swap: nswapmap goof");
! 
!         /*
!          * Count up swap space.
!          */
!         nfree = 0;
!         elast = 0;
!         bzero(perdev, nswdev * sizeof(*perdev));
!         for (localmp++; localmp->m_addr != 0; localmp++) {
!                 s = localmp->m_addr;                 /* start of swap region */
!                 e = localmp->m_addr + localmp->m_size;    /* end of region */
!                 elast = e;
!                 nfree += localmp->m_size;
! 
!                 /*
!                  * Swap space is split up among the configured disks.
!                  * The first dmmax blocks of swap space some from the
!                  * first disk, the next dmmax blocks from the next,
!                  * and so on.  The list of free space joins adjacent
!                  * free blocks, ignoring device boundries.  If we want
!                  * to keep track of this information per device, we'll
!                  * just have to extract it ourselves.
!                  */
! 
!                 /* calculate first device on which this falls */
!                 i = (s / dmmax) % nswdev;
!                 while (s < e) {         /* XXX this is inefficient */
!                         int bound = roundup(s + 1, dmmax);
! 
!                         if (bound > e)
!                                 bound = e;
!                         perdev[i] += bound - s;
!                         if (++i >= nswdev)
!                                 i = 0;
!                         s = bound;
!                 }
!         }
  }
  
  void
! NetBSDGetSwapInfo(int* total, int* free)
  {
!         int div, i, avail, npfree, used=0, xsize, xfree;
! 
! 	fetchswap();
!         div = netbsdSwapBlockSize / 512;
!         avail = npfree = 0;
!         for (i = 0; i < nswdev; i++) {
!                 /*
!                  * Don't report statistics for partitions which have not
!                  * yet been activated via swapon(8).
!                  */
!                 if (!sw[i].sw_freed) {
!                         printf ("Device %d not available for swapping.\n",
!                         i);
!                         continue;
!                 }
!                 xsize = sw[i].sw_nblks;
!                 xfree = perdev[i];
!                 used = xsize - xfree;
!                 npfree++;
!                 avail += xsize;
!         }
!         /*
!          * If only one partition has been set up via swapon(8), we don't
!          * need to bother with totals.
!          */
!         if (npfree > 1) {
!                 used = avail - nfree;
!         }
!         *total = avail;
!         *free = avail-used;
  }
--- 81,118 ----
  
  extern char *getbsize __P((int *headerlenp, long *printoutblocksizep));
  
  int
  NetBSDInitSwapInfo()
  {
! 	return(1);
  }
  
  void
! NetBSDGetSwapInfo(int *total, int *free)
  {
! 	struct  swapent *sep;
! 	int	totalinuse, totalsize;
! 	int     rnswap, nswap = swapctl(SWAP_NSWAP, 0, 0);
! 
! 	if (nswap < 1) {
! 		*total = *free = 0;
! 		return;
! 	}
! 
! 	sep = (struct swapent *)malloc(nswap * sizeof(*sep));
! 	if (sep == NULL)
! 		err(1, "malloc");
! 	rnswap = swapctl(SWAP_STATS, (void *)sep, nswap);
! 	if (nswap < 0)
! 		errx(1, "SWAP_STATS");
! 	if (nswap != rnswap)
! 		warnx("SWAP_STATS gave different value than SWAP_NSWAP");
! 
! 	totalsize = totalinuse = 0;
! 	for (; rnswap-- > 0; sep++) {
! 		totalsize += sep->se_nblks;
! 		totalinuse += sep->se_inuse;
! 	}
! 	*total = totalsize;
! 	*free = totalsize - totalinuse;
  }