views:

1443

answers:

5

I have a Samurize config that shows a CPU usage graph similar to Task manager.

How do I also display the name of the process with the current highest CPU usage percentage?

I would like this to be updated, at most, once per second. Samurize can call a command line tool and display it's output on screen, so this could also be an option.

A: 

Further clarification:

I have investigated writing my own command line c# .NET application to enumerate the array returned from System.Diagnostics.Process.GetProcesses(), but the Process instance class does not seem to include a CPU percentage property.

Can I calculate this in some way?

Ash
A: 

You might be able to use Pmon.exe for this. You can get it as part of the Windows Resource Kit tools (the link is to the Server 2003 version, which can apparently be used in XP as well).

hasseg
+1  A: 

With PowerShell:

Get-Process | Sort-Object CPU -desc | Select-Object -first 3 | Format-Table | CPU,ProcessName -hidetableheader

returns somewhat like:

  16.8041632 System
   12.568072 csrss
  11.9872368 powershell
PabloG
+2  A: 

What you want to get its the instant CPU usage (kind of)...

Actually, the instant CPU usage for a process does not exists. Instead you have to make two measurements and calculate the average CPU usage, the formula is quite simple:

AvgCpuUsed = [TotalCPUTime(process,time2) - TotalCPUTime(process,time1)] / [time2-time1]

The lower Time2 and Time1 difference is, the more "instant" your meassurement will be. Windows Task Manager calculate the CPU use with an interval of one second. I've found that is more than enough and you might even consider doing it in 5 seconds intervals cause the act of measuring itself takes up CPU cicles...

So, first, to get the average CPU time

using System.Diagnostics;

float GetAverageCPULoad(int procID, DateTme from, DateTime, to)
{
  // For the current process
  //Process proc = Process.GetCurrentProcess();
  // Or for any other process given its id
  Process proc = Process.GetProcessById(procID);
  System.TimeSpan lifeInterval = (to - from);
  // Get the CPU use
  float CPULoad = (proc.TotalProcessorTime.TotalMilliseconds /
                  lifeInterval.TotalMilliseconds) * 100;
  // You need to take the number of present cores into account
  return CPULoad / System.Environment.ProcessorCount;
}

now, for the "instant" CPU load you'll need an specialized class:

class ProcLoad
{
  // Last time you checked for a process
  public Dictionary<int, DateTime> lastCheckedDict = new Dictionary<int, DateTime>();

  public float GetCPULoad(int procID)
  {
    if (lastCheckedDict.ContainsKey(procID))
    {
      DateTime last = lastCheckedDict[procID];
      lastCheckedDict[procID] = DateTime.Now;
      return GetAverageCPULoad(procID, last, lastCheckedDict[procID]);
    }
    else
    {
      lastCheckedDict.Add(procID, DateTime.Now);
      return 0;
    }
  }
}

You should call that class from a timer (or whatever interval method you like) for each process you want to monitor, if you want all the processes just use the Process.GetProcesses static method

Jorge Córdoba