[comp.unix.ultrix] how do you write a program to determine free swap

simpsong@NCoast.ORG (Gregory R. Simpson @ The North Coast) (11/06/90)

Hello, the Subject line pretty much sums it up. I'm using a
network of DecStation 3100's and DecStation 5000's to do
ray tracing simulations that are very memory intensive.
I automatically parallel process the job across the entire
network, but in order to do so I have to:

rsh node /etc/pstat -s | tail -1 | awk ...

on all my nodes to determine if there is enough virtual
memory available on the machine to kick off the job...

I'd like to write a simple vm_check program that returns
nothing except the amount of swap remaining... (which is
all I'm interested in...

I tried man -k mem, swap, get, and I scrolled through
xman with no success...

Any hints, code segements, include files to look
at etc. would be appreciated...

Thanks,

Greg


-- 
Gregory R. Simpson
GE Lighting  (216)-266-6506

simpsong@ltd.ge.com
or 
uunet!crd.ge.com!ltd.decnet!simpsong

mogul@wrl.dec.com (Jeffrey Mogul) (11/07/90)

In article <1990Nov6.033516.18772@NCoast.ORG> simpsong@NCoast.ORG (Gregory R. Simpson @ The North Coast) writes:
>I'd like to write a simple vm_check program that returns
>nothing except the amount of swap remaining... (which is
>all I'm interested in...

As you might have guessed from the output of /etc/pstat -s,
there isn't simple value stored in the kernel that says "this
much swap space is in use"; rather, pstat grovels through
kernel data structures counting things up, and computes
a total through a fairly hairy computation.  I counted over
160 lines in pstat just to do what you're asking for.

Since the Ultrix version of pstat.c carries the usual Digital/AT&T
copyright, I can't post that on the net.  If you get a 4.2BSD
source tape, I suspect that the code to do this calculation hasn't
changed all that much, so perhaps starting with that version of
pstat.c, you can create a program to do what you want.

-Jeff

alan@shodha.enet.dec.com ( Alan's Home for Wayward Notes File.) (11/07/90)

In article <1990Nov7.015529.15623@wrl.dec.com>, mogul@wrl.dec.com (Jeffrey Mogul) writes:
> In article <1990Nov6.033516.18772@NCoast.ORG> simpsong@NCoast.ORG (Gregory R. Simpson @ The North Coast) writes:
> >I'd like to write a simple vm_check program that returns
> >nothing except the amount of swap remaining... (which is
> >all I'm interested in...
> 
> As you might have guessed from the output of /etc/pstat -s,
> there isn't simple value stored in the kernel that says "this
> much swap space is in use"; rather, pstat grovels through
> kernel data structures counting things up, and computes
> a total through a fairly hairy computation.  I counted over
> 160 lines in pstat just to do what you're asking for.

	Actually it depends on what version of ULTRIX you're
	running.  In V3.1 and earlier.  It's the really hairy
	stuff that Jeff mentioned.  In V4.0 there is a data
	structure called "swapu" that has how much is being
	used for various things.  The amount free is just the
	total - the amount used.  You can get total from adding
	amount indicated in the "swdevt" table.
> 
> Since the Ultrix version of pstat.c carries the usual Digital/AT&T
> copyright, I can't post that on the net.  If you get a 4.2BSD
> source tape, I suspect that the code to do this calculation hasn't
> changed all that much, so perhaps starting with that version of
> pstat.c, you can create a program to do what you want.
> 

	In general such things look something like:

/*
 *	Correctly dealing with errors, counting up the amount of
 *	page/swap space and getting the units right is left as an 
 *	exercise to the reader.
 */
#include <sys/types.h>
#include <sys/file.h>
#include <sys/dmap.h>

#include <nlist.h>

void	exit(), nlist() ;
off_t	lseek() ;

struct nlist nl[] = {
	{ "_swapu" },
#define	NM_SWAPU	0
	{ 0 },
};

main()
{
	register rc, fd ;
	struct swapu_t data ;

	nlist("/vmunix", nl) ;

	if((fd = open("/dev/kmem", O_RDONLY)) == -1 )
		exit(-1) ;

	if( lseek(fd, (off_t)nl[NM_SWAPU].n_value, L_SET) == -1 )
		exit(-1) ;

	if( read(fd, (char *)&data, sizeof(data)) == -1 )
		exit(-1) ;

	printf("%d\n", data.total_used) ;
	printf("%d\n", data.wasted) ;

	if( close(fd) == -1 )
		exit(-1) ;

	return 0 ;
}

	You may season to taste.

> -Jeff


-- 
Alan Rollow				alan@nabeth.enet.dec.com

karrer@ethz.UUCP (Andreas Karrer) (11/07/90)

alan@shodha.enet.dec.com ( Alan's Home for Wayward Notes File.) writes:

>In article <1990Nov7.015529.15623@wrl.dec.com>, mogul@wrl.dec.com (Jeffrey Mogul) writes:
>> In article <1990Nov6.033516.18772@NCoast.ORG> simpsong@NCoast.ORG (Gregory R. Simpson @ The North Coast) writes:
>> >I'd like to write a simple vm_check program that returns
>> >nothing except the amount of swap remaining... (which is
>> >all I'm interested in...
>> 
>> As you might have guessed from the output of /etc/pstat -s,
>> there isn't simple value stored in the kernel that says "this
>> much swap space is in use"; rather, pstat grovels through
>> kernel data structures counting things up, and computes
>> a total through a fairly hairy computation.  I counted over
>> 160 lines in pstat just to do what you're asking for.

>	Actually it depends on what version of ULTRIX you're
>	running.  In V3.1 and earlier.  It's the really hairy
>	stuff that Jeff mentioned.  In V4.0 there is a data
>	structure called "swapu" that has how much is being
>	used for various things.  The amount free is just the
>	total - the amount used.  You can get total from adding
>	amount indicated in the "swdevt" table.

[... source for simplified pstat -s omitted ...]

Thanks for this piece of code. (on Ultrix4.0) it returns the amount of
used and wasted swap space using less than 0.1 sec cpu time (in good ol'
512 byte blocks). Please compare: "/bin/time /etc/pstat -s":

Machine       Ultrix V  swap space   cpu sec
VAX 11/785        3.1      66 M      2.4 - well, a 785...
DECstation 5000   4.0      65        2.3 - hmm...
DECsystem 5400    4.0     122        3.8 - qualifies as "fairly hairy"
DECsystem 5820    4.0     127        3.8 - ditto
DECsystem 5840    4.0     156        6.1 - !!! outrageous !!!

it seems the faster the machine, the slower is pstat -s  :-(

+-----------
  Andi Karrer, Communication Systems, ETH Zuerich, Switzerland
  karrer@ks.id.ethz.ch                  karrer@czheth5a.bitnet