1 // -*- C++ -*-
2 //
3 // Copyright (C) 2009-2018 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library.  This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15 
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
19 
20 // You should have received a copy of the GNU General Public License along
21 // with this library; see the file COPYING3.  If not see
22 // <http://www.gnu.org/licenses/>.
23 
24 /** @file profile/impl/profiler.h
25  *  @brief Interface of the profiling runtime library.
26  */
27 
28 // Written by Lixia Liu and Silvius Rus.
29 
30 #ifndef _GLIBCXX_PROFILE_PROFILER_H
31 #define _GLIBCXX_PROFILE_PROFILER_H 1
32 
33 #include <bits/c++config.h>
34 
35 // Mechanism to define data with inline linkage.
36 #define _GLIBCXX_PROFILE_DEFINE_UNINIT_DATA(__type, __name)             \
37   inline __type&                                                        \
38   __get_##__name()                                                      \
39   {                                                                     \
40     static __type __name;                                               \
41     return __name;                                                      \
42   }
43 #define _GLIBCXX_PROFILE_DEFINE_DATA(__type, __name, __initial_value...) \
44   inline __type& __get_##__name() {                                      \
45     static __type __name(__initial_value);                               \
46     return __name;                                                       \
47   }
48 #define _GLIBCXX_PROFILE_DATA(__name) \
49   __get_##__name()
50 
51 namespace __gnu_profile
52 {
53   /** @brief Reentrance guard.
54    *
55    * Mechanism to protect all __gnu_profile operations against recursion,
56    * multithreaded and exception reentrance.
57    */
58   struct __reentrance_guard
59   {
60     static bool
61     __get_in()
62     {
63       if (__inside() == true)
64 	return false;
65       else
66 	{
67 	  __inside() = true;
68 	  return true;
69 	}
70     }
71 
72     static bool&
73     __inside()
74     {
75       static __thread bool _S_inside(false);
76       return _S_inside;
77     }
78 
79     __reentrance_guard() { }
80     ~__reentrance_guard() { __inside() = false; }
81   };
82 
83   // Forward declarations of implementation functions.
84   // Don't use any __gnu_profile:: in user code.
85   // Instead, use the __profcxx... macros, which offer guarded access.
86   class __container_size_info;
87   class __hashfunc_info;
88   class __map2umap_info;
89   class __vector2list_info;
90   class __list2slist_info;
91   class __list2vector_info;
92 
93   bool __turn_on();
94   bool __turn_off();
95   bool __is_invalid();
96   bool __is_on();
97   bool __is_off();
98   void __report();
99 
100   __container_size_info*
101   __trace_hashtable_size_construct(std::size_t);
102   void __trace_hashtable_size_resize(__container_size_info*,
103 				     std::size_t, std::size_t);
104   void __trace_hashtable_size_destruct(__container_size_info*,
105 				       std::size_t, std::size_t);
106 
107   __hashfunc_info*
108   __trace_hash_func_construct();
109   void __trace_hash_func_destruct(__hashfunc_info*,
110 				  std::size_t, std::size_t, std::size_t);
111 
112   __container_size_info*
113   __trace_vector_size_construct(std::size_t);
114   void __trace_vector_size_resize(__container_size_info*,
115 				  std::size_t, std::size_t);
116   void __trace_vector_size_destruct(__container_size_info*,
117 				    std::size_t, std::size_t);
118 
119   __vector2list_info*
120   __trace_vector_to_list_construct();
121   void __trace_vector_to_list_insert(__vector2list_info*,
122 				     std::size_t, std::size_t);
123   void __trace_vector_to_list_iterate(__vector2list_info*, int);
124   void __trace_vector_to_list_invalid_operator(__vector2list_info*);
125   void __trace_vector_to_list_resize(__vector2list_info*,
126 				     std::size_t, std::size_t);
127   void __trace_vector_to_list_destruct(__vector2list_info*);
128 
129   __list2slist_info*
130   __trace_list_to_slist_construct();
131   void __trace_list_to_slist_rewind(__list2slist_info*);
132   void __trace_list_to_slist_operation(__list2slist_info*);
133   void __trace_list_to_slist_destruct(__list2slist_info*);
134 
135   __list2vector_info*
136   __trace_list_to_vector_construct();
137   void __trace_list_to_vector_insert(__list2vector_info*,
138 				     std::size_t, std::size_t);
139   void __trace_list_to_vector_iterate(__list2vector_info*, int);
140   void __trace_list_to_vector_invalid_operator(__list2vector_info*);
141   void __trace_list_to_vector_resize(__list2vector_info*,
142 				     std::size_t, std::size_t);
143   void __trace_list_to_vector_destruct(__list2vector_info*);
144 
145   __map2umap_info*
146   __trace_map_to_unordered_map_construct();
147   void __trace_map_to_unordered_map_invalidate(__map2umap_info*);
148   void __trace_map_to_unordered_map_insert(__map2umap_info*, std::size_t,
149 					   std::size_t);
150   void __trace_map_to_unordered_map_erase(__map2umap_info*, std::size_t,
151 					  std::size_t);
152   void __trace_map_to_unordered_map_iterate(__map2umap_info*, std::size_t);
153   void __trace_map_to_unordered_map_find(__map2umap_info*, std::size_t);
154   void __trace_map_to_unordered_map_destruct(__map2umap_info*);
155 } // namespace __gnu_profile
156 
157 // Master switch turns on all diagnostics that are not explicitly turned off.
158 #ifdef _GLIBCXX_PROFILE
159 #ifndef _GLIBCXX_PROFILE_NO_HASHTABLE_TOO_SMALL
160 #define _GLIBCXX_PROFILE_HASHTABLE_TOO_SMALL
161 #endif
162 #ifndef _GLIBCXX_PROFILE_NO_HASHTABLE_TOO_LARGE
163 #define _GLIBCXX_PROFILE_HASHTABLE_TOO_LARGE
164 #endif
165 #ifndef _GLIBCXX_PROFILE_NO_VECTOR_TOO_SMALL
166 #define _GLIBCXX_PROFILE_VECTOR_TOO_SMALL
167 #endif
168 #ifndef _GLIBCXX_PROFILE_NO_VECTOR_TOO_LARGE
169 #define _GLIBCXX_PROFILE_VECTOR_TOO_LARGE
170 #endif
171 #ifndef _GLIBCXX_PROFILE_NO_INEFFICIENT_HASH
172 #define _GLIBCXX_PROFILE_INEFFICIENT_HASH
173 #endif
174 #ifndef _GLIBCXX_PROFILE_NO_VECTOR_TO_LIST
175 #define _GLIBCXX_PROFILE_VECTOR_TO_LIST
176 #endif
177 #ifndef _GLIBCXX_PROFILE_NO_LIST_TO_SLIST
178 #define _GLIBCXX_PROFILE_LIST_TO_SLIST
179 #endif
180 #ifndef _GLIBCXX_PROFILE_NO_LIST_TO_VECTOR
181 #define _GLIBCXX_PROFILE_LIST_TO_VECTOR
182 #endif
183 #ifndef _GLIBCXX_PROFILE_NO_MAP_TO_UNORDERED_MAP
184 #define _GLIBCXX_PROFILE_MAP_TO_UNORDERED_MAP
185 #endif
186 #endif
187 
188 // Expose global management routines to user code.
189 #ifdef _GLIBCXX_PROFILE
190 #define __profcxx_report() __gnu_profile::__report()
191 #define __profcxx_turn_on() __gnu_profile::__turn_on()
192 #define __profcxx_turn_off() __gnu_profile::__turn_off()
193 #define __profcxx_is_invalid() __gnu_profile::__is_invalid()
194 #define __profcxx_is_on() __gnu_profile::__is_on()
195 #define __profcxx_is_off() __gnu_profile::__is_off()
196 #else
197 #define __profcxx_report()
198 #define __profcxx_turn_on()
199 #define __profcxx_turn_off()
200 #define __profcxx_is_invalid()
201 #define __profcxx_is_on()
202 #define __profcxx_is_off()
203 #endif
204 
205 // Turn on/off instrumentation for HASHTABLE_TOO_SMALL and HASHTABLE_TOO_LARGE.
206 #if (defined(_GLIBCXX_PROFILE_HASHTABLE_TOO_SMALL) \
207      || defined(_GLIBCXX_PROFILE_HASHTABLE_TOO_LARGE))
208 #define __profcxx_hashtable_size_construct(__x...) \
209   __gnu_profile::__trace_hashtable_size_construct(__x)
210 #define __profcxx_hashtable_size_resize(__x...) \
211   __gnu_profile::__trace_hashtable_size_resize(__x)
212 #define __profcxx_hashtable_size_destruct(__x...) \
213   __gnu_profile::__trace_hashtable_size_destruct(__x)
214 #else
215 #define __profcxx_hashtable_size_construct(__x...) 0
216 #define __profcxx_hashtable_size_resize(__x...)
217 #define __profcxx_hashtable_size_destruct(__x...)
218 #endif
219 
220 // Turn on/off instrumentation for VECTOR_TOO_SMALL and VECTOR_TOO_LARGE.
221 #if (defined(_GLIBCXX_PROFILE_VECTOR_TOO_SMALL) \
222      || defined(_GLIBCXX_PROFILE_VECTOR_TOO_LARGE))
223 #define __profcxx_vector_size_construct(__x...) \
224   __gnu_profile::__trace_vector_size_construct(__x)
225 #define __profcxx_vector_size_resize(__x...) \
226   __gnu_profile::__trace_vector_size_resize(__x)
227 #define __profcxx_vector_size_destruct(__x...) \
228   __gnu_profile::__trace_vector_size_destruct(__x)
229 #else
230 #define __profcxx_vector_size_construct(__x...) 0
231 #define __profcxx_vector_size_resize(__x...)
232 #define __profcxx_vector_size_destruct(__x...)
233 #endif
234 
235 // Turn on/off instrumentation for INEFFICIENT_HASH.
236 #if defined(_GLIBCXX_PROFILE_INEFFICIENT_HASH)
237 #define __profcxx_hash_func_construct(__x...) \
238   __gnu_profile::__trace_hash_func_construct(__x)
239 #define __profcxx_hash_func_destruct(__x...) \
240   __gnu_profile::__trace_hash_func_destruct(__x)
241 #else
242 #define __profcxx_hash_func_construct(__x...) 0
243 #define __profcxx_hash_func_destruct(__x...)
244 #endif
245 
246 // Turn on/off instrumentation for VECTOR_TO_LIST.
247 #if defined(_GLIBCXX_PROFILE_VECTOR_TO_LIST)
248 #define __profcxx_vector2list_construct(__x...) \
249   __gnu_profile::__trace_vector_to_list_construct(__x)
250 #define __profcxx_vector2list_insert(__x...) \
251   __gnu_profile::__trace_vector_to_list_insert(__x)
252 #define __profcxx_vector2list_iterate(__x...) \
253   __gnu_profile::__trace_vector_to_list_iterate(__x)
254 #define __profcxx_vector2list_invalid_operator(__x...) \
255   __gnu_profile::__trace_vector_to_list_invalid_operator(__x)
256 #define __profcxx_vector2list_resize(__x...) \
257   __gnu_profile::__trace_vector_to_list_resize(__x)
258 #define __profcxx_vector2list_destruct(__x...) \
259   __gnu_profile::__trace_vector_to_list_destruct(__x)
260 #else
261 #define __profcxx_vector2list_construct(__x...) 0
262 #define __profcxx_vector2list_insert(__x...)
263 #define __profcxx_vector2list_iterate(__x...)
264 #define __profcxx_vector2list_invalid_operator(__x...)
265 #define __profcxx_vector2list_resize(__x...)
266 #define __profcxx_vector2list_destruct(__x...)
267 #endif
268 
269 // Turn on/off instrumentation for LIST_TO_VECTOR.
270 #if defined(_GLIBCXX_PROFILE_LIST_TO_VECTOR)
271 #define __profcxx_list2vector_construct(__x...) \
272   __gnu_profile::__trace_list_to_vector_construct(__x)
273 #define __profcxx_list2vector_insert(__x...) \
274   __gnu_profile::__trace_list_to_vector_insert(__x)
275 #define __profcxx_list2vector_iterate(__x...) \
276   __gnu_profile::__trace_list_to_vector_iterate(__x)
277 #define __profcxx_list2vector_invalid_operator(__x...) \
278   __gnu_profile::__trace_list_to_vector_invalid_operator(__x)
279 #define __profcxx_list2vector_destruct(__x...) \
280   __gnu_profile::__trace_list_to_vector_destruct(__x)
281 #else
282 #define __profcxx_list2vector_construct(__x...) 0
283 #define __profcxx_list2vector_insert(__x...)
284 #define __profcxx_list2vector_iterate(__x...)
285 #define __profcxx_list2vector_invalid_operator(__x...)
286 #define __profcxx_list2vector_destruct(__x...)
287 #endif
288 
289 // Turn on/off instrumentation for LIST_TO_SLIST.
290 #if defined(_GLIBCXX_PROFILE_LIST_TO_SLIST)
291 #define __profcxx_list2slist_construct(__x...) \
292     __gnu_profile::__trace_list_to_slist_construct(__x)
293 #define __profcxx_list2slist_rewind(__x...) \
294   __gnu_profile::__trace_list_to_slist_rewind(__x)
295 #define __profcxx_list2slist_operation(__x...) \
296   __gnu_profile::__trace_list_to_slist_operation(__x)
297 #define __profcxx_list2slist_destruct(__x...) \
298   __gnu_profile::__trace_list_to_slist_destruct(__x)
299 #else
300 #define __profcxx_list2slist_construct(__x...) 0
301 #define __profcxx_list2slist_rewind(__x...)
302 #define __profcxx_list2slist_operation(__x...)
303 #define __profcxx_list2slist_destruct(__x...)
304 #endif
305 
306 // Turn on/off instrumentation for MAP_TO_UNORDERED_MAP.
307 #if defined(_GLIBCXX_PROFILE_MAP_TO_UNORDERED_MAP)
308 #define __profcxx_map2umap_construct(__x...) \
309   __gnu_profile::__trace_map_to_unordered_map_construct(__x)
310 #define __profcxx_map2umap_insert(__x...) \
311   __gnu_profile::__trace_map_to_unordered_map_insert(__x)
312 #define __profcxx_map2umap_erase(__x...) \
313   __gnu_profile::__trace_map_to_unordered_map_erase(__x)
314 #define __profcxx_map2umap_iterate(__x...) \
315   __gnu_profile::__trace_map_to_unordered_map_iterate(__x)
316 #define __profcxx_map2umap_invalidate(__x...) \
317   __gnu_profile::__trace_map_to_unordered_map_invalidate(__x)
318 #define __profcxx_map2umap_find(__x...) \
319   __gnu_profile::__trace_map_to_unordered_map_find(__x)
320 #define __profcxx_map2umap_destruct(__x...) \
321   __gnu_profile::__trace_map_to_unordered_map_destruct(__x)
322 #else
323 #define __profcxx_map2umap_construct(__x...) 0
324 #define __profcxx_map2umap_insert(__x...)
325 #define __profcxx_map2umap_erase(__x...)
326 #define __profcxx_map2umap_iterate(__x...)
327 #define __profcxx_map2umap_invalidate(__x...)
328 #define __profcxx_map2umap_find(__x...)
329 #define __profcxx_map2umap_destruct(__x...)
330 #endif
331 
332 // Set default values for compile-time customizable variables.
333 #ifndef _GLIBCXX_PROFILE_TRACE_PATH_ROOT
334 #define _GLIBCXX_PROFILE_TRACE_PATH_ROOT "libstdcxx-profile"
335 #endif
336 #ifndef _GLIBCXX_PROFILE_TRACE_ENV_VAR
337 #define _GLIBCXX_PROFILE_TRACE_ENV_VAR "_GLIBCXX_PROFILE_TRACE_PATH_ROOT"
338 #endif
339 #ifndef _GLIBCXX_PROFILE_MAX_WARN_COUNT_ENV_VAR
340 #define _GLIBCXX_PROFILE_MAX_WARN_COUNT_ENV_VAR \
341   "_GLIBCXX_PROFILE_MAX_WARN_COUNT"
342 #endif
343 #ifndef _GLIBCXX_PROFILE_MAX_WARN_COUNT
344 #define _GLIBCXX_PROFILE_MAX_WARN_COUNT 10
345 #endif
346 #ifndef _GLIBCXX_PROFILE_MAX_STACK_DEPTH
347 #define _GLIBCXX_PROFILE_MAX_STACK_DEPTH 32
348 #endif
349 #ifndef _GLIBCXX_PROFILE_MAX_STACK_DEPTH_ENV_VAR
350 #define _GLIBCXX_PROFILE_MAX_STACK_DEPTH_ENV_VAR \
351   "_GLIBCXX_PROFILE_MAX_STACK_DEPTH"
352 #endif
353 #ifndef _GLIBCXX_PROFILE_MEM_PER_DIAGNOSTIC
354 #define _GLIBCXX_PROFILE_MEM_PER_DIAGNOSTIC (1 << 28)
355 #endif
356 #ifndef _GLIBCXX_PROFILE_MEM_PER_DIAGNOSTIC_ENV_VAR
357 #define _GLIBCXX_PROFILE_MEM_PER_DIAGNOSTIC_ENV_VAR \
358   "_GLIBCXX_PROFILE_MEM_PER_DIAGNOSTIC"
359 #endif
360 
361 // Instrumentation hook implementations.
362 #include "profile/impl/profiler_hash_func.h"
363 #include "profile/impl/profiler_hashtable_size.h"
364 #include "profile/impl/profiler_map_to_unordered_map.h"
365 #include "profile/impl/profiler_vector_size.h"
366 #include "profile/impl/profiler_vector_to_list.h"
367 #include "profile/impl/profiler_list_to_slist.h"
368 #include "profile/impl/profiler_list_to_vector.h"
369 
370 #endif // _GLIBCXX_PROFILE_PROFILER_H
371