2

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.

Som3guy
  • 57
  • 1
  • 9
  • related: [How to get the max memory usage of a program using psutil in Python](http://stackoverflow.com/q/22732932/4279) – jfs Aug 31 '14 at 11:32

1 Answers1

4

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
Community
  • 1
  • 1
johntellsall
  • 14,394
  • 4
  • 46
  • 40
  • Thank you that does clear a few things up for me. If you dont mind I am having trouble getting that into my script. `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=`ps -o rss= -p $p | awk '{print $1*4, "KiB"}'` echo "Process ID:"$p "Memory Usage:"$memU #total="$((total+memU))" #echo "This is the current running total:" $total done < $filename #echo "Total=" $total `code` Thanks for any help again. – Som3guy Aug 26 '14 at 13:19
  • Ran out of time to edit my comment! http://pastebin.com/yUbHjU5j You can see my code at the above pastbin better formated. ps throws an error, but I thought I was passing the variable correctly. Thanks again good sir. – Som3guy Aug 26 '14 at 13:27
  • Or at least how to do I pass a list of pids to the command you gave. I tried using the variable $p with no luck. I tried passing the list /tmp/psadd which has a new pid on each line with no luck as well. – Som3guy Aug 26 '14 at 13:41
  • pstree doesn't list mysqld and sub-processes, it lists sub-*threads*. "Child threads of a process are found under the parent process and are shown with the process name in curly braces" http://man.cx/?page=pstree It's confusing :) – johntellsall Aug 26 '14 at 16:12
  • Doesn't `ps` already give `rss` in `KiB` and not in `pages`? In the [manual](http://linuxcommand.org/lc3_man_pages/ps1.html) I can find `resident set size, the non-swapped physical memory that a task has used (in kiloBytes). (alias rssize, rsz)`. Also, the key `resident` would give `resident pages` instead of `resident set size` given by key `rss`. I guess this means the above command would give 4 times the memory occupied. – Saravanabalagi Ramachandran Apr 21 '20 at 11:19
  • However if you get it directly from /proc/stat then `rss` would be expressed in number of pages, wherein you need to multiply by page size to get memory used in same unit (as that of the page size) – Saravanabalagi Ramachandran Apr 21 '20 at 11:23