BRE12
tbb_profiling.h
1 /*
2  Copyright 2005-2016 Intel Corporation. All Rights Reserved.
3 
4  This file is part of Threading Building Blocks. Threading Building Blocks is free software;
5  you can redistribute it and/or modify it under the terms of the GNU General Public License
6  version 2 as published by the Free Software Foundation. Threading Building Blocks is
7  distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
8  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
9  See the GNU General Public License for more details. You should have received a copy of
10  the GNU General Public License along with Threading Building Blocks; if not, write to the
11  Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
12 
13  As a special exception, you may use this file as part of a free software library without
14  restriction. Specifically, if other files instantiate templates or use macros or inline
15  functions from this file, or you compile this file and link it with other files to produce
16  an executable, this file does not by itself cause the resulting executable to be covered
17  by the GNU General Public License. This exception does not however invalidate any other
18  reasons why the executable file might be covered by the GNU General Public License.
19 */
20 
21 #ifndef __TBB_profiling_H
22 #define __TBB_profiling_H
23 
24 namespace tbb {
25  namespace internal {
26 
27  //
28  // This is not under __TBB_ITT_STRUCTURE_API because these values are used directly in flow_graph.h.
29  //
30 
31  // include list of index names
32  #define TBB_STRING_RESOURCE(index_name,str) index_name,
33  enum string_index {
34  #include "internal/_tbb_strings.h"
35  NUM_STRINGS
36  };
37  #undef TBB_STRING_RESOURCE
38 
39  enum itt_relation
40  {
41  __itt_relation_is_unknown = 0,
42  __itt_relation_is_dependent_on,
43  __itt_relation_is_sibling_of,
44  __itt_relation_is_parent_of,
45  __itt_relation_is_continuation_of,
46  __itt_relation_is_child_of,
47  __itt_relation_is_continued_by,
48  __itt_relation_is_predecessor_to
49  };
50 
51  }
52 }
53 
54 // Check if the tools support is enabled
55 #if (_WIN32||_WIN64||__linux__) && !__MINGW32__ && TBB_USE_THREADING_TOOLS
56 
57 #if _WIN32||_WIN64
58 #include <stdlib.h> /* mbstowcs_s */
59 #endif
60 #include "tbb_stddef.h"
61 
62 namespace tbb {
63  namespace internal {
64 
65 #if _WIN32||_WIN64
66  void __TBB_EXPORTED_FUNC itt_set_sync_name_v3( void *obj, const wchar_t* name );
67  inline size_t multibyte_to_widechar( wchar_t* wcs, const char* mbs, size_t bufsize) {
68 #if _MSC_VER>=1400
69  size_t len;
70  mbstowcs_s( &len, wcs, bufsize, mbs, _TRUNCATE );
71  return len; // mbstowcs_s counts null terminator
72 #else
73  size_t len = mbstowcs( wcs, mbs, bufsize );
74  if(wcs && len!=size_t(-1) )
75  wcs[len<bufsize-1? len: bufsize-1] = wchar_t('\0');
76  return len+1; // mbstowcs does not count null terminator
77 #endif
78  }
79 #else
80  void __TBB_EXPORTED_FUNC itt_set_sync_name_v3( void *obj, const char* name );
81 #endif
82  } // namespace internal
83 } // namespace tbb
84 
86 
88 #if _WIN32||_WIN64
89  #define __TBB_DEFINE_PROFILING_SET_NAME(sync_object_type) \
90  namespace profiling { \
91  inline void set_name( sync_object_type& obj, const wchar_t* name ) { \
92  tbb::internal::itt_set_sync_name_v3( &obj, name ); \
93  } \
94  inline void set_name( sync_object_type& obj, const char* name ) { \
95  size_t len = tbb::internal::multibyte_to_widechar(NULL, name, 0); \
96  wchar_t *wname = new wchar_t[len]; \
97  tbb::internal::multibyte_to_widechar(wname, name, len); \
98  set_name( obj, wname ); \
99  delete[] wname; \
100  } \
101  }
102 #else /* !WIN */
103  #define __TBB_DEFINE_PROFILING_SET_NAME(sync_object_type) \
104  namespace profiling { \
105  inline void set_name( sync_object_type& obj, const char* name ) { \
106  tbb::internal::itt_set_sync_name_v3( &obj, name ); \
107  } \
108  }
109 #endif /* !WIN */
110 
111 #else /* no tools support */
112 
113 #if _WIN32||_WIN64
114  #define __TBB_DEFINE_PROFILING_SET_NAME(sync_object_type) \
115  namespace profiling { \
116  inline void set_name( sync_object_type&, const wchar_t* ) {} \
117  inline void set_name( sync_object_type&, const char* ) {} \
118  }
119 #else /* !WIN */
120  #define __TBB_DEFINE_PROFILING_SET_NAME(sync_object_type) \
121  namespace profiling { \
122  inline void set_name( sync_object_type&, const char* ) {} \
123  }
124 #endif /* !WIN */
125 
126 #endif /* no tools support */
127 
128 #include "atomic.h"
129 // Need these to work regardless of tools support
130 namespace tbb {
131  namespace internal {
132 
133  enum notify_type {prepare=0, cancel, acquired, releasing};
134 
135  const uintptr_t NUM_NOTIFY_TYPES = 4; // set to # elements in enum above
136 
137  void __TBB_EXPORTED_FUNC call_itt_notify_v5(int t, void *ptr);
138  void __TBB_EXPORTED_FUNC itt_store_pointer_with_release_v3(void *dst, void *src);
139  void* __TBB_EXPORTED_FUNC itt_load_pointer_with_acquire_v3(const void *src);
140  void* __TBB_EXPORTED_FUNC itt_load_pointer_v3( const void* src );
141 #if __TBB_ITT_STRUCTURE_API
142  enum itt_domain_enum { ITT_DOMAIN_FLOW=0 };
143 
144  void __TBB_EXPORTED_FUNC itt_make_task_group_v7( itt_domain_enum domain, void *group, unsigned long long group_extra,
145  void *parent, unsigned long long parent_extra, string_index name_index );
146  void __TBB_EXPORTED_FUNC itt_metadata_str_add_v7( itt_domain_enum domain, void *addr, unsigned long long addr_extra,
147  string_index key, const char *value );
148  void __TBB_EXPORTED_FUNC itt_relation_add_v7( itt_domain_enum domain, void *addr0, unsigned long long addr0_extra,
149  itt_relation relation, void *addr1, unsigned long long addr1_extra );
150  void __TBB_EXPORTED_FUNC itt_task_begin_v7( itt_domain_enum domain, void *task, unsigned long long task_extra,
151  void *parent, unsigned long long parent_extra, string_index name_index );
152  void __TBB_EXPORTED_FUNC itt_task_end_v7( itt_domain_enum domain );
153 
154  void __TBB_EXPORTED_FUNC itt_region_begin_v9( itt_domain_enum domain, void *region, unsigned long long region_extra,
155  void *parent, unsigned long long parent_extra, string_index name_index );
156  void __TBB_EXPORTED_FUNC itt_region_end_v9( itt_domain_enum domain, void *region, unsigned long long region_extra );
157 #endif // __TBB_ITT_STRUCTURE_API
158 
159  // two template arguments are to workaround /Wp64 warning with tbb::atomic specialized for unsigned type
160  template <typename T, typename U>
161  inline void itt_store_word_with_release(tbb::atomic<T>& dst, U src) {
162 #if TBB_USE_THREADING_TOOLS
163  // This assertion should be replaced with static_assert
164  __TBB_ASSERT(sizeof(T) == sizeof(void *), "Type must be word-sized.");
165  itt_store_pointer_with_release_v3(&dst, (void *)uintptr_t(src));
166 #else
167  dst = src;
168 #endif // TBB_USE_THREADING_TOOLS
169  }
170 
171  template <typename T>
172  inline T itt_load_word_with_acquire(const tbb::atomic<T>& src) {
173 #if TBB_USE_THREADING_TOOLS
174  // This assertion should be replaced with static_assert
175  __TBB_ASSERT(sizeof(T) == sizeof(void *), "Type must be word-sized.");
176 #if defined(_MSC_VER) && !defined(__INTEL_COMPILER)
177  // Workaround for overzealous compiler warnings
178  #pragma warning (push)
179  #pragma warning (disable: 4311)
180 #endif
181  T result = (T)itt_load_pointer_with_acquire_v3(&src);
182 #if defined(_MSC_VER) && !defined(__INTEL_COMPILER)
183  #pragma warning (pop)
184 #endif
185  return result;
186 #else
187  return src;
188 #endif // TBB_USE_THREADING_TOOLS
189  }
190 
191  template <typename T>
192  inline void itt_store_word_with_release(T& dst, T src) {
193 #if TBB_USE_THREADING_TOOLS
194  // This assertion should be replaced with static_assert
195  __TBB_ASSERT(sizeof(T) == sizeof(void *), "Type must be word-sized.");
196  itt_store_pointer_with_release_v3(&dst, (void *)src);
197 #else
198  __TBB_store_with_release(dst, src);
199 #endif // TBB_USE_THREADING_TOOLS
200  }
201 
202  template <typename T>
203  inline T itt_load_word_with_acquire(const T& src) {
204 #if TBB_USE_THREADING_TOOLS
205  // This assertion should be replaced with static_assert
206  __TBB_ASSERT(sizeof(T) == sizeof(void *), "Type must be word-sized");
207  return (T)itt_load_pointer_with_acquire_v3(&src);
208 #else
209  return __TBB_load_with_acquire(src);
210 #endif // TBB_USE_THREADING_TOOLS
211  }
212 
213  template <typename T>
214  inline void itt_hide_store_word(T& dst, T src) {
215 #if TBB_USE_THREADING_TOOLS
216  //TODO: This assertion should be replaced with static_assert
217  __TBB_ASSERT(sizeof(T) == sizeof(void *), "Type must be word-sized");
218  itt_store_pointer_with_release_v3(&dst, (void *)src);
219 #else
220  dst = src;
221 #endif
222  }
223 
224  //TODO: rename to itt_hide_load_word_relaxed
225  template <typename T>
226  inline T itt_hide_load_word(const T& src) {
227 #if TBB_USE_THREADING_TOOLS
228  //TODO: This assertion should be replaced with static_assert
229  __TBB_ASSERT(sizeof(T) == sizeof(void *), "Type must be word-sized.");
230  return (T)itt_load_pointer_v3(&src);
231 #else
232  return src;
233 #endif
234  }
235 
236 #if TBB_USE_THREADING_TOOLS
237  inline void call_itt_notify(notify_type t, void *ptr) {
238  call_itt_notify_v5((int)t, ptr);
239  }
240 
241 #else
242  inline void call_itt_notify(notify_type /*t*/, void * /*ptr*/) {}
243 
244 #endif // TBB_USE_THREADING_TOOLS
245 
246 #if __TBB_ITT_STRUCTURE_API
247  inline void itt_make_task_group( itt_domain_enum domain, void *group, unsigned long long group_extra,
248  void *parent, unsigned long long parent_extra, string_index name_index ) {
249  itt_make_task_group_v7( domain, group, group_extra, parent, parent_extra, name_index );
250  }
251 
252  inline void itt_metadata_str_add( itt_domain_enum domain, void *addr, unsigned long long addr_extra,
253  string_index key, const char *value ) {
254  itt_metadata_str_add_v7( domain, addr, addr_extra, key, value );
255  }
256 
257  inline void itt_relation_add( itt_domain_enum domain, void *addr0, unsigned long long addr0_extra,
258  itt_relation relation, void *addr1, unsigned long long addr1_extra ) {
259  itt_relation_add_v7( domain, addr0, addr0_extra, relation, addr1, addr1_extra );
260  }
261 
262  inline void itt_task_begin( itt_domain_enum domain, void *task, unsigned long long task_extra,
263  void *parent, unsigned long long parent_extra, string_index name_index ) {
264  itt_task_begin_v7( domain, task, task_extra, parent, parent_extra, name_index );
265  }
266 
267  inline void itt_task_end( itt_domain_enum domain ) {
268  itt_task_end_v7( domain );
269  }
270 
271  inline void itt_region_begin( itt_domain_enum domain, void *region, unsigned long long region_extra,
272  void *parent, unsigned long long parent_extra, string_index name_index ) {
273  itt_region_begin_v9( domain, region, region_extra, parent, parent_extra, name_index );
274  }
275 
276  inline void itt_region_end( itt_domain_enum domain, void *region, unsigned long long region_extra ) {
277  itt_region_end_v9( domain, region, region_extra );
278  }
279 #endif // __TBB_ITT_STRUCTURE_API
280 
281  } // namespace internal
282 } // namespace tbb
283 
284 #endif /* __TBB_profiling_H */
Primary template for atomic.
Definition: atomic.h:405
Definition: _flow_graph_async_msg_impl.h:32
The namespace tbb contains all components of the library.
Definition: parallel_for.h:44