Bits of Bytes
bits of coding, C++, Qt, git, gamedev, linux and other tech stuff
December 17, 2016
active, C++, CLI, code, coding, command line, command line interface, cpp, CPU, file, idle, Linux, parsing, proc, processing, program, programming, source, stat, statistics, stats, tokenization, usage
This article explains how to create a simple program
← Previous post
Next post →
Just a thought.
You are now doing string matching and parsing to get the cpu statistics, but there also seems to be c kernel functions for exactly that. It would save you the hassle of parsing /proc/stat and it will be quite a bit more efficient to use the c functions.
Have a look in the stat implementation: https://github.com/torvalds/linux/blob/master/fs/proc/stat.c
The downside is that your code will be bigger if you go that route and it will probably have to be recompiled if your kernel gets updated..
I haven’t looked into this much, but to the best of my knowledge programs in user space can’t call kernel functions.
The only way to communicate with the kernel is using syscalls.
I realized that right after posting my message 😉
You would have to make a kernel module, expose a syscall and use that in user space.
That would be quite a complicated piece of code for something seemingly simple.
This does make me think.. Why don’t the /proc/* provide an output that is easy to parse. Like json or so. The output as is right now seems to be focused on human readability.
I am not a kernel developer, but I believe the philosophy behind it is providing information in a compact and “straight to the point” way. It’s low-level stuff after all.
Parsing space-separated numbers is not that hard after all, as you can see in my (simple) example code. 🙂
just tried it on a raspberry pi, it shows high loads while the system monitor shows the oposite
Your email address will not be published. Required fields are marked *
Save my name, email, and website in this browser for the next time I comment.