Jump to content
Sign in to follow this  
francoiste

How to check for CPU capabilities - AVX2?

Recommended Posts

in AutoIt we have:

_WinAPI_IsProcessorFeaturePresent($iFeature)

this internally just calls what is provided by microsoft: https://msdn.microsoft.com/en-us/library/windows/desktop/ms724482(v=vs.85).aspx

unfortunately this is completely outdated as we can only check for features like MMX and up to a maximum of SSE2 or SSE3 - which have been introduced back in 2001 / 2004.
but nowadays there is stuff like AVX, AVX2, AVX-512 ...
for example see: https://en.wikipedia.org/wiki/Template:Multimedia_extensions

well, for CPP we have the FeatureDetector (by Mysticial): https://github.com/Mysticial/FeatureDetector
it calls __cpuid / __cpuidex from <intrin.h>.
for additional details see: https://msdn.microsoft.com/en-us/library/hskdteyh.aspx

 

has anyone tried to determine such SIMD features and related CPU capabilites with AutoIt?

Edited by francoiste

Share this post


Link to post
Share on other sites

So I was kinda interested in this after reading your question

Looking through the forum I found one user who had a similar question 'Andreik' 

That is pretty cool I didn't even realize we could do assembly in AutoIt!!

I took his asm and altered it a bit to allow us to send a value to eax and get back the result of cpuid

#include <Memory.au3>
#include <String.au3>

MsgBox(0, "", GetVendorID() & " : " & CpuId_Vendor() & @CRLF & CpuId_Processor_Brand())

Func Cpuid_Get_Leaf($iLeaf, $iSubLeaf = 0)

    Local Const $sCode32 =  "0x"     & _ ; use32
                            "55"     & _ ; push ebp
                            "89E5"   & _ ; mov ebp, esp
                            "53"     & _ ; push ebx
                            "8B4508" & _ ; mov eax, [ebp + 08] ;$iLeaf
                            "8B4D0C" & _ ; mov ecx, [epb + 12] ;$iSubLeaf
                            "31DB"   & _ ; xor ebx, ebx ; set ebx = 0
                            "31D2"   & _ ; xor edx, edx ; set edx = 0
                            "0FA2"   & _ ; cpuid
                            "8B6D10" & _ ; mov ebp, [ebp + 16] ;ptr int[4]
                            "894500" & _ ; mov [ebp + 00], eax
                            "895D04" & _ ; mov [edi + 04], ebx
                            "894D08" & _ ; mov [edi + 08], ecx
                            "89550C" & _ ; mov [edi + 12], edx
                            "5B"     & _ ; pop ebx
                            "5D"     & _ ; pop ebp
                            "C3"         ; ret

    Local Const $sCode64 =  "0x"         & _ ; use 64
                            "53"         & _ ; push rbx
                            "89C8"       & _ ; mov  eax, ecx ;$ileaf
                            "89D1"       & _ ; mov  ecx, edx ;$iSubleaf
                            "31DB"       & _ ; xor  ebx, ebx
                            "31D2"       & _ ; xor  edx, edx
                            "0FA2"       & _ ; cpuid
                            "67418900"   & _ ; mov  [r8d], eax ;ptr int[4]
                            "6741895804" & _ ; mov  [r8d + 04], ebx
                            "6741894808" & _ ; mov  [r8d + 08], ecx
                            "674189500C" & _ ; mov  [r8d + 12], edx
                            "5B"         & _ ; pop rbx
                            "C3"             ; ret


    Local Const $sCode = @AutoItX64 ? $sCode64 : $sCode32
    Local Const $iSize = BinaryLen($sCode)
    Local $aE_X[4] = [0, 0, 0, 0]
    Local $iErr

    Do
        $iErr = 1
        Local $pBuffer = _MemVirtualAlloc(0, $iSize, BitOR($MEM_COMMIT, $MEM_RESERVE), $PAGE_EXECUTE_READWRITE)
        If $pBuffer = 0 Then ExitLoop
        $iErr = 2
        DllStructSetData(DllStructCreate("BYTE[" & $iSize & "]", $pBuffer), 1, $sCode)
        If @error Then ExitLoop
        $iErr = 3
        Local $tRet = DllStructCreate("int EAX;int EBX;int ECX;int EDX;")
        If @error Then ExitLoop
        $iErr = 4
        Local $aRet = DllCallAddress("uint:cdecl", $pBuffer, "int", Int($iLeaf), "int", Int($iSubLeaf), "ptr", DllStructGetPtr($tRet))
        If @error Then ExitLoop
        $iErr = 0;Success?
    Until(True)

    _MemVirtualFree($pBuffer, $iSize, $MEM_DECOMMIT)

    ConsoleWrite($iErr <> 0 ? "Error " & $iErr & @CRLF : Hex($aRet[0], 8) & " Leaf: 0x" & Hex($iLeaf, 8) & @CRLF)

    For $i = 0 To $iErr <> 0 ? -1 : UBound($aE_X) - 1
        $aE_X[$i] = "0x" & Hex(DllStructGetData($tRet, $i + 1))
        ConsoleWrite("E" & Chr(65 + $i) & "X: " & $aE_X[$i] & @CRLF)
    Next

    Return SetError($iErr, @error, $aE_X)
EndFunc

Func CpuId_Vendor()
    $aCPUID = Cpuid_Get_Leaf(0x0)
    Return RevBinStr($aCPUID[1]) & RevBinStr($aCPUID[3]) & RevBinStr($aCPUID[2])
EndFunc

Func CpuId_Processor_Brand()
    Local $sPBS = "NOT SUPPORT"
    $aCPUID = Cpuid_Get_Leaf(0x80000000) ;need to get max extended value first
    If $aCPUID[0] < 0x80000004 Then Return $sPBS
    $aCPUID = Cpuid_Get_Leaf(0x80000002)
    $sPBS = RevBinStr($aCPUID[0]) & RevBinStr($aCPUID[1]) & RevBinStr($aCPUID[2]) & RevBinStr($aCPUID[3])
    $aCPUID = Cpuid_Get_Leaf(0x80000003)
    $sPBS &= RevBinStr($aCPUID[0]) & RevBinStr($aCPUID[1]) & RevBinStr($aCPUID[2]) & RevBinStr($aCPUID[3])
    $aCPUID = Cpuid_Get_Leaf(0x80000004)
    $sPBS &= RevBinStr($aCPUID[0]) & RevBinStr($aCPUID[1]) & RevBinStr($aCPUID[2]) & RevBinStr($aCPUID[3])
    Return StringStripWS($sPBS, 7)
EndFunc

Func RevBinStr($val)
    Local $rev
    For $n = BinaryLen($val) To 1 Step -1
        $rev &= Hex(BinaryMid($val, $n, 1))
    Next
    Return BinaryToString("0x" & $rev)
EndFunc

Func Get_BitGroup_Dword($iDword, $iLsb, $iMsb) 
    ;EX - Get_BitGroup_Dword(0xFEDCBA98, 0, 3) => 0x8
    ;EX - Get_BitGroup_Dword(0xFEDCBA98, 3, 3) => 0x1
    ;EX - Get_BitGroup_Dword(0xFEDCBA98, 28, 31) => 0xF
    ;EX - Get_BitGroup_Dword(0xFEDCBA98, 24, 27) => 0xE
    Local $iVal1 = BitShift($iDword, $iLsb) ;>>
    Local Const $iMask = 0xFFFFFFFF
    Local $iVal2 = BitNOT(BitShift($iMask, ($iLsb-$iMsb-1))) ;~<<
    Return BitAND($iVal1, $iVal2)
EndFunc

Func GetVendorID() ;Andreik
    if @AutoItX64 then return "32Bit ONLY"

    $Code = "0x"            & _    ; use32
            "55"            & _    ; push ebp
            "89E5"          & _    ; mov ebp, esp
            "60"            & _    ; pushad
            "B800000000"    & _    ; mov eax,0
            "0FA2"          & _    ; cpuid
            "8B7D08"        & _    ; mov edi, [ebp + 08]
            "891F"          & _    ; mov [edi], ebx
            "895704"        & _    ; mov [edi + 4], edx
            "894F08"        & _    ; mov [edi + 8], ecx
            "B000"          & _    ; mov al, 0
            "88470C"        & _    ; mov [edi + 12], al
            "61"            & _    ; popad
            "5D"            & _    ; pop ebp
            "C20400"               ; ret 4
    $iSize = BinaryLen($Code)
    $pBuffer = _MemVirtualAlloc(0,$iSize,$MEM_COMMIT,$PAGE_EXECUTE_READWRITE)
    $tBuffer = DllStructCreate("byte Code[" & $iSize & "]",$pBuffer)
    DllStructSetData($tBuffer,"Code",$Code)
    $aRet = DllCallAddress("int",$pBuffer,"str","")
    _MemVirtualFree($pBuffer,$iSize,$MEM_DECOMMIT)
    Return $aRet[1]
EndFunc

So you can see how to use it I matched his Version ID call for an example

but you can also call different Leaf IDs and get the appropriate returns to parse what you desire out of eax, ebx, ecx, edx;

Do note when: $iLeaf = 0x0 highest supported ID = EAX and Version ID = EBX, EDX, ECX

See: https://en.wikipedia.org/wiki/CPUID for a list of leaf IDs and the return values you need

 

for instance 0x80000000 would return the highest supported Extended Leaf ID

0x80000002 then 0x80000003 then 0x80000004 would get you the Processor Brand String

Edited by Bilgus
Realized a need to zero ECX register, added Functionality, 64 Bit Thanks LARSJ!

Share this post


Link to post
Share on other sites

I expanded the example a bit to add Processor brand and to get the endianess right;

apparently the bytes need reversed for autoit to display them properly..

I originally was passing arrays of bytes to the assembly code function to get Autoit to display the strings correctly.

but I realized as I was looking through the returned values that the endian was reversed to what was shown on the Wiki page.

Once I fixed that the strings were then reversed so I added a RevBinStr function to handle it

In order to get the other values you'll need to pass your EAX value ($iLeaf) and check for set bits

BitAND($aCPUID[?],1) or BitAND($aCPUID[?],2) .... BitAND($aCPUID[?],32)

 

Share this post


Link to post
Share on other sites

francoiste I see you edited your question a bit but what I have above should do what you want 

example for EAX = 7 would be something like this

Func CpuId_EFF()
    ;$aCPUID;; [0] = EAX, [1] = EBX, [2] = ECX, [3] = EDX
    Local $sRet = "NOT SUPPORT"
    $aCPUID = Cpuid_Get_Leaf(0x0) ;need to get max extended value first
    If $aCPUID[0] < 0x7 Then Return $sRet
    $aCPUID = Cpuid_Get_Leaf(0x7) ;This returns extended feature flags in EBX, ECX, and EDX.
    ;EBX------------------------------------------------------
    if BitAND($aCPUID[1], 3) <> 0 then $sRet &= "sgx "
    ;..............................................
    if BitAND($aCPUID[1], 31) <> 0 then $sRet &= "avx512bw "
    if BitAND($aCPUID[1], 32) <> 0 then $sRet &= "avx512vl "
    ;ECX-------------------------------------------------------
    if BitAND($aCPUID[2], 2) <> 0 then $sRet &= "avx512vbmi "
    ;..............................................
    if BitAND($aCPUID[2], 7) <> 0 then $sRet &= "avx512vbmi2 "
    ;..............................................
    if BitAND($aCPUID[2], 11) <> 0 then $sRet &= "avx512vnni "
    ;EDX-------------------------------------------------------
    if BitAND($aCPUID[3], 3) <> 0 then $sRet &= "avx512_4vnniw "
    if BitAND($aCPUID[3], 4) <> 0 then $sRet &= "avx512_4fmaps "
    if BitAND($aCPUID[3], 27) <> 0 then $sRet &= "spec_ctrl "
    Return $sRet
EndFunc

Now this is just an example I think I'd probably make an array of strings or maybe an indexed string of some sort to clean it up a bit but you get the idea

Note: the bit values are all one higher than the wiki page since we are supplying the actual value of the number at that bit rather than the bit position

Share this post


Link to post
Share on other sites

The easiest way to write assembler code in AutoIt is to use flat assembler because there is a fasm.dll that contains all the functions you need.

In this post you can find a short instruction and a small but complete UDF to use the functions in fasm.dll.

Share this post


Link to post
Share on other sites

Reading over a more in-depth reference it appears with EAX = 7 and ECX =0 EAX recieves the max sub level for the command

I have expanded the function yet again with $iSubLeaf to allow this although I'm not sure how Important it is being I don't have any thing new

enough to test it on 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Irios
      Below is a function w/ a working example showing how to access the AIDA64 shared memory.
      First enable shared memory here: Preferences > Hardware Monitoring > External Applications > Enable Shared Memory
      The length of the data depends on the number of active sensors and their content. As AIDA64 does not provide a buffer size value, we use _WinAPI_GetSystemInfo() <WinAPISys.au3> to get dwPageSize ($aArray[1]). This value is the used as an address offset, and we continue reading chunks of data until we encounter a 0x00 NUL character.
      Based on the Delphi example found in the AIDA64 documentation: https://www.aida64.co.uk/user-manual/external-applications
      Feedback appreciated, especially as all this shared memory stuff is not my ordinary cup of... cake. And regarding this whole ordeal of reading data without knowing the exact length; I'm suspecting my solution to read chunks of data like done below is not by the book, and I'm concerned what might happen if the final chunk is exactly 4096 bytes in length. Will there still be a NUL terminator there? Hmm.
       
      #NoTrayIcon #include <WinAPIFiles.au3> #include <WinAPISys.au3> ; #INDEX# =========================================================================================================================== ; Title .........: AIDA64 Shared Memory access for AutoIt3 ; Author(s) .....: demux4555 ; Reference .....: https://www.aida64.co.uk/user-manual/external-applications ; =================================================================================================================================== Global $vSharedmem_data ; The variable used to store the data we read from the shared memory Global $return = ExtApp_SharedMem_ReadBuffer_v2($vSharedmem_data) ; Now, let's see what happens when we run the function... If @error Then _Echo("! ExtApp_SharedMem_ReadBuffer_v2(): @errror = " & @error) If IsBinary($vSharedmem_data) Then ; Convert type Binary to actual human readable text. We also remove the excess of trailing 0x00 characters. $vSharedmem_data = StringStripWS(BinaryToString($vSharedmem_data), $STR_STRIPLEADING+$STR_STRIPTRAILING) EndIf _Echo() _Echo("> return = " & $return) ; The return value. Will be True if everything went ok. _Echo("> length = " & StringLen($vSharedmem_data)) ; The number of characters read from shared memory. _Echo("> data = " & $vSharedmem_data) ; The actual data. _Echo("> data(40) = " & "..." & StringRight($vSharedmem_data, 40)) ; shows the 40 right-most characters, and _should_ show the very end of the data at this point. _Echo() Exit ; #FUNCTION# ==================================================================================================================== ; Name ..........: ExtApp_SharedMem_ReadBuffer_v2 ; Description ...: AIDA64 Shared Memory Example for AutoIt3 ; Syntax ........: ExtApp_SharedMem_ReadBuffer_v2(Byref $_dOutput[, $_sSharedmemName = "AIDA64_SensorValues"]) ; Parameters ....: $_dOutput - [in/out] Variable to store the read data. ; $_sSharedmemName - [optional] Name of the AIDA64 shared memory. Default is "AIDA64_SensorValues". ; Return values .: Success: True. $_dOutput will be type Binary, containing a string of the XML values of the active sensors. ; Failure: False. ; Author ........: demux4555 ; Reference .....: https://www.aida64.co.uk/user-manual/external-applications ; =============================================================================================================================== Func ExtApp_SharedMem_ReadBuffer_v2(ByRef $_dOutput, $_sSharedmemName = "AIDA64_SensorValues") Local $_bReturn = False Local $_aGSI = _WinAPI_GetSystemInfo() ; Retrieves information about the current system... Ref: https://docs.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-getsysteminfo Local $_iPageSize = $_aGSI[1] ; ... the page size and the granularity of page protection and commitment. Usually it is 4096, but we read it anyway. Ref: https://docs.microsoft.com/en-us/windows/win32/api/sysinfoapi/ns-sysinfoapi-system_info Local $_hMapping = _WinAPI_OpenFileMapping($_sSharedmemName, $FILE_MAP_READ) ; Opens a named file mapping object. Ref: https://docs.microsoft.com/en-us/windows/win32/api/memoryapi/nf-memoryapi-openfilemappingw If Not IsPtr($_hMapping) Then Return SetError(-2, 0, $_bReturn) Local $_pMappedData = _WinAPI_MapViewOfFile($_hMapping, 0, 0, $FILE_MAP_READ) ; Pointer to the start address. Maps a view of a file mapping into the address space of a calling process. Ref: https://docs.microsoft.com/en-us/windows/win32/api/memoryapi/nf-memoryapi-mapviewoffile If @error Or Not IsPtr($_pMappedData) Then Return SetError(-2, 0, $_bReturn) ; Now we loop until we reach the end of the data. Local $_tData, $_dBuffer While 1 $_tData = DllStructCreate("BYTE[" & $_iPageSize & "]", $_pMappedData) ; Note: we use type BYTE[] (AutoIt type Binary) instead of CHAR[] (AutoIt type String). This allows us to look for value 0x00 (NUL termination of the data). If @error Then ExitLoop $_dBuffer = DllStructGetData($_tData, 1) ; The returned value is type Binary. If @error Or ($_dBuffer==0) Or (BinaryLen($_dBuffer)=0) Then ExitLoop ; Pretty sure $_dBuffer==0 can not happen, so just in case. $_dOutput = Binary($_dOutput & $_dBuffer) ; Add the read data to the end of the output variable. If StringRight($_dBuffer, 2)=="00" Then ExitLoop ; Look for NUL termination of the string data. $_pMappedData += $_iPageSize ; We change the address by using the page granularity value (i.e. 4096) as the offset. WEnd ; Quick cleanup $_bReturn = _WinAPI_UnmapViewOfFile($_pMappedData)=1 ; Unmaps a mapped view of a file from the calling process's address space. Ref: https://docs.microsoft.com/en-us/windows/win32/api/memoryapi/nf-memoryapi-unmapviewoffile _WinAPI_CloseHandle($_hMapping) Return $_bReturn EndFunc; Func _Echo($_data = "") ConsoleWrite($_data & @CRLF) EndFunc  
       
    • By argumentum
      The back story:
      I've got a Dell XPS w/ i7-8700K. The fastest, by single core, I could get, by well known PC maker.
      The problem is that the fan can get so loud, like, REALLY LOUD, I can not use the CPU at its max. clock speed.
      I could leave it at 90% all the time and not use this but I want to have the full 4.x Ghz and no parked cores, at all times, if I can.
      But as room temperature and CPU load changes, a set throttle, may still make fan noise.
      The solution:
      To avoid the fan from going "airplane turbine mode", the utility gets the temp. from "Core Temp" ( you can google it )
      It has a"plug-in" called "Core Temp Remote Server". The utility gets the values via TCP.
      When it "feels" it's gonna get hot, drops the CPU throttle to a selected value, lets say 99% ( where is quieter ) and back up to 100% when it "feels" is ok to go back.
      Now temperature can creep up to higher than expected if load is sustained or room temperature changes. So there is an "anti creep up" feature, to temporarily set the throttle even lower, 5% at a time, until the known quiet temperature is achieved.
      If don't know how to find the temperature you should use, check out these videos. They will tell you how.
      https://www.youtube.com/watch?v=p3B5WCJZTuw&ab_channel=SergeantPope-KomadaComputerRepair
      https://www.youtube.com/watch?v=VuP6I0mOb1s&ab_channel=Techquickie
      https://www.google.com/search?q=find+max+cpu+temperature
      The end result:
      Any thermal problem, is a hardware problem. No way around that, other than attending to the CPU cooling and case ventilation. Software can not fix that.
      But without this utility, the PC would slow down the CPU anyway, to keep it from melting.
      This software preemptively slow down the CPU, keeping the CPU related fan speeds from going to maximum RPM. Hence having a slower, but a quieter box.
    • By red0fireus
      Hello, I'm very stumped with finding the decimal place for the CPU %.
      I found some example code online
      #include <Array.au3> $aProcess_Info = GetCPU("RzSynapse") _ArrayDisplay($aProcess_Info) Func GetCPU($sProcess = "") Local $aAllProcess_Info[1000][3] = [[0]], $aOneProcess_Info[3] $oWinMgmts = ObjGet("winmgmts:\\.\root\cimv2") $aCPU_Usage = $oWinMgmts.ExecQuery("SELECT Name, IDProcess, PercentProcessorTime FROM Win32_PerfFormattedData_PerfProc_Process") For $vCPU_Info In $aCPU_Usage If $sProcess Then If $vCPU_Info.Name = $sProcess Then $aOneProcess_Info[0] = $sProcess $aOneProcess_Info[1] = $vCPU_Info.IDProcess $aOneProcess_Info[2] = $vCPU_Info.PercentProcessorTime & " %" Return $aOneProcess_Info EndIf Else $aAllProcess_Info[0][0] += 1 $aAllProcess_Info[$aAllProcess_Info[0][0]][0] = $vCPU_Info.Name $aAllProcess_Info[$aAllProcess_Info[0][0]][1] = $vCPU_Info.IDProcess $aAllProcess_Info[$aAllProcess_Info[0][0]][2] = $vCPU_Info.PercentProcessorTime & " %.1f" EndIf Next ReDim $aAllProcess_Info[$aAllProcess_Info[0][0] +1][3] Return $aAllProcess_Info EndFunc ;==>GetCPU Currently the code shows the CPU % as 0 % and I want to show 0.0 % I found this online for strings "%.1f" but i'm not sure how to incorporate it.
    • By VIP
      The simple widget shows the percentage of cpu, ram used and the CPU temperature (WMI).
      - Every 30s check, sync and update time for computer.
      - Drag adn Drop any files on GUI for reset Attribute (-RAHS) and set Full Access to Everyone !
      - Righ Click on x and click Un/Install app to boot with Windows!
      CPU_Widget_TimeSync.exe CRC32: 1956339E MD5: BFC2B596BD1EDA35D6B02B97F8FB57AE SHA-1: 796A663739EA5AC9890D9FA324A98E987F35E7C8
      Function code from:
      CPU: 
      RAM:  MemGetStats ( )
      Memory:   _WinAPI_EmptyWorkingSet ()
       
    • By Wicked_Caty
      What am I supposed to do, if a program is able for a long time?
      My program is idle for 20 minutes, before doing a very tiny task, and then starting all over again.
      I tries using Sleep(), but that literally melts my CPU and pushes them up for 20 to 30%. Permanently checking the time costs too much CPU too.
      Any other way of doing this? Preferably without taking more than 1% CPU. It also needn't be exactly 20 minutes, everything between 15 and 25 is fine too
      Thanks!
×
×
  • Create New...