3 // Copyright (C) 2009, 2010 Free Software Foundation, Inc.
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)
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.
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.
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/>.
24 /** @file profile/impl/profiler_list_to_vector.h
25 * @brief diagnostics for list to vector.
28 // Written by Changhee Jung.
30 #ifndef _GLIBCXX_PROFILE_PROFILER_LIST_TO_VECTOR_H
31 #define _GLIBCXX_PROFILE_PROFILER_LIST_TO_VECTOR_H 1
35 #include "profile/impl/profiler.h"
36 #include "profile/impl/profiler_node.h"
37 #include "profile/impl/profiler_trace.h"
39 namespace __gnu_profile
41 /** @brief A list-to-vector instrumentation line in the object table. */
42 class __list2vector_info
43 : public __object_info_base
47 : _M_shift_count(0), _M_iterate(0), _M_resize(0), _M_list_cost(0),
48 _M_vector_cost(0), _M_valid(true), _M_max_size(0) { }
50 __list2vector_info(__stack_t __stack)
51 : __object_info_base(__stack), _M_shift_count(0), _M_iterate(0),
52 _M_resize(0), _M_list_cost(0), _M_vector_cost(0), _M_valid(true),
55 virtual ~__list2vector_info() { }
57 __list2vector_info(const __list2vector_info& __o)
58 : __object_info_base(__o), _M_shift_count(__o._M_shift_count),
59 _M_iterate(__o._M_iterate), _M_resize(__o._M_resize),
60 _M_list_cost(__o._M_list_cost), _M_vector_cost(__o._M_vector_cost),
61 _M_valid(__o._M_valid), _M_max_size(__o._M_max_size) { }
64 __merge(const __list2vector_info& __o)
66 _M_shift_count += __o._M_shift_count;
67 _M_iterate += __o._M_iterate;
68 _M_vector_cost += __o._M_vector_cost;
69 _M_list_cost += __o._M_list_cost;
70 _M_valid &= __o._M_valid;
71 _M_resize += __o._M_resize;
72 _M_max_size = std::max( _M_max_size, __o._M_max_size);
76 __write(FILE* __f) const
78 std::fprintf(__f, "%Zu %Zu %Zu %.0f %.0f\n", _M_shift_count,
79 _M_resize, _M_iterate, _M_vector_cost, _M_list_cost);
84 { return _M_list_cost - _M_vector_cost; }
89 std::stringstream __sstream;
91 << "change std::list to std::vector and its initial size from 0 to "
93 return __sstream.str();
98 { return _M_shift_count; }
102 { return _M_iterate; }
106 { return _M_list_cost; }
110 { return _M_resize; }
113 __set_list_cost(float __lc)
114 { _M_list_cost = __lc; }
117 __set_vector_cost(float __vc)
118 { _M_vector_cost = __vc; }
126 { _M_valid = false; }
129 __opr_insert(std::size_t __shift, std::size_t __size)
131 _M_shift_count += __shift;
132 _M_max_size = std::max(_M_max_size, __size);
136 __opr_iterate(std::size_t __num)
137 { _M_iterate += __num;}
140 __resize(std::size_t __from, std::size_t)
141 { _M_resize += __from; }
144 std::size_t _M_shift_count;
145 std::size_t _M_iterate;
146 std::size_t _M_resize;
148 float _M_vector_cost;
150 std::size_t _M_max_size;
153 class __list2vector_stack_info
154 : public __list2vector_info
157 __list2vector_stack_info(const __list2vector_info& __o)
158 : __list2vector_info(__o) {}
161 class __trace_list_to_vector
162 : public __trace_base<__list2vector_info, __list2vector_stack_info>
165 __trace_list_to_vector()
166 : __trace_base<__list2vector_info, __list2vector_stack_info>()
167 { __id = "list-to-vector"; }
169 ~__trace_list_to_vector() { }
171 // Insert a new node at construct with object, callstack and initial size.
173 __insert(__object_t __obj, __stack_t __stack)
174 { __add_object(__obj, __list2vector_info(__stack)); }
176 // Call at destruction/clean to set container final size.
178 __destruct(const void* __obj)
183 __list2vector_info* __res = __get_object_info(__obj);
187 float __vc = __vector_cost(__res->__shift_count(), __res->__iterate());
188 float __lc = __list_cost(__res->__shift_count(), __res->__iterate());
189 __res->__set_vector_cost(__vc);
190 __res->__set_list_cost(__lc);
191 __retire_object(__obj);
194 // Find the node in the live map.
195 __list2vector_info* __find(const void* __obj);
197 // Collect cost of operations.
199 __opr_insert(const void* __obj, std::size_t __shift, std::size_t __size)
201 __list2vector_info* __res = __get_object_info(__obj);
203 __res->__opr_insert(__shift, __size);
207 __opr_iterate(const void* __obj, std::size_t __num)
209 __list2vector_info* __res = __get_object_info(__obj);
211 __res->__opr_iterate(__num);
215 __invalid_operator(const void* __obj)
217 __list2vector_info* __res = __get_object_info(__obj);
219 __res->__set_invalid();
223 __resize(const void* __obj, std::size_t __from, std::size_t __to)
225 __list2vector_info* __res = __get_object_info(__obj);
227 __res->__resize(__from, __to);
231 __vector_cost(std::size_t __shift, std::size_t __iterate)
233 // The resulting vector will use a 'reserve' method.
235 * _GLIBCXX_PROFILE_DATA(__vector_shift_cost_factor).__value
237 * _GLIBCXX_PROFILE_DATA(__vector_iterate_cost_factor).__value);
241 __list_cost(std::size_t __shift, std::size_t __iterate)
244 * _GLIBCXX_PROFILE_DATA(__list_shift_cost_factor).__value
246 * _GLIBCXX_PROFILE_DATA(__list_iterate_cost_factor).__value);
252 __trace_list_to_vector_init()
253 { _GLIBCXX_PROFILE_DATA(_S_list_to_vector) = new __trace_list_to_vector(); }
256 __trace_list_to_vector_report(FILE* __f, __warning_vector_t& __warnings)
258 if (_GLIBCXX_PROFILE_DATA(_S_list_to_vector))
260 _GLIBCXX_PROFILE_DATA(_S_list_to_vector)->
261 __collect_warnings(__warnings);
262 _GLIBCXX_PROFILE_DATA(_S_list_to_vector)->__write(__f);
267 __trace_list_to_vector_construct(const void* __obj)
269 if (!__profcxx_init())
272 _GLIBCXX_PROFILE_DATA(_S_list_to_vector)->__insert(__obj, __get_stack());
276 __trace_list_to_vector_destruct(const void* __obj)
278 if (!__profcxx_init())
281 _GLIBCXX_PROFILE_DATA(_S_list_to_vector)->__destruct(__obj);
285 __trace_list_to_vector_insert(const void* __obj,
286 std::size_t __shift, std::size_t __size)
288 if (!__profcxx_init())
291 _GLIBCXX_PROFILE_DATA(_S_list_to_vector)->__opr_insert(__obj, __shift,
296 __trace_list_to_vector_iterate(const void* __obj, std::size_t __num = 1)
298 if (!__profcxx_init())
301 _GLIBCXX_PROFILE_DATA(_S_list_to_vector)->__opr_iterate(__obj, __num);
305 __trace_list_to_vector_invalid_operator(const void* __obj)
307 if (!__profcxx_init())
310 _GLIBCXX_PROFILE_DATA(_S_list_to_vector)->__invalid_operator(__obj);
314 __trace_list_to_vector_resize(const void* __obj,
315 std::size_t __from, std::size_t __to)
317 if (!__profcxx_init())
320 _GLIBCXX_PROFILE_DATA(_S_list_to_vector)->__resize(__obj, __from, __to);
323 } // namespace __gnu_profile
324 #endif /* _GLIBCXX_PROFILE_PROFILER_LIST_TO_VECTOR_H__ */