iOS性能指标数据收集

3,482 阅读3分钟

日常app发现性能问题往往需要通过用户反馈,再通过分析代码进行优化。为了更高效的定位问题,需要收集app相关的性能指标,例如CPU、RAM、FPS、启动时长、网络等。下面通过一些系统api来统计这些指标,了解这些指标的具体含义,通过对比,获取的指标数据与Xcode基本是一致的。

CPU

1、CPU Core Count

首先是获取系统的内核数,Xcode上显示的CPU最大占用率,往往在不同设备也不同,这个和内核数有关,例如调试设备iphone11(6核),显示最大占用为600%,设备ipad mini2(双核),显示最大占用为200%,所以多线程场景下可能超过100%的情况。

+ (NSUInteger)sysCoresCount {

    return [NSProcessInfo processInfo].activeProcessorCount;}

2、App CPU Usage

获取应用的CPU占用,app作为一个进程在系统中运行,运行进程又包含多个线程,线程是系统调度和分配的基本单位,如果可以获取到每一条线程的CPU占用就可以知道整个进程的数据,Xcode也提供当前应用每个线程的CPU图表数据,所有线程的CPU累加就得到应用的CPU占用。

通过task_threads(mach_task_self(), &thread_list, &thread_count)获取到thread_list,返回kern_return_t类型,其实是int类型,返回0表示成功,task可以理解为多个线程运行环境的抽象。mach_task_self()入参表示当前应用,遍历所有线程thread_info(thread_list[j], THREAD_BASIC_INFO,(thread_info_t)thinfo, &thread_info_count)获取每个线程信息,thread_basic_info_t类型是一个结构体,包含的cpu_usage就是线程的CPU占用。

struct thread_basic_info {
	time_value_t    user_time;      /* user run time */
	time_value_t    system_time;    /* system run time */
	integer_t       cpu_usage;      /* scaled cpu usage percentage */
	policy_t        policy;         /* scheduling policy in effect */
	integer_t       run_state;      /* run state (see below) */
	integer_t       flags;          /* various flags (see below) */
	integer_t       suspend_count;  /* suspend count for thread */
	integer_t       sleep_time;     /* number of seconds that thread
	                                 *  has been sleeping */
};

+ (CGFloat)appCpuUsage {

    kern_return_t kr;
    task_info_data_t tinfo;
    mach_msg_type_number_t task_info_count;

    task_info_count = TASK_INFO_MAX;
    // task 线程执行环境的抽象
    kr = task_info(mach_task_self(), MACH_TASK_BASIC_INFO, (task_info_t)tinfo, &task_info_count);
    if (kr != KERN_SUCCESS) {
        return -1.f;
    }

    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;

    // get threads in the task
    kr = task_threads(mach_task_self(), &thread_list, &thread_count);
    if (kr != KERN_SUCCESS) {
        return -1.f;
    }

    long total_time     = 0;
    long total_userTime = 0;
    CGFloat total_cpu   = 0;

    // for each thread
    for (int i = 0; i < (int)thread_count; i++) {        thread_info_count = THREAD_INFO_MAX;
        kr = thread_info(thread_list[i], 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)) {
            total_time     = total_time + basic_info_th->user_time.seconds + basic_info_th->system_time.seconds;
            total_userTime = total_userTime + basic_info_th->user_time.microseconds + basic_info_th->system_time.microseconds;
            total_cpu      = total_cpu + basic_info_th->cpu_usage / (float)TH_USAGE_SCALE;
        }
    }

    kr = vm_deallocate(mach_task_self(), (vm_offset_t)thread_list, thread_count * sizeof(thread_t));
    assert(kr == KERN_SUCCESS);

    return total_cpu;
}

3、System CPU Usage

分别计算CPU各个状态下的差值,kernel CPU的四种状态,user、nice、system、idleidle是空闲状态不计入Usage。

+ (CGFloat)sysCpuUsage {
    kern_return_t kr;
    mach_msg_type_number_t count;
    static host_cpu_load_info_data_t host_cpu_load_info = {0, 0, 0, 0};
    host_cpu_load_info_data_t info;
    count = HOST_CPU_LOAD_INFO_COUNT;

    kr = host_statistics64(mach_host_self(), HOST_CPU_LOAD_INFO, (host_info64_t)&info, &count);
    if (kr != KERN_SUCCESS) {
        return -1.f;
    }
    natural_t user   = info.cpu_ticks[CPU_STATE_USER] - host_cpu_load_info.cpu_ticks[CPU_STATE_USER];
    natural_t nice   = info.cpu_ticks[CPU_STATE_NICE] - host_cpu_load_info.cpu_ticks[CPU_STATE_NICE]; // 进程的执行优先级
    natural_t system = info.cpu_ticks[CPU_STATE_SYSTEM] - host_cpu_load_info.cpu_ticks[CPU_STATE_SYSTEM];
    natural_t idle   = info.cpu_ticks[CPU_STATE_IDLE] - host_cpu_load_info.cpu_ticks[CPU_STATE_IDLE]; // CPU空闲状态
    natural_t total  = user + nice + system + idle;
    host_cpu_load_info    = info;

    return (user + nice + system) * 100.0 / total;
}

RAM

1、App Memory Usage

+ (NSUInteger)appMemoryUsage {

    task_vm_info_data_t vmInfo;
    mach_msg_type_number_t count = TASK_VM_INFO_COUNT;

    kern_return_t kernelReturn = task_info(mach_task_self(), TASK_VM_INFO, (task_info_t)&vmInfo, &count);
    if(kernelReturn == KERN_SUCCESS) {
        return vmInfo.phys_footprint;
    } else {
        return -1;
    }
}

2、System Memory Usage

系统的内存主要由这几个部分组成:

Wired memory:

Active memory:当前系统正在使用并且最近在使用的内存。

Inactive memory:当前没有在使用但最近在使用的内存,例如,某个app当前退出,之前使用的那部分内存标记为Inactive memory,当系统内存告急时可以被其他应用使用,如果在被其他应用使用之前再次打开app,将非活跃内存转化成活跃内存,开启速度则更快。

Free memory:空闲的那部分内存,未被使用的。

从Apple的文档Memory Usage Performance Guidelines可以找到相关内容。

需要注意的是vm_statistics64_data_t vmstat使用64位防止数据溢出,

+ (NSUInteger)sysMemoryUsage {   
    vm_size_t pagesize = 4096;

    mach_msg_type_number_t count = HOST_VM_INFO64_COUNT;

    vm_statistics64_data_t vmstat; 

    if (host_statistics64(mach_host_self(), HOST_VM_INFO64, (host_info64_t)&vmstat, &count) != KERN_SUCCESS) {
        return 0;
    }

    long long wireMemory     = vmstat.wire_count * pagesize;  // 当前应用使用的基础Memory
    long long activeMemory   = vmstat.active_count * pagesize; // 当前active状态的Memory
    long long inactiveMemory = vmstat.inactive_count * pagesize; // 当前inactive状态的Memory
    return wireMemory + activeMemory + inactiveMemory;
}

3、Total Physical Memory

+ (NSUInteger)totalPhysicalMemory {
    return [NSProcessInfo processInfo].physicalMemory;;
}