Page 1 of 1

Creating Script to Log "top" Command Output

Posted: Fri Jan 22, 2016 5:02 am
by aiman119
First and foremost, I do apologize for the lack of knowledge. I just started to get into the Linux side of the world and I'm currently trying to get back into coding. Anyway, I wanted to create a script that used the "top" command to log CPU load and RAM usage to a certain file every specified time period. Here are the specific points I really need:
  • CPU load on each core (most important)
    CPU vs GPU RAM usage
    Only specified processes (not the entire list)
    A write period of something like every 5 min but is also easily variable if necessary
A plus would also be temperatures of the CPU at those time intervals as well. Another plus would be graphing them. Again I apologize for the ignorance, but I am very interested and willing to learn. Thanks.

Re: Creating Script to Log "top" Command Output

Posted: Fri Jan 22, 2016 7:57 am
by PeterO
You say you want a "script" yet you've posted this in the C/C++ forum. C and C++ are not scripting languages, they are compiled languages.

If you really want a script, then Python would be the way to go and in fact I would suggest Python for this sort of task anyway,

PeterO

Re: Creating Script to Log "top" Command Output

Posted: Sat Jan 23, 2016 12:22 am
by jahboater
Rather than trying to interpret the output of top, perhaps another way might be to get the source and modify it.

Re: Creating Script to Log "top" Command Output

Posted: Sat Jan 23, 2016 9:08 am
by PeterO
jahboater wrote:Rather than trying to interpret the output of top, perhaps another way might be to get the source and modify it.
Maybe, but that will at least show where "top" gets the information from and I expect for OP case it will be easier to start from scratch rather than try to adapt the "top" code which will have lots of unwanted features in it.

PeterO

Re: Creating Script to Log "top" Command Output

Posted: Sat Jan 23, 2016 11:29 am
by DougieLawson
jahboater wrote:Rather than trying to interpret the output of top, perhaps another way might be to get the source and modify it.
Or just read the raw data from /proc/stat

Code: Select all

pi@eagle:~$ cat /proc/stat
cpu  1218339 0 21806 5167688 18682 0 593 0 0 0
cpu0 53679 0 7153 1529678 6192 0 582 0 0 0
cpu1 680433 0 3598 924124 2325 0 0 0 0 0
cpu2 87474 0 6656 1509227 6014 0 8 0 0 0
cpu3 396753 0 4399 1204659 4151 0 3 0 0 0
intr 9731008 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2714215 0 0 0 0 0 530 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 14042 0 8391 0 0 0 0 0 0 0 0 0 0 0 6529255 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 16027 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
ctxt 4360765
btime 1453532407
processes 13185
procs_running 2
procs_blocked 0
softirq 5181676 956449 2449555 0 27470 0 0 266575 937837 0 543790
pi@eagle:~$

Re: Creating Script to Log "top" Command Output

Posted: Sat Jan 23, 2016 11:44 am
by PeterO
DougieLawson wrote:
jahboater wrote:Rather than trying to interpret the output of top, perhaps another way might be to get the source and modify it.
Or just read the raw data from /proc/stat
OP also needs some per process info for "specified processes"

PeterO

Re: Creating Script to Log "top" Command Output

Posted: Sat Jan 23, 2016 5:17 pm
by DougieLawson
PeterO wrote: OP also needs some per process info for "specified processes"
That will need a deeper look at the code for the procfs tools. If they know the pid it should be easy to find things in /proc/$pid.

Re: Creating Script to Log "top" Command Output

Posted: Sat Jan 23, 2016 6:09 pm
by ejolson
PeterO wrote:You say you want a "script" yet you've posted this in the C/C++ forum. C and C++ are not scripting languages, they are compiled languages.
Having seen how much memory and CPU some resource monitoring deamons written in interpreted languages use, it seems like a good idea to write a simple one in C for the Pi. Back in the early days of Linux I wrote a resource monitor in C that kept track of load level, network utilization, page rate and memory used. This involved reading files in proc at suitable time intervals and writing a new record to a log file. After working on it for about a week my resource monitor included a web front end with graphs generated by gnuplot. On the Pi I would suggest also logging temperature.

Out of curiosity, how do you monitor GPU memory usage on the Pi?

Re: Creating Script to Log "top" Command Output

Posted: Sun Jan 24, 2016 12:51 am
by jahboater
Back in the early days of Linux I wrote a resource monitor in C that kept track of load level, network utilization, page rate and memory used. This involved reading files in proc
In the really early days (UNIX), much more fun was to get the kernel symbol table, open /dev/kmem, find the process table, and lseek() around getting the entries. Then Solaris had "kstat", finally we had /proc which is easy!