Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am working on a background program that will be running for a long time, and I have a external logging program (SmartInspect) that I want to feed with some values periodically, to monitor it in realtime when debugging.

I know I can simply fire up multiple programs, like the Task Manager, or IARSN TaskInfo, but I'd like to keep everything in my own program for this, as I also wants to add some simple rules like if the program uses more than X% CPU, flag this in the log.

I have a background thread that periodically feeds some statistics to SmartInspect, like memory consumption, working set, etc.

Is it possible for this thread to get a reasonably accurate measure of how much of the computer's CPU resources it consumes? The main program is a single-threaded application (apart from the watchdog thread that logs statistics) so if a technique is limited to how much does a single thread use then that would be good too.

I found some entries related to something called rusage for Linux and C. Is there something similar I can use for this?


Edit: Ok, I tried the performance counter way, but it added quite a lot of GC-data each time called, so the graph for memory usage and garbage collection skyrocketed. I guess I'll just leave this part out for now.

like image 234
Lasse V. Karlsen Avatar asked Nov 09 '08 14:11

Lasse V. Karlsen


People also ask

Do AC recharge cans work?

Are Air Conditioning Recharge Kits Worth It? No, they are not because they don't fix broken AC systems. Instead, they simply recharge refrigerant and leave the cause of the problem unattended. So while a recharge may get cool air blowing again, it masks the real issue as it worsens.

How many cans of AC Pro do I need?

Most cars hold between 28 and 32 ounces of refrigerant (or about 2—3 12oz cans), however larger vehicles and those with rear A/C will likely hold more. Check your vehicle manual for the system capacity for your specific vehicle.

Is AC used for heat?

Air Conditioning CompressorYou can use the a/c compressor while controlling the heat setting of the car to control the climate within.

Are all AC recharge kits the same?

AC recharge kits come in a variety of prices and capabilities. Some are just refrigerant in a can with a hose and a gauge. Others include supplies to diagnose issues beyond a low refrigerant pressure and make further repairs. Some of those can even be used to diagnose and recharge home HVAC systems.


2 Answers

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

like image 170
axk Avatar answered Oct 05 '22 23:10

axk


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'.

like image 35
Sunlight Avatar answered Oct 05 '22 23:10

Sunlight