plotting cpu and mem consumption

W

Wim Deprez

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,

--wim
______
[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
 
P

Phlip

Wim said:
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.

I'm trying to travel to Tierra del Fuego by sitting perfectly still in my
steno chair.

Your question is a non-sequitur. Cell phones (that support C++) have a fixed
amount of memory of at least three types - flash, ROM, and volatile. My
notebook has a swap file for a huge address space within a finite amount of
volatile storage. Universal code to cover all these situations is
impossible.

In an old-fashioned MS-DOS program, one could spot-check storage by
allocating blocks of memory until malloc() returned NULL. Available storage
equaled the sum of sizes of successfully allocated blocks. A protected-mode
application using virtual memory can't do that, because the swapping system
will continue creating new blocks long after all available memory is
over-committed.
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.

Conditional compilation is your friend.
 
V

Victor Bazarov

Since I don't read alt.comp.lang.learn.c-c++, I removed it from crosspost
list of newsgroups.

Wim Deprez said:
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).

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

Probably not. The best thing is to utilise system-specific ways
of gathering those data (CPU usage) or even use a system-specific
utility (if memory serves me right, it's called "profiler").
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.
And?

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

Well, again, look into using 'clock'. But you'd be much better off
if you don't reinvent the wheel but look into using some kind of
profiling library (or libraries, if no multi-platform one exists).

You should be able to find plenty of interesting libraries in the
"Available C++ Libraries FAQ" published here about monthly by Nikki
Locke.

Victor
 
W

Wim Deprez

Victor said:
Wim Deprez said:
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).
Does universal code for this problem exist?


Probably not. The best thing is to utilise system-specific ways
of gathering those data (CPU usage) or even use a system-specific
utility (if memory serves me right, it's called "profiler").

Yeah, I kind of gave up the search to some kind of "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.

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.

[...]
look into using 'clock'. But you'd be much better off
don't reinvent the wheel but look into using some kind of
profiling library (or libraries, if no multi-platform one exists).

You should be able to find plenty of interesting libraries in the
"Available C++ Libraries FAQ" published here about monthly by Nikki
Locke.

ok, I got the clock-function (from time.h:
http://www.cplusplus.com/ref/ctime/clock.html). I probably overlook
something, because I don't exactly get your point, it is not the /time/ that
it takes to run the program, but the amount cpu it uses, it is possible that
other programs are running in the background and such.

So I am looking through the Libraries FAQ, but I didn't find something of a
'profiler', only the 'mpatrol', but that is about debugging dynamically
allocated memory...

And I am still looking for something to measure the memory usage.

Thanks for the tips, I didn't know the Libraries FAQ (quite new to the
group).

Many kind greetings,

--wim
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top