如何确定机器上的硬件(CPU和RAM)?

Rob*_*uld 15 c c++ memory hardware cpu

我正在研究跨平台分析套件,并希望在每次运行的报告中添加有关机器CPU(架构/时钟速度/内核)和RAM(总数)的信息.目前我需要针对Windows和Unix,所以我需要从两个平台获取此信息的方法,任何线索?

编辑:谢谢你的答案,现在我得到了CPU体系结构,核心CPU数量和总内存,但我仍然缺乏CPU的时钟速度任何想法?

i_a*_*orf 10

在Windows上,您可以使用GlobalMemoryStatusEx来获取实际RAM的数量.

处理器信息可以通过GetSystemInfo获得.


小智 10

我来晚了,但这是我的贡献。我尝试采用更现代的 C++ 方法。

    #include <intrin.h> // NOTE this header is MSVC specific!
    #include <string>
    #include <array>

    std::string GetCpuInfo()
    {
        // 4 is essentially hardcoded due to the __cpuid function requirements.
        // NOTE: Results are limited to whatever the sizeof(int) * 4 is...
        std::array<int, 4> integerBuffer = {};
        constexpr size_t sizeofIntegerBuffer = sizeof(int) * integerBuffer.size();

        std::array<char, 64> charBuffer = {};

        // The information you wanna query __cpuid for.
        // https://learn.microsoft.com/en-us/cpp/intrinsics/cpuid-cpuidex?view=vs-2019
        constexpr std::array<int, 3> functionIds = {
            // Manufacturer
            //  EX: "Intel(R) Core(TM"
            0x8000'0002,
            // Model
            //  EX: ") i7-8700K CPU @"
            0x8000'0003,
            // Clockspeed
            //  EX: " 3.70GHz"
            0x8000'0004
        };

        std::string cpu;

        for (int id : functionIds)
        {
            // Get the data for the current ID.
            __cpuid(integerBuffer.data(), id);
            
            // Copy the raw data from the integer buffer into the character buffer
            std::memcpy(charBuffer.data(), integerBuffer.data(), sizeofIntegerBuffer);

            // Copy that data into a std::string
            cpu += std::string(charBuffer.data());
        }

        return cpu;
    }
Run Code Online (Sandbox Code Playgroud)

以下是我自己执行该函数的结果:“Intel(R) Core(TM) i7-8700K CPU @ 3.70GHz”

老实说,像这样的东西还没有标准化真的很烦人......

  • 正在寻找方法来获取 MSInfo 中显示的 CPU 信息,但我找不到方法。我在 30 秒内完成了复制、粘贴和工作。这个功能很漂亮,谢谢!! (2认同)

bsr*_*uth 7

以下是在Windows计算机上获取所需信息的一种方法.我从一个实际的项目中复制并粘贴了一些微小的修改,所以请随意清理它以使其更有意义.

        int CPUInfo[4] = {-1};
        unsigned   nExIds, i =  0;
        char CPUBrandString[0x40];
        // Get the information associated with each extended ID.
        __cpuid(CPUInfo, 0x80000000);
        nExIds = CPUInfo[0];
        for (i=0x80000000; i<=nExIds; ++i)
        {
            __cpuid(CPUInfo, i);
            // Interpret CPU brand string
            if  (i == 0x80000002)
                memcpy(CPUBrandString, CPUInfo, sizeof(CPUInfo));
            else if  (i == 0x80000003)
                memcpy(CPUBrandString + 16, CPUInfo, sizeof(CPUInfo));
            else if  (i == 0x80000004)
                memcpy(CPUBrandString + 32, CPUInfo, sizeof(CPUInfo));
        }
        //string includes manufacturer, model and clockspeed
        cout << "CPU Type: " << CPUBrandString << endl;


        SYSTEM_INFO sysInfo;
        GetSystemInfo(&sysInfo);
        cout << "Number of Cores: " << sysInfo.dwNumberOfProcessors << endl;

        MEMORYSTATUSEX statex;
        statex.dwLength = sizeof (statex);
        GlobalMemoryStatusEx(&statex);
        cout << "Total System Memory: " << (statex.ullTotalPhys/1024)/1024 << "MB" << endl;
Run Code Online (Sandbox Code Playgroud)

有关更多信息,请参阅GetSystemInfo,GlobalMemoryStatusEx__cpuid.虽然我没有包含它,但您还可以通过GetSystemInfo函数确定操作系统是32位还是64位.


Rob*_*uld 6

在Windows上确定CPU时钟速度:

double CPUSpeed()
{
    wchar_t Buffer[_MAX_PATH];
    DWORD BufSize = _MAX_PATH;
    DWORD dwMHz = _MAX_PATH;
    HKEY hKey;

    // open the key where the proc speed is hidden:
    long lError = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
                                L"HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0",
                                0,
                                KEY_READ,
                                &hKey);
    if(lError != ERROR_SUCCESS)
    {// if the key is not found, tell the user why:
        FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
                        NULL,
                        lError,
                        0,
                        Buffer,
                        _MAX_PATH,
                        0);
        wprintf(Buffer);
        return 0;
    }

    // query the key:
    RegQueryValueEx(hKey, L"~MHz", NULL, NULL, (LPBYTE) &dwMHz, &BufSize);
    return (double)dwMHz;
}
Run Code Online (Sandbox Code Playgroud)


Chr*_*ung 5

CPU很简单。使用cpuid说明。我将留下其他海报来寻找一种可移植的方法来确定系统有多少RAM。:-)

对于特定于Linux的方法,您可以访问/proc/meminfo(和/proc/cpuinfo,如果不麻烦解析cpuid响应的话)。


ara*_*nid 5

在 Linux 上,您可以解析 /proc/cpuinfo(包含有关每个处理器的信息块)和 /proc/meminfo(包含各种常规内存统计信息,包括 MemTotal)。


Mat*_*abo 5

对于带有GCC的Linux,您可以使用Windows之类的非常相似的解决方案。您需要包含,<cpuid.h>并且需要__cpuid()基于this来修改方法的输入。

#include <cpuid.h>

char CPUBrandString[0x40];
unsigned int CPUInfo[4] = {0,0,0,0};

__cpuid(0x80000000, CPUInfo[0], CPUInfo[1], CPUInfo[2], CPUInfo[3]);
unsigned int nExIds = CPUInfo[0];

memset(CPUBrandString, 0, sizeof(CPUBrandString));

for (unsigned int i = 0x80000000; i <= nExIds; ++i)
{
    __cpuid(i, CPUInfo[0], CPUInfo[1], CPUInfo[2], CPUInfo[3]);

    if (i == 0x80000002)
        memcpy(CPUBrandString, CPUInfo, sizeof(CPUInfo));
    else if (i == 0x80000003)
        memcpy(CPUBrandString + 16, CPUInfo, sizeof(CPUInfo));
    else if (i == 0x80000004)
        memcpy(CPUBrandString + 32, CPUInfo, sizeof(CPUInfo));
}

cout << "CPU Type: " << CPUBrandString << endl;
Run Code Online (Sandbox Code Playgroud)