diff options
author | Alex Rønne Petersen <alpeters@microsoft.com> | 2017-11-22 14:31:29 +0300 |
---|---|---|
committer | Alex Rønne Petersen <alpeters@microsoft.com> | 2017-11-23 16:16:03 +0300 |
commit | cbe30987b038a6a253f50a2d8aa1f29980ab8c17 (patch) | |
tree | a4b1a22262f0220a03bf97812df3f6297524c807 | |
parent | 0abcdc9c6069b90cd6d115299afaba20fe3e23b7 (diff) |
[samples] Clean up the profiler sample and add a simple makefile.
-rw-r--r-- | samples/profiler/.gitignore | 2 | ||||
-rw-r--r-- | samples/profiler/Makefile | 13 | ||||
-rw-r--r-- | samples/profiler/sample.c | 70 |
3 files changed, 60 insertions, 25 deletions
diff --git a/samples/profiler/.gitignore b/samples/profiler/.gitignore new file mode 100644 index 00000000000..cc36d59f1fe --- /dev/null +++ b/samples/profiler/.gitignore @@ -0,0 +1,2 @@ +/libmono-profiler-sample.so +/libmono-profiler-sample.dylib diff --git a/samples/profiler/Makefile b/samples/profiler/Makefile new file mode 100644 index 00000000000..365d861cabd --- /dev/null +++ b/samples/profiler/Makefile @@ -0,0 +1,13 @@ +.PHONY: all linux osx clean + +all: + +linux: + gcc -fPIC -shared -o libmono-profiler-sample.so sample.c `pkg-config --cflags mono-2` + +osx: + clang -undefined suppress -flat_namespace -o mono-profiler-sample.dylib sample.c `pkg-config --cflags mono-2` + +clean: + $(RM) -f libmono-profiler-sample.so + $(RM) -f libmono-profiler-sample.dylib diff --git a/samples/profiler/sample.c b/samples/profiler/sample.c index a210317e913..35e578be155 100644 --- a/samples/profiler/sample.c +++ b/samples/profiler/sample.c @@ -1,56 +1,76 @@ -#include <mono/metadata/profiler.h> - /* - * Bare bones profiler. Compile with: + * Bare bones profiler showing how a profiler module should be structured. + * + * Compilation: + * - Linux: gcc -fPIC -shared -o libmono-profiler-sample.so sample.c `pkg-config --cflags mono-2` + * - OS X: clang -undefined suppress -flat_namespace -o mono-profiler-sample.dylib sample.c `pkg-config --cflags mono-2` * - * linux : gcc -fPIC -shared -o libmono-profiler-sample.so sample.c `pkg-config --cflags --libs mono-2` - * mac : gcc -o mono-profiler-sample.dylib sample.c -lz `pkg-config --cflags mono-2` -undefined suppress -flat_namespace - * linux with a custom prefix (e.g. --prefix=/opt/my-mono-build): - * 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` + * If you're using a custom prefix for your Mono installation (e.g. /opt/mono), + * pkg-config must be invoked like this: PKG_CONFIG_PATH=/opt/mono pkg-config --cflags mono-2 * - * Install the binary where the dynamic loader can find it. eg /usr/lib etc. - * For a custom prefix build, <prefix>/lib would also work. - * Then run mono with: - * mono --profile=sample your_application.exe + * Install the resulting shared library where the dynamic loader can find it, + * e.g. /usr/local/lib or /opt/mono (custom prefix). * - * Note if you name a profiler with more than 8 characters (eg sample6789) appears to not work + * To use the module: mono --profile=sample hello.exe */ +#include <mono/metadata/profiler.h> + +/* + * Defining a type called _MonoProfiler will complete the opaque MonoProfiler + * type, which is used throughout the profiler API. + */ struct _MonoProfiler { + /* Handle obtained from mono_profiler_create (). */ + MonoProfilerHandle handle; + + /* Counts the number of calls observed. */ unsigned long long ncalls; }; -static MonoProfiler prof_instance; +/* + * Use static storage for the profiler structure for simplicity. The structure + * can be allocated dynamically as well, if needed. + */ +static MonoProfiler profiler; -/* called at the end of the program */ +/* + * Callback invoked after the runtime finishes shutting down. Managed code can + * no longer run and most runtime services are unavailable. + */ static void -sample_shutdown (MonoProfiler *prof) +sample_shutdown_end (MonoProfiler *prof) { - printf("total number of calls: %llu\n", prof->ncalls); + printf ("Total number of calls: %llu\n", prof->ncalls); } +/* + * Method enter callback invoked on entry to all instrumented methods. + */ static void sample_method_enter (MonoProfiler *prof, MonoMethod *method, MonoProfilerCallContext *ctx) { prof->ncalls++; } +/* + * Filter callback that decides which methods to instrument and how. + */ static MonoProfilerCallInstrumentationFlags -sample_instrumentation_filter (MonoProfiler *prof, MonoMethod *method) +sample_call_instrumentation_filter (MonoProfiler *prof, MonoMethod *method) { return MONO_PROFILER_CALL_INSTRUMENTATION_ENTER; } -/* the entry point */ +/* + * The entry point function invoked by the Mono runtime. + */ void mono_profiler_init_sample (const char *desc) { - MonoProfiler *prof = &prof_instance; + profiler.handle = mono_profiler_create (&profiler); - MonoProfilerHandle handle = mono_profiler_create (prof); - mono_profiler_set_runtime_shutdown_end_callback (handle, sample_shutdown); - mono_profiler_set_call_instrumentation_filter_callback (handle, sample_instrumentation_filter); - mono_profiler_set_method_enter_callback (handle, sample_method_enter); + mono_profiler_set_runtime_shutdown_end_callback (profiler.handle, sample_shutdown_end); + mono_profiler_set_call_instrumentation_filter_callback (profiler.handle, sample_call_instrumentation_filter); + mono_profiler_set_method_enter_callback (profiler.handle, sample_method_enter); } - - |