Usenet comp.unix.tru64 (2000-2013)
 help / color / mirror / Atom feed
From: lar3ry@zk3.dec.com (Larry Gensch)
To: thush2@my-deja.com
Subject: Re: mem used by unix ?
Date: 2000/08/24	[thread overview]
Message-ID: <8o33jo$hbq$1@mailint03.im.hou.compaq.com> (raw)
In-Reply-To: 8n9gbb$d1v$1@nnrp1.deja.com

[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 (<VMSTAT>) {
    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 (<SWAPON>) {
    $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 <<EOF;
Usage:
    $name            Display memory/swap used and free
    $name -d         Turn on all debugging on
    $name -dm        Turn on vmstat debugging
    $name -ds        Turn on swapon debugging
    $name -h         Display THIS help text
    $name -v         Display values as percentages

EOF

    exit 1;
}

sub percent () {
    my $num = $_[0];
    $num *= 1000;
    $num += 5;
    $num /= 10;
    int($num);
}
*** cut here ***

-- 
Larry Gensch					   Larry.Gensch@compaq.com
Compaq Computer Corporation                        As usual, the standard
110 Spit Brook Road ZKO3-2/W17                     disclaimers apply, etc.
Nashua, NH 03062                                   Yada yada yada



      parent reply	other threads:[~2000-08-24  4:00 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2000-08-14  4:00 mem used by unix ? thush2
2000-08-15  4:00 ` James Hanway
2000-08-15  4:00 ` Tony Verhulst
2000-08-22  4:00   ` Stephen Carpenter
2000-08-24  4:00 ` Larry Gensch [this message]
read-only archive

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox