Operating System - HP-UX
1825803 Members
2704 Online
109687 Solutions
New Discussion

Utility for returning memory usage as both a percent and bytes

 
SOLVED
Go to solution
Alzhy
Honored Contributor

Utility for returning memory usage as both a percent and bytes

Anyone has a small utility (shell, perl or small C source) to return something like:

server# freemem
120 pages 99 percent

Thanks!
Hakuna Matata.
2 REPLIES 2
Geoff Wild
Honored Contributor

Re: Utility for returning memory usage as both a percent and bytes

Found this on the forums - called memdetail:


/*
* @(#)$Header: memdetail.c,v 1.1 2001/05/03 15:35:00 nsr1521 Exp $
*
* determine detailed current memory utilized and print.
*
* No warranty is expressed or implied.
* Your mileage may vary.
*
* Contact: srobertson@bcbs-ga.com
*
*/

static char ident[] = "@(#)$Revision: 1.1 $";

#define KB 1024

#include
#include
#include

main() {
struct pst_static sbuf;
struct pst_dynamic dbuf;
struct pst_vminfo vbuf;
double page_size, sys_mem;
double phys_mem, used_phys_mem, avail_mem;
double total_virtual_mem, active_virtual_mem, avail_virtual_mem;
double total_real_mem, active_real_mem, avail_real_mem;
double total_swap_dev, used_swap_dev, avail_swap_dev;
double total_swap_mem, used_swap_mem, avail_swap_mem;

/* get static information about the system */
if (pstat_getstatic(&sbuf, sizeof(sbuf), (size_t)1, 0) != -1)
{
phys_mem = sbuf.physical_memory;
page_size = sbuf.page_size;
}
else
perror("pstat_getstatic()");

/* get dynamic information about the system */
if (pstat_getdynamic(&dbuf, sizeof(dbuf), (size_t)1, 0) != -1)
{
avail_mem = dbuf.psd_free;
total_virtual_mem = dbuf.psd_vm;
active_virtual_mem = dbuf.psd_avm;
total_real_mem = dbuf.psd_rm;
active_real_mem = dbuf.psd_arm;
}
else
perror("pstat_getdynamic()");

/* get VM information about the system */
if (pstat_getvminfo(&vbuf, sizeof(vbuf), (size_t)1, 0) != -1)
{
sys_mem = vbuf.psv_sysmem;
total_swap_dev = vbuf.psv_swapspc_max;
avail_swap_dev = vbuf.psv_swapspc_cnt;
total_swap_mem = vbuf.psv_swapmem_max;
avail_swap_mem = vbuf.psv_swapmem_cnt;
}
else
perror("pstat_getvminfo()");

/* calculate used physical memory */
used_phys_mem = phys_mem - avail_mem;
/* calculate avail virtual memory */
avail_virtual_mem = total_virtual_mem - active_virtual_mem;
/* calculate avail real memory */
avail_real_mem = total_real_mem - active_real_mem;
/* calculate used swap on device */
used_swap_dev = total_swap_dev - avail_swap_dev;
/* calculate used swap on memory */
used_swap_mem = total_swap_mem - avail_swap_mem;

(void)printf("%-15s\t%6s\t%6s\t%6s\t%6s\n",
"Memory Stat","total","used","avail","%used");

(void)printf("%-15s\t%6.1f\t%6.1f\t%6.1f\t%5.0f%%\n",
"physical",
(phys_mem * page_size) / (KB * KB),
(used_phys_mem * page_size) / (KB * KB),
(avail_mem * page_size) / (KB * KB),
used_phys_mem * 100 / phys_mem);

(void)printf("%-15s\t%6.1f\t%6.1f\t%6.1f\t%5.0f%%\n",
"active virtual",
(total_virtual_mem * page_size) / (KB * KB),
(active_virtual_mem * page_size) / (KB * KB),
(avail_virtual_mem * page_size) / (KB * KB),
active_virtual_mem * 100 / total_virtual_mem);

(void)printf("%-15s\t%6.1f\t%6.1f\t%6.1f\t%5.0f%%\n",
"active real",
(total_real_mem * page_size) / (KB * KB),
(active_real_mem * page_size) / (KB * KB),
(avail_real_mem * page_size) / (KB * KB),
active_real_mem * 100 / total_real_mem);

(void)printf("%-15s\t%6.1f\t%6.1f\t%6.1f\t%5.0f%%\n",
"memory swap",
(total_swap_mem * page_size) / (KB * KB),
(used_swap_mem * page_size) / (KB * KB),
(avail_swap_mem * page_size) / (KB * KB),
used_swap_mem * 100 / total_swap_mem);

(void)printf("%-15s\t%6.1f\t%6.1f\t%6.1f\t%5.0f%%\n",
"device swap",
(total_swap_dev * page_size) / (KB * KB),
(used_swap_dev * page_size) / (KB * KB),
(avail_swap_dev * page_size) / (KB * KB),
used_swap_dev * 100 / total_swap_dev);

return;
}





Looks like:

# memdetail
Memory Stat total used avail %used
physical 10080.0 10019.6 60.4 99%
active virtual 13073.8 5964.9 7109.0 46%
active real 7156.5 2706.1 4450.4 38%
memory swap 7697.2 1914.8 5782.4 25%
device swap 26528.0 12792.2 13735.8 48%


Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Geoff Wild
Honored Contributor
Solution

Re: Utility for returning memory usage as both a percent and bytes

Well that post looks ugly - here it is as an attachment...

Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.