繁体   English   中英

iOS & Mac CPU 线程使用率

[英]iOS & Mac CPU Usage for thread

有谁知道是否可以获得应用程序中特定线程、进程或某些代码的 cpu 使用情况? 如果您查看 AUGraph,它有一个 function,它返回平均 CPU 使用率。 他们是怎么做到的?

我不确定这是否也适用于 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

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

编辑:道歉——误读了你的问题。 这不会显示应用程序中特定线程/进程的使用情况,但会做 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]);

样品 output:

... 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);

样品 output 来自:

... 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 手册页

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM