[profiler] fixes to profiler sample
[mono.git] / samples / profiler / sample.c
1 #include <mono/metadata/profiler.h>
2
3 /*
4  * Bare bones profiler. Compile with:
5  * 
6  * linux : gcc -fPIC -shared -o libmono-profiler-sample.so sample.c `pkg-config --cflags --libs mono-2`
7  * mac : gcc sample.c -o mono-profiler-sample.dylib -Dmono_free=free -lz `pkg-config --cflags mono-2` -undefined suppress -flat_namespace  
8  * linux with a custom prefix (e.g. --prefix=/opt/my-mono-build):
9  *      gcc -fPIC -shared -o libmono-profiler-sample.so sample.c `PKG_CONFIG_PATH=/opt/my-mono-build/lib/pkgconfig/ pkg-config --cflags --libs mono-2`
10  *
11  * Install the binary where the dynamic loader can find it. eg /usr/lib etc.
12  * For a custom prefix build, <prefix>/lib would also work.
13  * Then run mono with:
14  * mono --profile=sample your_application.exe
15  *
16  * Note if you name a profiler with more than 8 characters (eg sample6789) appears to not work
17  */
18
19 struct _MonoProfiler {
20         unsigned long long ncalls;
21 };
22
23 static MonoProfiler prof_instance;
24
25 /* called at the end of the program */
26 static void
27 sample_shutdown (MonoProfiler *prof)
28 {
29         printf("total number of calls: %llu\n", prof->ncalls);
30 }
31
32 static void
33 sample_method_enter (MonoProfiler *prof, MonoMethod *method, MonoProfilerCallContext *ctx)
34 {
35         prof->ncalls++;
36 }
37
38 static void
39 sample_method_leave (MonoProfiler *prof, MonoMethod *method, MonoProfilerCallContext *ctx)
40 {
41 }
42
43 static MonoProfilerCallInstrumentationFlags
44 sample_instrumentation_filter (MonoProfiler *prof, MonoMethod *method)
45 {
46         return MONO_PROFILER_CALL_INSTRUMENTATION_PROLOGUE | MONO_PROFILER_CALL_INSTRUMENTATION_EPILOGUE;
47 }
48
49 /* the entry point */
50 void
51 mono_profiler_init_sample (const char *desc)
52 {
53         MonoProfiler *prof = &prof_instance;
54
55         MonoProfilerHandle handle = mono_profiler_create (prof);
56         mono_profiler_set_runtime_shutdown_end_callback (handle, sample_shutdown);
57         mono_profiler_set_call_instrumentation_filter_callback (handle, sample_instrumentation_filter);
58         mono_profiler_set_method_enter_callback (handle, sample_method_enter);
59         mono_profiler_set_method_leave_callback (handle, sample_method_leave);
60 }
61
62