您的位置:首页 > 运维架构 > Linux

Linux\Android IO\内存信息统计

2017-10-18 10:08 162 查看

统计每个进程的IO信息方法

如果内核版本大于2.6.20,通过cat /proc/pid/io 便可以获取进程的io信息。

1.kernel中打开统计IO信息配置。

Kernel with I/O accounting enabled

The following configs must be set:

a.CONFIG_TASKSTATS
b.CONFIG_TASK_IO_ACCOUNTING
c.CONFIG_TASK_XACCT
d.CONFIG_TASK_DELAY_ACCT


使能方法:

解压kernel后,进入kerne配置设置。

tar jxvf linux-2.6.30.5.tar.bz2

mv linux-2.6.30.5 /usr/src/

cd /usr/src/linux-2.6.30.5

make menuconfig,使用/进行搜索需要打开的四个配置项,









配置打开完成后,会生成/proc/$[pid]/io节点。

开机启动后台运行iotop.sh脚本,监控进行io信息。

iostart.sh

#!/system/bin/sh
IOTOPSH=/system/bin/iotop.sh
path=/data/local/tmp
IOinfoFile=$path/IOinfo.log
IOinfobakFile=$path/IOinfobak.log
test -e $IOinfoFile && busybox mv 	$IOinfoFile $IOinfobakFile
if [ ! -e $IOinfoFile ];then
touch $IOinfoFile
fi

while :
do
$IOTOPSH -m --only --showskips;
sleep 2;
done


iotop.sh

#!/system/bin/sh
#
# iotop similar program to capture I/O activity per process
# By laufersteppenwolf@xda
show_help() {
cat << EOL
Usage: ./iotop.sh [-h -m -b --show_skips]

Show the I/O usage on per-app/per-process basis.
READ and WRITTEN show the total amount of bytes read or written
to the storage per process.
READ_SPEED and WRITE_SPEED show the current read and write speeds.

Default behavior is to show all units in kb.

-h   | --help           Display this help and exit
-m   | --mb             Change units to MB
-b   | --bytes          Change units to bytes
--show_skips            Print a message when skipping a process
with no I/O activity
--only                  Skip processes with no I/O activity

Please note that this script is still in an early stage, which is
why it does not yet support all features iotop for PCs has.
If you want to contribute, feel free to fork the repo and issue
a pull request.

EOL
}

path=/data/local/tmp
IOinfoFile=$path/IOinfo.log
# reset variables just in case...
var=""
old="\n"
new=""
unit=""
read_old=0
write_old=0
read_new=0
write_new=0
old_read=0
old_write=0
only=0
show_skip=0

if [[ ! -e /proc/self/io ]]; then
ech
4000
o "Your kernel does not support I/O accounting,"  >> $IOinfoFile
echo "which is required for this tool to work :(" >> $IOinfoFile
echo "" >> $IOinfoFile
echo "Please recompile your kernel with I/O accounting enabled" >> $IOinfoFile
echo "or politely ask your kernel dev to enable it." >> $IOinfoFile
echo "" >> $IOinfoFile
echo "To enable I/O accounting the following configs have to be set:" >> $IOinfoFile
echo "CONFIG_TASKSTATS" >> $IOinfoFile
echo "CONFIG_TASK_IO_ACCOUNTING" >> $IOinfoFile
echo "CONFIG_TASK_XACCT" >> $IOinfoFile
echo "CONFIG_TASK_DELAY_ACCT" >> $IOinfoFile
echo "" >> $IOinfoFile
exit 1
fi

while :
do
case $1 in
-h | --help)
show_help
help=1
exit 0
;;
-m | --mb)
unit="mb"
shift
;;
-b | --bytes)
unit="bytes"
shift
;;
--only)
only="1"
shift
;;
--show_skips | --show-skips)
show_skip="1"
shift
;;
--) # End of all options
shift
break
;;
*)  # no more options. Stop while loop
break
;;
esac
done

# get all PIDs
#pid_all="$(ps -A -o pid | sed '/PID/d')"  # ubuntu
pid_all=$(ps | awk '{ print $2}' | sed '/PID/d')  # android

bytes2kb() {
local var="$(expr $1 '/' 1024)"
echo "$var"
}

bytes2mb() {
local var="$(expr $1 '/' 1048576)"
echo "$var"
}

get_old() {
read_old="$(cat /proc/${1}/io | grep 'read_bytes:' | cut -d ' ' -f2)"
write_old="$(cat /proc/${1}/io | grep 'write_bytes:' | cut -d ' ' -f2 | head -1)"
}

get_new() {
read_new="$(cat /proc/${1}/io | grep 'read_bytes:' | cut -d ' ' -f2)"
write_new="$(cat /proc/${1}/io | grep 'write_bytes:' | cut -d ' ' -f2 | head -1)"
}

for pid in ${pid_all}; do
process=""
if [[ -a /proc/${pid}/cmdline ]]; then
process="$(cat /proc/${pid}/cmdline)"
fi

if [[ -a /proc/${pid}/io && $process != "" ]]; then
get_old ${pid}
old="$old pid:$pid read:$read_old write:$write_old\n"
#echo -e $old
fi
done

sleep 1

#echo -e "$old"

for pid in ${pid_all}; do
process=""
if [[ -a /proc/${pid}/cmdline ]]; then
process="$(cat /proc/${pid}/cmdline)"
fi

if [[ -a /proc/${pid}/io && $process != "" ]]; then
get_new "${pid}"
old_read="$(echo -e ${old} | grep pid:$pid | head -1 | cut -d ' ' -f3 | cut -d ':' -f2)"
old_write="$(echo -e ${old} | grep pid:$pid | head -1 | cut -d ' ' -f4 | cut -d ':' -f2)"
if [[ $read_new = 0 && $write_new = 0 && $only = 1 ]]; then
if [[ $show_skip = 1 ]]; then
echo "Skipping process with no IO" >> $IOinfoFile
fi
else
#echo "$process"
#echo "old read: $old_read"
#echo "new read: $read_new"
#echo "old write: $old_write"
#echo "new write: $write_new"

read_speed="$(expr $read_new - $old_read)"
write_speed="$(expr $write_new - $old_write)"

#echo "read speed: $read_speed"
#echo "write speed: $write_speed"
#echo ""
#echo "b2kb: $(bytes2kb $read_speed)"

read_speed_kb="$(bytes2kb $read_speed)"
write_speed_kb="$(bytes2kb $write_speed)"
read_speed_mb="$(bytes2mb $read_speed)"
write_speed_mb="$(bytes2mb $write_speed)"
read_new_kb="$(bytes2kb $read_new)"
write_new_kb="$(bytes2kb $write_new)"
read_new_mb="$(bytes2mb $read_new)"
write_new_mb="$(bytes2mb $write_new)"

if [[ $unit = "mb" ]]; then
read_new_out="$read_new_mb"
write_new_out="$write_new_mb"
read_speed_out="$read_speed_mb"
write_speed_out="$write_speed_mb"
elif [[ $unit = "bytes" ]]; then
read_new_out="$read_new"
write_new_out="$write_new"
read_speed_out="$read_speed"
write_speed_out="$write_speed"
else
read_new_out="$read_new_kb"
write_new_out="$write_new_kb"
read_speed_out="$read_speed_kb"
write_speed_out="$write_speed_kb"
fi

new="$new $pid      $read_new_out		$write_new_out     $read_speed_out			$write_speed_out           $process\n"
fi
fi
done

echo " PID      READ        WRITTEN     READ_SPEED      WRITE_SPEED     PROCESS" >> $IOinfoFile
echo -e "$new" >> $IOinfoFile


rchar: 197448798054 // 读出的总字节数,read()或者pread()中的长度参数总和(pagecache中统计而来,不代表实际磁盘的读入)

wchar: 209896059897 // 写入的总字节数,write()或者pwrite()中的长度参数总和

syscr: 6491904 // read()或者pread()总的调用次数

syscw: 13633940 // write()或者pwrite()总的调用次数

read_bytes: 49616125952 // 实际从磁盘中读取的字节总数

write_bytes: 14038130688 // 实际写入到磁盘中的字节总数

cancelled_write_bytes: 2473984 // 由于截断pagecache导致应该发生而没有发生的写入字节数

内存信息统计方法

meminfo.sh

#!/system/bin/sh
printf "hello world!\n"
#判断文件日志是否存在
path=/data/local/tmp
MeminfoFile=$path/Meminfo.log
TopInfoFile=$path/Topinfo.log
FreeInfoFile=$path/Freeinfo.log
LogInfoFile=$path/logcatinfo.log

MeminfobakFile=$path/Meminfobak.log
TopInfobakFile=$path/Topinfobak.log
FreeInfobakFile=$path/Freeinfobak.log
LogInfobakFile=$path/logcatinfobak.log

test -e $MeminfoFile && busybox mv 	$MeminfoFile $MeminfobakFile
test -e $TopInfoFile && busybox mv 	$TopInfoFile $TopInfobakFile
test -e $FreeInfoFile && busybox mv $FreeInfoFile $FreeInfobakFile
test -e $LogInfoFile && busybox mv $LogInfoFile $LogInfobakFile

`logcat -v time >$LogInfoFile &`

#打印内存信息
printMem()
{
(printf "%s\t" ${1}  `date "+%Y/%m/%d %H:
b5ce
%M:%S"`) >> $MeminfoFile
printf "\n" >> $MeminfoFile
memtotal=`cat /proc/meminfo |head -n 1 |tail -n 1`
memfree=`cat /proc/meminfo |head -n 2 |tail -n 1`
membuffer=`cat /proc/meminfo |head -n 4 |tail -n 1`
(printf "%s" $memtotal)>>$MeminfoFile
printf "\n" >> $MeminfoFile
(printf "%s" $memfree)>>$MeminfoFile
printf "\n" >> $MeminfoFile
(printf "%s" $membuffer)>>$MeminfoFile
printf "\n" >> $MeminfoFile
(printf "---------------------dumpsys meminfo-----------------------------\n")>>$MeminfoFile
dumMem=`dumpsys meminfo>>$MeminfoFile`
printf "------------------------------Printf END--------------------------\n"  >> $MeminfoFile
}

printTop()
{
(printf "%s\t" ${1}  `date "+%Y/%m/%d %H:%M:%S"`) >> $TopInfoFile
printf "\n" >> $TopInfoFile
(printf "---------------------Top-----------------------------\n")>>$TopInfoFile
totalAll=`top -s vss -d 1 -n 1`
procname=`echo "$totalAll"|head -n 12|tail -n 10>>$TopInfoFile`
printf "------------------------------Printf END--------------------------\n"  >> $TopInfoFile
}

printFree()
{
(printf "%s\t" ${1}  `date "+%Y/%m/%d %H:%M:%S"`) >> $FreeInfoFile
printf "\n" >> $FreeInfoFile
(printf "---------------------Free-----------------------------\n")>>$FreeInfoFile
Free=`free>>$FreeInfoFile`
printf "------------------------------Printf END--------------------------\n"  >> $FreeInfoFile
}

printf "test start \n"
i=0
while [ true ]
do
i=$(($i+1))
totalsize=`busybox du -sh  -m $path`
filesize=`echo "$totalsize"|awk -F' ' '{print $1}'`
datasize=4096
echo $filesize
if [ $filesize -gt $datasize ]
then
break
fi

temp=$1
if [ ! -n "$1" ] ;then
(printf "%s\t" $i  `date "+%Y/%m/%d %H:%M:%S"`)
printf "\n"
else
if [ $i -gt $temp ]
then
break
fi
fi

#(printf "%s\t" $i  `date "+%Y/%m/%d %H:%M:%S"`)
#printf "\n"
printMem $i
printTop $i
printFree $i

done
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: