So I am trying to check the internal CPU and RAM usage from JVM's.
The set up is that we have a single server which hosts 39 JVM's each running its own service and has it's own unique CPU & RAM allocation. So for example it could be set up like the following:
CPU(Cores) RAM(MB)
JVM1 2 300
JVM2 1 50
JVM3 5 1024
These are fictional as I don't have the actual values to hand.
I know the PID's for each of the JVM's but I am wondering how would I see the CPU and RAM usage of each JVM on it's own dissregarding the Host systems usage. Also is it possible to pass multiple PID's into a jstat -gc as I will be looking to script this?.
I know that if I use:
ps -p <PID> -o %CPU %RAM
That will give me the CPU and RAM of that process on the host machine.
Any Help is greatly appreciated.
After playing around for a while I came up with the following script which pulls the memory usage and total memory in use to 2DP:
Script
#!/bin/sh
service=$(mktemp)
jstatop=$(mktemp)
ServiceStatsPre=$(mktemp)
datet=$(date +%Y-%m-%d)
hourt=$(date +%H)
ps -ef | grep java | grep Service | awk '{key=substr($9,31,match($9,"#")-31); serv[key]++}END{for(name in serv){if($9 ~ /Service/){print name}}}'>$service
printf "%-8sService\t\t\t\t%7s PID\t%7s Used\t%7s Total\n" > $ServiceStatsPre
filename=$service
while read -r line; do
if [ ! -z $line ]; then
pid=$(ps -ef | grep java | grep Service |awk -v svc="$line" '{if (match($9,svc)){print $2}}')
rncnt=0
rnag=1
while [ $rnag -eq 1 ]; do
jstat -gc $pid > $jstatop
if [ $? -ne 0 ]; then
sleep 5;
rncnt++;
else
rnag=0
fi
if [ $rncnt -eq 5 ]; then
rnag=0
fi
done
cat $jstatop | awk '{if (NR !=1) print}'|awk -v pid="$pid" -v svc="$line" -v d="$datet" -v h="$hourt" '{printf("%-40s %7d %6.2fMB %6.2fMB %11s %3s\n",svc,pid,($1+$5+$7)/1024,($3+$4+$6+$8)/1024,d,h)}' >> $ServiceStatsPre
fi
done < $filename
#printf "Date,Hour,Service,Used,Total\n" > Service_Stats.csv #Uncomment this line on initial Run to create file with headders
cat $ServiceStatsPre | awk '{if (NR !=1) print}' |
awk '{
printf("%-1s,%1s,%1s,%4.2f,%4.2f\n",$5,$6,$1,$3,$4);
}' >> Service_Stats.csv
rm $service;
rm $jstatop;
rm $ServiceStatsPre;
Breakdown
ps -ef | grep java | grep Service | awk '{key=substr($9,31,match($9,"#")-31); serv[key]++}END{for(name in serv){if($9 ~ /Service/){print name}}}'>$service
This is returning the name of the Service and putting it to a tempary file $service
printf "%-8sService\t\t\t\t%7s PID\t%7s Used\t%7s Total\n" > $ServiceStatsPre
This is just creating Headings in a Temp file $ServiceStatsPre (I know this isn't needed but I was originally using it to debug to a file not a temp file)
Outer While loop
filename=$service
if [ ! -z $line ]; then
while read -r line; do
pid=$(ps -ef | grep java | grep Service |awk -v svc="$line" '{if (match($9,svc)){print $2}}')
rncnt=0
rnag=1
...
cat $jstatop | awk '{if (NR !=1) print}'|awk -v pid="$pid" -v svc="$line" -v d="$datet" -v h="$hourt" '{printf("%-40s %7d %6.2fMB %6.2fMB %11s %3s\n",svc,pid,($1+$5+$7)/1024,($3+$4+$6+$8)/1024,d,h)}' >> $ServiceStatsPre
fi
done < $filename
This is first checking to see if the line that it is passing in is not empty (if [ ! -z $line ]; then) then it is extracting the PID for each of the services in the temp file $service and is setting a check (rnag) + retry counter (rncnt). The final part of this outer while loop is calculating the stats, memory usage in MB (($1+$5+$7)/1024), Total Memory used (($3+$4+$6+$8)/1024) as well as retuning the PID,Service Name,Date and Hour.
Inner While Loop
...
while [ $rnag -eq 1 ]; do
jstat -gc $pid > $jstatop
if [ $? -ne 0 ]; then
sleep 5;
rncnt++;
else
rnag=0
fi
if [ $rncnt -eq 5 ]; then
rnag=0
fi
done
...
This is to basically ensure that the jstat is running correctly and isn't returning an error.
The final part of this script:
#printf "Date,Hour,Service,Used,Total\n" > Service_Stats.csv #Uncomment this line on initial Run to create file with headders
cat $ServiceStatsPre | awk '{if (NR !=1) print}' |
awk '{
printf("%-1s,%1s,%1s,%4.2f,%4.2f\n",$5,$6,$1,$3,$4);
}' >> Service_Stats.csv
is just to resolve the formatting and put it into a format so that it can be used in a CSV.
Output
This script out puts like the following:
Date,Hour,Service,Used,Total
2016-07-03,07,undMFSAdapterServiceVM,331.00,188.87
2016-07-03,07,entSCSServiceVM,332.50,278.19
2016-07-03,07,kServiceVM,457.50,132.91
2016-07-03,07,ServiceTuTrackVm,432.00,282.66
2016-07-03,07,ter1WMSAdapterServiceVm,233.00,77.02
2016-07-03,07,kingMFSAdapterServiceVM,451.50,126.69
2016-07-03,07,erBuilderServiceVM,261.50,211.27
2016-07-03,07,ter3MFSAdapterServiceVM,449.50,210.23
2016-07-03,07,rServiceVM1,1187.00,529.26
2016-07-03,07,rServiceVM2,597.50,398.43
2016-07-03,07,rServiceVM3,2786.00,819.30
2016-07-03,07,rServiceVM4,451.50,163.13
2016-07-03,07,MessagingServiceVm,457.50,357.11
2016-07-03,07,viceVM,444.50,263.59
2016-07-03,07,ServiceVM,1910.50,909.19
2016-07-03,07,undPackingMFSAdapterServiceVM,208.00,113.51
2016-07-03,07,gisticLockServiceVM,245.00,173.05
2016-07-03,07,kingMFSAdapterServiceVM,781.50,327.13
2016-07-03,07,ferWMSAdapterServiceVm,196.00,84.02
2016-07-03,07,geMFSAdapterServiceVM,499.50,256.91
2016-07-03,07,ferMFSAdapterServiceVM,456.50,246.89
2016-07-03,07,kingWMSAdapterServiceVm,195.00,73.70
2016-07-03,07,AdapterServiceVm,149.50,72.62
2016-07-03,07,ter2MFSAdapterServiceVM,455.00,136.02
2016-07-03,07,ionServiceVM,484.00,240.46
2016-07-03,07,ter3WMSAdapterServiceVm,266.00,138.70
2016-07-03,07,iceVm,135.50,106.07
2016-07-03,07,viceVm,3317.00,1882.15
2016-07-03,07,entBCSServiceVM,356.50,143.93
2016-07-03,07,ServiceVm,951.00,227.12
2016-07-03,07,lingServiceVM,145.50,76.61
2016-07-03,07,entDBHServiceVM,182.50,4.63
2016-07-03,07,kingWMSAdapterServiceVm,208.00,103.13
2016-07-03,07,gingServiceVM,1529.50,235.84
2016-07-03,07,ServiceVM,249.50,131.78
2016-07-03,07,ter1MFSAdapterServiceVM,453.00,394.11
2016-07-03,07,AdapterServiceVM,461.00,208.41
2016-07-03,07,ter2WMSAdapterServiceVm,178.50,79.93
2016-07-03,07,AdapterServiceVm,395.00,131.08
2016-07-03,07,ingServiceVM,184.50,126.28
The $3 has been trimmed just to hide service names etc..
Helpful Links
A nice explanation of jstat -gc output
I am trying to write a script that will return info from the /proc/cpuinfo, /proc/meminfo and /proc/version files.
From the cpuinfo file, I want to return the cpu Mhz and model name.
I can get these via these commands
more /proc/cpuinfo | grep "model name" | head -n 1
more /proc/cpuinfo | grep "cpu MHz"
for the meminfo file, I want to get total memory, memory free and total used. I can get the first 2 via these commands:
more /proc/meminfo | grep MemTotal
more /proc/meminfo | grep MemFree
and I can get the linux version # with this:
more /proc/version
I can then have this saved as a file via redirecting the first output into a file and then append the next info items with using a >> instead of >.
My problem is this - how do I write a script that will take the info from the above and place it into this format:
/proc/cpuinfo, Model name: (result of first command above)
/proc/cpuinfo, cpu Mhz: (result of 2nd)
/proc/meminfo, MemTotal: (result of 3rd)
/proc/meminfo, MemFree: (result of 4th)
/proc/meminfo, MemUsed: (calculate it based off memtotal and memfree)
/proc/version, Linux version #:
I know how to use cut, awk and more, etc but do not know how to set this up. I do not know how to force the calculation of the mem used either.
Any help you can give would be appreciated.
EDIT: I use the more because I am not too familiar with Linux.
I am getting closer and closer to what I want to do with a combination of what is posted here and what I need to come up with.
MATH function -
I just want to take the memtotal and subtract memfree from it.
Could I just create a variable such as
memused=$(bc $memtotal - memfree)
and then echo it out?
With a simple shell function like:
filedata() {
grep -H "$#" | sed -e 's/:/, /'
}
You can get most of the data you need by calling
filedata 'model name' /proc/cpuinfo
filedata -E 'Mem(Total|Free)' /proc/meminfo
filedata . /proc/version
To get MemUsed you could use something like:
awk '/MemFree/ {free=$2} /MemTotal/ {total=$2} END {print FILENAME",","MemUsed:", total-free}' /proc/meminfo
Alternatively the following awk script will do it all for you (though not in exactly the order of your example output):
awk '/model name|cpu MHz|MemTotal|MemFree|^Linux/ {
print FILENAME",",$0
}
/MemTotal|MemFree/ {
v=$1
gsub(/^Mem/, "", v)
gsub(/:$/, "", v)
mem[v]=$2
}
END {
print "/proc/meminfo, MemUsed:", mem["Total"] - mem["Free"]
}' /proc/cpuinfo /proc/meminfo /proc/version
In a simplified way you can do the following:
EXAMPLE
#!/bin/sh
LOCATION=$1
if [ "$#" -ne "1" ]
then
echo "Usage: ./$0 <FILE>"
else
model=$(cat /proc/cpuinfo |grep -m 1 "model name"|cut -d' ' -f 4-);
mhz=$(cat /proc/cpuinfo |grep -m 1 "cpu MHz"|cut -d' ' -f 3-);
mem=$(cat /proc/meminfo | grep MemTotal|cut -d' ' -f 2-);
free=$(cat /proc/meminfo | grep MemFree|cut -d' ' -f 2-);
ver=$(cat /proc/version|cut -d' ' -f 3);
fi
echo -e \
"/proc/cpuinfo, Model Name: $model
/proc/cpuinfo, CPU MHz: $mhz
/proc/meminfo, MemTotal: $mem
/proc/meminfo, MemFree: $free
/proc/version, Linux Verion #: $ver" > $LOCATION
That will place each result in a variable so you can echo it into a file that you declare when you call the script like sh test.sh mynewfile.txt.
As for "I do not know how to force the calculation of the mem used either." please update you question to include how you expect those values to be present (kb, MB, GB) and a sample output you are looking for.
Here is how to get value using awk only:
model=$(awk -F: '/model name/ {print $2;exit}' /proc/cpuinfo)
mhz=$(awk -F: '/cpu MHz/ {print $2;exit}' /proc/cpuinfo)
mem=$(awk -F"[: ]+" '/MemTotal/ {print $2;exit}' /proc/meminfo)
etc
I think I may have a memory leak in my LAMP application (memory gets used up, swap starts getting used, etc.). If I could see how much memory the various processes are using, it might help me resolve my problem. Is there a way for me to see this information in *nix?
Getting right memory usage is trickier than one may think. The best way I could find is:
echo 0 $(awk '/TYPE/ {print "+", $2}' /proc/`pidof PROCESS`/smaps) | bc
Where "PROCESS" is the name of the process you want to inspect and "TYPE" is one of:
Rss: resident memory usage, all memory the process uses, including all memory this process shares with other processes. It does not include swap;
Shared: memory that this process shares with other processes;
Private: private memory used by this process, you can look for memory leaks here;
Swap: swap memory used by the process;
Pss: Proportional Set Size, a good overall memory indicator. It is the Rss adjusted for sharing: if a process has 1MiB private and 20MiB shared between other 10 processes, Pss is 1 + 20/10 = 3MiB
Other valid values are Size (i.e. virtual size, which is almost meaningless) and Referenced (the amount of memory currently marked as referenced or accessed).
You can use watch or some other bash-script-fu to keep an eye on those values for processes that you want to monitor.
For more informations about smaps: http://www.kernel.org/doc/Documentation/filesystems/proc.txt.
I don't know why the answer seem so complicated... It seems pretty simple to do this with ps:
mem()
{
ps -eo rss,pid,euser,args:100 --sort %mem | grep -v grep | grep -i $# | awk '{printf $1/1024 "MB"; $1=""; print }'
}
Example usage:
$ mem mysql
0.511719MB 781 root /bin/sh /usr/bin/mysqld_safe
0.511719MB 1124 root logger -t mysqld -p daemon.error
2.53516MB 1123 mysql /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib/mysql/plugin --user=mysql --pid-file=/var/run/mysqld/mysqld.pid --socket=/var/run/mysqld/mysqld.sock --port=3306
Use ps to find the process id for the application, then use top -p1010 (substitute 1010 for the real process id).
The RES column is the used physical memory and the VIRT column is the used virtual memory - including libraries and swapped memory.
More info can be found using "man top"
First get the pid:
ps ax | grep [process name]
And then:
top -p PID
You can watch various processes in the same time:
top -p PID1 -p PID2
You can use pmap to report memory usage.
Synopsis:
pmap [ -x | -d ] [ -q ] pids...
In case you don't have a current or long running process to track, you can use /usr/bin/time.
This is not the same as Bash time (as you will see).
Eg
# /usr/bin/time -f "%M" echo
2028
This is "Maximum resident set size of the process during its lifetime, in Kilobytes" (quoted from the man page). That is, the same as RES in top et al.
There are a lot more you can get from /usr/bin/time.
# /usr/bin/time -v echo
Command being timed: "echo"
User time (seconds): 0.00
System time (seconds): 0.00
Percent of CPU this job got: 0%
Elapsed (wall clock) time (h:mm:ss or m:ss): 0:00.00
Average shared text size (kbytes): 0
Average unshared data size (kbytes): 0
Average stack size (kbytes): 0
Average total size (kbytes): 0
Maximum resident set size (kbytes): 1988
Average resident set size (kbytes): 0
Major (requiring I/O) page faults: 0
Minor (reclaiming a frame) page faults: 77
Voluntary context switches: 1
Involuntary context switches: 0
Swaps: 0
File system inputs: 0
File system outputs: 0
Socket messages sent: 0
Socket messages received: 0
Signals delivered: 0
Page size (bytes): 4096
Exit status: 0
More elegant approach:
echo "Memory usage for PID <>:"; for mem in {Private,Rss,Shared,Swap,Pss};do grep $mem /proc/<pid>/smaps | awk -v mem_type="$mem" '{i=i+$2} END {print mem_type,"memory usage:"i}' ;done
Use top or htop and pay attention to the "RES" (resident memory size) column.
Thanks. I used this to create this simple bash script that can be used to watch a process and its memory usage:
$ watch watchmypid.sh
#!/bin/bash
#
PROCESSNAME=changethistoyourprocessname
MYPID=`pidof $PROCESSNAME`
echo "=======";
echo PID:$MYPID
echo "--------"
Rss=`echo 0 $(cat /proc/$MYPID/smaps | grep Rss | awk '{print $2}' | sed 's#^#+#') | bc;`
Shared=`echo 0 $(cat /proc/$MYPID/smaps | grep Shared | awk '{print $2}' | sed 's#^#+#') | bc;`
Private=`echo 0 $(cat /proc/$MYPID/smaps | grep Private | awk '{print $2}' | sed 's#^#+#') | bc;`
Swap=`echo 0 $(cat /proc/$MYPID/smaps | grep Swap | awk '{print $2}' | sed 's#^#+#') | bc;`
Pss=`echo 0 $(cat /proc/$MYPID/smaps | grep Pss | awk '{print $2}' | sed 's#^#+#') | bc;`
Mem=`echo "$Rss + $Shared + $Private + $Swap + $Pss"|bc -l`
echo "Rss " $Rss
echo "Shared " $Shared
echo "Private " $Private
echo "Swap " $Swap
echo "Pss " $Pss
echo "=================";
echo "Mem " $Mem
echo "=================";
The tool you want is ps.
To get information about what java programs are doing:
ps -F -C java
To get information about http:
ps -F -C httpd
If your program is ending before you get a chance to run these, open another terminal and run:
while true; do ps -F -C myCoolCode ; sleep 0.5s ; done
You can use pmap + awk.
Most likely, we're interested in the RSS memory which is the 3rd column in the last line of the example pmap output below (82564).
$ pmap -x <pid>
Address Kbytes RSS Dirty Mode Mapping
....
00007f9caf3e7000 4 4 4 r---- ld-2.17.so
00007f9caf3e8000 8 8 8 rw--- ld-2.17.so
00007fffe8931000 132 12 12 rw--- [ stack ]
00007fffe89fe000 8 8 0 r-x-- [ anon ]
ffffffffff600000 4 0 0 r-x-- [ anon ]
---------------- ------ ------ ------
total kB 688584 82564 9592
Awk is then used to extract that value.
$ pmap -x <pid> | awk '/total/ { print $4 "K" }'
The pmap values are in kilobytes. If we wanted it in megabytes, we could do something like this.
$ pmap -x <pid> | awk '/total/ { print $4 / 1024 "M" }'
Why all these complicated answers with various shell scripts?
Use htop, it automatically changes the sizes and you can select which info you want shown and it works in the terminal, so it does not require a desktop.
Example: htop -d8
Use
ps u `pidof $TASKS_LIST` or ps u -C $TASK
ps xu --sort %mem
ps h -o pmem -C $TASK
Example:
ps-of()
{
ps u `pidof "$#"`
}
$ ps-of firefox
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
const 18464 5.9 9.4 1190224 372496 ? Sl 11:28 0:33 /usr/lib/firefox/firefox
$ alias ps-mem="ps xu --sort %mem | sed -e :a -e '1p;\$q;N;6,\$D;ba'"
$ ps-mem
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
const 3656 0.0 0.4 565728 18648 ? Sl Nov21 0:56 /usr/bin/python /usr/lib/ubuntuone-client/ubuntuone-syncdaemon
const 11361 0.3 0.5 1054156 20372 ? Sl Nov25 43:50 /usr/bin/python /usr/bin/ubuntuone-control-panel-qt
const 3402 0.0 0.5 1415848 23328 ? Sl Nov21 1:16 nautilus -n
const 3577 2.3 2.0 1534020 79844 ? Sl Nov21 410:02 konsole
const 18464 6.6 12.7 1317832 501580 ? Sl 11:28 1:34 /usr/lib/firefox/firefox
$ ps h -o pmem -C firefox
12.7