从C ++ 11代码中获取系统内存和CPU使用率 [英] Get system memory and CPU usage from within C++11 code
问题描述
如何从ubuntu(或任何其他基于linux的系统)上的C ++ 11代码中获取内存使用和CPU消耗的总百分比或数量?
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.
推荐答案
阅读有关< a href = http://man7.org/linux/man-pages/man5/proc.5.html rel = nofollow noreferrer> proc(5)并使用 / proc / meminfo
。您可以打开它并将其作为顺序文件读取(即使它经常表现为管道;例如,它也不可搜索)。
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).
当然,这是特定于Linux的。
Of course, this is Linux specific.
您可能会扫描 / proc /
目录(使用 opendir(3), readdir(3), closedir
),用于以数字开头的目录,并读取他们的每个 / proc / 1234 / maps
或 / proc / 1234 / status
中的每一个-这些都对应于流程1234)
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)
也请阅读 linuxatemyram
这篇关于从C ++ 11代码中获取系统内存和CPU使用率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!