7

According to Android Developer Reference uptimeMillis() returns the number of milliseconds since boot, not counting time spent in deep sleep. I checked the implementation of uptimeMillis() in my code and it is roughly like this-

struct timespec t;
t.tv_sec = t.tv_nsec = 0;
clock_gettime(CLOCK_MONOTONIC, &t);
return (int64_t)(t.tv_sec)*1000000000LL + t.tv_nsec;

As far as I know CLOCK_MONOTONIC counts from some unspecified point linearly including sleep time.

Here are my doubts-

  1. If CLOCK_MONOTONIC includes sleep time, how come uptimeMillis() doesn't take it into account? If my understanding is wrong and CLOCK_MONOTONIC doesn't take sleep into account, then what should I use to get system uptime including sleep?

  2. What is deep sleep? Is the CPU idling referred as deep sleep?

  3. What is the value of unspecified point in Linux? Can you kindly point out in code where this clock is started?

Pavan Manjunath
  • 27,404
  • 12
  • 99
  • 125

1 Answers1

10
  1. CLOCK_MONOTONIC stops when the system is suspended. Some people felt this was a mistake, and subsequently there have been patches for adding a CLOCK_BOOTTIME clock: https://lwn.net/Articles/428176/ . I don't know if these patches have yet been included in the mainline kernel. CLOCK_BOOTTIME is in ndk-9c - it only took 2,5 years ;) – Wojciech

  2. Suspend, I guess.

  3. IIRC some fixed time before boot. You'll find the exact value if you dig into the kernel source. Then again, the entire point about it being unspecified is that it could change at any point, so relying on it seems unwise to me.

Prof. Falken
  • 24,226
  • 19
  • 100
  • 173
janneb
  • 36,249
  • 2
  • 81
  • 97
  • Thanks a lot janneb!! It was quite helpful. But can you elaborate on suspend? By suspend, do you mean the process is suspended or the entire CPU is idling? – Pavan Manjunath Jun 15 '11 at 16:13
  • @Pavan: Suspend, as in the entire system is suspended. – janneb Jun 15 '11 at 16:34
  • Thanks again! I checked in 2.6.35. `CLOCK_BOOTTIME` isn't there yet! But still `SystemClock.elapsedRealtime()` is able to do it! Any idea of how's it doing? – Pavan Manjunath Jun 15 '11 at 17:29
  • @Pavan: The "Realtime" in the method name suggests it's a realtime clock, don't you think? Wrt the CLOCK_BOOTTIME, the patch I linked to was posted in February 2011, whereas 2.6.35 was released in August 2010, so it's not surprising you won't find it there.. – janneb Jun 15 '11 at 19:41
  • 2
    CLOCK_BOOTTIME is in ndk-9c - it only took 2,5 years ;) – Wojciech Jan 28 '14 at 13:04