How to programmatically get the CPU cache page size in C++?

后端 未结 7 1567
渐次进展
渐次进展 2020-12-13 04:49

I\'d like my program to read the cache line size of the CPU it\'s running on in C++.

I know that this can\'t be done portably, so I will need a solution for Linux an

相关标签:
7条回答
  • 2020-12-13 05:25

    On Windows

    #include <Windows.h>
    #include <iostream>
    
    using std::cout; using std::endl;
    
    int main()
    {
        SYSTEM_INFO systemInfo;
        GetSystemInfo(&systemInfo);
        cout << "Page Size Is: " << systemInfo.dwPageSize;
        getchar();
    }
    

    On Linux

    http://linux.die.net/man/2/getpagesize

    0 讨论(0)
  • 2020-12-13 05:29

    Looks like at least SCO unix (http://uw714doc.sco.com/en/man/html.3C/sysconf.3C.html) has _SC_CACHE_LINE for sysconf. Perhaps other platforms have something similar?

    0 讨论(0)
  • 2020-12-13 05:44

    Here is sample code for those who wonder how to to utilize the function in accepted answer:

    #include <new>
    #include <iostream>
    #include <Windows.h>
    
    
    void ShowCacheSize()
    {
        using CPUInfo = SYSTEM_LOGICAL_PROCESSOR_INFORMATION;
        DWORD len = 0;
        CPUInfo* buffer = nullptr;
    
        // Determine required length of a buffer
        if ((GetLogicalProcessorInformation(buffer, &len) == FALSE) && (GetLastError() == ERROR_INSUFFICIENT_BUFFER))
        {
            // Allocate buffer of required size
            buffer = new (std::nothrow) CPUInfo[len]{ };
    
            if (buffer == nullptr)
            {
                std::cout << "Buffer allocation of " << len << " bytes failed" << std::endl;
            }
            else if (GetLogicalProcessorInformation(buffer, &len) != FALSE)
            {
                for (DWORD i = 0; i < len; ++i)
                {
                    // This will be true for multiple returned caches, we need just one
                    if (buffer[i].Relationship == RelationCache)
                    {
                        std::cout << "Cache line size is: " << buffer[i].Cache.LineSize << " bytes" << std::endl;
                        break;
                    }
                }
            }
            else
            {
                std::cout << "ERROR: " << GetLastError() << std::endl;
            }
    
            delete[] buffer;
        }
    }
    
    0 讨论(0)
  • 2020-12-13 05:45

    I think you need NtQuerySystemInformation from ntdll.dll.

    0 讨论(0)
  • 2020-12-13 05:47

    On Win32, GetLogicalProcessorInformation will give you back a SYSTEM_LOGICAL_PROCESSOR_INFORMATION which contains a CACHE_DESCRIPTOR, which has the information you need.

    0 讨论(0)
  • 2020-12-13 05:49

    For x86, the CPUID instruction. A quick google search reveals some libraries for win32 and c++. I have used CPUID via inline assembler as well.

    Some more info:

    • http://www.intel.com/Assets/PDF/appnote/241618.pdf
    • http://softpixel.com/~cwright/programming/simd/cpuid.php
    0 讨论(0)
提交回复
热议问题