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