I would like to know which commands top uses to get the CPU information on a UNIX system. For example, I would like to find CPU usage on an iphone (MAC OS X) either in real-time or say every 2 seconds, but not displayed through top rather through a single command which will output only the CPU usage value to a file or on the terminal... This value will later be used in a program that displays CPU information.
Also, where is the source code for TOP, if this is available? Thanks.
Source code is available in the tar files distributed here on sourceforge and at www.unixtop.org.
Top does not use a command to determine %cpu utilization. Top collects its information directly from the kernel using whatever mechanism is most appropriate for the operating system on which it is run. In the case of linux it gets its information by looking at files in /proc. For solaris the process is similar although the actual files and their content differ. In the case of freebsd it uses the kvm library to open /dev/kmem and retrieve the process table. Top retrieves information about each process -- including current cpu usage -- at regular intervals, then calculates its percentages based on that information. It does not rely on pre-calculated percentages that some operating system kernels track. Instead it takes the difference between the current sample and the previous one to determine the actual cpu utilization during the sample period and calculates a percentage of total available cpu power for that period.
If you want to access this information from another program you can run top in "batch" mode with "top -b". That will display the information as plain text suitable for postprocessing with grep or any other text based tool.
Log in to post a comment.