profiler_list_to_vector.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 terms
00007 // of the GNU General Public License as published by the Free Software
00008 // Foundation; either version 2, or (at your option) any later
00009 // version.
00010 
00011 // This library is distributed in the hope that it will be useful, but
00012 // WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // General Public License for more details.
00015 
00016 // You should have received a copy of the GNU General Public License
00017 // along with this library; see the file COPYING.  If not, write to
00018 // the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
00019 // MA 02111-1307, USA.
00020 
00021 // As a special exception, you may use this file as part of a free
00022 // software library without restriction.  Specifically, if other files
00023 // instantiate templates or use macros or inline functions from this
00024 // file, or you compile this file and link it with other files to
00025 // produce an executable, this file does not by itself cause the
00026 // resulting executable to be covered by the GNU General Public
00027 // License.  This exception does not however invalidate any other
00028 // reasons why the executable file might be covered by the GNU General
00029 // Public License.
00030 
00031 /** @file profile/impl/profiler_list_to_vector.h
00032  *  @brief diagnostics for list to vector.
00033  */
00034 
00035 // Written by Changhee Jung.
00036 
00037 #ifndef _GLIBCXX_PROFILE_PROFILER_LIST_TO_VECTOR_H
00038 #define _GLIBCXX_PROFILE_PROFILER_LIST_TO_VECTOR_H 1
00039 
00040 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00041 #include <cstdio>
00042 #include <cstdlib>
00043 #include <cstring>
00044 #else
00045 #include <stdio.h>
00046 #include <stdint.h>
00047 #include <string.h>
00048 #endif
00049 #include <string>
00050 #include <sstream>
00051 #include "profile/impl/profiler.h"
00052 #include "profile/impl/profiler_node.h"
00053 #include "profile/impl/profiler_trace.h"
00054 
00055 namespace __gnu_profile
00056 {
00057 
00058 /** @brief A list-to-vector instrumentation line in the object table.  */
00059 class __list2vector_info: public __object_info_base
00060 {
00061  public:
00062   __list2vector_info()
00063       :_M_shift_count(0), _M_iterate(0), _M_resize(0), _M_list_cost(0),
00064        _M_vector_cost(0), _M_valid(true), _M_max_size(0) {}
00065   __list2vector_info(__stack_t __stack)
00066       : __object_info_base(__stack), _M_shift_count(0), _M_iterate(0),
00067         _M_resize(0), _M_list_cost(0), _M_vector_cost(0), _M_valid(true),
00068         _M_max_size(0) {}
00069   virtual ~__list2vector_info() {}
00070   __list2vector_info(const __list2vector_info& __o);
00071   void __merge(const __list2vector_info& __o);
00072   void __write(FILE* __f) const;
00073   float __magnitude() const { return _M_list_cost - _M_vector_cost; }
00074   const char* __advice() const;
00075   size_t __shift_count() { return _M_shift_count; }
00076   size_t __iterate()   { return _M_iterate; }
00077   float __list_cost() { return _M_list_cost; }
00078   size_t __resize() { return _M_resize; }
00079   void __set_list_cost(float __lc) { _M_list_cost = __lc; }
00080   void __set_vector_cost(float __vc) { _M_vector_cost = __vc; }
00081   bool __is_valid() { return _M_valid; }
00082   void __set_invalid() { _M_valid = false; }
00083 
00084   void __opr_insert(size_t __shift, size_t __size);
00085   void __opr_iterate(size_t __num) { _M_iterate += __num;}
00086 
00087   void __resize(size_t __from, size_t __to);
00088 
00089 private:
00090   size_t _M_shift_count;
00091   size_t _M_iterate;
00092   size_t _M_resize;
00093   float _M_list_cost;
00094   float _M_vector_cost;
00095   bool  _M_valid;
00096   size_t _M_max_size;
00097 };
00098 
00099 inline __list2vector_info::__list2vector_info(const __list2vector_info& __o)
00100     : __object_info_base(__o)
00101 {
00102   _M_shift_count  = __o._M_shift_count;
00103   _M_iterate      = __o._M_iterate;
00104   _M_vector_cost  = __o._M_vector_cost;
00105   _M_list_cost    = __o._M_list_cost;
00106   _M_valid        = __o._M_valid;
00107   _M_resize       = __o._M_resize;
00108   _M_max_size     = __o._M_max_size;
00109 }
00110 
00111 inline const char* __list2vector_info::__advice() const {
00112   std::stringstream __sstream;
00113   __sstream 
00114       << "change std::list to std::vector and its initial size from 0 to "
00115       << _M_max_size;
00116   return strdup(__sstream.str().c_str());
00117 }
00118 
00119 inline void __list2vector_info::__merge(const __list2vector_info& __o)
00120 {
00121   _M_shift_count  += __o._M_shift_count;
00122   _M_iterate      += __o._M_iterate;
00123   _M_vector_cost  += __o._M_vector_cost;
00124   _M_list_cost    += __o._M_list_cost;
00125   _M_valid        &= __o._M_valid;
00126   _M_resize       += __o._M_resize;
00127   _M_max_size     = std::max( _M_max_size, __o._M_max_size);
00128 }
00129 
00130 inline void __list2vector_info::__opr_insert(size_t __shift, size_t __size) 
00131 {
00132   _M_shift_count += __shift;
00133   _M_max_size = std::max(_M_max_size, __size);
00134 }
00135 
00136 inline void __list2vector_info::__resize(size_t __from, size_t __to)
00137 {
00138   _M_resize += __from;
00139 }
00140 
00141 class __list2vector_stack_info: public __list2vector_info {
00142  public:
00143   __list2vector_stack_info(const __list2vector_info& __o) 
00144       : __list2vector_info(__o) {}
00145 };
00146 
00147 class __trace_list_to_vector
00148     : public __trace_base<__list2vector_info, __list2vector_stack_info> 
00149 {
00150  public:
00151   __trace_list_to_vector();
00152   ~__trace_list_to_vector() {}
00153 
00154   // Insert a new node at construct with object, callstack and initial size. 
00155   void __insert(__object_t __obj, __stack_t __stack);
00156   // Call at destruction/clean to set container final size.
00157   void __destruct(const void* __obj);
00158 
00159   // Find the node in the live map.
00160   __list2vector_info* __find(const void* __obj);
00161 
00162   // Collect cost of operations.
00163   void __opr_insert(const void* __obj, size_t __shift, size_t __size);
00164   void __opr_iterate(const void* __obj, size_t __num);
00165   void __invalid_operator(const void* __obj);
00166   void __resize(const void* __obj, size_t __from, size_t __to);
00167   float __vector_cost(size_t __shift, size_t __iterate);
00168   float __list_cost(size_t __shift, size_t __iterate);
00169 };
00170 
00171 inline __trace_list_to_vector::__trace_list_to_vector()
00172     : __trace_base<__list2vector_info, __list2vector_stack_info>()
00173 {
00174   __id = "list-to-vector";
00175 }
00176 
00177 inline void __trace_list_to_vector::__insert(__object_t __obj,
00178                                              __stack_t __stack)
00179 {
00180   __add_object(__obj, __list2vector_info(__stack));
00181 }
00182 
00183 inline void __list2vector_info::__write(FILE* __f) const
00184 {
00185   fprintf(__f, "%Zu %Zu %Zu %.0f %.0f\n",
00186           _M_shift_count, _M_resize, _M_iterate, _M_vector_cost, _M_list_cost);
00187 }
00188 
00189 inline float __trace_list_to_vector::__vector_cost(size_t __shift, 
00190                                                    size_t __iterate)
00191 {
00192   // The resulting vector will use a 'reserve' method.
00193   return __shift * _GLIBCXX_PROFILE_DATA(__vector_shift_cost_factor).__value + 
00194       __iterate * _GLIBCXX_PROFILE_DATA(__vector_iterate_cost_factor).__value; 
00195 }
00196 
00197 inline float __trace_list_to_vector::__list_cost(size_t __shift, 
00198                                                  size_t __iterate)
00199 {
00200   return __shift * _GLIBCXX_PROFILE_DATA(__list_shift_cost_factor).__value + 
00201       __iterate * _GLIBCXX_PROFILE_DATA(__list_iterate_cost_factor).__value; 
00202 }
00203 
00204 inline void __trace_list_to_vector::__destruct(const void* __obj)
00205 {
00206   if (!__is_on())
00207     return;
00208 
00209  __list2vector_info* __res = __get_object_info(__obj);
00210   if (!__res)
00211     return;
00212 
00213   float __vc = __vector_cost(__res->__shift_count(), __res->__iterate());
00214   float __lc = __list_cost(__res->__shift_count(), __res->__iterate());
00215   __res->__set_vector_cost(__vc);
00216   __res->__set_list_cost(__lc);
00217   __retire_object(__obj);
00218 }
00219 
00220 inline void __trace_list_to_vector::__opr_insert(const void* __obj, 
00221                                                  size_t __shift, size_t __size)
00222 {
00223   __list2vector_info* __res = __get_object_info(__obj);
00224   if (__res)
00225     __res->__opr_insert(__shift, __size);
00226 }
00227 
00228 inline void __trace_list_to_vector::__opr_iterate(const void* __obj,
00229                                                   size_t __num)
00230 {
00231   __list2vector_info* __res = __get_object_info(__obj);
00232   if (__res) {
00233     __res->__opr_iterate(__num);
00234   }
00235 }
00236 
00237 inline void __trace_list_to_vector::__invalid_operator(const void* __obj)
00238 {
00239   __list2vector_info* __res = __get_object_info(__obj);
00240   if (__res)
00241     __res->__set_invalid();
00242 }
00243 
00244 inline void __trace_list_to_vector::__resize(const void* __obj, size_t __from, 
00245                                              size_t __to)
00246 {
00247   __list2vector_info* __res = __get_object_info(__obj);
00248   if (__res)
00249     __res->__resize(__from, __to);
00250 }
00251 
00252 inline void __trace_list_to_vector_init()
00253 {
00254   _GLIBCXX_PROFILE_DATA(_S_list_to_vector) = new __trace_list_to_vector();
00255 }
00256 
00257 inline void __trace_list_to_vector_report(FILE* __f, 
00258                                           __warning_vector_t& __warnings)
00259 {
00260   if (_GLIBCXX_PROFILE_DATA(_S_list_to_vector)) {
00261     _GLIBCXX_PROFILE_DATA(_S_list_to_vector)->__collect_warnings(__warnings);
00262     _GLIBCXX_PROFILE_DATA(_S_list_to_vector)->__write(__f);
00263   }
00264 }
00265 
00266 inline void __trace_list_to_vector_construct(const void* __obj)
00267 {
00268   if (!__profcxx_init()) return;
00269 
00270   _GLIBCXX_PROFILE_DATA(_S_list_to_vector)->__insert(__obj, __get_stack());
00271 }
00272 
00273 inline void __trace_list_to_vector_destruct(const void* __obj)
00274 {
00275   if (!__profcxx_init()) return;
00276 
00277   _GLIBCXX_PROFILE_DATA(_S_list_to_vector)->__destruct(__obj);
00278 }
00279 
00280 inline void __trace_list_to_vector_insert(const void* __obj, 
00281                                           size_t __shift, size_t __size)
00282 {
00283   if (!__profcxx_init()) return;
00284 
00285   _GLIBCXX_PROFILE_DATA(_S_list_to_vector)->__opr_insert(__obj, __shift, 
00286                                                          __size);
00287 }
00288 
00289 
00290 inline void __trace_list_to_vector_iterate(const void* __obj, size_t __num = 1)
00291 {
00292   if (!__profcxx_init()) return;
00293 
00294   _GLIBCXX_PROFILE_DATA(_S_list_to_vector)->__opr_iterate(__obj, __num);
00295 }
00296 
00297 inline void __trace_list_to_vector_invalid_operator(const void* __obj)
00298 {
00299   if (!__profcxx_init()) return;
00300 
00301   _GLIBCXX_PROFILE_DATA(_S_list_to_vector)->__invalid_operator(__obj);
00302 }
00303 
00304 inline void __trace_list_to_vector_resize(const void* __obj, 
00305                                           size_t __from, size_t __to)
00306 {
00307   if (!__profcxx_init()) return;
00308 
00309   _GLIBCXX_PROFILE_DATA(_S_list_to_vector)->__resize(__obj, __from, __to);
00310 }
00311 
00312 } // namespace __gnu_profile
00313 #endif /* _GLIBCXX_PROFILE_PROFILER_LIST_TO_VECTOR_H__ */