libstdc++
profiler.h
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Copyright (C) 2009-2015 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   // Forward declarations of implementation functions.
00084   // Don't use any __gnu_profile:: in user code.
00085   // Instead, use the __profcxx... macros, which offer guarded access.
00086   class __container_size_info;
00087   class __hashfunc_info;
00088   class __map2umap_info;
00089   class __vector2list_info;
00090   class __list2slist_info;
00091   class __list2vector_info;
00092 
00093   bool __turn_on();
00094   bool __turn_off();
00095   bool __is_invalid();
00096   bool __is_on();
00097   bool __is_off();
00098   void __report();
00099 
00100   __container_size_info*
00101   __trace_hashtable_size_construct(std::size_t);
00102   void __trace_hashtable_size_resize(__container_size_info*,
00103                                      std::size_t, std::size_t);
00104   void __trace_hashtable_size_destruct(__container_size_info*,
00105                                        std::size_t, std::size_t);
00106 
00107   __hashfunc_info*
00108   __trace_hash_func_construct();
00109   void __trace_hash_func_destruct(__hashfunc_info*,
00110                                   std::size_t, std::size_t, std::size_t);
00111 
00112   __container_size_info*
00113   __trace_vector_size_construct(std::size_t);
00114   void __trace_vector_size_resize(__container_size_info*,
00115                                   std::size_t, std::size_t);
00116   void __trace_vector_size_destruct(__container_size_info*,
00117                                     std::size_t, std::size_t);
00118 
00119   __vector2list_info*
00120   __trace_vector_to_list_construct();
00121   void __trace_vector_to_list_insert(__vector2list_info*,
00122                                      std::size_t, std::size_t);
00123   void __trace_vector_to_list_iterate(__vector2list_info*, int);
00124   void __trace_vector_to_list_invalid_operator(__vector2list_info*);
00125   void __trace_vector_to_list_resize(__vector2list_info*,
00126                                      std::size_t, std::size_t);
00127   void __trace_vector_to_list_destruct(__vector2list_info*);
00128 
00129   __list2slist_info*
00130   __trace_list_to_slist_construct();
00131   void __trace_list_to_slist_rewind(__list2slist_info*);
00132   void __trace_list_to_slist_operation(__list2slist_info*);
00133   void __trace_list_to_slist_destruct(__list2slist_info*);
00134 
00135   __list2vector_info*
00136   __trace_list_to_vector_construct();
00137   void __trace_list_to_vector_insert(__list2vector_info*,
00138                                      std::size_t, std::size_t);
00139   void __trace_list_to_vector_iterate(__list2vector_info*, int);
00140   void __trace_list_to_vector_invalid_operator(__list2vector_info*);
00141   void __trace_list_to_vector_resize(__list2vector_info*,
00142                                      std::size_t, std::size_t);
00143   void __trace_list_to_vector_destruct(__list2vector_info*);
00144 
00145   __map2umap_info*
00146   __trace_map_to_unordered_map_construct();
00147   void __trace_map_to_unordered_map_invalidate(__map2umap_info*);
00148   void __trace_map_to_unordered_map_insert(__map2umap_info*, std::size_t,
00149                                            std::size_t);
00150   void __trace_map_to_unordered_map_erase(__map2umap_info*, std::size_t,
00151                                           std::size_t);
00152   void __trace_map_to_unordered_map_iterate(__map2umap_info*, std::size_t);
00153   void __trace_map_to_unordered_map_find(__map2umap_info*, std::size_t);
00154   void __trace_map_to_unordered_map_destruct(__map2umap_info*);
00155 } // namespace __gnu_profile
00156 
00157 // Master switch turns on all diagnostics that are not explicitly turned off.
00158 #ifdef _GLIBCXX_PROFILE
00159 #ifndef _GLIBCXX_PROFILE_NO_HASHTABLE_TOO_SMALL
00160 #define _GLIBCXX_PROFILE_HASHTABLE_TOO_SMALL
00161 #endif
00162 #ifndef _GLIBCXX_PROFILE_NO_HASHTABLE_TOO_LARGE
00163 #define _GLIBCXX_PROFILE_HASHTABLE_TOO_LARGE
00164 #endif
00165 #ifndef _GLIBCXX_PROFILE_NO_VECTOR_TOO_SMALL
00166 #define _GLIBCXX_PROFILE_VECTOR_TOO_SMALL
00167 #endif
00168 #ifndef _GLIBCXX_PROFILE_NO_VECTOR_TOO_LARGE
00169 #define _GLIBCXX_PROFILE_VECTOR_TOO_LARGE
00170 #endif
00171 #ifndef _GLIBCXX_PROFILE_NO_INEFFICIENT_HASH
00172 #define _GLIBCXX_PROFILE_INEFFICIENT_HASH
00173 #endif
00174 #ifndef _GLIBCXX_PROFILE_NO_VECTOR_TO_LIST
00175 #define _GLIBCXX_PROFILE_VECTOR_TO_LIST
00176 #endif
00177 #ifndef _GLIBCXX_PROFILE_NO_LIST_TO_SLIST
00178 #define _GLIBCXX_PROFILE_LIST_TO_SLIST
00179 #endif
00180 #ifndef _GLIBCXX_PROFILE_NO_LIST_TO_VECTOR
00181 #define _GLIBCXX_PROFILE_LIST_TO_VECTOR
00182 #endif
00183 #ifndef _GLIBCXX_PROFILE_NO_MAP_TO_UNORDERED_MAP
00184 #define _GLIBCXX_PROFILE_MAP_TO_UNORDERED_MAP
00185 #endif
00186 #endif
00187 
00188 // Expose global management routines to user code.
00189 #ifdef _GLIBCXX_PROFILE
00190 #define __profcxx_report() __gnu_profile::__report()
00191 #define __profcxx_turn_on() __gnu_profile::__turn_on()
00192 #define __profcxx_turn_off() __gnu_profile::__turn_off()
00193 #define __profcxx_is_invalid() __gnu_profile::__is_invalid()
00194 #define __profcxx_is_on() __gnu_profile::__is_on()
00195 #define __profcxx_is_off() __gnu_profile::__is_off()
00196 #else
00197 #define __profcxx_report()
00198 #define __profcxx_turn_on()
00199 #define __profcxx_turn_off()
00200 #define __profcxx_is_invalid()
00201 #define __profcxx_is_on()
00202 #define __profcxx_is_off()
00203 #endif
00204 
00205 // Turn on/off instrumentation for HASHTABLE_TOO_SMALL and HASHTABLE_TOO_LARGE.
00206 #if (defined(_GLIBCXX_PROFILE_HASHTABLE_TOO_SMALL) \
00207      || defined(_GLIBCXX_PROFILE_HASHTABLE_TOO_LARGE))
00208 #define __profcxx_hashtable_size_construct(__x...) \
00209   __gnu_profile::__trace_hashtable_size_construct(__x)
00210 #define __profcxx_hashtable_size_resize(__x...) \
00211   __gnu_profile::__trace_hashtable_size_resize(__x)
00212 #define __profcxx_hashtable_size_destruct(__x...) \
00213   __gnu_profile::__trace_hashtable_size_destruct(__x)
00214 #else
00215 #define __profcxx_hashtable_size_construct(__x...) 0
00216 #define __profcxx_hashtable_size_resize(__x...)
00217 #define __profcxx_hashtable_size_destruct(__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_size_construct(__x...) \
00224   __gnu_profile::__trace_vector_size_construct(__x)
00225 #define __profcxx_vector_size_resize(__x...) \
00226   __gnu_profile::__trace_vector_size_resize(__x)
00227 #define __profcxx_vector_size_destruct(__x...) \
00228   __gnu_profile::__trace_vector_size_destruct(__x)
00229 #else
00230 #define __profcxx_vector_size_construct(__x...) 0
00231 #define __profcxx_vector_size_resize(__x...)  
00232 #define __profcxx_vector_size_destruct(__x...) 
00233 #endif 
00234 
00235 // Turn on/off instrumentation for INEFFICIENT_HASH.
00236 #if defined(_GLIBCXX_PROFILE_INEFFICIENT_HASH)
00237 #define __profcxx_hash_func_construct(__x...) \
00238   __gnu_profile::__trace_hash_func_construct(__x)
00239 #define __profcxx_hash_func_destruct(__x...) \
00240   __gnu_profile::__trace_hash_func_destruct(__x)
00241 #else
00242 #define __profcxx_hash_func_construct(__x...) 0
00243 #define __profcxx_hash_func_destruct(__x...)
00244 #endif
00245 
00246 // Turn on/off instrumentation for VECTOR_TO_LIST.
00247 #if defined(_GLIBCXX_PROFILE_VECTOR_TO_LIST)
00248 #define __profcxx_vector2list_construct(__x...) \
00249   __gnu_profile::__trace_vector_to_list_construct(__x)
00250 #define __profcxx_vector2list_insert(__x...) \
00251   __gnu_profile::__trace_vector_to_list_insert(__x)
00252 #define __profcxx_vector2list_iterate(__x...) \
00253   __gnu_profile::__trace_vector_to_list_iterate(__x)
00254 #define __profcxx_vector2list_invalid_operator(__x...) \
00255   __gnu_profile::__trace_vector_to_list_invalid_operator(__x)
00256 #define __profcxx_vector2list_resize(__x...) \
00257   __gnu_profile::__trace_vector_to_list_resize(__x)
00258 #define __profcxx_vector2list_destruct(__x...) \
00259   __gnu_profile::__trace_vector_to_list_destruct(__x)
00260 #else
00261 #define __profcxx_vector2list_construct(__x...) 0
00262 #define __profcxx_vector2list_insert(__x...)
00263 #define __profcxx_vector2list_iterate(__x...)
00264 #define __profcxx_vector2list_invalid_operator(__x...)
00265 #define __profcxx_vector2list_resize(__x...)
00266 #define __profcxx_vector2list_destruct(__x...)
00267 #endif
00268 
00269 // Turn on/off instrumentation for LIST_TO_VECTOR. 
00270 #if defined(_GLIBCXX_PROFILE_LIST_TO_VECTOR)
00271 #define __profcxx_list2vector_construct(__x...) \
00272   __gnu_profile::__trace_list_to_vector_construct(__x)
00273 #define __profcxx_list2vector_insert(__x...) \
00274   __gnu_profile::__trace_list_to_vector_insert(__x)
00275 #define __profcxx_list2vector_iterate(__x...) \
00276   __gnu_profile::__trace_list_to_vector_iterate(__x)
00277 #define __profcxx_list2vector_invalid_operator(__x...) \
00278   __gnu_profile::__trace_list_to_vector_invalid_operator(__x)
00279 #define __profcxx_list2vector_destruct(__x...) \
00280   __gnu_profile::__trace_list_to_vector_destruct(__x)
00281 #else
00282 #define __profcxx_list2vector_construct(__x...) 0
00283 #define __profcxx_list2vector_insert(__x...)
00284 #define __profcxx_list2vector_iterate(__x...)
00285 #define __profcxx_list2vector_invalid_operator(__x...)
00286 #define __profcxx_list2vector_destruct(__x...)
00287 #endif
00288 
00289 // Turn on/off instrumentation for LIST_TO_SLIST.  
00290 #if defined(_GLIBCXX_PROFILE_LIST_TO_SLIST)
00291 #define __profcxx_list2slist_construct(__x...) \
00292     __gnu_profile::__trace_list_to_slist_construct(__x)
00293 #define __profcxx_list2slist_rewind(__x...) \
00294   __gnu_profile::__trace_list_to_slist_rewind(__x)
00295 #define __profcxx_list2slist_operation(__x...) \
00296   __gnu_profile::__trace_list_to_slist_operation(__x)
00297 #define __profcxx_list2slist_destruct(__x...) \
00298   __gnu_profile::__trace_list_to_slist_destruct(__x)
00299 #else
00300 #define __profcxx_list2slist_construct(__x...) 0
00301 #define __profcxx_list2slist_rewind(__x...)
00302 #define __profcxx_list2slist_operation(__x...)
00303 #define __profcxx_list2slist_destruct(__x...)
00304 #endif 
00305 
00306 // Turn on/off instrumentation for MAP_TO_UNORDERED_MAP.
00307 #if defined(_GLIBCXX_PROFILE_MAP_TO_UNORDERED_MAP)
00308 #define __profcxx_map2umap_construct(__x...) \
00309   __gnu_profile::__trace_map_to_unordered_map_construct(__x)
00310 #define __profcxx_map2umap_insert(__x...) \
00311   __gnu_profile::__trace_map_to_unordered_map_insert(__x)
00312 #define __profcxx_map2umap_erase(__x...) \
00313   __gnu_profile::__trace_map_to_unordered_map_erase(__x)
00314 #define __profcxx_map2umap_iterate(__x...) \
00315   __gnu_profile::__trace_map_to_unordered_map_iterate(__x)
00316 #define __profcxx_map2umap_invalidate(__x...) \
00317   __gnu_profile::__trace_map_to_unordered_map_invalidate(__x)
00318 #define __profcxx_map2umap_find(__x...) \
00319   __gnu_profile::__trace_map_to_unordered_map_find(__x)
00320 #define __profcxx_map2umap_destruct(__x...) \
00321   __gnu_profile::__trace_map_to_unordered_map_destruct(__x)
00322 #else
00323 #define __profcxx_map2umap_construct(__x...) 0
00324 #define __profcxx_map2umap_insert(__x...)
00325 #define __profcxx_map2umap_erase(__x...)
00326 #define __profcxx_map2umap_iterate(__x...)
00327 #define __profcxx_map2umap_invalidate(__x...)
00328 #define __profcxx_map2umap_find(__x...)
00329 #define __profcxx_map2umap_destruct(__x...)
00330 #endif
00331 
00332 // Set default values for compile-time customizable variables.
00333 #ifndef _GLIBCXX_PROFILE_TRACE_PATH_ROOT
00334 #define _GLIBCXX_PROFILE_TRACE_PATH_ROOT "libstdcxx-profile"
00335 #endif
00336 #ifndef _GLIBCXX_PROFILE_TRACE_ENV_VAR
00337 #define _GLIBCXX_PROFILE_TRACE_ENV_VAR "_GLIBCXX_PROFILE_TRACE_PATH_ROOT"
00338 #endif
00339 #ifndef _GLIBCXX_PROFILE_MAX_WARN_COUNT_ENV_VAR
00340 #define _GLIBCXX_PROFILE_MAX_WARN_COUNT_ENV_VAR \
00341   "_GLIBCXX_PROFILE_MAX_WARN_COUNT"
00342 #endif
00343 #ifndef _GLIBCXX_PROFILE_MAX_WARN_COUNT
00344 #define _GLIBCXX_PROFILE_MAX_WARN_COUNT 10
00345 #endif
00346 #ifndef _GLIBCXX_PROFILE_MAX_STACK_DEPTH
00347 #define _GLIBCXX_PROFILE_MAX_STACK_DEPTH 32
00348 #endif
00349 #ifndef _GLIBCXX_PROFILE_MAX_STACK_DEPTH_ENV_VAR
00350 #define _GLIBCXX_PROFILE_MAX_STACK_DEPTH_ENV_VAR \
00351   "_GLIBCXX_PROFILE_MAX_STACK_DEPTH"
00352 #endif
00353 #ifndef _GLIBCXX_PROFILE_MEM_PER_DIAGNOSTIC
00354 #define _GLIBCXX_PROFILE_MEM_PER_DIAGNOSTIC (1 << 28)
00355 #endif
00356 #ifndef _GLIBCXX_PROFILE_MEM_PER_DIAGNOSTIC_ENV_VAR
00357 #define _GLIBCXX_PROFILE_MEM_PER_DIAGNOSTIC_ENV_VAR \
00358   "_GLIBCXX_PROFILE_MEM_PER_DIAGNOSTIC"
00359 #endif
00360 
00361 // Instrumentation hook implementations.
00362 #include "profile/impl/profiler_hash_func.h"
00363 #include "profile/impl/profiler_hashtable_size.h"
00364 #include "profile/impl/profiler_map_to_unordered_map.h"
00365 #include "profile/impl/profiler_vector_size.h"
00366 #include "profile/impl/profiler_vector_to_list.h"
00367 #include "profile/impl/profiler_list_to_slist.h"
00368 #include "profile/impl/profiler_list_to_vector.h"
00369 
00370 #endif // _GLIBCXX_PROFILE_PROFILER_H