0

How can I get the total percentage or volume of memory usage and CPU consumption from within C++11 code on ubuntu (or any other linux-based system)?

The usage I want to get is for the whole system, not just for current thread or process.

hnefatl
  • 5,860
  • 2
  • 27
  • 49
Grigor Gevorgyan
  • 6,753
  • 4
  • 35
  • 64
  • 2
    What have you tried? There is no way of doing this using the features of Standard C++. –  May 17 '18 at 17:37
  • @NeilButterworth I tried to google and didn't find a sufficient answer :) also, I don't need to use standard c++, I just need to do it on linux system using some linux-specific system info files presumably – Grigor Gevorgyan May 17 '18 at 18:29
  • 1
    Possible duplicate of [How to determine CPU and memory consumption from inside a process?](https://stackoverflow.com/q/63166/608639), [How to get current CPU and RAM usage in C++?](https://stackoverflow.com/q/479722/608639), [Get RAM and CPU usage for process in Linux with C++](https://stackoverflow.com/q/6659460/608639), [How to get total CPU usage in Linux using C++](https://stackoverflow.com/q/3017162/608639), [How to get memory usage at runtime using C++?](https://stackoverflow.com/q/669438/608639), etc. – jww May 18 '18 at 00:00

1 Answers1

0

Read about proc(5) and use /proc/meminfo. You can open it and read it as a sequential file (even if it often behaves as a pipe; e.g. it is not seekable).

Of course, this is Linux specific.

And you might scan the /proc/ directory (using opendir(3), readdir(3), closedir) for directories starting with a digit, and read each of their /proc/1234/maps or /proc/1234/status - these correspond to process 1234)

Read also linuxatemyram

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547