简体   繁体   中英

iOS & Mac CPU Usage for thread

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?

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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