Differenze tra le versioni di "Ottenere alcune informazioni sulla CPU mediante l'API di Libcpuid"

Da Gambas-it.org - Wikipedia.
(Creata pagina con "'''Libcpuid''' è una piccola libreria, scritta in C, per la rilevazione e per l'estrazione delle caratteristiche della CPU x86. Per poter utilizzare le risorse di detta lib...")
 
 
(9 versioni intermedie di uno stesso utente non sono mostrate)
Riga 1: Riga 1:
 
'''Libcpuid''' è una piccola libreria, scritta in C, per la rilevazione e per l'estrazione delle caratteristiche della CPU x86.
 
'''Libcpuid''' è una piccola libreria, scritta in C, per la rilevazione e per l'estrazione delle caratteristiche della CPU x86.
  
 +
Per poter utilizzare le risorse di detta libreria è necessario richiamare in Gambas ed avere installa nel proprio sistema la libreria condivisa: "''libcpuid.so.15.0.0'' ".
  
Per poter utilizzare le risorse di detta libreria è necessario richiamare in Gambas ed avere installa nel proprio sistema la libreria dinamica condivisa: "''libcpuid.so.14.0.0''"
+
Mostriamo un semplice esempio pratico:   <SUP>[ [[#Note|nota 1]] ]</sup>
 
+
  Library "libcpuid:15.0.0"
 
 
Mostriamo un semplice esempio pratico:
 
  Library "libcpuid:14.0.0"
 
 
   
 
   
 
  Public Struct cpu_raw_data_t  
 
  Public Struct cpu_raw_data_t  
Riga 15: Riga 13:
 
   intel_fn12h[4, 4] As Integer
 
   intel_fn12h[4, 4] As Integer
 
   intel_fn14h[4, 4] As Integer
 
   intel_fn14h[4, 4] As Integer
End Struct
 
 
Public Struct cpu_sgx_t
 
  present As Integer
 
  max_enclave_32bit As Byte
 
  max_enclave_64bit As Byte
 
  flags[14] As Byte
 
  num_epc_sections As Integer
 
  misc_select As Integer
 
  secs_attributes As Long
 
  secs_xfrm As Long
 
End Struct
 
 
Public Struct cpu_id_t
 
  vendor_str[16] As Byte
 
  brand_str[64] As Byte
 
  vendor As Integer
 
  flags[128] As Byte
 
  family As Integer
 
  model As Integer
 
  stepping As Integer
 
  ext_family As Integer
 
  ext_model As Integer
 
  num_cores As Integer
 
  num_logical_cpus As Integer
 
  total_logical_cpus As Integer
 
  l1_data_cache As Integer
 
  l1_instruction_cache As Integer
 
  l2_cache As Integer
 
  l3_cache As Integer
 
  l4_cache As Integer
 
  l1_assoc As Integer
 
  l2_assoc As Integer
 
  l3_assoc As Integer
 
  l4_assoc As Integer
 
  l1_cacheline As Integer
 
  l2_cacheline As Integer
 
  l3_cacheline As Integer
 
  l4_cacheline As Integer
 
  cpu_codename[64] As Byte
 
  sse_size As Integer
 
  detection_hints[16] As Byte
 
  sgx As Struct Cpu_sgx_t
 
 
  End Struct
 
  End Struct
 
   
 
   
Riga 79: Riga 34:
 
  <FONT Color=gray>' ''int cpu_identify(struct cpu_raw_data_t* raw, struct cpu_id_t* data)''
 
  <FONT Color=gray>' ''int cpu_identify(struct cpu_raw_data_t* raw, struct cpu_id_t* data)''
 
  ' ''Identifies the CPU.''</font>
 
  ' ''Identifies the CPU.''</font>
  Private Extern cpu_identify(raw As Cpu_raw_data_t, data As Cpu_id_t) As Integer
+
  Private Extern cpu_identify(raw As Cpu_raw_data_t, data As Pointer) As Integer
 
   
 
   
 
  <FONT Color=gray>' ''void cpu_tsc_mark(struct cpu_mark_t* mark)''
 
  <FONT Color=gray>' ''void cpu_tsc_mark(struct cpu_mark_t* mark)''
Riga 105: Riga 60:
 
  Private Extern cpu_clock_measure(millis As Integer, quad_check As Integer) As Integer
 
  Private Extern cpu_clock_measure(millis As Integer, quad_check As Integer) As Integer
 
   
 
   
 +
<FONT Color=gray>' ''const char * cpu_feature_str(cpu_feature_t feature)''
 +
' ''Returns the short textual representation Of a CPU flag.''</font>
 +
Private Extern cpu_feature_str(feature As Integer) As String
 +
 +
 +
Public Sub Main()
 
   
 
   
'''Public''' Sub Main()
 
 
 
 
   Dim raw As New Cpu_raw_data_t  
 
   Dim raw As New Cpu_raw_data_t  
   Dim data As New Cpu_id_t
+
   Dim data As Pointer
 
   Dim mark As New Cpu_mark_t
 
   Dim mark As New Cpu_mark_t
 +
  Dim au As String
 +
  Dim cpu_vendor As String[]
 +
  Dim i As Integer
 +
 
 +
  cpu_vendor = ["Intel CPU", "AMD CPU", "Cyrix CPU", "NexGen CPU", "Transmeta CPU", "x86 CPU by UMC", "x86 CPU by IDT",
 +
                "x86 CPU by Rise Technology", "x86 CPU by SiS", "x86 CPU by National Semiconductor", ""]
 +
   
 +
  If Not cpuid_present() Then Error.Raise("La CPUID non è supportata !")
 
    
 
    
  If Not cpuid_present() Then Error.Raise("La CPUID non è supportata !")
+
  If cpuid_get_raw_data(raw) < 0 Then
   
+
    Error.Raise("Impossibile ottenere dati grezzi CPUID !\nError: " & cpuid_error())
  If cpuid_get_raw_data(raw) < 0 Then
+
  Endif
    Error.Raise("Impossibile ottenere dati grezzi CPUID !\nError: " & cpuid_error())
+
 
  Endif
+
  data = Alloc(SizeOf(gb.Byte), 432)
 +
 
 +
  If cpu_identify(raw, data) < 0 Then
 +
    Error.Raise("Impossibile identificare la CPU !\nError: " & cpuid_error())
 +
  Endif
 
    
 
    
  If cpu_identify(raw, data) < 0 Then
+
  cpu_tsc_mark(mark)
    Error.Raise("Impossibile identificare la CPU !\nError: " & cpuid_error())
+
  cpu_tsc_unmark(mark)
  Endif
 
   
 
  cpu_tsc_mark(mark)
 
  cpu_tsc_unmark(mark)
 
   
 
  Print "  \"VENDOR_STR\":        "; String@(data.vendor_str.data)
 
  Print "  \"CPU_CODENAME\":      "; String@(data.cpu_codename.data)
 
  Print "  \"BRAND_STR\":          "; String@(data.brand_str.data)
 
  Print "  \"NUM_CORES\":          "; data.num_cores
 
  Print "  \"NUM_LOGICAL_CPUS\":  "; data.num_logical_cpus
 
  Print "  \"TOTAL_LOGICAL_CPUS\": "; data.total_logical_cpus
 
  Print "  \"FAMILY\":            "; data.family
 
  Print "  \"MODEL\":              "; data.model
 
  Print "  \"STEPPING\":          "; data.stepping
 
  Print "  \"EXT_FAMILY\":        "; data.ext_family
 
  Print "  \"EXT_MODEL\":          "; data.ext_model
 
  Print "  \"CPU_CLOCK\":          "; cpu_clock()
 
  Print "  \"CPU_CLOCK_BY_OS\":    "; cpu_clock_by_os(); " Mhz"
 
  Print "  \"CPU_CLOCK_BY_IC\":    "; cpu_clock_by_ic(25, 16); " Mhz"
 
  Print "  \"CPU_CLOCK_MEASURE\":  "; cpu_clock_measure(400, 1); " Mhz"
 
  Print "  \"MARK_TSC\":          "; mark.tsc
 
  Print "  \"MARK_SYS_CLOCK\":    "; mark.sys_clock
 
 
    
 
    
  '''End'''
+
  Print "  \"VENDOR_STR\":          "; String@(data)
 +
  Print "  \"CPU_CODENAME\":        "; String@(data + 296)
 +
  Print "  \"BRAND_STR\":            "; String@(data + 16)
 +
  Print "  \"VENDOR\":              "; IIf(Int@(data + 80) = -1, "sconosciuto", cpu_vendor[Int@(data + 80)])
 +
  Print "  \"FAMILY\":              "; Int@(data + 212)
 +
  Print "  \"MODEL\":                "; Int@(data + 216)
 +
  Print "  \"STEPPING\":            "; Int@(data + 220)
 +
  Print "  \"EXT_FAMILY\":          "; Int@(data + 224)
 +
  Print "  \"EXT_MODEL\":            "; Int@(data + 228)
 +
  Print "  \"NUM_CORES\":            "; Int@(data + 232)
 +
  Print "  \"NUM_LOGICAL_CPUS\":    "; Int@(data + 236)
 +
  Print "  \"TOTAL_LOGICAL_CPUS\":  "; Int@(data + 240)
 +
  Print "  \"L1_DATA_CACHE\":        "; Int@(data + 244); " KB"
 +
  Print "  \"L1_INSTRUCTION_CACHE\": "; Int@(data + 248); " KB"
 +
  Print "  \"L2_CACHE\":            "; Int@(data + 252); " KB"
 +
  Print "  \"L3_CACHE\":            "; Int@(data + 256); " KB"
 +
  Print "  \"L4_CACHE\":            "; Int@(data + 260); " KB"
 +
  Print "  \"L1_ASSOC\":            "; IIf(Int@(data + 264) > 0, Int@(data + 264) & "-way", "intedeterminato")
 +
  Print "  \"L2_ASSOC\":            "; IIf(Int@(data + 268) > 0, Int@(data + 268) & "-way", "intedeterminato")
 +
  Print "  \"L3_ASSOC\":            "; IIf(Int@(data + 272) > 0, Int@(data + 272) & "-way", "intedeterminato")
 +
  Print "  \"L4_ASSOC\":            "; IIf(Int@(data + 276) > 0, Int@(data + 276) & "-way", "intedeterminato")
 +
  Print "  \"L1_CACHELINE\":        "; IIf(Int@(data + 280) > 0, Int@(data + 280) & " byte", "intedeterminato")
 +
  Print "  \"L2_CACHELINE\":        "; IIf(Int@(data + 284) > 0, Int@(data + 280) & " byte", "intedeterminato")
 +
  Print "  \"L3_CACHELINE\":        "; IIf(Int@(data + 288) > 0, Int@(data + 280) & " byte", "intedeterminato")
 +
  Print "  \"L4_CACHELINE\":        "; IIf(Int@(data + 292) > 0, Int@(data + 280) & " byte", "intedeterminato")
 +
  au = IIf(Byte@(data + 364), "(authoritative)", "(non-authoritative)")
 +
  Print "  \"SSE_SIZE\":            "; Int@(data + 360); " bit "; au
 +
  Print "  \"CPU_CLOCK\":            "; cpu_clock(); " Mhz"
 +
  Print "  \"CPU_CLOCK_BY_OS\":      "; cpu_clock_by_os(); " Mhz"
 +
  Print "  \"CPU_CLOCK_BY_IC\":      "; cpu_clock_by_ic(25, 16); " Mhz"
 +
  Print "  \"CPU_CLOCK_MEASURE\":    "; cpu_clock_measure(400, 1); " Mhz"
 +
  Print "  \"MARK_TSC\":            "; mark.tsc
 +
  Print "  \"MARK_SYS_CLOCK\":      "; "µs "; mark.sys_clock
 +
  For i = 0 To 109
 +
    Print "  \"FEATURE\":              "; cpu_feature_str(i); IIf(Byte@(data + (i + 84)), " Present", " Absent")
 +
  Next
 +
 
 +
  Free(data)
 +
 
 +
  End
 +
 
 +
 
  
 +
=Note=
 +
[1] Un ringraziamento al membro ''[http://www.gambas-it.org/smf/index.php?action=profile;u=1404 eloaders]'' del forum gambas.it.org per il suo aiuto.
  
  

Versione attuale delle 09:52, 28 lug 2023

Libcpuid è una piccola libreria, scritta in C, per la rilevazione e per l'estrazione delle caratteristiche della CPU x86.

Per poter utilizzare le risorse di detta libreria è necessario richiamare in Gambas ed avere installa nel proprio sistema la libreria condivisa: "libcpuid.so.15.0.0 ".

Mostriamo un semplice esempio pratico: [ nota 1 ]

Library "libcpuid:15.0.0"

Public Struct cpu_raw_data_t 
  basic_cpuid[32, 4] As Integer
  ext_cpuid[32, 4] As Integer
  intel_fn4[8, 4] As Integer
  intel_fn11[4, 4] As Integer
  intel_fn12h[4, 4] As Integer
  intel_fn14h[4, 4] As Integer
End Struct

Public Struct cpu_mark_t
  tsc As Long
  sys_clock As Long
End Struct

' int cpuid_present(void)
' Checks if the CPUID instruction is supported.
Private Extern cpuid_present() As Integer

' int cpuid_get_raw_data(struct cpu_raw_data_t* data)
' Obtains the raw CPUID data from the current CPU.
Private Extern cpuid_get_raw_data(data As Cpu_raw_data_t) As Integer

' const char* cpuid_error(void)
' Returns textual description of the last error.
Private Extern cpuid_error() As String

' int cpu_identify(struct cpu_raw_data_t* raw, struct cpu_id_t* data)
' Identifies the CPU.
Private Extern cpu_identify(raw As Cpu_raw_data_t, data As Pointer) As Integer

' void cpu_tsc_mark(struct cpu_mark_t* mark)
' Store TSC and timing info.
Private Extern cpu_tsc_mark(mark As Cpu_mark_t)

' void cpu_tsc_unmark(struct cpu_mark_t* mark)
' Calculate TSC and timing difference.
Private Extern cpu_tsc_unmark(mark As Cpu_mark_t)

' int cpu_clock(void)
' Get the CPU clock frequency (all-in-one method).
Private Extern cpu_clock() As Integer

' int cpu_clock_by_os(void)
' Returns the CPU clock, as reported by the OS.
Private Extern cpu_clock_by_os() As Integer

' int cpu_clock_by_ic(int millis, int runs)
' Measure the CPU clock frequency using instruction-counting.
Private Extern cpu_clock_by_ic(millis As Integer, runs As Integer) As Integer

' int cpu_clock_measure(int millis, int quad_check)
' Measure the CPU clock frequency.
Private Extern cpu_clock_measure(millis As Integer, quad_check As Integer) As Integer

' const char * cpu_feature_str(cpu_feature_t feature)
' Returns the short textual representation Of a CPU flag.
Private Extern cpu_feature_str(feature As Integer) As String


Public Sub Main()

 Dim raw As New Cpu_raw_data_t 
 Dim data As Pointer
 Dim mark As New Cpu_mark_t
 Dim au As String
 Dim cpu_vendor As String[]
 Dim i As Integer
 
 cpu_vendor = ["Intel CPU", "AMD CPU", "Cyrix CPU", "NexGen CPU", "Transmeta CPU", "x86 CPU by UMC", "x86 CPU by IDT",
               "x86 CPU by Rise Technology", "x86 CPU by SiS", "x86 CPU by National Semiconductor", ""]
    
 If Not cpuid_present() Then Error.Raise("La CPUID non è supportata !")
  
 If cpuid_get_raw_data(raw) < 0 Then
   Error.Raise("Impossibile ottenere dati grezzi CPUID !\nError: " & cpuid_error())
 Endif
 
 data = Alloc(SizeOf(gb.Byte), 432)
 
 If cpu_identify(raw, data) < 0 Then
   Error.Raise("Impossibile identificare la CPU !\nError: " & cpuid_error())
 Endif
  
 cpu_tsc_mark(mark)
 cpu_tsc_unmark(mark)
  
 Print "   \"VENDOR_STR\":           "; String@(data)
 Print "   \"CPU_CODENAME\":         "; String@(data + 296)
 Print "   \"BRAND_STR\":            "; String@(data + 16)
 Print "   \"VENDOR\":               "; IIf(Int@(data + 80) = -1, "sconosciuto", cpu_vendor[Int@(data + 80)])
 Print "   \"FAMILY\":               "; Int@(data + 212)
 Print "   \"MODEL\":                "; Int@(data + 216)
 Print "   \"STEPPING\":             "; Int@(data + 220)
 Print "   \"EXT_FAMILY\":           "; Int@(data + 224)
 Print "   \"EXT_MODEL\":            "; Int@(data + 228)
 Print "   \"NUM_CORES\":            "; Int@(data + 232)
 Print "   \"NUM_LOGICAL_CPUS\":     "; Int@(data + 236)
 Print "   \"TOTAL_LOGICAL_CPUS\":   "; Int@(data + 240)
 Print "   \"L1_DATA_CACHE\":        "; Int@(data + 244); " KB"
 Print "   \"L1_INSTRUCTION_CACHE\": "; Int@(data + 248); " KB"
 Print "   \"L2_CACHE\":             "; Int@(data + 252); " KB"
 Print "   \"L3_CACHE\":             "; Int@(data + 256); " KB"
 Print "   \"L4_CACHE\":             "; Int@(data + 260); " KB"
 Print "   \"L1_ASSOC\":             "; IIf(Int@(data + 264) > 0, Int@(data + 264) & "-way", "intedeterminato")
 Print "   \"L2_ASSOC\":             "; IIf(Int@(data + 268) > 0, Int@(data + 268) & "-way", "intedeterminato")
 Print "   \"L3_ASSOC\":             "; IIf(Int@(data + 272) > 0, Int@(data + 272) & "-way", "intedeterminato")
 Print "   \"L4_ASSOC\":             "; IIf(Int@(data + 276) > 0, Int@(data + 276) & "-way", "intedeterminato")
 Print "   \"L1_CACHELINE\":         "; IIf(Int@(data + 280) > 0, Int@(data + 280) & " byte", "intedeterminato")
 Print "   \"L2_CACHELINE\":         "; IIf(Int@(data + 284) > 0, Int@(data + 280) & " byte", "intedeterminato")
 Print "   \"L3_CACHELINE\":         "; IIf(Int@(data + 288) > 0, Int@(data + 280) & " byte", "intedeterminato")
 Print "   \"L4_CACHELINE\":         "; IIf(Int@(data + 292) > 0, Int@(data + 280) & " byte", "intedeterminato")
 au = IIf(Byte@(data + 364), "(authoritative)", "(non-authoritative)")
 Print "   \"SSE_SIZE\":             "; Int@(data + 360); " bit "; au
 Print "   \"CPU_CLOCK\":            "; cpu_clock(); " Mhz"
 Print "   \"CPU_CLOCK_BY_OS\":      "; cpu_clock_by_os(); " Mhz"
 Print "   \"CPU_CLOCK_BY_IC\":      "; cpu_clock_by_ic(25, 16); " Mhz" 
 Print "   \"CPU_CLOCK_MEASURE\":    "; cpu_clock_measure(400, 1); " Mhz"
 Print "   \"MARK_TSC\":             "; mark.tsc
 Print "   \"MARK_SYS_CLOCK\":       "; "µs "; mark.sys_clock
 For i = 0 To 109
   Print "   \"FEATURE\":              "; cpu_feature_str(i); IIf(Byte@(data + (i + 84)), " Present", " Absent")
 Next
 
 Free(data)
 
End


Note

[1] Un ringraziamento al membro eloaders del forum gambas.it.org per il suo aiuto.


Riferimenti