Comments on How To Extract Values From top And Plot Them

How To Extract Values From top And Plot Them Many researchers who are doing performance evaluation and benchmarking need to capture the values of the CPU and the RAM. Others might need to capture the throughput as well. In this short tutorial I will show how I capture the CPU and RAM values from “top” and then extract them in one line command.

17 Comment(s)

Add comment

Please register in our forum first to comment.

Comments

By:

Here's how I would pull that data:

top -bd 1 -n 60 > /tmp/top.txt
grep ^Cpu /tmp/top.txt | awk '{print $5}' | awk -F% '{print $1}' | nl > /tmp/cpu.txt
grep ^Mem /tmp/top.txt | awk '{print $4}' | awk -Fk '{print $1}' | nl > /tmp/mem.txt

By:

You can grep straight from a file, no need to pipe cat output to it.

grep Cpu foo.file

- N

By:

SAR....

By:

Hello,

I would like to make one remark. When extracting the information out of the file top.txt, I would not rely on the fixed number for the column (35-39). If something happens like the number for ni(ce) or sy(stem) are taking more room than they do now, the information of your choice may end up in columns 38-43.

I think it's better to take advantage of the patterns present in the file:

 after every column there is a "," :

cat top.txt | grep ^Cpu | cut -d "," -f 4 | cut -d "%" -f 1

 should do the job.

Note futher that I use the ^ sign before Cpu. That's selecting only the lines which start with "Cpu". By omitting this, a line with Cpu somewhere in it is also selected.

Kind regards,

 Stef Bon

By:

Sorry for asking, but I really enjoyed what you wrote, it helped me very much. I only have one issue, I would like to know what the top 10 processes at the time of the top command to be sent to my text file. So if you could help me with that I would really appreciate that.

Sorry, I am very new to scripting and appreciate the advice.

Thanks in advance.
Lawrence

By: Swapan

Hi,
Following extract I got from top command in linux. Can you please help me to understand what is meant by 0.0%wa?

 As well as I dont know what is si and st means. I will appriciate if you can help me to understand all these things.

 Thanks in advance.
With best regards
Swapan 

Cpu(s): 19.2%us, 19.7%sy, 0.0%ni, 61.1%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st

By: anil

"top" with -b is not working in mac and "top -d > file" is also giving unexpected characters in the output file .... so what to do to get row in top command

By: anon_Emoose

Mac terminal has slightly different parameters you need to pass to make this work:

"top -l 0" in terminal has the same effect of running "top -b"

also "top -i 1" is equivalent to "top -d 1"

By: borium

if you need the load average try this: 

load average updated every 5s:

top -bd 5 > top.txt 

 cat top.txt | grep load | grep average | awk '{ print $12 $13 $14 }' | tr ',' ' ' | nl -i 5 > load.txt


By: Anonymous

This is very helpful for my research. Thanks a lot :)

By:

You have given me a great explanation. It was very useful. Thank a lot.

 

By: Shehada

Thank you. I found it useful.

By: ilya

1. man sar 

2. enjoy

By: JT5D

Here's an alternative approach to pulling the data:

ps -arcwwwxo "command %cpu %mem" | grep -v grep | head -20

By: Kaleb

Very helpfull.thanks alot buddy!!!

By: chowdary

thanks bot, very use full information, thanks for explanation

By: tokyo

or just use this package https://github.com/sweetim/linux-top-parser