#!/bin/bash
#===============================================================================
#
#          FILE: process_doing_io
#
#         USAGE: process_doing_io
#
#   DESCRIPTION: Get the process list that doing I/O from kernel space
#
#  REQUIREMENTS:
#
#         NOTES:
#
#          BUGS:  ---
#        AUTHOR: rickz (Rick Zhang), xlrickz@gmail.com
#       COMPANY: X-LEGEND Entertainment Corp.
#       CREATED: Thu Mar 29 12:23:55 KST 2012
#      REVISION: 1.0
#
#          TODO:
#
#===============================================================================

#set -o nounset                              # Treat unset variables as an error
set -m                                       # Enable job control


# Is /proc/sys/vm/block_dump writable?
[ -w /proc/sys/vm/block_dump ] || exec echo "Error: You dont have permission to write /proc/sys/vm/block_dump
Please run this script by root."

# Turn on I/O debug mode
echo 1 > /proc/sys/vm/block_dump 

# Collect data for 10 seconds
sleep 10 

echo "Write/ |
read   | Process name and PID
times  |
------------------------------------"

# Calculate the write/read block/inode
dmesg -s1048576|awk -F"[bi][nl][o][cd][ek]" '/ READ | WRITE / {print $1}'|sort|uniq -cd|sort -gr 

# Turn off I/O debug mode
echo 0 > /proc/sys/vm/block_dump

