Page 1 of 1

Query CPU Performance Data,...

Posted: Fri Mar 20, 2009 10:20 am
by keremg
Hi,

well, since i never did soemthing like that before, i would like to know
how i can query performance data from the (intel) cpu internals. I would
like to do something similar like this application:

http://www.cpuid.com/perfmonitor.php

AFAIK it uses a kernel driver, so this seems to be only
available in kernel mode. Well, i know how to write a
Windows Kernel Driver and IOCTL Data from ring0 to
ring3 and vice versa, but what do i have to do, to get
those data in kernel mode? Amazing would be some example,
either in Assembler or if it can be done (i dont think so)
in C,...

Can i Query this Data from Usermode maybe, maybe by using
some undocumented calls? I am mainly using NT based OS like
Windows 2000 up to Windows 7 (32 bit), no need for 64 bit yet,..

Thanks in Advance,...

Regards

Kerem

Re: Query CPU Performance Data,...

Posted: Fri Mar 20, 2009 11:02 am
by stlw
keremg wrote:Hi,

well, since i never did soemthing like that before, i would like to know
how i can query performance data from the (intel) cpu internals. I would
like to do something similar like this application:

http://www.cpuid.com/perfmonitor.php

AFAIK it uses a kernel driver, so this seems to be only
available in kernel mode. Well, i know how to write a
Windows Kernel Driver and IOCTL Data from ring0 to
ring3 and vice versa, but what do i have to do, to get
those data in kernel mode? Amazing would be some example,
either in Assembler or if it can be done (i dont think so)
in C,...

Can i Query this Data from Usermode maybe, maybe by using
some undocumented calls? I am mainly using NT based OS like
Windows 2000 up to Windows 7 (32 bit), no need for 64 bit yet,..

Thanks in Advance,...

Regards

Kerem
This is all about perfmons (hardware performance monitor counters).
Intel® 64 and IA-32 Architectures
Software Developer’s Manual
Volume 3B:System Programming Guide, Part 2
18.13 ARCHITECTURAL PERFORMANCE MONITORING

Stanislav

Re: Query CPU Performance Data,...

Posted: Fri Mar 20, 2009 11:43 am
by keremg
stlw wrote: This is all about perfmons (hardware performance monitor counters).
Intel® 64 and IA-32 Architectures
Software Developer’s Manual
Volume 3B:System Programming Guide, Part 2
18.13 ARCHITECTURAL PERFORMANCE MONITORING

Stanislav
Hi Stanislav,

thanks for your reply. I must honestly confess that i really dont know
what and where i have to start. Can you provide some lines of assembly,
maybe a example to query the Number of memory data reads/writes as
it is provided on Pentium processor performance monitoring events
(manual Page 529/A.9 (Appendix A on Sheet)? This would be really great!

Thanks in advance,...


Regards

Kerem

Re: Query CPU Performance Data,...

Posted: Fri Mar 20, 2009 3:19 pm
by 01000101
This is the blind leading the blind, but I too have been looking into performance monitoring and may have some help that I can provide.

From what I understand (and I hope someone will correct/elaborate when necessary), you need to set one of the IA32_PERFEVTSELx MSRs with appropirate values (OS/USR mode, the desired bit mask [UnHalted instructions, etc...]. Then when one of the events happens (UnHalted instruction is executed) the cooresponding IA32_PMCx MSR will reflect the current count. It can also be set so that the APIC will generate an interrupt when the counter overflows. I'd assume the IA32_PMCx MSR must be manually read in order to query its counter state.

Re: Query CPU Performance Data,...

Posted: Fri Mar 20, 2009 6:02 pm
by 01000101
I just tried some performance monitoring in BOCHS, QEMU, and VirtualBox-OSE and it seems that none of them properly support it. BOCHS doesn't support it at all, QEMU gives a valid version but supports no events (in CPUID.0x0A.EBX, or an > 0 count in EAX), and VirtualBox gives all valid results except for CPUID.0x0A.EBX being null (no supported counters). The counters all return 0 when read.

Re: Query CPU Performance Data,...

Posted: Fri Mar 20, 2009 8:21 pm
by Brendan
Hi,
01000101 wrote:I just tried some performance monitoring in BOCHS, QEMU, and VirtualBox-OSE and it seems that none of them properly support it. BOCHS doesn't support it at all, QEMU gives a valid version but supports no events (in CPUID.0x0A.EBX, or an > 0 count in EAX), and VirtualBox gives all valid results except for CPUID.0x0A.EBX being null (no supported counters). The counters all return 0 when read.
If an emulator tried to support performance monitoring, then it'd need to do a series of checks for every instruction emulated, and it would probably make the emulator about 50 times slower. Performance monitoring is also CPU specific, which makes it a lot of work to implement, and it's something that's rarely used (e.g. only used by specialized profiling tools). Basically it's not something I'd expect any emulator will ever support.


Cheers,

Brendan

Re: Query CPU Performance Data,...

Posted: Sat Mar 21, 2009 1:24 pm
by bewing
Hey, I'm trying! I expect (hope for) my version of bochs to have one CPU model that supports it. It'll be really slow, as you say -- but if I put in all that memory-fetch-time modeling that you want, that model will be really super slow anyway.

Re: Query CPU Performance Data,...

Posted: Sun Mar 22, 2009 10:31 am
by keremg
Hi,

does someone have some example code for me?


TIA

Regards

Kerem

Re: Query CPU Performance Data,...

Posted: Sun Mar 22, 2009 10:45 am
by 01000101
keremg wrote: does someone have some example code for me?
stop looking for a free handout, and read the intel manuals. All the info about reading performance counters are there, you just need to look. Program the select MSR and read from the counter MSR, it's fairly simple.