performance - Show CPU cores utilization in MATLAB -
is anyway (any function etc) show cpu cores utilization
in matlab
in gui
likes have in task manager
of windows (performance tab)?
thanks.
to knowledge, there no matlab function can access system properties @ level of process usage. information 1 must call external classes.
a search on internet can fetch java classes can query process properties. advantage of java approach is more cross-platform.
for windows user, there still 2 ways of querying these information: direct call windows api (faster, quite complicated put in place in matlab), , using .net
object (slower, uber easy matlab handle .net classes seamlessly).
- create object
we need tell matlab instantiate system.diagnostics.performancecounter object. example /i create 2 of these objects, 1 looks @ system idle process (called idle
) , 1 looks @ matlab process (this 1 report matlab cpu usage).
function mon = createmonitor matlabprocess = system.diagnostics.process.getcurrentprocess(); % "matlab" process cpuidleprocess = 'idle' ; mon.numofcpu = double(system.environment.processorcount); mon.procperfcounter.matlab = system.diagnostics.performancecounter('process', '% processor time', matlabprocess.processname ); mon.procperfcounter.cpuidle = system.diagnostics.performancecounter('process', '% processor time', cpuidleprocess ); end
- querying object
with windows api we'd have lot of machine cycle calculations ourselves, these .net object neat because (at cost of performance though). matter of calling object , asking last cpu usage ... easy.
the detail care for, number reported processor core in use process, if have multiple core, reported number has divided total number of processor overall figure.
% calculate cpu usage cpu.total = 100 - h.mon.procperfcounter.cpuidle.nextvalue / h.mon.numofcpu ; cpu.matlab = h.mon.procperfcounter.matlab.nextvalue / h.mon.numofcpu ;
- displaying
there go. difficult part know , access these .net
subtleties. if want true monitor, you'll need define timer call these methods @ regular interval, display results.
just aware calling these .net
objects quite expensive in processor time, if create many performancecounter
monitor end eating of processor time (one each process quite taxing example) ... and don't try refresh timer @ crazy short intervals either
- fully functional example:
sorry 90% of gui mechanics (which kept rough possible still) won't explain of it. important bits snippets shown above (which included in functional example below).
function hcol = cpu_monitor h = create_gui ; end function mon = createmonitor matlabprocess = system.diagnostics.process.getcurrentprocess(); %// "matlab" process cpuidleprocess = 'idle' ; mon.numofcpu = double(system.environment.processorcount); mon.procperfcounter.matlab = system.diagnostics.performancecounter('process', '% processor time', matlabprocess.processname ); mon.procperfcounter.cpuidle = system.diagnostics.performancecounter('process', '% processor time', cpuidleprocess ); end function updatemeasure(obj,evt,hfig) h = guidata(hfig) ; %// calculate cpu usage cpu.total = 100 - h.mon.procperfcounter.cpuidle.nextvalue / h.mon.numofcpu ; cpu.matlab = h.mon.procperfcounter.matlab.nextvalue / h.mon.numofcpu ; %// update display set(h.txttotalcpu,'string',num2str(cpu.total,'%5.2f %%') ) set(h.txtmatlabcpu,'string',num2str(cpu.matlab,'%5.2f %%') ) end function startmonitor(obj,evt) h = guidata(obj) ; start(h.t) end function stopmonitor(obj,evt) h = guidata(obj) ; stop(h.t) end function h = create_gui %// boring part h.fig = figure('unit','pixels','position',[200 800 240 120],'menubar','none','name','cpu usage %','numbertitle','off') ; h.btnstart = uicontrol('callback',@startmonitor,'position',[10 80 100 30],'string', 'start' ); h.btnstart = uicontrol('callback',@stopmonitor,'position',[130 80 100 30 ],'string', 'stop' ); h.lbl1 = uicontrol('horizontalalignment','right','position',[10 50 100 20],'string','total :','style','text' ); h.txttotalcpu = uicontrol('position',[130 50 100 20],'string','0','style','text' ) ; h.lbl2 = uicontrol('horizontalalignment','right','position',[10 10 100 20],'string','matlab :','style','text' ); h.txtmatlabcpu = uicontrol('position',[130 10 100 20],'string','0','style','text' ) ; movegui(h.fig,'center') %// create monitor h.mon = createmonitor ; %// create timer h.t = timer; h.t.period = 1; h.t.executionmode = 'fixedrate'; h.t.timerfcn = {@updatemeasure,h.fig} ; h.t.taskstoexecute = inf; %// store handle collection guidata(h.fig,h) end
Comments
Post a Comment