iOS 启动时间会漂移吗? [英] Can iOS boot time drift?

查看:31
本文介绍了iOS 启动时间会漂移吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此代码来确定我的 iOS 设备上次重新启动的时间:

I'm using this code to determine when my iOS device last rebooted:

int mib[MIB_SIZE];
size_t size;
struct timeval boottime;

mib[0] = CTL_KERN;
mib[1] = KERN_BOOTTIME;
size = sizeof(boottime);
if (sysctl(mib, MIB_SIZE, &boottime, &size, NULL, 0) != -1) {
    return boottime.tv_sec;
}

return 0;

这次我看到了一些异常情况.特别是,我保存了 long 并且在几天和几周后检查了保存的 long 和上面代码返回的值.

I'm seeing some anomalies with this time. In particular, I save the long and days and weeks later check the saved long agains the value returned by the above code.

我不确定,但我想我看到了一些偏差.这对我来说没有任何意义.我不会转换为 NSDate 以防止漂移.我认为启动时间是由内核在启动时记录的,不会再次计算,它只是存储.但是,iOS 是否可以将启动时间保存为 NSDate,并且存在任何固有的漂移问题?

I'm not sure, but I think I'm seeing some drift. This doesn't make any sense to me. I'm not converting to NSDate to prevent drift. I would think that boot time is record by the kernel when it boots and isn't computed again, it is just stored. But could iOS be saving boot time as an NSDate, with any inherent drift problems with that?

推荐答案

虽然 iOS 内核是闭源的,但可以合理地假设它的大部分与 OSX 内核相同,即 开源.

While the iOS Kernel is closed-source, it's reasonable to assume most of it is the same as the OSX Kernel, which is open-source.

osfmk/kern/clock.c 中有函数:

/*
 *  clock_get_boottime_nanotime:
 *
 *  Return the boottime, used by sysctl.
 */
void
clock_get_boottime_nanotime(
    clock_sec_t         *secs,
    clock_nsec_t        *nanosecs)
{
    spl_t   s;

    s = splclock();
    clock_lock();

    *secs = (clock_sec_t)clock_boottime;
    *nanosecs = 0;

    clock_unlock();
    splx(s);
}

clock_boottime 声明为:

static uint64_t     clock_boottime;             /* Seconds boottime epoch */

最后对该函数的注释表明它确实可以改变:

and finally the comment to this function shows that it can, indeed, change:

/*
 *  clock_set_calendar_microtime:
 *
 *  Sets the current calendar value by
 *  recalculating the epoch and offset
 *  from the system clock.
 *
 *  Also adjusts the boottime to keep the
 *  value consistent, writes the new
 *  calendar value to the platform clock,
 *  and sends calendar change notifications.
 */
void
clock_set_calendar_microtime(
    clock_sec_t         secs,
    clock_usec_t        microsecs)
{
    ...

更新以回答来自 OP 的查询

我不确定clock_set_calendar_microtime() 的调用频率,因为我不熟悉内核的内部工作原理;但是它会调整 clock_boottime 值并且 clock_boottime 值在 clock_initialize_calendar() 中初始化,所以我会说它可以被多次调用.我一直无法找到任何调用它使用:

I am not certain about how often clock_set_calendar_microtime() is called, as I am not familiar with the inner workings of the kernel; however it adjusts the clock_boottime value and the clock_bootime value is initialized in clock_initialize_calendar(), so I would say it can be called more than once. I have been unable to find any call to it using:

$ find . -type f -exec grep -l clock_set_calendar_microtime {} \;

这篇关于iOS 启动时间会漂移吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆