Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

How do the commands top and ps calculate CPU utilization using the /proc/[$pid]/stat file? Also how do they obtain memory utilization information about the process?

share|improve this question
    
If you have two unrelated questions, post two questions. Don't combine them just because you had them at the same time. I've removed your second question. If you want to ask it, open a new question. –  cjm Apr 20 '11 at 18:01
5  
I'd recommend just reading the source for the procps package. It's pretty reasonable. I think the answer is in the file proc/sysinfo.c –  penguin359 Apr 20 '11 at 18:27
    
or more simply do a strace on the ps and top commands and find whereabouts of the files they look at, besides it is a implementation of procfs on Linux. The many parameters like VSZ or RSS are not properly calculated. I would suggest to look at pmap -[dx] command for more process specific memory usage. –  Nikhil Mulley Dec 4 '11 at 12:32
add comment

2 Answers 2

Here's how I would do "just reading the source for the procps package"

What package(s) is/are ps and top in?

dpkg -S top| grep '/top$'
dpkg -S ps|grep '/ps$'

I have apt-src installed, do you?

dpkg -S apt-src

Look at the versions of procps and apt-src

dpkg -l procps apt-src

Get the source for the procps package

apt-src install procps

See what we got:

ls -ld procps*

cd into procps-3.2.8 (or whatever version you downloaded) and look at the Makefile, and all files named with UPPER CASE, like README*, INSTALL, etc, and then read *.h and *.c, and consider executing make.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.