libstdc++
profiler_node.h
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Copyright (C) 2009, 2010, 2011 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_node.h
00025  *  @brief Data structures to represent a single profiling event.
00026  */
00027 
00028 // Written by Lixia Liu and Silvius Rus.
00029 
00030 #ifndef _GLIBCXX_PROFILE_PROFILER_NODE_H
00031 #define _GLIBCXX_PROFILE_PROFILER_NODE_H 1
00032 
00033 #include <cstdio> // FILE, fprintf
00034 
00035 #include <vector>
00036 #if defined _GLIBCXX_HAVE_EXECINFO_H
00037 #include <execinfo.h>
00038 #endif
00039 
00040 namespace __gnu_profile
00041 {
00042   typedef const void* __object_t;
00043   typedef void* __instruction_address_t;
00044   typedef std::_GLIBCXX_STD_C::vector<__instruction_address_t> __stack_npt;
00045   typedef __stack_npt* __stack_t;
00046 
00047   std::size_t __stack_max_depth();
00048 
00049   inline __stack_t
00050   __get_stack()
00051   {
00052 #if defined _GLIBCXX_HAVE_EXECINFO_H
00053     std::size_t __max_depth = __stack_max_depth();
00054     if (__max_depth == 0)
00055       return 0;
00056     __stack_npt __buffer(__max_depth);
00057     int __depth = backtrace(&__buffer[0], __max_depth);
00058     __stack_t __stack = new __stack_npt(__depth);
00059     __builtin_memcpy(&(*__stack)[0], &__buffer[0],
00060              __depth * sizeof(__object_t));
00061     return __stack;
00062 #else
00063     return 0;
00064 #endif
00065   }
00066 
00067   inline std::size_t
00068   __size(__stack_t __stack)
00069   {
00070     if (!__stack)
00071       return 0;
00072     else
00073       return __stack->size();
00074   }
00075 
00076   // XXX
00077   inline void
00078   __write(FILE* __f, __stack_t __stack)
00079   {
00080     if (!__stack)
00081       return;
00082   
00083     __stack_npt::const_iterator __it;
00084     for (__it = __stack->begin(); __it != __stack->end(); ++__it)
00085       std::fprintf(__f, "%p ", *__it);
00086   }
00087 
00088   /** @brief Hash function for summary trace using call stack as index.  */
00089   class __stack_hash 
00090   {
00091   public:
00092     std::size_t
00093     operator()(__stack_t __s) const
00094     {
00095       if (!__s) 
00096     return 0;
00097 
00098       std::size_t __index = 0;
00099       __stack_npt::const_iterator __it;
00100       for (__it = __s->begin(); __it != __s->end(); ++__it)
00101     __index += reinterpret_cast<std::size_t>(*__it);
00102       return __index;
00103     }
00104 
00105     bool operator() (__stack_t __stack1, __stack_t __stack2) const
00106     {
00107       if (!__stack1 && !__stack2)
00108     return true;
00109       if (!__stack1 || !__stack2)
00110     return false;
00111       if (__stack1->size() != __stack2->size())
00112     return false;
00113 
00114       std::size_t __byte_size
00115     = __stack1->size() * sizeof(__stack_npt::value_type);
00116       return __builtin_memcmp(&(*__stack1)[0], &(*__stack2)[0],
00117                   __byte_size) == 0;
00118     }
00119   };
00120 
00121 
00122   /** @brief Base class for a line in the object table.  */
00123   class __object_info_base
00124   {
00125   public:
00126     __object_info_base() { }
00127 
00128     __object_info_base(__stack_t __stack)
00129     : _M_stack(__stack), _M_valid(true) { }
00130 
00131     __object_info_base(const __object_info_base& __o)
00132     : _M_stack(__o._M_stack), _M_valid(__o._M_valid) { }
00133 
00134     virtual ~__object_info_base() { }
00135 
00136     bool
00137     __is_valid() const 
00138     { return _M_valid; }
00139     
00140     __stack_t
00141     __stack() const
00142     { return _M_stack; }
00143     
00144     virtual void __write(FILE* __f) const = 0;
00145 
00146   protected:
00147     __stack_t _M_stack;
00148     bool _M_valid;
00149   };
00150 
00151 
00152   /** @brief Base class for a line in the stack table.  */
00153   template<typename __object_info>
00154     class __stack_info_base
00155     {
00156     public:
00157       __stack_info_base() { }
00158       __stack_info_base(const __object_info& __info) = 0;
00159       virtual ~__stack_info_base() {}
00160       void __merge(const __object_info& __info) = 0;
00161       virtual float __magnitude() const = 0;
00162       virtual const char* __get_id() const = 0;
00163     };
00164 
00165 } // namespace __gnu_profile
00166 #endif /* _GLIBCXX_PROFILE_PROFILER_NODE_H */