3

当我调用 CreateProcess 并使用 PROC_THREAD_ATTRIBUTE_GROUP_AFFINITY 扩展属性时,我不断收到错误 87,ERROR_INVALID_PARAMETERS。我使用完全相同的代码调用 CreateRemoteThreadEx,效果很好。此外, PROC_THREAD_ATTRIBUTE_PREFERRED_NODE 似乎没有效果。那我做错什么了!?

Microsoft Windows Server 2008 R2 企业版,6.1.7601 SP1 Build 7601

我什至安装了这个服务包:在 Windows 7 或 Windows Server 2008 R2 中,无法通过调用使用 PROC_THREAD_ATTRIBUTE_PREFERRED_NODE 参数的 CreateProcess 函数来创建子进程

这是示例代码:

#include <windows.h>

typedef unsigned __int64 QWORD;

class CErr {
public:
    CErr(LPCSTR szFunc, DWORD nErr) {
        char szBuf[0x10000];
        DWORD fFlags = FORMAT_MESSAGE_IGNORE_INSERTS|FORMAT_MESSAGE_FROM_SYSTEM;
        DWORD fLang =  MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT);
        if (!nErr)
            nErr = GetLastError();
        FormatMessage(fFlags, NULL, nErr, fLang, szBuf, sizeof(szBuf) - 1, NULL);
        printf("%s: %s", szFunc, szBuf);
    }
};

int main(int argc, char* argv[])
{
    DWORD nErr;
    size_t cb;
    char sAttribsBuf[4096];
    auto pAttribs = (PPROC_THREAD_ATTRIBUTE_LIST)sAttribsBuf;

    if (!InitializeProcThreadAttributeList(NULL, 1, 0, &cb)
     && ((nErr = GetLastError()) != ERROR_INSUFFICIENT_BUFFER)) 
        throw CErr("InitializeProcThreadAttributeList", nErr);
    if (!InitializeProcThreadAttributeList(pAttribs, 1, 0, &cb))
        throw CErr("InitializeProcThreadAttributeList", 0);

#if 1 // if enabled, CreateProcess succeeds, but doesn't set affinity 

    WORD iNuma = 1;  // WORD is the only size that does not error here
    if (!UpdateProcThreadAttribute(pAttribs, 0, PROC_THREAD_ATTRIBUTE_PREFERRED_NODE,
     &iNuma, sizeof(iNuma), NULL, NULL))
        throw CErr("UpdateProcThreadAttribute", 0);

#else // if enabled, CreateProcess fails with ERROR_INVALID_PARAMETERS(87)

    GROUP_AFFINITY GrpAffinity = { 0 };
    GrpAffinity.Mask = 1;
    if (!UpdateProcThreadAttribute(pAttribs, 0, PROC_THREAD_ATTRIBUTE_GROUP_AFFINITY, 
     &GrpAffinity, sizeof(GrpAffinity), NULL, NULL))
        throw CErr("UpdateProcThreadAttribute", 0);

#endif

    auto fCreationFlags = EXTENDED_STARTUPINFO_PRESENT;
    PROCESS_INFORMATION pi = { 0 };                     
    STARTUPINFOEX si = { 0 };
    si.StartupInfo.cb = sizeof(si);
    si.lpAttributeList = pAttribs;
    if (!CreateProcess(NULL, "notepad.exe", NULL, NULL, false, fCreationFlags, 
     NULL, NULL, &si.StartupInfo, &pi))
        throw CErr("CreateProcess", 0);  // error if ...ATTRIBUTE_GROUP_AFFINITY

    // SetProcessAffinityMask(pi.hProcess,1);  // if enabled, notepad's affinity is set

    WaitForSingleObject(pi.hProcess, INFINITE);

    DeleteProcThreadAttributeList(pAttribs);

    return 0;
}
4

1 回答 1

3

从文档中不清楚,但我想我明白了。PROC_THREAD_ATTRIBUTE_PREFERRED_NODE 只应该与 CreateProcess() 一起使用。PROC_THREAD_ATTRIBUTE_IDEAL_PROCESSOR 和 PROC_THREAD_ATTRIBUTE_GROUP_AFFINITY 只应该与 CreateThread() 一起使用。

PROC_THREAD_ATTRIBUTE_PREFERRED_NODE 可能正在将进程的亲和性设置为与节点相同的 GROUP 中的所有处理器。我无法验证它,因为我的测试系统在两个 numa 节点上只有 12 个内核。将 PROC_THREAD_ATTRIBUTE_PREFERRED_NODE 设置为 0 或 1 会将亲和性设置为所有核心。我确实验证了 CreateProcess 创建的进程堆栈位于 PROC_THREAD_ATTRIBUTE_PREFERRED_NODE 指示的 numa 节点上。也没有记录,传入的节点的大小必须是 2 个字节。

于 2013-09-07T03:26:48.257 回答