Shell script to see the real footprint of process memory _linux Shell

Source: Internet
Author: User

Copy Code code as follows:

#!/bin/bash
#****************************************************************#
# SCRIPTNAME:PM
# AUTHOR:OSDBA
# Create Date:2010-01-31
# Modify Author:
# Modify Date:2010-01-31
# Function:show processes Memory Used
#***************************************************************#


function Getpidmem ()
{
Cat/proc/$1/smaps 2>/dev/null |awk/
' begin{
total_size=0;
total_rss=0;
total_shared_clean=0;
total_shared_dirty=0;
total_private_clean=0;
total_private_dirty=0;
Total_swap=0;

mem_size=0;
mem_rss=0;
mem_shared_clean=0;
mem_shared_dirty=0;
mem_private_clean=0;
mem_private_dirty=0;
Mem_swap=0;
}
{
if ($ ~/^size:/)
{
mem_size=$2;
}
else if ($ ~/^rss:/)
{
mem_rss=$2;
}
else if ($ ~/^shared_clean:/)
{
mem_shared_clean=$2;
}
else if ($ ~/^shared_dirty:/)
{
mem_shared_dirty=$2;
}
else if ($ ~/^private_clean:/)
{
mem_private_clean=$2;
}
else if ($ ~/^private_dirty:/)
{
mem_private_dirty=$2;
}
else if ($ ~/^swap:/)
{
mem_swap=$2;
}
Else
{
if (ipcmem_start==1)
{
Ipc_size=mem_size;
Ipc_rss=mem_rss;
Ipc_shared_clean=mem_shared_clean;
Ipc_shared_dirty=mem_shared_dirty;
Ipc_private_clean=mem_private_clean;
Ipc_private_dirty=mem_private_dirty;
Ipc_swap=mem_swap;
}
Else
{
Total_size=total_size+mem_size;
Total_rss=total_rss+mem_rss;
Total_shared_clean=total_shared_clean+mem_shared_clean;
Total_shared_dirty=total_shared_dirty+mem_shared_dirty;
Total_private_clean=total_private_clean+mem_private_clean;
Total_private_dirty=total_private_dirty+mem_private_dirty;
Total_swap=total_swap+mem_swap;
};
if ($ ~/^//sysv/)
{
Ipcmem_start=1;
}
Else
{
ipcmem_start=0;
};
};
}
end{
printf ("%15d%15d%15d%15d%15d%15d/n", total_rss,total_private_clean,total_private_dirty,total_shared_clean,total _SHARED_DIRTY,TOTAL_SWAP);
}
'
}

function Show_all_process_mem ()
{
Tmpfile= '/bin/mktemp-q '
echo "pid RSS (KB) priv_dirty priv_clean shr_dirty shr_clean SWAP pagetable cmdline"
echo "-----------------------------------------------------------------------------------------------"
ls-ld/proc/[0-9]* |awk ' {printf ("%s/n", $NF)} ' > $TMPFILE
While Read line
Todo
Pid= ' basename $LINE '
Cmdline= ' Ps-o command-p $PID |tail-1 |cut-b 1-26 '
Pagetablesize= ' cat/proc/$PID/status |grep "vmpte:" |awk ' {print $} '
If ["$pagetablesize" = ""]; Then
Pagetablesize=0
Fi

Meminfoline= ' Getpidmem $PID '
Rss= ' echo $meminfoline |awk ' {print $} '
Private_clean= ' echo $meminfoline |awk ' {print $} '
Private_dirty= ' echo $meminfoline |awk ' {print $} '
Shared_dirty= ' echo $meminfoline |awk ' {print $} '
Shared_clean= ' echo $meminfoline |awk ' {print $} '
Swap= ' echo $meminfoline |awk ' {print $} '
total_rss=$ ((TOTAL_RSS+RSS))
total_private_clean=$ ((Total_private_clean+private_clean))
total_private_dirty=$ ((Total_private_dirty+private_dirty))
total_shared_dirty=$ ((Total_shared_dirty+shared_dirty))
total_shared_clean=$ ((Total_rss+shared_clean))
total_swap=$ ((Total_swap+swap))
total_pagetablesize=$ ((total_pagetablesize+pagetablesize))
If ["$" = "a"]; then
echo $PID $meminfoline "$pagetablesize" "$cmdline" |awk/
'{
printf ("%9d%10d%10d%10d%10d%10d%10d%10d",%-26s/n);
}'
Fi
Done < $TMPFILE
/bin/rm $TMPFILE

echo $total _rss $total _private_clean $total _private_dirty $total _shared_dirty $total _shared_clean $total _swap $total _ Pagetablesize |awk/
'{
printf ("All%10d%10d%10d%10d%10d%10d%10d/n", $1,$2,$3,$4,$5,$6,$7);
}'
}


function Print_usage ()
{
echo "Usage:"
echo "PM.SH-A: Display all process Memory Info"
echo "Pm.sh-s: Display total Memory Info"
echo "Pm.sh-p <pid>: Display PID Memory Info"
}

Total_rss=0
Total_private_clean=0
Total_private_dirty=0
Total_shared_dirty=0
Total_shared_clean=0
Total_swap=0
Total_pagetablesize=0

If [$#-lt 1];then
Print_usage;
Exit 1
Fi

If ["$" = "a"];then
Show_all_process_mem $;
Exit 0
Fi
If ["$" = "s"];then
Show_all_process_mem $;
Exit 0
Fi

If ["$" = "P"];then
If [$#-ne 2];then
Print_usage;
Exit 1
Fi

Meminfoline= ' Getpidmem $ '
Cmdline= ' Ps-o command-p $ |tail-1 |cut-b 1-26 '
Pagetablesize= ' Cat/proc/$2/status |grep ' vmpte: ' |awk ' {print $} '
If ["$pagetablesize" = ""]; Then
Pagetablesize=0
Fi

echo "pid RSS (KB) priv_dirty priv_clean shr_dirty shr_clean SWAP pagetable cmdline"
echo "-----------------------------------------------------------------------------------------------"
echo $ $meminfoline "$pagetablesize" "$cmdline" |awk/
'{
printf ("%9d%10d%10d%10d%10d%10d%10d%10d",%-26s/n);
}'
Exit 0
Fi

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.