• 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;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152

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

  • 相关阅读:
    R语言dplyr包select函数删除dataframe数据中的以指定字符串开头的数据列(变量)
    读《mysql是怎样运行的》有感
    基于Springboot外卖系统19:用户地址+默认收货地址
    CSDN Q: “这段代码算是在STC89C52RC51单片机上完成PWM呼吸灯了吗?“
    YUV和RGB的相互转换实验
    带你深入了解Web3开发者堆栈
    「Redis」01 NoSQL及Redis概述
    C++ Primer学习笔记-----第十七章:标准款特殊设施
    [第三篇]——CentOS Docker 安装
    python基础
  • 原文地址:https://blog.csdn.net/weijianjain/article/details/133294845