From: hculver@ops-1.naitc.com (Henry Culver)
Subject: determining swap space programatically
Summary: how do I determine swap space programatically?
Keywords: swap nlist kernel
Organization: AC Nielsen Co.
Date:  9 Oct 92 15:01:41 GMT


Can anyone tell me how to determine swap space utilization programatically.
I'm working on a version of pstat for an hp817 under HP-UX 8.02 and am 
having trouble getting numbers which are reasonable.  

I have configured a device for swapping (/dev/dsk/c0d0s4) and a
directory on an existing file-system for swapping.

/system/TOOL/swapinfo reports:

**********
device         107426  /dev/dsk/c0d0s4
file-system   1048576  /usr/develop3/swapdir
total         1156002
**********

and /etc/swapon -a reports:

**********
Adding /dev/dsk/c1d0s4 as swap device
swapon: /dev/dsk/c1d0s4 already enabled for swap

Adding /usr/develop3/swapdir as swap filesystem,
        minimum=0, limit=131072, reserve=0 and priority=1
swapon: /usr/develop3/swapdir already enabled for swap
**********

The 107426 number from swapinfo is easilly verified using sam, but the 
1048576 number seems very wrong, (unless of course it is reporting
roughly the entire file system) 

The filesystem swap space was not added using sam.  Moreover sam seems
to know nothing about it.

OK, so its possible I'm not interpretting the man page for swapon correctly
and am specifying an incorrect limit.  This I can live with for the moment.
(by the way my /etc/checklist entry looks like:
	default       /usr/develop3/swapdir   swapfs  min=0,lim=131072 0 0
)

I assumed the maximum number of blocks specified in the manual page 
referred to blocks which are the block-size of the file-system (which
is 1024).  So I thought this would be roughly 130M.

Anyway, getting to the problem...

Working with little or no documentation (standard stupidity disclaimer) 
doing an nlist on "swaptab" and a read on the returned value into
a structure "swaptab" (from swap.h) and further dereferencing
the st_dev entry and using this for and additional read of /dev/kmem
into a structure of type swdevt (from conf.h), printing swdevt.sw_nblks
reports 107426! Wonderful!  However, dereferencing the st_fsp pointer
in the original structure gives me a NULL pointer, not what I expected.

I have nlisted just about every symbol in the kernel containing the
word swap including swaptab nswapdev nswapfs swdevt fswdevt nswap
and probably a few others.

Interesting things I've found:

1) nlisting swdevt saves me the first step (nlisting swaptab and dereferencing
st_dev for another /dev/kmem read)  

2) reading /dev/kmem several times from &swaptab for sizeof(struct swaptab)
returns the same information, over and over (this table seems to be filled
with the same entry over and over referring to the swap device "c1d0s4")

Either 2 or HP-UX fills every unused integer in the kernel with 107426

From the HP support line, while searching the problem database, I found
a reference which stated that using adb on the kernel and printing the
value of swapspc_cnt and swapspc_max would yield numbers which
represent the amount of swap space in use in 4k pages.  These numbers
seem to be completely out of left field.  If I new absolutely nothing
about the available swap space, they are in the right ballpark, but
if I had to guess, I'd say they more closely reflect the amount of 
physical memory in the machine (wild guess).

Help!
-Henry
(hculver@nis.naitc.com)
-- 
Henry L. Culver                A. C. Nielsen
hculver@nis.naitc.com       
Phone: (708) 317 3459