【问题标题】:How do you measure actual on-CPU time for an iOS thread?您如何测量 iOS 线程的实际 CPU 运行时间?
【发布时间】:2012-02-23 06:19:47
【问题描述】:

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

我发现了关于 here on stackoverflowelsewhere 的有趣讨论。不幸的是,两者都不适用于 iOS。

在 iOS 上有类似的功能吗?

【问题讨论】:

  • 您只是在寻找精确的挂钟时间吗?如果是这样,这个问题的答案应该有效:How can I get a precise time, for example in milliseconds in Objective-C?
  • @Brad Larson:不,我想测量与挂钟相反的值,以测量给定线程正在使用的纯 CPU 时间。
  • 只是为了检查:你真的需要把它放在你的代码中吗? Instruments 可以很好地做到这一点,无需您修改程序。
  • Rob 的正确之处在于 Instruments 具有强大的功能,可以仅显示方法的 CPU 执行时间(Shark 也是如此,但 Instruments 更容易使用)。您也可以使用 DTrace 的 vtimestamp,就像我在本文的“时间分析”部分中展示的那样:macresearch.org/…,但这仅适用于在 iOS 模拟器中运行的应用程序,而不适用于实际设备上。
  • 这很酷,但不幸的是我的分析是针对 ARM 处理器的,因此 iOS 模拟器是无关紧要的。

标签: objective-c ios multithreading profiling pthreads


【解决方案1】:

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

不久前,我在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], 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;
}

【讨论】:

  • 是的!这给了我们缺失的信息!!
【解决方案2】:

我使用此代码来测量线程 CPU 时间。它基于tcovo's answer,但更加简洁和专注。

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
    };
}

【讨论】:

  • 碰巧你有这个的 linux/android 形式吗?
  • @JoelTeply 此代码使用 Mach 内核 API,因此它特定于 macOS 和衍生产品(iOS、watchOS、tvOS)。不幸的是,我现在没有针对 Linux 内核的适配。
  • 嘿,有没有办法告诉你使用主线程花了多少时间?是这个吗?另外,您的方法名称中的_us 是什么意思?最后,thread_time 输出的数字是多少,即秒、毫秒等?谢谢
  • -us 是纳秒,知道了。哈哈。在这里问了一个问题stackoverflow.com/questions/57010582/…
  • @Wazza 这些函数为您提供有关它们被调用的线程的信息。如果您从主线程调用它们,您将获得有关主线程的信息。 us 代表 microseconds (10^-6),而不是 nano- (10^-9)。
【解决方案3】:

【讨论】:

  • 不幸的是,clock() 不测量每个线程的时间,除了非常不准确。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-12-08
  • 2019-06-10
  • 2015-03-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多