libstdc++
profiler_hash_func.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_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(__stack_t __stack)
00045     : __object_info_base(__stack), _M_longest_chain(0),
00046       _M_accesses(0), _M_hops(0) { }
00047 
00048     void
00049     __merge(const __hashfunc_info& __o)
00050     {
00051       __object_info_base::__merge(__o);
00052       _M_longest_chain  = std::max(_M_longest_chain, __o._M_longest_chain);
00053       _M_accesses      += __o._M_accesses;
00054       _M_hops          += __o._M_hops;
00055     }
00056 
00057     void
00058     __destruct(std::size_t __chain, std::size_t __accesses,
00059                std::size_t __hops)
00060     { 
00061       _M_longest_chain  = std::max(_M_longest_chain, __chain);
00062       _M_accesses      += __accesses;
00063       _M_hops          += __hops;
00064     }
00065 
00066     void
00067     __write(FILE* __f) const
00068     { std::fprintf(__f, "%Zu %Zu %Zu\n", _M_hops,
00069                    _M_accesses, _M_longest_chain); }
00070 
00071     float
00072     __magnitude() const
00073     { return static_cast<float>(_M_hops); }
00074 
00075     std::string
00076     __advice() const
00077     { return "change hash function"; }
00078 
00079   private:
00080     std::size_t _M_longest_chain;
00081     std::size_t _M_accesses;
00082     std::size_t _M_hops;
00083   };
00084 
00085   /** @brief A hash performance instrumentation line in the stack table.  */
00086   class __hashfunc_stack_info 
00087   : public __hashfunc_info
00088   {
00089   public:
00090     __hashfunc_stack_info(const __hashfunc_info& __o)
00091     : __hashfunc_info(__o) { }
00092   };
00093 
00094   /** @brief Hash performance instrumentation producer.  */
00095   class __trace_hash_func
00096   : public __trace_base<__hashfunc_info, __hashfunc_stack_info> 
00097   {
00098   public:
00099     __trace_hash_func()
00100     : __trace_base<__hashfunc_info, __hashfunc_stack_info>()
00101     { __id = "hash-distr"; }
00102 
00103     ~__trace_hash_func() {}
00104     
00105     // Call at destruction/clean to set container final size.
00106     void
00107     __destruct(__hashfunc_info* __obj_info, 
00108                std::size_t __chain, std::size_t __accesses, std::size_t __hops)
00109     {
00110       if (!__obj_info)
00111         return;
00112 
00113       __obj_info->__destruct(__chain, __accesses, __hops);
00114       __retire_object(__obj_info);
00115     }
00116   };
00117 
00118   inline void
00119   __trace_hash_func_init()
00120   { _GLIBCXX_PROFILE_DATA(_S_hash_func) = new __trace_hash_func(); }
00121 
00122   inline void
00123   __trace_hash_func_free()
00124   { delete _GLIBCXX_PROFILE_DATA(_S_hash_func); }
00125 
00126   inline void
00127   __trace_hash_func_report(FILE* __f, __warning_vector_t& __warnings)
00128   { __trace_report(_GLIBCXX_PROFILE_DATA(_S_hash_func), __f, __warnings); }
00129 
00130   inline __hashfunc_info*
00131   __trace_hash_func_construct()
00132   {
00133     if (!__profcxx_init())
00134       return 0;
00135 
00136     if (!__reentrance_guard::__get_in())
00137       return 0;
00138 
00139     __reentrance_guard __get_out;
00140     return _GLIBCXX_PROFILE_DATA(_S_hash_func)->__add_object(__get_stack());
00141   }
00142 
00143   inline void
00144   __trace_hash_func_destruct(__hashfunc_info* __obj_info,
00145                              std::size_t __chain, std::size_t __accesses,
00146                              std::size_t __hops)
00147   {
00148     _GLIBCXX_PROFILE_DATA(_S_hash_func)->__destruct(__obj_info, __chain,
00149                                                     __accesses, __hops);
00150   }
00151 
00152 } // namespace __gnu_profile
00153 #endif /* _GLIBCXX_PROFILE_PROFILER_HASH_FUNC_H */