hxtools/sadmin/pmap_dirty

81 lines
1.5 KiB
Perl
Executable File

#!/usr/bin/perl -w
# SPDX-License-Identifier: MIT
#
# pmap_dirty
# written by Jan Engelhardt, 2006-2007
#
# Usage: pmap_dirty [pid...]
#
# Ex: pmap_dirty
# pmap_dirty $$
# pmap_dirty `pidof init` `pidof java`
use Getopt::Long;
use strict;
our $COMPAT_PROC = ""; # FreeBSD:/compat/linux
my $Use_pid_sort;
&Getopt::Long::Configure(qw(bundling));
&GetOptions("p" => \$Use_pid_sort);
my %total_counter;
if (scalar(@ARGV) == 0) {
@ARGV = sort { $a <=> $b } map {
$_ = ($_ =~ m{^\Q$COMPAT_PROC\E/proc/(\d+)$})[0];
(!defined($_) || $_ eq "") ? () : $_
} glob("$COMPAT_PROC/proc/*");
}
&print_one();
foreach my $pid (@ARGV) {
my $proc_name;
my $fh;
open($fh, "< $COMPAT_PROC/proc/$pid/smaps");
my %counter = (
Private_Dirty => 0,
Referenced => 0,
);
my $shared_dirty = 0;
while (defined(my $line = <$fh>)) {
if ($line =~ /^(\w+):\s+(\d+)/i) {
$counter{$1} += $2;
$total_counter{$1} += $2;
}
}
close $fh;
if (open($fh, "< $COMPAT_PROC/proc/$pid/stat")) {
($proc_name) = (<$fh> =~ /^.*?\((.*)\)/);
close $fh;
}
&print_one("$pid($proc_name)", \%counter);
}
&print_one("Total", \%total_counter);
sub print_one
{
my($text, $counter) = @_;
if (!defined($counter)) {
if (!$Use_pid_sort) {
printf "%-10s %-10s %s\n", "PRIV_DIRT", "REFERENCD", "PROCESS";
}
return;
}
my $pd = $counter->{Private_Dirty}."K";
my $re = $counter->{Referenced}."K";
# Pv_Dirty Referenced
if ($Use_pid_sort) {
printf "$text: $pd (ref $re)\n";
} else {
printf "%-10s %-10s %s\n", $pd, $re, $text;
}
}