Calculating "pstat -s" info from C (SunOS)

Calculating "pstat -s" info from C (SunOS)

Post by Joe Heal » Tue, 24 May 1994 04:33:31



IN Article 47860 of comp.unix.questions:

Quote:

>What's a more efficient way to do (the equivalent of) this:

>system("pstat -s");

>SunOS and HPUX are my primary platforms of interest.

>Thanks for your time.

>-kin

The following script shows a simple, non-robust 46 line C program that
calculates the same info that "pstat -s" does on SunOS 4.1.3.

Hope this helps.

jth

---------------------script----------------------------
Script started on Sun May 22 15:22:58 1994
dspsim1% uname -a
SunOS dspsim1 4.1.3 3 sun4m
dspsim1% pstat -s
5712k allocated + 2904k reserved = 8616k used, 294276k available
dspsim1% cat swap.c
#include <stdio.h>
#include <sys/param.h>
#include <kvm.h>
#include <fcntl.h>
#include <nlist.h>
#include <vm/anon.h>

struct  anoninfo ainfo;  
#define ctok(x) ((ctob(x))>>10)

struct nlist nl[] = {
#define ANONINFO 0
    {"_anoninfo"},
    {""}

Quote:};

static  kvm_t   *kd;        /* magic cookie to pass into kvm_XXX */

main()
{
    int numalloc;

    if((kd=kvm_open(NULL,NULL,NULL,O_RDONLY,"kvm_open "))<0){
        perror("kvm_open");
        exit(1);
    }

    if(kvm_nlist(kd,nl)<0){
        perror("kvm_open");
        exit(1);
    }

    if(kvm_read(kd,nl[ANONINFO].n_value,(char *)&ainfo,sizeof ainfo)<0){
        perror("kvm_read");
        exit(1);
    }
    printf("--------------------------------\n");
    printf("running system(\"pstat -s\"):\n");
    system("pstat -s");
    printf("--------------------------------\n");
    printf("pstat -s type info:\n");
    numalloc = ctok(ainfo.ani_max - ainfo.ani_free);
    printf("\tallocated  = %u\n",numalloc);
    printf("\treserved   = %u\n",ctok(ainfo.ani_resv) - numalloc);
    printf("\tavailable  = %u\n",ctok(ainfo.ani_max - ainfo.ani_resv));
    exit(0);

Quote:}

dspsim1% cc swap.c -lkvm
dspsim1% a.out
--------------------------------
running system("pstat -s"):
5864k allocated + 2952k reserved = 8816k used, 294076k available
--------------------------------
pstat -s type info:
        allocated  = 5712
        reserved   = 2912
        available  = 294268
dspsim1% ^D  
script done on Sun May 22 15:23:40 1994

script done on Sun May 22 15:23:40 1994