Sau khi dành một chút thời gian đọc qua một vài bài khác nhau mà có vẻ khá phức tạp tôi đã đưa ra với điều này. Tôi cần nó cho một máy 8 lõi mà tôi muốn theo dõi máy chủ SQL. Đối với mã bên dưới, tôi đã chuyển vào "sqlservr" dưới tên appName.
private static void RunTest(string appName)
{
bool done = false;
PerformanceCounter total_cpu = new PerformanceCounter("Process", "% Processor Time", "_Total");
PerformanceCounter process_cpu = new PerformanceCounter("Process", "% Processor Time", appName);
while (!done)
{
float t = total_cpu.NextValue();
float p = process_cpu.NextValue();
Console.WriteLine(String.Format("_Total = {0} App = {1} {2}%\n", t, p, p/t * 100));
System.Threading.Thread.Sleep(1000);
}
}
Dường như đo chính xác% CPU đang được SQL sử dụng trên máy chủ lõi 8 của tôi.
http://stackoverflow.com/questions/4679962/what-is-the-correct-performance-counter-to-get-cpu-and-memory-usage-of-a-process/4680030 # 4680030 – SwDevMan81