如何从C#配置内存和CPU使用情况

时间:2012-04-16 11:06:03

标签: c# .net process profiling instrumentation

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

架构如下:

  

流程test.exe启动两个流程:A.exeB.exe。我需要衡量   来自test.exe

的进程A和B的meausre最大内存和CPU

有可能吗?提前致谢

3 个答案:

答案 0 :(得分:7)

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

答案 1 :(得分: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";
} 

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

答案 2 :(得分:0)

或者你可以试试这个

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