From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: fa528,c0c22eb240492d66 X-Google-Attributes: gidfa528,public From: lar3ry@zk3.dec.com (Larry Gensch) Subject: Re: mem used by unix ? Date: 2000/08/24 Message-ID: <8o33jo$hbq$1@mailint03.im.hou.compaq.com> X-Deja-AN: 661925102 References: <8n9gbb$d1v$1@nnrp1.deja.com> To: thush2@my-deja.com Content-Type: text/plain; charset=us-ascii X-Complaints-To: usenet@mailint03.im.hou.compaq.com X-Trace: mailint03.im.hou.compaq.com 967119288 17786 16.141.0.174 (24 Aug 2000 12:14:48 GMT) Organization: Compaq Computer Corporation Mime-Version: 1.0 Reply-To: larry.gensch@compaq.com NNTP-Posting-Date: 24 Aug 2000 12:14:48 GMT Newsgroups: comp.unix.tru64 [Posted and mailed] Here's a script I wrote a while back: *** cut here *** #! /usr/bin/perl # Note that this program is used to emulate the Linux "free -tk" # command on a Tru64 System. This has only been tested on Tru64 Unix # 5.0 and 5.1. # # Written by Larry Gensch (larry.gensch@compaq.com) # Compaq Computer Corporation # "Use it, modify it, play with it, learn from it... just don't sell it." # (No warranty is given; if it breaks, you own both parts!) # # This requires perl, which is part of a Tru64 Unix system (although # it is not a "mandatory" subset, and may not be installed on all # systems). You might want to change the first line to one of the # following if you get a "no such file or directory" error when you # execute this: #! /freeware/bin/perl #! /usr/local/bin/perl $vmstat = '/usr/bin/vmstat -s'; $swapon = '/usr/sbin/swapon -s'; $vmstat_debug = 0; # Change to 1 to debug `vmstat' call $swapon_debug = 0; # Change to 1 to debug `swapon' call @hdrs = ( 'total', 'used', 'free', 'shared', 'buffers', 'cached' ); $verbose = 0; for (@ARGV) { if ($_ =~ /^-h.*$/) { &usage(); } elsif ($_ eq '-v') { @hdrs = ( 'total', 'used', 'free', '%used', '%free' ); $verbose = 1; } elsif ($_ eq '-dm') { $vmstat_debug = 1; } elsif ($_ eq '-ds') { $swapon_debug = 1; } elsif ($_ eq '-d') { $vmstat_debug = $swapon_debug = 1; } elsif ($_ eq '-tk') { @hdrs = ( 'total', 'used', 'free', 'shared', 'buffers', 'cached' ); $verbose = 0; } else { printf "Unexpected option: %s\n", $_; &usage(); } } ## # # Step 1 -- Get memory information and page size # ## open(VMSTAT,"$vmstat|") or die "$vmstat failed."; $vm_active = 0; $vm_inactive = 0; $vm_free = 0; $vm_wired = 0; while () { if ($_ =~ /^Virtual Memory Statistics/) { ($pagesize = $_) =~ s/.*pagesize = (\d+).*$/\1/; if ($pagesize_debug) { printf "Page size is <%d>\n", $pagesize; exit; } } if ($_ =~ / active pages/) { ($vm_active = $_) =~ s/^ *(\d+) .*$/\1/; } if ($_ =~ / inactive pages/) { ($vm_inactive = $_) =~ s/^ *(\d+) .*$/\1/; } if ($_ =~ / free pages/) { ($vm_free = $_) =~ s/^ *(\d+) .*$/\1/; } if ($_ =~ / wired pages/) { ($vm_wired = $_) =~ s/^ *(\d+) .*$/\1/; } } close(VMSTAT) or die 'Cannot close VMSTAT'; die 'No pagesize found!' if ($pagesize == 0); $vm_active *= $pagesize; $vm_inactive *= $pagesize; $vm_free *= $pagesize; $vm_wired *= $pagesize; $mem_used = $vm_active + $vm_inactive + $vm_wired; $mem_free = $vm_free; $mem_total = $mem_used + $mem_free; $mem_used /= 1024; $mem_free /= 1024; $mem_total /= 1024; if ($vmstat_debug) { printf "Mem total = %d\n", $mem_total; printf "Mem used = %d\n", $mem_used; printf "Mem free = %d\n", $mem_free; } ## # # Step 2 -- Get swap information # ## $swap_total = 0; $swap_used = 0; $swap_free = 0; $got_total = 0; open(SWAPON,"$swapon|") or die '$swapon failed'; while () { $got_total = 1 if (/^Total swap allocation:/); next if (!$got_total); if (/Allocated space:/) { ($swap_total = $_) =~ s/^.* (\d+) pages.*$/\1/; next; } if (/Reserved space:/) { ($swap_used = $_) =~ s/^.* (\d+) pages.*$/\1/; next; } if (/Available space:/) { ($swap_free = $_) =~ s/^.* (\d+) pages.*$/\1/; next; } } close(SWAPON) or die 'Cannot close SWAPON'; $swap_total *= $pagesize / 1024; $swap_used *= $pagesize / 1024; $swap_free *= $pagesize / 1024; if ($swapon_debug) { printf "Swap total = %d\n", $swap_total; printf "Swap used = %d\n", $swap_used; printf "Swap free = %d\n", $swap_free; } ### # # Step 3 -- Print totals # ### $tot_total = $mem_total + $swap_total; $tot_used = $mem_used + $swap_used; $tot_free = $mem_free + $swap_free; printf '%7s', ''; for (@hdrs) { printf ' %10s', $_; } printf "\n"; if ($verbose) { $col1 = percent($mem_used / $mem_total); $col2 = percent($mem_free / $mem_total); printf "%-7s %10d %10d %10d %10d %10d\n", 'Mem:', $mem_total, $mem_used, $mem_free, $col1, $col2; } else { $col1 = $col2 = 0; printf "%-7s %10d %10d %10d %10d %10d %10d\n", 'Mem:', $mem_total, $mem_used, $mem_free, 0, 0, 0; } if (!$verbose) { printf "-/+ buffers/cache: %10d %10d\n", 0, 0; } if ($verbose) { $col1 = percent($swap_used / $swap_total); $col2 = percent($swap_free / $swap_total); printf "%-7s %10d %10d %10d %10d %10d\n", 'Swap:', $swap_total, $swap_used, $swap_free, $col1, $col2; $col1 = percent($tot_used / $tot_total); $col2 = percent($tot_free / $tot_total); printf "%-7s %10d %10d %10d %10d %10d\n", 'Total:', $tot_total, $tot_used, $tot_free, $col1, $col2; } else { printf "%-7s %10d %10d %10d\n", 'Swap:', $swap_total, $swap_used, $swap_free; printf "%-7s %10d %10d %10d\n", 'Total:', $tot_total, $tot_used, $tot_free; } exit 0; sub usage () { ($name = $0) =~ s@.*/@@; print <