gpt4 book ai didi

delphi - 如何获取CPU一级缓存(主缓存)信息?

转载 作者:行者123 更新时间:2023-12-03 15:38:59 25 4
gpt4 key购买 nike

我尝试使用 WMI 获取 CPU 缓存信息,效果很好,但仅适用于 2 级和 3 级缓存,所以我的问题是,如何获取 CPU 1 级缓存信息?

最佳答案

这里是WinAPI方式,使用GetLogicalProcessorInformation功能。如果您至少有 Delphi XE2,则不需要以下定义,它们已经在 Windows 单元中:

type
TLogicalProcessorRelationship = (
RelationProcessorCore = 0,
RelationNumaNode = 1,
RelationCache = 2,
RelationProcessorPackage = 3,
RelationGroup = 4,
RelationAll = $FFFF
);
TProcessorCacheType = (
CacheUnified,
CacheInstruction,
CacheData,
CacheTrace
);
TCacheDescriptor = record
Level: Byte;
Associativity: Byte;
LineSize: Word;
Size: DWORD;
pcType: TProcessorCacheType;
end;
PSystemLogicalProcessorInformation = ^TSystemLogicalProcessorInformation;
TSystemLogicalProcessorInformation = record
ProcessorMask: ULONG_PTR;
Relationship: TLogicalProcessorRelationship;
case Integer of
0: (Flags: Byte);
1: (NodeNumber: DWORD);
2: (Cache: TCacheDescriptor);
3: (Reserved: array [0..1] of ULONGLONG);
end;

function GetLogicalProcessorInformation(
Buffer: PSystemLogicalProcessorInformation;
var ReturnLength: DWORD): BOOL; stdcall;
external kernel32 name 'GetLogicalProcessorInformation';

以及如何显示所有 1 级缓存条目的缓存类型、级别和大小的示例:

procedure TForm1.Button1Click(Sender: TObject);
var
S: string;
I: Integer;
ReturnLength: DWORD;
Buffer: array of TSystemLogicalProcessorInformation;
begin
SetLength(Buffer, 1);
if not GetLogicalProcessorInformation(@Buffer[0], ReturnLength) then
begin
if GetLastError = ERROR_INSUFFICIENT_BUFFER then
begin
SetLength(Buffer,
ReturnLength div SizeOf(TSystemLogicalProcessorInformation) + 1);
if not GetLogicalProcessorInformation(@Buffer[0], ReturnLength) then
RaiseLastOSError;
end
else
RaiseLastOSError;
end;

for I := 0 to High(Buffer) do
begin
if (Buffer[I].Relationship = RelationCache) and
(Buffer[I].Cache.Level = 1) then
begin
S := 'Type: ';
case Buffer[I].Cache.pcType of
CacheUnified: S := S + 'Unified cache';
CacheInstruction: S := S + 'Instruction cache';
CacheData: S := S + 'Data cache';
CacheTrace: S := S + 'Trace cache';
end;
S := S + sLineBreak;
S := S + 'Level: ' + IntToStr(Buffer[I].Cache.Level) + sLineBreak;
S := S + 'Size: ' + IntToStr(Buffer[I].Cache.Size) + ' B';
ShowMessage(S);
end;
end;
end;

关于delphi - 如何获取CPU一级缓存(主缓存)信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13788715/

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