iOS 获取系统启动时间
来源:互联网 发布:青岛蓝博计价软件 编辑:程序博客网 时间:2024/06/06 16:55
做一个流量监控,之前的代码是通过sysctl读取进程列表,取得kernel_task进程的启动时间作为系统启动时间,如果系统重启就需要把网卡记录的流量全部累加,否则用本次读取的网卡流量数据减去上一次记录的数据认为是这段时间内用户产生的流量。
在iOS9上 sysctl被封了,于是这段代码就挂了。没办法拿到系统启动时间,就会不停的累加网卡记录的全部流量。
使用sysctl取得kernel_task启动时间
+ (NSDate *)systemStartTime{ size_t length = 0; static const int name[] = {CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0}; int err = sysctl( (int *) name, (sizeof(name) / sizeof(*name)) - 1, NULL, &length, NULL, 0); if (err == -1) { err = errno; } if (err == 0) { struct kinfo_proc *procBuffer = (struct kinfo_proc *)malloc(length); if(procBuffer == NULL) { return nil; } sysctl( (int *) name, (sizeof(name) / sizeof(*name)) - 1, procBuffer, &length, NULL, 0); int count = (int)length / sizeof(struct kinfo_proc); for (int i = 0; i < count; ++i) { NSString *procName = [NSString stringWithCString:procBuffer[i].kp_proc.p_comm encoding:NSUTF8StringEncoding]; if ([procName isEqualToString:@"kernel_task"]) { return [NSDate dateWithTimeIntervalSince1970:procBuffer[i].kp_proc.p_un.__p_starttime.tv_sec]; } } } return nil;}
使用sysctlbyname取得boottime
+ (NSDate *)systemStartTime { size_t size; sysctlbyname("kern.boottime", NULL, &size, NULL, 0); char *boot_time = malloc(size); sysctlbyname("kern.boottime", boot_time, &size, NULL, 0); uint32_t timestamp = 0; memcpy(×tamp, boot_time, sizeof(uint32_t)); free(boot_time); NSDate* bootTime = [NSDate dateWithTimeIntervalSince1970:timestamp]; NSLog(@"BootTime: %@", bootTime); return bootTime;}
boottime 也是有问题的,就是用户手动设置系统时间后,bootime会变动成手动设置的那个时刻;另外mach_absolute_time读取出来的tick数和bootime是一致的,个人猜测boottime其实就是换算mach_absolute_time()的结果。
0 0
- iOS 获取系统启动时间
- linux获取系统启动时间
- linux获取系统启动时间
- linux c获取系统启动时间
- iOS如何获取系统启动页
- linux c 获取系统启动时间 uptime
- VC++ 获取操作系统的时间,并且在保存系统启动时间。
- Android系统启动时间(不是系统当前时间)的获取
- /proc/uptime详解,获取系统启动时间,系统负载率
- Android下uptime获取系统启动和运行时间
- ios获取系统时间
- IOS获取本地时间
- IOS 获取时间
- IOS 本地获取时间--
- ios获取当前时间
- ios 获取当前时间
- 获取系统时间ios
- iOS获取当前时间
- ffmpeg 编译
- php tcp框架
- kinect+opencv之环境配置
- Host 'xxxx' is blocked because of many connection errors; unblock with 'mysqladmin flush-hosts'
- 位运算
- iOS 获取系统启动时间
- 网络层到运输层
- Zabbix 命令:zabbix_get
- python爬虫基础
- Acegi Security -- Spring下最优秀的安全系统
- 1023. 组个最小数
- libev安装
- Eclipse常用快捷键
- IOS 8 固定方向