Obtain true uptime through clock_gettime(CLOCK_MONOTONIC, struct *timespec)

instead of subtracting 'bootime' from 'now'.

Sponsored by:	TCP/IP Optimization Fundraise 2005
This commit is contained in:
Andre Oppermann 2005-10-17 15:37:22 +00:00
parent 4773bde95e
commit a21cbcb876
2 changed files with 7 additions and 25 deletions

View File

@ -396,26 +396,14 @@ getdrivedata(char **argv)
static long static long
getuptime(void) getuptime(void)
{ {
static struct timeval boottime; struct timespec sp;
static time_t now;
time_t uptime; time_t uptime;
if (boottime.tv_sec == 0) { (void)clock_gettime(CLOCK_MONOTONIC, &sp);
if (kd != NULL) { uptime = sp.tv_sec;
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;
if (uptime <= 0 || uptime > 60*60*24*365*10) if (uptime <= 0 || uptime > 60*60*24*365*10)
errx(1, "time makes no sense; namelist must be wrong"); errx(1, "time makes no sense; namelist must be wrong");
return(uptime); return(uptime);
} }

View File

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