Container process uptime in /proc is clearly bogus

klausman

New Member
Dec 30, 2024
2
2
3
Running pxmx 8.3.1.

I have several LXC containers and in one of them, I would like to programmatically determine the start time of a particular process. My approach has been:

- containeruptime = /proc/uptime field 0
- containerstarttime = now() - uptime
- procstartdelta = (/proc/MYPID/stat field 22 - /proc/1/stat field 22
- return containerstarttime+procstartdelta

Except that gives me completely weird values. Narrowing it down, I have found that /proc/PID/stat contains completely bogus process uptime values:

Code:
$ awk '{print $22}' /proc/{1,119279}/stat
172979854
180038658
$

This would mean that there is a process that has a longer uptime than init in that container, which is clearly not possible. What I now wonder is if I set up my LXC containers wrong or something. I don't see any obvious options regarding /proc, and have used defaults otherwise.

Any suggestions appreciated, except:

- reading the procps code. It's convoluted C code that is just very hard to read for me
- exec'ing ps and parsing its output.
 
Hi,
see man 5 procfs
Code:
              (22) starttime  %llu
                     The time the process started after system boot.  Before Linux 2.6, this value was expressed in jiffies.  Since
                     Linux 2.6, the value is expressed in clock ticks (divide by sysconf(_SC_CLK_TCK)).

                     The format for this field was %lu before Linux 2.6.
That's the Unix timestamp when the process was started. Not how long the process is running.
 
Last edited:
Hi,
see man 5 procfs
Code:
              (22) starttime  %llu
                     The time the process started after system boot.  Before Linux 2.6, this value was expressed in jiffies.  Since
                     Linux 2.6, the value is expressed in clock ticks (divide by sysconf(_SC_CLK_TCK)).

                     The format for this field was %lu before Linux 2.6.
That's the Unix timestamp when the process was started. Not how long the process is running.
That's also wrong. As the manpage says it's in clockticks (classically 100/s, but these days configurable) and not since 1970-01-01, but since system boot (can be calculated from the current time of day and /proc/uptime). Unfortunately, /proc/uptime is the uptime of the system, not the container, so it doesn't help me here.

In the end, I resolved it by calculating the container start time by using /proc/1/stat, and then the process start time from that.