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

From: Manish Baronia (manishbaronia_at_hotmail.com)
Date: 08/26/03

  • Next message: krp: "Common API"
    Date: Tue, 26 Aug 2003 10:04:05 +0530
    
    

    Mike Stroyan wrote:

    > 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

    This works. Thanks for your help.
    Manish


  • Next message: krp: "Common API"

    Relevant Pages

    • Re: [Lit.] Buffer overruns
      ... It fails when the values are equal. ... > The correct form of swap does not fail in that manner. ... the original coding since it has less memory references. ...
      (sci.crypt)
    • Re: swap() function without tmp
      ... This fails if x and y point to the same int object. ... It can also fail if type int has trap representations. ... If your goal is to swap two variables, ...
      (comp.lang.c)
    • Re: [Lit.] Buffer overruns
      ... It fails when the values are ... The correct form of swap does not fail in that manner. ... If a religion is defined to be a system of ideas that contains unprovable ...
      (sci.crypt)
    • Replace Hard Drive in Compaq Presario 2700?
      ... been making some very strange noises recently, and so I have purchased a ... replacement before it fails completely and would like to swap it over. ...
      (comp.sys.laptops)
    • Re: [Lit.] Buffer overruns
      ... It fails when the values are ... The correct form of swap does not fail in that manner. ... remove text between _ marks to respond via e-mail ...
      (sci.crypt)