【问题标题】:How to get CPU, GPU and RAM usage from macOS's Activity Monitor app using C++?如何使用 C++ 从 macOS 的 Activity Monitor 应用程序获取 CPU、GPU 和 RAM 使用情况?
【发布时间】:2021-08-29 23:05:34
【问题描述】:

我有一个 C++ 应用程序,我想分析 CPU、GPU 和 RAM 的使用情况。

所有这些信息都可以在 macOS 的 Monitor Control 应用程序中找到

现在我正在通过 shell 命令获取这些信息

const std::string getCPUCommand =
    "ps -p " + std::to_string(pid) + " -o %cpu | awk 'FNR == 2 {print $1}'";

但这只会获取 CPU 使用率,而不是 GPU,而且也是一个性能不高的命令。

有没有更好的方法来获取所有这些信息?

【问题讨论】:

    标签: c++ macos profiling


    【解决方案1】:

    关于 CPU 和 RAM 使用情况,已在此回答 post

    简单地说-

    交换/虚拟内存

    #include <sys/param.h>
    #include <sys/mount.h>
    
    struct statfs stats;
    if(statfs("/",&stats)==0)
        FreeSwap=(unisgned long long)stats.f_bsize * stats.f_bfree;
    

    xsw_usage 结构用于查找使用的总交换内存

    xsw_usage used_swap={0};
    
    unsigned int size=sizeof(used_swap);
    

    内存

    当前使用的内存-

    #include <mach/vm_statistics.h>
    #include <mach/mach_types.h>
    #include <mach/mach_init.h>
    #include <mach/mach_host.h>
    
    int main(int argc, const char * argv[]) {
        vm_size_t page_size;
        mach_port_t mach_port;
        mach_msg_type_number_t count;
        vm_statistics64_data_t vm_stats;
    
        mach_port = mach_host_self();
        count = sizeof(vm_stats) / sizeof(natural_t);
        if (KERN_SUCCESS == host_page_size(mach_port, &page_size) &&
            KERN_SUCCESS == host_statistics64(mach_port, HOST_VM_INFO,
                                            (host_info64_t)&vm_stats, &count))
        {
            long long free_memory = (int64_t)vm_stats.free_count * (int64_t)page_size;
    
            long long used_memory = ((int64_t)vm_stats.active_count +
                                     (int64_t)vm_stats.inactive_count +
                                     (int64_t)vm_stats.wire_count) *  (int64_t)page_size;
            printf("free memory: %lld\nused memory: %lld\n", free_memory, used_memory);
        }
    
        return 0;
    }
    

    对于 GPU 的使用,您可以使用 NVIDIA 的Cuda Toolkit

    特别适用于 Mac -

    #include <stdlib.h>
    #include <iostream>
    #include <iomanip>
    #include <cuda_runtime_api.h>
    
    #define CUDA_CALL(function, ...)  { \
        cudaError_t status = function(__VA_ARGS__); \
        anyCheck(status == cudaSuccess, cudaGetErrorString(status), #function, __FILE__, __LINE__); \
    }
    
    void anyCheck(bool is_ok, const char *description, const char *function, const char *file, int line) {
        if (!is_ok) {
            std::cout << "Error: " << description << " in " << function << " at " << file << ":" << line << std::endl;
            exit(EXIT_FAILURE);
        }
    }
    
    int main() {
        int cudaDeviceCount;
        struct cudaDeviceProp deviceProp;
        size_t memFree, memTotal;
    
        CUDA_CALL(cudaGetDeviceCount, &cudaDeviceCount);
    
        for (int deviceId = 0; deviceId < cudaDeviceCount; ++deviceId) {
            CUDA_CALL(cudaSetDevice, deviceId);
            CUDA_CALL(cudaGetDeviceProperties, &deviceProp, deviceId);
    
            //std::cout.imbue(std::locale("en_US.utf8"));
            std::cout << "Device " << deviceId;
            std::cout << " [PCIe " << deviceProp.pciDomainID << ":" << deviceProp.pciBusID
                      << ":" << deviceProp.pciDeviceID << ".0]";
            std::cout << ": " << deviceProp.name << " (CC " << deviceProp.major << "." << deviceProp.minor << ")";
            CUDA_CALL(cudaMemGetInfo, &memFree, &memTotal);
            std::cout << ": " << std::setprecision(5) << memFree/(1024*1024.) 
                      << " of " << memTotal/(1024*1024.) << " MB (i.e. "
                      << std::setprecision(3) << 100*memFree/(float)memTotal << "%) Free"
                      << std::endl;
        }
        return 0;
    }
    

    Ref-cuda-smi for mac

    【讨论】:

    • 虽然这些链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接的答案可能会失效。
    • 请添加更多详细信息以扩展您的答案,例如工作代码或文档引用。
    猜你喜欢
    • 2012-06-15
    • 1970-01-01
    • 2019-10-30
    • 2012-04-03
    • 2011-04-27
    • 2010-10-03
    • 1970-01-01
    • 1970-01-01
    • 2018-06-20
    相关资源
    最近更新 更多