libstdc++
profiler_map_to_unordered_map.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_map_to_unordered_map.h
00025  *  @brief Diagnostics for map to unordered_map.
00026  */
00027 
00028 // Written by Silvius Rus.
00029 
00030 #ifndef _GLIBCXX_PROFILE_PROFILER_MAP_TO_UNORDERED_MAP_H
00031 #define _GLIBCXX_PROFILE_PROFILER_MAP_TO_UNORDERED_MAP_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   inline int
00040   __log2(std::size_t __size)
00041   {
00042     for (int __bit_count = sizeof(std::size_t) - 1; __bit_count >= 0;
00043      -- __bit_count) 
00044       if ((2 << __bit_count) & __size)
00045     return __bit_count;
00046     return 0;
00047   }
00048 
00049   inline float
00050   __map_insert_cost(std::size_t __size)
00051   { return (_GLIBCXX_PROFILE_DATA(__map_insert_cost_factor).__value 
00052         * static_cast<float>(__log2(__size))); }
00053 
00054   inline float
00055   __map_erase_cost(std::size_t __size)
00056   { return (_GLIBCXX_PROFILE_DATA(__map_erase_cost_factor).__value
00057         * static_cast<float>(__log2(__size))); }
00058 
00059   inline float
00060   __map_find_cost(std::size_t __size)
00061   { return (_GLIBCXX_PROFILE_DATA(__map_find_cost_factor).__value
00062         * static_cast<float>(__log2(__size))); }
00063 
00064   /** @brief A map-to-unordered_map instrumentation line in the 
00065       object table.  */
00066   class __map2umap_info
00067   : public __object_info_base
00068   {
00069   public:
00070     __map2umap_info()
00071     : _M_insert(0), _M_erase(0), _M_find(0), _M_iterate(0),
00072       _M_umap_cost(0.0), _M_map_cost(0.0), _M_valid(true) { }
00073     
00074     __map2umap_info(__stack_t __stack)
00075     : __object_info_base(__stack), _M_insert(0), _M_erase(0), _M_find(0), 
00076       _M_iterate(0), _M_umap_cost(0.0), _M_map_cost(0.0), _M_valid(true) { }
00077 
00078     virtual ~__map2umap_info() { }
00079 
00080     __map2umap_info(const __map2umap_info& __o)
00081     : __object_info_base(__o), _M_insert(__o._M_insert),
00082       _M_erase(__o._M_erase), _M_find(__o._M_find),
00083       _M_iterate(__o._M_iterate), _M_umap_cost(__o._M_umap_cost),
00084       _M_map_cost(__o._M_map_cost), _M_valid(__o._M_valid) { }
00085 
00086     void
00087     __merge(const __map2umap_info& __o)
00088     {
00089       _M_insert    += __o._M_insert;
00090       _M_erase     += __o._M_erase;
00091       _M_find      += __o._M_find;
00092       _M_umap_cost += __o._M_umap_cost;
00093       _M_map_cost  += __o._M_map_cost;
00094       _M_valid     &= __o._M_valid;
00095     }
00096 
00097     void
00098     __write(FILE* __f) const
00099     {
00100       std::fprintf(__f, "%Zu %Zu %Zu %Zu %.0f %.0f %s\n",
00101            _M_insert, _M_erase, _M_find, _M_iterate, _M_map_cost,
00102            _M_umap_cost, _M_valid ? "valid" : "invalid");
00103     }
00104 
00105     float
00106     __magnitude() const
00107     { return _M_map_cost - _M_umap_cost; }
00108 
00109     std::string
00110     __advice() const
00111     { return "change std::map to std::unordered_map"; }
00112 
00113     void
00114     __record_insert(std::size_t __size, std::size_t __count)
00115     {
00116       _M_insert += __count;
00117       _M_map_cost += __count * __map_insert_cost(__size);
00118       _M_umap_cost
00119     += (__count
00120         * _GLIBCXX_PROFILE_DATA(__umap_insert_cost_factor).__value);
00121     }
00122 
00123     void
00124     __record_erase(std::size_t __size, std::size_t __count)
00125     {
00126       _M_erase += __count;
00127       _M_map_cost += __count * __map_erase_cost(__size);
00128       _M_umap_cost
00129     += (__count
00130         * _GLIBCXX_PROFILE_DATA(__umap_erase_cost_factor).__value);
00131     }
00132 
00133     void
00134     __record_find(std::size_t __size)
00135     {
00136       _M_find += 1;
00137       _M_map_cost += __map_find_cost(__size);
00138       _M_umap_cost += _GLIBCXX_PROFILE_DATA(__umap_find_cost_factor).__value;
00139     }
00140 
00141     void
00142     __record_iterate(std::size_t __count)
00143     {
00144       _M_iterate += __count;
00145       _M_map_cost
00146     += (__count
00147         * _GLIBCXX_PROFILE_DATA(__map_iterate_cost_factor).__value);
00148       _M_umap_cost
00149     += (__count
00150         * _GLIBCXX_PROFILE_DATA(__umap_iterate_cost_factor).__value);
00151     }
00152 
00153     void
00154     __record_invalidate()
00155     { _M_valid = false; }
00156 
00157   private:
00158     std::size_t _M_insert;
00159     std::size_t _M_erase;
00160     std::size_t _M_find;
00161     std::size_t _M_iterate;
00162     float _M_umap_cost;
00163     float _M_map_cost;
00164     bool  _M_valid;
00165   };
00166 
00167 
00168   /** @brief A map-to-unordered_map instrumentation line in the 
00169       stack table.  */
00170   class __map2umap_stack_info 
00171   : public __map2umap_info
00172   {
00173   public:
00174     __map2umap_stack_info(const __map2umap_info& __o)
00175     : __map2umap_info(__o) { }
00176   };
00177 
00178   /** @brief Map-to-unordered_map instrumentation producer.  */
00179   class __trace_map2umap
00180   : public __trace_base<__map2umap_info, __map2umap_stack_info> 
00181   {
00182   public:
00183     __trace_map2umap()
00184     : __trace_base<__map2umap_info, __map2umap_stack_info>()
00185     { __id = "map-to-unordered-map"; }
00186   };
00187 
00188   inline void
00189   __trace_map_to_unordered_map_init()
00190   { _GLIBCXX_PROFILE_DATA(_S_map2umap) = new __trace_map2umap(); }
00191 
00192   inline void
00193   __trace_map_to_unordered_map_report(FILE* __f,
00194                       __warning_vector_t& __warnings)
00195   {
00196     if (_GLIBCXX_PROFILE_DATA(_S_map2umap)) 
00197       {
00198     _GLIBCXX_PROFILE_DATA(_S_map2umap)->__collect_warnings(__warnings);
00199     _GLIBCXX_PROFILE_DATA(_S_map2umap)->__write(__f);
00200       }
00201   }
00202 
00203   inline void
00204   __trace_map_to_unordered_map_construct(const void* __obj)
00205   {
00206     if (!__profcxx_init())
00207       return;
00208 
00209     _GLIBCXX_PROFILE_DATA(_S_map2umap)->
00210       __add_object(__obj, __map2umap_info(__get_stack()));
00211   }
00212 
00213   inline void
00214   __trace_map_to_unordered_map_destruct(const void* __obj)
00215   {
00216     if (!__profcxx_init())
00217       return;
00218 
00219     _GLIBCXX_PROFILE_DATA(_S_map2umap)->__retire_object(__obj);
00220   }
00221 
00222   inline void
00223   __trace_map_to_unordered_map_insert(const void* __obj, 
00224                       std::size_t __size, std::size_t __count)
00225   {
00226     if (!__profcxx_init())
00227       return;
00228 
00229     __map2umap_info* __info
00230       = _GLIBCXX_PROFILE_DATA(_S_map2umap)->__get_object_info(__obj);
00231 
00232     if (__info)
00233       __info->__record_insert(__size, __count);
00234   }
00235 
00236   inline void
00237   __trace_map_to_unordered_map_erase(const void* __obj, 
00238                      std::size_t __size, std::size_t __count)
00239   {
00240     if (!__profcxx_init()) 
00241       return;
00242 
00243     __map2umap_info* __info 
00244       = _GLIBCXX_PROFILE_DATA(_S_map2umap)->__get_object_info(__obj);
00245 
00246     if (__info)
00247       __info->__record_erase(__size, __count);
00248   }
00249 
00250   inline void
00251   __trace_map_to_unordered_map_find(const void* __obj, std::size_t __size)
00252   {
00253     if (!__profcxx_init())
00254       return;
00255 
00256     __map2umap_info* __info
00257       = _GLIBCXX_PROFILE_DATA(_S_map2umap)->__get_object_info(__obj);
00258 
00259     if (__info)
00260       __info->__record_find(__size);
00261   }
00262 
00263   inline void
00264   __trace_map_to_unordered_map_iterate(const void* __obj, std::size_t __count)
00265   {
00266     if (!__profcxx_init())
00267       return;
00268 
00269     __map2umap_info* __info
00270       = _GLIBCXX_PROFILE_DATA(_S_map2umap)->__get_object_info(__obj);
00271     
00272     if (__info)
00273       __info->__record_iterate(__count);
00274   }
00275 
00276   inline void
00277   __trace_map_to_unordered_map_invalidate(const void* __obj)
00278   {
00279     if (!__profcxx_init())
00280       return;
00281 
00282     __map2umap_info* __info
00283       = _GLIBCXX_PROFILE_DATA(_S_map2umap)->__get_object_info(__obj);
00284 
00285     if (__info)
00286       __info->__record_invalidate();
00287   }
00288 
00289 } // namespace __gnu_profile
00290 #endif /* _GLIBCXX_PROFILE_PROFILER_MAP_TO_UNORDERED_MAP_H */