Re: Container Uptime Discussion

"Craig Small" <[email protected]> ("csmall") Fri, 9 Feb 2024 21:51:05 +1100
Newsgroups gmane.linux.procps.devel
Message-ID <CALy8Cw6nY3y5D9ANdwK4S7ty7LwqAwMCcvnowo0XEGfh+X75MA@mail.gmail.com>
--0000000000001accd10610f0b5bc
Content-Type: multipart/alternative; boundary="0000000000001accd00610f0b5ba"

--0000000000001accd00610f0b5ba
Content-Type: text/plain; charset="UTF-8"

On Fri, 9 Feb 2024 at 20:16, Jim Warner <[email protected]> wrote:

> Hi Craig,
>
> Here is a little more icing on the container uptime cake.
>
> An lxc container might remain running for longer than the PID1
> TIME_START, which was based on the host boot time.  During this period
> TIME_ELAPSED is reported as zero since it's really negative.
>
> When a running container passes TIME_START for PID1 the lxc manipulated
> /proc/uptime is now greater so the existing path would be taken.
> However, that path now yields an incorrect 'uptime -c' value
> representing the interval since TIME_START was passed.
>
I currently take the system uptime and the PID 1 elapsed time and return
the smallest.
I think this will need working on, but it's slightly better now and doesn't
segfault with hidepid=1

Elapsed time is boot_time - proc_start_time
boot_time is uptime from /proc/uptime
proc_start_time is measured seconds since boot.

So how does LXCFS determine the new uptime?
Find the main container process, get its start time.
Get the boottime from clock_gettime(CLOCK_BOOTTIME)
uptime is start_time - boottime.

I see the issue with using just elapsed time now, it is based on the wrong
uptime!

Host
clock_gettime = 2047
/proc/uptime = 2047
PID1 elapsed = 2047
PID1 start = 0.05 (2047-0.05 ~ 2047)

LXC Container
clock_gettime = 2836
/proc/uptime = 17
PID1 elapsed = 0
PID1 start = 2819 (2836-2819=17)

Docker container
clock_gettime = 3289
/proc/uptime = 3289
PID1 elapsed = 22
PID1 start = 3267 (3289-3267 = 22)

So, do I do any fancy stuff or just make the container uptime is PID1 start
- clock_gettime?
Perhaps boot_time in the library should use clock_gettime()?

If you could run the test program attached, or something like it I'd be
keen to see if it provides the system uptime.
I'll make the changes to use the clock.

 - Craig

--0000000000001accd00610f0b5ba
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div dir=3D"ltr">On Fri, 9 Feb 2024 at 20:16, Jim Warner &=
lt;<a href=3D"mailto:[email protected]">[email protected]</a>=
&gt; wrote:</div><div class=3D"gmail_quote"><blockquote class=3D"gmail_quot=
e" style=3D"margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204)=
;padding-left:1ex">Hi Craig,<br>
<br>
Here is a little more icing on the container uptime cake.<br>
<br>
An lxc container might remain running for longer than the PID1 <br>
TIME_START, which was based on the host boot time.=C2=A0 During this period=
 <br>
TIME_ELAPSED is reported as zero since it&#39;s really negative.<br>
<br>
When a running container passes TIME_START for PID1 the lxc manipulated <br=
>
/proc/uptime is now greater so the existing path would be taken. <br>
However, that path now yields an incorrect &#39;uptime -c&#39; value <br>
representing the interval since TIME_START was passed.<br></blockquote><div=
>I currently take the system uptime and the PID 1 elapsed time and return t=
he smallest.</div><div>I think this will need working on, but it&#39;s slig=
htly better now and doesn&#39;t segfault with hidepid=3D1<br></div><div><br=
></div><div>Elapsed time is boot_time - proc_start_time</div><div>boot_time=
 is uptime from /proc/uptime</div><div>proc_start_time is measured seconds =
since boot.</div><div><br></div><div>So how does LXCFS determine the new up=
time?</div><div>Find the main container process, get its start time.</div><=
div>Get the boottime from clock_gettime(CLOCK_BOOTTIME)</div><div>uptime is=
 start_time - boottime.</div><div><br></div><div>I see the issue with using=
 just elapsed time now, it is based on the wrong uptime!<br></div><div></di=
v><div><br></div><div>Host</div><div>clock_gettime =3D 2047</div><div>/proc=
/uptime =3D 2047</div><div>PID1 elapsed =3D 2047</div><div>PID1 start =3D 0=
.05 (2047-0.05 ~ 2047)<br></div><div></div><div><br></div><div>LXC Containe=
r</div><div>clock_gettime =3D 2836</div><div>/proc/uptime =3D 17</div><div>=
PID1 elapsed =3D 0</div><div>PID1 start =3D 2819 (2836-2819=3D17)</div><div=
></div><div><br></div><div>Docker container</div><div>clock_gettime =3D 328=
9</div><div>/proc/uptime =3D 3289</div><div>PID1 elapsed =3D 22</div><div>P=
ID1 start =3D 3267 (3289-3267 =3D 22)</div><div><br></div><div>So, do I do =
any fancy stuff or just make the container uptime is PID1 start - clock_get=
time?</div></div><div class=3D"gmail_quote">Perhaps boot_time in the librar=
y should use clock_gettime()?</div><div class=3D"gmail_quote"><br></div><di=
v class=3D"gmail_quote">If you could run the test program attached, or some=
thing like it I&#39;d be keen to see if it provides the system uptime.<br><=
/div><div class=3D"gmail_quote">I&#39;ll make the changes to use the clock.=
</div><div class=3D"gmail_quote"><br></div><div class=3D"gmail_quote">=C2=
=A0- Craig</div><div class=3D"gmail_quote"><br></div></div>

--0000000000001accd00610f0b5ba--
--0000000000001accd10610f0b5bc
Content-Type: text/x-csrc; charset="US-ASCII"; name="time.c"
Content-Disposition: attachment; filename="time.c"
Content-Transfer-Encoding: base64
Content-ID: <f_lseiyi4s0>
X-Attachment-Id: f_lseiyi4s0

I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDx0aW1lLmg+CgppbnQgbWFpbihpbnQgYXJnYywg
Y2hhciAqYXJndltdKQp7CiAgICBzdHJ1Y3QgdGltZXNwZWMgdHM7CgogICAgY2xvY2tfZ2V0dGlt
ZShDTE9DS19CT09UVElNRSwgJnRzKTsKICAgIHByaW50ZigiVGltZTogJWZcbiIsIChkb3VibGUp
KHRzLnR2X3NlYyArIHRzLnR2X25zZWMgKiAxZS05KSk7CiAgICByZXR1cm4gMDsKfQo=
--0000000000001accd10610f0b5bc--