Get CPU usage and temperature

  • Follow


Hello,

How I can get percentage of CPU usage and temperature of every single core 
of my quad core?

With GetSystemInfo API I can get only the number of cores.

I need also get the percentage of used RAM.

I use C++ builder 

0
Reply Noixe 1/17/2010 10:40:07 AM

I found this link: 
http://msdn.microsoft.com/en-us/library/aa373078(VS.85).aspx

but I can't use that information... there are a lot of functions! 

0
Reply Noixe 1/17/2010 2:10:16 PM


On 17 jan, 11:40, "Noixe" <no...@TOGLIMIemail.it> wrote:

> How I can get percentage of CPU usage and temperature of every single core
> of my quad core?

 e.g. with NtQuerySystemInformation() &
 SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION
(temperature with WMI, if available)
0
Reply Christian 1/17/2010 2:30:23 PM

"Christian ASTOR" <castorix@club-internet.fr> ha scritto:
 
> e.g. with NtQuerySystemInformation() &
> SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION
> (temperature with WMI, if available)

Could do an example?

I have tried NtQuerySystemInformation() but obtain only number of core. 
0
Reply Noixe 1/17/2010 2:35:25 PM

I tried:

    SYSTEM_INFO SysInfo;
    GetSystemInfo(&SysInfo);

    Caption = SysInfo.dwNumberOfProcessors;

   // Error. SYSTEM_INFORMATION_CLASS  don't exist.
    SYSTEM_INFORMATION_CLASS SystemInformationClass = 
SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION;


    PVOID SystemInformation;
    ULONG SystemInformationLength;
    PULONG ReturnLength;

    // I must import this function from ntdll.dll, but I don't know how.

    ZwQuerySystemInformation(SystemInformationClass,
                             SystemInformation,
                             SystemInformationLength,
                             ReturnLength); 

0
Reply Noixe 1/17/2010 3:09:14 PM

On 17 jan, 16:09, "Noixe" <no...@TOGLIMIemail.it> wrote:

> =A0 =A0 // I must import this function from ntdll.dll, but I don't know h=
ow.
>
> =A0 =A0 ZwQuerySystemInformation(SystemInformationClass,
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0SystemInformat=
ion,
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0SystemInformat=
ionLength,
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0ReturnLength);

You call it dynamically :
HMODULE hNtDll =3D LoadLibrary("NtDll.dll");
NtQuerySystemInformation =3D (NTQUERYSYSTEMINFORMATION)GetProcAddress
(hNtDll, "NtQuerySystemInformation");
SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION ProcPerfInfo[16];
NtQuerySystemInformation(SystemProcessorPerformanceInformation,
ProcPerfInfo,
		sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION) * 16, NULL);
// etc...
0
Reply Christian 1/17/2010 4:48:44 PM

"Christian ASTOR" <castorix@club-internet.fr> ha scritto:

I obtain an error in
NtQuerySystemInformation = (NTQUERYSYSTEMINFORMATION)GetProcAddress(hNtDll, 
"NtQuerySystemInformation");

Undefined symbol 'NtQuerySystemInformation'

 

0
Reply Noixe 1/17/2010 4:54:15 PM

On 17 jan, 17:54, "Noixe" <no...@TOGLIMIemail.it> wrote:
> "Christian ASTOR" <casto...@club-internet.fr> ha scritto:
>
> I obtain an error in
> NtQuerySystemInformation = (NTQUERYSYSTEMINFORMATION)GetProcAddress(hNtDll,
> "NtQuerySystemInformation");
>
> Undefined symbol 'NtQuerySystemInformation'

If you don't define it, it's normal...
typedef NTSTATUS (CALLBACK* NTQUERYSYSTEMINFORMATION)
(
	IN SYSTEM_INFORMATION_CLASS SystemInformationClass,
	OUT PVOID SystemInformation,
	IN ULONG SystemInformationLength,
	OUT PULONG ReturnLength OPTIONAL
);
NTQUERYSYSTEMINFORMATION NtQuerySystemInformation;

(and SYSTEM_INFORMATION_CLASS also...)
0
Reply Christian 1/17/2010 6:29:19 PM

"Christian ASTOR" <castorix@club-internet.fr> ha scritto:

> If you don't define it, it's normal...
> typedef NTSTATUS (CALLBACK* NTQUERYSYSTEMINFORMATION)
> (
> IN SYSTEM_INFORMATION_CLASS SystemInformationClass,
> OUT PVOID SystemInformation,
> IN ULONG SystemInformationLength,
> OUT PULONG ReturnLength OPTIONAL
> );
> NTQUERYSYSTEMINFORMATION NtQuerySystemInformation;
>
> (and SYSTEM_INFORMATION_CLASS also...)

I obtain
Undefined symbol 'NTQUERYSYSTEMINFORMATION'

Sorry, but this argument is new for me and all info are in english. It is 
too hard.

There is alway some problem.

Could you write an example that I can copy and past and compile? 

0
Reply Noixe 1/17/2010 7:40:54 PM

I found this code that compile correctly but the result is always 0.

#include <iostream>
#include "stdafx.h"
#include <windows.h>

typedef struct _SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION {
    ULONG IdleTime;
    LARGE_INTEGER KernelTime;
    LARGE_INTEGER UserTime;
    LARGE_INTEGER Reserved1[2];
    ULONG Reserved2;
} SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION;

SYSTEM_INFO SysInfo;
const int nCPU = SysInfo.dwNumberOfProcessors;

HINSTANCE hNtDll = LoadLibraryW(L"ntdll.dll");
PFN_NTQUERYSYSTEMINFORMATION NTQSI = 
PFN_NTQUERYSYSTEMINFORMATION(GetProcAddress(hNtDll, 
"NtQuerySystemInformation"));

SYSTEM_INFORMATION_CLASS SystemInformationClass = 
SystemProcessorPerformanceInformation;

ULONG SystemInformationLength;
PULONG ReturnLength;

SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION SystemInformation[4];


int main() {
   GetSystemInfo(&SysInfo);

   NTQSI(SystemInformationClass, SystemInformation, SystemInformationLength, 
ReturnLength);

   std::cout << SystemInformation[0].IdleTime << std::endl;
} 

0
Reply Noixe 1/17/2010 8:49:32 PM

"Noixe" <noixe@TOGLIMIemail.it> ha scritto:

> typedef struct _SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION {
>    ULONG IdleTime;

LARGE_INTEGER  IdleTime;

>   std::cout << SystemInformation[0].IdleTime << std::endl;

std::cout << SystemInformation[0].IdleTime.QuadPart << std::endl;

Is there a formula for calculate the usage? 
0
Reply Noixe 1/17/2010 10:21:07 PM

About a RAM I found:


MEMORYSTATUSEX MemInfo;
GlobalMemoryStatusEx(&MemInfo);
std::cout << "% RAM: " << MemInfo.dwMemoryLoad << std::endl;
std::cout << "Megabytes of Used RAM: " << (MemInfo.ullTotalPhys - 
MemInfo.ullAvailPhys) / 1048576 << std::endl;


But the MemInfo.dwMemoryLoad is always 8 and (MemInfo.ullTotalPhys - 
MemInfo.ullAvailPhys) / 1048576 generate a number too big 

0
Reply Noixe 1/17/2010 10:46:58 PM

On 17 jan, 20:40, "Noixe" <no...@TOGLIMIemail.it> wrote:

> Could you write an example that I can copy and past and compile?

A sample displaying the CPU usage for each core in a timer =>

#include <iostream>
#include <tchar.h>
#include <windows.h>

typedef enum _SYSTEM_INFORMATION_CLASS
{
	// reduced...
	SystemBasicInformation = 0,
	SystemProcessorPerformanceInformation = 8
 } SYSTEM_INFORMATION_CLASS;

 typedef struct _SYSTEM_BASIC_INFORMATION {
     ULONG Reserved;
     ULONG TimerResolution;
     ULONG PageSize;
     ULONG NumberOfPhysicalPages;
     ULONG LowestPhysicalPageNumber;
     ULONG HighestPhysicalPageNumber;
     ULONG AllocationGranularity;
     ULONG_PTR MinimumUserModeAddress;
     ULONG_PTR MaximumUserModeAddress;
     ULONG_PTR ActiveProcessorsAffinityMask;
     CCHAR NumberOfProcessors;
 } SYSTEM_BASIC_INFORMATION, *PSYSTEM_BASIC_INFORMATION;

 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;

typedef NTSTATUS (CALLBACK* NTQUERYSYSTEMINFORMATION)
(
	IN SYSTEM_INFORMATION_CLASS SystemInformationClass,
	OUT PVOID SystemInformation,
	IN ULONG SystemInformationLength,
	OUT PULONG ReturnLength OPTIONAL
);
NTQUERYSYSTEMINFORMATION NtQuerySystemInformation;

#define MAX_PROCESSORS 16
LARGE_INTEGER CPUIdleTimeOld[MAX_PROCESSORS] = {0 ,0};
LARGE_INTEGER CPUTotalTimeOld[MAX_PROCESSORS] = {0 ,0};
LARGE_INTEGER CPUKernelTimeOld[MAX_PROCESSORS] = {0 ,0};
CCHAR NumberOfProcessors;
VOID CALLBACK TimerProc(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD
dwTime);

int _tmain(int argc, _TCHAR* argv[])
{
 	SYSTEM_BASIC_INFORMATION BasicInfo;
	PSYSTEM_PROCESSOR_PERFORMANCE_INFORMATION PProcPerfInfo;
	SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION ProcPerfInfo
[MAX_PROCESSORS];

	HMODULE hNtDll = LoadLibrary("NtDll.dll");
	NtQuerySystemInformation = (NTQUERYSYSTEMINFORMATION)GetProcAddress
(hNtDll, "NtQuerySystemInformation");
	NtQuerySystemInformation(SystemBasicInformation, &BasicInfo, sizeof
(BasicInfo), NULL);
	NumberOfProcessors = BasicInfo.NumberOfProcessors;
	NtQuerySystemInformation(SystemProcessorPerformanceInformation,
ProcPerfInfo,
		sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION) * MAX_PROCESSORS,
NULL);

	PProcPerfInfo = ProcPerfInfo;
	for (int i=0; i < NumberOfProcessors; i++)
	{
		CPUIdleTimeOld[i] = PProcPerfInfo->IdleTime;
		CPUTotalTimeOld[i].QuadPart = PProcPerfInfo->UserTime.QuadPart +
PProcPerfInfo->KernelTime.QuadPart;
		CPUKernelTimeOld[i].QuadPart = PProcPerfInfo->KernelTime.QuadPart +
PProcPerfInfo->IdleTime.QuadPart;
		PProcPerfInfo++;
	}
	SetTimer(NULL, 1, 500, TimerProc);
	MSG msg;
	while(GetMessage(&msg, NULL, 0, 0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
		if (GetKeyState (VK_SPACE) & 0x80)
			break;
	}
	FreeLibrary(hNtDll);
    return 0;
}

VOID CALLBACK TimerProc(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD
dwTime)
{
	LARGE_INTEGER CPUIdleTime[MAXIMUM_PROCESSORS];
	LARGE_INTEGER CPUTotalTime[MAXIMUM_PROCESSORS];
    LARGE_INTEGER CPUKernelTime[MAXIMUM_PROCESSORS];
	SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION ProcPerfInfo
[MAX_PROCESSORS];
    NtQuerySystemInformation(SystemProcessorPerformanceInformation,
ProcPerfInfo,
       sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION) *
MAX_PROCESSORS, NULL );

	for (int i=0; i < NumberOfProcessors; i++)
	{
		LARGE_INTEGER CPUIdleTimeDiff;
		LARGE_INTEGER CPUTotalTimeDiff;
		LARGE_INTEGER CPUKernelTimeDiff;

		CPUIdleTime[i].QuadPart = ProcPerfInfo[i].IdleTime.QuadPart;
		CPUKernelTime[i].QuadPart = ProcPerfInfo[i].KernelTime.QuadPart -
ProcPerfInfo[i].IdleTime.QuadPart;
		CPUTotalTime[i].QuadPart = ProcPerfInfo[i].KernelTime.QuadPart +
ProcPerfInfo[i].UserTime.QuadPart;

		CPUIdleTimeDiff.QuadPart = CPUIdleTime[i].QuadPart - CPUIdleTimeOld
[i].QuadPart;
		CPUKernelTimeDiff.QuadPart = CPUKernelTime[i].QuadPart -
CPUKernelTimeOld[i].QuadPart;
		CPUTotalTimeDiff.QuadPart = CPUTotalTime[i].QuadPart -
CPUTotalTimeOld[i].QuadPart;

		BYTE CurrentCPU;
		if (CPUTotalTimeDiff.QuadPart != 0)
             CurrentCPU = static_cast<BYTE>(100 -
((CPUIdleTimeDiff.QuadPart * 100) / CPUTotalTimeDiff.QuadPart));
        else
            CurrentCPU = 0;
		fprintf(stdout, "CPU-%d : %d%%\n", i+1, CurrentCPU);
		CPUTotalTimeOld[i].QuadPart = CPUTotalTime[i].QuadPart;
		CPUIdleTimeOld[i].QuadPart = CPUIdleTime[i].QuadPart;
		CPUKernelTimeOld[i].QuadPart = CPUKernelTime[i].QuadPart;
	}
	fprintf(stdout, "\n");
 }
0
Reply Christian 1/17/2010 11:07:14 PM

"Christian ASTOR" <castorix@club-internet.fr> ha scritto:

> A sample displaying the CPU usage for each core in a timer =>

I obtain only a lot of error a compile time. I have correct some of it. This 
source compile but get a bad result:


#include <iostream>
#include <windows.h>
#include <ntsecapi.h>

typedef enum _SYSTEM_INFORMATION_CLASS {
  SystemBasicInformation,
  SystemProcessorInformation,
  SystemPerformanceInformation,
  SystemTimeOfDayInformation,
  SystemPathInformation,
  SystemProcessInformation,
  SystemCallCountInformation,
  SystemDeviceInformation,
  SystemProcessorPerformanceInformation,
  SystemFlagsInformation,
  SystemCallTimeInformation,
  SystemModuleInformation,
  SystemLocksInformation,
  SystemStackTraceInformation,
  SystemPagedPoolInformation,
  SystemNonPagedPoolInformation,
  SystemHandleInformation,
  SystemObjectInformation,
  SystemPageFileInformation,
  SystemVdmInstemulInformation,
  SystemVdmBopInformation,
  SystemFileCacheInformation,
  SystemPoolTagInformation,
  SystemInterruptInformation,
  SystemDpcBehaviorInformation,
  SystemFullMemoryInformation,
  SystemLoadGdiDriverInformation,
  SystemUnloadGdiDriverInformation,
  SystemTimeAdjustmentInformation,
  SystemSummaryMemoryInformation,
  SystemNextEventIdInformation,
  SystemEventIdsInformation,
  SystemCrashDumpInformation,
  SystemExceptionInformation,
  SystemCrashDumpStateInformation,
  SystemKernelDebuggerInformation,
  SystemContextSwitchInformation,
  SystemRegistryQuotaInformation,
  SystemExtendServiceTableInformation,
  SystemPrioritySeperation,
  SystemPlugPlayBusInformation,
  SystemDockInformation,
  SystemPowerInformation_,
  SystemProcessorSpeedInformation,
  SystemCurrentTimeZoneInformation,
  SystemLookasideInformation
} SYSTEM_INFORMATION_CLASS, *PSYSTEM_INFORMATION_CLASS;

typedef struct _SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION {
    LARGE_INTEGER IdleTime;
    LARGE_INTEGER KernelTime;
    LARGE_INTEGER UserTime;
    LARGE_INTEGER Reserved1[2];
    ULONG Reserved2;
} SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION;


SYSTEM_INFO SysInfo;
const int NumberOfProcessors = SysInfo.dwNumberOfProcessors;
typedef NTSTATUS (WINAPI * 
PFN_NTQUERYSYSTEMINFORMATION)(SYSTEM_INFORMATION_CLASS, PVOID, ULONG, 
PULONG);
HINSTANCE hNtDll = LoadLibraryW(L"ntdll.dll");
PFN_NTQUERYSYSTEMINFORMATION NTQSI = 
PFN_NTQUERYSYSTEMINFORMATION(GetProcAddress(hNtDll, 
"NtQuerySystemInformation"));

SYSTEM_INFORMATION_CLASS SystemInformationClass = 
SystemProcessorPerformanceInformation;

SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION SystemInformation[4], 
ProcPerfInfo[4];


int main() {

   GetSystemInfo(&SysInfo);

   ULONG SystemInformationLength = NumberOfProcessors * 
sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION);

   NTQSI(SystemInformationClass, SystemInformation, SystemInformationLength, 
0);


   LARGE_INTEGER CPUIdleTimeOld[MAXIMUM_PROCESSORS];
   LARGE_INTEGER CPUTotalTimeOld[MAXIMUM_PROCESSORS];
   LARGE_INTEGER CPUKernelTimeOld[MAXIMUM_PROCESSORS];

   LARGE_INTEGER CPUIdleTime[MAXIMUM_PROCESSORS];
   LARGE_INTEGER CPUTotalTime[MAXIMUM_PROCESSORS];
   LARGE_INTEGER CPUKernelTime[MAXIMUM_PROCESSORS];


   for (int i = 0; i < MAXIMUM_PROCESSORS; i++) {
       LARGE_INTEGER CPUIdleTimeDiff;
       LARGE_INTEGER CPUTotalTimeDiff;
       LARGE_INTEGER CPUKernelTimeDiff;

       CPUIdleTime[i].QuadPart = ProcPerfInfo[i].IdleTime.QuadPart;
       CPUKernelTime[i].QuadPart = ProcPerfInfo[i].KernelTime.QuadPart - 
ProcPerfInfo[i].IdleTime.QuadPart;
       CPUTotalTime[i].QuadPart = ProcPerfInfo[i].KernelTime.QuadPart + 
ProcPerfInfo[i].UserTime.QuadPart;

       CPUIdleTimeDiff.QuadPart = CPUIdleTime[i].QuadPart - 
CPUIdleTimeOld[i].QuadPart;
       CPUKernelTimeDiff.QuadPart = CPUKernelTime[i].QuadPart - 
CPUKernelTimeOld[i].QuadPart;
       CPUTotalTimeDiff.QuadPart = CPUTotalTime[i].QuadPart - 
CPUTotalTimeOld[i].QuadPart;

       BYTE CurrentCPU;

       if (CPUTotalTimeDiff.QuadPart != 0)
           CurrentCPU = static_cast<BYTE>(100 - ((CPUIdleTimeDiff.QuadPart * 
100) / CPUTotalTimeDiff.QuadPart));
        else
            CurrentCPU = 0;

       std::cout << "CPU " << (i + 1) << " " << CurrentCPU << std::endl;

       CPUTotalTimeOld[i].QuadPart = CPUTotalTime[i].QuadPart;
       CPUIdleTimeOld[i].QuadPart = CPUIdleTime[i].QuadPart;
       CPUKernelTimeOld[i].QuadPart = CPUKernelTime[i].QuadPart;
    };


   return 0;
} 

0
Reply Noixe 1/18/2010 10:08:51 AM

About RAM I have found a solution:

#include <iostream>
#include <windows.h>


int main() {
   MEMORYSTATUS MemInfo;

   GlobalMemoryStatus(&MemInfo);

   std::cout << "% RAM: " << MemInfo.dwMemoryLoad << std::endl;
   std::cout << "Megabytes of Used RAM: " << (MemInfo.dwTotalPhys - 
MemInfo.dwAvailPhys) / 1048576 << std::endl;

   return 0;
} 

0
Reply Noixe 1/18/2010 10:19:34 AM

On 18 jan, 11:08, "Noixe" <no...@TOGLIMIemail.it> wrote:

> I obtain only a lot of error a compile time. I have correct some of it. This
> source compile but get a bad result:

Compile it with VS, it works.
And I have tested the resultant exe on several stations (XP)...
0
Reply Christian 1/18/2010 10:20:41 AM

"Christian ASTOR" <castorix@club-internet.fr> ha scritto:

> Compile it with VS, it works.
> And I have tested the resultant exe on several stations (XP)...

I have C++ builder and gcc.

I don't understand why I obtain only strange value. Is it possible that the 
cause is compiler?

For example, the function about RAM work correctly, but function about CPU 
no. 

0
Reply Noixe 1/18/2010 10:53:26 AM

Miracle!

This work correctly for ram and cpu, but only on gcc.

If adapt this code for C++ Builder, work only ram beacuse 
CPUTotalTimeDiff.QuadPart is always 0.
Do you know the reason?

#include <iostream>
#include <windows.h>
#include <ntsecapi.h>



typedef enum _SYSTEM_INFORMATION_CLASS {
  SystemBasicInformation,
  SystemProcessorInformation,
  SystemPerformanceInformation,
  SystemTimeOfDayInformation,
  SystemPathInformation,
  SystemProcessInformation,
  SystemCallCountInformation,
  SystemDeviceInformation,
  SystemProcessorPerformanceInformation,
  SystemFlagsInformation,
  SystemCallTimeInformation,
  SystemModuleInformation,
  SystemLocksInformation,
  SystemStackTraceInformation,
  SystemPagedPoolInformation,
  SystemNonPagedPoolInformation,
  SystemHandleInformation,
  SystemObjectInformation,
  SystemPageFileInformation,
  SystemVdmInstemulInformation,
  SystemVdmBopInformation,
  SystemFileCacheInformation,
  SystemPoolTagInformation,
  SystemInterruptInformation,
  SystemDpcBehaviorInformation,
  SystemFullMemoryInformation,
  SystemLoadGdiDriverInformation,
  SystemUnloadGdiDriverInformation,
  SystemTimeAdjustmentInformation,
  SystemSummaryMemoryInformation,
  SystemNextEventIdInformation,
  SystemEventIdsInformation,
  SystemCrashDumpInformation,
  SystemExceptionInformation,
  SystemCrashDumpStateInformation,
  SystemKernelDebuggerInformation,
  SystemContextSwitchInformation,
  SystemRegistryQuotaInformation,
  SystemExtendServiceTableInformation,
  SystemPrioritySeperation,
  SystemPlugPlayBusInformation,
  SystemDockInformation,
  SystemPowerInformation_,
  SystemProcessorSpeedInformation,
  SystemCurrentTimeZoneInformation,
  SystemLookasideInformation
} SYSTEM_INFORMATION_CLASS;

typedef struct _SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION {
    LARGE_INTEGER IdleTime;
    LARGE_INTEGER KernelTime;
    LARGE_INTEGER UserTime;
    LARGE_INTEGER Reserved1[2];
    ULONG Reserved2;
} SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION;



typedef NTSTATUS (WINAPI * 
PFN_NTQUERYSYSTEMINFORMATION)(SYSTEM_INFORMATION_CLASS, PVOID, ULONG, 
PULONG);
HINSTANCE hNtDll = LoadLibraryW(L"ntdll.dll");
PFN_NTQUERYSYSTEMINFORMATION NTQSI = 
PFN_NTQUERYSYSTEMINFORMATION(GetProcAddress(hNtDll, 
"NtQuerySystemInformation"));

SYSTEM_INFORMATION_CLASS SystemInformationClass = 
SystemProcessorPerformanceInformation;

SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION ProcPerfInfo[4];


int main() {

   SYSTEM_INFO SysInfo;
   GetSystemInfo(&SysInfo);

   MEMORYSTATUS MemInfo;
   GlobalMemoryStatus(&MemInfo);

   const int NumberOfProcessors = SysInfo.dwNumberOfProcessors;
   ULONG SystemInformationLength = NumberOfProcessors * 
sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION);

   LARGE_INTEGER CPUIdleTimeOld[MAXIMUM_PROCESSORS],
                 CPUTotalTimeOld[MAXIMUM_PROCESSORS],
                 CPUKernelTimeOld[MAXIMUM_PROCESSORS],
                 CPUIdleTime[MAXIMUM_PROCESSORS],
                 CPUTotalTime[MAXIMUM_PROCESSORS],
                 CPUKernelTime[MAXIMUM_PROCESSORS];

   while (true) {

       NTQSI(SystemInformationClass, ProcPerfInfo, SystemInformationLength, 
0);
       LARGE_INTEGER CPUIdleTimeDiff = {0, 0};
       LARGE_INTEGER CPUTotalTimeDiff = {0, 0};
       LARGE_INTEGER CPUKernelTimeDiff = {0, 0};

       for (int i = 0; i < NumberOfProcessors; i++) {

           CPUIdleTime[i].QuadPart = ProcPerfInfo[i].IdleTime.QuadPart;
           CPUKernelTime[i].QuadPart = ProcPerfInfo[i].KernelTime.QuadPart - 
ProcPerfInfo[i].IdleTime.QuadPart;
           CPUTotalTime[i].QuadPart = ProcPerfInfo[i].KernelTime.QuadPart + 
ProcPerfInfo[i].UserTime.QuadPart;

           CPUIdleTimeDiff.QuadPart = CPUIdleTime[i].QuadPart - 
CPUIdleTimeOld[i].QuadPart;
           CPUKernelTimeDiff.QuadPart = CPUKernelTime[i].QuadPart - 
CPUKernelTimeOld[i].QuadPart;
           CPUTotalTimeDiff.QuadPart = CPUTotalTime[i].QuadPart - 
CPUTotalTimeOld[i].QuadPart;

           unsigned int CurrentCPU;

           if (CPUTotalTimeDiff.QuadPart != 0)
              CurrentCPU = static_cast<unsigned int>(100 - 
((CPUIdleTimeDiff.QuadPart * 100) / CPUTotalTimeDiff.QuadPart));
           else
              CurrentCPU = 0;

           std::cout << "CPU " << (i + 1) << ": " << CurrentCPU << "%" << 
std::endl;

           CPUTotalTimeOld[i].QuadPart = CPUTotalTime[i].QuadPart;
           CPUIdleTimeOld[i].QuadPart = CPUIdleTime[i].QuadPart;
           CPUKernelTimeOld[i].QuadPart = CPUKernelTime[i].QuadPart;
       };


       std::cout << "RAM: " << MemInfo.dwMemoryLoad << "%" << std::endl;
       std::cout << "MB RAM: " << (MemInfo.dwTotalPhys - 
MemInfo.dwAvailPhys) / 1048576 << std::endl;
       std::cout << std::endl;

       Sleep(1000);
   };

   return 0;
}
 

0
Reply Noixe 1/18/2010 12:30:44 PM

17 Replies
818 Views

(page loaded in 0.097 seconds)

Similiar Articles:


















7/22/2012 4:37:48 AM


Reply: