Xojo Conferences
XDCMay2019MiamiUSA

Platforms to show: All Mac Windows Linux Cross-Platform

/Util/CPU/CPU Info on Mac OS X
Function:
Required plugins for this example: MBS Util Plugin
You find this example project in your Plugins Download as a Xojo project file within the examples folder: /Util/CPU/CPU Info on Mac OS X
This example is the version from Tue, 10th Sep 2012.
Project "CPU Info on Mac OS X.rbp"
Class Window1 Inherits Window
Control List Inherits ListBox
ControlInstance List Inherits ListBox
End Control
EventHandler Sub Open() dim s as CPUInfo dim i,c as integer s=new CPUInfo c=s.CountOfCPUs w str(c)+" CPUs:" w "" for i=1 to c w ""+str(i)+". CPU:" w " CPU Type: "+st(s.CpuType(i)) w " CPU Sub Type: "+st(s.CPUSubType(i)) w " Is Master: "+st(s.IsMaster(i)) w " Running: "+st(s.running(i)) w " Slot Num: "+st(s.slotnum(i)) w " CPU Load:" w " User: "+st(s.CPULoadUser(i)) w " System: "+st(s.CPULoadSystem(i)) w " Idle: "+st(s.CPULoadIdle(i)) w " Nice: "+st(s.CPULoadNice(i)) if UBound(s.Temperature)>=i then // not always available w " Temperature: "+st(s.Temperature(i)) end if w "" next End EventHandler
Function st(v as variant) As string Return v End Function
Sub w(s as string) List.AddRow s End Sub
End Class
MenuBar Menu
MenuItem UntitledMenu3 = ""
MenuItem UntitledMenu2 = "File"
MenuItem FileQuit = "Quit"
MenuItem UntitledMenu0 = "Edit"
MenuItem EditUndo = "Undo"
MenuItem UntitledMenu1 = "-"
MenuItem EditCut = "Cut"
MenuItem EditCopy = "Copy"
MenuItem EditPaste = "Paste"
MenuItem EditClear = "Clear"
End MenuBar
Class CPUInfo
Sub Constructor() const PROCESSOR_BASIC_INFO=1 //* basic information */ const PROCESSOR_CPU_LOAD_INFO=2 //* cpu load information */ const PROCESSOR_PM_REGS_INFO=&h10000001 //* performance monitor register info */ const PROCESSOR_TEMPERATURE=&h10000002 //* Processor core temperature */ dim s as new SoftDeclareMBS dim port as integer dim p as MemoryBlock // Parameters dim buf as MemoryBlock dim bufptr as MemoryBlock dim count as MemoryBlock dim noOfMPU as MemoryBlock dim i,j as integer dim t as String if s.LoadFramework("System.framework") then if s.LoadFunction("mach_host_self") then if s.CallFunction(0,nil) then port=s.Result // port to talk through end if end if end if if port=0 then // failed for any reason Return end if s=new SoftDeclareMBS if s.LoadFramework("System.framework") then if s.LoadFunction("host_processor_info") then p=NewMemoryBlock(100) noOfMPU=NewMemoryBlock(4) count=NewMemoryBlock(4) bufptr=NewMemoryBlock(4) p.Long(0)=port p.Long(4)=PROCESSOR_BASIC_INFO p.Long(8)=noOfMPU.AddressMBS(0) p.Long(12)=bufptr.AddressMBS(0) p.Long(16)=count.AddressMBS(0) if s.CallFunction(5,p) then 'struct processor_basic_info { 'cpu_type_t cpu_type; /* type of cpu */ 'cpu_subtype_t cpu_subtype; /* subtype of cpu */ 'boolean_t running; /* is processor running */ 'int slot_num; /* slot number */ 'boolean_t is_master; /* is this the master processor */ '}; if s.Result=0 then CountOfCPUs=noOfMPU.Long(0) redim CpuType(CountOfCPUs) redim cpusubtype(CountOfCPUs) redim ismaster(CountOfCPUs) redim running(CountOfCPUs) redim slotnum(CountOfCPUs) buf=NewMemoryBlockFromPtrMBS(bufptr.Long(0)) j=0 for i=1 to CountOfCPUs CpuType(i)=buf.Long(j) j=j+4 CPUSubType(i)=buf.Long(j) j=j+4 Running(i)=buf.Long(j)>0 j=j+4 SlotNum(i)=buf.Long(j) j=j+4 isMaster(i)=buf.Long(j)>0 j=j+4 next vm_deallocate(port,bufptr.Long(0),count.Long(0)) end if end if end if end if const CPU_STATE_MAX =4 const CPU_STATE_USER =0 const CPU_STATE_SYSTEM=1 const CPU_STATE_IDLE =2 const CPU_STATE_NICE =3 if s.LoadFramework("System.framework") then if s.LoadFunction("host_processor_info") then p=NewMemoryBlock(100) noOfMPU=NewMemoryBlock(4) count=NewMemoryBlock(4) bufptr=NewMemoryBlock(4) p.Long(0)=port p.Long(4)=PROCESSOR_CPU_LOAD_INFO p.Long(8)=noOfMPU.AddressMBS(0) p.Long(12)=bufptr.AddressMBS(0) p.Long(16)=count.AddressMBS(0) if s.CallFunction(5,p) then if s.Result=0 then redim CPULoadIdle(CountOfCPUs) redim CPULoadNice(CountOfCPUs) redim CPULoadSystem(CountOfCPUs) redim CPULoadUser(CountOfCPUs) buf=NewMemoryBlockFromPtrMBS(bufptr.Long(0)) j=0 for i=1 to CountOfCPUs CPULoadUser(i)=buf.Long(j) j=j+4 CPULoadSystem(i)=buf.Long(j) j=j+4 CPULoadIdle(i)=buf.Long(j) j=j+4 CPULoadNice(i)=buf.Long(j) j=j+4 next vm_deallocate(port,bufptr.Long(0),count.Long(0)) end if end if end if end if if s.LoadFramework("System.framework") then if s.LoadFunction("host_processor_info") then p=NewMemoryBlock(100) noOfMPU=NewMemoryBlock(4) count=NewMemoryBlock(4) bufptr=NewMemoryBlock(4) p.Long(0)=port p.Long(4)=PROCESSOR_TEMPERATURE p.Long(8)=noOfMPU.AddressMBS(0) p.Long(12)=bufptr.AddressMBS(0) p.Long(16)=count.AddressMBS(0) if s.CallFunction(5,p) then if s.Result=0 then redim Temperature(CountOfCPUs) buf=NewMemoryBlockFromPtrMBS(bufptr.Long(0)) j=0 for i=1 to CountOfCPUs Temperature(i)=buf.Long(j) j=j+4 next vm_deallocate(port,bufptr.Long(0),count.Long(0)) end if end if end if end if End Sub
Protected Sub vm_deallocate(port as integer, pointer as integer, count as integer) // Release kernel memory dim s as SoftDeclareMBS dim m as MemoryBlock m=NewMemoryBlock(12) m.Long(0)=port m.Long(4)=pointer m.Long(8)=count s=new SoftDeclareMBS if s.LoadFramework("System.framework") then if s.LoadFunction("vm_deallocate") then if s.CallFunction(3,m) then end if end if end if End Sub
Property CPULoadIdle(0) As integer
Property CPULoadNice(0) As integer
Property CPULoadSystem(0) As integer
Property CPULoadUser(0) As integer
Property CPUSubType(0) As integer
Property CountOfCPUs As integer
Property CpuType(0) As integer
Property Running(0) As boolean
Property SlotNum(0) As integer
Property Temperature(0) As integer
Property isMaster(0) As boolean
End Class
Class App Inherits Application
End Class
End Project

See also:

Feedback, Comments & Corrections

The items on this page are in the following plugins: MBS Util Plugin.




Links
MBS FileMaker blog