From: Noixe on
"Noixe" <noixe(a)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?
From: Noixe on
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

From: Christian ASTOR on
On 17 jan, 20:40, "Noixe" <no...(a)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");
}
From: Noixe on
"Christian ASTOR" <castorix(a)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;
}

From: Noixe on
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;
}