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