3 // Copyright (C) 2009 Free Software Foundation, Inc.
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 terms
7 // of the GNU General Public License as published by the Free Software
8 // Foundation; either version 2, or (at your option) any later
11 // This library is distributed in the hope that it will be useful, but
12 // WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this library; see the file COPYING. If not, write to
18 // the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
19 // MA 02111-1307, USA.
21 // As a special exception, you may use this file as part of a free
22 // software library without restriction. Specifically, if other files
23 // instantiate templates or use macros or inline functions from this
24 // file, or you compile this file and link it with other files to
25 // produce an executable, this file does not by itself cause the
26 // resulting executable to be covered by the GNU General Public
27 // License. This exception does not however invalidate any other
28 // reasons why the executable file might be covered by the GNU General
31 /** @file profile/impl/profiler_trace.h
32 * @brief Diagnostics for container sizes.
35 // Written by Lixia Liu and Silvius Rus.
37 #ifndef _GLIBCXX_PROFILE_PROFILER_CONTAINER_SIZE_H
38 #define _GLIBCXX_PROFILE_PROFILER_CONTAINER_SIZE_H 1
40 #ifdef __GXX_EXPERIMENTAL_CXX0X__
52 #include "profile/impl/profiler.h"
53 #include "profile/impl/profiler_node.h"
54 #include "profile/impl/profiler_trace.h"
56 namespace __gnu_profile
59 /** @brief A container size instrumentation line in the object table. */
60 class __container_size_info
: public __object_info_base
63 __container_size_info();
64 __container_size_info(const __container_size_info
& __o
);
65 __container_size_info(__stack_t __stack
, size_t __num
);
66 virtual ~__container_size_info() {}
68 void __write(FILE* f
) const;
69 float __magnitude() const { return static_cast<float>(_M_cost
); }
70 const char* __advice() const;
72 void __merge(const __container_size_info
& __o
);
73 // Call if a container is destructed or cleaned.
74 void __destruct(size_t __num
, size_t __inum
);
75 // Estimate the cost of resize/rehash.
76 float __resize_cost(size_t __from
, size_t __to
) { return __from
; }
77 // Call if container is resized.
78 void __resize(size_t __from
, size_t __to
);
82 size_t _M_max
; // range of # buckets
85 size_t _M_item_min
; // range of # items
93 inline const char* __container_size_info::__advice() const
95 std::stringstream __message
;
96 if (_M_init
< _M_item_max
)
97 __message
<< "change initial container size from " << _M_init
98 << " to " << _M_item_max
;
100 return strdup(__message
.str().c_str());
103 inline void __container_size_info::__destruct(size_t __num
, size_t __inum
)
105 _M_max
= __max(_M_max
, __num
);
106 _M_item_max
= __max(_M_item_max
, __inum
);
109 _M_item_min
= __inum
;
111 _M_min
= __min(_M_min
, __num
);
112 _M_item_min
= __min(_M_item_min
, __inum
);
115 _M_item_total
+= __inum
;
119 inline void __container_size_info::__resize(size_t __from
, size_t __to
)
121 _M_cost
+= this->__resize_cost(__from
, __to
);
123 _M_max
= __max(_M_max
, __to
);
126 inline __container_size_info::__container_size_info(__stack_t __stack
,
128 : __object_info_base(__stack
), _M_init(0), _M_max(0), _M_item_max(0),
129 _M_min(0), _M_item_min(0), _M_total(0), _M_item_total(0), _M_cost(0),
130 _M_count(0), _M_resize(0)
132 _M_init
= _M_max
= __num
;
133 _M_item_min
= _M_item_max
= _M_item_total
= _M_total
= 0;
139 inline void __container_size_info::__merge(const __container_size_info
& __o
)
141 _M_init
= __max(_M_init
, __o
._M_init
);
142 _M_max
= __max(_M_max
, __o
._M_max
);
143 _M_item_max
= __max(_M_item_max
, __o
._M_item_max
);
144 _M_min
= __min(_M_min
, __o
._M_min
);
145 _M_item_min
= __min(_M_item_min
, __o
._M_item_min
);
146 _M_total
+= __o
._M_total
;
147 _M_item_total
+= __o
._M_item_total
;
148 _M_count
+= __o
._M_count
;
149 _M_cost
+= __o
._M_cost
;
150 _M_resize
+= __o
._M_resize
;
153 inline __container_size_info::__container_size_info()
154 : _M_init(0), _M_max(0), _M_item_max(0), _M_min(0), _M_item_min(0),
155 _M_total(0), _M_item_total(0), _M_cost(0), _M_count(0), _M_resize(0)
159 inline __container_size_info::__container_size_info(
160 const __container_size_info
& __o
)
161 : __object_info_base(__o
)
163 _M_init
= __o
._M_init
;
165 _M_item_max
= __o
._M_item_max
;
167 _M_item_min
= __o
._M_item_min
;
168 _M_total
= __o
._M_total
;
169 _M_item_total
= __o
._M_item_total
;
170 _M_cost
= __o
._M_cost
;
171 _M_count
= __o
._M_count
;
172 _M_resize
= __o
._M_resize
;
175 /** @brief A container size instrumentation line in the stack table. */
176 class __container_size_stack_info
: public __container_size_info
179 __container_size_stack_info(const __container_size_info
& __o
)
180 : __container_size_info(__o
) {}
183 /** @brief Container size instrumentation trace producer. */
184 class __trace_container_size
185 : public __trace_base
<__container_size_info
, __container_size_stack_info
>
188 ~__trace_container_size() {}
189 __trace_container_size()
190 : __trace_base
<__container_size_info
, __container_size_stack_info
>() {};
192 // Insert a new node at construct with object, callstack and initial size.
193 void __insert(const __object_t __obj
, __stack_t __stack
, size_t __num
);
194 // Call at destruction/clean to set container final size.
195 void __destruct(const void* __obj
, size_t __num
, size_t __inum
);
196 void __construct(const void* __obj
, size_t __inum
);
197 // Call at resize to set resize/cost information.
198 void __resize(const void* __obj
, int __from
, int __to
);
201 inline void __trace_container_size::__insert(const __object_t __obj
,
202 __stack_t __stack
, size_t __num
)
204 __add_object(__obj
, __container_size_info(__stack
, __num
));
207 inline void __container_size_info::__write(FILE* __f
) const
209 fprintf(__f
, "%Zu %Zu %Zu %Zu %Zu %Zu %Zu %Zu %Zu %Zu\n",
210 _M_init
, _M_count
, _M_cost
, _M_resize
, _M_min
, _M_max
, _M_total
,
211 _M_item_min
, _M_item_max
, _M_item_total
);
214 inline void __trace_container_size::__destruct(const void* __obj
,
215 size_t __num
, size_t __inum
)
217 if (!__is_on()) return;
219 __object_t __obj_handle
= static_cast<__object_t
>(__obj
);
221 __container_size_info
* __object_info
= __get_object_info(__obj_handle
);
225 __object_info
->__destruct(__num
, __inum
);
226 __retire_object(__obj_handle
);
229 inline void __trace_container_size::__resize(const void* __obj
, int __from
,
232 if (!__is_on()) return;
234 __container_size_info
* __object_info
= __get_object_info(__obj
);
238 __object_info
->__resize(__from
, __to
);
241 } // namespace __gnu_profile
242 #endif /* _GLIBCXX_PROFILE_PROFILER_CONTAINER_SIZE_H */