Reputation: 54251
Any help figuring out how to do this would be great: How much time each CPU core spent in the C0 power state over the past second.
This is for a mac app so Objective-C, cocoa and c are needed.
Upvotes: 9
Views: 1317
Reputation: 208
For getting C0 percentage you should do the following:
Read following MSRs at start and end points of the period you measure:
0x3FC (core c3), 0x3FD (core c6), 0x3FE (core c7), 0x10 (tsc)
Then do the following calculation:
Cx_ticks = (c3_after - c3_before) + (c6_after - c6_before) + (c7_after - c7_before)
total_ticks = (tsc_after - tsc_before)
Cx_percentage = Cx_ticks/total_ticks
C0_percentage = 100% - Cx_percentage
You can find more information in this document (go to Vol. 3C 35-95)
Upvotes: 0
Reputation: 720
OS X doesn't have any APIs that expose the c-state of the CPU. However, it seems like you can do this using the MWAIT/MONITOR instructions on intel CPUs. Intel mentions that you can track C-state residency using this technique in section 14.4 of the reference manual:
Software should use CPUID to discover if a target processor supports the enumeration of MWAIT extensions. If CPUID.05H.ECX[Bit 0] = 1, the target processor supports MWAIT extensions and their enumeration (see Chapter 3, “Instruction Set Reference, A-M,” of Intel® 64 and IA-32 Architectures Software Developer’s Manual, Volume 2A).
If CPUID.05H.ECX[Bit 1] = 1, the target processor supports using interrupts as break-events for MWAIT, even when interrupts are disabled. Use this feature to measure C-state residency as follows:
Software can write to bit 0 in the MWAIT Extensions register (ECX) when issuing an MWAIT to enter into a processor-specific C-state or sub C-state. When a processor comes out of an inactive C-state or sub C-state, software can read a timestamp before an interrupt service routine (ISR) is potentially executed.
You can find more info about the MWAIT instruction in the same manual. Good luck!
Upvotes: 7