3 // Copyright (C) 2009-2014 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_vector_to_list.h
25 * @brief diagnostics for vector to list.
28 // Written by Lixia Liu and Silvius Rus.
30 #ifndef _GLIBCXX_PROFILE_PROFILER_VECTOR_TO_LIST_H
31 #define _GLIBCXX_PROFILE_PROFILER_VECTOR_TO_LIST_H 1
33 #include "profile/impl/profiler.h"
34 #include "profile/impl/profiler_node.h"
35 #include "profile/impl/profiler_trace.h"
37 namespace __gnu_profile
39 /** @brief A vector-to-list instrumentation line in the object table. */
40 class __vector2list_info
41 : public __object_info_base
45 : _M_shift_count(0), _M_iterate(0), _M_resize(0), _M_list_cost(0),
46 _M_vector_cost(0), _M_valid(true) { }
48 __vector2list_info(__stack_t __stack
)
49 : __object_info_base(__stack
), _M_shift_count(0), _M_iterate(0),
50 _M_resize(0), _M_list_cost(0), _M_vector_cost(0), _M_valid(true) { }
52 virtual ~__vector2list_info() { }
54 __vector2list_info(const __vector2list_info
& __o
)
55 : __object_info_base(__o
), _M_shift_count(__o
._M_shift_count
),
56 _M_iterate(__o
._M_iterate
), _M_resize(__o
._M_resize
),
57 _M_list_cost(__o
._M_list_cost
), _M_vector_cost(__o
._M_vector_cost
),
58 _M_valid(__o
._M_valid
) { }
61 __merge(const __vector2list_info
& __o
)
63 _M_shift_count
+= __o
._M_shift_count
;
64 _M_iterate
+= __o
._M_iterate
;
65 _M_vector_cost
+= __o
._M_vector_cost
;
66 _M_list_cost
+= __o
._M_list_cost
;
67 _M_valid
&= __o
._M_valid
;
68 _M_resize
+= __o
._M_resize
;
72 __write(FILE* __f
) const
74 std::fprintf(__f
, "%Zu %Zu %Zu %.0f %.0f\n", _M_shift_count
,
75 _M_resize
, _M_iterate
, _M_vector_cost
, _M_list_cost
);
80 { return _M_vector_cost
- _M_list_cost
; }
84 { return "change std::vector to std::list"; }
88 { return _M_shift_count
; }
92 { return _M_iterate
; }
96 { return _M_list_cost
; }
100 { return _M_resize
; }
103 __set_list_cost(float __lc
)
104 { _M_list_cost
= __lc
; }
107 __set_vector_cost(float __vc
)
108 { _M_vector_cost
= __vc
; }
116 { _M_valid
= false; }
119 __opr_insert(std::size_t __pos
, std::size_t __num
)
120 { _M_shift_count
+= __num
- __pos
; }
123 __opr_iterate(std::size_t __num
)
124 { _M_iterate
+= __num
; }
127 __resize(std::size_t __from
, std::size_t)
128 { _M_resize
+= __from
; }
131 __opr_find(std::size_t __size
)
133 // Use average case complexity.
134 _M_iterate
+= 3.0 / 4.0 * __size
;
138 std::size_t _M_shift_count
;
139 std::size_t _M_iterate
;
140 std::size_t _M_resize
;
142 float _M_vector_cost
;
147 /** @brief A vector-to-list instrumentation line in the stack table. */
148 class __vector2list_stack_info
149 : public __vector2list_info
152 __vector2list_stack_info(const __vector2list_info
& __o
)
153 : __vector2list_info(__o
) { }
157 /** @brief Vector-to-list instrumentation producer. */
158 class __trace_vector_to_list
159 : public __trace_base
<__vector2list_info
, __vector2list_stack_info
>
162 __trace_vector_to_list()
163 : __trace_base
<__vector2list_info
, __vector2list_stack_info
>()
164 { __id
= "vector-to-list"; }
166 ~__trace_vector_to_list() { }
168 // Insert a new node at construct with object, callstack and initial size.
170 __insert(__object_t __obj
, __stack_t __stack
)
171 { __add_object(__obj
, __vector2list_info(__stack
)); }
173 // Call at destruction/clean to set container final size.
175 __destruct(const void* __obj
)
180 __vector2list_info
* __res
= __get_object_info(__obj
);
184 float __vc
= __vector_cost(__res
->__shift_count(), __res
->__iterate(),
186 float __lc
= __list_cost(__res
->__shift_count(), __res
->__iterate(),
188 __res
->__set_vector_cost(__vc
);
189 __res
->__set_list_cost(__lc
);
191 __retire_object(__obj
);
194 // Find the node in the live map.
196 __vector2list_info
* __find(const void* __obj
);
198 // Collect cost of operations.
200 __opr_insert(const void* __obj
, std::size_t __pos
, std::size_t __num
)
202 __vector2list_info
* __res
= __get_object_info(__obj
);
204 __res
->__opr_insert(__pos
, __num
);
208 __opr_iterate(const void* __obj
, std::size_t __num
)
210 __vector2list_info
* __res
= __get_object_info(__obj
);
212 __res
->__opr_iterate(__num
);
216 __invalid_operator(const void* __obj
)
218 __vector2list_info
* __res
= __get_object_info(__obj
);
220 __res
->__set_invalid();
224 __resize(const void* __obj
, std::size_t __from
, std::size_t __to
)
226 __vector2list_info
* __res
= __get_object_info(__obj
);
228 __res
->__resize(__from
, __to
);
232 __vector_cost(std::size_t __shift
, std::size_t __iterate
,
233 std::size_t __resize
)
236 * _GLIBCXX_PROFILE_DATA(__vector_shift_cost_factor
).__value
238 * _GLIBCXX_PROFILE_DATA(__vector_iterate_cost_factor
).__value
240 * _GLIBCXX_PROFILE_DATA(__vector_resize_cost_factor
).__value
);
244 __list_cost(std::size_t __shift
, std::size_t __iterate
,
245 std::size_t __resize
)
248 * _GLIBCXX_PROFILE_DATA(__list_shift_cost_factor
).__value
250 * _GLIBCXX_PROFILE_DATA(__list_iterate_cost_factor
).__value
252 * _GLIBCXX_PROFILE_DATA(__list_resize_cost_factor
).__value
);
256 __opr_find(const void* __obj
, std::size_t __size
)
258 __vector2list_info
* __res
= __get_object_info(__obj
);
260 __res
->__opr_find(__size
);
266 __trace_vector_to_list_init()
267 { _GLIBCXX_PROFILE_DATA(_S_vector_to_list
) = new __trace_vector_to_list(); }
270 __trace_vector_to_list_report(FILE* __f
, __warning_vector_t
& __warnings
)
272 if (_GLIBCXX_PROFILE_DATA(_S_vector_to_list
))
274 _GLIBCXX_PROFILE_DATA(_S_vector_to_list
)->
275 __collect_warnings(__warnings
);
276 _GLIBCXX_PROFILE_DATA(_S_vector_to_list
)->__write(__f
);
281 __trace_vector_to_list_construct(const void* __obj
)
283 if (!__profcxx_init())
286 _GLIBCXX_PROFILE_DATA(_S_vector_to_list
)->__insert(__obj
, __get_stack());
290 __trace_vector_to_list_destruct(const void* __obj
)
292 if (!__profcxx_init())
295 _GLIBCXX_PROFILE_DATA(_S_vector_to_list
)->__destruct(__obj
);
299 __trace_vector_to_list_insert(const void* __obj
, std::size_t __pos
,
302 if (!__profcxx_init())
305 _GLIBCXX_PROFILE_DATA(_S_vector_to_list
)->__opr_insert(__obj
, __pos
,
310 __trace_vector_to_list_iterate(const void* __obj
, std::size_t __num
= 1)
312 if (!__profcxx_init())
315 _GLIBCXX_PROFILE_DATA(_S_vector_to_list
)->__opr_iterate(__obj
, __num
);
319 __trace_vector_to_list_invalid_operator(const void* __obj
)
321 if (!__profcxx_init())
324 _GLIBCXX_PROFILE_DATA(_S_vector_to_list
)->__invalid_operator(__obj
);
328 __trace_vector_to_list_resize(const void* __obj
, std::size_t __from
,
331 if (!__profcxx_init())
334 _GLIBCXX_PROFILE_DATA(_S_vector_to_list
)->__resize(__obj
, __from
, __to
);
338 __trace_vector_to_list_find(const void* __obj
, std::size_t __size
)
340 if (!__profcxx_init())
343 _GLIBCXX_PROFILE_DATA(_S_vector_to_list
)->__opr_find(__obj
, __size
);
346 } // namespace __gnu_profile
347 #endif /* _GLIBCXX_PROFILE_PROFILER_VECTOR_TO_LIST_H */