8

The question is simple: do time periods when computer is in any of the states below count towards total uptime of a system ?

  • hibernation
  • suspending the system
  • changing runlevel

NOTE:

  • I am not interested in the uptime command itself. Quick look at source code link that Terrance provided reveals nothing about hibernation or suspending
  • I am interested in how does kernel compute the time system has been running, how does suspending/hibernating affect that value.
  • If possible, please provide a reference to documentation
Sergiy Kolodyazhnyy
  • 105,154
  • 20
  • 279
  • 497
  • 3
    Please specify what exactly you mean by "uptime". I suppose you're talking about the output of the command uptime. Is this correct? – UTF-8 Dec 24 '16 at 15:23
  • @UTF-8 Essentially , uptime is a measure of how long a system has been running. The uptime command merely parses what's in /proc/uptime file. I am not interested in the command itself, I am interested in how suspending or hibernating a system affects what uptime command and /proc/uptime file say. – Sergiy Kolodyazhnyy Dec 24 '16 at 15:29
  • I guess you could look at the source code for uptime. – Terrance Dec 24 '16 at 15:32
  • 2
    Why not just try it? My system had exactly 5 hours of uptime after I wrote my first comment. I then suspended it for 5 minutes, woke it up (conveniently after you wrote your comment), executed the command again, and it read 5:05 hours of uptime. – UTF-8 Dec 24 '16 at 15:32
  • @UTF-8 "Just try it" will show me results only for my system. I'm kind of looking for general reference on how it should work. If I write a script that deals witih uptime and it counts properly on my system, but not on another, then I've no way of knowing is it the user's problem or my problem ? I kind of want to know what the proper expected behavior is and if possible , know what official documentation says on it – Sergiy Kolodyazhnyy Dec 24 '16 at 15:37
  • @Terrance I'm not interested in the command itself, although thanks for the source code. Quick look suggests that sysinfo structure is what supplies uptime value, but again , this doesn't tell me anything about suspending or hybernating. I am really curious how uptime is calculated - is it point to point between boot and shutdown , or does suspending and hibernation gets subtracted. What also bothers me is runlevel change, because technically system is still running when runlevel changes, so does it add up or not ? – Sergiy Kolodyazhnyy Dec 24 '16 at 15:45
  • @UTF-8 by the way, what you saw after suspending your laptop is current time at the moment you execute it. What I am interested in is the second value, right next to it. See the man page for uptime. But again, I repeat, I don't care about uptime command itself. I want to know about the value that system calculates – Sergiy Kolodyazhnyy Dec 24 '16 at 15:48
  • I think you have to be interseted in the uptime command itself, as it is the result of that command that you are using. whatever calculates the value in /proc/uptime is clearly performing a calculation so you have no option but to be interested in it. UTF-8 checked what suspend does, and the time the PC is suspended is included in the uptime calculation. Hibernate the system and check if that counts. I don't know the answer to this but I suspect it is the difference between power on time and shutdown time (with hibernate and suspend being classed as 'not shut down'). It's just a guess. – hatterman Dec 24 '16 at 15:58
  • @hatterman I already looked at the source code. It grabs value from sysinfo structure and prints it out. There's no indicaton on calculations being done. No indication on what I am asking about , which is hibernation and syspending. – Sergiy Kolodyazhnyy Dec 24 '16 at 16:05
  • Hibernate and suspend your system, see what your uptime reports. I think that will give you your answer. – hatterman Dec 24 '16 at 16:06
  • @Serg I didn't read the current time. Right now, the output begins with "17:05:13 up 5:40" because 40 minutes (5:40 - 5:00) passed since I wrote my first comment. I suggest you add to you question that you're interested in the general rules uptime obeys, not how it behaves on a single system. – UTF-8 Dec 24 '16 at 16:07
  • @UTF-8 I've already specified that I'm not interested in the command itself, but in the information that it retrieves from the system. Please read my edited question – Sergiy Kolodyazhnyy Dec 24 '16 at 16:08
  • In addition, I can try suspending or changing runlevel, but I don't have hibernation enabled on my system. Plus , I don't want "well it works on my system" type of answers, I want to know how it is supposed to work and if there's any documentation that supports these rules – Sergiy Kolodyazhnyy Dec 24 '16 at 16:14
  • Well, I just tested mine with hibernation and suspend, and both still counted toward uptime. I think once it is a full restart, it changes the /proc/uptime file with a new timestamp. The /var/log/hibernate.log did have suspend and resume times listed in it. I guess those could be subtracted from overall if you don't want hibernate added in. That is what I am getting out of it. – Terrance Dec 24 '16 at 18:55
  • @Terrance Similar to your idea I based my answer on journalctl -b-0. – WinEunuuchs2Unix Nov 07 '19 at 02:43

2 Answers2

5

The uptime command gets its data from /proc/uptime, which is exposed entirely by the kernel. So, we'll check out the kernel documentation to see what this actually represents.

In Documentation/filesystems/proc.txt, we see:

uptime      Wall clock since boot, combined idle time of all cpus

(there are two values in this file, hence the two descriptions)

The reference to "wall clock" is important here - it means all elapsed time, regardless of whether or not the machine's clocks are running. So, this time will keep increasing in suspended or hibernated state.

Putting it another way, the uptime value is effectively the time elapsed since the last boot.

Jeremy Kerr
  • 27,199
3

The uptime is grossly misleading. If you use uptime -s it is more meaningful:

$ uptime -s
2019-10-31 05:30:09

Now you can see it simply shows you the last boot time. If you want to know the real uptime see this answer. Sample output:

$ suspendtime
Oct 31 05:55:19 to Oct 31 16:54:26 lasting 39,547 seconds
Oct 31 23:21:21 to Nov 01 04:29:12 lasting 18,471 seconds
Nov 01 05:51:27 to Nov 01 17:08:34 lasting 40,627 seconds
Nov 02 00:01:33 to Nov 02 10:28:46 lasting 37,633 seconds
Nov 02 18:15:59 to Nov 02 19:10:14 lasting 3,255 seconds
Nov 02 21:17:33 to Nov 03 05:31:54 lasting 33,261 seconds
Nov 03 12:06:39 to Nov 03 14:22:50 lasting 8,171 seconds
Nov 03 22:28:12 to Nov 04 04:17:13 lasting 20,941 seconds
Nov 04 05:49:40 to Nov 04 16:48:52 lasting 39,552 seconds
Nov 04 21:45:48 to Nov 05 04:19:26 lasting 23,618 seconds
Nov 05 05:52:05 to Nov 05 16:32:38 lasting 38,433 seconds
Nov 05 21:12:18 to Nov 06 04:16:50 lasting 25,472 seconds
Nov 06 05:50:45 to Nov 06 16:22:54 lasting 37,929 seconds

Linux uptime 572,689 seconds (6 Days 15 Hours 4 Minutes 49 Seconds)
13 Suspends 366,910 seconds (4 Days 5 Hours 55 Minutes 10 Seconds)
Real uptime 205,779 seconds (2 Days 9 Hours 9 Minutes 39 Seconds)
  • Very cool! Just got some upvotes from me. :) I really like the concept and the conversions you have done. Unfortunately for me, I stopped using Suspend and Hibernate on this system a while ago so I can't run your script. :( But I do have a copy of it now. :D – Terrance Nov 07 '19 at 04:10