aiman119
Posts: 2
Joined: Wed Jan 20, 2016 7:57 am

Creating Script to Log "top" Command Output

Fri Jan 22, 2016 5:02 am

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.

User avatar
PeterO
Posts: 5440
Joined: Sun Jul 22, 2012 4:14 pm

Re: Creating Script to Log "top" Command Output

Fri Jan 22, 2016 7:57 am

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
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

jahboater
Posts: 5027
Joined: Wed Feb 04, 2015 6:38 pm

Re: Creating Script to Log "top" Command Output

Sat Jan 23, 2016 12:22 am

Rather than trying to interpret the output of top, perhaps another way might be to get the source and modify it.
"We are in the beginning of a mass extinction, and all you can talk
about is money and fairy tales of eternal economic growth."
- Greta Thunberg

User avatar
PeterO
Posts: 5440
Joined: Sun Jul 22, 2012 4:14 pm

Re: Creating Script to Log "top" Command Output

Sat Jan 23, 2016 9:08 am

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
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

User avatar
DougieLawson
Posts: 37074
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Creating Script to Log "top" Command Output

Sat Jan 23, 2016 11:29 am

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:~$
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

User avatar
PeterO
Posts: 5440
Joined: Sun Jul 22, 2012 4:14 pm

Re: Creating Script to Log "top" Command Output

Sat Jan 23, 2016 11:44 am

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
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

User avatar
DougieLawson
Posts: 37074
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Creating Script to Log "top" Command Output

Sat Jan 23, 2016 5:17 pm

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.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

ejolson
Posts: 4249
Joined: Tue Mar 18, 2014 11:47 am

Re: Creating Script to Log "top" Command Output

Sat Jan 23, 2016 6:09 pm

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?

jahboater
Posts: 5027
Joined: Wed Feb 04, 2015 6:38 pm

Re: Creating Script to Log "top" Command Output

Sun Jan 24, 2016 12:51 am

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!
"We are in the beginning of a mass extinction, and all you can talk
about is money and fairy tales of eternal economic growth."
- Greta Thunberg

Return to “C/C++”