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