Skip to content

Instantly share code, notes, and snippets.

@sheeplogh
Created May 26, 2016 08:40
Show Gist options
  • Save sheeplogh/e9cd6737f37d6281e6b48d9884bcde6c to your computer and use it in GitHub Desktop.
Save sheeplogh/e9cd6737f37d6281e6b48d9884bcde6c to your computer and use it in GitHub Desktop.
Linuxでプロセス毎のswapsizeを表示する( http://www.maepachi.com/blog/entry/130 を元に小改造させていただきました)
#!/bin/env perl
# pachi
#
# This script find out process currently swapped by /proc/$PID/status
use strict;
use warnings;
# When you want to debug, you should exec "export SCRIPT_DEBUG=1".
my $debug = $ENV{SCRIPT_DEBUG} ? 1 : 0;
my $proc_status = `grep VmSwap /proc/*/status | sort -k 2 -r`;
my @proc_status = split m{\n}, $proc_status;
my $proc_swaps = [];
foreach my $status_line ( @proc_status ) {
my $process_info = {};
chomp $status_line;
my @status_line = split /\s+/, $status_line;
my $proc_path = $status_line[0];
my $proc_swap_value = $status_line[1];
my $pid = ( split( /\//,$proc_path ) )[2];
if ( ! -f "/proc/$pid/status" ) {
# file not found! next.
next;
}
# get process name by pid
my $process_name_line = `grep Name /proc/$pid/status`;
chomp $process_name_line;
my @process_name_line = split /\s+/, $process_name_line;
my $process_name = $process_name_line[1];
my $process_cmdline = `cat /proc/$pid/cmdline`;
chomp $process_cmdline;
$process_cmdline =~ s/\x00/ /g;
$process_info->{name} = $process_name;
$process_info->{pid} = $pid;
$process_info->{swap_value} = $proc_swap_value;
$process_info->{cmdline} = $process_cmdline;
# Process information is stored in hash
push @$proc_swaps ,$process_info;
}
# Print header
printf "--------------------+----------+---------------+\n";
printf " Swap size classified by processes |\n";
printf "--------------------+----------+---------------+\n";
printf " Process Name | PID | Swap Size |\n";
printf "--------------------+----------+---------------+\n";
# Print every process info
foreach my $proc_swap ( @$proc_swaps ) {
printf ("%-20s|", "$proc_swap->{name}");
printf ("%9s |", "$proc_swap->{pid}");
printf ("%14s |", "$proc_swap->{swap_value}KB");
printf (" %-20s", "$proc_swap->{cmdline}");
printf "\n";
}
# Print footer
printf "\n";
exit 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment