Last active
June 6, 2022 07:33
-
-
Save ddmitov/80a4fdc4404617790c9c3414bb749518 to your computer and use it in GitHub Desktop.
Linux RSS Memory Meter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
# Linux RSS Memory Meter version 2.0. | |
# MIT 2019 Dimitar D. Mitov | |
# https://github.com/ddmitov | |
# Linux RSS Memory Meter can measure | |
# the combined RSS memory usage of several processes | |
# together with their child processes | |
# providing every second a total in megabytes. | |
# Usage: | |
# ./rss-meter process_one process_two | |
# All command-line arguments are treated as | |
# full or partial names of processes that have to be measured. | |
BEGIN { | |
$SIG{TERM} = $SIG{INT} = $SIG{QUIT} = $SIG{HUP} = sub { | |
print "\nQuitting.\n\n"; | |
exit(0); | |
}; | |
} | |
use strict; | |
use warnings; | |
use POSIX qw(strftime); | |
binmode STDOUT, ":utf8"; | |
# Disable built-in buffering: | |
$| = 1; | |
my @commands = @ARGV; | |
while (1) { | |
sleep(1); | |
my $rss_total = 0; | |
my $number_of_processes = 0; | |
my $local_time = strftime('%d %B %Y %H:%M:%S', localtime); | |
system("clear"); | |
print "\n"; | |
print "========================================\n"; | |
print "Linux RSS Memory Meter version 2.0.\n"; | |
print "MIT 2019 Dimitar D. Mitov\n"; | |
print "https://github.com/ddmitov\n"; | |
print "\n"; | |
print "$local_time\n"; | |
print "Press Ctrl + C to quit.\n"; | |
print "========================================\n"; | |
print "\n"; | |
foreach my $command (@commands) { | |
my @ps_output = `ps -eo rss,args -C $command`; | |
foreach my $ps_output_line (@ps_output) { | |
$ps_output_line =~ s/^\s{1,}//; | |
# Output fields are separated by spaces: | |
my @ps_output_fields = split(/\s{1,}/, $ps_output_line); | |
my @executable_full_path = split(/\//, $ps_output_fields[1]); | |
my $executable = pop @executable_full_path; | |
if ($executable =~ $command) { | |
$number_of_processes++; | |
# First column is RSS memory: | |
my $process_memory = 0; | |
if (length($ps_output_fields[0]) > 0) { | |
$process_memory = | |
sprintf("%.3f", $ps_output_fields[0] / 1024); | |
} | |
# Second column is process command: | |
print "$process_memory MB $ps_output_fields[1]\n"; | |
$rss_total = $rss_total + $process_memory; | |
} | |
} | |
} | |
if ($rss_total == 0) { | |
print "Target processes are not running. Quitting.\n\n"; | |
exit(1); | |
} else { | |
print "\n"; | |
print "Number of processes: $number_of_processes\n"; | |
print "Total amount of RSS memory used: $rss_total MB\n"; | |
print "\n"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment