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_hash_func.h 25 * @brief Data structures to represent profiling traces. 26 */ 27 28 // Written by Lixia Liu and Silvius Rus. 29 30 #ifndef _GLIBCXX_PROFILE_PROFILER_HASH_FUNC_H 31 #define _GLIBCXX_PROFILE_PROFILER_HASH_FUNC_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 hash performance instrumentation line in the object table. */ 40 class __hashfunc_info 41 : public __object_info_base 42 { 43 public: __hashfunc_info(__stack_t __stack)44 __hashfunc_info(__stack_t __stack) 45 : __object_info_base(__stack), _M_longest_chain(0), 46 _M_accesses(0), _M_hops(0) { } 47 48 void __merge(const __hashfunc_info & __o)49 __merge(const __hashfunc_info& __o) 50 { 51 __object_info_base::__merge(__o); 52 _M_longest_chain = std::max(_M_longest_chain, __o._M_longest_chain); 53 _M_accesses += __o._M_accesses; 54 _M_hops += __o._M_hops; 55 } 56 57 void __destruct(std::size_t __chain,std::size_t __accesses,std::size_t __hops)58 __destruct(std::size_t __chain, std::size_t __accesses, 59 std::size_t __hops) 60 { 61 _M_longest_chain = std::max(_M_longest_chain, __chain); 62 _M_accesses += __accesses; 63 _M_hops += __hops; 64 } 65 66 void __write(FILE * __f)67 __write(FILE* __f) const 68 { std::fprintf(__f, "%Zu %Zu %Zu\n", _M_hops, 69 _M_accesses, _M_longest_chain); } 70 71 float __magnitude()72 __magnitude() const 73 { return static_cast<float>(_M_hops); } 74 75 std::string __advice()76 __advice() const 77 { return "change hash function"; } 78 79 private: 80 std::size_t _M_longest_chain; 81 std::size_t _M_accesses; 82 std::size_t _M_hops; 83 }; 84 85 /** @brief A hash performance instrumentation line in the stack table. */ 86 class __hashfunc_stack_info 87 : public __hashfunc_info 88 { 89 public: __hashfunc_stack_info(const __hashfunc_info & __o)90 __hashfunc_stack_info(const __hashfunc_info& __o) 91 : __hashfunc_info(__o) { } 92 }; 93 94 /** @brief Hash performance instrumentation producer. */ 95 class __trace_hash_func 96 : public __trace_base<__hashfunc_info, __hashfunc_stack_info> 97 { 98 public: __trace_hash_func()99 __trace_hash_func() 100 : __trace_base<__hashfunc_info, __hashfunc_stack_info>() 101 { __id = "hash-distr"; } 102 ~__trace_hash_func()103 ~__trace_hash_func() {} 104 105 // Call at destruction/clean to set container final size. 106 void __destruct(__hashfunc_info * __obj_info,std::size_t __chain,std::size_t __accesses,std::size_t __hops)107 __destruct(__hashfunc_info* __obj_info, 108 std::size_t __chain, std::size_t __accesses, std::size_t __hops) 109 { 110 if (!__obj_info) 111 return; 112 113 __obj_info->__destruct(__chain, __accesses, __hops); 114 __retire_object(__obj_info); 115 } 116 }; 117 118 inline void __trace_hash_func_init()119 __trace_hash_func_init() 120 { _GLIBCXX_PROFILE_DATA(_S_hash_func) = new __trace_hash_func(); } 121 122 inline void __trace_hash_func_free()123 __trace_hash_func_free() 124 { delete _GLIBCXX_PROFILE_DATA(_S_hash_func); } 125 126 inline void __trace_hash_func_report(FILE * __f,__warning_vector_t & __warnings)127 __trace_hash_func_report(FILE* __f, __warning_vector_t& __warnings) 128 { __trace_report(_GLIBCXX_PROFILE_DATA(_S_hash_func), __f, __warnings); } 129 130 inline __hashfunc_info* __trace_hash_func_construct()131 __trace_hash_func_construct() 132 { 133 if (!__profcxx_init()) 134 return 0; 135 136 if (!__reentrance_guard::__get_in()) 137 return 0; 138 139 __reentrance_guard __get_out; 140 return _GLIBCXX_PROFILE_DATA(_S_hash_func)->__add_object(__get_stack()); 141 } 142 143 inline void __trace_hash_func_destruct(__hashfunc_info * __obj_info,std::size_t __chain,std::size_t __accesses,std::size_t __hops)144 __trace_hash_func_destruct(__hashfunc_info* __obj_info, 145 std::size_t __chain, std::size_t __accesses, 146 std::size_t __hops) 147 { 148 _GLIBCXX_PROFILE_DATA(_S_hash_func)->__destruct(__obj_info, __chain, 149 __accesses, __hops); 150 } 151 152 } // namespace __gnu_profile 153 #endif /* _GLIBCXX_PROFILE_PROFILER_HASH_FUNC_H */ 154