sample.c 1.7 KB

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