我需要调用单个进程的准确CPU使用率
窍门是我还需要能够在多核机器上做到这一点。我的 C# 教育有点糟糕。我已经管理了以下代码。有人可以帮我吗?我尝试过使用 "_Total"
标志,并尝试修改其他一些代码片段,这些代码片段看起来像是试图检测核心数量。然而我被告知他们不包括超线程,只支持物理而不是逻辑处理器。我试图让它同时完成这两件事。显然,他们是使用等手动执行此操作的一种方法。
("Process", "% Processor Time", "1" process.ProcessName))
("Process", "% Processor Time", "2" process.ProcessName))
("Process", "% Processor Time", "3" process.ProcessName))
但我发现如果核心不存在,硬件将无法工作。我希望我能遇到更灵活的东西。我已经为此工作了好几天好几个小时,我要拔掉我的头发了。
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Threading;
using System.Collections;
using System.IO;
namespace Program_CPU_Monitor
{
class Program
{
static void Main(string[] args)
{
StreamWriter log;
log = File.AppendText("c:\\CPUMON.txt");
log.WriteLine("");
log.WriteLine("**Started logging Program CPU Monitor (2.6.0.63)**");
log.Close();
Console.Title = "Program CPU Monitor 2.6.0.63";
Console.WriteLine("Monitoring Program CPU & Memory usage...(1-min intervals)");
Console.WriteLine("Monitoring will start when Program is detected as running.");
Console.WriteLine("Please type in program name without the '.EXE', For example 'TESV' or 'calc'.");
Console.WriteLine("The program name is case sensative. Without the proper case it will not work.");
Console.WriteLine("This program will leave a log of the display called 'CPUMON.txt' on drive C:/.");
Console.WriteLine("Please type program name...");
Console.WriteLine("");
string procName = Console.ReadLine();
while (true)
{
Process[] runningNow = Process.GetProcesses();
foreach (Process process in runningNow)
{
using (PerformanceCounter pcProcess = new PerformanceCounter("Process", "% Processor Time", process.ProcessName))
using (PerformanceCounter memProcess = new PerformanceCounter("Memory", "Available MBytes"))
{
if (process.ProcessName == procName)
{
pcProcess.NextValue();
Thread.Sleep(60000);
StreamWriter OurStream;
OurStream = File.AppendText("c:\\CPUMON.txt");
Console.WriteLine("");
OurStream.WriteLine("");
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Process: '{0}' CPU Usage: {1}%", process.ProcessName, pcProcess.NextValue());
OurStream.WriteLine("Process: '{0}' CPU Usage: {1}%", process.ProcessName, pcProcess.NextValue());
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Process: '{0}' RAM Free: {1}MB", process.ProcessName, memProcess.NextValue());
OurStream.WriteLine("Process: '{0}' RAM Free: {1}MB", process.ProcessName, memProcess.NextValue());
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine(string.Format("Recorded: '{0}' at {1}", procName, DateTime.Now.ToString()));
OurStream.WriteLine(string.Format("Recorded: '{0}' at {1}", procName, DateTime.Now.ToString()));
OurStream.Close();
}
}
}
}
}
}
}
编辑::我对代码进行了以下更改,以根据建议和一般的摆弄来解决我的问题。
foreach (Process process in runningNow)
{
using (PerformanceCounter cpuUsage = new PerformanceCounter("Process", "% Processor Time", "_Total"))
using (PerformanceCounter pcProcess = new PerformanceCounter("Process", "% Processor Time", process.ProcessName))
using (PerformanceCounter memProcess = new PerformanceCounter("Memory", "Available MBytes"))
{
if (process.ProcessName == procName)
{
StreamWriter OurStream;
OurStream = File.AppendText("c:\\CPUMON.txt");
Console.WriteLine("");
OurStream.WriteLine("");
// Prime the Performance Counters
pcProcess.NextValue();
cpuUsage.NextValue();
Thread.Sleep(100);
isprimed = true;
double cpuUse = Math.Round(pcProcess.NextValue() / cpuUsage.NextValue() * 100, 2);
// Check for Not-A-Number (Division by Zero)
if (Double.IsNaN(cpuUse))
cpuUse = 0;
//Get CPU Usage
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Process: `{0}' CPU Usage: {1}%", process.ProcessName, Convert.ToInt32(cpuUse));
OurStream.WriteLine("Process: `{0}' CPU Usage: {1}%", process.ProcessName, Convert.ToInt32(cpuUse));
// Get Process Memory Usage
Console.ForegroundColor = ConsoleColor.Green;
double memUseage = process.PrivateMemorySize64 / 1048576;
Console.WriteLine("Process: `{0}' Memory Usage: {1}MB", process.ProcessName, memUseage);
OurStream.WriteLine("Process: `{0}' Memory Usage: {1}MB", process.ProcessName, memUseage);
// Get Total RAM free
Console.ForegroundColor = ConsoleColor.Cyan;
float mem = memProcess.NextValue();
Console.WriteLine("During: `{0}' RAM Free: {1}MB", process.ProcessName, mem);
OurStream.WriteLine("During: `{0}' RAM Free: {1}MB", process.ProcessName, mem);
//Record and close stream
Console.ForegroundColor = ConsoleColor.Yellow;
System.DateTime newDate = System.DateTime.Now;
Console.WriteLine("Recorded: {0}", newDate);
OurStream.WriteLine("Recorded: {0}", newDate);
OurStream.Close();
Thread.Sleep(59900);
The Trick is I also need to be able to do it on multicore machines. My education in C# is a tad broken. I have managed the following code. Can anyone help me out? I have tried using the "_Total"
flag and I have tried modifying some other code snippets that looked like they tried to detect the amount of cores. I was told however they did not include HT and only supported physical not logical processors. I was trying to get it to do both. Apparently their is a way to manually do this using
("Process", "% Processor Time", "1" process.ProcessName))
("Process", "% Processor Time", "2" process.ProcessName))
("Process", "% Processor Time", "3" process.ProcessName))
etc. But I have found out the hardware that doesn't work if the cores don't exist. I was hoping I could come across something more flexible. I have been working on this for days hours and hours at a time and I'm going to pull my hair out.
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Threading;
using System.Collections;
using System.IO;
namespace Program_CPU_Monitor
{
class Program
{
static void Main(string[] args)
{
StreamWriter log;
log = File.AppendText("c:\\CPUMON.txt");
log.WriteLine("");
log.WriteLine("**Started logging Program CPU Monitor (2.6.0.63)**");
log.Close();
Console.Title = "Program CPU Monitor 2.6.0.63";
Console.WriteLine("Monitoring Program CPU & Memory usage...(1-min intervals)");
Console.WriteLine("Monitoring will start when Program is detected as running.");
Console.WriteLine("Please type in program name without the '.EXE', For example 'TESV' or 'calc'.");
Console.WriteLine("The program name is case sensative. Without the proper case it will not work.");
Console.WriteLine("This program will leave a log of the display called 'CPUMON.txt' on drive C:/.");
Console.WriteLine("Please type program name...");
Console.WriteLine("");
string procName = Console.ReadLine();
while (true)
{
Process[] runningNow = Process.GetProcesses();
foreach (Process process in runningNow)
{
using (PerformanceCounter pcProcess = new PerformanceCounter("Process", "% Processor Time", process.ProcessName))
using (PerformanceCounter memProcess = new PerformanceCounter("Memory", "Available MBytes"))
{
if (process.ProcessName == procName)
{
pcProcess.NextValue();
Thread.Sleep(60000);
StreamWriter OurStream;
OurStream = File.AppendText("c:\\CPUMON.txt");
Console.WriteLine("");
OurStream.WriteLine("");
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Process: '{0}' CPU Usage: {1}%", process.ProcessName, pcProcess.NextValue());
OurStream.WriteLine("Process: '{0}' CPU Usage: {1}%", process.ProcessName, pcProcess.NextValue());
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Process: '{0}' RAM Free: {1}MB", process.ProcessName, memProcess.NextValue());
OurStream.WriteLine("Process: '{0}' RAM Free: {1}MB", process.ProcessName, memProcess.NextValue());
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine(string.Format("Recorded: '{0}' at {1}", procName, DateTime.Now.ToString()));
OurStream.WriteLine(string.Format("Recorded: '{0}' at {1}", procName, DateTime.Now.ToString()));
OurStream.Close();
}
}
}
}
}
}
}
EDIT:: I made the following changes to the code to fix my issues per advice and general fiddling around.
foreach (Process process in runningNow)
{
using (PerformanceCounter cpuUsage = new PerformanceCounter("Process", "% Processor Time", "_Total"))
using (PerformanceCounter pcProcess = new PerformanceCounter("Process", "% Processor Time", process.ProcessName))
using (PerformanceCounter memProcess = new PerformanceCounter("Memory", "Available MBytes"))
{
if (process.ProcessName == procName)
{
StreamWriter OurStream;
OurStream = File.AppendText("c:\\CPUMON.txt");
Console.WriteLine("");
OurStream.WriteLine("");
// Prime the Performance Counters
pcProcess.NextValue();
cpuUsage.NextValue();
Thread.Sleep(100);
isprimed = true;
double cpuUse = Math.Round(pcProcess.NextValue() / cpuUsage.NextValue() * 100, 2);
// Check for Not-A-Number (Division by Zero)
if (Double.IsNaN(cpuUse))
cpuUse = 0;
//Get CPU Usage
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Process: `{0}' CPU Usage: {1}%", process.ProcessName, Convert.ToInt32(cpuUse));
OurStream.WriteLine("Process: `{0}' CPU Usage: {1}%", process.ProcessName, Convert.ToInt32(cpuUse));
// Get Process Memory Usage
Console.ForegroundColor = ConsoleColor.Green;
double memUseage = process.PrivateMemorySize64 / 1048576;
Console.WriteLine("Process: `{0}' Memory Usage: {1}MB", process.ProcessName, memUseage);
OurStream.WriteLine("Process: `{0}' Memory Usage: {1}MB", process.ProcessName, memUseage);
// Get Total RAM free
Console.ForegroundColor = ConsoleColor.Cyan;
float mem = memProcess.NextValue();
Console.WriteLine("During: `{0}' RAM Free: {1}MB", process.ProcessName, mem);
OurStream.WriteLine("During: `{0}' RAM Free: {1}MB", process.ProcessName, mem);
//Record and close stream
Console.ForegroundColor = ConsoleColor.Yellow;
System.DateTime newDate = System.DateTime.Now;
Console.WriteLine("Recorded: {0}", newDate);
OurStream.WriteLine("Recorded: {0}", newDate);
OurStream.Close();
Thread.Sleep(59900);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您只能每 100 毫秒读取一次性能计数器,否则 timesilce 太小而无法获得准确的读数,如果您每 100 毫秒读取一次以上,它将始终报告 0 或 100% 使用情况。因为您调用
NextValue()
两次(一次针对文件,一次针对流),第二次读取将是自上次读取该行以来的用法。将您的代码更改为:
可能还有其他问题导致您出现问题,但调用 NextValue 两次是第一个引起我注意的问题。
说明:
当您请求
NextValue
太快时NextValue
仅报告 0 或 100% 的原因是,如果您当前是否执行代码是一个布尔因素。所以性能计数器正在做的就是提出这样的问题:
性能计数器使用的时间片大小为 100 毫秒,因此如果低于 100 毫秒,您基本上是在询问
对于该问题,您只能得到两个答案:“否”(0%) 或“是”(100%)。
You can only read the performance counters every 100 ms or the timesilce will be too small for it to get a accurate reading, if you read more than once every 100ms it will always report 0 or 100% usage. Because you call
NextValue()
twice (once for the file, once for your stream) the second reading will be the usage since the previous reading the line before.Change your code to this:
There may be other issues causing you problems but calling NextValue twice is the first one that jumped out at me.
Explanation:
The reason behind
NextValue
only reporting 0 or 100% when you requestNextValue
too fast is the fact that if you are currently executing code or not is a boolean factor.So what the performance counter is doing is asking the question:
The size of those time slices the performance counter works with is 100ms so if you go below 100ms you are basically asking
and the only two answers you can get to that question are "No" (0%) or "Yes" (100%).