1/*
2 Copyright (c) 2005-2019 Intel Corporation
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15*/
16
17#ifndef __TBB_profiling_H
18#define __TBB_profiling_H
19
20namespace tbb {
21 namespace internal {
22
23 // include list of index names
24 #define TBB_STRING_RESOURCE(index_name,str) index_name,
25 enum string_index {
26 #include "internal/_tbb_strings.h"
27 NUM_STRINGS
28 };
29 #undef TBB_STRING_RESOURCE
30
31 enum itt_relation
32 {
33 __itt_relation_is_unknown = 0,
34 __itt_relation_is_dependent_on, /**< "A is dependent on B" means that A cannot start until B completes */
35 __itt_relation_is_sibling_of, /**< "A is sibling of B" means that A and B were created as a group */
36 __itt_relation_is_parent_of, /**< "A is parent of B" means that A created B */
37 __itt_relation_is_continuation_of, /**< "A is continuation of B" means that A assumes the dependencies of B */
38 __itt_relation_is_child_of, /**< "A is child of B" means that A was created by B (inverse of is_parent_of) */
39 __itt_relation_is_continued_by, /**< "A is continued by B" means that B assumes the dependencies of A (inverse of is_continuation_of) */
40 __itt_relation_is_predecessor_to /**< "A is predecessor to B" means that B cannot start until A completes (inverse of is_dependent_on) */
41 };
42
43 }
44}
45
46// Check if the tools support is enabled
47#if (_WIN32||_WIN64||__linux__) && !__MINGW32__ && TBB_USE_THREADING_TOOLS
48
49#if _WIN32||_WIN64
50#include <stdlib.h> /* mbstowcs_s */
51#endif
52#include "tbb_stddef.h"
53
54namespace tbb {
55 namespace internal {
56
57#if _WIN32||_WIN64
58 void __TBB_EXPORTED_FUNC itt_set_sync_name_v3( void *obj, const wchar_t* name );
59 inline size_t multibyte_to_widechar( wchar_t* wcs, const char* mbs, size_t bufsize) {
60#if _MSC_VER>=1400
61 size_t len;
62 mbstowcs_s( &len, wcs, bufsize, mbs, _TRUNCATE );
63 return len; // mbstowcs_s counts null terminator
64#else
65 size_t len = mbstowcs( wcs, mbs, bufsize );
66 if(wcs && len!=size_t(-1) )
67 wcs[len<bufsize-1? len: bufsize-1] = wchar_t('\0');
68 return len+1; // mbstowcs does not count null terminator
69#endif
70 }
71#else
72 void __TBB_EXPORTED_FUNC itt_set_sync_name_v3( void *obj, const char* name );
73#endif
74 } // namespace internal
75} // namespace tbb
76
77//! Macro __TBB_DEFINE_PROFILING_SET_NAME(T) defines "set_name" methods for sync objects of type T
78/** Should be used in the "tbb" namespace only.
79 Don't place semicolon after it to avoid compiler warnings. **/
80#if _WIN32||_WIN64
81 #define __TBB_DEFINE_PROFILING_SET_NAME(sync_object_type) \
82 namespace profiling { \
83 inline void set_name( sync_object_type& obj, const wchar_t* name ) { \
84 tbb::internal::itt_set_sync_name_v3( &obj, name ); \
85 } \
86 inline void set_name( sync_object_type& obj, const char* name ) { \
87 size_t len = tbb::internal::multibyte_to_widechar(NULL, name, 0); \
88 wchar_t *wname = new wchar_t[len]; \
89 tbb::internal::multibyte_to_widechar(wname, name, len); \
90 set_name( obj, wname ); \
91 delete[] wname; \
92 } \
93 }
94#else /* !WIN */
95 #define __TBB_DEFINE_PROFILING_SET_NAME(sync_object_type) \
96 namespace profiling { \
97 inline void set_name( sync_object_type& obj, const char* name ) { \
98 tbb::internal::itt_set_sync_name_v3( &obj, name ); \
99 } \
100 }
101#endif /* !WIN */
102
103#else /* no tools support */
104
105#if _WIN32||_WIN64
106 #define __TBB_DEFINE_PROFILING_SET_NAME(sync_object_type) \
107 namespace profiling { \
108 inline void set_name( sync_object_type&, const wchar_t* ) {} \
109 inline void set_name( sync_object_type&, const char* ) {} \
110 }
111#else /* !WIN */
112 #define __TBB_DEFINE_PROFILING_SET_NAME(sync_object_type) \
113 namespace profiling { \
114 inline void set_name( sync_object_type&, const char* ) {} \
115 }
116#endif /* !WIN */
117
118#endif /* no tools support */
119
120#include "atomic.h"
121// Need these to work regardless of tools support
122namespace tbb {
123 namespace internal {
124
125 enum notify_type {prepare=0, cancel, acquired, releasing};
126
127 const uintptr_t NUM_NOTIFY_TYPES = 4; // set to # elements in enum above
128
129 void __TBB_EXPORTED_FUNC call_itt_notify_v5(int t, void *ptr);
130 void __TBB_EXPORTED_FUNC itt_store_pointer_with_release_v3(void *dst, void *src);
131 void* __TBB_EXPORTED_FUNC itt_load_pointer_with_acquire_v3(const void *src);
132 void* __TBB_EXPORTED_FUNC itt_load_pointer_v3( const void* src );
133 enum itt_domain_enum { ITT_DOMAIN_FLOW=0, ITT_DOMAIN_MAIN=1, ITT_DOMAIN_ALGO=2, ITT_NUM_DOMAINS };
134
135 void __TBB_EXPORTED_FUNC itt_make_task_group_v7( itt_domain_enum domain, void *group, unsigned long long group_extra,
136 void *parent, unsigned long long parent_extra, string_index name_index );
137 void __TBB_EXPORTED_FUNC itt_metadata_str_add_v7( itt_domain_enum domain, void *addr, unsigned long long addr_extra,
138 string_index key, const char *value );
139 void __TBB_EXPORTED_FUNC itt_relation_add_v7( itt_domain_enum domain, void *addr0, unsigned long long addr0_extra,
140 itt_relation relation, void *addr1, unsigned long long addr1_extra );
141 void __TBB_EXPORTED_FUNC itt_task_begin_v7( itt_domain_enum domain, void *task, unsigned long long task_extra,
142 void *parent, unsigned long long parent_extra, string_index name_index );
143 void __TBB_EXPORTED_FUNC itt_task_end_v7( itt_domain_enum domain );
144
145 void __TBB_EXPORTED_FUNC itt_region_begin_v9( itt_domain_enum domain, void *region, unsigned long long region_extra,
146 void *parent, unsigned long long parent_extra, string_index name_index );
147 void __TBB_EXPORTED_FUNC itt_region_end_v9( itt_domain_enum domain, void *region, unsigned long long region_extra );
148
149 // two template arguments are to workaround /Wp64 warning with tbb::atomic specialized for unsigned type
150 template <typename T, typename U>
151 inline void itt_store_word_with_release(tbb::atomic<T>& dst, U src) {
152#if TBB_USE_THREADING_TOOLS
153 // This assertion should be replaced with static_assert
154 __TBB_ASSERT(sizeof(T) == sizeof(void *), "Type must be word-sized.");
155 itt_store_pointer_with_release_v3(&dst, (void *)uintptr_t(src));
156#else
157 dst = src;
158#endif // TBB_USE_THREADING_TOOLS
159 }
160
161 template <typename T>
162 inline T itt_load_word_with_acquire(const tbb::atomic<T>& src) {
163#if TBB_USE_THREADING_TOOLS
164 // This assertion should be replaced with static_assert
165 __TBB_ASSERT(sizeof(T) == sizeof(void *), "Type must be word-sized.");
166#if defined(_MSC_VER) && !defined(__INTEL_COMPILER)
167 // Workaround for overzealous compiler warnings
168 #pragma warning (push)
169 #pragma warning (disable: 4311)
170#endif
171 T result = (T)itt_load_pointer_with_acquire_v3(&src);
172#if defined(_MSC_VER) && !defined(__INTEL_COMPILER)
173 #pragma warning (pop)
174#endif
175 return result;
176#else
177 return src;
178#endif // TBB_USE_THREADING_TOOLS
179 }
180
181 template <typename T>
182 inline void itt_store_word_with_release(T& dst, T src) {
183#if TBB_USE_THREADING_TOOLS
184 // This assertion should be replaced with static_assert
185 __TBB_ASSERT(sizeof(T) == sizeof(void *), "Type must be word-sized.");
186 itt_store_pointer_with_release_v3(&dst, (void *)src);
187#else
188 __TBB_store_with_release(dst, src);
189#endif // TBB_USE_THREADING_TOOLS
190 }
191
192 template <typename T>
193 inline T itt_load_word_with_acquire(const T& src) {
194#if TBB_USE_THREADING_TOOLS
195 // This assertion should be replaced with static_assert
196 __TBB_ASSERT(sizeof(T) == sizeof(void *), "Type must be word-sized");
197 return (T)itt_load_pointer_with_acquire_v3(&src);
198#else
199 return __TBB_load_with_acquire(src);
200#endif // TBB_USE_THREADING_TOOLS
201 }
202
203 template <typename T>
204 inline void itt_hide_store_word(T& dst, T src) {
205#if TBB_USE_THREADING_TOOLS
206 //TODO: This assertion should be replaced with static_assert
207 __TBB_ASSERT(sizeof(T) == sizeof(void *), "Type must be word-sized");
208 itt_store_pointer_with_release_v3(&dst, (void *)src);
209#else
210 dst = src;
211#endif
212 }
213
214 //TODO: rename to itt_hide_load_word_relaxed
215 template <typename T>
216 inline T itt_hide_load_word(const T& src) {
217#if TBB_USE_THREADING_TOOLS
218 //TODO: This assertion should be replaced with static_assert
219 __TBB_ASSERT(sizeof(T) == sizeof(void *), "Type must be word-sized.");
220 return (T)itt_load_pointer_v3(&src);
221#else
222 return src;
223#endif
224 }
225
226#if TBB_USE_THREADING_TOOLS
227 inline void call_itt_notify(notify_type t, void *ptr) {
228 call_itt_notify_v5((int)t, ptr);
229 }
230
231 inline void itt_make_task_group( itt_domain_enum domain, void *group, unsigned long long group_extra,
232 void *parent, unsigned long long parent_extra, string_index name_index ) {
233 itt_make_task_group_v7( domain, group, group_extra, parent, parent_extra, name_index );
234 }
235
236 inline void itt_metadata_str_add( itt_domain_enum domain, void *addr, unsigned long long addr_extra,
237 string_index key, const char *value ) {
238 itt_metadata_str_add_v7( domain, addr, addr_extra, key, value );
239 }
240
241 inline void itt_relation_add( itt_domain_enum domain, void *addr0, unsigned long long addr0_extra,
242 itt_relation relation, void *addr1, unsigned long long addr1_extra ) {
243 itt_relation_add_v7( domain, addr0, addr0_extra, relation, addr1, addr1_extra );
244 }
245
246 inline void itt_task_begin( itt_domain_enum domain, void *task, unsigned long long task_extra,
247 void *parent, unsigned long long parent_extra, string_index name_index ) {
248 itt_task_begin_v7( domain, task, task_extra, parent, parent_extra, name_index );
249 }
250
251 inline void itt_task_end( itt_domain_enum domain ) {
252 itt_task_end_v7( domain );
253 }
254
255 inline void itt_region_begin( itt_domain_enum domain, void *region, unsigned long long region_extra,
256 void *parent, unsigned long long parent_extra, string_index name_index ) {
257 itt_region_begin_v9( domain, region, region_extra, parent, parent_extra, name_index );
258 }
259
260 inline void itt_region_end( itt_domain_enum domain, void *region, unsigned long long region_extra ) {
261 itt_region_end_v9( domain, region, region_extra );
262 }
263#else
264 inline void call_itt_notify(notify_type /*t*/, void* /*ptr*/) {}
265
266 inline void itt_make_task_group( itt_domain_enum /*domain*/, void* /*group*/, unsigned long long /*group_extra*/,
267 void* /*parent*/, unsigned long long /*parent_extra*/, string_index /*name_index*/ ) {}
268
269 inline void itt_metadata_str_add( itt_domain_enum /*domain*/, void* /*addr*/, unsigned long long /*addr_extra*/,
270 string_index /*key*/, const char* /*value*/ ) {}
271
272 inline void itt_relation_add( itt_domain_enum /*domain*/, void* /*addr0*/, unsigned long long /*addr0_extra*/,
273 itt_relation /*relation*/, void* /*addr1*/, unsigned long long /*addr1_extra*/ ) {}
274
275 inline void itt_task_begin( itt_domain_enum /*domain*/, void* /*task*/, unsigned long long /*task_extra*/,
276 void* /*parent*/, unsigned long long /*parent_extra*/, string_index /*name_index*/ ) {}
277
278 inline void itt_task_end( itt_domain_enum /*domain*/ ) {}
279
280 inline void itt_region_begin( itt_domain_enum /*domain*/, void* /*region*/, unsigned long long /*region_extra*/,
281 void* /*parent*/, unsigned long long /*parent_extra*/, string_index /*name_index*/ ) {}
282
283 inline void itt_region_end( itt_domain_enum /*domain*/, void* /*region*/, unsigned long long /*region_extra*/ ) {}
284#endif // TBB_USE_THREADING_TOOLS
285
286 } // namespace internal
287} // namespace tbb
288
289#if TBB_PREVIEW_FLOW_GRAPH_TRACE
290#include <string>
291
292namespace tbb {
293namespace profiling {
294namespace interface10 {
295
296#if TBB_USE_THREADING_TOOLS && !(TBB_USE_THREADING_TOOLS == 2)
297class event {
298/** This class supports user event traces through itt.
299 Common use-case is tagging data flow graph tasks (data-id)
300 and visualization by Intel Advisor Flow Graph Analyzer (FGA) **/
301// TODO: Replace implementation by itt user event api.
302
303 const std::string my_name;
304
305 static void emit_trace(const std::string &input) {
306 itt_metadata_str_add( tbb::internal::ITT_DOMAIN_FLOW, NULL, tbb::internal::FLOW_NULL, tbb::internal::USER_EVENT, ( "FGA::DATAID::" + input ).c_str() );
307 }
308
309public:
310 event(const std::string &input)
311 : my_name( input )
312 { }
313
314 void emit() {
315 emit_trace(my_name);
316 }
317
318 static void emit(const std::string &description) {
319 emit_trace(description);
320 }
321
322};
323#else // TBB_USE_THREADING_TOOLS && !(TBB_USE_THREADING_TOOLS == 2)
324// Using empty struct if user event tracing is disabled:
325struct event {
326 event(const std::string &) { }
327
328 void emit() { }
329
330 static void emit(const std::string &) { }
331};
332#endif // TBB_USE_THREADING_TOOLS && !(TBB_USE_THREADING_TOOLS == 2)
333
334} // interfaceX
335using interface10::event;
336} // namespace profiling
337} // namespace tbb
338#endif // TBB_PREVIEW_FLOW_GRAPH_TRACE
339
340#endif /* __TBB_profiling_H */
341