Lockless Task Scheduler  v1.0a
A lockless task scheduler
profileitt.h
1 // ***********************************************************************
2 // Assembly : task_scheduler
3 // Author : viknash
4 // ***********************************************************************
5 // <copyright file="profile.h" >
6 // Copyright (c) viknash. All rights reserved.
7 // </copyright>
8 // <summary></summary>
9 // ***********************************************************************
10 #pragma once
11 
12 #include <thread>
13 
14 #include "utils.h"
15 #include "containers.h"
16 #include "profile.h"
17 
21 namespace task_scheduler
22 {
23  namespace profile {
24 
25 #if TASK_SCHEDULER_PROFILER == TASK_SCHEDULER_PROFILER_ITT
26  class itt_errors
27  {
28  public:
29 
30  uint32_t get_itt_error(enum errors::type _error)
31  {
32  switch (_error)
33  {
34  case errors::threads:
35  return __itt_suppress_threading_errors;
36  case errors::memory:
37  return __itt_suppress_memory_errors;
38  case errors::all:
39  return __itt_suppress_all_errors;
40  }
41  }
42 
43  inline void suppress(enum errors::type _error)
44  {
45  __itt_suppress_push((unsigned int)get_itt_error(_error));
46  }
47 
48  inline void unsuppress(enum errors::type _error)
49  {
50  ts_unused(_error);
51  __itt_suppress_pop();
52  }
53 
54  };
55 
56  template <class TKey>
57  static errors* instance()
58  {
59  static error_stack<itt_errors> error_instance;
60  return &error_instance;
61  }
62 
63  class memory_itt : public memory
64  {
65  public:
66  typedef memory super;
67 
68  void set_name(const tchar_t* _name) override;
69 
70  void allocate_begin(size_t _size, bool _initialized) override
71  {
72  if (!heap)
73  return;
74  __itt_heap_allocate_begin(heap, _size, _initialized ? 1 : 0);
75  }
76 
77  void allocate_end(void** _memory_allocation, size_t _size, bool _initialized) override
78  {
79  if (!heap)
80  return;
81  __itt_heap_allocate_end(heap, _memory_allocation, _size, _initialized ? 1 : 0);
82  }
83 
84  private:
85  __itt_heap_function heap;
86  };
87 
88  template< typename TKey>
89  memory* memory::instance()
90  {
91  static memory_itt memory;
92  return &memory;
93  }
94 
95  void memory_itt::set_name(const tchar_t* _name)
96  {
97  super::set_name(_name);
98  const tchar_t* domain_name = domain::instance<domain>()->name;
99  heap = __itt_heap_function_create(_name, domain_name);
100  }
101 
102  class domain_itt : public domain
103  {
104  typedef domain super;
105  public:
106  void set_name(const tchar_t* _name) override
107  {
108  super::set_name(_name);
109  itt_domain = __itt_domain_create(_name);
110  }
111 
112  super::handle operator* () override {
113  return (super::handle)itt_domain;
114  }
115 
116  __itt_domain* get_domain() const
117  {
118  ts_assert(itt_domain);
119  return itt_domain;
120  }
121 
122  private:
123  __itt_domain* itt_domain;
124  };
125 
126  template <class TKey>
127  static domain* domain::instance()
128  {
129  static domain_itt domain;
130  return &domain;
131  }
132 
134  {
135  public:
136  typedef __itt_string_handle* handle;
137 
138  itt_string(const tchar_t* _name)
139  {
140  string_handle = __itt_string_handle_create(_name);
141  }
142 
143  typename handle& operator* ()
144  {
145  return string_handle;
146  }
147  private:
148  __itt_string_handle* string_handle;
149  };
150 
152 
153  class id
154  {
155  typedef __itt_id handle;
156  public:
157  id(const tchar_t* _name)
158  : itt_id(__itt_null)
159  {
160  itt_id = __itt_id_make(reinterpret_cast<void*>(const_cast<tchar_t*>(_name)), thread_unique_number);
161  __itt_id_create((get_as<domain, domain_itt>()->get_domain()), itt_id);
162  }
163 
164  typename handle& operator* ()
165  {
166  return itt_id;
167  }
168 
169  private:
170  __itt_id itt_id;
171  };
172 
173  struct task_param
174  {
175  function task_function;
176  id task_id;
177  id parent_task_id;
178  string task_name;
179 
180  task_param(function _func, const tchar_t* _task_id, const tchar_t* _parent_task_id, const tchar_t* _task_name)
181  : task_function(_func)
182  , task_id(_task_id)
183  , parent_task_id(_parent_task_id)
184  , task_name(_task_name)
185  {}
186  };
187 
188  class task
189  {
190 
191  public:
192 
193  task()
194  {
195  }
196 
197  bool enter(task_param& _param)
198  {
199  if (*_param.task_function)
200  {
201  __itt_task_begin_fn((get_as<domain, domain_itt>()->get_domain()), *_param.task_id, *_param.parent_task_id, *_param.task_function);
202  }
203  else
204  {
205  __itt_task_begin((get_as<domain, domain_itt>()->get_domain()), *_param.task_id, *_param.parent_task_id, *_param.task_name);
206  }
207  return true;
208  }
209 
210  bool exit(task_param& param)
211  {
212  __itt_task_end((get_as<domain, domain_itt>()->get_domain()));
213  return true;
214  }
215  };
216 
218 
219 #endif
220 
221  }
222 }
thread_local thread_num_t thread_unique_number
The thread unique number
Definition: globals.h:72
Definition: profileitt.h:102
Class stl_allocator.
Definition: allocator.h:16
Definition: profilebase.h:212
Definition: profilebase.h:101
Definition: profilebase.h:128
Definition: profileitt.h:63
Class scoped_enter_exit.
Definition: utils.h:140
Definition: profileitt.h:153
Definition: profileitt.h:26
Definition: profileitt.h:188
Definition: profilebase.h:80
Definition: profileitt.h:133
Definition: profileitt.h:173