Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding CPU utilization in c++ using libproc

Tags:

c++

cpu-usage

I have been using libproc library in c++ to find the CPU utilization but I cannot find any examples to retrieve some fields.

  1. I want to calculate the cpu usage of a process/thread?
  2. To find the memory usage of a process/thread?

If any one knows please help.

like image 707
Charzhard Avatar asked Nov 21 '25 07:11

Charzhard


1 Answers

Instantaneous CPU percentage is commonly desired, but is not tracked by the kernel and is therefore not available anywhere procps can read. Tracking a percentage has to be implemented in the application by taking a snapshot, waiting a little while, and taking another snapshot to learn the utime+stime spent during the interval. This is the reason why top shows all CPU percentages as 0.0% when it starts, and corrects them on the next interval. procps provides a convenient place to store the CPU percentage, but does not implement it in the library.

Taken it from here, and there is some more information on how to implement it there.

like image 188
Etherealone Avatar answered Nov 22 '25 20:11

Etherealone