如何绘制 Matlab 的 CPU 使用率



我有一个显示 Matlab 的 CPU 使用率的代码,我的问题是如何绘制一段时间内的 CPU 使用率,因为这段代码不会在工作区中保存任何变量,我需要一种方法在代码运行时将 CPU 使用率存储在数组中,以便我可以绘制它谢谢你的时间。这是代码:

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 the cpu usage
cpu.total = 100 - h.mon.ProcPerfCounter.cpuIdle.NextValue / h.mon.NumOfCPU;
cpu.matlab = h.mon.ProcPerfCounter.Matlab.NextValue / h.mon.NumOfCPU;
%// update the 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 %// The 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 the monitor
h.mon = createMonitor;
%// Create the timer
h.t = timer;
h.t.Period = 0.25;
h.t.ExecutionMode = 'fixedRate';
h.t.TimerFcn = {@updateMeasure,h.fig};
h.t.TasksToExecute = Inf;
%// store the handle collection
guidata(h.fig,h)
end

一种方法是将变量保存到文件中,并在停止后读取并绘制它。因此,您可以在 updateMeasure 函数中添加一个dlmwrite命令,在 StartMonitor 中添加一个启动计时器的tic,并在 StopMonitor 中添加dlmreadplot。当然,这是快速和肮脏的,因为需要删除或检查创建的文件,因此您不会在以后的使用中继续附加到它。

这就是一切

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 the cpu usage
cpu.total = 100 - h.mon.ProcPerfCounter.cpuIdle.NextValue / h.mon.NumOfCPU;
cpu.matlab = h.mon.ProcPerfCounter.Matlab.NextValue / h.mon.NumOfCPU;
dlmwrite('cpulog.txt', [toc, cpu.total , cpu.matlab], '-append');
   %// update the 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)
tic
end
function StopMonitor(obj,evt)
h = guidata(obj);
stop(h.t)
data=dlmread('cpulog.txt');
data(1,:)=[]; 
figure;plot(data(:,1),data(:,2),data(:,1) ,data(:,3));
legend('total cpu','matlab cpu'); xlabel('sec'); ylabel('%');
end
function h = create_gui %// The 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 the monitor
h.mon = createMonitor;
%// Create the timer
h.t = timer;
h.t.Period = 0.25;
h.t.ExecutionMode = 'fixedRate';
h.t.TimerFcn = {@updateMeasure,h.fig};
h.t.TasksToExecute = Inf;
%// store the handle collection
guidata(h.fig,h)
end

最新更新