Does anyone got information concerning the native api NtQuerySystemInformation (in relation with cpu usage) ???
Dom
Dom
Adapted from "Windows NT 2000 Native Api Reference...
Regards, bilbo
Prototype:
NTSYSAPI NTSTATUS NTAPI
ZwQuerySystemInformation(
IN ULONG SystemInformationClass,
IN OUT PVOID SystemInformation,
IN ULONG SystemInformationLength,
OUT PULONG ReturnLength OPTIONAL);
Parameters:
SystemInformationClass
The type of system information to be queried (USE 8 in your case)
SystemInformation
Points to a caller-allocated buffer or variable that receives the requested system information.
SystemInformationLength
The size in bytes of SystemInformation, which the caller should set according to the given SystemInformationClass.
In your case SystemInformation will point to:
typedef struct _SYSTEM_PROCESSOR_TIMES { // Information Class 8
LARGE_INTEGER IdleTime;
LARGE_INTEGER KernelTime;
LARGE_INTEGER UserTime;
LARGE_INTEGER DpcTime;
LARGE_INTEGER InterruptTime;
ULONG InterruptCount;
} SYSTEM_PROCESSOR_TIMES, *PSYSTEM_PROCESSOR_TIMES;
where:
IdleTime
The idle time, measured in units of 100-nanoseconds, of the processor.
KernelTime
The time the processor spent executing in kernel mode, measured in units of 100-nanoseconds.
UserTime
The time the processor spent executing in user mode, measured in units of 100-nanoseconds.
DpcTime
The time the processor spent executing deferred procedure calls, measured in units of 100-nanoseconds.
InterruptTime
The time the processor spent executing interrupt routines, measured in units of 100-nanoseconds.
InterruptCount
The number of interrupts serviced by the processor.
Regards, bilbo
thx 4 help, bilbo...sourcecode is also welcomed....
Well, Dom, sorry for C language, I'm faster with it...
I leave to you the fun of converting the snippet to ASM...
Regards, bilbo
#include <windows.h>
#include <stdio.h>
typedef DWORD (NTAPI *pNtQuerySystemInformation)(
DWORD info_class, void *out, DWORD size, DWORD *out_size);
typedef struct _SYSTEM_PROCESSOR_TIMES { // Information Class 8
// times measured in units of 100-nanoseconds
__int64 IdleTime;
__int64 KernelTime;
__int64 UserTime;
__int64 DpcTime;
__int64 InterruptTime;
ULONG InterruptCount;
} SYSTEM_PROCESSOR_TIMES, *PSYSTEM_PROCESSOR_TIMES;
void
main(void)
{
DWORD size;
SYSTEM_PROCESSOR_TIMES buf;
pNtQuerySystemInformation NtQuerySystemInformation =
(pNtQuerySystemInformation)GetProcAddress(
GetModuleHandle("ntdll.dll"), "NtQuerySystemInformation");
// must return 0, and size must be sizeof(buf), 48
// displayed times are converted to seconds
NtQuerySystemInformation(8, &buf, sizeof(buf), &size);
printf("idle %d, k %d, u %d, dpc %d, int %d (cnt %d)\n",
(DWORD)(buf.IdleTime/10000000),
(DWORD)(buf.KernelTime/10000000),
(DWORD)(buf.UserTime/10000000),
(DWORD)(buf.DpcTime/10000000),
(DWORD)(buf.InterruptTime/10000000),
buf.InterruptCount);
}
I leave to you the fun of converting the snippet to ASM...
Regards, bilbo
hey bilbo, thanks for the snippet (no prob with c), but the reason why i need this api is that i want to receive the current cpu usage. how can i use the idle time (!?) to calculate cpu usage? :?
Dom
Dom
Well, if you run that API every second, and you subtract the previous values from the current ones, you will obtain the values mediated on ONE SECOND. If you run that API even more often, you will obtain even more istantaneous values, but you will add some load to the CPU (the measuring method will become invasive).
So, assuming that the one-second deltas are ok, take the DELTA IDLE TIME and refer it to the total time (1 second); complement it and you will have the CPU load.
The following console program will give you the idle percentage (sorry again for C)...
NOTE - I'm not using Sleep(1000) between API calls, because in that case you would obtain values bigger than 100%; we must use a Sleep for a minor number of milliseconds, in order to take into account the times for the extra code: this value will be calculated with an empirical method in the first loops (it depends on the computer speed!)
Regards, bilbo
So, assuming that the one-second deltas are ok, take the DELTA IDLE TIME and refer it to the total time (1 second); complement it and you will have the CPU load.
The following console program will give you the idle percentage (sorry again for C)...
NOTE - I'm not using Sleep(1000) between API calls, because in that case you would obtain values bigger than 100%; we must use a Sleep for a minor number of milliseconds, in order to take into account the times for the extra code: this value will be calculated with an empirical method in the first loops (it depends on the computer speed!)
Regards, bilbo
thx again, bilbo 8)
Dominik
Dominik