My computer says:$ uptime 10:20:35 up  1:46,  3 users,  load average: 0,03, 0,10, 0,13And if I check last I see:reboot   system boot  3.19.0-51-generi Tue Apr 12 08:34 - 10:20  (01:45)    And then I check:$ ls -l /var/log/boot.log -rw-r--r-- 1 root root 4734 Apr 12 08:34 boot.logThen I see in /var/log/syslog the first line of today being:Apr 12 08:34:39 PC... rsyslogd: [origin software=rsyslogd swVersion=7.4.4 x-pid=820 x-info=http://www.rsyslog.com] startSo all seems to converge in 8:34 being the time when my machine has booted.However, I wonder: what is the exact time uptime uses? Is uptime a process that launches and checks some file or is it something on the hardware?I'm running Ubuntu 14.04.
On Linux, when does uptime start counting from?
linux;linux kernel;uptime
On my system it gets the uptime from /proc/uptime:$ strace -eopen uptimeopen(/etc/ld.so.cache, O_RDONLY|O_CLOEXEC) = 3open(/lib/libproc-3.2.8.so, O_RDONLY|O_CLOEXEC) = 3open(/lib/x86_64-linux-gnu/libc.so.6, O_RDONLY|O_CLOEXEC) = 3open(/proc/version, O_RDONLY)         = 3open(/sys/devices/system/cpu/online, O_RDONLY|O_CLOEXEC) = 3open(/etc/localtime, O_RDONLY|O_CLOEXEC) = 3open(/proc/uptime, O_RDONLY)          = 3open(/var/run/utmp, O_RDONLY|O_CLOEXEC) = 4open(/proc/loadavg, O_RDONLY)         = 4 10:52:38 up 3 days, 23:38,  4 users,  load average: 0.00, 0.02, 0.05From the proc manpage:   /proc/uptime          This file contains two numbers: the uptime of the system          (seconds), and the amount of time spent in idle process          (seconds).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 read() and 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.