【问题标题】:How to profile memory and CPU usage from C#如何从 C# 分析内存和 CPU 使用情况
【发布时间】:2012-04-16 11:06:03
【问题描述】:

我正在运行一个使用 C# 启动两个进程的测试。我需要获得我的进程使用的顶级内存和 CPU。拜托,有人可以给我一个关于如何使用托管代码的指南吗? (我也使用单声道在 linux 上运行它)。

架构如下:

进程test.exe 启动两个进程:A.exeB.exe。我需要测量 测量进程 A 和 B 的最大内存和 CPU,来自 test.exe

有可能吗?提前致谢

【问题讨论】:

  • 使用的最高 CPU?你是说百分比吗?当它运行时,它是 100% 的核心。当它被阻止时,它是0%。进程的 CPU 使用百分比是这两者的运行平均值。这真的是你想知道的吗?

标签: c# .net process profiling instrumentation


【解决方案1】:

您可以使用System.Diagnostics.Process 类来启动进程。然后您可以检查UserProcessorTimeTotalProcessorTimePeakWorkingSet64 和其他属性来检查处理器使用情况和内存使用情况。检查这个MSDN Article for System.Diagnostics.Process

【讨论】:

  • 这不会告诉你处理器使用情况,只告诉你 CPU 时间。
【解决方案2】:

试试这个Get CPU Info

PerformanceCounter cpuCounter;
PerformanceCounter ramCounter;

cpuCounter = new PerformanceCounter();

cpuCounter.CategoryName = "Processor";
cpuCounter.CounterName = "% Processor Time";
cpuCounter.InstanceName = "_Total";

ramCounter = new PerformanceCounter("Memory", "Available MBytes");


public string getCurrentCpuUsage(){
        return cpuCounter.NextValue()+"%";
}

public string getAvailableRAM(){
        return ramCounter.NextValue()+"MB";
} 

编辑: 因此,您可以在开始流程之前和之后检查差异。

【讨论】:

    【解决方案3】:

    或者你可以试试这个

    private ulong GetCPU(ThreadEntry[] thList) 
    { 
      ulong result = 0; 
      for (int i = 0; i < thList.Length; i++) 
      { 
        ulong NewThC = 0; 
        ulong NewThE = 0; 
        ulong NewThK = 0; 
        ulong NewThU = 0; 
        if(GetThreadTimes(thList[i].ThreadID, ref NewThC, ref NewThE, ref NewThK, ref NewThU)) 
          result = result + NewThK + NewThU; 
        int error = Marshal.GetLastWin32Error(); 
    //often ERROR == 6 or ERROR == 18
      } 
      return result; 
    } 
    
    //GET ALL PROCESS CPU USAGE 
    private void timer1_Tick(object sender, EventArgs e) 
    { 
    //reset results 
      this.textBox1.Text = string.Empty; 
    //turn of timer 
      this.Enabled = false; 
      uint perm = GetCurrentPermissions();
    
    
      //SET PERMISION SO I CAN RECEIVE INFO ABOUT THREADS
      SetProcPermissions(0xFFFFFFFF);
    
    
    //get all running process (OPENNETCF)       
      List<ProcessEntry> processList = new List<ProcessEntry>(ProcessEntry.GetProcesses()); 
    
    //OLD Variables stored in list 
      if (OldResList == null || (OldResList != null && OldResList.Length !=   processList.Count)) 
        OldResList = new ulong[processList.Count]; 
    
    //SORT by ID only for testing       
      processList.Sort(CompareProcessEntry); 
    //GET ALL CPU USAGE       
      for(int i=0;i<processList.Count;i++) 
      { 
      //new value 
        ulong newRes = GetCPU( processList[i].GetThreads() ); 
      //result 
        ulong result = (newRes - OldResList[i]); 
      //valid result 
        result = result / (ulong)this.timer1.Interval; 
      //set result to the thexbox 
        this.textBox1.Text += processList[i].ExeFile + " " +  result.ToString() + " ;"; 
      //change old to new 
        OldResList[i] = newRes; 
      } 
      //sleep 
      Thread.Sleep(1000); 
      SetProcPermissions(0xFFFFFFFF);
      //start again 
    
      timer1.Enabled = true; 
      } 
      public static int CompareProcessEntry(ProcessEntry p1, ProcessEntry p2) 
      { 
       return p1.ProcessID.CompareTo(p2.ProcessID); 
      } 
    
      [DllImport("coredll")] 
      private static extern bool GetThreadTimes(uint p, ref ulong NewThC, ref ulong NewThE, ref ulong NewThK, ref ulong NewThU); 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-17
      • 2011-05-12
      • 1970-01-01
      相关资源
      最近更新 更多