简体   繁体   中英

Get a linux process cpu use

I am trying to figure out how to get a process (specified by PID) CPU use from my C++ application. The code is trivial and there are lots of answers online on how to do it with examples but one thing that puzzled me was from what sources should the various values be sampled.

For example, most examples seem to refer to parsing /proc/<pid>/stat but I've seen a few sources (such as chromium source) that actually iterates /proc/<pid>/task/<thr_id> and reads the stat file for each thread and sums the relevant fields before calculating the processes CPU load.

So the question, is there any advantage of using one approach over the other?

I've checked various versions of chromium source and it seems that only older versions used the thread stat files; the latest code here uses just the proc/<pid>/stat file. I guess using the thread stat files just gave more granularity to the data which is not required just for a more general 'how much CPU is process X using".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM