Page MenuHomeFreeBSD

w: Fix premature rounding.
ClosedPublic

Authored by des on Oct 25 2022, 10:42 PM.
Tags
None
Referenced Files
Unknown Object (File)
Mon, Oct 28, 12:00 PM
Unknown Object (File)
Mon, Oct 28, 12:00 PM
Unknown Object (File)
Mon, Oct 28, 12:00 PM
Unknown Object (File)
Mon, Oct 28, 11:43 AM
Unknown Object (File)
Oct 5 2024, 1:54 PM
Unknown Object (File)
Oct 4 2024, 10:00 AM
Unknown Object (File)
Oct 4 2024, 7:23 AM
Unknown Object (File)
Oct 1 2024, 7:19 PM
Subscribers

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 Passed
Unit
No Test Coverage
Build Status
Buildable 47998
Build 44885: arc lint + arc unit

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.