libstdc++
profiler_vector_to_list.h
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Copyright (C) 2009, 2010 Free Software Foundation, Inc.
00004 //
00005 // This file is part of the GNU ISO C++ Library.  This library is free
00006 // software; you can redistribute it and/or modify it under the
00007 // terms of the GNU General Public License as published by the
00008 // Free Software Foundation; either version 3, or (at your option)
00009 // any later version.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 
00016 // Under Section 7 of GPL version 3, you are granted additional
00017 // permissions described in the GCC Runtime Library Exception, version
00018 // 3.1, as published by the Free Software Foundation.
00019 
00020 // You should have received a copy of the GNU General Public License along
00021 // with this library; see the file COPYING3.  If not see
00022 // <http://www.gnu.org/licenses/>.
00023 
00024 /** @file profile/impl/profiler_vector_to_list.h
00025  *  @brief diagnostics for vector to list.
00026  */
00027 
00028 // Written by Lixia Liu and Silvius Rus.
00029 
00030 #ifndef _GLIBCXX_PROFILE_PROFILER_VECTOR_TO_LIST_H
00031 #define _GLIBCXX_PROFILE_PROFILER_VECTOR_TO_LIST_H 1
00032 
00033 #include "profile/impl/profiler.h"
00034 #include "profile/impl/profiler_node.h"
00035 #include "profile/impl/profiler_trace.h"
00036 
00037 namespace __gnu_profile
00038 {
00039   /** @brief A vector-to-list instrumentation line in the object table.  */
00040   class __vector2list_info
00041   : public __object_info_base
00042   {
00043   public:
00044     __vector2list_info()
00045     : _M_shift_count(0), _M_iterate(0), _M_resize(0), _M_list_cost(0),
00046       _M_vector_cost(0), _M_valid(true) { }
00047 
00048     __vector2list_info(__stack_t __stack)
00049     : __object_info_base(__stack), _M_shift_count(0), _M_iterate(0),
00050       _M_resize(0), _M_list_cost(0), _M_vector_cost(0), _M_valid(true) { }
00051 
00052     virtual ~__vector2list_info() { }
00053 
00054     __vector2list_info(const __vector2list_info& __o)
00055     : __object_info_base(__o), _M_shift_count(__o._M_shift_count),
00056       _M_iterate(__o._M_iterate), _M_resize(__o._M_resize),
00057       _M_list_cost(__o._M_list_cost), _M_vector_cost(__o._M_vector_cost),
00058       _M_valid(__o._M_valid) { }
00059 
00060     void
00061     __merge(const __vector2list_info& __o)
00062     {
00063       _M_shift_count  += __o._M_shift_count;
00064       _M_iterate      += __o._M_iterate;
00065       _M_vector_cost  += __o._M_vector_cost;
00066       _M_list_cost    += __o._M_list_cost;
00067       _M_valid        &= __o._M_valid;
00068       _M_resize       += __o._M_resize;
00069     }
00070 
00071     void
00072     __write(FILE* __f) const
00073     {
00074       std::fprintf(__f, "%Zu %Zu %Zu %.0f %.0f\n", _M_shift_count,
00075            _M_resize, _M_iterate, _M_vector_cost, _M_list_cost);
00076     }
00077 
00078     float
00079     __magnitude() const
00080     { return _M_vector_cost - _M_list_cost; }
00081 
00082     std::string
00083     __advice() const 
00084     { return "change std::vector to std::list"; }
00085 
00086     std::size_t
00087     __shift_count()
00088     { return _M_shift_count; }
00089 
00090     std::size_t
00091     __iterate()
00092     { return _M_iterate; }
00093 
00094     float
00095     __list_cost()
00096     { return _M_list_cost; }
00097 
00098     std::size_t
00099     __resize()
00100     { return _M_resize; }
00101 
00102     void
00103     __set_list_cost(float __lc)
00104     { _M_list_cost = __lc; }
00105     
00106     void
00107     __set_vector_cost(float __vc)
00108     { _M_vector_cost = __vc; }
00109     
00110     bool
00111     __is_valid()
00112     { return _M_valid; }
00113     
00114     void
00115     __set_invalid()
00116     { _M_valid = false; }
00117 
00118     void
00119     __opr_insert(std::size_t __pos, std::size_t __num)
00120     { _M_shift_count += __num - __pos; }
00121 
00122     void
00123     __opr_iterate(std::size_t __num)
00124     { _M_iterate += __num; }
00125 
00126     void
00127     __resize(std::size_t __from, std::size_t)
00128     { _M_resize += __from; }
00129 
00130     void
00131     __opr_find(std::size_t __size)
00132     {
00133       // Use average case complexity.
00134       _M_iterate += 3.0 / 4.0 * __size;
00135     }
00136 
00137   private:
00138     std::size_t _M_shift_count;
00139     std::size_t _M_iterate;
00140     std::size_t _M_resize;
00141     float _M_list_cost;
00142     float _M_vector_cost;
00143     bool  _M_valid;
00144   };
00145 
00146 
00147   /** @brief A vector-to-list instrumentation line in the stack table.  */
00148   class __vector2list_stack_info
00149   : public __vector2list_info
00150   {
00151   public:
00152     __vector2list_stack_info(const __vector2list_info& __o) 
00153     : __vector2list_info(__o) { }
00154   };
00155 
00156 
00157   /** @brief Vector-to-list instrumentation producer.  */
00158   class __trace_vector_to_list
00159   : public __trace_base<__vector2list_info, __vector2list_stack_info> 
00160   {
00161   public:
00162     __trace_vector_to_list()
00163     : __trace_base<__vector2list_info, __vector2list_stack_info>()
00164     { __id = "vector-to-list"; }
00165 
00166     ~__trace_vector_to_list() { }
00167 
00168     // Insert a new node at construct with object, callstack and initial size. 
00169     void
00170     __insert(__object_t __obj, __stack_t __stack)
00171     { __add_object(__obj, __vector2list_info(__stack)); }
00172 
00173     // Call at destruction/clean to set container final size.
00174     void
00175     __destruct(const void* __obj)
00176     {
00177       if (!__is_on())
00178     return;
00179 
00180       __vector2list_info* __res = __get_object_info(__obj);
00181       if (!__res)
00182     return;
00183 
00184       float __vc = __vector_cost(__res->__shift_count(), __res->__iterate(),
00185                  __res->__resize());
00186       float __lc = __list_cost(__res->__shift_count(), __res->__iterate(),
00187                    __res->__resize());
00188       __res->__set_vector_cost(__vc);
00189       __res->__set_list_cost(__lc);
00190 
00191       __retire_object(__obj);
00192     }
00193 
00194     // Find the node in the live map.
00195     // XXX Undefined?!?
00196     __vector2list_info* __find(const void* __obj);
00197 
00198     // Collect cost of operations.
00199     void
00200     __opr_insert(const void* __obj, std::size_t __pos, std::size_t __num)
00201     {
00202       __vector2list_info* __res = __get_object_info(__obj);
00203       if (__res)
00204     __res->__opr_insert(__pos, __num);
00205     }
00206 
00207     void
00208     __opr_iterate(const void* __obj, std::size_t __num)
00209     {
00210       __vector2list_info* __res = __get_object_info(__obj);
00211       if (__res)
00212     __res->__opr_iterate(__num);
00213     }
00214 
00215     void
00216     __invalid_operator(const void* __obj)
00217     {
00218       __vector2list_info* __res = __get_object_info(__obj);
00219       if (__res)
00220     __res->__set_invalid();
00221     }
00222 
00223     void
00224     __resize(const void* __obj, std::size_t __from, std::size_t __to)
00225     {
00226       __vector2list_info* __res = __get_object_info(__obj);
00227       if (__res)
00228     __res->__resize(__from, __to);
00229     }
00230 
00231     float
00232     __vector_cost(std::size_t __shift, std::size_t __iterate,
00233           std::size_t __resize)
00234     {
00235       return (__shift
00236           * _GLIBCXX_PROFILE_DATA(__vector_shift_cost_factor).__value
00237           + __iterate
00238           * _GLIBCXX_PROFILE_DATA(__vector_iterate_cost_factor).__value
00239           + __resize
00240           * _GLIBCXX_PROFILE_DATA(__vector_resize_cost_factor).__value);
00241     }
00242 
00243     float
00244     __list_cost(std::size_t __shift, std::size_t __iterate,
00245         std::size_t __resize)
00246     {
00247       return (__shift
00248           * _GLIBCXX_PROFILE_DATA(__list_shift_cost_factor).__value
00249           + __iterate
00250           * _GLIBCXX_PROFILE_DATA(__list_iterate_cost_factor).__value
00251           + __resize
00252           * _GLIBCXX_PROFILE_DATA(__list_resize_cost_factor).__value);
00253     }
00254 
00255     void
00256     __opr_find(const void* __obj, std::size_t __size)
00257     {
00258       __vector2list_info* __res = __get_object_info(__obj);
00259       if (__res)
00260     __res->__opr_find(__size);
00261     }
00262   };
00263 
00264 
00265   inline void
00266   __trace_vector_to_list_init()
00267   { _GLIBCXX_PROFILE_DATA(_S_vector_to_list) = new __trace_vector_to_list(); }
00268 
00269   inline void
00270   __trace_vector_to_list_report(FILE* __f, __warning_vector_t& __warnings)
00271   {
00272     if (_GLIBCXX_PROFILE_DATA(_S_vector_to_list))
00273       {
00274     _GLIBCXX_PROFILE_DATA(_S_vector_to_list)->
00275       __collect_warnings(__warnings);
00276     _GLIBCXX_PROFILE_DATA(_S_vector_to_list)->__write(__f);
00277       }
00278   }
00279 
00280   inline void
00281   __trace_vector_to_list_construct(const void* __obj)
00282   {
00283     if (!__profcxx_init())
00284       return;
00285 
00286     _GLIBCXX_PROFILE_DATA(_S_vector_to_list)->__insert(__obj, __get_stack());
00287   }
00288 
00289   inline void
00290   __trace_vector_to_list_destruct(const void* __obj)
00291   {
00292     if (!__profcxx_init())
00293       return;
00294 
00295     _GLIBCXX_PROFILE_DATA(_S_vector_to_list)->__destruct(__obj);
00296   }
00297 
00298   inline void
00299   __trace_vector_to_list_insert(const void* __obj, std::size_t __pos,
00300                 std::size_t __num)
00301   {
00302     if (!__profcxx_init())
00303       return;
00304 
00305     _GLIBCXX_PROFILE_DATA(_S_vector_to_list)->__opr_insert(__obj, __pos,
00306                                __num);
00307   }
00308 
00309   inline void
00310   __trace_vector_to_list_iterate(const void* __obj, std::size_t __num = 1)
00311   {
00312     if (!__profcxx_init())
00313       return;
00314 
00315     _GLIBCXX_PROFILE_DATA(_S_vector_to_list)->__opr_iterate(__obj, __num);
00316   }
00317 
00318   inline void
00319   __trace_vector_to_list_invalid_operator(const void* __obj)
00320   {
00321     if (!__profcxx_init())
00322       return;
00323 
00324     _GLIBCXX_PROFILE_DATA(_S_vector_to_list)->__invalid_operator(__obj);
00325   }
00326 
00327   inline void
00328   __trace_vector_to_list_resize(const void* __obj, std::size_t __from,
00329                 std::size_t __to)
00330   {
00331     if (!__profcxx_init())
00332       return;
00333 
00334     _GLIBCXX_PROFILE_DATA(_S_vector_to_list)->__resize(__obj, __from, __to);
00335   }
00336 
00337   inline void
00338   __trace_vector_to_list_find(const void* __obj, std::size_t __size)
00339   {
00340     if (!__profcxx_init())
00341       return;
00342 
00343     _GLIBCXX_PROFILE_DATA(_S_vector_to_list)->__opr_find(__obj, __size);
00344   }
00345 
00346 } // namespace __gnu_profile
00347 #endif /* _GLIBCXX_PROFILE_PROFILER_VECTOR_TO_LIST_H */