Are performance counters the right tool for my job? If so, how to use them?

I was asked how to find out how much memory and processor is used by my program, and I was sent to performance counters. However, when I look at the documentation, it begins by telling me about the provision of consumer data, creating a manifest for my application, and my eyes blur. It's nice that there is such a universal tool, I suppose, but all I wanted was a set of functions that tell me how much memory and processor my program uses.

Are performance counters the right tool for my job? If so, how to use them? I really don't want to configure anything outside of my C # application.

NOTE. I'm not looking for a third-party profiling application, I need to have the memory and CPU data displayed by my application.

UPDATE: I deleted the time in the functions, as this seemed to confuse the matter.

+7
c # performancecounter
source share
5 answers

You can use perfmon out of the box to monitor CPU, memory and disk usage, as well as many special .NET performance counters. perfmon comes with windows.

Glaze is only required if you want to write your own perfmon or want to offer your own performance counters. The latter would be interesting only if your users would need to track this value (for example, you are developing a server application, and your users should make sure that there were no more than 10% of connections rejected due to an invalid moon phase).

Use the profiler to determine how much time you spend on certain functions.


The performance counter API allows you to control data along with existing performance counters (which can tell you, for example, that the Foo function becomes very slow every night after 11, because some other process crashes the disk), and the monitor can work as a service and create log files for later analysis.

You should find out if these benefits deserve additional problems with performance counters, or if you are better off working with a logging system.

There are quite a few examples out there that can make your work easier. However, he still pays to understand architecture and “official” terminology. Usually, the MS API do requires a lot of reading and looks for good shell code, which does not mean that they are always painful.

+3
source share

Assuming a single instance application to use the CPU and memory (Process Working Set), you can do something like the following:

 public static void Main(string[] args) { string processName = Process.GetCurrentProcess().ProcessName; int processorCount = Environment.ProcessorCount; // Simulate process usage for (int i = 0; i < processorCount * 2; i++) { var t = new Thread(() => { while (true) { Console.WriteLine("Busy..."); } }); t.IsBackground = true; t.Start(); } var cpu = new PerformanceCounter("Process", "% Processor Time"); cpu.InstanceName = processName; cpu.NextValue(); var ram = new PerformanceCounter("Process", "Working Set"); ram.InstanceName = processName; float memUsage = (ram.NextValue() / 1024) / 1024; Console.WriteLine("Memory Usage: {0}MB", memUsage); float cpuUsage = cpu.NextValue() / (float)processorCount; Console.WriteLine("CPU Usage: {0}%", cpuUsage); } 

For the processor performance counter, the first NextValue call returns 0, so only the second call is used to display the value. Reported usage is the sum of all machine processors, so the average is calculated before displaying.

+3
source share

For simple information, look at these values ​​from C # code, and then choose what is most important to you:

 System.Diagnostics.Process.GetCurrentProcess (). TotalProcessorTime
 System.Diagnostics.Process.GetCurrentProcess (). PrivateMemorySize64
 System.Diagnostics.Process.GetCurrentProcess (). VirtualMemorySize64
 System.GC.GetTotalMemory (false)

PrivateMemorySize are the total private bytes (managed and unmanaged together) for your process. GetTotalMemory gives you the total use of managed memory for your application, and passing the truth will force the GC to get the value.

+2
source share

Performance counters won't tell you how much time your application spends in functions. To do this, you can use some kind of timer class that you start at the beginning of the function and stop at the end.

+1
source share

how much time is spent in function

To do this, you need a profiler (for example, one that is included in more complex versions of Visual Studio). This will give you detailed information about how many times each method is called (with calls or without functions).

Key details for identifying bottlenecks.

CPU

A common processor-second performance counter (or columns in the task manager or Process Explorer) will provide you with this. In practice, you may need more detailed information (kernel versus user time), which is also available.

memory

This is the hard part .... "memory" can mean: dedicated virtual address space, dedicated virtual address space, working set, peak working set, private memory, ...

You probably need to spend some time on the internal components of Windows or a similar link to understand how they are different and what they mean before starting any real analysis.

0
source share

All Articles