OSDN Git Service

2009-11-17 Benjamin Kosnik <bkoz@redhat.com>
[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 PROFCXX_PROFILER_H__
38 #define PROFCXX_PROFILER_H__ 1
39
40 #ifdef __GXX_EXPERIMENTAL_CXX0X__
41 #include <cstddef>
42 #else
43 #include <stddef.h>
44 #endif
45
46 namespace __gnu_profile
47 {
48   /** @brief Reentrance guard.
49    *
50    * Mechanism to protect all __gnu_profile operations against recursion,
51    * multithreaded and exception reentrance.
52    */
53   struct __reentrance_guard
54   {
55     static bool
56     _S_set_in()
57     {
58       if (_S_get_in())
59         return false;
60       else
61         {
62           _S_get_in() = true;
63           return true;
64         }
65     }
66
67     static bool&
68     _S_get_in()
69     {
70       static __thread bool _S_inside(false);
71       return _S_inside;
72     }
73
74     __reentrance_guard() { }
75     ~__reentrance_guard() { _S_get_in() = false; }
76   };
77
78 #define _GLIBCXX_PROFILE_REENTRANCE_GUARD(__x...)               \
79   {                                                             \
80     if (__gnu_profile::__reentrance_guard::_S_get_in())         \
81     {                                                           \
82       __gnu_profile::__reentrance_guard __get_out;              \
83       __x;                                                      \
84     }                                                           \
85   }
86
87
88   // Forward declarations of implementation functions.
89   // Don't use any __gnu_profile:: in user code.
90   // Instead, use the __profcxx... macros, which offer guarded access.
91   void __turn_on();
92   void __turn_off();
93   bool __is_invalid();
94   bool __is_on();
95   bool __is_off();
96   void __report(void);
97   void __trace_hashtable_size_resize(const void*, size_t, size_t);
98   void __trace_hashtable_size_destruct(const void*, size_t, size_t);
99   void __trace_hashtable_size_construct(const void*, size_t);
100   void __trace_vector_size_resize(const void*, size_t, size_t);
101   void __trace_vector_size_destruct(const void*, size_t, size_t);
102   void __trace_vector_size_construct(const void*, size_t);
103   void __trace_hash_func_destruct(const void*, size_t, size_t, size_t);
104   void __trace_hash_func_construct(const void*);
105   void __trace_vector_to_list_destruct(const void*);
106   void __trace_vector_to_list_construct(const void*);
107   void __trace_vector_to_list_insert(const void*, size_t, size_t);
108   void __trace_vector_to_list_iterate(const void*, size_t);
109   void __trace_vector_to_list_invalid_operator(const void*);
110   void __trace_vector_to_list_resize(const void*, size_t, size_t);
111   void __trace_map_to_unordered_map_construct(const void*);
112   void __trace_map_to_unordered_map_invalidate(const void*);
113   void __trace_map_to_unordered_map_insert(const void*, size_t, size_t);
114   void __trace_map_to_unordered_map_erase(const void*, size_t, size_t);
115   void __trace_map_to_unordered_map_iterate(const void*, size_t);
116   void __trace_map_to_unordered_map_find(const void*, size_t);
117   void __trace_map_to_unordered_map_destruct(const void*);
118 } // namespace __gnu_profile
119
120 // Master switch turns on all diagnostics.
121 #ifdef _GLIBCXX_PROFILE
122 #define _GLIBCXX_PROFILE_HASHTABLE_TOO_SMALL
123 #define _GLIBCXX_PROFILE_HASHTABLE_TOO_LARGE
124 #define _GLIBCXX_PROFILE_VECTOR_TOO_SMALL
125 #define _GLIBCXX_PROFILE_VECTOR_TOO_LARGE
126 #define _GLIBCXX_PROFILE_INEFFICIENT_HASH
127 #define _GLIBCXX_PROFILE_VECTOR_TO_LIST
128 #define _GLIBCXX_PROFILE_MAP_TO_UNORDERED_MAP
129 #endif
130
131 // Expose global management routines to user code.
132 #ifdef _GLIBCXX_PROFILE
133 #define __profcxx_report() \
134   _GLIBCXX_PROFILE_REENTRANCE_GUARD(__gnu_profile::__report())
135 #define __profcxx_turn_on() \
136   _GLIBCXX_PROFILE_REENTRANCE_GUARD(__gnu_profile::__turn_on())
137 #define __profcxx_turn_off() \
138   _GLIBCXX_PROFILE_REENTRANCE_GUARD(__gnu_profile::__turn_off())
139 #define __profcxx_is_invalid() \
140   _GLIBCXX_PROFILE_REENTRANCE_GUARD(__gnu_profile::__is_invalid())
141 #define __profcxx_is_on() \
142   _GLIBCXX_PROFILE_REENTRANCE_GUARD(__gnu_profile::__is_on())
143 #define __profcxx__is_off() \
144   _GLIBCXX_PROFILE_REENTRANCE_GUARD(__gnu_profile::__is_off())
145 #else
146 #define __profcxx_report()
147 #define __profcxx_turn_on()
148 #define __profcxx_turn_off()
149 #define __profcxx_is_invalid()
150 #define __profcxx_is_on()
151 #define __profcxx_is_off()
152 #endif
153
154 // Turn on/off instrumentation for HASHTABLE_TOO_SMALL and HASHTABLE_TOO_LARGE.
155 #if ((defined(_GLIBCXX_PROFILE_HASHTABLE_TOO_SMALL) \
156       && !defined(_NO_GLIBCXX_PROFILE_HASHTABLE_TOO_SMALL)) \
157      || (defined(_GLIBCXX_PROFILE_HASHTABLE_TOO_LARGE) \
158          && !defined(_NO_GLIBCXX_PROFILE_HASHTABLE_TOO_LARGE)))
159 #define __profcxx_hashtable_resize(__x...) \
160   _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
161       __gnu_profile::__trace_hashtable_size_resize(__x))
162 #define __profcxx_hashtable_destruct(__x...) \
163   _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
164       __gnu_profile::__trace_hashtable_size_destruct(__x))
165 #define __profcxx_hashtable_construct(__x...) \
166   _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
167       __gnu_profile::__trace_hashtable_size_construct(__x))
168 #else
169 #define __profcxx_hashtable_resize(__x...)
170 #define __profcxx_hashtable_destruct(__x...)
171 #define __profcxx_hashtable_construct(__x...)
172 #endif
173
174 // Turn on/off instrumentation for VECTOR_TOO_SMALL and VECTOR_TOO_LARGE.
175 #if ((defined(_GLIBCXX_PROFILE_VECTOR_TOO_SMALL) \
176       && !defined(_NO_GLIBCXX_PROFILE_VECTOR_TOO_SMALL)) \
177      || (defined(_GLIBCXX_PROFILE_VECTOR_TOO_LARGE) \
178          && !defined(_NO_GLIBCXX_PROFILE_VECTOR_TOO_LARGE)))
179 #define __profcxx_vector_resize(__x...) \
180   _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
181       __gnu_profile::__trace_vector_size_resize(__x))
182 #define __profcxx_vector_destruct(__x...) \
183   _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
184       __gnu_profile::__trace_vector_size_destruct(__x))
185 #define __profcxx_vector_construct(__x...) \
186   _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
187       __gnu_profile::__trace_vector_size_construct(__x))
188 #else
189 #define __profcxx_vector_resize(__x...)
190 #define __profcxx_vector_destruct(__x...)
191 #define __profcxx_vector_construct(__x...)
192 #endif
193
194 // Turn on/off instrumentation for INEFFICIENT_HASH.
195 #if (defined(_GLIBCXX_PROFILE_INEFFICIENT_HASH) \
196      && !defined(_NO_GLIBCXX_PROFILE_INEFFICIENT_HASH))
197 #define __profcxx_hashtable_construct2(__x...) \
198   _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
199       __gnu_profile::__trace_hash_func_construct(__x))
200 #define __profcxx_hashtable_destruct2(__x...) \
201   _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
202       __gnu_profile::__trace_hash_func_destruct(__x))
203 #else
204 #define __profcxx_hashtable_destruct2(__x...)
205 #define __profcxx_hashtable_construct2(__x...)
206 #endif
207
208 // Turn on/off instrumentation for VECTOR_TO_LIST.
209 #if (defined(_GLIBCXX_PROFILE_VECTOR_TO_LIST) \
210      && !defined(_NO_GLIBCXX_PROFILE_VECTOR_TO_LIST))
211 #define __profcxx_vector_construct2(__x...) \
212   _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
213       __gnu_profile::__trace_vector_to_list_construct(__x))
214 #define __profcxx_vector_destruct2(__x...) \
215   _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
216       __gnu_profile::__trace_vector_to_list_destruct(__x))
217 #define __profcxx_vector_insert(__x...) \
218   _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
219       __gnu_profile::__trace_vector_to_list_insert(__x))
220 #define __profcxx_vector_iterate(__x...) \
221   _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
222       __gnu_profile::__trace_vector_to_list_iterate(__x))
223 #define __profcxx_vector_invalid_operator(__x...) \
224   _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
225       __gnu_profile::__trace_vector_to_list_invalid_operator(__x))
226 #define __profcxx_vector_resize2(__x...) \
227   _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
228       __gnu_profile::__trace_vector_to_list_resize(__x))
229 #else
230 #define __profcxx_vector_destruct2(__x...)
231 #define __profcxx_vector_construct2(__x...)
232 #define __profcxx_vector_insert(__x...)
233 #define __profcxx_vector_iterate(__x...)
234 #define __profcxx_vector_invalid_operator(__x...)
235 #define __profcxx_vector_resize2(__x...)
236 #endif
237
238 // Turn on/off instrumentation for MAP_TO_UNORDERED_MAP.
239 #if (defined(_GLIBCXX_PROFILE_MAP_TO_UNORDERED_MAP) \
240      && !defined(_NO_GLIBCXX_PROFILE_MAP_TO_UNORDERED_MAP))
241 #define __profcxx_map_to_unordered_map_construct(__x...) \
242   _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
243       __gnu_profile::__trace_map_to_unordered_map_construct(__x))
244 #define __profcxx_map_to_unordered_map_destruct(__x...) \
245   _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
246       __gnu_profile::__trace_map_to_unordered_map_destruct(__x))
247 #define __profcxx_map_to_unordered_map_insert(__x...) \
248   _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
249       __gnu_profile::__trace_map_to_unordered_map_insert(__x))
250 #define __profcxx_map_to_unordered_map_erase(__x...) \
251   _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
252       __gnu_profile::__trace_map_to_unordered_map_erase(__x))
253 #define __profcxx_map_to_unordered_map_iterate(__x...) \
254   _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
255       __gnu_profile::__trace_map_to_unordered_map_iterate(__x))
256 #define __profcxx_map_to_unordered_map_invalidate(__x...) \
257   _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
258       __gnu_profile::__trace_map_to_unordered_map_invalidate(__x))
259 #define __profcxx_map_to_unordered_map_find(__x...) \
260   _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
261       __gnu_profile::__trace_map_to_unordered_map_find(__x))
262 #else
263 #define __profcxx_map_to_unordered_map_construct(__x...) \
264
265 #define __profcxx_map_to_unordered_map_destruct(__x...)
266 #define __profcxx_map_to_unordered_map_insert(__x...)
267 #define __profcxx_map_to_unordered_map_erase(__x...)
268 #define __profcxx_map_to_unordered_map_iterate(__x...)
269 #define __profcxx_map_to_unordered_map_invalidate(__x...)
270 #define __profcxx_map_to_unordered_map_find(__x...)
271 #endif
272
273 // Run multithreaded unless instructed not to do so.
274 #ifndef _GLIBCXX_PROFILE_NOTHREADS
275 #define _GLIBCXX_PROFILE_THREADS
276 #endif
277
278 // Set default values for compile-time customizable variables.
279 #ifndef _GLIBCXX_PROFILE_TRACE_PATH_ROOT
280 #define _GLIBCXX_PROFILE_TRACE_PATH_ROOT "libstdcxx-profile"
281 #endif
282 #ifndef _GLIBCXX_PROFILE_TRACE_ENV_VAR
283 #define _GLIBCXX_PROFILE_TRACE_ENV_VAR "GLIBCXX_PROFILE_TRACE_PATH_ROOT"
284 #endif
285 #ifndef _GLIBCXX_PROFILE_MAX_WARN_COUNT_ENV_VAR
286 #define _GLIBCXX_PROFILE_MAX_WARN_COUNT_ENV_VAR \
287   "GLIBCXX_PROFILE_MAX_WARN_COUNT"
288 #endif
289 #ifndef _GLIBCXX_PROFILE_MAX_WARN_COUNT
290 #define _GLIBCXX_PROFILE_MAX_WARN_COUNT 10
291 #endif
292 #ifndef _GLIBCXX_PROFILE_MAX_STACK_DEPTH
293 #define _GLIBCXX_PROFILE_MAX_STACK_DEPTH 32
294 #endif
295 #ifndef _GLIBCXX_PROFILE_MAX_STACK_DEPTH_ENV_VAR
296 #define _GLIBCXX_PROFILE_MAX_STACK_DEPTH_ENV_VAR \
297   "GLIBCXX_PROFILE_MAX_STACK_DEPTH"
298 #endif
299 #ifndef _GLIBCXX_PROFILE_MEM_PER_DIAGNOSTIC
300 #define _GLIBCXX_PROFILE_MEM_PER_DIAGNOSTIC 2 << 27
301 #endif
302 #ifndef _GLIBCXX_PROFILE_MEM_PER_DIAGNOSTIC_ENV_VAR
303 #define _GLIBCXX_PROFILE_MEM_PER_DIAGNOSTIC_ENV_VAR \
304   "GLIBCXX_PROFILE_MEM_PER_DIAGNOSTIC"
305 #endif
306
307 // Instrumentation hook implementations.
308 #include "profile/impl/profiler_hash_func.h"
309 #include "profile/impl/profiler_hashtable_size.h"
310 #include "profile/impl/profiler_map_to_unordered_map.h"
311 #include "profile/impl/profiler_vector_size.h"
312 #include "profile/impl/profiler_vector_to_list.h"
313
314 #endif // PROFCXX_PROFILER_H__