1/*===-- ittnotify_config.h - JIT Profiling API internal config-----*- C -*-===*
2 *
3 * Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 * See https://llvm.org/LICENSE.txt for license information.
5 * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 *
7 *===----------------------------------------------------------------------===*
8 *
9 * This file provides Intel(R) Performance Analyzer JIT (Just-In-Time)
10 * Profiling API internal config.
11 *
12 * NOTE: This file comes in a style different from the rest of LLVM
13 * source base since  this is a piece of code shared from Intel(R)
14 * products.  Please do not reformat / re-style this code to make
15 * subsequent merges and contributions from the original source base eaiser.
16 *
17 *===----------------------------------------------------------------------===*/
18#ifndef _ITTNOTIFY_CONFIG_H_
19#define _ITTNOTIFY_CONFIG_H_
20
21/** @cond exclude_from_documentation */
22#ifndef ITT_OS_WIN
23#  define ITT_OS_WIN   1
24#endif /* ITT_OS_WIN */
25
26#ifndef ITT_OS_LINUX
27#  define ITT_OS_LINUX 2
28#endif /* ITT_OS_LINUX */
29
30#ifndef ITT_OS_MAC
31#  define ITT_OS_MAC   3
32#endif /* ITT_OS_MAC */
33
34#ifndef ITT_OS
35#  if defined WIN32 || defined _WIN32
36#    define ITT_OS ITT_OS_WIN
37#  elif defined( __APPLE__ ) && defined( __MACH__ )
38#    define ITT_OS ITT_OS_MAC
39#  else
40#    define ITT_OS ITT_OS_LINUX
41#  endif
42#endif /* ITT_OS */
43
44#ifndef ITT_PLATFORM_WIN
45#  define ITT_PLATFORM_WIN 1
46#endif /* ITT_PLATFORM_WIN */
47
48#ifndef ITT_PLATFORM_POSIX
49#  define ITT_PLATFORM_POSIX 2
50#endif /* ITT_PLATFORM_POSIX */
51
52#ifndef ITT_PLATFORM
53#  if ITT_OS==ITT_OS_WIN
54#    define ITT_PLATFORM ITT_PLATFORM_WIN
55#  else
56#    define ITT_PLATFORM ITT_PLATFORM_POSIX
57#  endif /* _WIN32 */
58#endif /* ITT_PLATFORM */
59
60#if defined(_UNICODE) && !defined(UNICODE)
61#define UNICODE
62#endif
63
64#include <stddef.h>
65#if ITT_PLATFORM==ITT_PLATFORM_WIN
66#include <tchar.h>
67#else  /* ITT_PLATFORM==ITT_PLATFORM_WIN */
68#include <stdint.h>
69#if defined(UNICODE) || defined(_UNICODE)
70#include <wchar.h>
71#endif /* UNICODE || _UNICODE */
72#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */
73
74#ifndef CDECL
75#  if ITT_PLATFORM==ITT_PLATFORM_WIN
76#    define CDECL __cdecl
77#  else /* ITT_PLATFORM==ITT_PLATFORM_WIN */
78#    if defined _M_X64 || defined _M_AMD64 || defined __x86_64__
79#      define CDECL /* not actual on x86_64 platform */
80#    else  /* _M_X64 || _M_AMD64 || __x86_64__ */
81#      define CDECL __attribute__ ((cdecl))
82#    endif /* _M_X64 || _M_AMD64 || __x86_64__ */
83#  endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */
84#endif /* CDECL */
85
86#ifndef STDCALL
87#  if ITT_PLATFORM==ITT_PLATFORM_WIN
88#    define STDCALL __stdcall
89#  else /* ITT_PLATFORM==ITT_PLATFORM_WIN */
90#    if defined _M_X64 || defined _M_AMD64 || defined __x86_64__
91#      define STDCALL /* not supported on x86_64 platform */
92#    else  /* _M_X64 || _M_AMD64 || __x86_64__ */
93#      define STDCALL __attribute__ ((stdcall))
94#    endif /* _M_X64 || _M_AMD64 || __x86_64__ */
95#  endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */
96#endif /* STDCALL */
97
98#define ITTAPI    CDECL
99#define LIBITTAPI CDECL
100
101/* TODO: Temporary for compatibility! */
102#define ITTAPI_CALL    CDECL
103#define LIBITTAPI_CALL CDECL
104
105#if ITT_PLATFORM==ITT_PLATFORM_WIN
106/* use __forceinline (VC++ specific) */
107#define ITT_INLINE           __forceinline
108#define ITT_INLINE_ATTRIBUTE /* nothing */
109#else  /* ITT_PLATFORM==ITT_PLATFORM_WIN */
110/*
111 * Generally, functions are not inlined unless optimization is specified.
112 * For functions declared inline, this attribute inlines the function even
113 * if no optimization level was specified.
114 */
115#ifdef __STRICT_ANSI__
116#define ITT_INLINE           static
117#else  /* __STRICT_ANSI__ */
118#define ITT_INLINE           static inline
119#endif /* __STRICT_ANSI__ */
120#define ITT_INLINE_ATTRIBUTE __attribute__ ((always_inline))
121#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */
122/** @endcond */
123
124#ifndef ITT_ARCH_IA32
125#  define ITT_ARCH_IA32  1
126#endif /* ITT_ARCH_IA32 */
127
128#ifndef ITT_ARCH_IA32E
129#  define ITT_ARCH_IA32E 2
130#endif /* ITT_ARCH_IA32E */
131
132#ifndef ITT_ARCH_IA64
133#  define ITT_ARCH_IA64  3
134#endif /* ITT_ARCH_IA64 */
135
136#ifndef ITT_ARCH
137#  if defined _M_X64 || defined _M_AMD64 || defined __x86_64__
138#    define ITT_ARCH ITT_ARCH_IA32E
139#  elif defined _M_IA64 || defined __ia64
140#    define ITT_ARCH ITT_ARCH_IA64
141#  else
142#    define ITT_ARCH ITT_ARCH_IA32
143#  endif
144#endif
145
146#ifdef __cplusplus
147#  define ITT_EXTERN_C extern "C"
148#else
149#  define ITT_EXTERN_C /* nothing */
150#endif /* __cplusplus */
151
152#define ITT_TO_STR_AUX(x) #x
153#define ITT_TO_STR(x)     ITT_TO_STR_AUX(x)
154
155#define __ITT_BUILD_ASSERT(expr, suffix) do { \
156    static char __itt_build_check_##suffix[(expr) ? 1 : -1]; \
157    __itt_build_check_##suffix[0] = 0; \
158} while(0)
159#define _ITT_BUILD_ASSERT(expr, suffix)  __ITT_BUILD_ASSERT((expr), suffix)
160#define ITT_BUILD_ASSERT(expr)           _ITT_BUILD_ASSERT((expr), __LINE__)
161
162#define ITT_MAGIC { 0xED, 0xAB, 0xAB, 0xEC, 0x0D, 0xEE, 0xDA, 0x30 }
163
164/* Replace with snapshot date YYYYMMDD for promotion build. */
165#define API_VERSION_BUILD    20111111
166
167#ifndef API_VERSION_NUM
168#define API_VERSION_NUM 0.0.0
169#endif /* API_VERSION_NUM */
170
171#define API_VERSION "ITT-API-Version " ITT_TO_STR(API_VERSION_NUM) \
172                                " (" ITT_TO_STR(API_VERSION_BUILD) ")"
173
174/* OS communication functions */
175#if ITT_PLATFORM==ITT_PLATFORM_WIN
176#include <windows.h>
177typedef HMODULE           lib_t;
178typedef DWORD             TIDT;
179typedef CRITICAL_SECTION  mutex_t;
180#define MUTEX_INITIALIZER { 0 }
181#define strong_alias(name, aliasname) /* empty for Windows */
182#else  /* ITT_PLATFORM==ITT_PLATFORM_WIN */
183#include <dlfcn.h>
184#if defined(UNICODE) || defined(_UNICODE)
185#include <wchar.h>
186#endif /* UNICODE */
187#ifndef _GNU_SOURCE
188#define _GNU_SOURCE 1 /* need for PTHREAD_MUTEX_RECURSIVE */
189#endif /* _GNU_SOURCE */
190#include <pthread.h>
191typedef void*             lib_t;
192typedef pthread_t         TIDT;
193typedef pthread_mutex_t   mutex_t;
194#define MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
195#define _strong_alias(name, aliasname) \
196            extern __typeof (name) aliasname __attribute__ ((alias (#name)));
197#define strong_alias(name, aliasname) _strong_alias(name, aliasname)
198#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */
199
200#if ITT_PLATFORM==ITT_PLATFORM_WIN
201#define __itt_get_proc(lib, name) GetProcAddress(lib, name)
202#define __itt_mutex_init(mutex)   InitializeCriticalSection(mutex)
203#define __itt_mutex_lock(mutex)   EnterCriticalSection(mutex)
204#define __itt_mutex_unlock(mutex) LeaveCriticalSection(mutex)
205#define __itt_load_lib(name)      LoadLibraryA(name)
206#define __itt_unload_lib(handle)  FreeLibrary(handle)
207#define __itt_system_error()      (int)GetLastError()
208#define __itt_fstrcmp(s1, s2)     lstrcmpA(s1, s2)
209#define __itt_fstrlen(s)          lstrlenA(s)
210#define __itt_fstrcpyn(s1, s2, l) lstrcpynA(s1, s2, l)
211#define __itt_fstrdup(s)          _strdup(s)
212#define __itt_thread_id()         GetCurrentThreadId()
213#define __itt_thread_yield()      SwitchToThread()
214#ifndef ITT_SIMPLE_INIT
215ITT_INLINE long
216__itt_interlocked_increment(volatile long* ptr) ITT_INLINE_ATTRIBUTE;
217ITT_INLINE long __itt_interlocked_increment(volatile long* ptr)
218{
219    return InterlockedIncrement(ptr);
220}
221#endif /* ITT_SIMPLE_INIT */
222#else /* ITT_PLATFORM!=ITT_PLATFORM_WIN */
223#define __itt_get_proc(lib, name) dlsym(lib, name)
224#define __itt_mutex_init(mutex)   {\
225    pthread_mutexattr_t mutex_attr;                                         \
226    int error_code = pthread_mutexattr_init(&mutex_attr);                   \
227    if (error_code)                                                         \
228        __itt_report_error(__itt_error_system, "pthread_mutexattr_init",    \
229                           error_code);                                     \
230    error_code = pthread_mutexattr_settype(&mutex_attr,                     \
231                                           PTHREAD_MUTEX_RECURSIVE);        \
232    if (error_code)                                                         \
233        __itt_report_error(__itt_error_system, "pthread_mutexattr_settype", \
234                           error_code);                                     \
235    error_code = pthread_mutex_init(mutex, &mutex_attr);                    \
236    if (error_code)                                                         \
237        __itt_report_error(__itt_error_system, "pthread_mutex_init",        \
238                           error_code);                                     \
239    error_code = pthread_mutexattr_destroy(&mutex_attr);                    \
240    if (error_code)                                                         \
241        __itt_report_error(__itt_error_system, "pthread_mutexattr_destroy", \
242                           error_code);                                     \
243}
244#define __itt_mutex_lock(mutex)   pthread_mutex_lock(mutex)
245#define __itt_mutex_unlock(mutex) pthread_mutex_unlock(mutex)
246#define __itt_load_lib(name)      dlopen(name, RTLD_LAZY)
247#define __itt_unload_lib(handle)  dlclose(handle)
248#define __itt_system_error()      errno
249#define __itt_fstrcmp(s1, s2)     strcmp(s1, s2)
250#define __itt_fstrlen(s)          strlen(s)
251#define __itt_fstrcpyn(s1, s2, l) strncpy(s1, s2, l)
252#define __itt_fstrdup(s)          strdup(s)
253#define __itt_thread_id()         pthread_self()
254#define __itt_thread_yield()      sched_yield()
255#if ITT_ARCH==ITT_ARCH_IA64
256#ifdef __INTEL_COMPILER
257#define __TBB_machine_fetchadd4(addr, val) __fetchadd4_acq((void *)addr, val)
258#else  /* __INTEL_COMPILER */
259/* TODO: Add Support for not Intel compilers for IA64 */
260#endif /* __INTEL_COMPILER */
261#else /* ITT_ARCH!=ITT_ARCH_IA64 */
262ITT_INLINE long
263__TBB_machine_fetchadd4(volatile void* ptr, long addend) ITT_INLINE_ATTRIBUTE;
264ITT_INLINE long __TBB_machine_fetchadd4(volatile void* ptr, long addend)
265{
266    long result;
267    __asm__ __volatile__("lock\nxadd %0,%1"
268                          : "=r"(result),"=m"(*(long*)ptr)
269                          : "0"(addend), "m"(*(long*)ptr)
270                          : "memory");
271    return result;
272}
273#endif /* ITT_ARCH==ITT_ARCH_IA64 */
274#ifndef ITT_SIMPLE_INIT
275ITT_INLINE long
276__itt_interlocked_increment(volatile long* ptr) ITT_INLINE_ATTRIBUTE;
277ITT_INLINE long __itt_interlocked_increment(volatile long* ptr)
278{
279    return __TBB_machine_fetchadd4(ptr, 1) + 1L;
280}
281#endif /* ITT_SIMPLE_INIT */
282#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */
283
284typedef enum {
285    __itt_collection_normal = 0,
286    __itt_collection_paused = 1
287} __itt_collection_state;
288
289typedef enum {
290    __itt_thread_normal  = 0,
291    __itt_thread_ignored = 1
292} __itt_thread_state;
293
294#pragma pack(push, 8)
295
296typedef struct ___itt_thread_info
297{
298    const char* nameA; /*!< Copy of original name in ASCII. */
299#if defined(UNICODE) || defined(_UNICODE)
300    const wchar_t* nameW; /*!< Copy of original name in UNICODE. */
301#else  /* UNICODE || _UNICODE */
302    void* nameW;
303#endif /* UNICODE || _UNICODE */
304    TIDT               tid;
305    __itt_thread_state state;   /*!< Thread state (paused or normal) */
306    int                extra1;  /*!< Reserved to the runtime */
307    void*              extra2;  /*!< Reserved to the runtime */
308    struct ___itt_thread_info* next;
309} __itt_thread_info;
310
311#include "ittnotify_types.h" /* For __itt_group_id definition */
312
313typedef struct ___itt_api_info_20101001
314{
315    const char*    name;
316    void**         func_ptr;
317    void*          init_func;
318    __itt_group_id group;
319}  __itt_api_info_20101001;
320
321typedef struct ___itt_api_info
322{
323    const char*    name;
324    void**         func_ptr;
325    void*          init_func;
326    void*          null_func;
327    __itt_group_id group;
328}  __itt_api_info;
329
330struct ___itt_domain;
331struct ___itt_string_handle;
332
333typedef struct ___itt_global
334{
335    unsigned char          magic[8];
336    unsigned long          version_major;
337    unsigned long          version_minor;
338    unsigned long          version_build;
339    volatile long          api_initialized;
340    volatile long          mutex_initialized;
341    volatile long          atomic_counter;
342    mutex_t                mutex;
343    lib_t                  lib;
344    void*                  error_handler;
345    const char**           dll_path_ptr;
346    __itt_api_info*        api_list_ptr;
347    struct ___itt_global*  next;
348    /* Joinable structures below */
349    __itt_thread_info*     thread_list;
350    struct ___itt_domain*  domain_list;
351    struct ___itt_string_handle* string_list;
352    __itt_collection_state state;
353} __itt_global;
354
355#pragma pack(pop)
356
357#define NEW_THREAD_INFO_W(gptr,h,h_tail,t,s,n) { \
358    h = (__itt_thread_info*)malloc(sizeof(__itt_thread_info)); \
359    if (h != NULL) { \
360        h->tid    = t; \
361        h->nameA  = NULL; \
362        h->nameW  = n ? _wcsdup(n) : NULL; \
363        h->state  = s; \
364        h->extra1 = 0;    /* reserved */ \
365        h->extra2 = NULL; /* reserved */ \
366        h->next   = NULL; \
367        if (h_tail == NULL) \
368            (gptr)->thread_list = h; \
369        else \
370            h_tail->next = h; \
371    } \
372}
373
374#define NEW_THREAD_INFO_A(gptr,h,h_tail,t,s,n) { \
375    h = (__itt_thread_info*)malloc(sizeof(__itt_thread_info)); \
376    if (h != NULL) { \
377        h->tid    = t; \
378        h->nameA  = n ? __itt_fstrdup(n) : NULL; \
379        h->nameW  = NULL; \
380        h->state  = s; \
381        h->extra1 = 0;    /* reserved */ \
382        h->extra2 = NULL; /* reserved */ \
383        h->next   = NULL; \
384        if (h_tail == NULL) \
385            (gptr)->thread_list = h; \
386        else \
387            h_tail->next = h; \
388    } \
389}
390
391#define NEW_DOMAIN_W(gptr,h,h_tail,name) { \
392    h = (__itt_domain*)malloc(sizeof(__itt_domain)); \
393    if (h != NULL) { \
394        h->flags  = 0;    /* domain is disabled by default */ \
395        h->nameA  = NULL; \
396        h->nameW  = name ? _wcsdup(name) : NULL; \
397        h->extra1 = 0;    /* reserved */ \
398        h->extra2 = NULL; /* reserved */ \
399        h->next   = NULL; \
400        if (h_tail == NULL) \
401            (gptr)->domain_list = h; \
402        else \
403            h_tail->next = h; \
404    } \
405}
406
407#define NEW_DOMAIN_A(gptr,h,h_tail,name) { \
408    h = (__itt_domain*)malloc(sizeof(__itt_domain)); \
409    if (h != NULL) { \
410        h->flags  = 0;    /* domain is disabled by default */ \
411        h->nameA  = name ? __itt_fstrdup(name) : NULL; \
412        h->nameW  = NULL; \
413        h->extra1 = 0;    /* reserved */ \
414        h->extra2 = NULL; /* reserved */ \
415        h->next   = NULL; \
416        if (h_tail == NULL) \
417            (gptr)->domain_list = h; \
418        else \
419            h_tail->next = h; \
420    } \
421}
422
423#define NEW_STRING_HANDLE_W(gptr,h,h_tail,name) { \
424    h = (__itt_string_handle*)malloc(sizeof(__itt_string_handle)); \
425    if (h != NULL) { \
426        h->strA   = NULL; \
427        h->strW   = name ? _wcsdup(name) : NULL; \
428        h->extra1 = 0;    /* reserved */ \
429        h->extra2 = NULL; /* reserved */ \
430        h->next   = NULL; \
431        if (h_tail == NULL) \
432            (gptr)->string_list = h; \
433        else \
434            h_tail->next = h; \
435    } \
436}
437
438#define NEW_STRING_HANDLE_A(gptr,h,h_tail,name) { \
439    h = (__itt_string_handle*)malloc(sizeof(__itt_string_handle)); \
440    if (h != NULL) { \
441        h->strA   = name ? __itt_fstrdup(name) : NULL; \
442        h->strW   = NULL; \
443        h->extra1 = 0;    /* reserved */ \
444        h->extra2 = NULL; /* reserved */ \
445        h->next   = NULL; \
446        if (h_tail == NULL) \
447            (gptr)->string_list = h; \
448        else \
449            h_tail->next = h; \
450    } \
451}
452
453#endif /* _ITTNOTIFY_CONFIG_H_ */
454