C++ MAC获取所有进程和当前进程的CPU使用率

#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;

#define CP_USER 0
#define CP_SYS  1
#define CP_IDLE 2
#define CP_NICE 3
#define CP_STATES 4

/**
 * @brief 获取当前进程cpu使用率
 * @param p_nProcessCPU 传出参
 */
void getProcessCPU(unsigned int& p_nProcessCPU)
{
    kern_return_t kr = { 0 };
    task_info_data_t tinfo = { 0 };
    mach_msg_type_number_t task_info_count = TASK_INFO_MAX;

    kr = task_info(mach_task_self(), TASK_BASIC_INFO, (task_info_t)tinfo, &task_info_count);
    if (kr != KERN_SUCCESS) 
    {
        p_nProcessCPU = 0;
        return;
    }

    task_basic_info_t basic_info = { 0 };
    thread_array_t thread_list = { 0 };
    mach_msg_type_number_t thread_count = { 0 };

    thread_info_data_t thinfo = { 0 };
    thread_basic_info_t basic_info_th = { 0 };

    basic_info = (task_basic_info_t)tinfo;

    // get threads in the task
    kr = task_threads(mach_task_self(), &thread_list, &thread_count);
    if (kr != KERN_SUCCESS) 
    {
        p_nProcessCPU = 0;
        return;
    }

    long tot_sec = 0;
    long tot_usec = 0;
    float tot_cpu = 0;

    for (int i = 0; i < thread_count; i++) 
    {
        mach_msg_type_number_t thread_info_count = THREAD_INFO_MAX;

        kr = thread_info(thread_list[i], THREAD_BASIC_INFO, (thread_info_t)thinfo, &thread_info_count);
        if (kr != KERN_SUCCESS) 
        {
            p_nProcessCPU = 0;
            return;
        }

        basic_info_th = (thread_basic_info_t)thinfo;
        if ((basic_info_th->flags & TH_FLAGS_IDLE) == 0) 
        {
            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 / (float)TH_USAGE_SCALE;
        }
    }

    kr = vm_deallocate(mach_task_self(), (vm_offset_t)thread_list, thread_count * sizeof(thread_t));
    if (kr != KERN_SUCCESS) 
    {
        p_nProcessCPU = 0;
        return;
    }

    float value = tot_cpu * 100;
    p_nProcessCPU = value;
}

host_cpu_load_info_data_t _get_cpu_percentage()
{
    kern_return_t              error;
    mach_msg_type_number_t     count;
    host_cpu_load_info_data_t  r_load;
    mach_port_t                mach_port;

    count = HOST_CPU_LOAD_INFO_COUNT;
    mach_port = mach_host_self();
    error = host_statistics(mach_port, HOST_CPU_LOAD_INFO,
        (host_info_t)&r_load, &count);

    if (error != KERN_SUCCESS)
    {
        return host_cpu_load_info_data_t();
    }

    return r_load;
}

/**
 * @brief 获取当前所有进程cpu使用率
 * @param p_nUsePercentageCPU 传出参
 */
void getCpuUsePercentage(unsigned int& p_nUsePercentageCPU)
{
    host_cpu_load_info_data_t load1 = _get_cpu_percentage();

    std::this_thread::sleep_for(std::chrono::seconds(1));

    host_cpu_load_info_data_t load2 = _get_cpu_percentage();

    // pre load times
    unsigned long long current_user = load1.cpu_ticks[CP_USER];
    unsigned long long current_system = load1.cpu_ticks[CP_SYS];
    unsigned long long current_nice = load1.cpu_ticks[CP_NICE];
    unsigned long long current_idle = load1.cpu_ticks[CP_IDLE];

    // Current load times
    unsigned long long next_user = load2.cpu_ticks[CP_USER];
    unsigned long long next_system = load2.cpu_ticks[CP_SYS];
    unsigned long long next_nice = load2.cpu_ticks[CP_NICE];
    unsigned long long next_idle = load2.cpu_ticks[CP_IDLE];

    // Difference between the two
    unsigned long long diff_user = next_user - current_user;
    unsigned long long diff_system = next_system - current_system;
    unsigned long long diff_nice = next_nice - current_nice;
    unsigned long long diff_idle = next_idle - current_idle;

    float value = static_cast<float>(diff_user + diff_system + diff_nice) /
        static_cast<float>(diff_user + diff_system + diff_nice + diff_idle) *
        100.0;
    p_nUsePercentageCPU = value;
}

int main()
{
    unsigned int nSystemCPU = 0;
    unsigned int nProcessCPU = 0;
    while (1)
    {
        getCpuUsePercentage(nSystemCPU);
        getProcessCPU(nProcessCPU);
        cout << "\nSystemCPU:" << nSystemCPU << endl;
        cout << "ProcessCPU:" << nProcessCPU << endl;
    }
    return 0;
}

参考文章:
https://blog.csdn.net/fishmai/article/details/125055032
https://www.jianshu.com/p/bc940ac493aa
https://blog.csdn.net/summerpowerz/article/details/81146435

你可能感兴趣的:(杂项,c++,开发语言,mac)