libstdc++
profiler_node.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_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 void* __instruction_address_t;
00043   typedef std::_GLIBCXX_STD_C::vector<__instruction_address_t> __stack_npt;
00044   typedef __stack_npt* __stack_t;
00045 
00046   std::size_t __stack_max_depth();
00047 
00048   inline __stack_t
00049   __get_stack()
00050   {
00051 #if defined _GLIBCXX_HAVE_EXECINFO_H
00052     __try
00053       {
00054         std::size_t __max_depth = __stack_max_depth();
00055         if (__max_depth == 0)
00056           return 0;
00057         __stack_npt __buffer(__max_depth);
00058         int __depth = backtrace(&__buffer[0], __max_depth);
00059         return new(std::nothrow) __stack_npt(__buffer.begin(),
00060                                              __buffer.begin() + __depth);
00061       }
00062     __catch(...)
00063       {
00064         return 0;
00065       }
00066 #else
00067     return 0;
00068 #endif
00069   }
00070 
00071   inline std::size_t
00072   __size(__stack_t __stack)
00073   {
00074     if (!__stack)
00075       return 0;
00076     else
00077       return __stack->size();
00078   }
00079 
00080   // XXX
00081   inline void
00082   __write(FILE* __f, __stack_t __stack)
00083   {
00084     if (!__stack)
00085       return;
00086   
00087     __stack_npt::const_iterator __it;
00088     for (__it = __stack->begin(); __it != __stack->end(); ++__it)
00089       std::fprintf(__f, "%p ", *__it);
00090   }
00091 
00092   /** @brief Hash function for summary trace using call stack as index.  */
00093   class __stack_hash 
00094   {
00095   public:
00096     std::size_t
00097     operator()(__stack_t __s) const
00098     {
00099       if (!__s) 
00100         return 0;
00101 
00102       std::size_t __index = 0;
00103       __stack_npt::const_iterator __it;
00104       for (__it = __s->begin(); __it != __s->end(); ++__it)
00105         __index += reinterpret_cast<std::size_t>(*__it);
00106       return __index;
00107     }
00108 
00109     bool operator() (__stack_t __stack1, __stack_t __stack2) const
00110     {
00111       if (!__stack1 && !__stack2)
00112         return true;
00113       if (!__stack1 || !__stack2)
00114         return false;
00115       if (__stack1->size() != __stack2->size())
00116         return false;
00117 
00118       std::size_t __byte_size
00119         = __stack1->size() * sizeof(__stack_npt::value_type);
00120       return __builtin_memcmp(&(*__stack1)[0], &(*__stack2)[0],
00121                               __byte_size) == 0;
00122     }
00123   };
00124 
00125 
00126   /** @brief Base class for a line in the object table.  */
00127   class __object_info_base
00128   {
00129   public:
00130     __object_info_base(__stack_t __stack)
00131     : _M_stack(__stack), _M_valid(true) { }
00132 
00133     bool
00134     __is_valid() const 
00135     { return _M_valid; }
00136     
00137     void
00138     __set_invalid()
00139     { _M_valid = false; }
00140 
00141     void
00142     __merge(const __object_info_base& __o)
00143     { _M_valid &= __o._M_valid; }
00144 
00145     __stack_t
00146     __stack() const
00147     { return _M_stack; }
00148     
00149   protected:
00150     __stack_t _M_stack;
00151     bool _M_valid;
00152   };
00153 
00154 } // namespace __gnu_profile
00155 #endif /* _GLIBCXX_PROFILE_PROFILER_NODE_H */