Page MenuHomeFreeBSD

w: Fix premature rounding.
ClosedPublic

Authored by des on Oct 25 2022, 10:42 PM.
Tags
None
Referenced Files
Unknown Object (File)
Dec 23 2023, 11:01 AM
Unknown Object (File)
Sep 10 2023, 9:58 AM
Unknown Object (File)
Jul 6 2023, 11:20 PM
Unknown Object (File)
May 12 2023, 4:32 PM
Unknown Object (File)
May 12 2023, 4:19 PM
Unknown Object (File)
May 12 2023, 3:58 PM
Unknown Object (File)
Mar 5 2023, 6:00 PM
Unknown Object (File)
Feb 17 2023, 4:37 PM

Details

Summary

If the system has been up more longer than a minute, we add 30 seconds to
the uptime so that subsequent calculations will round to the nearest minute
rather than truncate. However, since the introduction of libxo, we output
the raw value after performing the adjustment. Rewrite so that we output
the raw value first, then perform the adjustment and recalculate before
outputting the humanized value.

While there, reduce stack usage and avoid needless allocations.

Sponsored by: Klara, Inc.

Diff Detail

Repository
rG FreeBSD src repository
Lint
Lint Not Applicable
Unit
Tests Not Applicable

Event Timeline

des requested review of this revision.Oct 25 2022, 10:42 PM

Thank you! I remember trying to fix this issue a while ago.

I'll dump here the tests I was planning to add:

Test Plan

Test 1

From the bug report #30680: A machine with an uptime of 172785 seconds
(1 day, 23 hours, 59 minutes, and 45 seconds).

% uptime
10:09AM  up 2 days, 1 user, load averages: 0.01, 0.01, 0.01

% uptime --libxo json,pretty
{
  "uptime-information": {
    "time-of-day": " 10:09AM",
    "uptime": 172785,
    "days": 1,
    "hours": 23,
    "minutes": 59,
    "seconds": 45,
    "uptime-human": " 2 days,",
    "users": 1,
    "load-average-1": 0.01,
    "load-average-5": 0.01,
    "load-average-15": 0.01
  }
}

Test 2

From the bug report #30680: A machine with an uptime of 1 second.

% uptime
10:09AM  up 1 sec, 1 user, load averages: 0.01, 0.01, 0.01

% uptime --libxo json,pretty
{
  "uptime-information": {
    "time-of-day": " 10:09AM",
    "uptime": 1,
    "days": 0,
    "hours": 0,
    "minutes": 0,
    "seconds": 1,
    "uptime-human": " 1 sec,",
    "users": 1,
    "load-average-1": 0.01,
    "load-average-5": 0.01,
    "load-average-15": 0.01
  }
}

Test 3

Given a machine with an uptime of 90090 seconds
(1 day, 1 hour, 1 minute, and 30 seconds).

Notice how it rounds up the minutes:

% uptime
10:09AM  up 1 day,  1:02, 1 user, load averages: 0.01, 0.01, 0.01

When displaying as JSON using libxo, we do not add 30 extra seconds to
seconds, we do however round up uptime-human:

% uptime --libxo json,pretty
{
  "uptime-information": {
    "time-of-day": "10:09AM",
    "uptime": 90090,
    "days": 1,
    "hours": 1,
    "minutes": 1,
    "seconds": 30,
    "uptime-human": " 1 day,  1:02,",
    "users": 1,
    "load-average-1": 0.01,
    "load-average-5": 0.01,
    "load-average-15": 0.01
  }
}

JSON data from uptime (seconds) can be directly consumed, and will
now match the uptime displayed in other programs, e.g. NMS:

% uptime --libxo json,pretty | jq .[].seconds
30

% bsnmpget -o quiet snmpEngineTime
90090
usr.bin/w/w.c
515

I wonder if it should be like this?

This revision is now accepted and ready to land.Oct 26 2022, 2:43 AM
kevans added inline comments.
usr.bin/w/w.c
515

at sec == 60 you're just repeating the arithmetic we did above. since it's already a round minute.

This revision was automatically updated to reflect the committed changes.