Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine the hardware (CPU and RAM) on a machine?

I'm working on a cross platform profiling suite, and would like to add information about the machine's CPU (architecture/clock speed/cores) and RAM(total) to the report of each run. Currently I need to target Windows and Unix, so I need methods to obtain this information from both platforms, any clues?

Edit: Thanks for the great answers, Now I got CPU architecture, CPU number of cores and total Memory, but I'm still lacking a clockspeed for the CPU any ideas for that one?

like image 811
Robert Gould Avatar asked May 12 '09 01:05

Robert Gould


People also ask

How do I find my CPU and RAM?

Press Ctrl + Shift + Esc to launch Task Manager. Or, right-click the Taskbar and select Task Manager. Select the Performance tab and click Memory in the left panel. The Memory window lets you see your current RAM usage, check RAM speed, and view other memory hardware specifications.


3 Answers

Here is one method for getting the information you want on a Windows machine. I copied and pasted it from an actual project with some minor modifications, so feel free to clean it up to make more sense.

        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;

For more information, see GetSystemInfo, GlobalMemoryStatusEx and __cpuid. Although I didn't include it, you can also determine if the OS is 32 or 64 bit via the GetSystemInfo function.

like image 124
bsruth Avatar answered Oct 20 '22 18:10

bsruth


On Windows you can use GlobalMemoryStatusEx to get the amount of actual RAM.

Processor information can be obtained via GetSystemInfo.

like image 43
i_am_jorf Avatar answered Oct 20 '22 20:10

i_am_jorf


For Linux with GCC you can use a very similar solution like windows. You need to include the <cpuid.h> and you need to modify the input for the __cpuid() method based on 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;
like image 8
Mate Zabo Avatar answered Oct 20 '22 18:10

Mate Zabo