Most of the time you can trust the times you see in programs such as ps and top within Linux, but there are those other times where you just want to check what is going on.
First up, if you’re using a FreeBSD system and the procps Linux tools, you’re out of luck because a lot of the procfs is broken. You’re almost guaranteed to get wierd results.
Jiffies and Hertz
The first thing to find is your jiffies. This is the number of clock ticks per second. There are two main ways of doing it.
The first, and more difficult way is to add up the cpu numbers in /proc/stat, divide it by the first number in /proc/uptime and divide it by the number of cpus.
My cpu line in /proc/stat looks like:
cpu 1267430 145826 552700 137029699 905086 8295 17885 0 0
and my /proc/uptime is:
I’ve got 2 cpus on this system so the calculation is:
(1267430 + 145826 + 552700 + 137029699 + 905086 + 8295 + 17885 + 0 + 0 ) / 695317.99 / 2
which equals 100.
The second way is to use getconf, with the command “getconf CLK_TCK” which again on this system gives you 100.
We call this value “Hertz” and is usually 100 or 1024 though for some architectures it is other values.
For each process the procfs has the start time, but it is expressed as the number of jiffes since the computer was booted. If we want to know the real “wall clock” time a process is started we have to start with number of seconds since epoch (for example using the time() function) subtract the number of seconds since the computer was booted and add the process start time from procfs.
The seconds since boot is the first value in /proc/uptime:
The 22nd item in a procfs PID stat file is the number of jiffes since boot when the process started. We have to divide it by Hertz to calculate the number of seconds.
27139 (bash) S 11942 27139 27139 34821 5200 4202496 5500 25537 0 14 40 16 18 15 20 0 1 0 69446639 23433216 1378 18446744073709551615 4194304 5082140 14073724008 6512 140737240085456 140127945249678 0 65536 3686404 1266761467 0 0 0 17 1 0 0 0 0 0
To get the start time, take the current time, subtract the boot time and add the process start time.
perl -e ‘$nowstr = localtime(time() – 696141.85 + 69446639 / 100); print $nowstr, “n”;
Mon Jul 12 22:20:07 2010
Unless you are very quick, you will have to take a few seconds off because the time() function will be now, while the values from the proc files will be a few seconds earlier as it takes time for you to cut and paste.