正常运行时间iOS Objective-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
由实现bsd/kern/kern_sysctl.c
中的sysctl_boottime
函数.它会调用boottime_sec
.
KERN_BOOTTIME
is implemented by the sysctl_boottime
function in bsd/kern/kern_sysctl.c
. It calls boottime_sec
.
boottime_sec
在中实现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
在osfmk/kern/clock.c .硬编码总是在其nanosecs
参数中返回0.
这篇关于正常运行时间iOS Objective-C-毫秒精度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!