5

Does anyone know about a profiler library in C# that I could embed in my source code that would do sample-based profiling? I.e. periodically get the instruction pointer location, store it in memory and allow to save it to a file and analyze later, presumably by some desktop application?

I know there are lots of traditional profiler applications (like JetBrains, Ants etc.), but I want to profile a C# program running on a non-desktop platform, where none of these profilers can be used. I want my application to sample itself, not an external profiler.

kaalus
  • 4,475
  • 3
  • 29
  • 39
  • I presume that System.Diagnostics.Stopwatch is not enough ? – Antonio Bakula Aug 29 '12 at 09:39
  • @AntonioBakula I want a sampling profiler that does not require me to instrument the code to be profiled. – kaalus Aug 29 '12 at 09:49
  • The program counter is the *least* useful thing you can sample. It is much better to grab samples of the call stack, and for each call on the stack, not just the function doing the calling, but the line it is calling from. What's more, high sampling frequency is not essential. [*More on that.*](http://stackoverflow.com/a/1779343/23771) – Mike Dunlavey Aug 30 '12 at 14:34
  • @MikeDunlavey You are right of course, when I said to capture the instruction pointer location what I meant was to get the current stack trace. I don't think there's a way to get IP from C# anyway, while there is the StackTrace class. – kaalus Sep 03 '12 at 09:45
  • @kaalus: Good luck. More on that: I and others have fund the best way to do performance tuning is to humanly eyeball a small number of the actual raw (un-summarized) samples. You see opportunities that would otherwise be missed. There's rational justification for that [*here*](http://scicomp.stackexchange.com/a/2719/1262). – Mike Dunlavey Sep 03 '12 at 12:31

2 Answers2

1

The Eqatec profiler might suit. I know at least in earlier versions the profiling got compiled into your binaries.

The user guide seems to provide a good overview of how to use it:

http://eqatec.zendesk.com/entries/20325613-user-guide#Intro

scdove
  • 586
  • 5
  • 10
0

The ETW tracing infrastructure provides that capability. Try running the perfview tool by Microsoft. It uses ETW to show sampled stack traces.

You can use ETW yourself to build a sampling profiler. It might be a little work, though.

usr
  • 168,620
  • 35
  • 240
  • 369
  • As far as I can see ETW (Event Tracing for Windows) is a tracing framework that provides logging to circular log files. There is no profiling of any kind there. And it does not work outside Windows anyway, which is the key point. Please correct me if I am wrong. – kaalus Aug 29 '12 at 10:32
  • You can turn on an ETW provider that fires an event once every 1ms with a stack trace of all running processes. You can make the app sample itself that way. The simplest way would be to ship perfview.exe as a resource, extract it and run it using command line switches. It will write the trace data to a well-known file format that can be opened on another machine. – usr Aug 29 '12 at 10:35
  • I did not pick up the requirement for Windows from the question. ETW is Windows-only. A cross-platform profiling lib would have to use two different profiling engines anyway. – usr Aug 29 '12 at 10:36