Показать сообщение отдельно

Определяет загрузку ЦП в % для каждого ядра
  #6  
Старый 11.12.2009, 20:07
Gar|k
Постоянный
Регистрация: 20.03.2009
Сообщений: 564
Провел на форуме:
991929

Репутация: 395


По умолчанию Определяет загрузку ЦП в % для каждого ядра

Немного не в тему но может кому пригодится.

Код:
//---------

#include "stdio.h"
#include "windows.h"

//---------

typedef DWORD (__stdcall *LPFN_NtQuerySystemInformation)(DWORD, PVOID, DWORD, PDWORD);

#define SystemProcessorPerformanceInformation 0x8

//---------

#pragma pack(push,8)

typedef struct _SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION {
    LARGE_INTEGER IdleTime;
    LARGE_INTEGER KernelTime;
    LARGE_INTEGER UserTime;
    LARGE_INTEGER DpcTime;
    LARGE_INTEGER InterruptTime;
    ULONG InterruptCount;
} SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION,
*PSYSTEM_PROCESSOR_PERFORMANCE_INFORMATION;

#pragma pack(pop)

//---------

int main(int argc, char* argv[]) {

	SYSTEM_INFO   systeminfo;
	unsigned long bytesreturned;

	SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION spi_old[32];
	memset(spi_old,0,sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION)*32);

	SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION spi[32];
	memset(spi,0,sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION)*32);

	LPFN_NtQuerySystemInformation ntquerysysteminformation =(LPFN_NtQuerySystemInformation) GetProcAddress(GetModuleHandle("ntdll.dll"), "NtQuerySystemInformation");

	if(!ntquerysysteminformation) {

	  printf("\n*** no ntquerysysteminformation api?.. bugger");
	  return -1;
	}

	GetSystemInfo(&systeminfo);

	printf("\n[i] Processor Count: %d\n",systeminfo.dwNumberOfProcessors);

	ntquerysysteminformation(SystemProcessorPerformanceInformation,spi_old, (sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION)*systeminfo.dwNumberOfProcessors),&bytesreturned);

	Sleep(500);

	ntquerysysteminformation(SystemProcessorPerformanceInformation,spi, (sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION)*systeminfo.dwNumberOfProcessors),&bytesreturned);

	for(int cpuloopcount = 0; cpuloopcount < systeminfo.dwNumberOfProcessors; cpuloopcount++) {

		BYTE cpuusage = (BYTE) (100 - (((spi[cpuloopcount].IdleTime.QuadPart - spi_old[cpuloopcount].IdleTime.QuadPart) * 100) /  \
		((spi[cpuloopcount].KernelTime.QuadPart +  spi[cpuloopcount].UserTime.QuadPart) - (spi_old[cpuloopcount].KernelTime.QuadPart + spi_old[cpuloopcount].UserTime.QuadPart))));

		printf("\n[i] CPU %02d: = %0d%%",cpuloopcount, cpuusage);

	}
  return 0;
}
 
Ответить с цитированием