libstdc++
profiler.h
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Copyright (C) 2009, 2010 Free Software Foundation, Inc.
00004 //
00005 // This file is part of the GNU ISO C++ Library.  This library is free
00006 // software; you can redistribute it and/or modify it under the
00007 // terms of the GNU General Public License as published by the
00008 // Free Software Foundation; either version 3, or (at your option)
00009 // any later version.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 
00016 // Under Section 7 of GPL version 3, you are granted additional
00017 // permissions described in the GCC Runtime Library Exception, version
00018 // 3.1, as published by the Free Software Foundation.
00019 
00020 // You should have received a copy of the GNU General Public License along
00021 // with this library; see the file COPYING3.  If not see
00022 // <http://www.gnu.org/licenses/>.
00023 
00024 /** @file profile/impl/profiler.h
00025  *  @brief Interface of the profiling runtime library.
00026  */
00027 
00028 // Written by Lixia Liu and Silvius Rus.
00029 
00030 #ifndef _GLIBCXX_PROFILE_PROFILER_H
00031 #define _GLIBCXX_PROFILE_PROFILER_H 1
00032 
00033 #include <bits/c++config.h>
00034 
00035 // Mechanism to define data with inline linkage.
00036 #define _GLIBCXX_PROFILE_DEFINE_UNINIT_DATA(__type, __name)             \
00037   inline __type&                                                        \
00038   __get_##__name()                                                      \
00039   {                                                                     \
00040     static __type __name;                                               \
00041     return __name;                                                      \
00042   }
00043 #define _GLIBCXX_PROFILE_DEFINE_DATA(__type, __name, __initial_value...) \
00044   inline __type& __get_##__name() {                                      \
00045     static __type __name(__initial_value);                               \
00046     return __name;                                                       \
00047   }
00048 #define _GLIBCXX_PROFILE_DATA(__name) \
00049   __get_##__name()
00050 
00051 namespace __gnu_profile
00052 {
00053   /** @brief Reentrance guard.
00054    *
00055    * Mechanism to protect all __gnu_profile operations against recursion,
00056    * multithreaded and exception reentrance.
00057    */
00058   struct __reentrance_guard
00059   {
00060     static bool
00061     __get_in()
00062     {
00063       if (__inside() == true)
00064     return false;
00065       else
00066     {
00067       __inside() = true;
00068       return true;
00069     }
00070     }
00071 
00072     static bool&
00073     __inside()
00074     {
00075       static __thread bool _S_inside(false);
00076       return _S_inside;
00077     }
00078 
00079     __reentrance_guard() { }
00080     ~__reentrance_guard() { __inside() = false; }
00081   };
00082 
00083 #define _GLIBCXX_PROFILE_REENTRANCE_GUARD(__x...)           \
00084   {                                                             \
00085     if (__gnu_profile::__reentrance_guard::__get_in())          \
00086     {                                                           \
00087       __gnu_profile::__reentrance_guard __get_out;      \
00088       __x;                                                      \
00089     }                                                           \
00090   }
00091 
00092   // Forward declarations of implementation functions.
00093   // Don't use any __gnu_profile:: in user code.
00094   // Instead, use the __profcxx... macros, which offer guarded access.
00095   bool __turn_on();
00096   bool __turn_off();
00097   bool __is_invalid();
00098   bool __is_on();
00099   bool __is_off();
00100   void __report(void);
00101   void __trace_hashtable_size_resize(const void*, std::size_t, std::size_t);
00102   void __trace_hashtable_size_destruct(const void*, std::size_t, std::size_t);
00103   void __trace_hashtable_size_construct(const void*, std::size_t);
00104   void __trace_vector_size_resize(const void*, std::size_t, std::size_t);
00105   void __trace_vector_size_destruct(const void*, std::size_t, std::size_t);
00106   void __trace_vector_size_construct(const void*, std::size_t);
00107   void __trace_hash_func_destruct(const void*, std::size_t, std::size_t,
00108                   std::size_t);
00109   void __trace_hash_func_construct(const void*);
00110   void __trace_vector_to_list_destruct(const void*);
00111   void __trace_vector_to_list_construct(const void*);
00112   void __trace_vector_to_list_insert(const void*, std::size_t, std::size_t);
00113   void __trace_vector_to_list_iterate(const void*, std::size_t);
00114   void __trace_vector_to_list_invalid_operator(const void*);
00115   void __trace_vector_to_list_resize(const void*, std::size_t, std::size_t);
00116   void __trace_vector_to_list_find(const void*, std::size_t);
00117 
00118   void __trace_list_to_slist_destruct(const void*);
00119   void __trace_list_to_slist_construct(const void*);
00120   void __trace_list_to_slist_rewind(const void*);
00121   void __trace_list_to_slist_operation(const void*);
00122 
00123   void __trace_list_to_vector_destruct(const void*);
00124   void __trace_list_to_vector_construct(const void*);
00125   void __trace_list_to_vector_insert(const void*, std::size_t, std::size_t);
00126   void __trace_list_to_vector_iterate(const void*, std::size_t);
00127   void __trace_list_to_vector_invalid_operator(const void*);
00128   void __trace_list_to_vector_resize(const void*, std::size_t, std::size_t);
00129 
00130   void __trace_list_to_set_destruct(const void*);
00131   void __trace_list_to_set_construct(const void*);
00132   void __trace_list_to_set_insert(const void*, std::size_t, std::size_t); 
00133   void __trace_list_to_set_iterate(const void*, std::size_t);
00134   void __trace_list_to_set_invalid_operator(const void*);
00135   void __trace_list_to_set_find(const void*, std::size_t); 
00136 
00137   void __trace_map_to_unordered_map_construct(const void*);
00138   void __trace_map_to_unordered_map_invalidate(const void*);
00139   void __trace_map_to_unordered_map_insert(const void*, std::size_t,
00140                        std::size_t);
00141   void __trace_map_to_unordered_map_erase(const void*, std::size_t,
00142                       std::size_t);
00143   void __trace_map_to_unordered_map_iterate(const void*, std::size_t);
00144   void __trace_map_to_unordered_map_find(const void*, std::size_t);
00145   void __trace_map_to_unordered_map_destruct(const void*);
00146 } // namespace __gnu_profile
00147 
00148 // Master switch turns on all diagnostics that are not explicitly turned off.
00149 #ifdef _GLIBCXX_PROFILE
00150 #ifndef _GLIBCXX_PROFILE_NO_HASHTABLE_TOO_SMALL
00151 #define _GLIBCXX_PROFILE_HASHTABLE_TOO_SMALL
00152 #endif
00153 #ifndef _GLIBCXX_PROFILE_NO_HASHTABLE_TOO_LARGE
00154 #define _GLIBCXX_PROFILE_HASHTABLE_TOO_LARGE
00155 #endif
00156 #ifndef _GLIBCXX_PROFILE_NO_VECTOR_TOO_SMALL
00157 #define _GLIBCXX_PROFILE_VECTOR_TOO_SMALL
00158 #endif
00159 #ifndef _GLIBCXX_PROFILE_NO_VECTOR_TOO_LARGE
00160 #define _GLIBCXX_PROFILE_VECTOR_TOO_LARGE
00161 #endif
00162 #ifndef _GLIBCXX_PROFILE_NO_INEFFICIENT_HASH
00163 #define _GLIBCXX_PROFILE_INEFFICIENT_HASH
00164 #endif
00165 #ifndef _GLIBCXX_PROFILE_NO_VECTOR_TO_LIST
00166 #define _GLIBCXX_PROFILE_VECTOR_TO_LIST
00167 #endif
00168 #ifndef _GLIBCXX_PROFILE_NO_LIST_TO_SLIST
00169 #define _GLIBCXX_PROFILE_LIST_TO_SLIST
00170 #endif
00171 #ifndef _GLIBCXX_PROFILE_NO_LIST_TO_VECTOR
00172 #define _GLIBCXX_PROFILE_LIST_TO_VECTOR
00173 #endif
00174 #ifndef _GLIBCXX_PROFILE_NO_MAP_TO_UNORDERED_MAP
00175 #define _GLIBCXX_PROFILE_MAP_TO_UNORDERED_MAP
00176 #endif
00177 #endif
00178 
00179 // Expose global management routines to user code.
00180 #ifdef _GLIBCXX_PROFILE
00181 #define __profcxx_report() \
00182   _GLIBCXX_PROFILE_REENTRANCE_GUARD(__gnu_profile::__report())
00183 #define __profcxx_turn_on() \
00184   _GLIBCXX_PROFILE_REENTRANCE_GUARD(__gnu_profile::__turn_on())
00185 #define __profcxx_turn_off() \
00186   _GLIBCXX_PROFILE_REENTRANCE_GUARD(__gnu_profile::__turn_off())
00187 #define __profcxx_is_invalid() \
00188   _GLIBCXX_PROFILE_REENTRANCE_GUARD(__gnu_profile::__is_invalid())
00189 #define __profcxx_is_on() \
00190   _GLIBCXX_PROFILE_REENTRANCE_GUARD(__gnu_profile::__is_on())
00191 #define __profcxx__is_off() \
00192   _GLIBCXX_PROFILE_REENTRANCE_GUARD(__gnu_profile::__is_off())
00193 #else
00194 #define __profcxx_report()
00195 #define __profcxx_turn_on()
00196 #define __profcxx_turn_off()
00197 #define __profcxx_is_invalid()
00198 #define __profcxx_is_on()
00199 #define __profcxx_is_off()
00200 #endif
00201 
00202 // Turn on/off instrumentation for HASHTABLE_TOO_SMALL and HASHTABLE_TOO_LARGE.
00203 #if (defined(_GLIBCXX_PROFILE_HASHTABLE_TOO_SMALL) \
00204      || defined(_GLIBCXX_PROFILE_HASHTABLE_TOO_LARGE))
00205 #define __profcxx_hashtable_resize(__x...) \
00206   _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
00207       __gnu_profile::__trace_hashtable_size_resize(__x))
00208 #define __profcxx_hashtable_destruct(__x...) \
00209   _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
00210       __gnu_profile::__trace_hashtable_size_destruct(__x))
00211 #define __profcxx_hashtable_construct(__x...) \
00212   _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
00213       __gnu_profile::__trace_hashtable_size_construct(__x))
00214 #else
00215 #define __profcxx_hashtable_resize(__x...)  
00216 #define __profcxx_hashtable_destruct(__x...) 
00217 #define __profcxx_hashtable_construct(__x...)  
00218 #endif
00219 
00220 // Turn on/off instrumentation for VECTOR_TOO_SMALL and VECTOR_TOO_LARGE.
00221 #if (defined(_GLIBCXX_PROFILE_VECTOR_TOO_SMALL) \
00222      || defined(_GLIBCXX_PROFILE_VECTOR_TOO_LARGE))
00223 #define __profcxx_vector_resize(__x...) \
00224   _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
00225       __gnu_profile::__trace_vector_size_resize(__x))
00226 #define __profcxx_vector_destruct(__x...) \
00227   _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
00228       __gnu_profile::__trace_vector_size_destruct(__x))
00229 #define __profcxx_vector_construct(__x...) \
00230   _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
00231       __gnu_profile::__trace_vector_size_construct(__x))
00232 #else
00233 #define __profcxx_vector_resize(__x...)  
00234 #define __profcxx_vector_destruct(__x...) 
00235 #define __profcxx_vector_construct(__x...)  
00236 #endif 
00237 
00238 // Turn on/off instrumentation for INEFFICIENT_HASH.
00239 #if defined(_GLIBCXX_PROFILE_INEFFICIENT_HASH)
00240 #define __profcxx_hashtable_construct2(__x...) \
00241   _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
00242       __gnu_profile::__trace_hash_func_construct(__x))
00243 #define __profcxx_hashtable_destruct2(__x...) \
00244   _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
00245       __gnu_profile::__trace_hash_func_destruct(__x))
00246 #else
00247 #define __profcxx_hashtable_destruct2(__x...) 
00248 #define __profcxx_hashtable_construct2(__x...)  
00249 #endif
00250 
00251 // Turn on/off instrumentation for VECTOR_TO_LIST.
00252 #if defined(_GLIBCXX_PROFILE_VECTOR_TO_LIST)
00253 #define __profcxx_vector_construct2(__x...) \
00254   _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
00255       __gnu_profile::__trace_vector_to_list_construct(__x))
00256 #define __profcxx_vector_destruct2(__x...) \
00257   _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
00258       __gnu_profile::__trace_vector_to_list_destruct(__x))
00259 #define __profcxx_vector_insert(__x...) \
00260   _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
00261       __gnu_profile::__trace_vector_to_list_insert(__x))
00262 #define __profcxx_vector_iterate(__x...) \
00263   _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
00264       __gnu_profile::__trace_vector_to_list_iterate(__x))
00265 #define __profcxx_vector_invalid_operator(__x...) \
00266   _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
00267       __gnu_profile::__trace_vector_to_list_invalid_operator(__x))
00268 #define __profcxx_vector_resize2(__x...) \
00269   _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
00270       __gnu_profile::__trace_vector_to_list_resize(__x))
00271 #define __profcxx_vector_find(__x...) \
00272   _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
00273       __gnu_profile::__trace_vector_to_list_find(__x))
00274 #else
00275 #define __profcxx_vector_destruct2(__x...)
00276 #define __profcxx_vector_construct2(__x...)
00277 #define __profcxx_vector_insert(__x...)
00278 #define __profcxx_vector_iterate(__x...)
00279 #define __profcxx_vector_invalid_operator(__x...)
00280 #define __profcxx_vector_resize2(__x...)
00281 #define __profcxx_vector_find(__x...)
00282 #endif
00283 
00284 // Turn on/off instrumentation for LIST_TO_VECTOR. 
00285 #if defined(_GLIBCXX_PROFILE_LIST_TO_VECTOR)
00286 #define __profcxx_list_construct2(__x...) \
00287   _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
00288       __gnu_profile::__trace_list_to_vector_construct(__x))
00289 #define __profcxx_list_destruct2(__x...) \
00290   _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
00291       __gnu_profile::__trace_list_to_vector_destruct(__x))
00292 #define __profcxx_list_insert(__x...) \
00293   _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
00294       __gnu_profile::__trace_list_to_vector_insert(__x))
00295 #define __profcxx_list_iterate(__x...) \
00296   _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
00297       __gnu_profile::__trace_list_to_vector_iterate(__x))
00298 #define __profcxx_list_invalid_operator(__x...) \
00299   _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
00300       __gnu_profile::__trace_list_to_vector_invalid_operator(__x))
00301 #else
00302 #define __profcxx_list_destruct2(__x...)
00303 #define __profcxx_list_construct2(__x...)
00304 #define __profcxx_list_insert(__x...)
00305 #define __profcxx_list_iterate(__x...)
00306 #define __profcxx_list_invalid_operator(__x...)
00307 #endif
00308 
00309 // Turn on/off instrumentation for LIST_TO_SLIST.  
00310 #if defined(_GLIBCXX_PROFILE_LIST_TO_SLIST)
00311 #define __profcxx_list_rewind(__x...) \
00312   _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
00313       __gnu_profile::__trace_list_to_slist_rewind(__x))
00314 #define __profcxx_list_operation(__x...) \
00315   _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
00316       __gnu_profile::__trace_list_to_slist_operation(__x))
00317 #define __profcxx_list_destruct(__x...) \
00318   _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
00319       __gnu_profile::__trace_list_to_slist_destruct(__x))
00320 #define __profcxx_list_construct(__x...) \
00321   _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
00322       __gnu_profile::__trace_list_to_slist_construct(__x))
00323 #else
00324 #define __profcxx_list_rewind(__x...)  
00325 #define __profcxx_list_operation(__x...)
00326 #define __profcxx_list_destruct(__x...) 
00327 #define __profcxx_list_construct(__x...)  
00328 #endif 
00329 
00330 // Turn on/off instrumentation for MAP_TO_UNORDERED_MAP.
00331 #if defined(_GLIBCXX_PROFILE_MAP_TO_UNORDERED_MAP)
00332 #define __profcxx_map_to_unordered_map_construct(__x...) \
00333   _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
00334       __gnu_profile::__trace_map_to_unordered_map_construct(__x))
00335 #define __profcxx_map_to_unordered_map_destruct(__x...) \
00336   _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
00337       __gnu_profile::__trace_map_to_unordered_map_destruct(__x))
00338 #define __profcxx_map_to_unordered_map_insert(__x...) \
00339   _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
00340       __gnu_profile::__trace_map_to_unordered_map_insert(__x))
00341 #define __profcxx_map_to_unordered_map_erase(__x...) \
00342   _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
00343       __gnu_profile::__trace_map_to_unordered_map_erase(__x))
00344 #define __profcxx_map_to_unordered_map_iterate(__x...) \
00345   _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
00346       __gnu_profile::__trace_map_to_unordered_map_iterate(__x))
00347 #define __profcxx_map_to_unordered_map_invalidate(__x...) \
00348   _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
00349       __gnu_profile::__trace_map_to_unordered_map_invalidate(__x))
00350 #define __profcxx_map_to_unordered_map_find(__x...) \
00351   _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
00352       __gnu_profile::__trace_map_to_unordered_map_find(__x))
00353 #else
00354 #define __profcxx_map_to_unordered_map_construct(__x...) \
00355   
00356 #define __profcxx_map_to_unordered_map_destruct(__x...)
00357 #define __profcxx_map_to_unordered_map_insert(__x...)
00358 #define __profcxx_map_to_unordered_map_erase(__x...)
00359 #define __profcxx_map_to_unordered_map_iterate(__x...)
00360 #define __profcxx_map_to_unordered_map_invalidate(__x...)
00361 #define __profcxx_map_to_unordered_map_find(__x...)
00362 #endif
00363 
00364 // Set default values for compile-time customizable variables.
00365 #ifndef _GLIBCXX_PROFILE_TRACE_PATH_ROOT
00366 #define _GLIBCXX_PROFILE_TRACE_PATH_ROOT "libstdcxx-profile"
00367 #endif
00368 #ifndef _GLIBCXX_PROFILE_TRACE_ENV_VAR
00369 #define _GLIBCXX_PROFILE_TRACE_ENV_VAR "_GLIBCXX_PROFILE_TRACE_PATH_ROOT"
00370 #endif
00371 #ifndef _GLIBCXX_PROFILE_MAX_WARN_COUNT_ENV_VAR
00372 #define _GLIBCXX_PROFILE_MAX_WARN_COUNT_ENV_VAR \
00373   "_GLIBCXX_PROFILE_MAX_WARN_COUNT"
00374 #endif
00375 #ifndef _GLIBCXX_PROFILE_MAX_WARN_COUNT
00376 #define _GLIBCXX_PROFILE_MAX_WARN_COUNT 10
00377 #endif
00378 #ifndef _GLIBCXX_PROFILE_MAX_STACK_DEPTH
00379 #define _GLIBCXX_PROFILE_MAX_STACK_DEPTH 32
00380 #endif
00381 #ifndef _GLIBCXX_PROFILE_MAX_STACK_DEPTH_ENV_VAR
00382 #define _GLIBCXX_PROFILE_MAX_STACK_DEPTH_ENV_VAR \
00383   "_GLIBCXX_PROFILE_MAX_STACK_DEPTH"
00384 #endif
00385 #ifndef _GLIBCXX_PROFILE_MEM_PER_DIAGNOSTIC
00386 #define _GLIBCXX_PROFILE_MEM_PER_DIAGNOSTIC (1 << 28)
00387 #endif
00388 #ifndef _GLIBCXX_PROFILE_MEM_PER_DIAGNOSTIC_ENV_VAR
00389 #define _GLIBCXX_PROFILE_MEM_PER_DIAGNOSTIC_ENV_VAR \
00390   "_GLIBCXX_PROFILE_MEM_PER_DIAGNOSTIC"
00391 #endif
00392 
00393 // Instrumentation hook implementations.
00394 #include "profile/impl/profiler_hash_func.h"
00395 #include "profile/impl/profiler_hashtable_size.h"
00396 #include "profile/impl/profiler_map_to_unordered_map.h"
00397 #include "profile/impl/profiler_vector_size.h"
00398 #include "profile/impl/profiler_vector_to_list.h"
00399 #include "profile/impl/profiler_list_to_slist.h"
00400 #include "profile/impl/profiler_list_to_vector.h"
00401 
00402 #endif // _GLIBCXX_PROFILE_PROFILER_H