正常运行时间iOS目标C - 毫秒级精度 [英] Uptime iOS Objective C - millisecond precision
问题描述
我正在尝试获得iOS的正常运行时间。我正在使用mach_absolute_time - 但我发现它在睡眠时暂停了。
I'm trying to get uptime for iOS. I was using mach_absolute_time - but I found that it paused during sleep.
我找到了这个片段:
- (time_t)uptime
{
struct timeval boottime;
int mib[2] = {CTL_KERN, KERN_BOOTTIME};
size_t size = sizeof(boottime);
time_t now;
time_t uptime = -1;
(void)time(&now);
if (sysctl(mib, 2, &boottime, &size, NULL, 0) != -1 && boottime.tv_sec != 0)
{
uptime = now - boottime.tv_sec;
}
return uptime;
}
它可以解决问题。但是,它整整回来了。有没有办法从这里获得毫秒?
It does the trick. BUT, it's returning whole seconds. Any way to get milliseconds out of this?
谢谢!
推荐答案
内核(显然)不存储其启动时间的更高分辨率时间戳。
The kernel does not (apparently) store a higher-resolution timestamp of its boot time.
KERN_BOOTTIME
已实施通过 sysctl_boottime
函数在 bsd / kern / kern_sysctl.c
。它调用 boottime_sec
。
KERN_BOOTTIME
is implemented by the sysctl_boottime
function in bsd/kern/kern_sysctl.c
. It calls boottime_sec
.
boottime_sec
在< a href =http://www.opensource.apple.com/source/xnu/xnu-1699.32.7/bsd/kern/kern_time.c\"rel =nofollow> bsd / kern / kern_time.c
。它调用 clock_get_boottime_nanotime
,它有一个很有希望的名字。
boottime_sec
is implemented in bsd/kern/kern_time.c
. It calls clock_get_boottime_nanotime
, which has a promising name.
clock_get_boottime_nanotime
在 <$ c $中实施C> osfmk /克恩/ clock.c 。硬编码总是在 nanosecs
参数中返回0。
这篇关于正常运行时间iOS目标C - 毫秒级精度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!