iOS 和Mac 线程的 CPU 使用率

发布于 2024-11-25 14:05:32 字数 91 浏览 4 评论 0原文

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

Does anyone know if it is possible to get the cpu usage for a specific thread, process, or some code in the application?
If you look at AUGraph it has a function which returns average cpu usage. How do they do that?

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

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

发布评论

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

评论(2

分分钟 2024-12-02 14:05:32

我不确定这是否也适用于 iOS,但对于 OS X,您可以通过访问 Mach/BSD 子系统来获取所需的信息。基本上,您可以获得给定进程的线程列表,然后对所有线程的使用情况求和以获得进程的 CPU 使用情况。如果只需要一个线程cpu使用情况,则不需要求和。

它并不像人们希望的那样简单和直接,但它就是这样(此代码基于 Amit Singh“Mac OS X 内部结构”):

            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

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

I am not sure that this also applies to iOS, but for OS X, you can get the info you need by accessing the Mach/BSD subsystem. Basically you get the list of threads for a given process, then sum all threads' usage to get the process cpu usage. If you need only a thread cpu usage, you don't need to sum up.

It is not as easy and straightforward as one might desire, but here it is (this code is based on 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

I don't know if there is a better way to do that, possibly so, but this one has worked for me.

吃颗糖壮壮胆 2024-12-02 14:05:32

编辑:抱歉——误读了你的问题。这并不显示应用程序中特定线程/进程的用法,但确实执行了 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 手册页 此处

EDIT: Apologies--misread your question. This doesn't show usage for a specific thread/process in an app, but does do what AUGraph does.

Just use 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]);

Sample 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

So, those reported values are percentages. To see them as such:

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

Sample output from this:

... 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%

More from the Apple man page here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文