diff options
Diffstat (limited to 'openmp/runtime/src')
-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 |