Whipped up a script to see which jails are using up my RAM!
EDIT: NEW VERSION 0.03 is lower in thread... code in this post is 0.02
EDIT: NEW VERSION 0.03 is lower in thread... code in this post is 0.02

Perl:
#!/usr/local/bin/perl
# jmem - show jail memory usage in a top like interface
# Tue Jul 22 14:14:42 PDT 2025, created, rudy
use strict;
our (%jproc, %jrss, %jvirtm, %jname, %jpath, %jcpu);
our ($tproc, $trss, $tvirtm, $tpath, $tcpu);
our $uname =`uname -rns`;
chomp($uname);
# Some ANSI codes
my $cursor_top_left = "\033[1;1H";
my $clear_screen = "\033[2J";
my $color_title = "\e[44m\033[1m\033[33m";
my $color_bar = "\033[35m";
my $color_up = "\033[37m";
my $color_reset = "\033[0m";
# The Loop (make it like top!)
print $clear_screen; # Clear the screen
while (1) {
&runJLS;
&runPS;
print $cursor_top_left; # Move the cursor to the top-left
&printStats;
&zeroStats;
sleep 2;
}
# barf up output on screen
sub printStats {
# grab uptime to put in footer
my $uptime = `uptime` ; chomp($uptime);
# pad out width based on width of uptime
my $width = length($uptime);
$width = 69 if $width < 69;
my $pad = " "x ($width-69);
# print header
printf "$color_title$pad%3s %-32s %5s %10s %10s %4s$color_reset\n", "JID","NAME","PROCS","RSS","VIRTSZ","%CPU";
# print stats
foreach my $j (sort {$jvirtm{$b} <=> $jvirtm{$a}} keys %jname) {
printf "%3s %-32s %5s %7d MB %7d MB %4d\n", $j,$jname{$j},$jproc{$j},$jrss{$j}/1024,$jvirtm{$j}/1024,$jcpu{$j};
}
# print summary and footer
printf "$color_title$pad%3s %-32s %5s %7d MB %7d MB %4d$color_reset\n", "","Total:",$tproc,$trss/1024,$tvirtm/1024,$tcpu;
print "$color_up$uptime$color_reset\n";
printf "$color_title$pad%-30s %26d VMs running$color_reset\n", $uname, (scalar(keys %jname) - 1);
print " "x$width,"\r";
}
# grab jail names
sub runJLS {
$jname{0} = "HOST";
chomp($jname{0});
my @jls = `/usr/sbin/jls`;
shift(@jls);
foreach my $j (@jls) {
$j =~ s/^ *//;
chomp($j);
my @col=split(/ +/,$j);
# print "$col[0] = $col[1]\n";
$jname{$col[0]} = $col[1];
$jpath{$col[0]} = $col[2];
}
}
# reset counters
sub zeroStats {
foreach my $j ( keys %jname) {
$jproc{$j} = 0;
$jrss{$j} = 0;
$jvirtm{$j} = 0;
$jcpu{$j} = 0;
}
$tproc = 0;
$trss = 0;
$tvirtm = 0;
$tcpu = 0;
}
# grab memory/cpu/proc count per jail from ps
sub runPS {
my @res = `/bin/ps ax -o 'pid,jid,rss,vsz,%cpu,args' 2>&1`;
shift(@res);
foreach my $i (@res) {
$i = " ".$i; # prepend so split starts on 1!
my @col=split(/ +/,$i);
next if $i =~ /idle/ && $col[2] == 0; # skip idle...
next if $col[1] == $$; # skip this script
$jproc{$col[2]}++;
$jrss{$col[2]} += $col[3];
$jvirtm{$col[2]} += $col[4];
$jcpu{$col[2]} += $col[5];
$tproc++;
$trss += $col[3];
$tvirtm += $col[4];
$tcpu += $col[5];
}
}
Attachments
Last edited: