Merged revision 156805 into branch.
[official-gcc.git] / libstdc++-v3 / include / profile / impl / profiler.h
blobb5ac409bb464dfafa6f789e87d8be648c2d1a815
1 // -*- C++ -*-
2 //
3 // Copyright (C) 2009, 2010 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 terms
7 // of the GNU General Public License as published by the Free Software
8 // Foundation; either version 2, or (at your option) any later
9 // version.
11 // This library is distributed in the hope that it will be useful, but
12 // WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this library; see the file COPYING. If not, write to
18 // the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
19 // MA 02111-1307, USA.
21 // As a special exception, you may use this file as part of a free
22 // software library without restriction. Specifically, if other files
23 // instantiate templates or use macros or inline functions from this
24 // file, or you compile this file and link it with other files to
25 // produce an executable, this file does not by itself cause the
26 // resulting executable to be covered by the GNU General Public
27 // License. This exception does not however invalidate any other
28 // reasons why the executable file might be covered by the GNU General
29 // Public License.
31 /** @file profile/impl/profiler.h
32 * @brief Interface of the profiling runtime library.
35 // Written by Lixia Liu and Silvius Rus.
37 #ifndef _GLIBCXX_PROFILE_PROFILER_H
38 #define _GLIBCXX_PROFILE_PROFILER_H 1
40 #ifdef __GXX_EXPERIMENTAL_CXX0X__
41 #include <cstddef>
42 #else
43 #include <stddef.h>
44 #endif
46 // Mechanism to define data with inline linkage.
47 #define _GLIBCXX_PROFILE_DEFINE_DATA(__type, __name, __initial_value...) \
48 inline __type& __get_##__name() { \
49 static __type __name(__initial_value); \
50 return __name; \
52 #define _GLIBCXX_PROFILE_DATA(__name) \
53 __get_##__name()
55 namespace __gnu_profile
57 /** @brief Reentrance guard.
59 * Mechanism to protect all __gnu_profile operations against recursion,
60 * multithreaded and exception reentrance.
62 struct __reentrance_guard
64 static bool
65 __get_in()
67 if (__inside() == true)
68 return false;
69 else
71 __inside() = true;
72 return true;
76 static bool&
77 __inside()
79 static __thread bool _S_inside(false);
80 return _S_inside;
83 __reentrance_guard() { }
84 ~__reentrance_guard() { __inside() = false; }
87 #define _GLIBCXX_PROFILE_REENTRANCE_GUARD(__x...) \
88 { \
89 if (__gnu_profile::__reentrance_guard::__get_in()) \
90 { \
91 __gnu_profile::__reentrance_guard __get_out; \
92 __x; \
93 } \
96 // Forward declarations of implementation functions.
97 // Don't use any __gnu_profile:: in user code.
98 // Instead, use the __profcxx... macros, which offer guarded access.
99 bool __turn_on();
100 bool __turn_off();
101 bool __is_invalid();
102 bool __is_on();
103 bool __is_off();
104 void __report(void);
105 void __trace_hashtable_size_resize(const void*, size_t, size_t);
106 void __trace_hashtable_size_destruct(const void*, size_t, size_t);
107 void __trace_hashtable_size_construct(const void*, size_t);
108 void __trace_vector_size_resize(const void*, size_t, size_t);
109 void __trace_vector_size_destruct(const void*, size_t, size_t);
110 void __trace_vector_size_construct(const void*, size_t);
111 void __trace_hash_func_destruct(const void*, size_t, size_t, size_t);
112 void __trace_hash_func_construct(const void*);
113 void __trace_vector_to_list_destruct(const void*);
114 void __trace_vector_to_list_construct(const void*);
115 void __trace_vector_to_list_insert(const void*, size_t, size_t);
116 void __trace_vector_to_list_iterate(const void*, size_t);
117 void __trace_vector_to_list_invalid_operator(const void*);
118 void __trace_vector_to_list_resize(const void*, size_t, size_t);
119 void __trace_vector_to_list_find(const void*, size_t);
121 void __trace_list_to_slist_destruct(const void*);
122 void __trace_list_to_slist_construct(const void*);
123 void __trace_list_to_slist_rewind(const void*);
124 void __trace_list_to_slist_operation(const void*);
126 void __trace_list_to_vector_destruct(const void*);
127 void __trace_list_to_vector_construct(const void*);
128 void __trace_list_to_vector_insert(const void*, size_t, size_t);
129 void __trace_list_to_vector_iterate(const void*, size_t);
130 void __trace_list_to_vector_invalid_operator(const void*);
131 void __trace_list_to_vector_resize(const void*, size_t, size_t);
133 void __trace_list_to_set_destruct(const void*);
134 void __trace_list_to_set_construct(const void*);
135 void __trace_list_to_set_insert(const void*, size_t, size_t);
136 void __trace_list_to_set_iterate(const void*, size_t);
137 void __trace_list_to_set_invalid_operator(const void*);
138 void __trace_list_to_set_find(const void*, size_t);
140 void __trace_map_to_unordered_map_construct(const void*);
141 void __trace_map_to_unordered_map_invalidate(const void*);
142 void __trace_map_to_unordered_map_insert(const void*, size_t, size_t);
143 void __trace_map_to_unordered_map_erase(const void*, size_t, size_t);
144 void __trace_map_to_unordered_map_iterate(const void*, size_t);
145 void __trace_map_to_unordered_map_find(const void*, size_t);
146 void __trace_map_to_unordered_map_destruct(const void*);
147 } // namespace __gnu_profile
149 // Master switch turns on all diagnostics that are not explicitly turned off.
150 #ifdef _GLIBCXX_PROFILE
151 #ifndef _GLIBCXX_PROFILE_NO_HASHTABLE_TOO_SMALL
152 #define _GLIBCXX_PROFILE_HASHTABLE_TOO_SMALL
153 #endif
154 #ifndef _GLIBCXX_PROFILE_NO_HASHTABLE_TOO_LARGE
155 #define _GLIBCXX_PROFILE_HASHTABLE_TOO_LARGE
156 #endif
157 #ifndef _GLIBCXX_PROFILE_NO_VECTOR_TOO_SMALL
158 #define _GLIBCXX_PROFILE_VECTOR_TOO_SMALL
159 #endif
160 #ifndef _GLIBCXX_PROFILE_NO_VECTOR_TOO_LARGE
161 #define _GLIBCXX_PROFILE_VECTOR_TOO_LARGE
162 #endif
163 #ifndef _GLIBCXX_PROFILE_NO_INEFFICIENT_HASH
164 #define _GLIBCXX_PROFILE_INEFFICIENT_HASH
165 #endif
166 #ifndef _GLIBCXX_PROFILE_NO_VECTOR_TO_LIST
167 #define _GLIBCXX_PROFILE_VECTOR_TO_LIST
168 #endif
169 #ifndef _GLIBCXX_PROFILE_NO_LIST_TO_SLIST
170 #define _GLIBCXX_PROFILE_LIST_TO_SLIST
171 #endif
172 #ifndef _GLIBCXX_PROFILE_NO_LIST_TO_VECTOR
173 #define _GLIBCXX_PROFILE_LIST_TO_VECTOR
174 #endif
175 #ifndef _GLIBCXX_PROFILE_NO_MAP_TO_UNORDERED_MAP
176 #define _GLIBCXX_PROFILE_MAP_TO_UNORDERED_MAP
177 #endif
178 #endif
180 // Expose global management routines to user code.
181 #ifdef _GLIBCXX_PROFILE
182 #define __profcxx_report() \
183 _GLIBCXX_PROFILE_REENTRANCE_GUARD(__gnu_profile::__report())
184 #define __profcxx_turn_on() \
185 _GLIBCXX_PROFILE_REENTRANCE_GUARD(__gnu_profile::__turn_on())
186 #define __profcxx_turn_off() \
187 _GLIBCXX_PROFILE_REENTRANCE_GUARD(__gnu_profile::__turn_off())
188 #define __profcxx_is_invalid() \
189 _GLIBCXX_PROFILE_REENTRANCE_GUARD(__gnu_profile::__is_invalid())
190 #define __profcxx_is_on() \
191 _GLIBCXX_PROFILE_REENTRANCE_GUARD(__gnu_profile::__is_on())
192 #define __profcxx__is_off() \
193 _GLIBCXX_PROFILE_REENTRANCE_GUARD(__gnu_profile::__is_off())
194 #else
195 #define __profcxx_report()
196 #define __profcxx_turn_on()
197 #define __profcxx_turn_off()
198 #define __profcxx_is_invalid()
199 #define __profcxx_is_on()
200 #define __profcxx_is_off()
201 #endif
203 // Turn on/off instrumentation for HASHTABLE_TOO_SMALL and HASHTABLE_TOO_LARGE.
204 #if (defined(_GLIBCXX_PROFILE_HASHTABLE_TOO_SMALL) \
205 || defined(_GLIBCXX_PROFILE_HASHTABLE_TOO_LARGE))
206 #define __profcxx_hashtable_resize(__x...) \
207 _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
208 __gnu_profile::__trace_hashtable_size_resize(__x))
209 #define __profcxx_hashtable_destruct(__x...) \
210 _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
211 __gnu_profile::__trace_hashtable_size_destruct(__x))
212 #define __profcxx_hashtable_construct(__x...) \
213 _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
214 __gnu_profile::__trace_hashtable_size_construct(__x))
215 #else
216 #define __profcxx_hashtable_resize(__x...)
217 #define __profcxx_hashtable_destruct(__x...)
218 #define __profcxx_hashtable_construct(__x...)
219 #endif
221 // Turn on/off instrumentation for VECTOR_TOO_SMALL and VECTOR_TOO_LARGE.
222 #if (defined(_GLIBCXX_PROFILE_VECTOR_TOO_SMALL) \
223 || defined(_GLIBCXX_PROFILE_VECTOR_TOO_LARGE))
224 #define __profcxx_vector_resize(__x...) \
225 _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
226 __gnu_profile::__trace_vector_size_resize(__x))
227 #define __profcxx_vector_destruct(__x...) \
228 _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
229 __gnu_profile::__trace_vector_size_destruct(__x))
230 #define __profcxx_vector_construct(__x...) \
231 _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
232 __gnu_profile::__trace_vector_size_construct(__x))
233 #else
234 #define __profcxx_vector_resize(__x...)
235 #define __profcxx_vector_destruct(__x...)
236 #define __profcxx_vector_construct(__x...)
237 #endif
239 // Turn on/off instrumentation for INEFFICIENT_HASH.
240 #if defined(_GLIBCXX_PROFILE_INEFFICIENT_HASH)
241 #define __profcxx_hashtable_construct2(__x...) \
242 _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
243 __gnu_profile::__trace_hash_func_construct(__x))
244 #define __profcxx_hashtable_destruct2(__x...) \
245 _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
246 __gnu_profile::__trace_hash_func_destruct(__x))
247 #else
248 #define __profcxx_hashtable_destruct2(__x...)
249 #define __profcxx_hashtable_construct2(__x...)
250 #endif
252 // Turn on/off instrumentation for VECTOR_TO_LIST.
253 #if defined(_GLIBCXX_PROFILE_VECTOR_TO_LIST)
254 #define __profcxx_vector_construct2(__x...) \
255 _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
256 __gnu_profile::__trace_vector_to_list_construct(__x))
257 #define __profcxx_vector_destruct2(__x...) \
258 _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
259 __gnu_profile::__trace_vector_to_list_destruct(__x))
260 #define __profcxx_vector_insert(__x...) \
261 _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
262 __gnu_profile::__trace_vector_to_list_insert(__x))
263 #define __profcxx_vector_iterate(__x...) \
264 _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
265 __gnu_profile::__trace_vector_to_list_iterate(__x))
266 #define __profcxx_vector_invalid_operator(__x...) \
267 _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
268 __gnu_profile::__trace_vector_to_list_invalid_operator(__x))
269 #define __profcxx_vector_resize2(__x...) \
270 _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
271 __gnu_profile::__trace_vector_to_list_resize(__x))
272 #define __profcxx_vector_find(__x...) \
273 _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
274 __gnu_profile::__trace_vector_to_list_find(__x))
275 #else
276 #define __profcxx_vector_destruct2(__x...)
277 #define __profcxx_vector_construct2(__x...)
278 #define __profcxx_vector_insert(__x...)
279 #define __profcxx_vector_iterate(__x...)
280 #define __profcxx_vector_invalid_operator(__x...)
281 #define __profcxx_vector_resize2(__x...)
282 #define __profcxx_vector_find(__x...)
283 #endif
285 // Turn on/off instrumentation for LIST_TO_VECTOR.
286 #if defined(_GLIBCXX_PROFILE_LIST_TO_VECTOR)
287 #define __profcxx_list_construct2(__x...) \
288 _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
289 __gnu_profile::__trace_list_to_vector_construct(__x))
290 #define __profcxx_list_destruct2(__x...) \
291 _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
292 __gnu_profile::__trace_list_to_vector_destruct(__x))
293 #define __profcxx_list_insert(__x...) \
294 _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
295 __gnu_profile::__trace_list_to_vector_insert(__x))
296 #define __profcxx_list_iterate(__x...) \
297 _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
298 __gnu_profile::__trace_list_to_vector_iterate(__x))
299 #define __profcxx_list_invalid_operator(__x...) \
300 _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
301 __gnu_profile::__trace_list_to_vector_invalid_operator(__x))
302 #else
303 #define __profcxx_list_destruct2(__x...)
304 #define __profcxx_list_construct2(__x...)
305 #define __profcxx_list_insert(__x...)
306 #define __profcxx_list_iterate(__x...)
307 #define __profcxx_list_invalid_operator(__x...)
308 #endif
310 // Turn on/off instrumentation for LIST_TO_SLIST.
311 #if defined(_GLIBCXX_PROFILE_LIST_TO_SLIST)
312 #define __profcxx_list_rewind(__x...) \
313 _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
314 __gnu_profile::__trace_list_to_slist_rewind(__x))
315 #define __profcxx_list_operation(__x...) \
316 _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
317 __gnu_profile::__trace_list_to_slist_operation(__x))
318 #define __profcxx_list_destruct(__x...) \
319 _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
320 __gnu_profile::__trace_list_to_slist_destruct(__x))
321 #define __profcxx_list_construct(__x...) \
322 _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
323 __gnu_profile::__trace_list_to_slist_construct(__x))
324 #else
325 #define __profcxx_list_rewind(__x...)
326 #define __profcxx_list_operation(__x...)
327 #define __profcxx_list_destruct(__x...)
328 #define __profcxx_list_construct(__x...)
329 #endif
331 // Turn on/off instrumentation for MAP_TO_UNORDERED_MAP.
332 #if defined(_GLIBCXX_PROFILE_MAP_TO_UNORDERED_MAP)
333 #define __profcxx_map_to_unordered_map_construct(__x...) \
334 _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
335 __gnu_profile::__trace_map_to_unordered_map_construct(__x))
336 #define __profcxx_map_to_unordered_map_destruct(__x...) \
337 _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
338 __gnu_profile::__trace_map_to_unordered_map_destruct(__x))
339 #define __profcxx_map_to_unordered_map_insert(__x...) \
340 _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
341 __gnu_profile::__trace_map_to_unordered_map_insert(__x))
342 #define __profcxx_map_to_unordered_map_erase(__x...) \
343 _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
344 __gnu_profile::__trace_map_to_unordered_map_erase(__x))
345 #define __profcxx_map_to_unordered_map_iterate(__x...) \
346 _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
347 __gnu_profile::__trace_map_to_unordered_map_iterate(__x))
348 #define __profcxx_map_to_unordered_map_invalidate(__x...) \
349 _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
350 __gnu_profile::__trace_map_to_unordered_map_invalidate(__x))
351 #define __profcxx_map_to_unordered_map_find(__x...) \
352 _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
353 __gnu_profile::__trace_map_to_unordered_map_find(__x))
354 #else
355 #define __profcxx_map_to_unordered_map_construct(__x...) \
357 #define __profcxx_map_to_unordered_map_destruct(__x...)
358 #define __profcxx_map_to_unordered_map_insert(__x...)
359 #define __profcxx_map_to_unordered_map_erase(__x...)
360 #define __profcxx_map_to_unordered_map_iterate(__x...)
361 #define __profcxx_map_to_unordered_map_invalidate(__x...)
362 #define __profcxx_map_to_unordered_map_find(__x...)
363 #endif
365 // Run multithreaded unless instructed not to do so.
366 #ifndef _GLIBCXX_PROFILE_NO_THREADS
367 #define _GLIBCXX_PROFILE_THREADS
368 #endif
370 // Set default values for compile-time customizable variables.
371 #ifndef _GLIBCXX_PROFILE_TRACE_PATH_ROOT
372 #define _GLIBCXX_PROFILE_TRACE_PATH_ROOT "libstdcxx-profile"
373 #endif
374 #ifndef _GLIBCXX_PROFILE_TRACE_ENV_VAR
375 #define _GLIBCXX_PROFILE_TRACE_ENV_VAR "_GLIBCXX_PROFILE_TRACE_PATH_ROOT"
376 #endif
377 #ifndef _GLIBCXX_PROFILE_MAX_WARN_COUNT_ENV_VAR
378 #define _GLIBCXX_PROFILE_MAX_WARN_COUNT_ENV_VAR \
379 "_GLIBCXX_PROFILE_MAX_WARN_COUNT"
380 #endif
381 #ifndef _GLIBCXX_PROFILE_MAX_WARN_COUNT
382 #define _GLIBCXX_PROFILE_MAX_WARN_COUNT 10
383 #endif
384 #ifndef _GLIBCXX_PROFILE_MAX_STACK_DEPTH
385 #define _GLIBCXX_PROFILE_MAX_STACK_DEPTH 32
386 #endif
387 #ifndef _GLIBCXX_PROFILE_MAX_STACK_DEPTH_ENV_VAR
388 #define _GLIBCXX_PROFILE_MAX_STACK_DEPTH_ENV_VAR \
389 "_GLIBCXX_PROFILE_MAX_STACK_DEPTH"
390 #endif
391 #ifndef _GLIBCXX_PROFILE_MEM_PER_DIAGNOSTIC
392 #define _GLIBCXX_PROFILE_MEM_PER_DIAGNOSTIC 2 << 27
393 #endif
394 #ifndef _GLIBCXX_PROFILE_MEM_PER_DIAGNOSTIC_ENV_VAR
395 #define _GLIBCXX_PROFILE_MEM_PER_DIAGNOSTIC_ENV_VAR \
396 "_GLIBCXX_PROFILE_MEM_PER_DIAGNOSTIC"
397 #endif
399 // Instrumentation hook implementations.
400 #include "profile/impl/profiler_hash_func.h"
401 #include "profile/impl/profiler_hashtable_size.h"
402 #include "profile/impl/profiler_map_to_unordered_map.h"
403 #include "profile/impl/profiler_vector_size.h"
404 #include "profile/impl/profiler_vector_to_list.h"
405 #include "profile/impl/profiler_list_to_slist.h"
406 #include "profile/impl/profiler_list_to_vector.h"
408 #endif // _GLIBCXX_PROFILE_PROFILER_H