# where uptime utility gets its info



## m4rtin (Nov 25, 2009)

If I run uptime utility in FreeBSD I can see the uptime of my system. However, where is this information collected from? Are there different places where FreeBSD writes it's information when it boots up? :stud


----------



## vivek (Nov 25, 2009)

w gets it data from /proc  and w is also uptime, see source code for yourself /usr/src/usr.bin/w/w.c


----------



## SirDice (Nov 25, 2009)

The beauty of FreeBSD is that you can have a look at the source for all binaries that make up the base OS.

The answer is in the source of w(1) (uptime is symlinked to w):
http://www.freebsd.org/cgi/cvsweb.cgi/src/usr.bin/w/w.c?rev=1.62


See clock_gettime(8)


----------



## SirDice (Nov 25, 2009)

vivek said:
			
		

> w gets it data from /proc



It doesn't, /proc isn't loaded by default so all the base OS tools don't use it.


----------



## m4rtin (Nov 25, 2009)

ok, I see. As I understand, _clock_gettime_ is the system call, which asks for how long system has been up:


```
/*
	 * Print how long system has been up.
	 */
	if (clock_gettime(CLOCK_MONOTONIC, &tp) != -1) {
		uptime = tp.tv_sec;
		if (uptime > 60)
			uptime += 30;
		days = uptime / 86400;
		uptime %= 86400;
		hrs = uptime / 3600;
		uptime %= 3600;
		mins = uptime / 60;
		secs = uptime % 60;
		(void)printf(" up");
		if (days > 0)
			(void)printf(" %d day%s,", days, days > 1 ? "s" : "");
		if (hrs > 0 && mins > 0)
			(void)printf(" %2d:%02d,", hrs, mins);
		else if (hrs > 0)
			(void)printf(" %d hr%s,", hrs, hrs > 1 ? "s" : "");
		else if (mins > 0)
			(void)printf(" %d min%s,", mins, mins > 1 ? "s" : "");
		else
			(void)printf(" %d sec%s,", secs, secs > 1 ? "s" : "");
	}
```

It looks like there isn't a particular file where uptime is written in. In the CLOCK_GETTIME(2) there is said:



> CLOCK_UPTIME which starts at zero when the kernel boots
> and increments monotonically in SI seconds while the machine is running



So basically there is some sort of uptime_daemon working in the background, which counts seconds and starts from zero if the device is rebooted?


----------



## SirDice (Nov 25, 2009)

You will have to view the source for libc, in particular what the clock_gettime call does.


----------



## expl (Nov 25, 2009)

You get uptime the same way you get normal real time/date, by calling SYS_CLOCK_GETTIME syscall just with flag CLOCK_UPTIME (defined in time.h). There is no 'daemon' running and calculating uptime, kernel just knows time it booted and gets uptime from its clock.


----------

