What is the way to get the number of commands executed?

I read that

One way to understand how a processor used its time is to look at hardware counters. To aid in performance tuning, processors keep track of various counters while executing code: the number of instructions executed, the number of different types of memory accesses, the number of branches encountered, etc. To read the counters, you will need a tool like the profiler in Visual Studio 2010 Premium or Ultimate, AMD Code Analyst, or Intel VTune.

So what is the way to do this by coding, how to use a PerformanceCounter AND get this number of commands executed?

And is there a way to count the hit of instructions like DotTrace, Ants, VS profiler?

+3


source to share


2 answers


For VS Profiler :

To see a list of a list of all CPU counters supported on the current platform

In the Performance Explorer, right-click a performance session and select Properties.

Perform one of the following actions:

  • Click Sampling, and then select Performance Counter from the Sample list. The CPU counters are listed under Available Performance Counters. Note. Click Cancel to return to the previous fetch configuration.

-or -

  1. Select CPU Counters and then select Collect CPU Counters. CPU counters are listed under Available Counters. Note. Click Cancel to return to your previous counter collection configuration.

You cannot access the full cpu counter from your program because: fooobar.com/questions/665718 / ...

You can use the RDPMC statement or the MVKC native __readpmc compiler, which is the same.

However, Windows prevents user-mode applications from executing this instruction by setting CR4.PCE to 0. Presumably this is because the value of each counter is determined by the MSR registers, which are only accessible in kernel mode. In other words, if you are not a kernel-mode module (such as a device driver), you will catch the "privileged command" trap if you try to execute this instruction.



(RDPMC is a command that returns CPU counters)

I will add that usually the number of commands executed is completely useless. The important thing is that CPU time was used to execute some code. Each instruction has a different CPU time, so even if you know the number of them, you will not know the number of CPU cycles / time.

If you want to know the CPU cycles used for some instructions, you can use the ASM RDTSC / RDTSCP instruction. Using it in C # is tricky and quite time consuming (so using it is slow enough, which often compromises the dimension you are trying to do). If you're interested, I wrote an answer about this a few days ago: fooobar.com/questions/1582498 / ...

+1


source


Already answered your question , so I think this is a duplicate question.

You can most likely call native methods in WinAPI from C # via DLLImport. But for simplicity you can try to use the package from here .



But you must clearly understand what you are doing. There will be a difference between the first call to your function and the second due to JITting time. And if your method allocates memory - GC can occur anytime your method is called and it will be reflected in the dimension.

+1


source







All Articles