jails jmem - Memory usage for jails

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 :)

jmem.png

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

  • Screenshot_2025-07-22_15-48-54.png
    Screenshot_2025-07-22_15-48-54.png
    83.4 KB · Views: 373
Last edited:
Great idea!

Even though it is a little script, you should submit this as a port (simply so it gets the visibility that it deserves in my opinion)
 
Great job :)

I only changed "VMs" to "Jails" line 50 it seems more appropriate, other than that I really like it, designed like top, well done.
Even if I don't understand all the code (my knowledge in Perl is flirting with zero) suddenly I do want to know more about Perl ^^

If you have other good stuff like this please do not hesitate to share them with us, we have a dedicated thread for that in case you didn't notice, it's a very long thread but at least the treasure doesn't get lost among the numerous other threads.

Thank you.
 
Great job :)

I only changed "VMs" to "Jails" line 50 it seems more appropriate, other than that I really like it, designed like top, well done.
Even if I don't understand all the code (my knowledge in Perl is flirting with zero) suddenly I do want to know more about Perl ^^

If you have other good stuff like this please do not hesitate to share them with us, we have a dedicated thread for that in case you didn't notice, it's a very long thread but at least the treasure doesn't get lost among the numerous other threads.

Thank you.
Newer version is attached here... I made code a bit cleaner by using one %jmem has for most of the data. Makes the JSON output clean.
 

Attachments

Nice.
Just in case people don't know, with the new version there is now a dependency, the package name is converters/p5-JSON.
I don't know if it's just me, with the new version it seems that the total count of jail is N-1.
The number of jails in row is fine but the message displayed at the bottom "x VMs running" isn't correct, at least in my case, with the previous version it was correct.

EDIT:
I tried to clarify the explanation.
 
Back
Top