]>
Commit | Line | Data |
---|---|---|
1218d701 SR |
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_trace.h | |
32 | * @brief Diagnostics for container sizes. | |
33 | */ | |
34 | ||
35 | // Written by Lixia Liu and Silvius Rus. | |
36 | ||
6b223191 BK |
37 | #ifndef _GLIBCXX_PROFILE_PROFILER_CONTAINER_SIZE_H |
38 | #define _GLIBCXX_PROFILE_PROFILER_CONTAINER_SIZE_H 1 | |
1218d701 SR |
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 | ||
50 | #include "profile/impl/profiler.h" | |
51 | #include "profile/impl/profiler_node.h" | |
52 | #include "profile/impl/profiler_trace.h" | |
53 | ||
9ee6a740 | 54 | namespace __gnu_profile |
1218d701 | 55 | { |
6b223191 BK |
56 | /** @brief A container size instrumentation line in the object table. */ |
57 | class __container_size_info: public __object_info_base | |
58 | { | |
59 | public: | |
60 | __container_size_info(); | |
61 | __container_size_info(const __container_size_info& __o); | |
62 | __container_size_info(__stack_t __stack, size_t __num); | |
63 | virtual ~__container_size_info() { } | |
64 | ||
65 | void __write(FILE* f) const; | |
66 | float __magnitude() const { return static_cast<float>(_M_cost); } | |
67 | const char* __advice() const; | |
68 | ||
69 | void __merge(const __container_size_info& __o); | |
70 | ||
71 | // Call if a container is destructed or cleaned. | |
72 | void __destruct(size_t __num, size_t __inum); | |
73 | ||
74 | // Estimate the cost of resize/rehash. | |
75 | float __resize_cost(size_t __from, size_t __to) { return __from; } | |
76 | ||
77 | // Call if container is resized. | |
78 | void __resize(size_t __from, size_t __to); | |
79 | ||
80 | private: | |
81 | size_t _M_init; | |
82 | size_t _M_max; // Range of # buckets. | |
83 | size_t _M_min; | |
84 | size_t _M_total; | |
85 | size_t _M_item_min; // Range of # items. | |
86 | size_t _M_item_max; | |
87 | size_t _M_item_total; | |
88 | size_t _M_count; | |
89 | size_t _M_resize; | |
90 | size_t _M_cost; | |
91 | }; | |
92 | ||
93 | inline const char* | |
94 | __container_size_info::__advice() const | |
95 | { | |
96 | const size_t __max_chars_size_t_printed = 20; | |
97 | const char* __message_pattern = | |
1218d701 | 98 | "change initial container size from %d to %d"; |
6b223191 BK |
99 | size_t __message_size = (strlen(__message_pattern) |
100 | + 2 * __max_chars_size_t_printed | |
101 | - 2 * 2); | |
102 | char* __message = new char[__message_size + 1]; | |
103 | ||
104 | if (_M_init < _M_item_max) | |
105 | snprintf(__message, __message_size, __message_pattern, _M_init, | |
106 | _M_item_max); | |
107 | else | |
108 | snprintf(__message, __message_size, __message_pattern, _M_init, | |
109 | _M_item_max); | |
110 | ||
111 | return __message; | |
1218d701 | 112 | } |
1218d701 | 113 | |
6b223191 BK |
114 | inline void |
115 | __container_size_info::__destruct(size_t __num, size_t __inum) | |
116 | { | |
117 | _M_max = __max(_M_max, __num); | |
118 | _M_item_max = __max(_M_item_max, __inum); | |
119 | if (_M_min == 0) | |
120 | { | |
121 | _M_min = __num; | |
122 | _M_item_min = __inum; | |
123 | } | |
124 | else | |
125 | { | |
126 | _M_min = __min(_M_min, __num); | |
127 | _M_item_min = __min(_M_item_min, __inum); | |
128 | } | |
129 | _M_total += __num; | |
130 | _M_item_total += __inum; | |
131 | _M_count += 1; | |
132 | } | |
1218d701 | 133 | |
6b223191 BK |
134 | inline void |
135 | __container_size_info::__resize(size_t __from, size_t __to) | |
136 | { | |
137 | _M_cost += this->__resize_cost(__from, __to); | |
138 | _M_resize += 1; | |
139 | _M_max = __max(_M_max, __to); | |
140 | } | |
1218d701 | 141 | |
6b223191 BK |
142 | inline void |
143 | __container_size_info::__merge(const __container_size_info& __o) | |
144 | { | |
145 | _M_init = __max(_M_init, __o._M_init); | |
146 | _M_max = __max(_M_max, __o._M_max); | |
147 | _M_item_max = __max(_M_item_max, __o._M_item_max); | |
148 | _M_min = __min(_M_min, __o._M_min); | |
149 | _M_item_min = __min(_M_item_min, __o._M_item_min); | |
150 | _M_total += __o._M_total; | |
151 | _M_item_total += __o._M_item_total; | |
152 | _M_count += __o._M_count; | |
153 | _M_cost += __o._M_cost; | |
154 | _M_resize += __o._M_resize; | |
155 | } | |
1218d701 | 156 | |
6b223191 BK |
157 | inline __container_size_info::__container_size_info() |
158 | : _M_init(0), _M_max(0), _M_item_max(0), _M_min(0), _M_item_min(0), | |
159 | _M_total(0), _M_item_total(0), _M_cost(0), _M_count(0), _M_resize(0) | |
160 | { } | |
161 | ||
162 | inline __container_size_info::__container_size_info(__stack_t __stack, | |
163 | size_t __num) | |
164 | : __object_info_base(__stack), _M_init(0), _M_max(0), _M_item_max(0), | |
165 | _M_min(0), _M_item_min(0), _M_total(0), _M_item_total(0), _M_cost(0), | |
166 | _M_count(0), _M_resize(0) | |
167 | { | |
168 | _M_init = _M_max = __num; | |
169 | _M_item_min = _M_item_max = _M_item_total = _M_total = 0; | |
170 | _M_min = 0; | |
171 | _M_count = 0; | |
172 | _M_resize = 0; | |
173 | } | |
1218d701 | 174 | |
6b223191 BK |
175 | inline __container_size_info::__container_size_info(const __container_size_info& __o) |
176 | : __object_info_base(__o) | |
177 | { | |
178 | _M_init = __o._M_init; | |
179 | _M_max = __o._M_max; | |
180 | _M_item_max = __o._M_item_max; | |
181 | _M_min = __o._M_min; | |
182 | _M_item_min = __o._M_item_min; | |
183 | _M_total = __o._M_total; | |
184 | _M_item_total = __o._M_item_total; | |
185 | _M_cost = __o._M_cost; | |
186 | _M_count = __o._M_count; | |
187 | _M_resize = __o._M_resize; | |
188 | } | |
1218d701 | 189 | |
6b223191 BK |
190 | /** @brief A container size instrumentation line in the stack table. */ |
191 | class __container_size_stack_info: public __container_size_info | |
192 | { | |
193 | public: | |
194 | __container_size_stack_info(const __container_size_info& __o) | |
195 | : __container_size_info(__o) { } | |
196 | }; | |
197 | ||
198 | /** @brief Container size instrumentation trace producer. */ | |
199 | class __trace_container_size | |
200 | : public __trace_base<__container_size_info, __container_size_stack_info> | |
201 | { | |
202 | public: | |
203 | __trace_container_size() | |
204 | : __trace_base<__container_size_info, __container_size_stack_info>() { }; | |
205 | ||
206 | ~__trace_container_size() { } | |
207 | ||
208 | // Insert a new node at construct with object, callstack and initial size. | |
209 | void __insert(const __object_t __obj, __stack_t __stack, size_t __num); | |
210 | ||
211 | // Call at destruction/clean to set container final size. | |
212 | void __destruct(const void* __obj, size_t __num, size_t __inum); | |
213 | void __construct(const void* __obj, size_t __inum); | |
214 | ||
215 | // Call at resize to set resize/cost information. | |
216 | void __resize(const void* __obj, int __from, int __to); | |
217 | }; | |
218 | ||
219 | inline void | |
220 | __trace_container_size::__insert(const __object_t __obj, | |
221 | __stack_t __stack, size_t __num) | |
222 | { __add_object(__obj, __container_size_info(__stack, __num)); } | |
223 | ||
224 | inline void | |
225 | __container_size_info::__write(FILE* __f) const | |
226 | { | |
227 | fprintf(__f, "%Zu %Zu %Zu %Zu %Zu %Zu %Zu %Zu %Zu %Zu\n", | |
228 | _M_init, _M_count, _M_cost, _M_resize, _M_min, _M_max, _M_total, | |
229 | _M_item_min, _M_item_max, _M_item_total); | |
230 | } | |
1218d701 | 231 | |
6b223191 BK |
232 | inline void |
233 | __trace_container_size::__destruct(const void* __obj, size_t __num, | |
234 | size_t __inum) | |
235 | { | |
236 | if (!__is_on()) return; | |
1218d701 | 237 | |
6b223191 BK |
238 | __object_t __obj_handle = static_cast<__object_t>(__obj); |
239 | ||
240 | __container_size_info* __object_info = __get_object_info(__obj_handle); | |
241 | if (!__object_info) | |
242 | return; | |
1218d701 | 243 | |
6b223191 BK |
244 | __object_info->__destruct(__num, __inum); |
245 | __retire_object(__obj_handle); | |
246 | } | |
247 | ||
248 | inline void | |
249 | __trace_container_size::__resize(const void* __obj, int __from, int __to) | |
250 | { | |
251 | if (!__is_on()) return; | |
1218d701 | 252 | |
6b223191 BK |
253 | __container_size_info* __object_info = __get_object_info(__obj); |
254 | if (!__object_info) | |
255 | return; | |
1218d701 | 256 | |
6b223191 BK |
257 | __object_info->__resize(__from, __to); |
258 | } | |
9ee6a740 | 259 | } // namespace __gnu_profile |
6b223191 | 260 | #endif /* _GLIBCXX_PROFILE_PROFILER_CONTAINER_SIZE_H */ |