user435739
user435739

Reputation:

top vs /proc/pid/stat which is better for cpu% usage calculation

Which is the better and more precise in calculating cpu_time consumed by a process?

a) Top b) cat /proc/pid/stat

Upvotes: 0

Views: 4395

Answers (3)

Generic Ratzlaugh
Generic Ratzlaugh

Reputation: 747

Top can be used in scripts using the batch mode option. For example, try: top -b -n 1 But, you will either want to use -n 2 to get only current info from the second report, or set CPULOOP=1 first and then use -n 1. See: http://linux.die.net/man/1/top for more info.

Upvotes: 2

Santosh A
Santosh A

Reputation: 5351

The output of both "top" and "cat /proc/pid/stat" are same at any given point of time.
The difference is the way in which the output are displayed.

For "top" , the output is dynamically displayed on the screen. The output can't be saved to a file, wherein unknown characters are written in the file as it is dynamic. The output printed on the screen has the status of all the processes.

For "cat /proc/pid/stat" , the current status of the process is printed on the screen. This is done only once but for "top" until user press quit the status is printed on the screen.
This command "cat /proc/pid/stat" is more specific to a particular process than printing the status of all the process.

Upvotes: 1

Mathuin
Mathuin

Reputation: 800

Well, top uses the /proc filesystem, so either one is fine, but top parses the stat file for you, so I'd use top. Note that top outputs a dynamic list, so in a script, you should use other means.

Upvotes: 0

Related Questions