llvm/openmp/runtime/src/thirdparty/ittnotify/ittnotify_static.cpp


//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "kmp_config.h" // INTEL_ITTNOTIFY_PREFIX definition
#include "ittnotify_config.h"

#if ITT_PLATFORM == ITT_PLATFORM_WIN
#if !defined(PATH_MAX)
#define PATH_MAX
#endif
#else /* ITT_PLATFORM!=ITT_PLATFORM_WIN */
#include <limits.h>
#include <dlfcn.h>
#include <errno.h>
#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>

#define INTEL_NO_MACRO_BODY
#define INTEL_ITTNOTIFY_API_PRIVATE
#include "ittnotify.h"
#include "legacy/ittnotify.h"

#include "disable_warnings.h"

static const char api_version[] =;

#define _N_(n)

#ifndef HAS_CPP_ATTR
#if defined(__cplusplus) && defined(__has_cpp_attribute)
#define HAS_CPP_ATTR(X)
#else
#define HAS_CPP_ATTR
#endif
#endif

#ifndef HAS_C_ATTR
#if defined(__STDC__) && defined(__has_c_attribute)
#define HAS_C_ATTR
#else
#define HAS_C_ATTR(X)
#endif
#endif

#ifndef HAS_GNU_ATTR
#if defined(__has_attribute)
#define HAS_GNU_ATTR(X)
#else
#define HAS_GNU_ATTR
#endif
#endif

#ifndef ITT_ATTRIBUTE_FALLTHROUGH
#if (HAS_CPP_ATTR(fallthrough) || HAS_C_ATTR(fallthrough)) &&                  \
    (__cplusplus >= 201703L || _MSVC_LANG >= 201703L)
#define ITT_ATTRIBUTE_FALLTHROUGH
#elif HAS_CPP_ATTR(gnu::fallthrough)
#define ITT_ATTRIBUTE_FALLTHROUGH
#elif HAS_CPP_ATTR(clang::fallthrough)
#define ITT_ATTRIBUTE_FALLTHROUGH
#elif HAS_GNU_ATTR(fallthrough) && !__INTEL_COMPILER
#define ITT_ATTRIBUTE_FALLTHROUGH
#else
#define ITT_ATTRIBUTE_FALLTHROUGH
#endif
#endif

#if ITT_OS == ITT_OS_WIN
static const char *ittnotify_lib_name = "libittnotify.dll";
#elif ITT_OS == ITT_OS_LINUX || ITT_OS == ITT_OS_FREEBSD
static const char *ittnotify_lib_name =;
#elif ITT_OS == ITT_OS_MAC
static const char *ittnotify_lib_name = "libittnotify.dylib";
#else
#error Unsupported or unknown OS.
#endif

#ifdef __ANDROID__
#include <android/log.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <linux/limits.h>

#ifdef ITT_ANDROID_LOG
#define ITT_ANDROID_LOG_TAG
#define ITT_ANDROID_LOGI
#define ITT_ANDROID_LOGW
#define ITT_ANDROID_LOGE
#define ITT_ANDROID_LOGD
#else
#define ITT_ANDROID_LOGI
#define ITT_ANDROID_LOGW
#define ITT_ANDROID_LOGE
#define ITT_ANDROID_LOGD
#endif

/* default location of userapi collector on Android */
#define ANDROID_ITTNOTIFY_DEFAULT_PATH_MASK

#if ITT_ARCH == ITT_ARCH_IA32 || ITT_ARCH == ITT_ARCH_ARM
#define ANDROID_ITTNOTIFY_DEFAULT_PATH
#else
#define ANDROID_ITTNOTIFY_DEFAULT_PATH
#endif

#endif

#ifndef LIB_VAR_NAME
#if ITT_ARCH == ITT_ARCH_IA32 || ITT_ARCH == ITT_ARCH_ARM
#define LIB_VAR_NAME
#else
#define LIB_VAR_NAME
#endif
#endif /* LIB_VAR_NAME */

#define ITT_MUTEX_INIT_AND_LOCK(p)

#define ITT_MODULE_OBJECT_VERSION

__itt_init_ittlib_t;

/* this define used to control initialization function name. */
#ifndef __itt_init_ittlib_name
ITT_EXTERN_C int _N_(init_ittlib)(const char *, __itt_group_id);
static __itt_init_ittlib_t *__itt_init_ittlib_ptr =;
#define __itt_init_ittlib_name
#endif /* __itt_init_ittlib_name */

__itt_fini_ittlib_t;

/* this define used to control finalization function name. */
#ifndef __itt_fini_ittlib_name
ITT_EXTERN_C void _N_(fini_ittlib)(void);
static __itt_fini_ittlib_t *__itt_fini_ittlib_ptr =;
#define __itt_fini_ittlib_name
#endif /* __itt_fini_ittlib_name */

extern __itt_global _N_(_ittapi_global);

/* building pointers to imported funcs */
#undef ITT_STUBV
#undef ITT_STUB
#define ITT_STUB(api, type, name, args, params, ptr, group, format)

#define ITT_STUBV

#undef __ITT_INTERNAL_INIT
#include "ittnotify_static.h"

#undef ITT_STUB
#undef ITT_STUBV
#define ITT_STUB(api, type, name, args, params, ptr, group, format)

#define ITT_STUBV

#define __ITT_INTERNAL_INIT
#include "ittnotify_static.h"
#undef __ITT_INTERNAL_INIT

ITT_GROUP_LIST(group_list);

#pragma pack(push, 8)

__itt_group_alias;

static __itt_group_alias group_alias[] =;

#pragma pack(pop)

#if ITT_PLATFORM == ITT_PLATFORM_WIN
#if _MSC_VER
#pragma warning(push)
#pragma warning(disable : 4054) /* warning C4054: 'type cast' : from function  \
                                   pointer 'XXX' to data pointer 'void *' */
#endif
#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */

static __itt_api_info api_list[] =;

#if ITT_PLATFORM == ITT_PLATFORM_WIN
#if _MSC_VER
#pragma warning(pop)
#endif
#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */

/* static part descriptor which handles. all notification api attributes. */
__itt_global _N_(_ittapi_global) =;

__itt_api_init_t;
__itt_api_fini_t;

static __itt_domain dummy_domain;
/* ========================================================================= */

#ifdef ITT_NOTIFY_EXT_REPORT
ITT_EXTERN_C void _N_(error_handler)(__itt_error_code, va_list args);
#endif /* ITT_NOTIFY_EXT_REPORT */

#if ITT_PLATFORM == ITT_PLATFORM_WIN
#if _MSC_VER
#pragma warning(push)
#pragma warning(                                                               \
    disable : 4055) /* warning C4055: 'type cast' : from data pointer 'void *' \
                       to function pointer 'XXX' */
#endif
#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */

static void __itt_report_error(int code, ...) {}

#if ITT_PLATFORM == ITT_PLATFORM_WIN
#if _MSC_VER
#pragma warning(pop)
#endif
#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */

#if ITT_PLATFORM == ITT_PLATFORM_WIN
static __itt_domain *ITTAPI
ITT_VERSIONIZE(ITT_JOIN(_N_(domain_createW), _init))(const wchar_t *name) {
  __itt_domain *h_tail = NULL, *h = NULL;

  if (name == NULL) {
    return NULL;
  }

  ITT_MUTEX_INIT_AND_LOCK(_N_(_ittapi_global));
  if (_N_(_ittapi_global).api_initialized) {
    if (ITTNOTIFY_NAME(domain_createW) &&
        ITTNOTIFY_NAME(domain_createW) !=
            ITT_VERSIONIZE(ITT_JOIN(_N_(domain_createW), _init))) {
      __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
      return ITTNOTIFY_NAME(domain_createW)(name);
    } else {
      __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
      return &dummy_domain;
    }
  }
  for (h_tail = NULL, h = _N_(_ittapi_global).domain_list; h != NULL;
       h_tail = h, h = h->next) {
    if (h->nameW != NULL && !wcscmp(h->nameW, name))
      break;
  }
  if (h == NULL) {
    NEW_DOMAIN_W(&_N_(_ittapi_global), h, h_tail, name);
  }
  if (PTHREAD_SYMBOLS)
    __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
  return h;
}

static __itt_domain *ITTAPI ITT_VERSIONIZE(ITT_JOIN(_N_(domain_createA),
                                                    _init))(const char *name)
#else /* ITT_PLATFORM!=ITT_PLATFORM_WIN */
static __itt_domain *ITTAPI ITT_VERSIONIZE(ITT_JOIN(_N_(domain_create),
                                                    _init))(const char *name)
#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */
{}

static void ITTAPI ITT_VERSIONIZE(ITT_JOIN(
    _N_(module_load_with_sections), _init))(__itt_module_object *module_obj) {}

static void ITTAPI ITT_VERSIONIZE(ITT_JOIN(
    _N_(module_unload_with_sections), _init))(__itt_module_object *module_obj) {}

#if ITT_PLATFORM == ITT_PLATFORM_WIN
static __itt_string_handle *ITTAPI ITT_VERSIONIZE(
    ITT_JOIN(_N_(string_handle_createW), _init))(const wchar_t *name) {
  __itt_string_handle *h_tail = NULL, *h = NULL;

  if (name == NULL) {
    return NULL;
  }

  ITT_MUTEX_INIT_AND_LOCK(_N_(_ittapi_global));
  if (_N_(_ittapi_global).api_initialized) {
    if (ITTNOTIFY_NAME(string_handle_createW) &&
        ITTNOTIFY_NAME(string_handle_createW) !=
            ITT_VERSIONIZE(ITT_JOIN(_N_(string_handle_createW), _init))) {
      __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
      return ITTNOTIFY_NAME(string_handle_createW)(name);
    } else {
      __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
      return NULL;
    }
  }
  for (h_tail = NULL, h = _N_(_ittapi_global).string_list; h != NULL;
       h_tail = h, h = h->next) {
    if (h->strW != NULL && !wcscmp(h->strW, name))
      break;
  }
  if (h == NULL) {
    NEW_STRING_HANDLE_W(&_N_(_ittapi_global), h, h_tail, name);
  }
  __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
  return h;
}

static __itt_string_handle *ITTAPI
ITT_VERSIONIZE(ITT_JOIN(_N_(string_handle_createA), _init))(const char *name)
#else /* ITT_PLATFORM!=ITT_PLATFORM_WIN */
static __itt_string_handle *ITTAPI
ITT_VERSIONIZE(ITT_JOIN(_N_(string_handle_create), _init))(const char *name)
#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */
{}

#if ITT_PLATFORM == ITT_PLATFORM_WIN
static __itt_counter ITTAPI ITT_VERSIONIZE(ITT_JOIN(
    _N_(counter_createW), _init))(const wchar_t *name, const wchar_t *domain) {
  __itt_counter_info_t *h_tail = NULL, *h = NULL;
  __itt_metadata_type type = __itt_metadata_u64;

  if (name == NULL) {
    return NULL;
  }

  ITT_MUTEX_INIT_AND_LOCK(_N_(_ittapi_global));
  if (_N_(_ittapi_global).api_initialized) {
    if (ITTNOTIFY_NAME(counter_createW) &&
        ITTNOTIFY_NAME(counter_createW) !=
            ITT_VERSIONIZE(ITT_JOIN(_N_(counter_createW), _init))) {
      __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
      return ITTNOTIFY_NAME(counter_createW)(name, domain);
    } else {
      __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
      return NULL;
    }
  }
  for (h_tail = NULL, h = _N_(_ittapi_global).counter_list; h != NULL;
       h_tail = h, h = h->next) {
    if (h->nameW != NULL && h->type == (int)type && !wcscmp(h->nameW, name) &&
        ((h->domainW == NULL && domain == NULL) ||
         (h->domainW != NULL && domain != NULL && !wcscmp(h->domainW, domain))))
      break;
  }
  if (h == NULL) {
    NEW_COUNTER_W(&_N_(_ittapi_global), h, h_tail, name, domain, type);
  }
  __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
  return (__itt_counter)h;
}

static __itt_counter ITTAPI ITT_VERSIONIZE(ITT_JOIN(_N_(counter_createA),
                                                    _init))(const char *name,
                                                            const char *domain)
#else /* ITT_PLATFORM!=ITT_PLATFORM_WIN */
static __itt_counter ITTAPI ITT_VERSIONIZE(ITT_JOIN(_N_(counter_create),
                                                    _init))(const char *name,
                                                            const char *domain)
#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */
{}

#if ITT_PLATFORM == ITT_PLATFORM_WIN
static __itt_counter ITTAPI ITT_VERSIONIZE(ITT_JOIN(_N_(counter_create_typedW),
                                                    _init))(
    const wchar_t *name, const wchar_t *domain, __itt_metadata_type type) {
  __itt_counter_info_t *h_tail = NULL, *h = NULL;

  if (name == NULL) {
    return NULL;
  }

  ITT_MUTEX_INIT_AND_LOCK(_N_(_ittapi_global));
  if (_N_(_ittapi_global).api_initialized) {
    if (ITTNOTIFY_NAME(counter_create_typedW) &&
        ITTNOTIFY_NAME(counter_create_typedW) !=
            ITT_VERSIONIZE(ITT_JOIN(_N_(counter_create_typedW), _init))) {
      __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
      return ITTNOTIFY_NAME(counter_create_typedW)(name, domain, type);
    } else {
      __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
      return NULL;
    }
  }
  for (h_tail = NULL, h = _N_(_ittapi_global).counter_list; h != NULL;
       h_tail = h, h = h->next) {
    if (h->nameW != NULL && h->type == (int)type && !wcscmp(h->nameW, name) &&
        ((h->domainW == NULL && domain == NULL) ||
         (h->domainW != NULL && domain != NULL && !wcscmp(h->domainW, domain))))
      break;
  }
  if (h == NULL) {
    NEW_COUNTER_W(&_N_(_ittapi_global), h, h_tail, name, domain, type);
  }
  __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
  return (__itt_counter)h;
}

static __itt_counter ITTAPI ITT_VERSIONIZE(ITT_JOIN(
    _N_(counter_create_typedA), _init))(const char *name, const char *domain,
                                        __itt_metadata_type type)
#else /* ITT_PLATFORM!=ITT_PLATFORM_WIN */
static __itt_counter ITTAPI ITT_VERSIONIZE(ITT_JOIN(
    _N_(counter_create_typed), _init))(const char *name, const char *domain,
                                       __itt_metadata_type type)
#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */
{}

#if ITT_PLATFORM == ITT_PLATFORM_WIN
static __itt_histogram *ITTAPI ITT_VERSIONIZE(ITT_JOIN(_N_(histogram_createW),
                                                       _init))(
    const __itt_domain *domain, const wchar_t *name, __itt_metadata_type x_type,
    __itt_metadata_type y_type) {
  __itt_histogram *h_tail = NULL, *h = NULL;

  if (domain == NULL || name == NULL) {
    return NULL;
  }

  ITT_MUTEX_INIT_AND_LOCK(_N_(_ittapi_global));
  if (_N_(_ittapi_global).api_initialized) {
    if (ITTNOTIFY_NAME(histogram_createW) &&
        ITTNOTIFY_NAME(histogram_createW) !=
            ITT_VERSIONIZE(ITT_JOIN(_N_(histogram_createW), _init))) {
      __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
      return ITTNOTIFY_NAME(histogram_createW)(domain, name, x_type, y_type);
    } else {
      __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
      return NULL;
    }
  }
  for (h_tail = NULL, h = _N_(_ittapi_global).histogram_list; h != NULL;
       h_tail = h, h = h->next) {
    if (h->domain == NULL)
      continue;
    else if (h->domain != domain && h->nameW != NULL && !wcscmp(h->nameW, name))
      break;
  }
  if (h == NULL) {
    NEW_HISTOGRAM_W(&_N_(_ittapi_global), h, h_tail, domain, name, x_type,
                    y_type);
  }
  __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
  return (__itt_histogram *)h;
}

static __itt_histogram *ITTAPI ITT_VERSIONIZE(ITT_JOIN(_N_(histogram_createA),
                                                       _init))(
    const __itt_domain *domain, const char *name, __itt_metadata_type x_type,
    __itt_metadata_type y_type)
#else /* ITT_PLATFORM!=ITT_PLATFORM_WIN */
static __itt_histogram *ITTAPI ITT_VERSIONIZE(ITT_JOIN(
    _N_(histogram_create), _init))(const __itt_domain *domain, const char *name,
                                   __itt_metadata_type x_type,
                                   __itt_metadata_type y_type)
#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */
{}

/* -------------------------------------------------------------------------- */

static void ITTAPI ITT_VERSIONIZE(ITT_JOIN(_N_(pause), _init))(void) {}

static void ITTAPI ITT_VERSIONIZE(ITT_JOIN(_N_(resume), _init))(void) {}

#if ITT_PLATFORM == ITT_PLATFORM_WIN
static void ITTAPI ITT_VERSIONIZE(ITT_JOIN(_N_(thread_set_nameW),
                                           _init))(const wchar_t *name) {
  if (!_N_(_ittapi_global).api_initialized &&
      _N_(_ittapi_global).thread_list == NULL) {
    __itt_init_ittlib_name(NULL, __itt_group_all);
  }
  if (ITTNOTIFY_NAME(thread_set_nameW) &&
      ITTNOTIFY_NAME(thread_set_nameW) !=
          ITT_VERSIONIZE(ITT_JOIN(_N_(thread_set_nameW), _init))) {
    ITTNOTIFY_NAME(thread_set_nameW)(name);
  }
}

static int ITTAPI ITT_VERSIONIZE(ITT_JOIN(_N_(thr_name_setW),
                                          _init))(const wchar_t *name,
                                                  int namelen) {
  (void)namelen;
  ITT_VERSIONIZE(ITT_JOIN(_N_(thread_set_nameW), _init))(name);
  return 0;
}

static void ITTAPI ITT_VERSIONIZE(ITT_JOIN(_N_(thread_set_nameA),
                                           _init))(const char *name)
#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */
static void ITTAPI ITT_VERSIONIZE(ITT_JOIN(_N_(thread_set_name),
                                           _init))(const char *name)
#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */
{}

#if ITT_PLATFORM == ITT_PLATFORM_WIN
static int ITTAPI ITT_VERSIONIZE(ITT_JOIN(_N_(thr_name_setA),
                                          _init))(const char *name,
                                                  int namelen) {
  (void)namelen;
  ITT_VERSIONIZE(ITT_JOIN(_N_(thread_set_nameA), _init))(name);
  return 0;
}
#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */
static int ITTAPI ITT_VERSIONIZE(ITT_JOIN(_N_(thr_name_set),
                                          _init))(const char *name,
                                                  int namelen) {}
#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */

static void ITTAPI ITT_VERSIONIZE(ITT_JOIN(_N_(thread_ignore), _init))(void) {}

static void ITTAPI ITT_VERSIONIZE(ITT_JOIN(_N_(thr_ignore), _init))(void) {}

static void ITTAPI ITT_VERSIONIZE(ITT_JOIN(_N_(enable_attach), _init))(void) {}

/* -------------------------------------------------------------------------- */

static const char *__itt_fsplit(const char *s, const char *sep,
                                const char **out, int *len) {}

/* This function return value of env variable that placed into static buffer.
 * !!! The same static buffer is used for subsequent calls. !!!
 * This was done to avoid dynamic allocation for few calls.
 * Actually we need this function only four times.
 */
static const char *__itt_get_env_var(const char *name) {}

static const char *__itt_get_lib_name(void) {}

/* Avoid clashes with std::min */
#define __itt_min

static __itt_group_id __itt_get_groups(void) {}

#undef __itt_min

static int __itt_lib_version(lib_t lib) {}

/* It's not used right now! Comment it out to avoid warnings.
static void __itt_reinit_all_pointers(void)
{
    register int i;
    // Fill all pointers with initial stubs
    for (i = 0; _N_(_ittapi_global).api_list_ptr[i].name != NULL; i++)
        *_N_(_ittapi_global).api_list_ptr[i].func_ptr =
_N_(_ittapi_global).api_list_ptr[i].init_func;
}
*/

static void __itt_nullify_all_pointers(void) {}

#if ITT_PLATFORM == ITT_PLATFORM_WIN
#if _MSC_VER
#pragma warning(push)
#pragma warning(disable : 4054) /* warning C4054: 'type cast' : from function  \
                                   pointer 'XXX' to data pointer 'void *' */
#pragma warning(                                                               \
    disable : 4055) /* warning C4055: 'type cast' : from data pointer 'void *' \
                       to function pointer 'XXX' */
#endif
#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */

ITT_EXTERN_C void _N_(fini_ittlib)(void) {}

/* !!! this function should be called under mutex lock !!! */
static void __itt_free_allocated_resources(void) {}

ITT_EXTERN_C int _N_(init_ittlib)(const char *lib_name,
                                  __itt_group_id init_groups) {}

ITT_EXTERN_C __itt_error_handler_t *
_N_(set_error_handler)(__itt_error_handler_t *handler) {}

#if ITT_PLATFORM == ITT_PLATFORM_WIN
#if _MSC_VER
#pragma warning(pop)
#endif
#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */

/** __itt_mark_pt_region functions marks region of interest
 * region parameter defines different regions.
 * 0 <= region < 8 */

#if defined(ITT_API_IPT_SUPPORT) &&                                            \
    (ITT_PLATFORM == ITT_PLATFORM_WIN ||                                       \
     ITT_PLATFORM == ITT_PLATFORM_POSIX) &&                                    \
    !defined(__ANDROID__)
void __itt_pt_mark(__itt_pt_region region);
void __itt_pt_mark_event(__itt_pt_region region);
#endif

ITT_EXTERN_C void _N_(mark_pt_region_begin)(__itt_pt_region region) {}

ITT_EXTERN_C void _N_(mark_pt_region_end)(__itt_pt_region region) {}