libstdc++
profiler_container_size.h
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Copyright (C) 2009, 2010, 2011 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_container_size.h
00025  *  @brief Diagnostics for container sizes.
00026  */
00027 
00028 // Written by Lixia Liu and Silvius Rus.
00029 
00030 #ifndef _GLIBCXX_PROFILE_PROFILER_CONTAINER_SIZE_H
00031 #define _GLIBCXX_PROFILE_PROFILER_CONTAINER_SIZE_H 1
00032 
00033 #include <sstream>
00034 
00035 #include "profile/impl/profiler.h"
00036 #include "profile/impl/profiler_node.h"
00037 #include "profile/impl/profiler_trace.h"
00038 
00039 namespace __gnu_profile
00040 {
00041   /** @brief A container size instrumentation line in the object table.  */
00042   class __container_size_info
00043   : public __object_info_base 
00044   {
00045   public:
00046     __container_size_info()
00047     : _M_init(0), _M_max(0), _M_min(0), _M_total(0), _M_item_min(0),
00048       _M_item_max(0), _M_item_total(0), _M_count(0), _M_resize(0), _M_cost(0) 
00049     { }
00050 
00051     __container_size_info(const __container_size_info& __o)
00052     : __object_info_base(__o), _M_init(__o._M_init), _M_max(__o._M_max),
00053       _M_min(__o._M_min), _M_total(__o._M_total),
00054       _M_item_min(__o._M_item_min), _M_item_max(__o._M_item_max),
00055       _M_item_total(__o._M_item_total), _M_count(__o._M_count),
00056       _M_resize(__o._M_resize), _M_cost(__o._M_cost)
00057     { }
00058 
00059     __container_size_info(__stack_t __stack, std::size_t __num)
00060     : __object_info_base(__stack), _M_init(__num), _M_max(__num),
00061       _M_min(0), _M_total(0), _M_item_min(0), _M_item_max(0),
00062       _M_item_total(0), _M_count(0), _M_resize(0), _M_cost(0)
00063     { }
00064 
00065     virtual ~__container_size_info() { }
00066 
00067     void
00068     __write(FILE* __f) const
00069     {
00070       std::fprintf(__f, "%Zu %Zu %Zu %Zu %Zu %Zu %Zu %Zu %Zu %Zu\n", 
00071            _M_init, _M_count, _M_cost, _M_resize, _M_min, _M_max,
00072            _M_total, _M_item_min, _M_item_max, _M_item_total);
00073     }
00074 
00075     float
00076     __magnitude() const
00077     { return static_cast<float>(_M_cost); }
00078 
00079     std::string
00080     __advice() const
00081     {
00082       std::stringstream __message;
00083       if (_M_init < _M_item_max)
00084     __message << "change initial container size from " << _M_init
00085           << " to " << _M_item_max;
00086       return __message.str();
00087     }
00088 
00089     void
00090     __merge(const __container_size_info& __o)
00091     {
00092       _M_init        = std::max(_M_init, __o._M_init);
00093       _M_max         = std::max(_M_max, __o._M_max);
00094       _M_item_max    = std::max(_M_item_max, __o._M_item_max);
00095       _M_min         = std::min(_M_min, __o._M_min);
00096       _M_item_min    = std::min(_M_item_min, __o._M_item_min);
00097       _M_total      += __o._M_total;
00098       _M_item_total += __o._M_item_total;
00099       _M_count      += __o._M_count;
00100       _M_cost       += __o._M_cost;
00101       _M_resize     += __o._M_resize;
00102     }
00103 
00104     // Call if a container is destructed or cleaned.
00105     void
00106     __destruct(std::size_t __num, std::size_t __inum)
00107     {
00108       _M_max = std::max(_M_max, __num);
00109       _M_item_max = std::max(_M_item_max, __inum);
00110       if (_M_min == 0)
00111     {
00112       _M_min = __num; 
00113       _M_item_min = __inum;
00114     }
00115       else
00116     {
00117       _M_min = std::min(_M_min, __num);
00118       _M_item_min = std::min(_M_item_min, __inum);
00119     }
00120       _M_total += __num;
00121       _M_item_total += __inum;
00122       _M_count += 1;
00123     }
00124 
00125     // Estimate the cost of resize/rehash. 
00126     float
00127     __resize_cost(std::size_t __from, std::size_t)
00128     { return __from; }
00129 
00130     // Call if container is resized.
00131     void
00132     __resize(std::size_t __from, std::size_t __to)
00133     {
00134       _M_cost += this->__resize_cost(__from, __to);
00135       _M_resize += 1;
00136       _M_max = std::max(_M_max, __to);
00137     }
00138 
00139   private:
00140     std::size_t _M_init;
00141     std::size_t _M_max;  // range of # buckets
00142     std::size_t _M_min;
00143     std::size_t _M_total;
00144     std::size_t _M_item_min;  // range of # items
00145     std::size_t _M_item_max;
00146     std::size_t _M_item_total;
00147     std::size_t _M_count;
00148     std::size_t _M_resize;
00149     std::size_t _M_cost;
00150   };
00151 
00152 
00153   /** @brief A container size instrumentation line in the stack table.  */
00154   class __container_size_stack_info
00155   : public __container_size_info
00156   {
00157   public:
00158     __container_size_stack_info(const __container_size_info& __o)
00159     : __container_size_info(__o) { }
00160   };
00161 
00162   
00163   /** @brief Container size instrumentation trace producer.  */
00164   class __trace_container_size
00165   : public __trace_base<__container_size_info, __container_size_stack_info> 
00166   {
00167   public:
00168     ~__trace_container_size() { }
00169 
00170     __trace_container_size()
00171     : __trace_base<__container_size_info, __container_size_stack_info>() { };
00172 
00173     // Insert a new node at construct with object, callstack and initial size. 
00174     void
00175     __insert(const __object_t __obj, __stack_t __stack, std::size_t __num)
00176     { __add_object(__obj, __container_size_info(__stack, __num)); }
00177 
00178     // XXX Undefined?
00179     void
00180     __construct(const void* __obj, std::size_t __inum);
00181   
00182     // Call at destruction/clean to set container final size.
00183     void
00184     __destruct(const void* __obj, std::size_t __num, std::size_t __inum)
00185     {
00186       if (!__is_on())
00187     return;
00188 
00189       __object_t __obj_handle = static_cast<__object_t>(__obj);
00190 
00191       __container_size_info* __object_info = __get_object_info(__obj_handle);
00192       if (!__object_info)
00193     return;
00194 
00195       __object_info->__destruct(__num, __inum);
00196       __retire_object(__obj_handle);
00197     }
00198 
00199     // Call at resize to set resize/cost information.
00200     void
00201     __resize(const void* __obj, int __from, int __to)
00202     {
00203       if (!__is_on())
00204     return;
00205 
00206       __container_size_info* __object_info = __get_object_info(__obj);
00207       if (!__object_info)
00208     return;
00209 
00210       __object_info->__resize(__from, __to);
00211     }
00212   };
00213 
00214 } // namespace __gnu_profile
00215 #endif /* _GLIBCXX_PROFILE_PROFILER_CONTAINER_SIZE_H */