在MATLAB中显示CPU内核利用率 [英] Show CPU cores utilization in MATLAB

查看:239
本文介绍了在MATLAB中显示CPU内核利用率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

反正(是否有任何功能)在GUI中以MATLAB形式显示在MATLAB中的CPU cores utilization,就像我们在Windows(性能"选项卡)中的Task Manager中一样?

Is anyway (any function etc) to show CPU cores utilization in MATLAB in a GUI likes that we have in Task Manager of windows (Performance Tab)?

谢谢.

推荐答案

据我所知,没有Matlab函数可以在进程使用级别访问系统属性.要获取此信息,必须调用外部类.

To my knowledge, there are no Matlab function which can access the system properties at the level of the process usage. To get this information one must call external classes.

在Internet上进行搜索可以获取一些 Java 类,这些类可以查询流程属性. Java方法的优点是它更有可能是跨平台的.

A search on internet can fetch you some Java classes which can query the process properties. The advantage of the Java approach is it is more likely to be cross-platform.

对于Windows用户,仍然有两种查询这些信息的方式:直接调用Windows API(速度更快,但在Matlab中放置起来相当复杂),以及使用.net对象(速度较慢,但​​超级简单) Matlab几乎无缝地处理.net类).

For windows user, there are still 2 ways of querying these information: by direct call to windows API (faster, but quite complicated to put in place in Matlab), and by using .net object (slower, but uber easy as Matlab handle .net classes almost seamlessly).

  • 创建对象

我们需要告诉Matlab实例化 System.Diagnostics.PerformanceCounter 对象.对于示例/i,创建两个对象,一个对象查看系统空闲进程(称为Idle),另一个对象查看 Matlab 进程(该对象将报告Matlab CPU使用情况.

We need to tell Matlab to instantiate a System.Diagnostics.PerformanceCounter object. For the example /i create two of these objects, one which looks at the System Idle Process (called Idle) and one which looks at the Matlab process (this one will 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 the object
    • 使用Windows API,我们必须自己进行很多机器周期计算,但是这些.net对象很简洁,因为它们为您完成了所有这些工作(尽管以性能为代价).因此,现在只需调用对象并询问最近一次的CPU使用率是什么……很容易.

      With windows API we'd have to do a lot of machine cycle calculations ourselves, but these .net object are neat because they do all that for you (at a cost of performance though). So now it is only a matter of calling the object and asking what was the last CPU usage ... easy.

      唯一需要注意的细节是,报告的数量仅针对该进程正在使用的处理器内核,因此,如果您有多个内核,则报告的数量必须除以要处理的处理器总数.得到一个整体图.

      The only detail to care for, is that the number reported is only for the processor core which was in use by the process, so if you have multiple core, the reported number has to be divided by the total number of processor to get an overall figure.

         % 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 ;
      


      • 显示

        • Displaying
        • 你去了.最困难的部分是了解和访问这些.net的细微之处.现在,如果您想要一个真正的监视器,则需要定义一个计时器,该计时器将定期调用这些方法,然后显示结果.

          There you go. The most difficult part was to know and access these .net subtleties. Now if you want a true monitor, you'll need to define a timer which will call these methods at regular interval, then display the results.

          请注意,调用这些.net对象在处理器时间上是非常昂贵的,因此,如果创建太多的PerformanceCounter,监视器将最终消耗大部分的处理器时间(每个进程占用一个处理器的时间非常多)例如)... 也不要尝试在短时间内疯狂刷新您的计时器

          Just be aware that calling these .net objects is quite expensive in processor time, so if you create too many PerformanceCounter your monitor will end up eating most of the processor time (one for each process would be quite taxing for example) ... and don't try to refresh your timer at crazy short intervals either

          • 完整的功能示例:

          对不起,但其中90%仅用于gui机械手(我尽可能保持粗略,但仍然如此),所以我将不对所有内容进行解释.唯一重要的位是上面显示的摘录(包含在下面的完整功能示例中).

          Sorry but 90% of it is just for the gui mechanics (which I kept as rough as possible but still) so i won't explain all of it. The only important bits were the snippets shown above (which are included in the fully 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 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 = 1;
             h.t.ExecutionMode = 'fixedRate';
             h.t.TimerFcn = {@updateMeasure,h.fig} ;
             h.t.TasksToExecute = Inf;
          
             %// store the handle collection
             guidata(h.fig,h)
          
          end
          

          这篇关于在MATLAB中显示CPU内核利用率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆