Home | History | Annotate | Line # | Download | only in impl
      1 // -*- C++ -*-
      2 //
      3 // Copyright (C) 2009-2019 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(__stack_t __stack)
     45     : __object_info_base(__stack), _M_shift_count(0), _M_iterate(0),
     46       _M_resize(0), _M_list_cost(0), _M_vector_cost(0)
     47     { }
     48 
     49     void
     50     __merge(const __vector2list_info& __o)
     51     {
     52       __object_info_base::__merge(__o);
     53       _M_shift_count  += __o._M_shift_count;
     54       _M_iterate      += __o._M_iterate;
     55       _M_vector_cost  += __o._M_vector_cost;
     56       _M_list_cost    += __o._M_list_cost;
     57       _M_resize       += __o._M_resize;
     58     }
     59 
     60     void
     61     __write(FILE* __f) const
     62     {
     63       std::fprintf(__f, "%Zu %Zu %Zu %.0f %.0f\n", _M_shift_count,
     64 		   _M_resize, _M_iterate, _M_vector_cost, _M_list_cost);
     65     }
     66 
     67     float
     68     __magnitude() const
     69     { return _M_vector_cost - _M_list_cost; }
     70 
     71     std::string
     72     __advice() const
     73     { return "change std::vector to std::list"; }
     74 
     75     std::size_t
     76     __shift_count()
     77     { return _M_shift_count; }
     78 
     79     std::size_t
     80     __iterate()
     81     { return _M_iterate; }
     82 
     83     float
     84     __list_cost()
     85     { return _M_list_cost; }
     86 
     87     std::size_t
     88     __resize()
     89     { return _M_resize; }
     90 
     91     void
     92     __set_list_cost(float __lc)
     93     { _M_list_cost = __lc; }
     94 
     95     void
     96     __set_vector_cost(float __vc)
     97     { _M_vector_cost = __vc; }
     98 
     99     void
    100     __opr_insert(std::size_t __pos, std::size_t __num)
    101     { _M_shift_count += __num - __pos; }
    102 
    103     void
    104     __opr_iterate(int __num)
    105     { __gnu_cxx::__atomic_add(&_M_iterate, __num); }
    106 
    107     void
    108     __resize(std::size_t __from, std::size_t)
    109     { _M_resize += __from; }
    110 
    111   private:
    112     std::size_t _M_shift_count;
    113     mutable _Atomic_word _M_iterate;
    114     std::size_t _M_resize;
    115     float _M_list_cost;
    116     float _M_vector_cost;
    117   };
    118 
    119 
    120   /** @brief A vector-to-list instrumentation line in the stack table.  */
    121   class __vector2list_stack_info
    122   : public __vector2list_info
    123   {
    124   public:
    125     __vector2list_stack_info(const __vector2list_info& __o)
    126     : __vector2list_info(__o) { }
    127   };
    128 
    129 
    130   /** @brief Vector-to-list instrumentation producer.  */
    131   class __trace_vector_to_list
    132   : public __trace_base<__vector2list_info, __vector2list_stack_info>
    133   {
    134   public:
    135     __trace_vector_to_list()
    136     : __trace_base<__vector2list_info, __vector2list_stack_info>()
    137     { __id = "vector-to-list"; }
    138 
    139     ~__trace_vector_to_list() { }
    140 
    141     // Call at destruction/clean to set container final size.
    142     void
    143     __destruct(__vector2list_info* __obj_info)
    144     {
    145       float __vc = __vector_cost(__obj_info->__shift_count(),
    146 				 __obj_info->__iterate(),
    147 				 __obj_info->__resize());
    148       float __lc = __list_cost(__obj_info->__shift_count(),
    149 			       __obj_info->__iterate(),
    150 			       __obj_info->__resize());
    151       __obj_info->__set_vector_cost(__vc);
    152       __obj_info->__set_list_cost(__lc);
    153 
    154       __retire_object(__obj_info);
    155     }
    156 
    157     // Collect cost of operations.
    158     float
    159     __vector_cost(std::size_t __shift, std::size_t __iterate,
    160 		  std::size_t __resize)
    161     {
    162       return (__shift
    163 	      * _GLIBCXX_PROFILE_DATA(__vector_shift_cost_factor).__value
    164 	      + __iterate
    165 	      * _GLIBCXX_PROFILE_DATA(__vector_iterate_cost_factor).__value
    166 	      + __resize
    167 	      * _GLIBCXX_PROFILE_DATA(__vector_resize_cost_factor).__value);
    168     }
    169 
    170     float
    171     __list_cost(std::size_t __shift, std::size_t __iterate,
    172 		std::size_t __resize)
    173     {
    174       return (__shift
    175 	      * _GLIBCXX_PROFILE_DATA(__list_shift_cost_factor).__value
    176 	      + __iterate
    177 	      * _GLIBCXX_PROFILE_DATA(__list_iterate_cost_factor).__value
    178 	      + __resize
    179 	      * _GLIBCXX_PROFILE_DATA(__list_resize_cost_factor).__value);
    180     }
    181   };
    182 
    183 
    184   inline void
    185   __trace_vector_to_list_init()
    186   { _GLIBCXX_PROFILE_DATA(_S_vector_to_list) = new __trace_vector_to_list(); }
    187 
    188   inline void
    189   __trace_vector_to_list_free()
    190   { delete _GLIBCXX_PROFILE_DATA(_S_vector_to_list); }
    191 
    192   inline void
    193   __trace_vector_to_list_report(FILE* __f, __warning_vector_t& __warnings)
    194   { __trace_report(_GLIBCXX_PROFILE_DATA(_S_vector_to_list), __f, __warnings); }
    195 
    196   inline __vector2list_info*
    197   __trace_vector_to_list_construct()
    198   {
    199     if (!__profcxx_init())
    200       return 0;
    201 
    202     if (!__reentrance_guard::__get_in())
    203       return 0;
    204 
    205     __reentrance_guard __get_out;
    206     return _GLIBCXX_PROFILE_DATA(_S_vector_to_list)
    207       ->__add_object(__get_stack());
    208   }
    209 
    210   inline void
    211   __trace_vector_to_list_insert(__vector2list_info* __obj_info,
    212 				std::size_t __pos,
    213 				std::size_t __num)
    214   {
    215     if (!__obj_info)
    216       return;
    217 
    218     __obj_info->__opr_insert(__pos, __num);
    219   }
    220 
    221   inline void
    222   __trace_vector_to_list_iterate(__vector2list_info* __obj_info, int)
    223   {
    224     if (!__obj_info)
    225       return;
    226 
    227     // We only collect if an iteration took place no matter in what side.
    228     __obj_info->__opr_iterate(1);
    229   }
    230 
    231   inline void
    232   __trace_vector_to_list_invalid_operator(__vector2list_info* __obj_info)
    233   {
    234     if (!__obj_info)
    235       return;
    236 
    237     __obj_info->__set_invalid();
    238   }
    239 
    240   inline void
    241   __trace_vector_to_list_resize(__vector2list_info* __obj_info,
    242 				std::size_t __from,
    243 				std::size_t __to)
    244   {
    245     if (!__obj_info)
    246       return;
    247 
    248     __obj_info->__resize(__from, __to);
    249   }
    250 
    251   inline void
    252   __trace_vector_to_list_destruct(__vector2list_info* __obj_info)
    253   {
    254     if (!__obj_info)
    255       return;
    256 
    257     _GLIBCXX_PROFILE_DATA(_S_vector_to_list)->__destruct(__obj_info);
    258   }
    259 
    260 } // namespace __gnu_profile
    261 #endif /* _GLIBCXX_PROFILE_PROFILER_VECTOR_TO_LIST_H */
    262