]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/l4re-core/libstdc++-v3/contrib/libstdc++-v3-4.5/include/profile/impl/profiler_map_to_unordered_map.h
Update
[l4.git] / l4 / pkg / l4re-core / libstdc++-v3 / contrib / libstdc++-v3-4.5 / include / profile / impl / profiler_map_to_unordered_map.h
1 // -*- C++ -*-
2 //
3 // Copyright (C) 2009 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_map_to_unordered_map.h
32  *  @brief Diagnostics for map to unordered_map.
33  */
34
35 // Written by Silvius Rus.
36
37 #ifndef _GLIBCXX_PROFILE_PROFILER_MAP_TO_UNORDERED_MAP_H
38 #define _GLIBCXX_PROFILE_PROFILER_MAP_TO_UNORDERED_MAP_H 1
39
40 #ifdef __GXX_EXPERIMENTAL_CXX0X__
41 #include <cstdlib>
42 #include <cstdio>
43 #include <cstring>
44 #else
45 #include <stdlib.h>
46 #include <stdio.h>
47 #include <string.h>
48 #endif
49 #include "profile/impl/profiler.h"
50 #include "profile/impl/profiler_node.h"
51 #include "profile/impl/profiler_trace.h"
52
53 namespace __gnu_profile
54 {
55
56 inline int __log2(size_t __size)
57 {
58   for (int __bit_count = sizeof(size_t) - 1; __bit_count >= 0; -- __bit_count) 
59   {
60     if ((2 << __bit_count) & __size) {
61       return __bit_count;
62     }
63   }
64   return 0;
65 }
66
67 inline float __map_insert_cost(size_t __size)
68 {
69   return (_GLIBCXX_PROFILE_DATA(__map_insert_cost_factor).__value 
70           * static_cast<float>(__log2(__size)));
71 }
72
73 inline float __map_erase_cost(size_t __size)
74 {
75   return (_GLIBCXX_PROFILE_DATA(__map_erase_cost_factor).__value
76           * static_cast<float>(__log2(__size)));
77 }
78
79 inline float __map_find_cost(size_t __size)
80 {
81   return (_GLIBCXX_PROFILE_DATA(__map_find_cost_factor).__value
82           * static_cast<float>(__log2(__size)));
83 }
84
85 /** @brief A map-to-unordered_map instrumentation line in the object table.  */
86 class __map2umap_info: public __object_info_base
87 {
88  public:
89   __map2umap_info()
90       : _M_insert(0), _M_erase(0), _M_find(0), _M_iterate(0),
91         _M_map_cost(0.0), _M_umap_cost(0.0), _M_valid(true) {}
92   __map2umap_info(__stack_t __stack)
93       : __object_info_base(__stack), _M_insert(0), _M_erase(0), _M_find(0), 
94         _M_iterate(0), _M_map_cost(0.0), _M_umap_cost(0.0), _M_valid(true) {} 
95   virtual ~__map2umap_info() {}
96   __map2umap_info(const __map2umap_info& o);
97   void __merge(const __map2umap_info& o);
98   void __write(FILE* __f) const;
99   float __magnitude() const { return _M_map_cost - _M_umap_cost; }
100   const char* __advice() const;
101
102   void __record_insert(size_t __size, size_t __count);
103   void __record_erase(size_t __size, size_t __count);
104   void __record_find(size_t __size);
105   void __record_iterate(size_t __count);
106   void __record_invalidate();
107
108  private:
109   size_t _M_insert;
110   size_t _M_erase;
111   size_t _M_find;
112   size_t _M_iterate;
113   float _M_umap_cost;
114   float _M_map_cost;
115   bool  _M_valid;
116 };
117
118 inline const char* __map2umap_info::__advice() const
119 {
120   return strdup("change std::map to std::unordered_map");
121 }
122
123 inline __map2umap_info::__map2umap_info(const __map2umap_info& __o)
124     : __object_info_base(__o), 
125       _M_insert(__o._M_insert),
126       _M_erase(__o._M_erase),
127       _M_find(__o._M_find),
128       _M_iterate(__o._M_iterate),
129       _M_map_cost(__o._M_map_cost),
130       _M_umap_cost(__o._M_umap_cost),
131       _M_valid(__o._M_valid)
132 {}
133
134 inline void __map2umap_info::__merge(const __map2umap_info& __o)
135 {
136   _M_insert    += __o._M_insert;
137   _M_erase     += __o._M_erase;
138   _M_find      += __o._M_find;
139   _M_map_cost  += __o._M_map_cost;
140   _M_umap_cost += __o._M_umap_cost;
141   _M_valid     &= __o._M_valid;
142 }
143
144 inline void __map2umap_info:: __record_insert(size_t __size, size_t __count)
145 {
146   _M_insert += __count;
147   _M_map_cost += __count * __map_insert_cost(__size);
148   _M_umap_cost += (__count
149                    * _GLIBCXX_PROFILE_DATA(__umap_insert_cost_factor).__value);
150 }
151
152 inline void __map2umap_info:: __record_erase(size_t __size, size_t __count)
153 {
154   _M_erase += __count;
155   _M_map_cost += __count * __map_erase_cost(__size);
156   _M_umap_cost += (__count
157                    * _GLIBCXX_PROFILE_DATA(__umap_erase_cost_factor).__value);
158 }
159
160 inline void __map2umap_info:: __record_find(size_t __size)
161 {
162   _M_find += 1;
163   _M_map_cost += __map_find_cost(__size);
164   _M_umap_cost += _GLIBCXX_PROFILE_DATA(__umap_find_cost_factor).__value;
165 }
166
167 inline void __map2umap_info:: __record_iterate(size_t __count)
168 {
169   _M_iterate += __count;
170   _M_map_cost += (__count
171                   * _GLIBCXX_PROFILE_DATA(__map_iterate_cost_factor).__value);
172   _M_umap_cost += (
173       __count * _GLIBCXX_PROFILE_DATA(__umap_iterate_cost_factor).__value);
174 }
175
176 inline void __map2umap_info:: __record_invalidate()
177 {
178   _M_valid = false;
179 }
180
181 inline void __map2umap_info::__write(FILE* __f) const
182 {
183   fprintf(__f, "%Zu %Zu %Zu %Zu %.0f %.0f %s\n",
184           _M_insert, _M_erase, _M_find, _M_iterate, _M_map_cost, _M_umap_cost,
185           _M_valid ? "valid" : "invalid");
186 }
187
188 /** @brief A map-to-unordered_map instrumentation line in the stack table.  */
189 class __map2umap_stack_info: public __map2umap_info
190 {
191  public:
192   __map2umap_stack_info(const __map2umap_info& o) : __map2umap_info(o) {}
193 };
194
195 /** @brief Map-to-unordered_map instrumentation producer.  */
196 class __trace_map2umap
197     : public __trace_base<__map2umap_info, __map2umap_stack_info> 
198 {
199  public:
200   __trace_map2umap();
201 };
202
203 inline __trace_map2umap::__trace_map2umap()
204     : __trace_base<__map2umap_info, __map2umap_stack_info>()
205 {
206   __id = "map-to-unordered-map";
207 }
208
209 inline void __trace_map_to_unordered_map_init()
210 {
211   _GLIBCXX_PROFILE_DATA(_S_map2umap) = new __trace_map2umap();
212 }
213
214 inline void __trace_map_to_unordered_map_report(
215     FILE* __f, __warning_vector_t& __warnings)
216 {
217   if (_GLIBCXX_PROFILE_DATA(_S_map2umap)) {
218     _GLIBCXX_PROFILE_DATA(_S_map2umap)->__collect_warnings(__warnings);
219     _GLIBCXX_PROFILE_DATA(_S_map2umap)->__write(__f);
220   }
221 }
222
223 inline void __trace_map_to_unordered_map_construct(const void* __obj)
224 {
225   if (!__profcxx_init()) return;
226
227   _GLIBCXX_PROFILE_DATA(_S_map2umap)->__add_object(
228       __obj, __map2umap_info(__get_stack()));
229 }
230
231 inline void __trace_map_to_unordered_map_destruct(const void* __obj)
232 {
233   if (!__profcxx_init()) return;
234
235   _GLIBCXX_PROFILE_DATA(_S_map2umap)->__retire_object(__obj);
236 }
237
238 inline void __trace_map_to_unordered_map_insert(const void* __obj, 
239                                                 size_t __size, size_t __count)
240 {
241   if (!__profcxx_init()) return;
242
243   __map2umap_info* __info =
244       _GLIBCXX_PROFILE_DATA(_S_map2umap)->__get_object_info(__obj);
245
246   if (__info) __info->__record_insert(__size, __count);
247 }
248
249 inline void __trace_map_to_unordered_map_erase(const void* __obj, 
250                                                size_t __size, size_t __count)
251 {
252   if (!__profcxx_init()) return;
253
254   __map2umap_info* __info =
255       _GLIBCXX_PROFILE_DATA(_S_map2umap)->__get_object_info(__obj);
256
257   if (__info) __info->__record_erase(__size, __count);
258 }
259
260 inline void __trace_map_to_unordered_map_find(const void* __obj, size_t __size)
261 {
262   if (!__profcxx_init()) return;
263
264   __map2umap_info* __info =
265       _GLIBCXX_PROFILE_DATA(_S_map2umap)->__get_object_info(__obj);
266
267   if (__info) __info->__record_find(__size);
268 }
269
270 inline void __trace_map_to_unordered_map_iterate(const void* __obj, 
271                                                  size_t __count)
272 {
273   if (!__profcxx_init()) return;
274
275   __map2umap_info* __info =
276       _GLIBCXX_PROFILE_DATA(_S_map2umap)->__get_object_info(__obj);
277
278   if (__info) __info->__record_iterate(__count);
279 }
280
281 inline void __trace_map_to_unordered_map_invalidate(const void* __obj)
282 {
283   if (!__profcxx_init()) return;
284
285   __map2umap_info* __info =
286       _GLIBCXX_PROFILE_DATA(_S_map2umap)->__get_object_info(__obj);
287
288   if (__info) __info->__record_invalidate();
289 }
290
291 } // namespace __gnu_profile
292 #endif /* _GLIBCXX_PROFILE_PROFILER_MAP_TO_UNORDERED_MAP_H */