Advertisement
Guest User

Untitled

a guest
May 26th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. #!/bin/env perl
  2. # pachi
  3. #
  4. # This script find out process currently swapped by /proc/$PID/status
  5. use strict;
  6. use warnings;
  7.  
  8. # When you want to debug, you should exec "export SCRIPT_DEBUG=1".
  9. my $debug = $ENV{SCRIPT_DEBUG} ? 1 : 0;
  10.  
  11. my $proc_status = `grep VmSwap /proc/*/status | sort -k 2 -r`;
  12. my @proc_status = split m{\n}, $proc_status;
  13. my $proc_swaps = [];
  14.  
  15. foreach my $status_line ( @proc_status ) {
  16. my $process_info = {};
  17.  
  18. chomp $status_line;
  19. my @status_line = split /\s+/, $status_line;
  20. my $proc_path = $status_line[0];
  21. my $proc_swap_value = $status_line[1];
  22. my $pid = ( split( /\//,$proc_path ) )[2];
  23.  
  24. if ( ! -f "/proc/$pid/status" ) {
  25. # file not found! next.
  26. next;
  27. }
  28.  
  29. # get process name by pid
  30. my $process_name_line = `grep Name /proc/$pid/status`;
  31. chomp $process_name_line;
  32. my @process_name_line = split /\s+/, $process_name_line;
  33. my $process_name = $process_name_line[1];
  34. my $process_cmdline = `cat /proc/$pid/cmdline`;
  35. chomp $process_cmdline;
  36. $process_cmdline =~ s/\x00/ /g;
  37. $process_info->{name} = $process_name;
  38. $process_info->{pid} = $pid;
  39. $process_info->{swap_value} = $proc_swap_value;
  40. $process_info->{cmdline} = $process_cmdline;
  41.  
  42.  
  43. # Process information is stored in hash
  44. push @$proc_swaps ,$process_info;
  45.  
  46. }
  47.  
  48. # Print header
  49. printf "--------------------+----------+---------------+\n";
  50. printf " Swap size classified by processes |\n";
  51. printf "--------------------+----------+---------------+\n";
  52. printf " Process Name | PID | Swap Size |\n";
  53. printf "--------------------+----------+---------------+\n";
  54.  
  55. # Print every process info
  56. foreach my $proc_swap ( @$proc_swaps ) {
  57. printf ("%-20s|", "$proc_swap->{name}");
  58. printf ("%9s |", "$proc_swap->{pid}");
  59. printf ("%14s |", "$proc_swap->{swap_value}KB");
  60. printf (" %-20s", "$proc_swap->{cmdline}");
  61. printf "\n";
  62. }
  63.  
  64. # Print footer
  65. printf "\n";
  66.  
  67. exit 0;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement