31#include "ompt-specific.cpp"
37#define ompt_get_callback_success 1
38#define ompt_get_callback_failure 0
40#define no_tool_present 0
42#define OMPT_API_ROUTINE static
45#define OMPT_STR_MATCH(haystack, needle) (!strcasecmp(haystack, needle))
52#define OMPT_VERBOSE_INIT_PRINT(...) \
54 fprintf(verbose_file, __VA_ARGS__)
55#define OMPT_VERBOSE_INIT_CONTINUED_PRINT(...) \
57 fprintf(verbose_file, __VA_ARGS__)
59static FILE *verbose_file;
60static int verbose_init;
67 const char *state_name;
68 ompt_state_t state_id;
74} kmp_mutex_impl_info_t;
87ompt_callbacks_active_t ompt_enabled;
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
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
101ompt_callbacks_internal_t ompt_callbacks;
103static ompt_start_tool_result_t *ompt_start_tool_result = NULL;
106static HMODULE ompt_tool_module = NULL;
107#define OMPT_DLCLOSE(Lib) FreeLibrary(Lib)
109static void *ompt_tool_module = NULL;
110#define OMPT_DLCLOSE(Lib) dlclose(Lib)
114static ompt_start_tool_result_t *libomptarget_ompt_result = NULL;
120static ompt_interface_fn_t ompt_fn_lookup(
const char *s);
122OMPT_API_ROUTINE ompt_data_t *ompt_get_thread_data(
void);
128typedef ompt_start_tool_result_t *(*ompt_start_tool_t)(
unsigned int,
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;
146 ompt_start_tool_t start_tool =
147 (ompt_start_tool_t)dlsym(RTLD_DEFAULT,
"ompt_start_tool");
149 ret = start_tool(omp_version, runtime_version);
154#elif OMPT_HAVE_WEAK_ATTRIBUTE
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;
167 ompt_start_tool_t next_tool =
168 (ompt_start_tool_t)dlsym(RTLD_NEXT,
"ompt_start_tool");
170 ret = next_tool(omp_version, runtime_version);
183#pragma comment(lib, "psapi.lib")
186#define NUM_MODULES 128
188static ompt_start_tool_result_t *
189ompt_tool_windows(
unsigned int omp_version,
const char *runtime_version) {
191 DWORD needed, new_size;
193 HANDLE process = GetCurrentProcess();
194 modules = (HMODULE *)malloc(NUM_MODULES *
sizeof(HMODULE));
195 ompt_start_tool_t ompt_tool_p = NULL;
198 printf(
"ompt_tool_windows(): looking for ompt_start_tool\n");
200 if (!EnumProcessModules(process, modules, NUM_MODULES *
sizeof(HMODULE),
207 new_size = needed /
sizeof(HMODULE);
208 if (new_size > NUM_MODULES) {
210 printf(
"ompt_tool_windows(): resize buffer to %d bytes\n", needed);
212 modules = (HMODULE *)realloc(modules, needed);
214 if (!EnumProcessModules(process, modules, needed, &needed)) {
219 for (i = 0; i < new_size; ++i) {
220 (FARPROC &)ompt_tool_p = GetProcAddress(modules[i],
"ompt_start_tool");
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",
229 return (*ompt_tool_p)(omp_version, runtime_version);
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",
244#error Activation of OMPT is not supported on this platform.
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;
253 const char *sep =
";";
255 const char *sep =
":";
258 OMPT_VERBOSE_INIT_PRINT(
"----- START LOGGING OF TOOL REGISTRATION -----\n");
259 OMPT_VERBOSE_INIT_PRINT(
"Search for OMP tool in 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);
267 ret = ompt_tool_windows(omp_version, runtime_version);
269#error Activation of OMPT is not supported on this platform.
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");
280 OMPT_VERBOSE_INIT_CONTINUED_PRINT(
"Failed.\n");
281 const char *tool_libs = getenv(
"OMP_TOOL_LIBRARIES");
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);
287 char *fname = __kmp_str_token(libs, sep, &buf);
293 OMPT_VERBOSE_INIT_PRINT(
"Opening %s... ", fname);
294 void *h = dlopen(fname, RTLD_LAZY);
296 OMPT_VERBOSE_INIT_CONTINUED_PRINT(
"Failed: %s\n", dlerror());
298 OMPT_VERBOSE_INIT_CONTINUED_PRINT(
"Success. \n");
299 OMPT_VERBOSE_INIT_PRINT(
"Searching for ompt_start_tool in %s... ",
302 start_tool = (ompt_start_tool_t)dlsym(h,
"ompt_start_tool");
304 char *error = dlerror();
306 OMPT_VERBOSE_INIT_CONTINUED_PRINT(
"Failed: %s\n", error);
308 OMPT_VERBOSE_INIT_CONTINUED_PRINT(
"Failed: %s\n",
309 "ompt_start_tool = NULL");
313 OMPT_VERBOSE_INIT_PRINT(
"Opening %s... ", fname);
314 HMODULE h = LoadLibrary(fname);
316 OMPT_VERBOSE_INIT_CONTINUED_PRINT(
"Failed: Error %u\n",
317 (
unsigned)GetLastError());
319 OMPT_VERBOSE_INIT_CONTINUED_PRINT(
"Success. \n");
320 OMPT_VERBOSE_INIT_PRINT(
"Searching for ompt_start_tool in %s... ",
322 start_tool = (ompt_start_tool_t)GetProcAddress(h,
"ompt_start_tool");
324 OMPT_VERBOSE_INIT_CONTINUED_PRINT(
"Failed: Error %u\n",
325 (
unsigned)GetLastError());
328#error Activation of OMPT is not supported on this platform.
331 ret = (*start_tool)(omp_version, runtime_version);
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;
339 OMPT_VERBOSE_INIT_CONTINUED_PRINT(
340 "Found but not using the OMPT interface.\n");
341 OMPT_VERBOSE_INIT_PRINT(
"Continuing search...\n");
345 fname = __kmp_str_token(NULL, sep, &buf);
347 __kmp_str_free(&libs);
349 OMPT_VERBOSE_INIT_PRINT(
"No OMP_TOOL_LIBRARIES defined.\n");
354 OMPT_VERBOSE_INIT_PRINT(
"----- END LOGGING OF TOOL REGISTRATION -----\n");
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);
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");
370 ret = (*start_tool)(omp_version, runtime_version);
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");
379 OMPT_VERBOSE_INIT_CONTINUED_PRINT(
380 "Found but not using the OMPT interface.\n");
382 OMPT_VERBOSE_INIT_CONTINUED_PRINT(
"Failed: %s\n", dlerror());
387 OMPT_VERBOSE_INIT_PRINT(
"No OMP tool loaded.\n");
388 OMPT_VERBOSE_INIT_PRINT(
"----- END LOGGING OF TOOL REGISTRATION -----\n");
392void ompt_pre_init() {
396 static int ompt_pre_initialized = 0;
398 if (ompt_pre_initialized)
401 ompt_pre_initialized = 1;
406 const char *ompt_env_var = getenv(
"OMP_TOOL");
407 tool_setting_e tool_setting = omp_tool_error;
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;
416 const char *ompt_env_verbose_init = getenv(
"OMP_TOOL_VERBOSE_INIT");
419 if (ompt_env_verbose_init && strcmp(ompt_env_verbose_init,
"") &&
420 !OMPT_STR_MATCH(ompt_env_verbose_init,
"disabled")) {
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;
427 verbose_file = fopen(ompt_env_verbose_init,
"w");
432 printf(
"ompt_pre_init(): tool_setting = %d\n", tool_setting);
434 switch (tool_setting) {
435 case omp_tool_disabled:
436 OMPT_VERBOSE_INIT_PRINT(