Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2307140
  • 博文数量: 276
  • 博客积分: 5998
  • 博客等级: 大校
  • 技术积分: 5175
  • 用 户 组: 普通用户
  • 注册时间: 2010-12-24 14:43
文章分类

全部博文(276)

文章存档

2014年(25)

2013年(11)

2012年(69)

2011年(167)

2010年(4)

分类: LINUX

2011-07-21 04:57:40

1、vim iodump

#!/usr/bin/env perl

# This program is part of Aspersa ()

 

=pod

 

=head1 NAME

 

iodump - Compute per-PID I/O stats for Linux when iotop/pidstat/iopp are not available.

 

=head1 SYNOPSIS

 

Prepare the system:

 

  dmesg -c

  /etc/init.d/klogd stop

  echo 1 > /proc/sys/vm/block_dump

 

Start the reporting:

 

  while true; do sleep 1; dmesg -c; done | perl iodump

  CTRL-C

 

Stop the system from dumping these messages:

 

  echo 0 > /proc/sys/vm/block_dump

  /etc/init.d/klogd start

 

=head1 AUTHOR

 

Baron Schwartz

 

=cut

 

use strict;

use warnings FATAL => 'all';

use English qw(-no_match_vars);

use sigtrap qw(handler finish untrapped normal-signals);

 

my %tasks;

 

my $oktorun = 1;

my $line;

while ( $oktorun && (defined ($line = <>)) ) {

   my ( $task, $pid, $activity, $where, $device );

   ( $task, $pid, $activity, $where, $device )

      = $line =~ m/(\S+)\((\d+)\): (READ|WRITE) block (\d+) on (\S+)/;

   if ( !$task ) {

      ( $task, $pid, $activity, $where, $device )

         = $line =~ m/(\S+)\((\d+)\): (dirtied) inode \(.*?\) (\d+) on (\S+)/;

   }

   if ( $task ) {

      my $s = $tasks{$pid} ||= { pid => $pid, task => $task };

      ++$s->{lc $activity};

      ++$s->{activity};

      ++$s->{devices}->{$device};

   }

}

 

printf("%-15s %10s %10s %10s %10s %10s %s\n",

   qw(TASK PID TOTAL READ WRITE DIRTY DEVICES));

foreach my $task (

   reverse sort { $a->{activity} <=> $b->{activity} } values %tasks

) {

   printf("%-15s %10d %10d %10d %10d %10d %s\n",

      $task->{task}, $task->{pid},

      ($task->{'activity'}  || 0),

      ($task->{'read'}      || 0),

      ($task->{'write'}     || 0),

      ($task->{'dirty'}     || 0),

      join(', ', keys %{$task->{devices}}));

}

 

sub finish {

   my ( $signal ) = @_;

   if ( $oktorun ) {

      print STDERR "# Caught SIG$signal.\n";

      $oktorun = 0;

   }

   else {

      print STDERR "# Exiting on SIG$signal.\n";

      exit(1);

   }

}

 

 
2、Then turn on kernel messages about I/O:
 
echo 1 > /proc/sys/vm/block_dump
 
3、This makes the kernel start writing messages about every I/O operation that takes place. Now all you have to do is get those messages and feed them into my script:
 
while true; do sleep 1; dmesg -c; done | perl iodump 
 
4、Wait a little while, then cancel the script. The results should look something like the following:
 
root@kanga:~# while true; do sleep 1; dmesg -c; done | perl iodump^C# Caught SIGINT.TASK                   PID      TOTAL       READ      WRITE      DIRTY DEVICESfirefox               4450       4538        251       4287          0 sda4, sda3kjournald             2100        551          0        551          0 sda4firefox              28452        185        185          0          0 sda4kjournald              782         59          0         59          0 sda3pdflush                 31         30          0         30          0 sda4, sda3syslogd               2485          2          0          2          0 sda3firefox              28414          2          2          0          0 sda4, sda3firefox              28413          1          1          0          0 sda4firefox              28410          1          1          0          0 sda4firefox              28307          1          1          0          0 sda4firefox              28451          1          1          0          0 sda4

http://www.xaprb.com/blog/2009/08/23/how-to-find-per-process-io-statistics-on-linux/

阅读(1199) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~