diff options
author | Jonas Hahnfeld <hahnjo@hahnjo.de> | 2017-11-11 13:59:48 +0000 |
---|---|---|
committer | Jonas Hahnfeld <hahnjo@hahnjo.de> | 2017-11-11 13:59:48 +0000 |
commit | d0ef19ef9b2c4c61ecd10b133632c795c3bc2d31 (patch) | |
tree | 0887d4ea63782e1a28eaca6791a334261bbc6998 /openmp/runtime/src/ompt-general.cpp | |
parent | 3acaa701adbb0820d968ffcd4886e443f9fe9ca3 (diff) | |
download | bcm5719-llvm-d0ef19ef9b2c4c61ecd10b133632c795c3bc2d31.tar.gz bcm5719-llvm-d0ef19ef9b2c4c61ecd10b133632c795c3bc2d31.zip |
[OMPT] Provide initialization for Mac OS X
Traditionally, the library had a weak symbol for ompt_start_tool()
that served as fallback and disabled OMPT if called. Tools could
provide their own version and replace the default implementation
to register callbacks and lookup functions. This mechanism has
worked reasonably well on Linux systems where this interface was
initially developed.
On Darwin / Mac OS X the situation is a bit more complicated and
the weak symbol doesn't work out-of-the-box. In my tests, the
library with the tool needed to link against the OpenMP runtime
to make the process work. This would effectively mean that a tool
needed to choose a runtime library whereas one design goal of the
interface was to allow tools that are agnostic of the runtime.
The solution is to use dlsym() with the argument RTLD_DEFAULT so
that static implementations of ompt_start_tool() are found in the
main executable. This works because the linker on Mac OS X includes
all symbols of an executable in the global symbol table by default.
To use the same code path on Linux, the application would need to
be built with -Wl,--export-dynamic. To avoid this restriction, we
continue to use weak symbols on Linux systems as before.
Finally this patch extends the existing test to cover all possible
ways of initializing the tool as described by the standard. It
also fixes ompt_finalize() to not call omp_get_thread_num() when
the library is shut down which resulted in hangs on Darwin.
The changes have been tested on Linux to make sure that it passes
the current tests as well as the newly extended one.
Differential Revision: https://reviews.llvm.org/D39801
llvm-svn: 317980
Diffstat (limited to 'openmp/runtime/src/ompt-general.cpp')
-rw-r--r-- | openmp/runtime/src/ompt-general.cpp | 86 |
1 files changed, 53 insertions, 33 deletions
diff --git a/openmp/runtime/src/ompt-general.cpp b/openmp/runtime/src/ompt-general.cpp index 056e53bd65a..c1e8b0f22eb 100644 --- a/openmp/runtime/src/ompt-general.cpp +++ b/openmp/runtime/src/ompt-general.cpp @@ -88,51 +88,62 @@ OMPT_API_ROUTINE ompt_data_t *ompt_get_thread_data(void); * initialization and finalization (private operations) ****************************************************************************/ -/* On Unix-like systems that support weak symbols the following implementation - * of ompt_start_tool() will be used in case no tool-supplied implementation of - * this function is present in the address space of a process. - * - * On Windows, the ompt_tool_windows function is used to find the - * ompt_tool symbol across all modules loaded by a process. If ompt_tool is - * found, ompt_tool's return value is used to initialize the tool. Otherwise, - * NULL is returned and OMPT won't be enabled */ - typedef ompt_start_tool_result_t *(*ompt_start_tool_t)(unsigned int, const char *); -#if KMP_OS_UNIX +#if KMP_OS_DARWIN -#if OMPT_HAVE_WEAK_ATTRIBUTE -_OMP_EXTERN OMPT_WEAK_ATTRIBUTE -#elif defined KMP_DYNAMIC_LIB -_OMP_EXTERN -#warning Activation of OMPT is might fail for tools statically linked into the application. -#else -#error Activation of OMPT is not supported on this platform. -#endif -ompt_start_tool_result_t * +// While Darwin supports weak symbols, the library that wishes to provide a new +// implementation has to link against this runtime which defeats the purpose +// of having tools that are agnostic of the underlying runtime implementation. +// +// Fortunately, the linker includes all symbols of an executable in the global +// symbol table by default so dlsym() even finds static implementations of +// ompt_start_tool. For this to work on Linux, -Wl,--export-dynamic needs to be +// passed when building the application which we don't want to rely on. + +static ompt_start_tool_result_t *ompt_tool_darwin(unsigned int omp_version, + const char *runtime_version) { + ompt_start_tool_result_t *ret = NULL; + // Search symbol in the current address space. + ompt_start_tool_t start_tool = + (ompt_start_tool_t)dlsym(RTLD_DEFAULT, "ompt_start_tool"); + if (start_tool) { + ret = start_tool(omp_version, runtime_version); + } + return ret; +} + +#elif OMPT_HAVE_WEAK_ATTRIBUTE + +// On Unix-like systems that support weak symbols the following implementation +// of ompt_start_tool() will be used in case no tool-supplied implementation of +// this function is present in the address space of a process. + +_OMP_EXTERN OMPT_WEAK_ATTRIBUTE ompt_start_tool_result_t * ompt_start_tool(unsigned int omp_version, const char *runtime_version) { -#ifdef KMP_DYNAMIC_LIB ompt_start_tool_result_t *ret = NULL; - // Try next symbol in the address space + // Search next symbol in the current address space. This can happen if the + // runtime library is linked before the tool. Since glibc 2.2 strong symbols + // don't override weak symbols that have been found before unless the user + // sets the environment variable LD_DYNAMIC_WEAK. ompt_start_tool_t next_tool = (ompt_start_tool_t)dlsym(RTLD_NEXT, "ompt_start_tool"); - if (next_tool) - ret = (next_tool)(omp_version, runtime_version); + if (next_tool) { + ret = next_tool(omp_version, runtime_version); + } return ret; -#else -#if OMPT_DEBUG - printf("ompt_start_tool() is called from the RTL\n"); -#endif - return NULL; -#endif } #elif OMPT_HAVE_PSAPI +// On Windows, the ompt_tool_windows function is used to find the +// ompt_start_tool symbol across all modules loaded by a process. If +// ompt_start_tool is found, ompt_start_tool's return value is used to +// initialize the tool. Otherwise, NULL is returned and OMPT won't be enabled. + #include <psapi.h> #pragma comment(lib, "psapi.lib") -#define ompt_start_tool ompt_tool_windows // The number of loaded modules to start enumeration with EnumProcessModules() #define NUM_MODULES 128 @@ -193,8 +204,8 @@ ompt_tool_windows(unsigned int omp_version, const char *runtime_version) { return NULL; } #else -#error Either __attribute__((weak)) or psapi.dll are required for OMPT support -#endif // OMPT_HAVE_WEAK_ATTRIBUTE +#error Activation of OMPT is not supported on this platform. +#endif static ompt_start_tool_result_t * ompt_try_start_tool(unsigned int omp_version, const char *runtime_version) { @@ -208,7 +219,16 @@ ompt_try_start_tool(unsigned int omp_version, const char *runtime_version) { #endif // Try in the current address space - if ((ret = ompt_start_tool(omp_version, runtime_version))) +#if KMP_OS_DARWIN + ret = ompt_tool_darwin(omp_version, runtime_version); +#elif OMPT_HAVE_WEAK_ATTRIBUTE + ret = ompt_start_tool(omp_version, runtime_version); +#elif OMPT_HAVE_PSAPI + ret = ompt_tool_windows(omp_version, runtime_version); +#else +#error Activation of OMPT is not supported on this platform. +#endif + if (ret) return ret; // Try tool-libraries-var ICV |