加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

objective-c – 如何衡量iOS线程的实际CPU时间?

发布时间:2020-12-16 04:51:19 所属栏目:百科 来源:网络整理
导读:我正在寻找适用于 Android SystemClock.currentThreadTimeMillis()或Microsoft GetThreadTimes()或Posix clock_gettime(CLOCK_THREAD_CPUTIME_ID,)和 pthread_getcpuclockid()功能的iOS模拟,以测量多线程应用程序中函数使用的实际“干净”时间.也就是说,我不
我正在寻找适用于 Android SystemClock.currentThreadTimeMillis()或Microsoft GetThreadTimes()或Posix clock_gettime(CLOCK_THREAD_CPUTIME_ID,)和 pthread_getcpuclockid()功能的iOS模拟,以测量多线程应用程序中函数使用的实际“干净”时间.也就是说,我不想测量功能中花费的实际挂钟时间,而是测量CPU时间.

我发现有关此here on stackoverflow和elsewhere的有趣讨论.不幸的是,它们都不适用于iOS.

在iOS上有这样的功能吗?

解决方法

万一有人在寻找一个好的答案:

不久之前,我在this answer中找到了一些很棒的代码(用于在OSX中查找CPU时间/内存使用情况),并稍微调整了一下.我用它来对ARM上的一些NEON优化进行基准测试.您可能只需要为当前线程获取时间的部分.

#include <sys/types.h>
#include <sys/sysctl.h>
#include <mach/mach_init.h>
#include <mach/mach_host.h>
#include <mach/mach_port.h>
#include <mach/mach_traps.h>
#include <mach/task_info.h>
#include <mach/thread_info.h>
#include <mach/thread_act.h>
#include <mach/vm_region.h>
#include <mach/vm_map.h>
#include <mach/task.h>


typedef struct {
    double utime,stime;
} CPUTime;

int get_cpu_time(CPUTime *rpd,bool_t thread_only)
{
    task_t task;
    kern_return_t error;
    mach_msg_type_number_t count;
    thread_array_t thread_table;
    thread_basic_info_t thi;
    thread_basic_info_data_t thi_data;
    unsigned table_size;
    struct task_basic_info ti;

    if (thread_only) {
        // just get time of this thread
        count = THREAD_BASIC_INFO_COUNT;
        thi = &thi_data;
        error = thread_info(mach_thread_self(),THREAD_BASIC_INFO,(thread_info_t)thi,&count);
        rpd->utime = thi->user_time.seconds + thi->user_time.microseconds * 1e-6;
        rpd->stime = thi->system_time.seconds + thi->system_time.microseconds * 1e-6;
        return 0;
    }


    // get total time of the current process

    task = mach_task_self();
    count = TASK_BASIC_INFO_COUNT;
    error = task_info(task,TASK_BASIC_INFO,(task_info_t)&ti,&count);
    assert(error == KERN_SUCCESS);
    { /* calculate CPU times,adapted from top/libtop.c */
        unsigned i;
        // the following times are for threads which have already terminated and gone away
        rpd->utime = ti.user_time.seconds + ti.user_time.microseconds * 1e-6;
        rpd->stime = ti.system_time.seconds + ti.system_time.microseconds * 1e-6;
        error = task_threads(task,&thread_table,&table_size);
        assert(error == KERN_SUCCESS);
        thi = &thi_data;
        // for each active thread,add up thread time
        for (i = 0; i != table_size; ++i) {
            count = THREAD_BASIC_INFO_COUNT;
            error = thread_info(thread_table[i],&count);
            assert(error == KERN_SUCCESS);
            if ((thi->flags & TH_FLAGS_IDLE) == 0) {
                rpd->utime += thi->user_time.seconds + thi->user_time.microseconds * 1e-6;
                rpd->stime += thi->system_time.seconds + thi->system_time.microseconds * 1e-6;
            }
            error = mach_port_deallocate(mach_task_self(),thread_table[i]);
            assert(error == KERN_SUCCESS);
        }
        error = vm_deallocate(mach_task_self(),(vm_offset_t)thread_table,table_size * sizeof(thread_array_t));
        assert(error == KERN_SUCCESS);
    }
    if (task != mach_task_self()) {
        mach_port_deallocate(mach_task_self(),task);
        assert(error == KERN_SUCCESS);
    }
    return 0;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读