gpt4 book ai didi

delphi - SetProcessAffinityMask - 选择多个处理器?

转载 作者:行者123 更新时间:2023-12-03 14:45:16 24 4
gpt4 key购买 nike

如何使用 SetProcessAffinityMask 选择多个逻辑处理器?

在 Windows 任务管理器中,您可以执行此操作作为示例:

enter image description here

我更新了我的CreateProcess执行此操作的程序:

type
TProcessPriority = (ptLow = $00000040,
ptBelowNormal = $00004000,
ptNormal = $00000020,
ptAboveNormal = $00008000,
ptHigh = $00000080,
ptRealtime = $00000100);

procedure RunProcess(FileName: string; Priority: TProcessPriority);
var
StartInfo: TStartupInfo;
ProcInfo: TProcessInformation;
CmdLine: string;
Done: Boolean;
begin
FillChar(StartInfo, SizeOf(TStartupInfo), #0);
FillChar(ProcInfo, SizeOf(TProcessInformation), #0);
StartInfo.cb := SizeOf(TStartupInfo);

CmdLine := FileName;
UniqueString(CmdLine);
try
Done := CreateProcess(nil, PChar(CmdLine), nil, nil, False,
CREATE_NEW_PROCESS_GROUP + Integer(Priority),
nil, nil, StartInfo, ProcInfo);
if Done then
begin
// Todo: Get actual cpu core count before attempting to set affinity!
// 0 = <All Processors>
// 1 = CPU 0
// 2 = CPU 1
// 3 = CPU 2
// 4 = CPU 3
// 5 = CPU 5
// 6 = CPU 6
// 7 = CPU 6
// 8 = CPU 7

// this sets to CPU 0 - but how to allow multiple parameters to
// set more than one logical processor?
SetProcessAffinityMask(ProcInfo.hProcess, 1);
end else
MessageDlg('Could not run ' + FileName, mtError, [mbOk], 0)
finally
CloseHandle(ProcInfo.hProcess);
CloseHandle(ProcInfo.hThread);
end;
end;

请注意我在那里发表的评论。最好更新我的过程以包含一个新的 Affinity 参数,我可以将其传递给 SetProcessAffinityMask。

由于显而易见的原因,调用其中任何一个都不会选择相应的处理器,它们给出了我想要做什么的想法:

SetProcessAffinityMask(ProcInfo.hProcess, 1 + 2); 
SetProcessAffinityMask(ProcInfo.hProcess, 1 and 2);

例如,为进程选择任意 CPU,如任务管理器中所示。

我应该如何使用数组、集合或其他东西来做到这一点?我无法让它使用多个值。

谢谢。

最佳答案

它是一个位掩码,如 documentation 中所述。 .

A process affinity mask is a bit vector in which each bit represents a logical processor on which the threads of the process are allowed to run.

  • 处理器 0 是 $01。
  • 处理器 1 的价格为 02 美元。
  • 处理器 2 售价 04 美元。
  • 处理器 3 售价 08 美元。
  • 处理器 4 售价 10 美元。

等等。您可以使用逻辑来组合它们。因此处理器 0 和 1 将是 $01$02,等于 $03

我将使用移位运算符 shl 为特定处理器创建值。像这样:

function SingleProcessorMask(const ProcessorIndex: Integer): DWORD_PTR;
begin
//When shifting constants the compiler will force the result to be 32-bit
//if you have more than 32 processors, `Result:= 1 shl x` will return
//an incorrect result.
Result := DWORD_PTR(1) shl (ProcessorIndex);
end;

您可以轻松地扩展它,以便在循环中使用逻辑为处理器列表生成掩码。

function CombinedProcessorMask(const Processors: array of Integer): DWORD_PTR;
var
i: Integer;
begin
Result := 0;
for i := low(Processors) to high(Processors) do
Result := Result or SingleProcessorMask(Processors[i]);
end;

您可以测试处理器是否处于位掩码中,如下所示:

function ProcessorInMask(const ProcessorMask: DWORD_PTR; 
const ProcessorIndex: Integer): Boolean;
begin
Result := (SingleProcessorMask(ProcessorIndex) and ProcessorMask)<>0;
end;

注意:我使用 DWORD_PTR 因为对于 64 位目标,位掩码是 64 位宽。这种细微差别对于 XE 上的您来说并不重要,但值得正确对待它以使将来的代码移植更容易。

关于delphi - SetProcessAffinityMask - 选择多个处理器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9078838/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com