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