libstdc++
profiler_container_size.h
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Copyright (C) 2009-2015 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(__stack_t __stack)
00047     : __object_info_base(__stack), _M_init(0), _M_max(0),
00048       _M_min(0), _M_total(0), _M_item_min(0), _M_item_max(0),
00049       _M_item_total(0), _M_count(0), _M_resize(0), _M_cost(0)
00050     { }
00051 
00052     void
00053     __write(FILE* __f) const
00054     {
00055       std::fprintf(__f, "%Zu %Zu %Zu %Zu %Zu %Zu %Zu %Zu %Zu %Zu\n", 
00056                    _M_init, _M_count, _M_cost, _M_resize, _M_min, _M_max,
00057                    _M_total, _M_item_min, _M_item_max, _M_item_total);
00058     }
00059 
00060     float
00061     __magnitude() const
00062     { return static_cast<float>(_M_cost); }
00063 
00064     std::string
00065     __advice() const
00066     {
00067       std::stringstream __message;
00068       if (_M_init < _M_item_max)
00069         __message << "change initial container size from " << _M_init
00070                   << " to " << _M_item_max;
00071       return __message.str();
00072     }
00073 
00074     void
00075     __init(std::size_t __num)
00076     {
00077       _M_init = __num;
00078       _M_max = __num;
00079     }
00080 
00081     void
00082     __merge(const __container_size_info& __o)
00083     {
00084       __object_info_base::__merge(__o);
00085       _M_init        = std::max(_M_init, __o._M_init);
00086       _M_max         = std::max(_M_max, __o._M_max);
00087       _M_item_max    = std::max(_M_item_max, __o._M_item_max);
00088       _M_min         = std::min(_M_min, __o._M_min);
00089       _M_item_min    = std::min(_M_item_min, __o._M_item_min);
00090       _M_total      += __o._M_total;
00091       _M_item_total += __o._M_item_total;
00092       _M_count      += __o._M_count;
00093       _M_cost       += __o._M_cost;
00094       _M_resize     += __o._M_resize;
00095     }
00096 
00097     // Call if a container is destructed or cleaned.
00098     void
00099     __destruct(std::size_t __num, std::size_t __inum)
00100     {
00101       _M_max = std::max(_M_max, __num);
00102       _M_item_max = std::max(_M_item_max, __inum);
00103       if (_M_min == 0)
00104         {
00105           _M_min = __num; 
00106           _M_item_min = __inum;
00107         }
00108       else
00109         {
00110           _M_min = std::min(_M_min, __num);
00111           _M_item_min = std::min(_M_item_min, __inum);
00112         }
00113 
00114       _M_total += __num;
00115       _M_item_total += __inum;
00116       _M_count += 1;
00117     }
00118 
00119     // Estimate the cost of resize/rehash. 
00120     float
00121     __resize_cost(std::size_t __from, std::size_t)
00122     { return __from; }
00123 
00124     // Call if container is resized.
00125     void
00126     __resize(std::size_t __from, std::size_t __to)
00127     {
00128       _M_cost += this->__resize_cost(__from, __to);
00129       _M_resize += 1;
00130       _M_max = std::max(_M_max, __to);
00131     }
00132 
00133   private:
00134     std::size_t _M_init;
00135     std::size_t _M_max;  // range of # buckets
00136     std::size_t _M_min;
00137     std::size_t _M_total;
00138     std::size_t _M_item_min;  // range of # items
00139     std::size_t _M_item_max;
00140     std::size_t _M_item_total;
00141     std::size_t _M_count;
00142     std::size_t _M_resize;
00143     std::size_t _M_cost;
00144   };
00145 
00146   /** @brief A container size instrumentation line in the stack table.  */
00147   class __container_size_stack_info
00148   : public __container_size_info
00149   {
00150   public:
00151     __container_size_stack_info(const __container_size_info& __o)
00152     : __container_size_info(__o) { }
00153   };
00154   
00155   /** @brief Container size instrumentation trace producer.  */
00156   class __trace_container_size
00157   : public __trace_base<__container_size_info, __container_size_stack_info> 
00158   {
00159   public:
00160     ~__trace_container_size() { }
00161 
00162     __trace_container_size()
00163     : __trace_base<__container_size_info, __container_size_stack_info>() { };
00164 
00165     // Insert a new node at construct with object, callstack and initial size. 
00166     __container_size_info*
00167     __insert(__stack_t __stack, std::size_t __num)
00168     {
00169       __container_size_info* __ret =  __add_object(__stack);
00170       if (__ret)
00171         __ret->__init(__num);
00172       return __ret;
00173     }
00174 
00175     // Call at destruction/clean to set container final size.
00176     void
00177     __destruct(__container_size_info* __obj_info,
00178                std::size_t __num, std::size_t __inum)
00179     {
00180       __obj_info->__destruct(__num, __inum);
00181       __retire_object(__obj_info);
00182     }
00183   };
00184 
00185 } // namespace __gnu_profile
00186 #endif /* _GLIBCXX_PROFILE_PROFILER_CONTAINER_SIZE_H */