From pundu on Wed, 10 May 2000
I would like to know how one can calculate cpu load and memory
used by processes as shown by 'top' command. It would be nice if any one can explain me how you could do these by writing your own programs , or by any other means.
Why don't you download the sources to 'top' and 'uptime' and read them? On a reasonably modern Debian system you could just issue the command 'apt-get source procps' to have your system find, fetch, unpack and patch those. ('top', 'uptime', 'kill' and a number of other process management commands are in the "procps" package --- since these are all tools that implement process management and reporting using the /proc kernel support.
(Technically there were/are other ways to do these sorts of process management things, in cases where you don't have /proc enabled --- but they are no widely used anymore. There is a /proc alternative that's implemented as a device driver --- for embedded systems, and there's some old techniques for doing it by reading some of the kernel's data structures though /dev/kmem --- basically by using root level read access to wander around the kernels memory extracting and parsing bits of it from all over).
Your distribution probably came with sources (maybe on an extra CD) or you could always wander around Metalab (formerly known as Sunsite) http://metalab.unc.edu/pub/Linux to find lots of source code for lots of Linux stuff. You might also look at Freshmeat (http://www.freshmeat.net), Appwatch (http://www.appwatch.com) and even ExecPC's LSM (Linux Software Map) at http://www.execpc.com/lsm (You can even get 'appindex' a little curses package which can help you find apps from Freshmeat and the LSM by downloading RSS files from each of them on demand).
[ As of publication time, there's another one, called IceWALKERS (www.icewalk.com) -- Heather ]
Another good site to find the sources to your free software is the "Official GNU Web site" (http://www.gnu.org) and at the old GNU master archive site: ftp://prep.ai.mit.edu/gnu
Of course you could always compare these sources to those from another free implemention of UNIX. Look at the FreeBSD web site (http://www.freebsd.org) and its ilk (OpenBSD http://www.openbsd.org and NetBSD http://www.netbsd.org).
Of course I realize that you might not have realized that the source code was available. That's one of the features of Linux that you may have heard touted in the press. That "open source" thing means you can look at the sources to any of the core systems and packages (from the kernel, and libraries, through the compilers and the rest of the tool chain, and down into most of the utilities and applications).
I also realize that many people have no idea how to find these sources. Obviously the first step is to find out what package the program you what to look at came from. Under any of the RPM based systems (S.u.S.E. Red Hat, TurboLinux, Caldera OpenLinux, etc) you can use a command like 'rpm -qf /usr/bin/top' to find out that 'top' is part of the procps package. Under Debian you could install the dlocate package, or use a command like 'grep /usr/bin/top /var/lib/dpkg/info/*.list' or one like 'dpkg -S bin/top' (note I don't need a full path in that case). All of these will give you a package name (procps in this case). Then you can use the techniques and web sites I've mentioned above to find the package sources.
Incidentally the canonical (master) URL for procps seems to be:
... according to the Appindex and LSM entries I read).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18