On my system it gets the uptime from
$ strace -eopen uptime
open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
open("/lib/libproc-3.2.8.so", O_RDONLY|O_CLOEXEC) = 3
open("/lib/x86_64-linux-gnu/libc.so.6", O_RDONLY|O_CLOEXEC) = 3
open("/proc/version", O_RDONLY) = 3
open("/sys/devices/system/cpu/online", O_RDONLY|O_CLOEXEC) = 3
open("/etc/localtime", O_RDONLY|O_CLOEXEC) = 3
open("/proc/uptime", O_RDONLY) = 3
open("/var/run/utmp", O_RDONLY|O_CLOEXEC) = 4
open("/proc/loadavg", O_RDONLY) = 4
10:52:38 up 3 days, 23:38, 4 users, load average: 0.00, 0.02, 0.05
From the proc manpage:
This file contains two numbers: the uptime of the system
(seconds), and the amount of time spent in idle process
The proc filesystem contains a set of pseudo files. Those are not real files, they just look like files, but they contain values that are provided directly by the kernel. Every time you read a file, such as
/proc/uptime, its contents are regenerated on the fly. The proc filesystem is an interface to the kernel.
In the linux kernel source code of the file
fs/proc/uptime.c at line 49, you see a function call:
proc_create("uptime", 0, NULL, &uptime_proc_fops);
This creates a proc filesystem entry called
uptime (the procfs is usually mounted under
/proc), and associates a function to it, which defines valid file operations on that pseudo file and the functions associated to them. In case of uptime it's just
open() operations. However, if you trace the functions back you will end up here, where the uptime is calculated.
Internally, there is a timer-interupt which updates periodically the systems uptime (besides other values). The interval, in which the timer-interupt ticks, is defined by the preprocessor-macro
HZ, whose exact value is defined in the kernel config file and applied at compilation time.
The idle time and the number of CPU cycles, combined with the frequency
HZ (cycles per second) can be calculated in a number (of seconds) since the last boot.
To address your question: When does “uptime” start counting from?
Since the uptime is a kernel internal value, which ticks up every cycle, it starts counting when the kernel has initialized. That is, when the first cycle has ended. Even before anything is mounted, directly after the bootloader gives control to the kernel image.