1 // -*- C++ -*- 2 // 3 // Copyright (C) 2009-2014 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 7 // terms of the GNU General Public License as published by the 8 // Free Software Foundation; either version 3, or (at your option) 9 // any later version. 10 // 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. 15 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. 19 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/>. 23 24 /** @file profile/impl/profiler_vector_to_list.h 25 * @brief diagnostics for vector to list. 26 */ 27 28 // Written by Lixia Liu and Silvius Rus. 29 30 #ifndef _GLIBCXX_PROFILE_PROFILER_VECTOR_TO_LIST_H 31 #define _GLIBCXX_PROFILE_PROFILER_VECTOR_TO_LIST_H 1 32 33 #include "profile/impl/profiler.h" 34 #include "profile/impl/profiler_node.h" 35 #include "profile/impl/profiler_trace.h" 36 37 namespace __gnu_profile 38 { 39 /** @brief A vector-to-list instrumentation line in the object table. */ 40 class __vector2list_info 41 : public __object_info_base 42 { 43 public: 44 __vector2list_info() 45 : _M_shift_count(0), _M_iterate(0), _M_resize(0), _M_list_cost(0), 46 _M_vector_cost(0), _M_valid(true) { } 47 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) { } 51 52 virtual ~__vector2list_info() { } 53 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) { } 59 60 void 61 __merge(const __vector2list_info& __o) 62 { 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; 69 } 70 71 void 72 __write(FILE* __f) const 73 { 74 std::fprintf(__f, "%Zu %Zu %Zu %.0f %.0f\n", _M_shift_count, 75 _M_resize, _M_iterate, _M_vector_cost, _M_list_cost); 76 } 77 78 float 79 __magnitude() const 80 { return _M_vector_cost - _M_list_cost; } 81 82 std::string 83 __advice() const 84 { return "change std::vector to std::list"; } 85 86 std::size_t 87 __shift_count() 88 { return _M_shift_count; } 89 90 std::size_t 91 __iterate() 92 { return _M_iterate; } 93 94 float 95 __list_cost() 96 { return _M_list_cost; } 97 98 std::size_t 99 __resize() 100 { return _M_resize; } 101 102 void 103 __set_list_cost(float __lc) 104 { _M_list_cost = __lc; } 105 106 void 107 __set_vector_cost(float __vc) 108 { _M_vector_cost = __vc; } 109 110 bool 111 __is_valid() 112 { return _M_valid; } 113 114 void 115 __set_invalid() 116 { _M_valid = false; } 117 118 void 119 __opr_insert(std::size_t __pos, std::size_t __num) 120 { _M_shift_count += __num - __pos; } 121 122 void 123 __opr_iterate(std::size_t __num) 124 { _M_iterate += __num; } 125 126 void 127 __resize(std::size_t __from, std::size_t) 128 { _M_resize += __from; } 129 130 void 131 __opr_find(std::size_t __size) 132 { 133 // Use average case complexity. 134 _M_iterate += 3.0 / 4.0 * __size; 135 } 136 137 private: 138 std::size_t _M_shift_count; 139 std::size_t _M_iterate; 140 std::size_t _M_resize; 141 float _M_list_cost; 142 float _M_vector_cost; 143 bool _M_valid; 144 }; 145 146 147 /** @brief A vector-to-list instrumentation line in the stack table. */ 148 class __vector2list_stack_info 149 : public __vector2list_info 150 { 151 public: 152 __vector2list_stack_info(const __vector2list_info& __o) 153 : __vector2list_info(__o) { } 154 }; 155 156 157 /** @brief Vector-to-list instrumentation producer. */ 158 class __trace_vector_to_list 159 : public __trace_base<__vector2list_info, __vector2list_stack_info> 160 { 161 public: 162 __trace_vector_to_list() 163 : __trace_base<__vector2list_info, __vector2list_stack_info>() 164 { __id = "vector-to-list"; } 165 166 ~__trace_vector_to_list() { } 167 168 // Insert a new node at construct with object, callstack and initial size. 169 void 170 __insert(__object_t __obj, __stack_t __stack) 171 { __add_object(__obj, __vector2list_info(__stack)); } 172 173 // Call at destruction/clean to set container final size. 174 void 175 __destruct(const void* __obj) 176 { 177 if (!__is_on()) 178 return; 179 180 __vector2list_info* __res = __get_object_info(__obj); 181 if (!__res) 182 return; 183 184 float __vc = __vector_cost(__res->__shift_count(), __res->__iterate(), 185 __res->__resize()); 186 float __lc = __list_cost(__res->__shift_count(), __res->__iterate(), 187 __res->__resize()); 188 __res->__set_vector_cost(__vc); 189 __res->__set_list_cost(__lc); 190 191 __retire_object(__obj); 192 } 193 194 // Find the node in the live map. 195 // XXX Undefined?!? 196 __vector2list_info* __find(const void* __obj); 197 198 // Collect cost of operations. 199 void 200 __opr_insert(const void* __obj, std::size_t __pos, std::size_t __num) 201 { 202 __vector2list_info* __res = __get_object_info(__obj); 203 if (__res) 204 __res->__opr_insert(__pos, __num); 205 } 206 207 void 208 __opr_iterate(const void* __obj, std::size_t __num) 209 { 210 __vector2list_info* __res = __get_object_info(__obj); 211 if (__res) 212 __res->__opr_iterate(__num); 213 } 214 215 void 216 __invalid_operator(const void* __obj) 217 { 218 __vector2list_info* __res = __get_object_info(__obj); 219 if (__res) 220 __res->__set_invalid(); 221 } 222 223 void 224 __resize(const void* __obj, std::size_t __from, std::size_t __to) 225 { 226 __vector2list_info* __res = __get_object_info(__obj); 227 if (__res) 228 __res->__resize(__from, __to); 229 } 230 231 float 232 __vector_cost(std::size_t __shift, std::size_t __iterate, 233 std::size_t __resize) 234 { 235 return (__shift 236 * _GLIBCXX_PROFILE_DATA(__vector_shift_cost_factor).__value 237 + __iterate 238 * _GLIBCXX_PROFILE_DATA(__vector_iterate_cost_factor).__value 239 + __resize 240 * _GLIBCXX_PROFILE_DATA(__vector_resize_cost_factor).__value); 241 } 242 243 float 244 __list_cost(std::size_t __shift, std::size_t __iterate, 245 std::size_t __resize) 246 { 247 return (__shift 248 * _GLIBCXX_PROFILE_DATA(__list_shift_cost_factor).__value 249 + __iterate 250 * _GLIBCXX_PROFILE_DATA(__list_iterate_cost_factor).__value 251 + __resize 252 * _GLIBCXX_PROFILE_DATA(__list_resize_cost_factor).__value); 253 } 254 255 void 256 __opr_find(const void* __obj, std::size_t __size) 257 { 258 __vector2list_info* __res = __get_object_info(__obj); 259 if (__res) 260 __res->__opr_find(__size); 261 } 262 }; 263 264 265 inline void 266 __trace_vector_to_list_init() 267 { _GLIBCXX_PROFILE_DATA(_S_vector_to_list) = new __trace_vector_to_list(); } 268 269 inline void 270 __trace_vector_to_list_report(FILE* __f, __warning_vector_t& __warnings) 271 { 272 if (_GLIBCXX_PROFILE_DATA(_S_vector_to_list)) 273 { 274 _GLIBCXX_PROFILE_DATA(_S_vector_to_list)-> 275 __collect_warnings(__warnings); 276 _GLIBCXX_PROFILE_DATA(_S_vector_to_list)->__write(__f); 277 } 278 } 279 280 inline void 281 __trace_vector_to_list_construct(const void* __obj) 282 { 283 if (!__profcxx_init()) 284 return; 285 286 _GLIBCXX_PROFILE_DATA(_S_vector_to_list)->__insert(__obj, __get_stack()); 287 } 288 289 inline void 290 __trace_vector_to_list_destruct(const void* __obj) 291 { 292 if (!__profcxx_init()) 293 return; 294 295 _GLIBCXX_PROFILE_DATA(_S_vector_to_list)->__destruct(__obj); 296 } 297 298 inline void 299 __trace_vector_to_list_insert(const void* __obj, std::size_t __pos, 300 std::size_t __num) 301 { 302 if (!__profcxx_init()) 303 return; 304 305 _GLIBCXX_PROFILE_DATA(_S_vector_to_list)->__opr_insert(__obj, __pos, 306 __num); 307 } 308 309 inline void 310 __trace_vector_to_list_iterate(const void* __obj, std::size_t __num = 1) 311 { 312 if (!__profcxx_init()) 313 return; 314 315 _GLIBCXX_PROFILE_DATA(_S_vector_to_list)->__opr_iterate(__obj, __num); 316 } 317 318 inline void 319 __trace_vector_to_list_invalid_operator(const void* __obj) 320 { 321 if (!__profcxx_init()) 322 return; 323 324 _GLIBCXX_PROFILE_DATA(_S_vector_to_list)->__invalid_operator(__obj); 325 } 326 327 inline void 328 __trace_vector_to_list_resize(const void* __obj, std::size_t __from, 329 std::size_t __to) 330 { 331 if (!__profcxx_init()) 332 return; 333 334 _GLIBCXX_PROFILE_DATA(_S_vector_to_list)->__resize(__obj, __from, __to); 335 } 336 337 inline void 338 __trace_vector_to_list_find(const void* __obj, std::size_t __size) 339 { 340 if (!__profcxx_init()) 341 return; 342 343 _GLIBCXX_PROFILE_DATA(_S_vector_to_list)->__opr_find(__obj, __size); 344 } 345 346 } // namespace __gnu_profile 347 #endif /* _GLIBCXX_PROFILE_PROFILER_VECTOR_TO_LIST_H */ 348