libstdc++
profiler_hash_func.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_hash_func.h
00025  *  @brief Data structures to represent profiling traces.
00026  */
00027 
00028 // Written by Lixia Liu and Silvius Rus.
00029 
00030 #ifndef _GLIBCXX_PROFILE_PROFILER_HASH_FUNC_H
00031 #define _GLIBCXX_PROFILE_PROFILER_HASH_FUNC_H 1
00032 
00033 #include "profile/impl/profiler.h"
00034 #include "profile/impl/profiler_node.h"
00035 #include "profile/impl/profiler_trace.h"
00036 
00037 namespace __gnu_profile
00038 {
00039   /** @brief A hash performance instrumentation line in the object table.  */
00040   class __hashfunc_info
00041   : public __object_info_base
00042   {
00043   public:
00044     __hashfunc_info()
00045     : _M_longest_chain(0), _M_accesses(0), _M_hops(0) { }
00046 
00047     __hashfunc_info(const __hashfunc_info& __o)
00048     : __object_info_base(__o), _M_longest_chain(__o._M_longest_chain),
00049       _M_accesses(__o._M_accesses), _M_hops(__o._M_hops) { }
00050 
00051     __hashfunc_info(__stack_t __stack)
00052     : __object_info_base(__stack), _M_longest_chain(0),
00053       _M_accesses(0), _M_hops(0) { }
00054 
00055     virtual ~__hashfunc_info() { }
00056 
00057     void
00058     __merge(const __hashfunc_info& __o)
00059     {
00060       _M_longest_chain  = std::max(_M_longest_chain, __o._M_longest_chain);
00061       _M_accesses      += __o._M_accesses;
00062       _M_hops          += __o._M_hops;
00063     }
00064 
00065     void
00066     __destruct(std::size_t __chain, std::size_t __accesses,
00067            std::size_t __hops)
00068     { 
00069       _M_longest_chain  = std::max(_M_longest_chain, __chain);
00070       _M_accesses      += __accesses;
00071       _M_hops          += __hops;
00072     }
00073 
00074     void
00075     __write(FILE* __f) const
00076     { std::fprintf(__f, "%Zu %Zu %Zu\n", _M_hops,
00077            _M_accesses, _M_longest_chain); }
00078 
00079     float
00080     __magnitude() const
00081     { return static_cast<float>(_M_hops); }
00082 
00083     std::string
00084     __advice() const
00085     { return "change hash function"; }
00086 
00087   private:
00088     std::size_t _M_longest_chain;
00089     std::size_t _M_accesses;
00090     std::size_t _M_hops;
00091   };
00092 
00093 
00094   /** @brief A hash performance instrumentation line in the stack table.  */
00095   class __hashfunc_stack_info 
00096   : public __hashfunc_info
00097   {
00098   public:
00099     __hashfunc_stack_info(const __hashfunc_info& __o)
00100     : __hashfunc_info(__o) { }
00101   };
00102 
00103 
00104   /** @brief Hash performance instrumentation producer.  */
00105   class __trace_hash_func
00106   : public __trace_base<__hashfunc_info, __hashfunc_stack_info> 
00107   {
00108   public:
00109     __trace_hash_func()
00110     : __trace_base<__hashfunc_info, __hashfunc_stack_info>()
00111     { __id = "hash-distr"; }
00112 
00113     ~__trace_hash_func() {}
00114     
00115     // Insert a new node at construct with object, callstack and initial size.
00116     void
00117     __insert(__object_t __obj, __stack_t __stack)
00118     { __add_object(__obj, __hashfunc_info(__stack)); }
00119 
00120     // Call at destruction/clean to set container final size.
00121     void
00122     __destruct(const void* __obj, std::size_t __chain,
00123            std::size_t __accesses, std::size_t __hops)
00124     {
00125       if (!__is_on())
00126     return;
00127 
00128       // First find the item from the live objects and update the informations.
00129       __hashfunc_info* __objs = __get_object_info(__obj);
00130       if (!__objs)
00131     return;
00132 
00133       __objs->__destruct(__chain, __accesses, __hops);
00134       __retire_object(__obj);
00135     }
00136   };
00137 
00138 
00139   inline void
00140   __trace_hash_func_init()
00141   { _GLIBCXX_PROFILE_DATA(_S_hash_func) = new __trace_hash_func(); }
00142 
00143   inline void
00144   __trace_hash_func_report(FILE* __f, __warning_vector_t& __warnings)
00145   {
00146     if (_GLIBCXX_PROFILE_DATA(_S_hash_func))
00147       {
00148     _GLIBCXX_PROFILE_DATA(_S_hash_func)->__collect_warnings(__warnings);
00149     _GLIBCXX_PROFILE_DATA(_S_hash_func)->__write(__f);
00150       }
00151   }
00152 
00153   inline void
00154   __trace_hash_func_construct(const void* __obj)
00155   {
00156     if (!__profcxx_init())
00157       return;
00158 
00159     _GLIBCXX_PROFILE_DATA(_S_hash_func)->__insert(__obj, __get_stack());
00160   }
00161 
00162   inline void
00163   __trace_hash_func_destruct(const void* __obj, std::size_t __chain,
00164                  std::size_t __accesses, std::size_t __hops)
00165   {
00166     if (!__profcxx_init())
00167       return;
00168 
00169     _GLIBCXX_PROFILE_DATA(_S_hash_func)->__destruct(__obj, __chain,
00170                             __accesses, __hops);
00171   }
00172 
00173 } // namespace __gnu_profile
00174 #endif /* _GLIBCXX_PROFILE_PROFILER_HASH_FUNC_H */