Re: C API to get Total SWAP installed on a HPUX machine

From: Mike Stroyan (stroyan_at_hpstryn.fc.hp.com)
Date: 08/25/03

  • Next message: Chuck Slivkoff: "Re: ignite display problem"
    Date: Mon, 25 Aug 2003 20:28:44 GMT
    
    

    Manish Baronia <manishbaronia@hotmail.com> wrote:
    | I want to find out total swap installed on a HP Unix machine (In
    |C/C++). For this I got following code somewhere:
    ...
    |Can someone tell me if this is the correct way to find swap on HP UX
    |machines. I have tried it on 3 HP machines. It runs fine on one and
    |fails on other two.

      You can get swap summary data from pstat_getvminfo. Here is an
    example using several pstat interfaces.

    # This is a shell archive. Remove anything before this line,
    # then unpack it by saving it in a file and typing "sh file".
    #
    # Wrapped by Mike Stroyan <stroyan@hpstryn> on Mon Aug 25 14:27:16 2003
    #
    # This archive contains:
    # system_memstat.c
    #

    LANG=""; export LANG
    PATH=/bin:/usr/bin:/usr/sbin:/usr/ccs/bin:$PATH; export PATH
    EXIT_STATUS=0

    echo x - system_memstat.c
    cat >system_memstat.c <<'@EOF'
    #define _PSTAT64
    #include <sys/param.h>
    #include <sys/pstat.h>
    #include <sys/unistd.h>
    #include <stdio.h>
    int main(int argc, char *argv[])
    {
        struct pst_static pst;
        struct pst_dynamic dyn;
        struct pst_swapinfo swp;
        struct pst_vminfo vminfo;
        long page;
        long total_free_pages;
        long reserve;
        long total_used;
        long total_avail;

        if (pstat_getstatic(&pst, sizeof(pst), (size_t)1, 0) == -1) {
            perror("pstat_getstatic");
            return 1;
        }
        if (pstat_getvminfo(&vminfo, sizeof(vminfo), (size_t)1, 0) == -1) {
            perror("pstat_getvminfo");
            return 1;
        }
        if (pstat_getdynamic(&dyn, sizeof(dyn), (size_t)1, 0) == -1) {
            perror("pstat_getdynamic");
            return 1;
        }

        printf("page size is %d bytes\n", page = pst.page_size);
        printf("physical memory size is %lld pages, %lld bytes\n",
            (long long) pst.physical_memory,
            (long long) pst.physical_memory * pst.page_size);
        printf("pst_maxmem is %lld pages, %lld bytes\n",
            (long long) pst.pst_maxmem,
            (long long) pst.pst_maxmem * pst.page_size);
        printf("pst_lotsfree is %lld pages, %lld bytes\n",
            (long long) pst.pst_lotsfree,
            (long long) pst.pst_lotsfree * pst.page_size);
        printf("pst_desfree is %lld pages, %lld bytes\n",
            (long long) pst.pst_desfree,
            (long long) pst.pst_desfree * pst.page_size);
        printf("pst_minfree is %lld pages, %lld bytes\n",
            (long long) pst.pst_minfree,
            (long long) pst.pst_minfree * pst.page_size);

        total_free_pages = 0;
        {
            int i = 0;
            while (pstat_getswap(&swp, sizeof(struct pst_swapinfo), 1, i++) == 1) {
                printf("--------------------------------------\n");
                printf("pss_swapchunk = %lld \n", (long long) swp.pss_swapchunk);
                printf("pss_nblksenabled = %lld \n",
                    (long long) swp.pss_nblksenabled);
                printf("pss_reserve = %lld \n", (long long) swp.pss_reserve);
                printf("pss_nblksavail = %lld \n", (long long) swp.pss_nblksavail);
                printf("Number of Free Swap Pages %lld. %d bytes\n",
                    (long long) swp.pss_nfpgs,
                    (long long) swp.pss_nfpgs*pst.page_size );
                printf("--------------------------------------\n\n");
                total_free_pages += swp.pss_nfpgs;
            }
        }

    #define PtoMB(pages) (((pages) * pst.page_size) / (1024.0*1024))
        printf("THIS STUFF MATCHES GLANCE.\n");
        printf("Total virtual memory %.1fmb\n", PtoMB(dyn.psd_vm));
        printf("active virtual memory %.1fmb\n", PtoMB(dyn.psd_avm));
        printf("total real memory %.1fmb\n", PtoMB(dyn.psd_rm));
        printf("active real memory %.1fmb\n", PtoMB(dyn.psd_arm));
        printf("free memory %.1fmb\n", PtoMB(dyn.psd_free));
        printf("pseudo-swap avail %.0fmb\n", PtoMB(vminfo.psv_swapmem_max));
        printf("\n");

    #define PtoKB(pages) (((pages) * pst.page_size) / 1024.0)
        printf("THIS STUFF MATCHES SWAPINFO.\n");
        printf("free on-disk backing store %.0f Kb (dev-reserve)\n",
            PtoKB(vminfo.psv_swapspc_cnt));
        printf("max on-disk backing store %.0f Kb\n",
            PtoKB(vminfo.psv_swapspc_max));
        printf("enable memory swap: %s\n",
            vminfo.psv_swapmem_on? "yes" : "no");
        printf("memory not available for memory swap %.0f Kb\n",
            PtoKB(vminfo.psv_sysmem));
        printf("free in-memory backing store %.0f Kb\n",
            PtoKB(vminfo.psv_swapmem_cnt));
        printf("max in-memory backing store %.0f Kb\n",
            PtoKB(vminfo.psv_swapmem_max));
        printf("reserved on-disk backing store %.0f Kb\n",
            PtoKB(reserve = (total_free_pages - vminfo.psv_swapspc_cnt)));
        printf("backing store overhead %.0f KB\n",
            PtoKB(vminfo.psv_swapper_mem));
        printf("paged in %.0f KB\n",
            PtoKB(vminfo.psv_spgpgin));
        printf("paged out %.0f KB\n",
            PtoKB(vminfo.psv_spgpgout));
        printf("swapped in %.0f KB\n",
            PtoKB(vminfo.psv_spswpin));
        printf("swapped out %.0f KB\n",
            PtoKB(vminfo.psv_spswpout));
        printf(" Kb Kb Kb PCT\n");
        printf("TYPE AVAIL USED FREE USED\n");
        printf("dev %8.0f%8.0f%8.0f %3.0f%%\n",
            PtoKB(vminfo.psv_swapspc_max),
            PtoKB(vminfo.psv_swapspc_max - total_free_pages),
            PtoKB(total_free_pages),
            100.0 * (vminfo.psv_swapspc_max - total_free_pages) /
                (vminfo.psv_swapspc_max));
        printf("reserve -%8.0f%8.0f\n",
            PtoKB(reserve),
            -PtoKB(reserve));
        printf("memory %8.0f%8.0f%8.0f %3.0f%%\n",
            PtoKB(vminfo.psv_swapmem_max),
            PtoKB(vminfo.psv_swapmem_max - vminfo.psv_swapmem_cnt),
            PtoKB(vminfo.psv_swapmem_cnt),
            100.0*(vminfo.psv_swapmem_max-vminfo.psv_swapmem_cnt) /
                (vminfo.psv_swapmem_max));
        printf("total %8.0f%8.0f%8.0f %3.0f%%\n",
            PtoKB(total_avail = vminfo.psv_swapspc_max + vminfo.psv_swapmem_max),
            PtoKB(total_used = total_avail - vminfo.psv_swapspc_cnt -
                  vminfo.psv_swapmem_cnt),
            PtoKB(vminfo.psv_swapspc_cnt + vminfo.psv_swapmem_cnt),
            100.0*(total_used / (double)total_avail));
        return 0;
    }
    @EOF

    chmod 600 system_memstat.c

    if [ $EXIT_STATUS -eq 1 ];then
            exit 1
    fi
    exit 0

    -- 
    Mike Stroyan, mike.stroyan@hp.com
    

  • Next message: Chuck Slivkoff: "Re: ignite display problem"

    Relevant Pages

    • Re: Why low memory will lead unstable system?
      ... 85% of the machines I worked on were Intel ... And in system someone had implemented a way to swap to ... >>swap - and for many often used things we set the 'sticky bit' so it ... VM and demand paging came until that time frame. ...
      (comp.unix.bsd.freebsd.misc)
    • Re: excessive swap-in time
      ... So when it starts causing a swap in, ... I think the speed of machines ... 24 bit color) and also SVGATextMode. ... I have ONE such video card, ...
      (comp.os.linux.development.system)
    • Re: Why is swap not de-allocating?
      ... > SuSE kernel. ... All of them have 1gig of RAM and ~2gigs of swap. ... > machines appears to behave fine in that the RAM fills quickly and swap ...
      (uk.comp.os.linux)
    • (no subject)
      ... I have three Oracle DB servers, one main and two back up. ... trouble using SWAP on the backup machines. ... total used free shared buffers cached ... I didn't' set the machines up so I'm not sure what is different. ...
      (Fedora)
    • Re: ISPs whinging about BBCs iPlayer
      ... would have been to a unix machine, ... The early ARPANET had dedicated machines called "IMPs" (Interface ...
      (uk.telecom.broadband)