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 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
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.
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.
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
31 /** @file profile/impl/profiler_list_to_vector.h
32 * @brief diagnostics for list to vector.
35 // Written by Changhee Jung.
37 #ifndef _GLIBCXX_PROFILE_PROFILER_LIST_TO_VECTOR_H
38 #define _GLIBCXX_PROFILE_PROFILER_LIST_TO_VECTOR_H 1
40 #ifdef __GXX_EXPERIMENTAL_CXX0X__
51 #include "profile/impl/profiler.h"
52 #include "profile/impl/profiler_node.h"
53 #include "profile/impl/profiler_trace.h"
55 namespace __gnu_profile
58 /** @brief A list-to-vector instrumentation line in the object table. */
59 class __list2vector_info: public __object_info_base
63 :_M_shift_count(0), _M_iterate(0), _M_resize(0), _M_list_cost(0),
64 _M_vector_cost(0), _M_valid(true), _M_max_size(0) {}
65 __list2vector_info(__stack_t __stack)
66 : __object_info_base(__stack), _M_shift_count(0), _M_iterate(0),
67 _M_resize(0), _M_list_cost(0), _M_vector_cost(0), _M_valid(true),
69 virtual ~__list2vector_info() {}
70 __list2vector_info(const __list2vector_info& __o);
71 void __merge(const __list2vector_info& __o);
72 void __write(FILE* __f) const;
73 float __magnitude() const { return _M_list_cost - _M_vector_cost; }
74 const char* __advice() const;
75 size_t __shift_count() { return _M_shift_count; }
76 size_t __iterate() { return _M_iterate; }
77 float __list_cost() { return _M_list_cost; }
78 size_t __resize() { return _M_resize; }
79 void __set_list_cost(float __lc) { _M_list_cost = __lc; }
80 void __set_vector_cost(float __vc) { _M_vector_cost = __vc; }
81 bool __is_valid() { return _M_valid; }
82 void __set_invalid() { _M_valid = false; }
84 void __opr_insert(size_t __shift, size_t __size);
85 void __opr_iterate(size_t __num) { _M_iterate += __num;}
87 void __resize(size_t __from, size_t __to);
90 size_t _M_shift_count;
99 inline __list2vector_info::__list2vector_info(const __list2vector_info& __o)
100 : __object_info_base(__o)
102 _M_shift_count = __o._M_shift_count;
103 _M_iterate = __o._M_iterate;
104 _M_vector_cost = __o._M_vector_cost;
105 _M_list_cost = __o._M_list_cost;
106 _M_valid = __o._M_valid;
107 _M_resize = __o._M_resize;
108 _M_max_size = __o._M_max_size;
111 inline const char* __list2vector_info::__advice() const {
112 std::stringstream __sstream;
114 << "change std::list to std::vector and its initial size from 0 to "
116 return strdup(__sstream.str().c_str());
119 inline void __list2vector_info::__merge(const __list2vector_info& __o)
121 _M_shift_count += __o._M_shift_count;
122 _M_iterate += __o._M_iterate;
123 _M_vector_cost += __o._M_vector_cost;
124 _M_list_cost += __o._M_list_cost;
125 _M_valid &= __o._M_valid;
126 _M_resize += __o._M_resize;
127 _M_max_size = std::max( _M_max_size, __o._M_max_size);
130 inline void __list2vector_info::__opr_insert(size_t __shift, size_t __size)
132 _M_shift_count += __shift;
133 _M_max_size = std::max(_M_max_size, __size);
136 inline void __list2vector_info::__resize(size_t __from, size_t __to)
141 class __list2vector_stack_info: public __list2vector_info {
143 __list2vector_stack_info(const __list2vector_info& __o)
144 : __list2vector_info(__o) {}
147 class __trace_list_to_vector
148 : public __trace_base<__list2vector_info, __list2vector_stack_info>
151 __trace_list_to_vector();
152 ~__trace_list_to_vector() {}
154 // Insert a new node at construct with object, callstack and initial size.
155 void __insert(__object_t __obj, __stack_t __stack);
156 // Call at destruction/clean to set container final size.
157 void __destruct(const void* __obj);
159 // Find the node in the live map.
160 __list2vector_info* __find(const void* __obj);
162 // Collect cost of operations.
163 void __opr_insert(const void* __obj, size_t __shift, size_t __size);
164 void __opr_iterate(const void* __obj, size_t __num);
165 void __invalid_operator(const void* __obj);
166 void __resize(const void* __obj, size_t __from, size_t __to);
167 float __vector_cost(size_t __shift, size_t __iterate);
168 float __list_cost(size_t __shift, size_t __iterate);
171 inline __trace_list_to_vector::__trace_list_to_vector()
172 : __trace_base<__list2vector_info, __list2vector_stack_info>()
174 __id = "list-to-vector";
177 inline void __trace_list_to_vector::__insert(__object_t __obj,
180 __add_object(__obj, __list2vector_info(__stack));
183 inline void __list2vector_info::__write(FILE* __f) const
185 fprintf(__f, "%Zu %Zu %Zu %.0f %.0f\n",
186 _M_shift_count, _M_resize, _M_iterate, _M_vector_cost, _M_list_cost);
189 inline float __trace_list_to_vector::__vector_cost(size_t __shift,
192 // The resulting vector will use a 'reserve' method.
193 return __shift * _GLIBCXX_PROFILE_DATA(__vector_shift_cost_factor).__value +
194 __iterate * _GLIBCXX_PROFILE_DATA(__vector_iterate_cost_factor).__value;
197 inline float __trace_list_to_vector::__list_cost(size_t __shift,
200 return __shift * _GLIBCXX_PROFILE_DATA(__list_shift_cost_factor).__value +
201 __iterate * _GLIBCXX_PROFILE_DATA(__list_iterate_cost_factor).__value;
204 inline void __trace_list_to_vector::__destruct(const void* __obj)
209 __list2vector_info* __res = __get_object_info(__obj);
213 float __vc = __vector_cost(__res->__shift_count(), __res->__iterate());
214 float __lc = __list_cost(__res->__shift_count(), __res->__iterate());
215 __res->__set_vector_cost(__vc);
216 __res->__set_list_cost(__lc);
217 __retire_object(__obj);
220 inline void __trace_list_to_vector::__opr_insert(const void* __obj,
221 size_t __shift, size_t __size)
223 __list2vector_info* __res = __get_object_info(__obj);
225 __res->__opr_insert(__shift, __size);
228 inline void __trace_list_to_vector::__opr_iterate(const void* __obj,
231 __list2vector_info* __res = __get_object_info(__obj);
233 __res->__opr_iterate(__num);
237 inline void __trace_list_to_vector::__invalid_operator(const void* __obj)
239 __list2vector_info* __res = __get_object_info(__obj);
241 __res->__set_invalid();
244 inline void __trace_list_to_vector::__resize(const void* __obj, size_t __from,
247 __list2vector_info* __res = __get_object_info(__obj);
249 __res->__resize(__from, __to);
252 inline void __trace_list_to_vector_init()
254 _GLIBCXX_PROFILE_DATA(_S_list_to_vector) = new __trace_list_to_vector();
257 inline void __trace_list_to_vector_report(FILE* __f,
258 __warning_vector_t& __warnings)
260 if (_GLIBCXX_PROFILE_DATA(_S_list_to_vector)) {
261 _GLIBCXX_PROFILE_DATA(_S_list_to_vector)->__collect_warnings(__warnings);
262 _GLIBCXX_PROFILE_DATA(_S_list_to_vector)->__write(__f);
266 inline void __trace_list_to_vector_construct(const void* __obj)
268 if (!__profcxx_init()) return;
270 _GLIBCXX_PROFILE_DATA(_S_list_to_vector)->__insert(__obj, __get_stack());
273 inline void __trace_list_to_vector_destruct(const void* __obj)
275 if (!__profcxx_init()) return;
277 _GLIBCXX_PROFILE_DATA(_S_list_to_vector)->__destruct(__obj);
280 inline void __trace_list_to_vector_insert(const void* __obj,
281 size_t __shift, size_t __size)
283 if (!__profcxx_init()) return;
285 _GLIBCXX_PROFILE_DATA(_S_list_to_vector)->__opr_insert(__obj, __shift,
290 inline void __trace_list_to_vector_iterate(const void* __obj, size_t __num = 1)
292 if (!__profcxx_init()) return;
294 _GLIBCXX_PROFILE_DATA(_S_list_to_vector)->__opr_iterate(__obj, __num);
297 inline void __trace_list_to_vector_invalid_operator(const void* __obj)
299 if (!__profcxx_init()) return;
301 _GLIBCXX_PROFILE_DATA(_S_list_to_vector)->__invalid_operator(__obj);
304 inline void __trace_list_to_vector_resize(const void* __obj,
305 size_t __from, size_t __to)
307 if (!__profcxx_init()) return;
309 _GLIBCXX_PROFILE_DATA(_S_list_to_vector)->__resize(__obj, __from, __to);
312 } // namespace __gnu_profile
313 #endif /* _GLIBCXX_PROFILE_PROFILER_LIST_TO_VECTOR_H__ */