#!/usr/bin/env perl
# memory + swap usage for trend

use strict;
use warnings;
use Time::HiRes "usleep";

# be sure to flush right away!
$| = 1;

# sleep time
my $ms = ($ARGV[0]? $ARGV[0]: 0.1) * 1000000;

# main loop
while()
{
  open FD, "/proc/meminfo" or die;
  while(<FD>)
  {
    # 2.4 kernel
    my ($tot, $used, $free, $buf, $cache) = /^Mem: *(\d+) +(\d+) +(\d+) +\d+ +(\d+) +(\d+)$/;
    if($tot)
    {
      print(($tot - $free - $buf - $cache) / 1024. . "\n");
      last;
    }
    
    # 2.6 kernel
    my ($active) = /^Active: *(\d+) kB$/;
    if($active)
    {
      print "$active\n";
      last;
    }
  }
  close FD;

  usleep $ms;
}
