如何测量 iOS 线程的实际 CPU 时间?

发布于 2025-01-01 18:38:06 字数 1070 浏览 1 评论 0原文

我正在寻找 Android 的 iOS 模拟 SystemClock.currentThreadTimeMillis () 或 Microsoft 的 GetThreadTimes() 或Posix clock_gettime(CLOCK_THREAD_CPUTIME_ID, )pthread_getcpuclockid() 函数用于测量多线程中函数使用的实际“干净”时间 应用。也就是说,我不想测量函数中花费的实际挂钟时间,而是测量 CPU 上的时间。

我在 这里在 stackoverflow其他地方。不幸的是,这两种情况都不适用于 iOS。

iOS 上有类似的功能吗?

I am looking for an iOS analog for Android's SystemClock.currentThreadTimeMillis() or Microsoft's GetThreadTimes() or Posix clock_gettime(CLOCK_THREAD_CPUTIME_ID, ) and pthread_getcpuclockid() functions to measure the actual "clean" time used by a function in a multithreaded application. That is, I don't want to measure the actual wall clock time spent in a function, but the on-CPU time.

I found interesting discussions about this here on stackoverflow and elsewhere. Unfortunately, neither applies to iOS.

Is there a comparable function for this on iOS?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

何处潇湘 2025-01-08 18:38:06

如果有人正在寻找一个好的答案:

不久前,我在这个答案中发现了一些很棒的代码(用于查找CPU时间/OSX 中的内存使用情况),并稍作调整。我用它来对 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], THREAD_BASIC_INFO, (thread_info_t)thi, &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;
}

In case anyone is looking for a good answer:

A while ago I found some great code in this answer (for finding CPU time/memory usage in OSX), and adapted it slightly. I used this for benchmarking some NEON optimizations on the ARM. You would probably only need the section which gets time for the current thread.

#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], THREAD_BASIC_INFO, (thread_info_t)thi, &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;
}
审判长 2025-01-08 18:38:06

我使用这段代码来测量线程 CPU 时间。它基于 tcovo 的答案,但更加简洁和集中。

thread_time.h

#ifndef thread_time_h
#define thread_time_h

#include <stdint.h>

typedef struct thread_time {
    int64_t user_time_us;
    int64_t system_time_us;
} thread_time_t;

thread_time_t thread_time();
thread_time_t thread_time_sub(thread_time_t const a, thread_time_t const b);

#endif /* thread_time_h */

thread_time.c

#include "thread_time.h"
#include <mach/mach_init.h>
#include <mach/thread_act.h>
#include <mach/thread_info.h>

int64_t time_value_to_us(time_value_t const t) {
    return (int64_t)t.seconds * 1000000 + t.microseconds;
}

thread_time_t thread_time() {
    thread_basic_info_data_t basic_info;
    mach_msg_type_number_t count = THREAD_BASIC_INFO_COUNT;
    kern_return_t const result = thread_info(mach_thread_self(), THREAD_BASIC_INFO, (thread_info_t)&basic_info, &count);
    if (result == KERN_SUCCESS) {
        return (thread_time_t){
            .user_time_us   = time_value_to_us(basic_info.user_time),
            .system_time_us = time_value_to_us(basic_info.system_time)
        };
    } else {
        return (thread_time_t){-1, -1};
    }
}

thread_time_t thread_time_sub(thread_time_t const a, thread_time_t const b) {
    return (thread_time_t){
        .user_time_us   = a.user_time_us   - b.user_time_us,
        .system_time_us = a.system_time_us - b.system_time_us
    };
}

I use this code to measure thread CPU time. It's based on tcovo's answer, but is more concise and focused.

thread_time.h

#ifndef thread_time_h
#define thread_time_h

#include <stdint.h>

typedef struct thread_time {
    int64_t user_time_us;
    int64_t system_time_us;
} thread_time_t;

thread_time_t thread_time();
thread_time_t thread_time_sub(thread_time_t const a, thread_time_t const b);

#endif /* thread_time_h */

thread_time.c

#include "thread_time.h"
#include <mach/mach_init.h>
#include <mach/thread_act.h>
#include <mach/thread_info.h>

int64_t time_value_to_us(time_value_t const t) {
    return (int64_t)t.seconds * 1000000 + t.microseconds;
}

thread_time_t thread_time() {
    thread_basic_info_data_t basic_info;
    mach_msg_type_number_t count = THREAD_BASIC_INFO_COUNT;
    kern_return_t const result = thread_info(mach_thread_self(), THREAD_BASIC_INFO, (thread_info_t)&basic_info, &count);
    if (result == KERN_SUCCESS) {
        return (thread_time_t){
            .user_time_us   = time_value_to_us(basic_info.user_time),
            .system_time_us = time_value_to_us(basic_info.system_time)
        };
    } else {
        return (thread_time_t){-1, -1};
    }
}

thread_time_t thread_time_sub(thread_time_t const a, thread_time_t const b) {
    return (thread_time_t){
        .user_time_us   = a.user_time_us   - b.user_time_us,
        .system_time_us = a.system_time_us - b.system_time_us
    };
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文