From: cheloha Date: Mon, 8 Aug 2022 16:54:09 +0000 (+0000) Subject: top(1): display uptime with seconds, print uptime in fixed format X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=2d9cf35622ef3a511287cc24bc7f7926f57d50fc;p=openbsd top(1): display uptime with seconds, print uptime in fixed format 1. It's sometimes useful to know the system uptime with more precision than one minute. So, this patch changes top(1) to print seconds of uptime in addition to minutes, hours, and days. 2. It's *always* annoying when the information you want on a realtime display is not shown in the same place in a consistent format. So, this patch also changes top(1) to always print the uptime like this: up D days HH:MM:SS This is much easier to read at a glance. In particular, it requires no additional thought on my part to figure out whether the machine has been up less than one day. Maybe of note is that these changes make top(1)'s output different from that of uptime(1). I don't think this matters very much. top(1) is a realtime display, so it isn't likely to be parsed. uptime(1) is a different story. Link: https://marc.info/?l=openbsd-tech&m=160046282400892&w=2 Positive feedback from kn@. ok gnezdo@ bluhm@ millert@ --- diff --git a/usr.bin/top/display.c b/usr.bin/top/display.c index abb8a4ba57e..3cc91fc2105 100644 --- a/usr.bin/top/display.c +++ b/usr.bin/top/display.c @@ -1,4 +1,4 @@ -/* $OpenBSD: display.c,v 1.65 2020/08/26 16:21:28 kn Exp $ */ +/* $OpenBSD: display.c,v 1.66 2022/08/08 16:54:09 cheloha Exp $ */ /* * Top users/processes display for Unix @@ -208,31 +208,28 @@ display_init(struct statics * statics) return (display_lines); } +/* + * Print the time elapsed since the system booted. + */ static void format_uptime(char *buf, size_t buflen) { - time_t uptime; - int days, hrs, mins; struct timespec boottime; - - /* - * Print how long system has been up. - */ - if (clock_gettime(CLOCK_BOOTTIME, &boottime) != -1) { - uptime = boottime.tv_sec; - uptime += 30; - days = uptime / (3600 * 24); - uptime %= (3600 * 24); - hrs = uptime / 3600; - uptime %= 3600; - mins = uptime / 60; - if (days > 0) - snprintf(buf, buflen, "up %d day%s, %2d:%02d", - days, days > 1 ? "s" : "", hrs, mins); - else - snprintf(buf, buflen, "up %2d:%02d", - hrs, mins); - } + time_t uptime; + unsigned int days, hrs, mins, secs; + + if (clock_gettime(CLOCK_BOOTTIME, &boottime) == -1) + err(1, "clock_gettime"); + + uptime = boottime.tv_sec; + days = uptime / (3600 * 24); + uptime %= (3600 * 24); + hrs = uptime / 3600; + uptime %= 3600; + mins = uptime / 60; + secs = uptime % 60; + snprintf(buf, buflen, "up %u days %02u:%02u:%02u", + days, hrs, mins, secs); }