Hi Group,

for a project[1] I am trying to find an OS independant way to measure the amount of cpu usage and memory consumption of the program. It would be nice if I could do that in my C++ code, so I can show the results at runtime or even plot a nice graph (I got wild dreams).

After some google'ing I found some (nice) examples[2] but they are all windows based. Does universal code for this problem exist?

Of course I can make different classes (one for windows and another for *nix), I do something similar to assign sockets. But then I also need code for *nix, without using bash-scripting of course.

So I am looking for something OS independant, but examples for windows or *nix can help me out for now as well.

Goodnight everyone and thanks in advance,

[1] I am trying to compare some differint multicast protocols, and yes, also cpu usage and memory consumption is an important part of that :-)
[2] if anybody wants them, I can always post links, just ask

Recommended Answers

All 8 Replies

im not thinking you will find universal code for what you want to do. but good luck on your search i will keep an eye open for you :)


There are some ways to do this.

For CPU monitoring (and network), you can look into a package called mrtg that can be setup to monitor CPU processes, and graph it out. It will require you to setup snmpd and other things in order to get it to function properly.

For code, and how much CPU you use, check out your compiler to see if it supports profiling. I did that for COBOL code on a VAX/VMS, and for C++ on my Macintosh. Found out what functions used the most CPU time, and how long it took to do them. I liked profiling sorts, and comparing quicksort vs. bubble sort and the like.

Now, I know it isn't cross-platform, but the profiling might be the most efficient and non-skewing of the techniques. Why? because it is built into the compiler environment, and I am sure that the program coders took that into account.


BigK: Yeah, I kind of gave up the search to "universal code" (I just like the sound of it (-:). I guess I'll have to write separate implementations for each OS and use conditional compile statements in the source code.

kc0arf: the problem is that I want to do it at run time, in the code itself. the idea is that my program plots a graph (or make a log file, whatever), so every (future) user can see how much resources a protocol uses, without needing to know to go to the /proc-dir

Another major problem is that I haven't found interesting code concerning the memory consumption. Until know, I only found ways to measure CPU-cycles and only for windows. Can anyone help me?

Many kind greetings and thanks in advance,

--wim deprez

You could try looking in *nix development fourms.Also try looking in google.You will definately find good info if you search properly.

Well I guess that is my problem then, I first started looking in to Google, but didn't find anything satisfacting to my needs, that is why I started posting on forums. Maybe I do not use the right words to query.

I found some examples for the windowssystem:
http://www.codeproject.com/system/cpuusage.asp and
http://www.codeguru.com/Cpp/V-S/debug/article.php/c4415/ . Now I am
wondering if it would be a bad idea to use a filepointer to read the
/proc/stat in *nix systems. For now, it is the only posibility I can come up
with. If somebody has a better idea, you're welcome.

And then I still need to find a good solution to check the memory usage. At comp.lang.c++ they referred me to the "Available C++ Libraries FAQ" by Nikki Locke (http://www.trumphurst.com/cpplibs/cpplibs.phtml) but I couldn't find a solution.

Thanks for your help, but I am still on my quest :-)

Many kind greetings,

--wim deprez

i need to compute the cpu usage time for my function calls...how do i do that in linux with C++ as my programming language.
Thanks in advance.

I have similar problem. Propably even more difficult:P I'd like to measure processor usage while my application is working but on windows mobile. Moreover I'd like to write my own class to do it. Have you got some ideas?

Stop posting in this thread o.0 its 4 years old.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.