| 1 |
#!/usr/bin/perl -w |
| 2 |
# SPDX-License-Identifier: MIT |
| 3 |
# |
| 4 |
# pmap_dirty |
| 5 |
# written by Jan Engelhardt, 2006-2007 |
| 6 |
# |
| 7 |
# Usage: pmap_dirty [pid...] |
| 8 |
# |
| 9 |
# Ex: pmap_dirty |
| 10 |
# pmap_dirty $$ |
| 11 |
# pmap_dirty `pidof init` `pidof java` |
| 12 |
|
| 13 |
use Getopt::Long; |
| 14 |
use strict; |
| 15 |
our $COMPAT_PROC = ""; # FreeBSD:/compat/linux |
| 16 |
|
| 17 |
my $Use_pid_sort; |
| 18 |
&Getopt::Long::Configure(qw(bundling)); |
| 19 |
&GetOptions("p" => \$Use_pid_sort); |
| 20 |
|
| 21 |
my %total_counter; |
| 22 |
|
| 23 |
if (scalar(@ARGV) == 0) { |
| 24 |
@ARGV = sort { $a <=> $b } map { |
| 25 |
$_ = ($_ =~ m{^\Q$COMPAT_PROC\E/proc/(\d+)$})[0]; |
| 26 |
(!defined($_) || $_ eq "") ? () : $_ |
| 27 |
} glob("$COMPAT_PROC/proc/*"); |
| 28 |
} |
| 29 |
|
| 30 |
&print_one(); |
| 31 |
|
| 32 |
foreach my $pid (@ARGV) { |
| 33 |
my $proc_name; |
| 34 |
my $fh; |
| 35 |
open($fh, "< $COMPAT_PROC/proc/$pid/smaps"); |
| 36 |
|
| 37 |
my %counter = ( |
| 38 |
Private_Dirty => 0, |
| 39 |
Referenced => 0, |
| 40 |
); |
| 41 |
my $shared_dirty = 0; |
| 42 |
while (defined(my $line = <$fh>)) { |
| 43 |
if ($line =~ /^(\w+):\s+(\d+)/i) { |
| 44 |
$counter{$1} += $2; |
| 45 |
$total_counter{$1} += $2; |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
close $fh; |
| 50 |
if (open($fh, "< $COMPAT_PROC/proc/$pid/stat")) { |
| 51 |
($proc_name) = (<$fh> =~ /^.*?\((.*)\)/); |
| 52 |
close $fh; |
| 53 |
} |
| 54 |
|
| 55 |
&print_one("$pid($proc_name)", \%counter); |
| 56 |
} |
| 57 |
|
| 58 |
&print_one("Total", \%total_counter); |
| 59 |
|
| 60 |
sub print_one |
| 61 |
{ |
| 62 |
my($text, $counter) = @_; |
| 63 |
|
| 64 |
if (!defined($counter)) { |
| 65 |
if (!$Use_pid_sort) { |
| 66 |
printf "%-10s %-10s %s\n", "PRIV_DIRT", "REFERENCD", "PROCESS"; |
| 67 |
} |
| 68 |
return; |
| 69 |
} |
| 70 |
|
| 71 |
my $pd = $counter->{Private_Dirty}."K"; |
| 72 |
my $re = $counter->{Referenced}."K"; |
| 73 |
|
| 74 |
# Pv_Dirty Referenced |
| 75 |
if ($Use_pid_sort) { |
| 76 |
printf "$text: $pd (ref $re)\n"; |
| 77 |
} else { |
| 78 |
printf "%-10s %-10s %s\n", $pd, $re, $text; |
| 79 |
} |
| 80 |
} |