Check memory per processes and subprocesses

2020-03-30 09:07发布

问题:

I am attempted to create a script that show how much each process and subprocess of mysqld is using. You can see what I have done in my code.

#!/bin/bash
#file contains the output of: pstree mysql -a -p |awk '{print $1;}' | sed 's/|-        {mysqld},//' >> psadd
filename='psadd'
#total= '0'
echo Start
while read p; do
    memU= cat /proc/$p/smaps |grep -e Private -e Shared |awk '{print $2}' |awk '{total = total + $1}END{print total}'
    echo "Process ID:"$p  "Memory Usage:"$memU
total="$((total+memu))"
echo "This is the current running total:" $total
done < $filename
echo "Total=" $total

Please if you have any ideas they will be much appreciated.

回答1:

Calculating a processes memory usage is... complicated. I generally use a proc's RSS -- Resident Set Size -- the amount of memory a process is holding in memory, that isn't shared by other procs.

The following finds the process ID of the MySQL daemon, and uses ps to output the RSS value with no header. Lastly it multiplies this by four to get the RSS size in KiB. (Default pagesize is 4 KiB.)

ps has tons of information -- have fun!

shell

ps -o rss= -p `pidof mysqld` | awk '{print $1*4, "KiB"}'

output

7808 KiB