4

I'm running a test that launches two processes, using C#. I need to get the top memory and CPU used by my process. How can I do it using managed code? (I also run it on Linux using Mono).

The architecture is the following: the process test.exe launches two processes: A.exe and B.exe. I need to measure max memory and CPU for processes A and B, from test.exe

Is it possible to do?

halfer
  • 19,824
  • 17
  • 99
  • 186
Daniel Peñalba
  • 30,507
  • 32
  • 137
  • 219
  • Top CPU used? Do you mean percent? When it's running, it is 100% of its core. When it is blocked, it is 0%. CPU usage percent by a process is a running average of those two. Is that really what you want to know? – Mike Dunlavey Apr 16 '12 at 16:52

3 Answers3

8

You can use the System.Diagnostics.Process class to start the process. You can then check the UserProcessorTime, TotalProcessorTime, PeakWorkingSet64 and other properties to check processor usage and memory usage. Check this MSDN Article for System.Diagnostics.Process.

Espen Burud
  • 1,871
  • 10
  • 9
3

Try use this 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";
} 

EDIT: So you can check difference before you start your process and after.

Community
  • 1
  • 1
Likurg
  • 2,742
  • 17
  • 22
1

Or you can try this

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); 
Likurg
  • 2,742
  • 17
  • 22