Home | History | Annotate | Download | only in impl
      1 // -*- C++ -*-
      2 //
      3 // Copyright (C) 2009, 2010, 2011 Free Software Foundation, Inc.
      4 //
      5 // This file is part of the GNU ISO C++ Library.  This library is free
      6 // software; you can redistribute it and/or modify it under the terms
      7 // of the GNU General Public License as published by the Free Software
      8 // Foundation; either version 2, or (at your option) any later
      9 // version.
     10 
     11 // This library is distributed in the hope that it will be useful, but
     12 // WITHOUT ANY WARRANTY; without even the implied warranty of
     13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     14 // General Public License for more details.
     15 
     16 // You should have received a copy of the GNU General Public License
     17 // along with this library; see the file COPYING.  If not, write to
     18 // the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
     19 // MA 02111-1307, USA.
     20 
     21 // As a special exception, you may use this file as part of a free
     22 // software library without restriction.  Specifically, if other files
     23 // instantiate templates or use macros or inline functions from this
     24 // file, or you compile this file and link it with other files to
     25 // produce an executable, this file does not by itself cause the
     26 // resulting executable to be covered by the GNU General Public
     27 // License.  This exception does not however invalidate any other
     28 // reasons why the executable file might be covered by the GNU General
     29 // Public License.
     30 
     31 /** @file profile/impl/profiler_hash_func.h
     32  *  @brief Data structures to represent profiling traces.
     33  */
     34 
     35 // Written by Lixia Liu and Silvius Rus.
     36 
     37 #ifndef _GLIBCXX_PROFILE_PROFILER_HASH_FUNC_H
     38 #define _GLIBCXX_PROFILE_PROFILER_HASH_FUNC_H 1
     39 
     40 #include "profile/impl/profiler.h"
     41 #include "profile/impl/profiler_node.h"
     42 #include "profile/impl/profiler_trace.h"
     43 
     44 namespace __gnu_profile
     45 {
     46   /** @brief A hash performance instrumentation line in the object table.  */
     47   class __hashfunc_info
     48   : public __object_info_base
     49   {
     50   public:
     51     __hashfunc_info()
     52     : _M_longest_chain(0), _M_accesses(0), _M_hops(0) { }
     53 
     54     __hashfunc_info(const __hashfunc_info& __o)
     55     : __object_info_base(__o), _M_longest_chain(__o._M_longest_chain),
     56       _M_accesses(__o._M_accesses), _M_hops(__o._M_hops) { }
     57 
     58     __hashfunc_info(__stack_t __stack)
     59     : __object_info_base(__stack), _M_longest_chain(0),
     60       _M_accesses(0), _M_hops(0) { }
     61 
     62     virtual ~__hashfunc_info() { }
     63 
     64     void
     65     __merge(const __hashfunc_info& __o)
     66     {
     67       _M_longest_chain  = std::max(_M_longest_chain, __o._M_longest_chain);
     68       _M_accesses      += __o._M_accesses;
     69       _M_hops          += __o._M_hops;
     70     }
     71 
     72     void
     73     __destruct(std::size_t __chain, std::size_t __accesses,
     74 	       std::size_t __hops)
     75     {
     76       _M_longest_chain  = std::max(_M_longest_chain, __chain);
     77       _M_accesses      += __accesses;
     78       _M_hops          += __hops;
     79     }
     80 
     81     void
     82     __write(FILE* __f) const
     83     { std::fprintf(__f, "%Zu %Zu %Zu\n", _M_hops,
     84 		   _M_accesses, _M_longest_chain); }
     85 
     86     float
     87     __magnitude() const
     88     { return static_cast<float>(_M_hops); }
     89 
     90     std::string
     91     __advice() const
     92     { return "change hash function"; }
     93 
     94   private:
     95     std::size_t _M_longest_chain;
     96     std::size_t _M_accesses;
     97     std::size_t _M_hops;
     98   };
     99 
    100 
    101   /** @brief A hash performance instrumentation line in the stack table.  */
    102   class __hashfunc_stack_info
    103   : public __hashfunc_info
    104   {
    105   public:
    106     __hashfunc_stack_info(const __hashfunc_info& __o)
    107     : __hashfunc_info(__o) { }
    108   };
    109 
    110 
    111   /** @brief Hash performance instrumentation producer.  */
    112   class __trace_hash_func
    113   : public __trace_base<__hashfunc_info, __hashfunc_stack_info>
    114   {
    115   public:
    116     __trace_hash_func()
    117     : __trace_base<__hashfunc_info, __hashfunc_stack_info>()
    118     { __id = "hash-distr"; }
    119 
    120     ~__trace_hash_func() {}
    121 
    122     // Insert a new node at construct with object, callstack and initial size.
    123     void
    124     __insert(__object_t __obj, __stack_t __stack)
    125     { __add_object(__obj, __hashfunc_info(__stack)); }
    126 
    127     // Call at destruction/clean to set container final size.
    128     void
    129     __destruct(const void* __obj, std::size_t __chain,
    130 	       std::size_t __accesses, std::size_t __hops)
    131     {
    132       if (!__is_on())
    133 	return;
    134 
    135       // First find the item from the live objects and update the informations.
    136       __hashfunc_info* __objs = __get_object_info(__obj);
    137       if (!__objs)
    138 	return;
    139 
    140       __objs->__destruct(__chain, __accesses, __hops);
    141       __retire_object(__obj);
    142     }
    143   };
    144 
    145 
    146   inline void
    147   __trace_hash_func_init()
    148   { _GLIBCXX_PROFILE_DATA(_S_hash_func) = new __trace_hash_func(); }
    149 
    150   inline void
    151   __trace_hash_func_report(FILE* __f, __warning_vector_t& __warnings)
    152   {
    153     if (_GLIBCXX_PROFILE_DATA(_S_hash_func))
    154       {
    155 	_GLIBCXX_PROFILE_DATA(_S_hash_func)->__collect_warnings(__warnings);
    156 	_GLIBCXX_PROFILE_DATA(_S_hash_func)->__write(__f);
    157       }
    158   }
    159 
    160   inline void
    161   __trace_hash_func_construct(const void* __obj)
    162   {
    163     if (!__profcxx_init())
    164       return;
    165 
    166     _GLIBCXX_PROFILE_DATA(_S_hash_func)->__insert(__obj, __get_stack());
    167   }
    168 
    169   inline void
    170   __trace_hash_func_destruct(const void* __obj, std::size_t __chain,
    171 			     std::size_t __accesses, std::size_t __hops)
    172   {
    173     if (!__profcxx_init())
    174       return;
    175 
    176     _GLIBCXX_PROFILE_DATA(_S_hash_func)->__destruct(__obj, __chain,
    177 						    __accesses, __hops);
    178   }
    179 
    180 } // namespace __gnu_profile
    181 #endif /* _GLIBCXX_PROFILE_PROFILER_HASH_FUNC_H */
    182