Skip to content

Commit

Permalink
Obtain true uptime through clock_gettime(CLOCK_MONOTONIC, struct *tim…
Browse files Browse the repository at this point in the history
…espec)

instead of subtracting 'bootime' from 'now'.

Sponsored by:	TCP/IP Optimization Fundraise 2005
  • Loading branch information
andreoppermann committed Oct 17, 2005
1 parent 58c0a29 commit 74d3dde
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 25 deletions.
20 changes: 4 additions & 16 deletions usr.bin/vmstat/vmstat.c
Original file line number Diff line number Diff line change
Expand Up @@ -396,26 +396,14 @@ getdrivedata(char **argv)
static long
getuptime(void)
{
static struct timeval boottime;
static time_t now;
struct timespec sp;
time_t uptime;

if (boottime.tv_sec == 0) {
if (kd != NULL) {
kread(X_BOOTTIME, &boottime, sizeof(boottime));
} else {
size_t size;

size = sizeof(boottime);
mysysctl("kern.boottime", &boottime, &size, NULL, 0);
if (size != sizeof(boottime))
errx(1, "kern.boottime size mismatch");
}
}
(void)time(&now);
uptime = now - boottime.tv_sec;
(void)clock_gettime(CLOCK_MONOTONIC, &sp);
uptime = sp.tv_sec;
if (uptime <= 0 || uptime > 60*60*24*365*10)
errx(1, "time makes no sense; namelist must be wrong");

return(uptime);
}

Expand Down
12 changes: 3 additions & 9 deletions usr.bin/w/w.c
Original file line number Diff line number Diff line change
Expand Up @@ -424,9 +424,8 @@ pr_header(time_t *nowp, int nusers)
{
double avenrun[3];
time_t uptime;
struct timespec tp;
int days, hrs, i, mins, secs;
int mib[2];
size_t size;
char buf[256];

/*
Expand All @@ -437,14 +436,9 @@ pr_header(time_t *nowp, int nusers)
(void)printf("%s ", buf);
/*
* Print how long system has been up.
* (Found by looking getting "boottime" from the kernel)
*/
mib[0] = CTL_KERN;
mib[1] = KERN_BOOTTIME;
size = sizeof(boottime);
if (sysctl(mib, 2, &boottime, &size, NULL, 0) != -1 &&
boottime.tv_sec != 0) {
uptime = now - boottime.tv_sec;
if (clock_gettime(CLOCK_MONOTONIC, &tp) != -1) {
uptime = tp.tv_sec;
if (uptime > 60)
uptime += 30;
days = uptime / 86400;
Expand Down

0 comments on commit 74d3dde

Please sign in to comment.