Check hyper thread technology

Just starting out? Need help? Post your questions and find answers here.
User avatar
oryaaaaa
Addict
Addict
Posts: 825
Joined: Mon Jan 12, 2004 11:40 pm
Location: Okazaki, JAPAN

Check hyper thread technology

Post by oryaaaaa »

SSE2 processing Core i7 (4 cores HT) = Core i5 (4 cores)
then enable htt by CountCPUs() will return 4

I can't understand ebx oparation.

Code: Select all

Procedure.b CheckHTT()
  ; Check Hyper thread technology
  Protected htt.l
  !MOV Eax, 1
  !CPUID
  !MOV [p.v_htt], Edx
  If (sse2 >> 28)>0
    ; ? ebx [23 : 16] > 1
    ProcedureReturn #True 
  EndIf
EndProcedure
Thank you
sec
Enthusiast
Enthusiast
Posts: 792
Joined: Sat Aug 09, 2003 3:13 am
Location: 90-61-92 // EU or ASIA
Contact:

Re: Check hyper thread technology

Post by sec »

oryaaaaa wrote:SSE2 processing Core i7 (4 cores HT) = Core i5 (4 cores)
then enable htt by CountCPUs() will return 4

I can't understand ebx oparation.

Code: Select all

Procedure.b CheckHTT()
  ; Check Hyper thread technology
  Protected htt.l
  !MOV Eax, 1
  !CPUID
  !MOV [p.v_htt], Edx
  If (sse2 >> 28)>0
    ; ? ebx [23 : 16] > 1
    ProcedureReturn #True 
  EndIf
EndProcedure
Thank you
ebx[23:16] means bit 23 to bit16. you can write:

Code: Select all

(ebx >> 16) & $ff
or

Code: Select all

shr ebx, 16
and ebx, $ff
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Check hyper thread technology

Post by wilbert »

Officially you should preserve rbx/ebx since it is non volatile.

Code: Select all

Procedure.a LogicalProcessorsPerPhysical()
  CompilerIf #PB_Compiler_Processor = #PB_Processor_x64
    !push rbx
  CompilerElse
    !push ebx
  CompilerEndIf
  !mov eax, 1
  !cpuid
  !mov eax, 1
  !bt edx, 26
  !jnc lproc_per_physical_exit
  !bswap ebx
  !movzx eax, bh
  !lproc_per_physical_exit:
  CompilerIf #PB_Compiler_Processor = #PB_Processor_x64
    !pop rbx
  CompilerElse
    !pop ebx
  CompilerEndIf
  ProcedureReturn 
EndProcedure

Debug LogicalProcessorsPerPhysical()
Windows (x64)
Raspberry Pi OS (Arm64)
sec
Enthusiast
Enthusiast
Posts: 792
Joined: Sat Aug 09, 2003 3:13 am
Location: 90-61-92 // EU or ASIA
Contact:

Re: Check hyper thread technology

Post by sec »

wilbert wrote:Officially you should preserve rbx/ebx since it is non volatile.

Code: Select all

Procedure.a LogicalProcessorsPerPhysical()
  CompilerIf #PB_Compiler_Processor = #PB_Processor_x64
    !push rbx
  CompilerElse
    !push ebx
  CompilerEndIf
  !mov eax, 1
  !cpuid
  !bswap ebx
  !movzx eax, bh
  CompilerIf #PB_Compiler_Processor = #PB_Processor_x64
    !pop rbx
  CompilerElse
    !pop ebx
  CompilerEndIf
  ProcedureReturn 
EndProcedure

Debug LogicalProcessorsPerPhysical()
Hi wilbert, do you know why LogicalProcessorsPerPhysical is "16" on my CPU? is it not correct on this cpu: http://ark.intel.com/products/69669/
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Check hyper thread technology

Post by wilbert »

sec wrote:Hi wilbert, do you know why LogicalProcessorsPerPhysical is "16" on my CPU? is it not correct on this cpu: http://ark.intel.com/products/69669/
That seems incorrect.
I updated my code above to check for the HTT flags first. Does it still return 16 ?
It looks like if that flag is not set, you can't rely on bits 23:16 . I updated the code to return 1 in that case but for your cpu that also doesn't seem correct since it has 2 cores.
On my Core2Duo it reports 2 as it should.
Windows (x64)
Raspberry Pi OS (Arm64)
sec
Enthusiast
Enthusiast
Posts: 792
Joined: Sat Aug 09, 2003 3:13 am
Location: 90-61-92 // EU or ASIA
Contact:

Re: Check hyper thread technology

Post by sec »

wilbert wrote:
sec wrote:Hi wilbert, do you know why LogicalProcessorsPerPhysical is "16" on my CPU? is it not correct on this cpu: http://ark.intel.com/products/69669/
That seems incorrect.
I updated my code above to check for the HTT flags first. Does it still return 16 ?
It looks like if that flag is not set, you can't rely on bits 23:16 . I updated the code to return 1 in that case but for your cpu that also doesn't seem correct since it has 2 cores.
On my Core2Duo it reports 2 as it should.
yes, it is still return 16.

I am tried another lib: http://prdownloads.sourceforge.net/libc ... p?download it's wrong too :shock:
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Check hyper thread technology

Post by wilbert »

sec wrote:I am tried another lib: http://prdownloads.sourceforge.net/libc ... p?download it's wrong too :shock:
Does the code below show the right amount of physical cores ?

Code: Select all

Procedure.a LogicalProcessorsPerPhysical()
  CompilerIf #PB_Compiler_Processor = #PB_Processor_x64
    !push rbx
  CompilerElse
    !push ebx
  CompilerEndIf
  !mov eax, 1
  !cpuid
  !mov eax, 1
  !bt edx, 26
  !jnc lproc_per_physical_exit
  !bswap ebx
  !movzx eax, bh
  !lproc_per_physical_exit:
  CompilerIf #PB_Compiler_Processor = #PB_Processor_x64
    !pop rbx
  CompilerElse
    !pop ebx
  CompilerEndIf
  ProcedureReturn 
EndProcedure

Procedure.a ProcessorCores()
  CompilerIf #PB_Compiler_Processor = #PB_Processor_x64
    !push rbx
  CompilerElse
    !push ebx
  CompilerEndIf
  !mov eax, 0
  !cpuid
  ; GenuineIntel ?
  !xor ebx, 0x756e6547; Genu
  !xor ecx, 0x6c65746e; ntel
  !xor edx, 0x49656e69; ineI
  !mov eax, ebx
  !or eax, ecx
  !or eax, edx
  !jz proc_cores_genuine_intel
  !xor ebx, 0x756e6547; Genu
  !xor ecx, 0x6c65746e; ntel
  !xor edx, 0x49656e69; ineI  
  ;AuthenticAMD ?
  !xor ebx, 0x68747541; Auth
  !xor ecx, 0x444d4163; cAMD
  !xor edx, 0x69746e65; enti  
  !mov eax, ebx
  !or eax, ecx
  !or eax, edx
  !jz proc_cores_authentic_amd
  !xor eax, eax
  !jmp proc_cores_exit
  !proc_cores_genuine_intel:
  !mov eax, 4
  !cpuid
  !shr eax, 26
  !and eax, 0x3f
  !jmp proc_cores_exit
  !proc_cores_authentic_amd:
  !mov eax, 0x80000008
  !cpuid
  !movzx eax, cl
  !proc_cores_exit:
  !inc eax
  CompilerIf #PB_Compiler_Processor = #PB_Processor_x64
    !pop rbx
  CompilerElse
    !pop ebx
  CompilerEndIf
  ProcedureReturn 
EndProcedure

Procedure.a HasHTT()
  ProcedureReturn Bool(LogicalProcessorsPerPhysical() > ProcessorCores())
EndProcedure


Debug ProcessorCores()
Windows (x64)
Raspberry Pi OS (Arm64)
sec
Enthusiast
Enthusiast
Posts: 792
Joined: Sat Aug 09, 2003 3:13 am
Location: 90-61-92 // EU or ASIA
Contact:

Re: Check hyper thread technology

Post by sec »

wilbert wrote:
sec wrote:I am tried another lib: http://prdownloads.sourceforge.net/libc ... p?download it's wrong too :shock:
Does the code below show the right amount of physical cores ?

Code: Select all

Procedure.a LogicalProcessorsPerPhysical()
  CompilerIf #PB_Compiler_Processor = #PB_Processor_x64
    !push rbx
  CompilerElse
    !push ebx
  CompilerEndIf
  !mov eax, 1
  !cpuid
  !mov eax, 1
  !bt edx, 26
  !jnc lproc_per_physical_exit
  !bswap ebx
  !movzx eax, bh
  !lproc_per_physical_exit:
  CompilerIf #PB_Compiler_Processor = #PB_Processor_x64
    !pop rbx
  CompilerElse
    !pop ebx
  CompilerEndIf
  ProcedureReturn 
EndProcedure

Procedure.a ProcessorCores()
  CompilerIf #PB_Compiler_Processor = #PB_Processor_x64
    !push rbx
  CompilerElse
    !push ebx
  CompilerEndIf
  !mov eax, 0
  !cpuid
  ; GenuineIntel ?
  !xor ebx, 0x756e6547; Genu
  !xor ecx, 0x6c65746e; ntel
  !xor edx, 0x49656e69; ineI
  !mov eax, ebx
  !or eax, ecx
  !or eax, edx
  !jz proc_cores_genuine_intel
  !xor ebx, 0x756e6547; Genu
  !xor ecx, 0x6c65746e; ntel
  !xor edx, 0x49656e69; ineI  
  ;AuthenticAMD ?
  !xor ebx, 0x68747541; Auth
  !xor ecx, 0x444d4163; cAMD
  !xor edx, 0x69746e65; enti  
  !mov eax, ebx
  !or eax, ecx
  !or eax, edx
  !jz proc_cores_authentic_amd
  !xor eax, eax
  !jmp proc_cores_exit
  !proc_cores_genuine_intel:
  !mov eax, 4
  !cpuid
  !shr eax, 26
  !and eax, 0x3f
  !jmp proc_cores_exit
  !proc_cores_authentic_amd:
  !mov eax, 0x80000008
  !cpuid
  !movzx eax, cl
  !proc_cores_exit:
  !inc eax
  CompilerIf #PB_Compiler_Processor = #PB_Processor_x64
    !pop rbx
  CompilerElse
    !pop ebx
  CompilerEndIf
  ProcedureReturn 
EndProcedure

Procedure.a HasHTT()
  ProcedureReturn Bool(LogicalProcessorsPerPhysical() > ProcessorCores())
EndProcedure


Debug ProcessorCores()
No, it shows 8 :shock:

I try query by WMIC in cmd:

Code: Select all

D:\WMIC CPU Get NumberOfCores,NumberOfLogicalProcessors /Format:List

NumberOfCores=2
NumberOfLogicalProcessors=2
User avatar
oryaaaaa
Addict
Addict
Posts: 825
Joined: Mon Jan 12, 2004 11:40 pm
Location: Okazaki, JAPAN

Re: Check hyper thread technology

Post by oryaaaaa »

now, I challenged. Is there is an issue with AMD?

Code: Select all

Procedure.b CheckHTT()
  ; Check Hyper thread technology
  Protected htt.l
  !MOV Eax, 1
  !CPUID
  !MOV [p.v_htt], Edx
  ProcedureReturn (htt >> 28) & 1
EndProcedure

Procedure.b CountCPUsHTT()
  If CheckHTT()
    ProcedureReturn Int(CountCPUs() /2)
  Else
    ProcedureReturn CountCPUs()
  EndIf
EndProcedure

debug CountCPUsHTT()
Core i7 Q720 (4coresHT) is 8.

Code: Select all

Procedure.a HasHTT()
  ProcedureReturn Bool(LogicalProcessorsPerPhysical() > ProcessorCores())
EndProcedure

Debug ProcessorCores()
Helle
Enthusiast
Enthusiast
Posts: 178
Joined: Wed Apr 12, 2006 7:59 pm
Location: Germany
Contact:

Re: Check hyper thread technology

Post by Helle »

For AMD and Intel:

Code: Select all

j = 1
hThread = GetCurrentThread_()
OldThreadAffinityMask = SetThreadAffinityMask_(hThread, j)
While SetThreadAffinityMask_(hThread, j)
  Cores + 1
  !MOV eax,1
  !CPUID
  !AND ebx,1000000h          ;Initial APIC ID. Bit24: 0=phys., 1=log.Core
  !JNZ @f
  APIC_CoreSequence$ + "P"
  PhysCores + 1
  !JMP NextCore
!@@:
  HT$ = "Yes"
  APIC_CoreSequence$ + "L"
  LogCores + 1
!NextCore:
  j << 1
Wend
SetThreadAffinityMask_(hThread, OldThreadAffinityMask)     ;Restore

If LogCores
  HT$ = "Yes"
 Else
  HT$ = "No"
EndIf

Debug Cores
Debug PhysCores
Debug LogCores
Debug APIC_CoreSequence$
Debug HT$
Post Reply