add CpuInfo
This commit is contained in:
parent
f5117f0a84
commit
12590b9c64
25
inc/hgl/platform/CpuInfo.h
Normal file
25
inc/hgl/platform/CpuInfo.h
Normal file
@ -0,0 +1,25 @@
|
||||
#ifndef HGL_PLATFORM_CPU_INFO_INCLUDE
|
||||
#define HGL_PLATFORM_CPU_INFO_INCLUDE
|
||||
|
||||
#include<hgl/type/DataType.h>
|
||||
namespace hgl
|
||||
{
|
||||
enum class CpuArch
|
||||
{
|
||||
x86_64,
|
||||
ARMv8,
|
||||
};//
|
||||
|
||||
struct CpuInfo
|
||||
{
|
||||
uint cpu_count; ///<处理器数量
|
||||
uint core_count; ///<处理器核心数量
|
||||
uint logical_core_count; ///<逻辑处理器核心数量
|
||||
};//struct CpuInfo
|
||||
|
||||
/**
|
||||
* 取得CPU信息
|
||||
*/
|
||||
bool GetCpuInfo(CpuInfo *);
|
||||
}//namespace hgl
|
||||
#endif//HGL_PLATFORM_CPU_INFO_INCLUDE
|
@ -1,4 +1,6 @@
|
||||
IF(UNIX)
|
||||
SET(CPU_INFO_HEADER ${CMPLATFORM_ROOT_INCLUDE_PATH}/hgl/platform/CpuInfo.h)
|
||||
|
||||
IF(UNIX)
|
||||
SET(PLATFORM_FILE_SOURCE UNIX/File.cpp
|
||||
UNIX/FileAccess.cpp
|
||||
UNIX/EnumFile.cpp)
|
||||
@ -63,6 +65,8 @@
|
||||
ENDIF()
|
||||
|
||||
IF(WIN32)
|
||||
set(CPU_INFO_SOURCE Win/CpuInfo.cpp)
|
||||
|
||||
SET(PLATFORM_FILE_SOURCE Win/File.cpp
|
||||
Win/FileAccess.cpp
|
||||
Win/EnumFile.cpp
|
||||
@ -114,6 +118,8 @@ SOURCE_GROUP("Time" FILES ${PLATFORM_TIME_SOURCE})
|
||||
SOURCE_GROUP("Desktop" FILES ${PLATFORM_DESKTOP_SOURCE})
|
||||
SOURCE_GROUP("Window" FILES ${PLATFORM_WINDOW_SOURCE})
|
||||
|
||||
SOURCE_GROUP("Hardware\\Cpuinfo" FILES ${CPU_INFO_HEADER} ${CPU_INFO_SOURCE})
|
||||
|
||||
SET(PLATFORM_SOURCE ${PLATFORM_SOURCE}
|
||||
${PLATFORM_FILE_SOURCE}
|
||||
${PLATFORM_CODEPAGE_SOURCE}
|
||||
@ -126,6 +132,9 @@ SET(PLATFORM_SOURCE ${PLATFORM_SOURCE}
|
||||
${PLATFORM_EXTERNAL_MODULE_SOURCE}
|
||||
${PLATFORM_INPUT_DEVICE_SOURCE}
|
||||
${PLATFORM_WINDOW_SOURCE}
|
||||
|
||||
${CPU_INFO_HEADER}
|
||||
${CPU_INFO_SOURCE}
|
||||
)
|
||||
|
||||
add_cm_library(CMPlatform "CM" ${PLATFORM_SOURCE})
|
||||
|
70
src/Win/CpuInfo.cpp
Normal file
70
src/Win/CpuInfo.cpp
Normal file
@ -0,0 +1,70 @@
|
||||
#include<hgl/platform/CpuInfo.h>
|
||||
#include<hgl/type/StrChar.h>
|
||||
#include<sysinfoapi.h>
|
||||
#include<string.h>
|
||||
#include<iostream>
|
||||
|
||||
namespace hgl
|
||||
{
|
||||
namespace
|
||||
{
|
||||
DWORD CountSetBits(ULONG_PTR bitMask)
|
||||
{
|
||||
DWORD LSHIFT = sizeof(ULONG_PTR)*8 - 1;
|
||||
DWORD bitSetCount = 0;
|
||||
ULONG_PTR bitTest = (ULONG_PTR)1 << LSHIFT;
|
||||
DWORD i;
|
||||
|
||||
for (i = 0; i <= LSHIFT; ++i)
|
||||
{
|
||||
bitSetCount += ((bitMask & bitTest)?1:0);
|
||||
bitTest/=2;
|
||||
}
|
||||
|
||||
return bitSetCount;
|
||||
}
|
||||
}//namespace
|
||||
|
||||
bool GetCpuInfo(CpuInfo *ci)
|
||||
{
|
||||
if(!ci)return(false);
|
||||
|
||||
AutoFree<SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX> buf;
|
||||
DWORD length=0;
|
||||
|
||||
GetLogicalProcessorInformationEx(RelationAll,nullptr,&length);
|
||||
|
||||
if(GetLastError()!=ERROR_INSUFFICIENT_BUFFER)
|
||||
return(false);
|
||||
|
||||
buf=(SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX *)malloc(length);
|
||||
|
||||
if(!GetLogicalProcessorInformationEx(RelationAll,buf,&length))
|
||||
return(false);
|
||||
|
||||
char *sp=(char *)(buf.data());
|
||||
SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX *p;
|
||||
|
||||
memset(ci,0,sizeof(CpuInfo));
|
||||
|
||||
while(length>0)
|
||||
{
|
||||
p=(SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX *)sp;
|
||||
|
||||
if(p->Relationship==RelationProcessorPackage)
|
||||
++ci->cpu_count;
|
||||
else
|
||||
if(p->Relationship==RelationProcessorCore)
|
||||
{
|
||||
++ci->core_count;
|
||||
|
||||
ci->logical_core_count+=CountSetBits(p->Processor.GroupMask[0].Mask);
|
||||
}
|
||||
|
||||
sp+=p->Size;
|
||||
length-=p->Size;
|
||||
}
|
||||
|
||||
return(true);
|
||||
}
|
||||
}//namespace hgl
|
Loading…
x
Reference in New Issue
Block a user