正常运行时间iOS Objective-C-毫秒精度 [英] Uptime iOS Objective-C - millisecond precision

查看:130
本文介绍了正常运行时间iOS Objective-C-毫秒精度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试延长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_nanotimeosfmk/kern/clock.c .硬编码总是在其nanosecs参数中返回0.

这篇关于正常运行时间iOS Objective-C-毫秒精度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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