//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "perfgrap"
#pragma resource "*.dfm"
#define SystemProcessorTimes 8
#define MAX_PROCESSORS 32
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
typedef struct _SYSTEM_BASIC_INFORMATION
{
ULONG Unknown,
MaximumIncrement,
PhysicalPageSize,
NumberOfPhysicalPages,
LowestPhysicalPage,
HighestPhysicalPage,
AllocationGranularity,
LowestUserAddress,
HighestUserAddress,
ActiveProcessors;
char NumberProcessors;
}SYSTEM_BASIC_INFORMATION, *PSYSTEM_BASIC_INFORMATION;
typedef struct _SYSTEM_PROCESSOR_TIMES
{
__int64 IdleTime,
KernelTime,
UserTime,
DpcTime,
InterruptTime;
ULONG InterruptCount;
} SYSTEM_PROCESSORS_TIMES[MAX_PROCESSORS], SYSTEM_PROCESSOR_TIMES, *PSYSTEM_PROCESSOR_TIMES;
typedef UINT __stdcall (*ZwQuerySystemInformation)(DWORD,void*,DWORD,DWORD*);
/*function*/
SYSTEM_INFO SYSINFO;
int GetProcessorsCount(void)
{
SYSTEM_INFO SYSINFO;
GetSystemInfo(&SYSINFO);
return SYSINFO.dwNumberOfProcessors;
}
int GetProcessorType(SYSTEM_INFO& SYSINFO)
{
GetSystemInfo(&SYSINFO);
return SYSINFO.dwProcessorType;
}
int GetProcessorOemId(SYSTEM_INFO& SYSINFO)
{
GetSystemInfo(&SYSINFO);
return SYSINFO.dwOemId;
}
int * cpu_usage;
__int64 temp = 0;
unsigned int cycle_count = 0,
count_mas = 0,
interval = 1000; //период опроса (мс)
HMODULE lib = NULL;
DWORD nowtime, oldtime, pertime, rez, curtime = 0;
SYSTEM_PROCESSORS_TIMES CurrentSysProcTimes, PreviousSysProcTimes;
ZwQuerySystemInformation func;
String msg = "";
char * windowname = new char[20];
bool init = false;
void __fastcall TForm1::Button1Click(TObject *Sender)
{
strcpy(windowname,"CPU usage");
ZeroMemory(&CurrentSysProcTimes[0],sizeof(CurrentSysProcTimes));
ZeroMemory(&PreviousSysProcTimes[0],sizeof(PreviousSysProcTimes));
lib = LoadLibrary(L"Ntdll.dll"); //динамическая загрузка библиотеки
if (!lib)
{
msg = "Error #1: Не удалось динамически загрузить быблиотеку!";
Application->MessageBox(msg.c_str(), L"Warning", MB_OK | MB_ICONERROR);
return;
}
func = (ZwQuerySystemInformation)GetProcAddress(lib,"ZwQuerySystemInformation"); //запрос адреса функции
if (!func)
{
msg = "Error #2: Не удалось получить адрес функции!";
Application->MessageBox(msg.c_str(), L"Warning", MB_OK | MB_ICONERROR);
FreeLibrary(lib);
return;
}
Label1->Caption = "количество ядер : " + IntToStr(GetProcessorsCount()) + " / тип процессора : " + IntToStr(GetProcessorType(SYSINFO)) + " / OemId : " + IntToStr(GetProcessorOemId(SYSINFO));
init = true;
msg = "Инициализация успешна!";
Application->MessageBox(msg.c_str(), (String(windowname)).c_str(), MB_OK | MB_ICONINFORMATION);
Button1->Enabled = false;
Button3->Enabled = true;
cpu_usage = new int[100000];
count_mas = 0;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
nowtime = GetTickCount();
pertime = nowtime - oldtime;
curtime += pertime;
oldtime = nowtime;
/*read the usage*/
func(SystemProcessorTimes,
&CurrentSysProcTimes[0],
sizeof(CurrentSysProcTimes),
&rez);
temp = 0;
for(int j=0;j<MAX_PROCESSORS;j++)
{
temp += CurrentSysProcTimes[j].IdleTime - PreviousSysProcTimes[j].IdleTime;
}
temp /= 10000; //переводим из наносекунд в миллисекунды
temp /= (int) GetProcessorsCount(); //делим на количество ядер