Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ figure out CPU/Memory usage

I have a C++ app called ./blah (to which I have the source code)

when I run ./blah

I can run "top" and see how much memory & cpu "./blah" is using.

Now, is there anyway for "./blah" to access that information itself? I.e. when I run ./blah, I want it to every second dump out it's CPU & Memory usage. What library should I be using to do this?

I'm on MacOSX; but I'd prefer a solution that works on Linux too.

Thanks!

like image 884
anon Avatar asked Feb 16 '10 23:02

anon


People also ask

How do I check my CPU memory usage?

Press Ctrl + Shift + Esc to launch Task Manager. Or, right-click the Taskbar and select Task Manager. Select the Performance tab and click Memory in the left panel. The Memory window lets you see your current RAM usage, check RAM speed, and view other memory hardware specifications.

How do you calculate memory usage?

Keeping in mind the formula, MEM%= 100-(((free+buffers+cached)*100)/TotalMemory).

How do I check high memory usage?

Check Computer Memory Usage EasilyTo open up Resource Monitor, press Windows Key + R and type resmon into the search box. Resource Monitor will tell you exactly how much RAM is being used, what is using it, and allow you to sort the list of apps using it by several different categories.

How do I tell how much RAM my terminal is using?

Entering cat /proc/meminfo in your terminal opens the /proc/meminfo file. This is a virtual file that reports the amount of available and used memory. It contains real-time information about the system's memory usage as well as the buffers and shared memory used by the kernel.


1 Answers

You want getrusage(). From the man page:

int getrusage(int who, struct rusage *r_usage);

getrusage() returns information describing the resources utilized by the current process, or all its terminated child processes. The who parameter is either RUSAGE_SELF or RUSAGE_CHILDREN. The buffer to which r_usage points will be filled in with the following structure:

struct rusage {
         struct timeval ru_utime; /* user time used */
         struct timeval ru_stime; /* system time used */
         long ru_maxrss;          /* integral max resident set size */
         long ru_ixrss;           /* integral shared text memory size */
         long ru_idrss;           /* integral unshared data size */
         long ru_isrss;           /* integral unshared stack size */
         long ru_minflt;          /* page reclaims */
         long ru_majflt;          /* page faults */
         long ru_nswap;           /* swaps */
         long ru_inblock;         /* block input operations */
         long ru_oublock;         /* block output operations */
         long ru_msgsnd;          /* messages sent */
         long ru_msgrcv;          /* messages received */
         long ru_nsignals;        /* signals received */
         long ru_nvcsw;           /* voluntary context switches */
         long ru_nivcsw;          /* involuntary context switches */
 };
like image 114
Carl Norum Avatar answered Sep 28 '22 19:09

Carl Norum