Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capture CPU and Memory usage dynamically

I am running a shell script to execute a c++ application, which measures the performance of an api. i can capture the latency (time taken to return a value for a given set of parameters) of the api, but i also wish to capture the cpu and memory usage alongside at intervals of say 5-10 seconds.

is there a way to do this without effecting the performance of the system too much and that too within the same script? i have found many examples where one can do outside (independently) of the script we are running; but not one where we can do within the same script.

like image 441
gagneet Avatar asked Apr 30 '09 13:04

gagneet


People also ask

How do you track memory usage of a particular process?

You can check memory of a process or a set of processes in human readable format (in KB or kilobytes) with pmap command. All you need is the PID of the processes you want to check memory usage of. As you can see, the total memory used by the process 917 is 516104 KB or kilobytes.

How do I monitor CPU usage in Python?

Use the os Module to Retrieve Current CPU Usage in Python We can use the cpu_count() function from this module to retrieve the CPU usage. The psutil. getloadavg() function provides the load information about the CPU in the form of a tuple.


1 Answers

If you are looking for capturing CPU and Mem utilization dynamically for entire linux box, then following command can help you too:

CPU

vmstat -n 15 10| awk '{now=strftime("%Y-%m-%d %T "); print now $0}'> CPUDataDump.csv &

vmstat is used for collection of CPU counters

-n for delay value, in this case it's 15, that means after every 15 sec, stats will be collected.

then 10 is the number of intervals, there would be 10 iterations in this example

awk '{now=strftime("%Y-%m-%d %T "); print now $0}' this will dump the timestamp of each iteration

in the end, the dump file with & for continuation

Memory

free -m -s 10 10 | awk '{now=strftime("%Y-%m-%d %T "); print now $0}'> DataDumpMemoryfile.csv &

free is for mem stats collection

-m this is for units of mem (you can use -b for bytes, -k for kilobytes, -g for gigabytes)

then 10 is the number of intervals (there would be 10 iterations in this example)

awk'{now=strftime("%Y-%m-%d %T "); print now $0}' this will dump the timestamp of each iteration

in the end, the dump & for continuation

like image 51
Prateek Gupta Avatar answered Sep 28 '22 12:09

Prateek Gupta