iOS 之电量能耗检测及优化

2019-05-15  本文已影响0人  iOS小童

能耗

  1. CPU

    不论用户是否正在直接使用,CPU都是应用所使用的主要硬件,在后台操作和处理推送通知时,应用仍会消耗CPU资源

计算量的能耗:

能耗优化:

  1. 网络

避免在没有链接Wi-Fi的情况下进行高带宽消耗的操作,比如视频流。蜂窝无线网络对电量的消耗远大于Wi-Fi信号,根源在于LTE设备基于多输入,多输出技术,使用多个并发信号以维护两端的LTE链接

  1. 定位管理器和GPS

使用GPS计算坐标需要确定两点信息:

时间锁:每个GPS卫星每毫米广播唯一一个1023位随机数,因而数据传播速率1.024Mbit/s。GSP的接受芯片必须正确的与卫星的时间锁对齐。

频率锁:GPS接收器必须计算由接收器与卫星的相对运动导致的多普勒偏差带来的信号误差。

所以:不断的计算坐标会使用CPU和GPS的硬件资源,迅速的消耗电池电量。

  1. 屏幕
  1. 其他硬件

硬件的使用,基本规则是一样的:只有在前台需要时才与硬件交互,否则应该处理停止状态。扬声器和无线蓝牙可能例外

  1. 电池电量
/**
 设置进程的最小电量

 @param minLevel f最小阀值
 @return YES:可继续执行,NO:不可继续执行
 */
- (BOOL)shouldProceedWithMiniLevel:(NSUInteger)minLevel{
    UIDevice *device = [UIDevice currentDevice];
    device.batteryMonitoringEnabled = YES;
    UIDeviceBatteryState state = device.batteryState;
    if (state == UIDeviceBatteryStateCharging || state == UIDeviceBatteryStateFull) {
        return YES;
    }
    NSUInteger batteryLevel = (NSUInteger)(device.batteryLevel * 100);
    if (batteryLevel >= minLevel) {
        return YES;
    }
    return NO;
}
/**
 CPU的使用率

 @return 已使用的CPU
 */
 -(CGFloat)getAPPCPUUsage{
    kern_return_t kr;
    task_info_data_t tinfo;
    mach_msg_type_number_t task_info_count;
    
    task_info_count = TASK_INFO_MAX;
    kr = task_info(mach_task_self(), TASK_BASIC_INFO, (task_info_t)tinfo, &task_info_count);
    if (kr != KERN_SUCCESS) {
        return -1;
    }
    
    task_basic_info_t      basic_info;
    thread_array_t         thread_list;
    mach_msg_type_number_t thread_count;
    
    thread_info_data_t     thinfo;
    mach_msg_type_number_t thread_info_count;
    
    thread_basic_info_t basic_info_th;
    uint32_t stat_thread = 0; // Mach threads
    
    basic_info = (task_basic_info_t)tinfo;
    
    // get threads in the task
    kr = task_threads(mach_task_self(), &thread_list, &thread_count);
    if (kr != KERN_SUCCESS) {
        return -1;
    }
    if (thread_count > 0)
        stat_thread += thread_count;
    
    long tot_sec = 0;
    long tot_usec = 0;
    float tot_cpu = 0;
    int j;
    
    for (j = 0; j < (int)thread_count; j++)
    {
        thread_info_count = THREAD_INFO_MAX;
        kr = thread_info(thread_list[j], THREAD_BASIC_INFO,
                         (thread_info_t)thinfo, &thread_info_count);
        if (kr != KERN_SUCCESS) {
            return -1;
        }
        
        basic_info_th = (thread_basic_info_t)thinfo;
        
        if (!(basic_info_th->flags & TH_FLAGS_IDLE)) {
            tot_sec = tot_sec + basic_info_th->user_time.seconds + basic_info_th->system_time.seconds;
            tot_usec = tot_usec + basic_info_th->user_time.microseconds + basic_info_th->system_time.microseconds;
            tot_cpu = tot_cpu + basic_info_th->cpu_usage / (float)TH_USAGE_SCALE * 100.0;
        }
        
    } // for each thread
    
    kr = vm_deallocate(mach_task_self(), (vm_offset_t)thread_list, thread_count * sizeof(thread_t));
    assert(kr == KERN_SUCCESS);
    
    return tot_cpu;
    }
上一篇 下一篇

猜你喜欢

热点阅读