iOS&线程的Mac CPU使用率

时间:2011-07-22 09:46:05

标签: ios macos audio cpu-usage

有谁知道是否可以获取应用程序中特定线程,进程或某些代码的cpu使用情况? 如果你看一下AUGraph,它有一个返回平均cpu使用率的函数。他们是如何做到的?

2 个答案:

答案 0 :(得分:3)

我不确定这也适用于iOS,但对于OS X,您可以通过访问Mach / BSD子系统获得所需的信息。基本上,您获取给定进程的线程列表,然后将所有线程的使用量相加以获得进程cpu使用情况。如果只需要线程cpu使用,则无需总结。

它并不像人们想要的那么容易和直接,但在这里(这段代码基于Amit Singh“Mac OS X internals”):

            pid_t pid = ...;   //-- this is the process id you need info for
            task_t port;
            task_for_pid(mach_task_self(), pid, &port);

            task_info_data_t tinfo;
            mach_msg_type_number_t task_info_count;

            task_info_count = TASK_INFO_MAX;
            kr = task_info(port, TASK_BASIC_INFO, (task_info_t)tinfo, &task_info_count);
            if (kr != KERN_SUCCESS) {
                    continue; //-- skip this task
            }

            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(port, &thread_list, &thread_count);
            if (kr != KERN_SUCCESS) {
                    <HANDLE ERROR>
                    continue;
            }
            if (thread_count > 0)
                    stat_thread += thread_count;

            long tot_sec = 0;
            long tot_usec = 0;
            long tot_cpu = 0;
            int j;

            for (j = 0; j < 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) {
                            <HANDLE ERROR>
                            continue;
                    }
                    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->system_time.microseconds + basic_info_th->system_time.microseconds;
                            tot_cpu = tot_cpu + basic_info_th->cpu_usage;
                    }

            } // for each thread

我不知道是否有更好的方法可以做到这一点,可能是这样,但这个对我有用。

答案 1 :(得分:2)

编辑:道歉 - 误读了您的问题。这不会显示应用程序中特定线程/进程的用法,但会执行AUGraph所做的事情。

只需使用getloadavg()

#include <stdlib.h>

double loadavg[3];
getloadavg(loadavg, 3);
NSLog(@"Average over last minute: %f", loadavg[0]);
NSLog(@"Average over last 5 minutes: %f", loadavg[1]);
NSLog(@"Average over last 10 minutes: %f", loadavg[2]);

示例输出:

... getloadavg[62486:207] Average over last minute: 0.377441
... getloadavg[62486:207] Average over last 5 minutes: 0.450195
... getloadavg[62486:207] Average over last 10 minutes: 0.415527

所以,那些报告的值是百分比。要看到它们:

#include <stdlib.h>

double loadavg[3];
getloadavg(loadavg, 3);
NSLog(@"Average over last minute: %02.2f%%", loadavg[0] * 100);
NSLog(@"Average over last 5 minutes: %02.2f%%", loadavg[1] * 100);
NSLog(@"Average over last 10 minutes: %02.2f%%", loadavg[2] * 100);

此示例输出:

... getloadavg[62531:207] Average over last minute: 23.93%
... getloadavg[62531:207] Average over last 5 minutes: 33.01%
... getloadavg[62531:207] Average over last 10 minutes: 36.72%

更多来自Apple手册页here