使用Android的CLOCK_MONOTONIC(Linux)的正常运行时间正常运行、时间、Android、CLOCK_MONOTONIC

2023-09-06 05:08:01 作者:别摔倒地会疼的!

根据 Android开发者参考 uptimeMillis()返回开机以来的毫秒数,。我检查的实施  uptimeMillis()在我的code,它大致是这样的 -

 结构的timespec吨;t.tv_sec = t.tv_nsec = 0;clock_gettime(CLOCK_MONOTONIC,& T公司);回报(的int64_t)(t.tv_sec)* 1000000000LL + t.tv_nsec; 

据我知道,从一些未指定点线性包括睡眠时间 CLOCK_MONOTONIC 计数。

下面是我的疑惑 -

Android7大挑战者

如果 CLOCK_MONOTONIC 包括睡眠时间,怎么来的 uptimeMillis()并没有考虑到这一点?如果我的理解是错误的, CLOCK_MONOTONIC 没有考虑到睡眠帐户,那么我应该怎么用得到系统正常运行时间,包括睡眠?

什么是深度睡眠?在CPU闲置称为深度睡眠?

什么是Linux中的未指定点的价值?能否请你在code,其中该时钟开始指出的?

解决方案

在系统暂停CLOCK_MONOTONIC停止。有些人认为这是一个错误,并随后出现了补丁添加CLOCK_BOOTTIME时钟: https://开头LWN。净/文章/ 428176 / 。 我不知道这些补丁尚未被列入主线内核 CLOCK_BOOTTIME是NDK-9C - 只用了2,5岁;) - 沃伊切赫

挂起,我猜。

IIRC启动前一段固定时间。如果你深入到内核源代码,你会发现确切的价值。话又说回来,对整个点是它的未指定是,它可以在任何时候改变,因此依靠似乎是不明智的我。

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-

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?

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

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

解决方案

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

Suspend, I guess.

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.