Differenze tra le versioni di "Ottenere informazioni generali sulla CPU con le sole risorse di Gambas"

Da Gambas-it.org - Wikipedia.
 
(Una versione intermedia di uno stesso utente non è mostrata)
Riga 2: Riga 2:
  
 
Ne mostriamo un possibile codice:
 
Ne mostriamo un possibile codice:
  '''Public''' Sub Main()
+
  Public Sub Main()
 
+
 
   Dim s, v, cpu, sys As String
 
   Dim s, v, cpu, sys As String
 
 
  sys = "/sys/devices/system/cpu"
 
   
 
  If Not Exist(sys) Then Error.Raise("Impossibile trovare il percorso contenente le cartelle delle CPU !")
 
   
 
  For Each cpu In Dir(sys, "cpu*", gb.Directory)
 
     
 
    If IsDigit(Right(cpu, 1)) Then
 
      Print "== Analisi "; cpu; " =="
 
      Print "Driver: "; Replace(File.Load(sys &/ cpu &/ "cpufreq/scaling_driver"), "\n", Null)
 
      Print "Latenza massima di transizione: "; Durata(Replace(File.Load(sys &/ cpu &/ "cpufreq/cpuinfo_transition_latency"), "\n", Null))
 
      s = Velocitas(Replace(File.Load(sys &/ cpu &/ "cpufreq/cpuinfo_max_freq"), "\n", Null)) & "  " & Velocitas(Replace(File.Load(sys &/ cpu &/ "cpufreq/cpuinfo_min_freq"), "\n", Null))
 
      Print "Limiti hardware: "; s
 
 
   
 
   
      s = Null
+
  Print "Numero dei processori presenti: "; System.Cores
      For Each v In Split(Replace(File.Load(sys &/ cpu &/ "cpufreq/scaling_available_frequencies"), "\n", Null), " ")
+
        If IsDigit(v) Then s &= Velocitas(v) & "  "
+
  sys = "/sys/devices/system/cpu"
      Next
+
 
      Print "Salti di frequenze disponibili: "; s
+
  If Not Exist(sys) Then Error.Raise("Impossibile trovare il percorso contenente le cartelle delle CPU !")
       
+
      Print "Regolatori di frequenza della CPU disponibili: "; Replace(File.Load(sys &/ cpu &/ "cpufreq/scaling_available_governors"), "\n", Null)
+
  For Each cpu In Dir(sys, "cpu*", gb.Directory)
       
+
    If IsDigit(Right(cpu, 1)) Then
      s = Replace(File.Load(sys &/ cpu &/ "cpufreq/scaling_governor"), "\n", Null) & "' può impostare la frequenza fra "
+
      Print "\n\n== Analisi "; cpu; " =="
      s &= Velocitas(Replace(File.Load(sys &/ cpu &/ "cpufreq/scaling_max_freq"), "\n", Null)) & " e " & Velocitas(Replace(File.Load(sys &/ cpu &/ "cpufreq/scaling_min_freq"), "\n", Null))
+
      Print "Driver: "; Replace(File.Load(sys &/ cpu &/ "cpufreq/scaling_driver"), "\n", Null)
      Print "Il regolatore '"; s
+
      Print "Latenza massima di transizione: "; Durata(Replace(File.Load(sys &/ cpu &/ "cpufreq/cpuinfo_transition_latency"), "\n", Null))
       
+
      s = Velocitas(Replace(File.Load(sys &/ cpu &/ "cpufreq/cpuinfo_max_freq"), "\n", Null)) & "  " & Velocitas(Replace(File.Load(sys &/ cpu &/ "cpufreq/cpuinfo_min_freq"), "\n", Null))
      Print "L'attuale frequenza della CPU è: "; Velocitas(Replace(File.Load(sys &/ cpu &/ "cpufreq/scaling_cur_freq"), "\n", Null))
+
      Print "Limiti hardware: "; s
       
+
      s = Null
      Print
+
      For Each v In Split(Replace(File.Load(sys &/ cpu &/ "cpufreq/scaling_available_frequencies"), "\n", Null), " ")
    Endif
+
        If IsDigit(v) Then s &= Velocitas(v) & "  "
     
+
      Next
  Next
+
      Print "Salti di frequenze disponibili: "; s
 +
      Print "Regolatori di frequenza della CPU disponibili: "; Replace(File.Load(sys &/ cpu &/ "cpufreq/scaling_available_governors"), "\n", Null)
 +
      s = Replace(File.Load(sys &/ cpu &/ "cpufreq/scaling_governor"), "\n", Null) & "' può impostare la frequenza fra "
 +
      s &= Velocitas(Replace(File.Load(sys &/ cpu &/ "cpufreq/scaling_max_freq"), "\n", Null)) & " e " & Velocitas(Replace(File.Load(sys &/ cpu &/ "cpufreq/scaling_min_freq"), "\n", Null))
 +
      Print "Il regolatore '"; s
 +
      Print "L'attuale frequenza della CPU è: "; Velocitas(Replace(File.Load(sys &/ cpu &/ "cpufreq/scaling_cur_freq"), "\n", Null))
 +
      Print
 +
    Endif
 +
  Next
 
   
 
   
  '''End'''
+
  End
 
   
 
   
 
   
 
   
  '''Private''' Function Durata(d As String) As String
+
  Private Function Durata(d As String) As String
 
   
 
   
 
   Dim tmp, dur As Long
 
   Dim tmp, dur As Long
 +
 +
  dur = Val(d)
 
    
 
    
  dur = Val(d)
+
   If dur > 1000000 Then
    
+
    tmp = dur Mod 10000
  If dur > 1000000 Then
+
    If tmp >= 5000 Then dur += 10000
    tmp = dur Mod 10000
+
    Return CStr(dur / 1000000) & "," & CStr((dur Mod 1000000) / 10000) & " ms"
    If tmp >= 5000 Then dur += 10000
+
  Else If dur > 100000 Then
    Return CStr(dur / 1000000) & "," & CStr((dur Mod 1000000) / 10000) & " ms"
+
    tmp = dur Mod 1000
  Else If dur > 100000 Then
+
    If tmp >= 500 Then dur += 1000
    tmp = dur Mod 1000
+
    Return CStr(dur / 1000) & " us"
    If tmp >= 500 Then dur += 1000
+
  Else If dur > 1000 Then
    Return CStr(dur / 1000) & " us"
+
    tmp = dur Mod 100
  Else If dur > 1000 Then
+
    If tmp >= 50 Then dur += 100
    tmp = dur Mod 100
+
    Return CStr(dur / 1000) & "," & CStr((dur Mod 1000) / 100) & " us"
    If tmp >= 50 Then dur += 100
+
  Else
    Return CStr(dur / 1000) & "," & CStr((dur Mod 1000) / 100) & " us"
+
    Return CStr(dur) & " ns"
  Else
+
  Endif
    Return CStr(dur) & " ns"
+
  Endif
+
  End
 
+
  '''End'''
 
 
   
 
   
 +
Private Function Velocitas(v As String) As String
 
   
 
   
'''Private''' Function Velocitas(v As String) As String
 
 
 
 
   Dim tmp, vel As Long
 
   Dim tmp, vel As Long
    
+
  vel = Val(v)
+
   vel = Val(v)
    
+
  If vel > 1000000 Then
+
   If vel > 1000000 Then
    tmp = vel Mod 10000
+
    tmp = vel Mod 10000
    If (tmp >= 5000) Then vel += 10000
+
    If (tmp >= 5000) Then vel += 10000
    Return CStr(vel \ 1000000) & "," & CStr((vel Mod 1000000) / 10000) & " GHz"
+
    Return CStr(vel \ 1000000) & "," & CStr((vel Mod 1000000) / 10000) & " GHz"
  Else If vel > 100000 Then
+
  Else If vel > 100000 Then
    tmp = vel Mod 1000
+
    tmp = vel Mod 1000
    If tmp >= 500 Then vel += 1000
+
    If tmp >= 500 Then vel += 1000
    Return CStr(vel / 1000) & " MHz"
+
    Return CStr(vel / 1000) & " MHz"
  Else If vel > 1000
+
  Else If vel > 1000
    tmp = vel Mod 100
+
    tmp = vel Mod 100
    If tmp >= 50 Then vel += 100
+
    If tmp >= 50 Then vel += 100
    Return CStr(vel / 1000) & "," & CStr((vel Mod 1000) / 100)
+
    Return CStr(vel / 1000) & "," & CStr((vel Mod 1000) / 100)
  Else
+
  Else
    Return CStr(vel) & " KHz"
+
    Return CStr(vel) & " KHz"
  Endif
+
  Endif
 
+
  '''End'''
+
  End

Versione attuale delle 08:26, 29 lug 2023

E' possibile ottenere con le sole risorse di Gambas alcune informazioni generali sulla CPU analizzando i file contenuti nella cartella di sistema /sys/devices/system/cpu e nelle sue sub-cartelle.

Ne mostriamo un possibile codice:

Public Sub Main()

 Dim s, v, cpu, sys As String

 Print "Numero dei processori presenti: "; System.Cores

 sys = "/sys/devices/system/cpu"
  
 If Not Exist(sys) Then Error.Raise("Impossibile trovare il percorso contenente le cartelle delle CPU !")

 For Each cpu In Dir(sys, "cpu*", gb.Directory)
   If IsDigit(Right(cpu, 1)) Then
     Print "\n\n== Analisi "; cpu; " =="
     Print "Driver: "; Replace(File.Load(sys &/ cpu &/ "cpufreq/scaling_driver"), "\n", Null)
     Print "Latenza massima di transizione: "; Durata(Replace(File.Load(sys &/ cpu &/ "cpufreq/cpuinfo_transition_latency"), "\n", Null))
     s = Velocitas(Replace(File.Load(sys &/ cpu &/ "cpufreq/cpuinfo_max_freq"), "\n", Null)) & "  " & Velocitas(Replace(File.Load(sys &/ cpu &/ "cpufreq/cpuinfo_min_freq"), "\n", Null))
     Print "Limiti hardware: "; s
     s = Null
     For Each v In Split(Replace(File.Load(sys &/ cpu &/ "cpufreq/scaling_available_frequencies"), "\n", Null), " ")
       If IsDigit(v) Then s &= Velocitas(v) & "  "
     Next
     Print "Salti di frequenze disponibili: "; s
     Print "Regolatori di frequenza della CPU disponibili: "; Replace(File.Load(sys &/ cpu &/ "cpufreq/scaling_available_governors"), "\n", Null)
     s = Replace(File.Load(sys &/ cpu &/ "cpufreq/scaling_governor"), "\n", Null) & "' può impostare la frequenza fra "
     s &= Velocitas(Replace(File.Load(sys &/ cpu &/ "cpufreq/scaling_max_freq"), "\n", Null)) & " e " & Velocitas(Replace(File.Load(sys &/ cpu &/ "cpufreq/scaling_min_freq"), "\n", Null))
     Print "Il regolatore '"; s
     Print "L'attuale frequenza della CPU è: "; Velocitas(Replace(File.Load(sys &/ cpu &/ "cpufreq/scaling_cur_freq"), "\n", Null))
     Print
   Endif
 Next

End


Private Function Durata(d As String) As String

 Dim tmp, dur As Long

 dur = Val(d)
 
 If dur > 1000000 Then
   tmp = dur Mod 10000
   If tmp >= 5000 Then dur += 10000
   Return CStr(dur / 1000000) & "," & CStr((dur Mod 1000000) / 10000) & " ms"
 Else If dur > 100000 Then
   tmp = dur Mod 1000
   If tmp >= 500 Then dur += 1000
   Return CStr(dur / 1000) & " us"
 Else If dur > 1000 Then
   tmp = dur Mod 100
   If tmp >= 50 Then dur += 100
   Return CStr(dur / 1000) & "," & CStr((dur Mod 1000) / 100) & " us"
 Else
   Return CStr(dur) & " ns"
 Endif

End


Private Function Velocitas(v As String) As String

 Dim tmp, vel As Long

 vel = Val(v)

 If vel > 1000000 Then
   tmp = vel Mod 10000
   If (tmp >= 5000) Then vel += 10000
   Return CStr(vel \ 1000000) & "," & CStr((vel Mod 1000000) / 10000) & " GHz"
 Else If vel > 100000 Then
   tmp = vel Mod 1000
   If tmp >= 500 Then vel += 1000
   Return CStr(vel / 1000) & " MHz"
 Else If vel > 1000
   tmp = vel Mod 100
   If tmp >= 50 Then vel += 100
   Return CStr(vel / 1000) & "," & CStr((vel Mod 1000) / 100)
 Else
   Return CStr(vel) & " KHz"
 Endif

End