LLVM OpenMP* Runtime Library
Loading...
Searching...
No Matches
ompt-general.cpp
1/*
2 * ompt-general.cpp -- OMPT implementation of interface functions
3 */
4
5//===----------------------------------------------------------------------===//
6//
7// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
8// See https://llvm.org/LICENSE.txt for license information.
9// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
10//
11//===----------------------------------------------------------------------===//
12
13/*****************************************************************************
14 * system include files
15 ****************************************************************************/
16
17#include <assert.h>
18
19#include <stdint.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23#if KMP_OS_UNIX
24#include <dlfcn.h>
25#endif
26
27/*****************************************************************************
28 * ompt include files
29 ****************************************************************************/
30
31#include "ompt-specific.cpp"
32
33/*****************************************************************************
34 * macros
35 ****************************************************************************/
36
37#define ompt_get_callback_success 1
38#define ompt_get_callback_failure 0
39
40#define no_tool_present 0
41
42#define OMPT_API_ROUTINE static
43
44#ifndef OMPT_STR_MATCH
45#define OMPT_STR_MATCH(haystack, needle) (!strcasecmp(haystack, needle))
46#endif
47
48// prints for an enabled OMP_TOOL_VERBOSE_INIT.
49// In the future a prefix could be added in the first define, the second define
50// omits the prefix to allow for continued lines. Example: "PREFIX: Start
51// tool... Success." instead of "PREFIX: Start tool... PREFIX: Success."
52#define OMPT_VERBOSE_INIT_PRINT(...) \
53 if (verbose_init) \
54 fprintf(verbose_file, __VA_ARGS__)
55#define OMPT_VERBOSE_INIT_CONTINUED_PRINT(...) \
56 if (verbose_init) \
57 fprintf(verbose_file, __VA_ARGS__)
58
59static FILE *verbose_file;
60static int verbose_init;
61
62/*****************************************************************************
63 * types
64 ****************************************************************************/
65
66typedef struct {
67 const char *state_name;
68 ompt_state_t state_id;
69} ompt_state_info_t;
70
71typedef struct {
72 const char *name;
73 kmp_mutex_impl_t id;
74} kmp_mutex_impl_info_t;
75
76enum tool_setting_e {
77 omp_tool_error,
78 omp_tool_unset,
79 omp_tool_disabled,
80 omp_tool_enabled
81};
82
83/*****************************************************************************
84 * global variables
85 ****************************************************************************/
86
87ompt_callbacks_active_t ompt_enabled;
88
89ompt_state_info_t ompt_state_info[] = {
90#define ompt_state_macro(state, code) {#state, state},
91 FOREACH_OMPT_STATE(ompt_state_macro)
92#undef ompt_state_macro
93};
94
95kmp_mutex_impl_info_t kmp_mutex_impl_info[] = {
96#define kmp_mutex_impl_macro(name, id) {#name, name},
97 FOREACH_KMP_MUTEX_IMPL(kmp_mutex_impl_macro)
98#undef kmp_mutex_impl_macro
99};
100
101ompt_callbacks_internal_t ompt_callbacks;
102
103static ompt_start_tool_result_t *ompt_start_tool_result = NULL;
104
105#if KMP_OS_WINDOWS
106static HMODULE ompt_tool_module = NULL;
107#define OMPT_DLCLOSE(Lib) FreeLibrary(Lib)
108#else
109static void *ompt_tool_module = NULL;
110#define OMPT_DLCLOSE(Lib) dlclose(Lib)
111#endif
112
114static ompt_start_tool_result_t *libomptarget_ompt_result = NULL;
115
116/*****************************************************************************
117 * forward declarations
118 ****************************************************************************/
119
120static ompt_interface_fn_t ompt_fn_lookup(const char *s);
121
122OMPT_API_ROUTINE ompt_data_t *ompt_get_thread_data(void);
123
124/*****************************************************************************
125 * initialization and finalization (private operations)
126 ****************************************************************************/
127
128typedef ompt_start_tool_result_t *(*ompt_start_tool_t)(unsigned int,
129 const char *);
130
131#if KMP_OS_DARWIN
132
133// While Darwin supports weak symbols, the library that wishes to provide a new
134// implementation has to link against this runtime which defeats the purpose
135// of having tools that are agnostic of the underlying runtime implementation.
136//
137// Fortunately, the linker includes all symbols of an executable in the global
138// symbol table by default so dlsym() even finds static implementations of
139// ompt_start_tool. For this to work on Linux, -Wl,--export-dynamic needs to be
140// passed when building the application which we don't want to rely on.
141
142static ompt_start_tool_result_t *ompt_tool_darwin(unsigned int omp_version,
143 const char *runtime_version) {
144 ompt_start_tool_result_t *ret = NULL;
145 // Search symbol in the current address space.
146 ompt_start_tool_t start_tool =
147 (ompt_start_tool_t)dlsym(RTLD_DEFAULT, "ompt_start_tool");
148 if (start_tool) {
149 ret = start_tool(omp_version, runtime_version);
150 }
151 return ret;
152}
153
154#elif OMPT_HAVE_WEAK_ATTRIBUTE
155
156// On Unix-like systems that support weak symbols the following implementation
157// of ompt_start_tool() will be used in case no tool-supplied implementation of
158// this function is present in the address space of a process.
159
160_OMP_EXTERN OMPT_WEAK_ATTRIBUTE ompt_start_tool_result_t *
161ompt_start_tool(unsigned int omp_version, const char *runtime_version) {
162 ompt_start_tool_result_t *ret = NULL;
163 // Search next symbol in the current address space. This can happen if the
164 // runtime library is linked before the tool. Since glibc 2.2 strong symbols
165 // don't override weak symbols that have been found before unless the user
166 // sets the environment variable LD_DYNAMIC_WEAK.
167 ompt_start_tool_t next_tool =
168 (ompt_start_tool_t)dlsym(RTLD_NEXT, "ompt_start_tool");
169 if (next_tool) {
170 ret = next_tool(omp_version, runtime_version);
171 }
172 return ret;
173}
174
175#elif OMPT_HAVE_PSAPI
176
177// On Windows, the ompt_tool_windows function is used to find the
178// ompt_start_tool symbol across all modules loaded by a process. If
179// ompt_start_tool is found, ompt_start_tool's return value is used to
180// initialize the tool. Otherwise, NULL is returned and OMPT won't be enabled.
181
182#include <psapi.h>
183#pragma comment(lib, "psapi.lib")
184
185// The number of loaded modules to start enumeration with EnumProcessModules()
186#define NUM_MODULES 128
187
188static ompt_start_tool_result_t *
189ompt_tool_windows(unsigned int omp_version, const char *runtime_version) {
190 int i;
191 DWORD needed, new_size;
192 HMODULE *modules;
193 HANDLE process = GetCurrentProcess();
194 modules = (HMODULE *)malloc(NUM_MODULES * sizeof(HMODULE));
195 ompt_start_tool_t ompt_tool_p = NULL;
196
197#if OMPT_DEBUG
198 printf("ompt_tool_windows(): looking for ompt_start_tool\n");
199#endif
200 if (!EnumProcessModules(process, modules, NUM_MODULES * sizeof(HMODULE),
201 &needed)) {
202 // Regardless of the error reason use the stub initialization function
203 free(modules);
204 return NULL;
205 }
206 // Check if NUM_MODULES is enough to list all modules
207 new_size = needed / sizeof(HMODULE);
208 if (new_size > NUM_MODULES) {
209#if OMPT_DEBUG
210 printf("ompt_tool_windows(): resize buffer to %d bytes\n", needed);
211#endif
212 modules = (HMODULE *)realloc(modules, needed);
213 // If resizing failed use the stub function.
214 if (!EnumProcessModules(process, modules, needed, &needed)) {
215 free(modules);
216 return NULL;
217 }
218 }
219 for (i = 0; i < new_size; ++i) {
220 (FARPROC &)ompt_tool_p = GetProcAddress(modules[i], "ompt_start_tool");
221 if (ompt_tool_p) {
222#if OMPT_DEBUG
223 TCHAR modName[MAX_PATH];
224 if (GetModuleFileName(modules[i], modName, MAX_PATH))
225 printf("ompt_tool_windows(): ompt_start_tool found in module %s\n",
226 modName);
227#endif
228 free(modules);
229 return (*ompt_tool_p)(omp_version, runtime_version);
230 }
231#if OMPT_DEBUG
232 else {
233 TCHAR modName[MAX_PATH];
234 if (GetModuleFileName(modules[i], modName, MAX_PATH))
235 printf("ompt_tool_windows(): ompt_start_tool not found in module %s\n",
236 modName);
237 }
238#endif
239 }
240 free(modules);
241 return NULL;
242}
243#else
244#error Activation of OMPT is not supported on this platform.
245#endif
246
247static ompt_start_tool_result_t *
248ompt_try_start_tool(unsigned int omp_version, const char *runtime_version) {
249 ompt_start_tool_result_t *ret = NULL;
250 ompt_start_tool_t start_tool = NULL;
251#if KMP_OS_WINDOWS
252 // Cannot use colon to describe a list of absolute paths on Windows
253 const char *sep = ";";
254#else
255 const char *sep = ":";
256#endif
257
258 OMPT_VERBOSE_INIT_PRINT("----- START LOGGING OF TOOL REGISTRATION -----\n");
259 OMPT_VERBOSE_INIT_PRINT("Search for OMP tool in current address space... ");
260
261#if KMP_OS_DARWIN
262 // Try in the current address space
263 ret = ompt_tool_darwin(omp_version, runtime_version);
264#elif OMPT_HAVE_WEAK_ATTRIBUTE
265 ret = ompt_start_tool(omp_version, runtime_version);
266#elif OMPT_HAVE_PSAPI
267 ret = ompt_tool_windows(omp_version, runtime_version);
268#else
269#error Activation of OMPT is not supported on this platform.
270#endif
271 if (ret) {
272 OMPT_VERBOSE_INIT_CONTINUED_PRINT("Success.\n");
273 OMPT_VERBOSE_INIT_PRINT(
274 "Tool was started and is using the OMPT interface.\n");
275 OMPT_VERBOSE_INIT_PRINT("----- END LOGGING OF TOOL REGISTRATION -----\n");
276 return ret;
277 }
278
279 // Try tool-libraries-var ICV
280 OMPT_VERBOSE_INIT_CONTINUED_PRINT("Failed.\n");
281 const char *tool_libs = getenv("OMP_TOOL_LIBRARIES");
282 if (tool_libs) {
283 OMPT_VERBOSE_INIT_PRINT("Searching tool libraries...\n");
284 OMPT_VERBOSE_INIT_PRINT("OMP_TOOL_LIBRARIES = %s\n", tool_libs);
285 char *libs = __kmp_str_format("%s", tool_libs);
286 char *buf;
287 char *fname = __kmp_str_token(libs, sep, &buf);
288 // Reset dl-error
289 dlerror();
290
291 while (fname) {
292#if KMP_OS_UNIX
293 OMPT_VERBOSE_INIT_PRINT("Opening %s... ", fname);
294 void *h = dlopen(fname, RTLD_LAZY);
295 if (!h) {
296 OMPT_VERBOSE_INIT_CONTINUED_PRINT("Failed: %s\n", dlerror());
297 } else {
298 OMPT_VERBOSE_INIT_CONTINUED_PRINT("Success. \n");
299 OMPT_VERBOSE_INIT_PRINT("Searching for ompt_start_tool in %s... ",
300 fname);
301 dlerror(); // Clear any existing error
302 start_tool = (ompt_start_tool_t)dlsym(h, "ompt_start_tool");
303 if (!start_tool) {
304 char *error = dlerror();
305 if (error != NULL) {
306 OMPT_VERBOSE_INIT_CONTINUED_PRINT("Failed: %s\n", error);
307 } else {
308 OMPT_VERBOSE_INIT_CONTINUED_PRINT("Failed: %s\n",
309 "ompt_start_tool = NULL");
310 }
311 } else
312#elif KMP_OS_WINDOWS
313 OMPT_VERBOSE_INIT_PRINT("Opening %s... ", fname);
314 HMODULE h = LoadLibrary(fname);
315 if (!h) {
316 OMPT_VERBOSE_INIT_CONTINUED_PRINT("Failed: Error %u\n",
317 (unsigned)GetLastError());
318 } else {
319 OMPT_VERBOSE_INIT_CONTINUED_PRINT("Success. \n");
320 OMPT_VERBOSE_INIT_PRINT("Searching for ompt_start_tool in %s... ",
321 fname);
322 start_tool = (ompt_start_tool_t)GetProcAddress(h, "ompt_start_tool");
323 if (!start_tool) {
324 OMPT_VERBOSE_INIT_CONTINUED_PRINT("Failed: Error %u\n",
325 (unsigned)GetLastError());
326 } else
327#else
328#error Activation of OMPT is not supported on this platform.
329#endif
330 { // if (start_tool)
331 ret = (*start_tool)(omp_version, runtime_version);
332 if (ret) {
333 OMPT_VERBOSE_INIT_CONTINUED_PRINT("Success.\n");
334 OMPT_VERBOSE_INIT_PRINT(
335 "Tool was started and is using the OMPT interface.\n");
336 ompt_tool_module = h;
337 break;
338 }
339 OMPT_VERBOSE_INIT_CONTINUED_PRINT(
340 "Found but not using the OMPT interface.\n");
341 OMPT_VERBOSE_INIT_PRINT("Continuing search...\n");
342 }
343 OMPT_DLCLOSE(h);
344 }
345 fname = __kmp_str_token(NULL, sep, &buf);
346 }
347 __kmp_str_free(&libs);
348 } else {
349 OMPT_VERBOSE_INIT_PRINT("No OMP_TOOL_LIBRARIES defined.\n");
350 }
351
352 // usable tool found in tool-libraries
353 if (ret) {
354 OMPT_VERBOSE_INIT_PRINT("----- END LOGGING OF TOOL REGISTRATION -----\n");
355 return ret;
356 }
357
358#if KMP_OS_UNIX
359 { // Non-standard: load archer tool if application is built with TSan
360 const char *fname = "libarcher.so";
361 OMPT_VERBOSE_INIT_PRINT(
362 "...searching tool libraries failed. Using archer tool.\n");
363 OMPT_VERBOSE_INIT_PRINT("Opening %s... ", fname);
364 void *h = dlopen(fname, RTLD_LAZY);
365 if (h) {
366 OMPT_VERBOSE_INIT_CONTINUED_PRINT("Success.\n");
367 OMPT_VERBOSE_INIT_PRINT("Searching for ompt_start_tool in %s... ", fname);
368 start_tool = (ompt_start_tool_t)dlsym(h, "ompt_start_tool");
369 if (start_tool) {
370 ret = (*start_tool)(omp_version, runtime_version);
371 if (ret) {
372 OMPT_VERBOSE_INIT_CONTINUED_PRINT("Success.\n");
373 OMPT_VERBOSE_INIT_PRINT(
374 "Tool was started and is using the OMPT interface.\n");
375 OMPT_VERBOSE_INIT_PRINT(
376 "----- END LOGGING OF TOOL REGISTRATION -----\n");
377 return ret;
378 }
379 OMPT_VERBOSE_INIT_CONTINUED_PRINT(
380 "Found but not using the OMPT interface.\n");
381 } else {
382 OMPT_VERBOSE_INIT_CONTINUED_PRINT("Failed: %s\n", dlerror());
383 }
384 }
385 }
386#endif
387 OMPT_VERBOSE_INIT_PRINT("No OMP tool loaded.\n");
388 OMPT_VERBOSE_INIT_PRINT("----- END LOGGING OF TOOL REGISTRATION -----\n");
389 return ret;
390}
391
392void ompt_pre_init() {
393 //--------------------------------------------------
394 // Execute the pre-initialization logic only once.
395 //--------------------------------------------------
396 static int ompt_pre_initialized = 0;
397
398 if (ompt_pre_initialized)
399 return;
400
401 ompt_pre_initialized = 1;
402
403 //--------------------------------------------------
404 // Use a tool iff a tool is enabled and available.
405 //--------------------------------------------------
406 const char *ompt_env_var = getenv("OMP_TOOL");
407 tool_setting_e tool_setting = omp_tool_error;
408
409 if (!ompt_env_var || !strcmp(ompt_env_var, ""))
410 tool_setting = omp_tool_unset;
411 else if (OMPT_STR_MATCH(ompt_env_var, "disabled"))
412 tool_setting = omp_tool_disabled;
413 else if (OMPT_STR_MATCH(ompt_env_var, "enabled"))
414 tool_setting = omp_tool_enabled;
415
416 const char *ompt_env_verbose_init = getenv("OMP_TOOL_VERBOSE_INIT");
417 // possible options: disabled | stdout | stderr | <filename>
418 // if set, not empty and not disabled -> prepare for logging
419 if (ompt_env_verbose_init && strcmp(ompt_env_verbose_init, "") &&
420 !OMPT_STR_MATCH(ompt_env_verbose_init, "disabled")) {
421 verbose_init = 1;
422 if (OMPT_STR_MATCH(ompt_env_verbose_init, "STDERR"))
423 verbose_file = stderr;
424 else if (OMPT_STR_MATCH(ompt_env_verbose_init, "STDOUT"))
425 verbose_file = stdout;
426 else
427 verbose_file = fopen(ompt_env_verbose_init, "w");
428 } else
429 verbose_init = 0;
430
431#if OMPT_DEBUG
432 printf("ompt_pre_init(): tool_setting = %d\n", tool_setting);
433#endif
434 switch (tool_setting) {
435 case omp_tool_disabled:
436 OMPT_VERBOSE_INIT_PRINT(