bobby
bobby

Reputation: 305

CPU load of my program

How can we know that how much load does our program is on CPU?

I tried to find it using htop. But htop wont give the cpu load. It actually gives the cpu utilization percentage of my program(using pid).

I am using C programming, Linux environment.

Upvotes: 2

Views: 446

Answers (1)

sirgeorge
sirgeorge

Reputation: 6531

The function you are probably looking for is getrusage. It fills struct rusage. There are two members of the struct you are interested in:

  1. ru_utime - user CPU time used
  2. ru_stime - system CPU time used

You can call the function at regular intervals of time and based on the results you can estimate the cpu load (e.g. in percentage) of your own process.

If you want to get it at the system level, then you need to read (and parse) /proc/stat file (also at regular intervals), see here.

Upvotes: 2

Related Questions