Can a C# program measure its own CPU usage somehow?

≡放荡痞女 提交于 2019-11-28 16:05:37

You can also use System.Diagnostics.Process.TotalProcessorTime and System.Diagnostics.ProcessThread.TotalProcessorTime properties to calculate your processor usage as this article describes.

Have a look at System.Diagnostics.PerformanceCounter. If you run up perfmon.exe, you'll see the range of performance counters available to you (set the 'performance object' to 'Process'), one of which is '% Processor Time'.

You can through the System.Diagnostic.PerformanceCounter class. Here's an example of somebody monitoring CPU usage:

http://blogs.msdn.com/dotnetinterop/archive/2007/02/02/system-diagnostics-performancecounter-and-processor-time-on-multi-core-or-multi-cpu.aspx

Note that this does require elevated privileges. And there may be a performance hit using it.

sundar venugopal

It is good that you are logging to monitors like smartinspect. But windows itself gathers the data for each resource in this case your program (or process). WMI is the standard for Application monitoring. We can view the data captured by WMI. Many application management, health monitoring or applicaiton monitoring tools support WMI out of the box.

So I would not recommend you to log your CPU usage within the application to a log file.

If you think availablity and performance is critical then go for solutions like Microsoft Operations manager solution.

To get an idea about WMI and to get the list of process see below: - Win32_PerfFormattedData_PerfProc_Process to get Cpu time, filter is processID See this article - You can get the processID from Win32_process class.

WMI Made Easy For C# by Kevin Matthew Goss

oConn.Username = "JohnDoe";
oConn.Password = "JohnsPass";

System.Management.ManagementScope oMs = new System.Management.ManagementScope("\\MachineX", oConn);    

//get Fixed disk stats
System.Management.ObjectQuery oQuery = new System.Management.ObjectQuery("select FreeSpace,Size,Name from Win32_LogicalDisk where DriveType=3");

//Execute the query 
ManagementObjectSearcher oSearcher = new ManagementObjectSearcher(oMs,oQuery);

//Get the results
ManagementObjectCollection oReturnCollection = oSearcher.Get();   

//loop through found drives and write out info
foreach( ManagementObject oReturn in oReturnCollection )
{
    // Disk name
    Console.WriteLine("Name : " + oReturn["Name"].ToString());
    // Free Space in bytes
    Console.WriteLine("FreeSpace: " + oReturn["FreeSpace"].ToString());
    // Size in bytes
    Console.WriteLine("Size: " + oReturn["Size"].ToString());
} 

You can monitor the process from Remote system as well.

This code project article describes how to use the high performance timer:

http://www.codeproject.com/KB/cs/highperformancetimercshar.aspx

You can use it to time the execution of your code.

Here you can find a number of open source C# profilers:

http://csharp-source.net/open-source/profile

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!