'CPU'에 해당되는 글 1건

  1. 2011.10.31 코어수 알아내기
2011. 10. 31. 17:30

프로세서 코어수를 알아내는 방법

GetLogicalProcessorInformation() 함수를 이용


#include <windows.h>
#include <malloc.h>    
#include <stdio.h>
#include <tchar.h>

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;
}


int getProcessorNumber(int *core, int *logical){
	typedef BOOL (WINAPI *LPFN_GLPI)(PSYSTEM_LOGICAL_PROCESSOR_INFORMATION, PDWORD);
	LPFN_GLPI glpi;

	DWORD returnLength = 0;
	DWORD logicalProcessorCount = 0;
	DWORD numaNodeCount = 0;
	DWORD processorCoreCount = 0;
	DWORD processorL1CacheCount = 0;
	DWORD processorL2CacheCount = 0;
	DWORD processorL3CacheCount = 0;
	DWORD processorPackageCount = 0;
	DWORD byteOffset = 0;
	PCACHE_DESCRIPTOR Cache;

	PSYSTEM_LOGICAL_PROCESSOR_INFORMATION buffer = NULL;
	PSYSTEM_LOGICAL_PROCESSOR_INFORMATION ptr = NULL;

	*core = *logical = 0;

	glpi = (LPFN_GLPI) GetProcAddress( GetModuleHandle("kernel32"),"GetLogicalProcessorInformation");
	if (NULL == glpi) {
		printf("\nGetLogicalProcessorInformation is not supported.\n");
		return (0);
	}

	DWORD rc = glpi(buffer, &returnLength);
	if (FALSE == rc) {
		if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
			if (buffer) 
				free(buffer);

			buffer = (PSYSTEM_LOGICAL_PROCESSOR_INFORMATION)malloc(returnLength);
		}
		rc = glpi(buffer, &returnLength);
	}
	if (FALSE == rc) {
		printf("\nError: GetLogicalProcessorInformation() returned false\n");
		return (0);
	}

	ptr = buffer;
	while( byteOffset < returnLength ){
		switch (ptr->Relationship) 
		{
		case RelationNumaNode:
			numaNodeCount++;
			break;

		case RelationProcessorCore:
			processorCoreCount++;
			logicalProcessorCount += CountSetBits(ptr->ProcessorMask);
			break;

		case RelationCache:
			Cache = &ptr->Cache;
			if (Cache->Level == 1) {
				processorL1CacheCount++;
			}
			else if (Cache->Level == 2) {
				processorL2CacheCount++;
			}
			else if (Cache->Level == 3) {
				processorL3CacheCount++;
			}
			break;

		case RelationProcessorPackage:
			processorPackageCount++;
			break;

		default:
			tprintf("\nError: Unsupported LOGICAL_PROCESSOR_RELATIONSHIP value.\n");
			break;
		}
		byteOffset += sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION);
		ptr++;
	}
	free(buffer);
	*core = processorCoreCount;
	*logical = logicalProcessorCount;
	return 1;
}

int main ()
{
	int core, logical;
	getProcessorNumber(&core, &logical);
	printf("Processor Core : %d\n", core);
	printf("Logical Processor : %d\n", logical);

	return 0;
}


소스파일



실행 결과



참고자료

http://msdn.microsoft.com/en-us/library/windows/desktop/ms686694(v=vs.85).aspx
http://msdn.microsoft.com/en-us/library/ms683194%28VS.85%29.aspx
http://msdn.microsoft.com/en-us/library/system.environment.processorcount.aspx

'windows' 카테고리의 다른 글

_CrtIsValidHeapPointer() 관련 오류 발생시 해결방법  (0) 2011.11.01
컴파일 옵션 종류  (0) 2011.10.31
Posted by Нуеоп
이전버튼 1 이전버튼