Conoscere la frequenza delle CPU mediante le funzioni esterne del API di Libcpufreq

Da Gambas-it.org - Wikipedia.

La libreria Libcpufreq consente di gestire le funzionalità del subsistema cpufreq del kernel di Linux.
In particolare consente di verificare le caratteristiche della CPU in ordine alla sua frequenza.

Per poter fruire delle sue risorse è necessario richiamare nel codice Gambas la libreria dinamica condivisa: libcpufreq:0.0.0


Mostriamo un semplice esempio:

Public Struct cpufreq_policy
  minS As Long
  maxS As Long
  governor As Pointer
End Struct


Library "libcpufreq:0.0.0"

' int cpufreq_cpu_exists(unsigned int cpu)
' Returns 0 if the specified CPU is present.
Private Extern cpufreq_cpu_exists(cpu As Integer) As Integer

' struct cpufreq_affected_cpus * cpufreq_get_affected_cpus(unsigned int cpu)
' Determines affected CPUs.
Private Extern cpufreq_get_affected_cpus(cpu As Integer) As Pointer

' unsigned long cpufreq_get_freq_kernel(unsigned int cpu)
' Determines current CPU frequency _kernel variant means kernel's opinion of CPU frequency.
Private Extern cpufreq_get_freq_kernel(cpu As Integer) As Long

' int cpufreq_get_hardware_limits(unsigned int cpu, unsigned long *min, unsigned long *max)
' Determines hardware CPU frequency limits.
Private Extern cpufreq_get_hardware_limits(cpu As Integer, minP As Pointer, maxP As Pointer) As Integer

' char * cpufreq_get_driver(unsigned int cpu)
' Determine CPUfreq driver used.
Private Extern cpufreq_get_driver(cpu As Integer) As String

' struct cpufreq_available_governors * cpufreq_get_available_governors(unsigned int cpu)
' Determines CPUfreq governors currently available.
Private Extern cpufreq_get_available_governors(cpu As Integer) As Pointer

' struct cpufreq_available_frequencies * cpufreq_get_available_frequencies(unsigned int cpu)
' Determines CPU frequency states available.
Private Extern cpufreq_get_available_frequencies(cpu As Integer) As Pointer

' struct cpufreq_policy * cpufreq_get_policy(unsigned int cpu)
' Determines CPUfreq policy currently used.
Private Extern cpufreq_get_policy(cpu As Integer) As Cpufreq_policy


Public Sub Main()

 Dim cpu, p As Pointer
 Dim b As Byte
 Dim mn, mx As Long
 Dim pol As New Cpufreq_policy
 
  While cpufreq_cpu_exists(b) = 0
    Inc b
  Wend
  Print "Sono presenti num. "; b;; "CPU\n"
  
  b = 0
     
  While True 
     
    cpu = cpufreq_get_affected_cpus(b)
    If IsNull(cpu) Then Exit
    b = Int@(cpu)
     
    Print "== Analisi della CPU n. "; b;; "=="
    cpufreq_get_hardware_limits(b, VarPtr(mn), VarPtr(mx))
    Print "Limiti hardware della frequenza:"
    Print Space(33); "Max. "; mx / 1000;; "Mhz\n"; Space(33); "Min. "; mn / 1000;; "Mhz"
    Print "\nDriver usato:                    "; cpufreq_get_driver(b)
    Print "\nRegolatori di frequenza:"
     
    p = cpufreq_get_available_governors(b)
    While IsNull(p) = False
      Print Space(33); String@(Pointer@(p))
      p = p + 8
      p = Pointer@(p)
    Wend
    
    With pol = cpufreq_get_policy(b)
      Print "\nIl regolatore '"; String@(.governor); "' può impostare la frequenza fra "; .minS / 1000;; "Mhz e "; .maxS / 1000;; "Mhz"
    End With
    
    p = cpufreq_get_available_frequencies(b)
    Print "\nSalti di frequenze disponibili:"
    While IsNull(p) = False
      Print Space(33); Long@(p) / 1000;; "Mhz"
      p = p + 8
      p = Pointer@(p)
    Wend
       
    cpu = cpu + 8
    cpu = Pointer@(cpu)
    
    Print "\nFrequenza attuale della CPU  =   "; cpufreq_get_freq_kernel(b) / 1000;; "Mhz"
    Print "----------\n"  
     
    Inc b
   
  Wend
 
End



Riferimenti