这是WinAPI方式,它使用GetLogicalProcessorInformation http://msdn.microsoft.com/en-us/library/windows/desktop/ms683194%28v=vs.85%29.aspx功能。如果您至少有 Delphi XE2,则不需要以下定义,它们已经在Windows
unit:
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;