windows 如何以编程方式获取 C++ 中的 CPU 缓存页面大小?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/150294/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 11:20:12  来源:igfitidea点击:

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

c++windowslinuxcpu

提问by Mathieu Pagé

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

我希望我的程序读取它在 C++ 中运行的 CPU 的缓存行大小。

I know that this can't be done portably, so I will need a solution for Linux and another for Windows (Solutions for other systems could be usefull to others, so post them if you know them).

我知道这不能移植,所以我需要一个适用于 Linux 的解决方案和另一个适用于 Windows 的解决方案(其他系统的解决方案可能对其他人有用,所以如果你知道他们,请发布它们)。

For Linux I could read the content of /proc/cpuinfo and parse the line begining with cache_alignment. Maybe there is a better way involving a call to an API.

对于 Linux,我可以读取 /proc/cpuinfo 的内容并解析以 cache_alignment 开头的行。也许有更好的方法涉及调用 API。

For Windows I simply have no idea.

对于 Windows,我根本不知道。

采纳答案by Nick

On Win32, GetLogicalProcessorInformationwill give you back a SYSTEM_LOGICAL_PROCESSOR_INFORMATIONwhich contains a CACHE_DESCRIPTOR, which has the information you need.

在 Win32 上,GetLogicalProcessorInformation将返回一个SYSTEM_LOGICAL_PROCESSOR_INFORMATION包含 的CACHE_DESCRIPTOR,其中包含您需要的信息。

回答by PiedPiper

On Linux try the proccpuinfo library, an architecture independent C API for reading /proc/cpuinfo

在 Linux 上尝试使用proccpuinfo 库,这是一个独立于体系结构的 C API,用于读取 /proc/cpuinfo

回答by robottobor

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

对于 x86,CPUID指令。快速谷歌搜索显示了一些适用于 win32 和 c++ 的。我也通过内联汇编器使用了 CPUID。

Some more info:

更多信息:

回答by Researcher

On Windows

在 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

在 Linux 上

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

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

回答by Evan Teran

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?

看起来至少 SCO unix ( http://uw714doc.sco.com/en/man/html.3C/sysconf.3C.html) 有 _SC_CACHE_LINE 用于 sysconf。也许其他平台也有类似的东西?

回答by cubex

I think you need NtQuerySystemInformationfrom ntdll.dll.

我认为你需要NtQuerySystemInformationntdll.dll.

回答by metablaster

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;
    }
}