Linux\Android IO\内存信息统计

来源:互联网 发布:foxmail咋样知乎 编辑:程序博客网 时间:2024/06/05 04:08

统计每个进程的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_TASKSTATSb.CONFIG_TASK_IO_ACCOUNTINGc.CONFIG_TASK_XACCTd.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/shIOTOPSH=/system/bin/iotop.shpath=/data/local/tmpIOinfoFile=$path/IOinfo.logIOinfobakFile=$path/IOinfobak.logtest -e $IOinfoFile && busybox mv $IOinfoFile $IOinfobakFileif [ ! -e $IOinfoFile ];thentouch $IOinfoFilefiwhile : 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@xdashow_help() {cat << EOLUsage: ./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 writtento 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 activityPlease note that this script is still in an early stage, which iswhy it does not yet support all features iotop for PCs has.If you want to contribute, feel free to fork the repo and issuea pull request.EOL}path=/data/local/tmpIOinfoFile=$path/IOinfo.log# reset variables just in case...var=""old="\n"new=""unit=""read_old=0write_old=0read_new=0write_new=0old_read=0old_write=0only=0show_skip=0if [[ ! -e /proc/self/io ]]; then    echo "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 1fiwhile :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            ;;      esacdone# get all PIDs#pid_all="$(ps -A -o pid | sed '/PID/d')"  # ubuntupid_all=$(ps | awk '{ print $2}' | sed '/PID/d')  # androidbytes2kb() {    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    fidonesleep 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    fidoneecho " PID      READ        WRITTEN     READ_SPEED      WRITE_SPEED     PROCESS" >> $IOinfoFileecho -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/shprintf "hello world!\n"#判断文件日志是否存在path=/data/local/tmpMeminfoFile=$path/Meminfo.logTopInfoFile=$path/Topinfo.logFreeInfoFile=$path/Freeinfo.logLogInfoFile=$path/logcatinfo.logMeminfobakFile=$path/Meminfobak.logTopInfobakFile=$path/Topinfobak.logFreeInfobakFile=$path/Freeinfobak.logLogInfobakFile=$path/logcatinfobak.logtest -e $MeminfoFile && busybox mv $MeminfoFile $MeminfobakFiletest -e $TopInfoFile && busybox mv $TopInfoFile $TopInfobakFiletest -e $FreeInfoFile && busybox mv $FreeInfoFile $FreeInfobakFiletest -e $LogInfoFile && busybox mv $LogInfoFile $LogInfobakFile`logcat -v time >$LogInfoFile &`#打印内存信息printMem(){    (printf "%s\t" ${1}  `date "+%Y/%m/%d %H:%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=0while [ 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 $idone
原创粉丝点击