OSDN Git Service

libstdc++ profile mode bug fixes and minor updates
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / profile / impl / profiler.h
1 // -*- C++ -*-
2 //
3 // Copyright (C) 2009 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.
10
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.
15
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.
20
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.
30
31 /** @file profile/impl/profiler.h
32  *  @brief Interface of the profiling runtime library.
33  */
34
35 // Written by Lixia Liu and Silvius Rus.
36
37 #ifndef _GLIBCXX_PROFILE_PROFILER_H
38 #define _GLIBCXX_PROFILE_PROFILER_H 1
39
40 #ifdef __GXX_EXPERIMENTAL_CXX0X__
41 #include <cstddef>
42 #else
43 #include <stddef.h>
44 #endif
45
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;                                                       \
51   }
52 #define _GLIBCXX_PROFILE_DATA(__name) \
53   __get_##__name()
54
55 /**
56  * @namespace std::__cxxprof_guard
57  * @brief Mechanism to protect all __gnu_profile operations against
58  * multithreaded and exception reentrance.
59  */
60 namespace __gnu_profile
61 {
62   /** @brief Reentrance guard.
63    *
64    * Mechanism to protect all __gnu_profile operations against recursion,
65    * multithreaded and exception reentrance.
66    */
67   struct __reentrance_guard
68   {
69     static bool
70     __get_in()
71     {
72       if (__inside() == true)
73         return false;
74       else
75         {
76           __inside() = true;
77           return true;
78         }
79     }
80
81     static bool&
82     __inside()
83     {
84       static __thread bool _S_inside(false);
85       return _S_inside;
86     }
87
88     __reentrance_guard() { }
89     ~__reentrance_guard() { __inside() = false; }
90   };
91
92 #define _GLIBCXX_PROFILE_REENTRANCE_GUARD(__x...)               \
93   {                                                             \
94     if (__gnu_profile::__reentrance_guard::__get_in())          \
95     {                                                           \
96       __gnu_profile::__reentrance_guard __get_out;              \
97       __x;                                                      \
98     }                                                           \
99   }
100 }
101
102 /**
103  * @namespace std::__gnu_profile
104  * @brief Implementation of profile extension.
105  */
106 namespace __gnu_profile
107 {
108 // Forward declarations of implementation functions.
109 // Don't use any __gnu_profile:: in user code.
110 // Instead, use the __profcxx... macros, which offer guarded access.
111 bool __turn_on();
112 bool __turn_off();
113 bool __is_invalid();
114 bool __is_on();
115 bool __is_off();
116 void __report(void);
117 void __trace_hashtable_size_resize(const void*, size_t, size_t);
118 void __trace_hashtable_size_destruct(const void*, size_t, size_t);
119 void __trace_hashtable_size_construct(const void*, size_t);
120 void __trace_vector_size_resize(const void*, size_t, size_t);
121 void __trace_vector_size_destruct(const void*, size_t, size_t);
122 void __trace_vector_size_construct(const void*, size_t);
123 void __trace_hash_func_destruct(const void*, size_t, size_t, size_t);
124 void __trace_hash_func_construct(const void*);
125 void __trace_vector_to_list_destruct(const void*);
126 void __trace_vector_to_list_construct(const void*);
127 void __trace_vector_to_list_insert(const void*, size_t, size_t);
128 void __trace_vector_to_list_iterate(const void*, size_t);
129 void __trace_vector_to_list_invalid_operator(const void*);
130 void __trace_vector_to_list_resize(const void*, size_t, size_t);
131 void __trace_vector_to_list_find(const void*, size_t);
132
133 void __trace_list_to_slist_destruct(const void*);
134 void __trace_list_to_slist_construct(const void*);
135 void __trace_list_to_slist_rewind(const void*); 
136 void __trace_list_to_slist_operation(const void*);
137
138 void __trace_list_to_vector_destruct(const void*);
139 void __trace_list_to_vector_construct(const void*);
140 void __trace_list_to_vector_insert(const void*, size_t, size_t); 
141 void __trace_list_to_vector_iterate(const void*, size_t);
142 void __trace_list_to_vector_invalid_operator(const void*);
143 void __trace_list_to_vector_resize(const void*, size_t, size_t); 
144
145 void __trace_list_to_set_destruct(const void*);
146 void __trace_list_to_set_construct(const void*);
147 void __trace_list_to_set_insert(const void*, size_t, size_t); 
148 void __trace_list_to_set_iterate(const void*, size_t);
149 void __trace_list_to_set_invalid_operator(const void*);
150 void __trace_list_to_set_find(const void*, size_t); 
151
152 void __trace_map_to_unordered_map_construct(const void*);
153 void __trace_map_to_unordered_map_invalidate(const void*);
154 void __trace_map_to_unordered_map_insert(const void*, size_t, size_t);
155 void __trace_map_to_unordered_map_erase(const void*, size_t, size_t);
156 void __trace_map_to_unordered_map_iterate(const void*, size_t);
157 void __trace_map_to_unordered_map_find(const void*, size_t);
158 void __trace_map_to_unordered_map_destruct(const void*);
159 } // namespace __gnu_profile
160
161 // Master switch turns on all diagnostics that are not explicitly turned off.
162 #ifdef _GLIBCXX_PROFILE
163 #ifndef _GLIBCXX_PROFILE_NO_HASHTABLE_TOO_SMALL
164 #define _GLIBCXX_PROFILE_HASHTABLE_TOO_SMALL
165 #endif
166 #ifndef _GLIBCXX_PROFILE_NO_HASHTABLE_TOO_LARGE
167 #define _GLIBCXX_PROFILE_HASHTABLE_TOO_LARGE
168 #endif
169 #ifndef _GLIBCXX_PROFILE_NO_VECTOR_TOO_SMALL
170 #define _GLIBCXX_PROFILE_VECTOR_TOO_SMALL
171 #endif
172 #ifndef _GLIBCXX_PROFILE_NO_VECTOR_TOO_LARGE
173 #define _GLIBCXX_PROFILE_VECTOR_TOO_LARGE
174 #endif
175 #ifndef _GLIBCXX_PROFILE_NO_INEFFICIENT_HASH
176 #define _GLIBCXX_PROFILE_INEFFICIENT_HASH
177 #endif
178 #ifndef _GLIBCXX_PROFILE_NO_VECTOR_TO_LIST
179 #define _GLIBCXX_PROFILE_VECTOR_TO_LIST
180 #endif
181 #ifndef _GLIBCXX_PROFILE_NO_LIST_TO_SLIST
182 #define _GLIBCXX_PROFILE_LIST_TO_SLIST
183 #endif
184 #ifndef _GLIBCXX_PROFILE_NO_LIST_TO_VECTOR
185 #define _GLIBCXX_PROFILE_LIST_TO_VECTOR
186 #endif
187 #ifndef _GLIBCXX_PROFILE_NO_MAP_TO_UNORDERED_MAP
188 #define _GLIBCXX_PROFILE_MAP_TO_UNORDERED_MAP
189 #endif
190 #endif
191
192 // Expose global management routines to user code.
193 #ifdef _GLIBCXX_PROFILE
194 #define __profcxx_report() \
195   _GLIBCXX_PROFILE_REENTRANCE_GUARD(__gnu_profile::__report())
196 #define __profcxx_turn_on() \
197   _GLIBCXX_PROFILE_REENTRANCE_GUARD(__gnu_profile::__turn_on())
198 #define __profcxx_turn_off() \
199   _GLIBCXX_PROFILE_REENTRANCE_GUARD(__gnu_profile::__turn_off())
200 #define __profcxx_is_invalid() \
201   _GLIBCXX_PROFILE_REENTRANCE_GUARD(__gnu_profile::__is_invalid())
202 #define __profcxx_is_on() \
203   _GLIBCXX_PROFILE_REENTRANCE_GUARD(__gnu_profile::__is_on())
204 #define __profcxx__is_off() \
205   _GLIBCXX_PROFILE_REENTRANCE_GUARD(__gnu_profile::__is_off())
206 #else
207 #define __profcxx_report()
208 #define __profcxx_turn_on()
209 #define __profcxx_turn_off()
210 #define __profcxx_is_invalid()
211 #define __profcxx_is_on()
212 #define __profcxx_is_off()
213 #endif
214
215 // Turn on/off instrumentation for HASHTABLE_TOO_SMALL and HASHTABLE_TOO_LARGE.
216 #if (defined(_GLIBCXX_PROFILE_HASHTABLE_TOO_SMALL) \
217      || defined(_GLIBCXX_PROFILE_HASHTABLE_TOO_LARGE))
218 #define __profcxx_hashtable_resize(__x...) \
219   _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
220       __gnu_profile::__trace_hashtable_size_resize(__x))
221 #define __profcxx_hashtable_destruct(__x...) \
222   _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
223       __gnu_profile::__trace_hashtable_size_destruct(__x))
224 #define __profcxx_hashtable_construct(__x...) \
225   _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
226       __gnu_profile::__trace_hashtable_size_construct(__x))
227 #else
228 #define __profcxx_hashtable_resize(__x...)  
229 #define __profcxx_hashtable_destruct(__x...) 
230 #define __profcxx_hashtable_construct(__x...)  
231 #endif
232
233 // Turn on/off instrumentation for VECTOR_TOO_SMALL and VECTOR_TOO_LARGE.
234 #if (defined(_GLIBCXX_PROFILE_VECTOR_TOO_SMALL) \
235      || defined(_GLIBCXX_PROFILE_VECTOR_TOO_LARGE))
236 #define __profcxx_vector_resize(__x...) \
237   _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
238       __gnu_profile::__trace_vector_size_resize(__x))
239 #define __profcxx_vector_destruct(__x...) \
240   _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
241       __gnu_profile::__trace_vector_size_destruct(__x))
242 #define __profcxx_vector_construct(__x...) \
243   _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
244       __gnu_profile::__trace_vector_size_construct(__x))
245 #else
246 #define __profcxx_vector_resize(__x...)  
247 #define __profcxx_vector_destruct(__x...) 
248 #define __profcxx_vector_construct(__x...)  
249 #endif 
250
251 // Turn on/off instrumentation for INEFFICIENT_HASH.
252 #if defined(_GLIBCXX_PROFILE_INEFFICIENT_HASH)
253 #define __profcxx_hashtable_construct2(__x...) \
254   _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
255       __gnu_profile::__trace_hash_func_construct(__x))
256 #define __profcxx_hashtable_destruct2(__x...) \
257   _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
258       __gnu_profile::__trace_hash_func_destruct(__x))
259 #else
260 #define __profcxx_hashtable_destruct2(__x...) 
261 #define __profcxx_hashtable_construct2(__x...)  
262 #endif
263
264 // Turn on/off instrumentation for VECTOR_TO_LIST.
265 #if defined(_GLIBCXX_PROFILE_VECTOR_TO_LIST)
266 #define __profcxx_vector_construct2(__x...) \
267   _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
268       __gnu_profile::__trace_vector_to_list_construct(__x))
269 #define __profcxx_vector_destruct2(__x...) \
270   _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
271       __gnu_profile::__trace_vector_to_list_destruct(__x))
272 #define __profcxx_vector_insert(__x...) \
273   _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
274       __gnu_profile::__trace_vector_to_list_insert(__x))
275 #define __profcxx_vector_iterate(__x...) \
276   _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
277       __gnu_profile::__trace_vector_to_list_iterate(__x))
278 #define __profcxx_vector_invalid_operator(__x...) \
279   _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
280       __gnu_profile::__trace_vector_to_list_invalid_operator(__x))
281 #define __profcxx_vector_resize2(__x...) \
282   _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
283       __gnu_profile::__trace_vector_to_list_resize(__x))
284 #define __profcxx_vector_find(__x...) \
285   _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
286       __gnu_profile::__trace_vector_to_list_find(__x))
287 #else
288 #define __profcxx_vector_destruct2(__x...)
289 #define __profcxx_vector_construct2(__x...)
290 #define __profcxx_vector_insert(__x...)
291 #define __profcxx_vector_iterate(__x...)
292 #define __profcxx_vector_invalid_operator(__x...)
293 #define __profcxx_vector_resize2(__x...)
294 #define __profcxx_vector_find(__x...)
295 #endif
296
297 // Turn on/off instrumentation for LIST_TO_VECTOR. 
298 #if defined(_GLIBCXX_PROFILE_LIST_TO_VECTOR)
299 #define __profcxx_list_construct2(__x...) \
300   _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
301       __gnu_profile::__trace_list_to_vector_construct(__x))
302 #define __profcxx_list_destruct2(__x...) \
303   _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
304       __gnu_profile::__trace_list_to_vector_destruct(__x))
305 #define __profcxx_list_insert(__x...) \
306   _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
307       __gnu_profile::__trace_list_to_vector_insert(__x))
308 #define __profcxx_list_iterate(__x...) \
309   _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
310       __gnu_profile::__trace_list_to_vector_iterate(__x))
311 #define __profcxx_list_invalid_operator(__x...) \
312   _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
313       __gnu_profile::__trace_list_to_vector_invalid_operator(__x))
314 #else
315 #define __profcxx_list_destruct2(__x...)
316 #define __profcxx_list_construct2(__x...)
317 #define __profcxx_list_insert(__x...)
318 #define __profcxx_list_iterate(__x...)
319 #define __profcxx_list_invalid_operator(__x...)
320 #endif
321
322 // Turn on/off instrumentation for LIST_TO_SLIST.  
323 #if defined(_GLIBCXX_PROFILE_LIST_TO_SLIST)
324 #define __profcxx_list_rewind(__x...) \
325   _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
326       __gnu_profile::__trace_list_to_slist_rewind(__x))
327 #define __profcxx_list_operation(__x...) \
328   _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
329       __gnu_profile::__trace_list_to_slist_operation(__x))
330 #define __profcxx_list_destruct(__x...) \
331   _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
332       __gnu_profile::__trace_list_to_slist_destruct(__x))
333 #define __profcxx_list_construct(__x...) \
334   _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
335       __gnu_profile::__trace_list_to_slist_construct(__x))
336 #else
337 #define __profcxx_list_rewind(__x...)  
338 #define __profcxx_list_operation(__x...)
339 #define __profcxx_list_destruct(__x...) 
340 #define __profcxx_list_construct(__x...)  
341 #endif 
342
343 // Turn on/off instrumentation for MAP_TO_UNORDERED_MAP.
344 #if defined(_GLIBCXX_PROFILE_MAP_TO_UNORDERED_MAP)
345 #define __profcxx_map_to_unordered_map_construct(__x...) \
346   _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
347       __gnu_profile::__trace_map_to_unordered_map_construct(__x))
348 #define __profcxx_map_to_unordered_map_destruct(__x...) \
349   _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
350       __gnu_profile::__trace_map_to_unordered_map_destruct(__x))
351 #define __profcxx_map_to_unordered_map_insert(__x...) \
352   _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
353       __gnu_profile::__trace_map_to_unordered_map_insert(__x))
354 #define __profcxx_map_to_unordered_map_erase(__x...) \
355   _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
356       __gnu_profile::__trace_map_to_unordered_map_erase(__x))
357 #define __profcxx_map_to_unordered_map_iterate(__x...) \
358   _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
359       __gnu_profile::__trace_map_to_unordered_map_iterate(__x))
360 #define __profcxx_map_to_unordered_map_invalidate(__x...) \
361   _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
362       __gnu_profile::__trace_map_to_unordered_map_invalidate(__x))
363 #define __profcxx_map_to_unordered_map_find(__x...) \
364   _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
365       __gnu_profile::__trace_map_to_unordered_map_find(__x))
366 #else
367 #define __profcxx_map_to_unordered_map_construct(__x...) \
368   
369 #define __profcxx_map_to_unordered_map_destruct(__x...)
370 #define __profcxx_map_to_unordered_map_insert(__x...)
371 #define __profcxx_map_to_unordered_map_erase(__x...)
372 #define __profcxx_map_to_unordered_map_iterate(__x...)
373 #define __profcxx_map_to_unordered_map_invalidate(__x...)
374 #define __profcxx_map_to_unordered_map_find(__x...)
375 #endif
376
377 // Run multithreaded unless instructed not to do so.
378 #ifndef _GLIBCXX_PROFILE_NO_THREADS
379 #define _GLIBCXX_PROFILE_THREADS
380 #endif
381
382 // Set default values for compile-time customizable variables.
383 #ifndef _GLIBCXX_PROFILE_TRACE_PATH_ROOT
384 #define _GLIBCXX_PROFILE_TRACE_PATH_ROOT "libstdcxx-profile"
385 #endif
386 #ifndef _GLIBCXX_PROFILE_TRACE_ENV_VAR
387 #define _GLIBCXX_PROFILE_TRACE_ENV_VAR "_GLIBCXX_PROFILE_TRACE_PATH_ROOT"
388 #endif
389 #ifndef _GLIBCXX_PROFILE_MAX_WARN_COUNT_ENV_VAR
390 #define _GLIBCXX_PROFILE_MAX_WARN_COUNT_ENV_VAR \
391   "_GLIBCXX_PROFILE_MAX_WARN_COUNT"
392 #endif
393 #ifndef _GLIBCXX_PROFILE_MAX_WARN_COUNT
394 #define _GLIBCXX_PROFILE_MAX_WARN_COUNT 10
395 #endif
396 #ifndef _GLIBCXX_PROFILE_MAX_STACK_DEPTH
397 #define _GLIBCXX_PROFILE_MAX_STACK_DEPTH 32
398 #endif
399 #ifndef _GLIBCXX_PROFILE_MAX_STACK_DEPTH_ENV_VAR
400 #define _GLIBCXX_PROFILE_MAX_STACK_DEPTH_ENV_VAR \
401   "_GLIBCXX_PROFILE_MAX_STACK_DEPTH"
402 #endif
403 #ifndef _GLIBCXX_PROFILE_MEM_PER_DIAGNOSTIC
404 #define _GLIBCXX_PROFILE_MEM_PER_DIAGNOSTIC 2 << 27
405 #endif
406 #ifndef _GLIBCXX_PROFILE_MEM_PER_DIAGNOSTIC_ENV_VAR
407 #define _GLIBCXX_PROFILE_MEM_PER_DIAGNOSTIC_ENV_VAR \
408   "_GLIBCXX_PROFILE_MEM_PER_DIAGNOSTIC"
409 #endif
410
411 // Instrumentation hook implementations.
412 #include "profile/impl/profiler_hash_func.h"
413 #include "profile/impl/profiler_hashtable_size.h"
414 #include "profile/impl/profiler_map_to_unordered_map.h"
415 #include "profile/impl/profiler_vector_size.h"
416 #include "profile/impl/profiler_vector_to_list.h"
417 #include "profile/impl/profiler_list_to_slist.h"
418 #include "profile/impl/profiler_list_to_vector.h"
419
420 #endif // _GLIBCXX_PROFILE_PROFILER_H