annotate gcc/mem-stats.h @ 138:fc828634a951

merge
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Thu, 08 Nov 2018 14:17:14 +0900
parents 84e7813d76e9
children 1830386684a0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 /* A memory statistics tracking infrastructure.
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2 Copyright (C) 2015-2018 Free Software Foundation, Inc.
111
kono
parents:
diff changeset
3 Contributed by Martin Liska <mliska@suse.cz>
kono
parents:
diff changeset
4
kono
parents:
diff changeset
5 This file is part of GCC.
kono
parents:
diff changeset
6
kono
parents:
diff changeset
7 GCC is free software; you can redistribute it and/or modify it under
kono
parents:
diff changeset
8 the terms of the GNU General Public License as published by the Free
kono
parents:
diff changeset
9 Software Foundation; either version 3, or (at your option) any later
kono
parents:
diff changeset
10 version.
kono
parents:
diff changeset
11
kono
parents:
diff changeset
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
kono
parents:
diff changeset
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
kono
parents:
diff changeset
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
kono
parents:
diff changeset
15 for more details.
kono
parents:
diff changeset
16
kono
parents:
diff changeset
17 You should have received a copy of the GNU General Public License
kono
parents:
diff changeset
18 along with GCC; see the file COPYING3. If not see
kono
parents:
diff changeset
19 <http://www.gnu.org/licenses/>. */
kono
parents:
diff changeset
20
kono
parents:
diff changeset
21 #ifndef GCC_MEM_STATS_H
kono
parents:
diff changeset
22 #define GCC_MEM_STATS_H
kono
parents:
diff changeset
23
kono
parents:
diff changeset
24 /* Forward declaration. */
kono
parents:
diff changeset
25 template<typename Key, typename Value,
kono
parents:
diff changeset
26 typename Traits = simple_hashmap_traits<default_hash_traits<Key>,
kono
parents:
diff changeset
27 Value> >
kono
parents:
diff changeset
28 class hash_map;
kono
parents:
diff changeset
29
kono
parents:
diff changeset
30 #define LOCATION_LINE_EXTRA_SPACE 30
kono
parents:
diff changeset
31 #define LOCATION_LINE_WIDTH 48
kono
parents:
diff changeset
32
kono
parents:
diff changeset
33 /* Memory allocation location. */
kono
parents:
diff changeset
34 struct mem_location
kono
parents:
diff changeset
35 {
kono
parents:
diff changeset
36 /* Default constructor. */
kono
parents:
diff changeset
37 inline
kono
parents:
diff changeset
38 mem_location () {}
kono
parents:
diff changeset
39
kono
parents:
diff changeset
40 /* Constructor. */
kono
parents:
diff changeset
41 inline
kono
parents:
diff changeset
42 mem_location (mem_alloc_origin origin, bool ggc,
kono
parents:
diff changeset
43 const char *filename = NULL, int line = 0,
kono
parents:
diff changeset
44 const char *function = NULL):
kono
parents:
diff changeset
45 m_filename (filename), m_function (function), m_line (line), m_origin
kono
parents:
diff changeset
46 (origin), m_ggc (ggc) {}
kono
parents:
diff changeset
47
kono
parents:
diff changeset
48 /* Copy constructor. */
kono
parents:
diff changeset
49 inline
kono
parents:
diff changeset
50 mem_location (mem_location &other): m_filename (other.m_filename),
kono
parents:
diff changeset
51 m_function (other.m_function), m_line (other.m_line),
kono
parents:
diff changeset
52 m_origin (other.m_origin), m_ggc (other.m_ggc) {}
kono
parents:
diff changeset
53
kono
parents:
diff changeset
54 /* Compute hash value based on file name, function name and line in
kono
parents:
diff changeset
55 source code. As there is just a single pointer registered for every
kono
parents:
diff changeset
56 constant that points to e.g. the same file name, we can use hash
kono
parents:
diff changeset
57 of the pointer. */
kono
parents:
diff changeset
58 hashval_t
kono
parents:
diff changeset
59 hash ()
kono
parents:
diff changeset
60 {
kono
parents:
diff changeset
61 inchash::hash hash;
kono
parents:
diff changeset
62
kono
parents:
diff changeset
63 hash.add_ptr (m_filename);
kono
parents:
diff changeset
64 hash.add_ptr (m_function);
kono
parents:
diff changeset
65 hash.add_int (m_line);
kono
parents:
diff changeset
66
kono
parents:
diff changeset
67 return hash.end ();
kono
parents:
diff changeset
68 }
kono
parents:
diff changeset
69
kono
parents:
diff changeset
70 /* Return true if the memory location is equal to OTHER. */
kono
parents:
diff changeset
71 int
kono
parents:
diff changeset
72 equal (mem_location &other)
kono
parents:
diff changeset
73 {
kono
parents:
diff changeset
74 return m_filename == other.m_filename && m_function == other.m_function
kono
parents:
diff changeset
75 && m_line == other.m_line;
kono
parents:
diff changeset
76 }
kono
parents:
diff changeset
77
kono
parents:
diff changeset
78 /* Return trimmed filename for the location. */
kono
parents:
diff changeset
79 inline const char *
kono
parents:
diff changeset
80 get_trimmed_filename ()
kono
parents:
diff changeset
81 {
kono
parents:
diff changeset
82 const char *s1 = m_filename;
kono
parents:
diff changeset
83 const char *s2;
kono
parents:
diff changeset
84
kono
parents:
diff changeset
85 while ((s2 = strstr (s1, "gcc/")))
kono
parents:
diff changeset
86 s1 = s2 + 4;
kono
parents:
diff changeset
87
kono
parents:
diff changeset
88 return s1;
kono
parents:
diff changeset
89 }
kono
parents:
diff changeset
90
kono
parents:
diff changeset
91 inline char *
kono
parents:
diff changeset
92 to_string ()
kono
parents:
diff changeset
93 {
kono
parents:
diff changeset
94 unsigned l = strlen (get_trimmed_filename ()) + strlen (m_function)
kono
parents:
diff changeset
95 + LOCATION_LINE_EXTRA_SPACE;
kono
parents:
diff changeset
96
kono
parents:
diff changeset
97 char *s = XNEWVEC (char, l);
kono
parents:
diff changeset
98 sprintf (s, "%s:%i (%s)", get_trimmed_filename (),
kono
parents:
diff changeset
99 m_line, m_function);
kono
parents:
diff changeset
100
kono
parents:
diff changeset
101 s[MIN (LOCATION_LINE_WIDTH, l - 1)] = '\0';
kono
parents:
diff changeset
102
kono
parents:
diff changeset
103 return s;
kono
parents:
diff changeset
104 }
kono
parents:
diff changeset
105
kono
parents:
diff changeset
106 /* Return display name associated to ORIGIN type. */
kono
parents:
diff changeset
107 static const char *
kono
parents:
diff changeset
108 get_origin_name (mem_alloc_origin origin)
kono
parents:
diff changeset
109 {
kono
parents:
diff changeset
110 return mem_alloc_origin_names[(unsigned) origin];
kono
parents:
diff changeset
111 }
kono
parents:
diff changeset
112
kono
parents:
diff changeset
113 /* File name of source code. */
kono
parents:
diff changeset
114 const char *m_filename;
kono
parents:
diff changeset
115 /* Funcation name. */
kono
parents:
diff changeset
116 const char *m_function;
kono
parents:
diff changeset
117 /* Line number in source code. */
kono
parents:
diff changeset
118 int m_line;
kono
parents:
diff changeset
119 /* Origin type. */
kono
parents:
diff changeset
120 mem_alloc_origin m_origin;
kono
parents:
diff changeset
121 /* Flag if used by GGC allocation. */
kono
parents:
diff changeset
122 bool m_ggc;
kono
parents:
diff changeset
123 };
kono
parents:
diff changeset
124
kono
parents:
diff changeset
125 /* Memory usage register to a memory location. */
kono
parents:
diff changeset
126 struct mem_usage
kono
parents:
diff changeset
127 {
kono
parents:
diff changeset
128 /* Default constructor. */
kono
parents:
diff changeset
129 mem_usage (): m_allocated (0), m_times (0), m_peak (0), m_instances (1) {}
kono
parents:
diff changeset
130
kono
parents:
diff changeset
131 /* Constructor. */
kono
parents:
diff changeset
132 mem_usage (size_t allocated, size_t times, size_t peak, size_t instances = 0):
kono
parents:
diff changeset
133 m_allocated (allocated), m_times (times), m_peak (peak),
kono
parents:
diff changeset
134 m_instances (instances) {}
kono
parents:
diff changeset
135
kono
parents:
diff changeset
136 /* Register overhead of SIZE bytes. */
kono
parents:
diff changeset
137 inline void
kono
parents:
diff changeset
138 register_overhead (size_t size)
kono
parents:
diff changeset
139 {
kono
parents:
diff changeset
140 m_allocated += size;
kono
parents:
diff changeset
141 m_times++;
kono
parents:
diff changeset
142
kono
parents:
diff changeset
143 if (m_peak < m_allocated)
kono
parents:
diff changeset
144 m_peak = m_allocated;
kono
parents:
diff changeset
145 }
kono
parents:
diff changeset
146
kono
parents:
diff changeset
147 /* Release overhead of SIZE bytes. */
kono
parents:
diff changeset
148 inline void
kono
parents:
diff changeset
149 release_overhead (size_t size)
kono
parents:
diff changeset
150 {
kono
parents:
diff changeset
151 gcc_assert (size <= m_allocated);
kono
parents:
diff changeset
152
kono
parents:
diff changeset
153 m_allocated -= size;
kono
parents:
diff changeset
154 }
kono
parents:
diff changeset
155
kono
parents:
diff changeset
156 /* Sum the usage with SECOND usage. */
kono
parents:
diff changeset
157 mem_usage
kono
parents:
diff changeset
158 operator+ (const mem_usage &second)
kono
parents:
diff changeset
159 {
kono
parents:
diff changeset
160 return mem_usage (m_allocated + second.m_allocated,
kono
parents:
diff changeset
161 m_times + second.m_times,
kono
parents:
diff changeset
162 m_peak + second.m_peak,
kono
parents:
diff changeset
163 m_instances + second.m_instances);
kono
parents:
diff changeset
164 }
kono
parents:
diff changeset
165
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
166 /* Equality operator. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
167 inline bool
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
168 operator== (const mem_usage &second) const
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
169 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
170 return (m_allocated == second.m_allocated
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
171 && m_peak == second.m_peak
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
172 && m_times == second.m_times);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
173 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
174
111
kono
parents:
diff changeset
175 /* Comparison operator. */
kono
parents:
diff changeset
176 inline bool
kono
parents:
diff changeset
177 operator< (const mem_usage &second) const
kono
parents:
diff changeset
178 {
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
179 if (*this == second)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
180 return false;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
181
111
kono
parents:
diff changeset
182 return (m_allocated == second.m_allocated ?
kono
parents:
diff changeset
183 (m_peak == second.m_peak ? m_times < second.m_times
kono
parents:
diff changeset
184 : m_peak < second.m_peak) : m_allocated < second.m_allocated);
kono
parents:
diff changeset
185 }
kono
parents:
diff changeset
186
kono
parents:
diff changeset
187 /* Compare wrapper used by qsort method. */
kono
parents:
diff changeset
188 static int
kono
parents:
diff changeset
189 compare (const void *first, const void *second)
kono
parents:
diff changeset
190 {
kono
parents:
diff changeset
191 typedef std::pair<mem_location *, mem_usage *> mem_pair_t;
kono
parents:
diff changeset
192
kono
parents:
diff changeset
193 const mem_pair_t f = *(const mem_pair_t *)first;
kono
parents:
diff changeset
194 const mem_pair_t s = *(const mem_pair_t *)second;
kono
parents:
diff changeset
195
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
196 if (*f.second == *s.second)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
197 return 0;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
198
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
199 return *f.second < *s.second ? 1 : -1;
111
kono
parents:
diff changeset
200 }
kono
parents:
diff changeset
201
kono
parents:
diff changeset
202 /* Dump usage coupled to LOC location, where TOTAL is sum of all rows. */
kono
parents:
diff changeset
203 inline void
kono
parents:
diff changeset
204 dump (mem_location *loc, mem_usage &total) const
kono
parents:
diff changeset
205 {
kono
parents:
diff changeset
206 char *location_string = loc->to_string ();
kono
parents:
diff changeset
207
kono
parents:
diff changeset
208 fprintf (stderr, "%-48s %10" PRIu64 ":%5.1f%%"
kono
parents:
diff changeset
209 "%10" PRIu64 "%10" PRIu64 ":%5.1f%%%10s\n",
kono
parents:
diff changeset
210 location_string, (uint64_t)m_allocated,
kono
parents:
diff changeset
211 get_percent (m_allocated, total.m_allocated),
kono
parents:
diff changeset
212 (uint64_t)m_peak, (uint64_t)m_times,
kono
parents:
diff changeset
213 get_percent (m_times, total.m_times), loc->m_ggc ? "ggc" : "heap");
kono
parents:
diff changeset
214
kono
parents:
diff changeset
215 free (location_string);
kono
parents:
diff changeset
216 }
kono
parents:
diff changeset
217
kono
parents:
diff changeset
218 /* Dump footer. */
kono
parents:
diff changeset
219 inline void
kono
parents:
diff changeset
220 dump_footer () const
kono
parents:
diff changeset
221 {
kono
parents:
diff changeset
222 print_dash_line ();
kono
parents:
diff changeset
223 fprintf (stderr, "%s%54" PRIu64 "%27" PRIu64 "\n", "Total",
kono
parents:
diff changeset
224 (uint64_t)m_allocated, (uint64_t)m_times);
kono
parents:
diff changeset
225 print_dash_line ();
kono
parents:
diff changeset
226 }
kono
parents:
diff changeset
227
kono
parents:
diff changeset
228 /* Return fraction of NOMINATOR and DENOMINATOR in percent. */
kono
parents:
diff changeset
229 static inline float
kono
parents:
diff changeset
230 get_percent (size_t nominator, size_t denominator)
kono
parents:
diff changeset
231 {
kono
parents:
diff changeset
232 return denominator == 0 ? 0.0f : nominator * 100.0 / denominator;
kono
parents:
diff changeset
233 }
kono
parents:
diff changeset
234
kono
parents:
diff changeset
235 /* Print line made of dashes. */
kono
parents:
diff changeset
236 static inline void
kono
parents:
diff changeset
237 print_dash_line (size_t count = 140)
kono
parents:
diff changeset
238 {
kono
parents:
diff changeset
239 while (count--)
kono
parents:
diff changeset
240 fputc ('-', stderr);
kono
parents:
diff changeset
241 fputc ('\n', stderr);
kono
parents:
diff changeset
242 }
kono
parents:
diff changeset
243
kono
parents:
diff changeset
244 /* Dump header with NAME. */
kono
parents:
diff changeset
245 static inline void
kono
parents:
diff changeset
246 dump_header (const char *name)
kono
parents:
diff changeset
247 {
kono
parents:
diff changeset
248 fprintf (stderr, "%-48s %11s%16s%10s%17s\n", name, "Leak", "Peak",
kono
parents:
diff changeset
249 "Times", "Type");
kono
parents:
diff changeset
250 print_dash_line ();
kono
parents:
diff changeset
251 }
kono
parents:
diff changeset
252
kono
parents:
diff changeset
253 /* Current number of allocated bytes. */
kono
parents:
diff changeset
254 size_t m_allocated;
kono
parents:
diff changeset
255 /* Number of allocations. */
kono
parents:
diff changeset
256 size_t m_times;
kono
parents:
diff changeset
257 /* Peak allocation in bytes. */
kono
parents:
diff changeset
258 size_t m_peak;
kono
parents:
diff changeset
259 /* Number of container instances. */
kono
parents:
diff changeset
260 size_t m_instances;
kono
parents:
diff changeset
261 };
kono
parents:
diff changeset
262
kono
parents:
diff changeset
263 /* Memory usage pair that connectes memory usage and number
kono
parents:
diff changeset
264 of allocated bytes. */
kono
parents:
diff changeset
265 template <class T>
kono
parents:
diff changeset
266 struct mem_usage_pair
kono
parents:
diff changeset
267 {
kono
parents:
diff changeset
268 mem_usage_pair (T *usage_, size_t allocated_): usage (usage_),
kono
parents:
diff changeset
269 allocated (allocated_) {}
kono
parents:
diff changeset
270
kono
parents:
diff changeset
271 T *usage;
kono
parents:
diff changeset
272 size_t allocated;
kono
parents:
diff changeset
273 };
kono
parents:
diff changeset
274
kono
parents:
diff changeset
275 /* Memory allocation description. */
kono
parents:
diff changeset
276 template <class T>
kono
parents:
diff changeset
277 class mem_alloc_description
kono
parents:
diff changeset
278 {
kono
parents:
diff changeset
279 public:
kono
parents:
diff changeset
280 struct mem_location_hash : nofree_ptr_hash <mem_location>
kono
parents:
diff changeset
281 {
kono
parents:
diff changeset
282 static hashval_t
kono
parents:
diff changeset
283 hash (value_type l)
kono
parents:
diff changeset
284 {
kono
parents:
diff changeset
285 inchash::hash hstate;
kono
parents:
diff changeset
286
kono
parents:
diff changeset
287 hstate.add_ptr ((const void *)l->m_filename);
kono
parents:
diff changeset
288 hstate.add_ptr (l->m_function);
kono
parents:
diff changeset
289 hstate.add_int (l->m_line);
kono
parents:
diff changeset
290
kono
parents:
diff changeset
291 return hstate.end ();
kono
parents:
diff changeset
292 }
kono
parents:
diff changeset
293
kono
parents:
diff changeset
294 static bool
kono
parents:
diff changeset
295 equal (value_type l1, value_type l2)
kono
parents:
diff changeset
296 {
kono
parents:
diff changeset
297 return l1->m_filename == l2->m_filename
kono
parents:
diff changeset
298 && l1->m_function == l2->m_function
kono
parents:
diff changeset
299 && l1->m_line == l2->m_line;
kono
parents:
diff changeset
300 }
kono
parents:
diff changeset
301 };
kono
parents:
diff changeset
302
kono
parents:
diff changeset
303 /* Internal class type definitions. */
kono
parents:
diff changeset
304 typedef hash_map <mem_location_hash, T *> mem_map_t;
kono
parents:
diff changeset
305 typedef hash_map <const void *, mem_usage_pair<T> > reverse_mem_map_t;
kono
parents:
diff changeset
306 typedef hash_map <const void *, std::pair<T *, size_t> > reverse_object_map_t;
kono
parents:
diff changeset
307 typedef std::pair <mem_location *, T *> mem_list_t;
kono
parents:
diff changeset
308
kono
parents:
diff changeset
309 /* Default contructor. */
kono
parents:
diff changeset
310 mem_alloc_description ();
kono
parents:
diff changeset
311
kono
parents:
diff changeset
312 /* Default destructor. */
kono
parents:
diff changeset
313 ~mem_alloc_description ();
kono
parents:
diff changeset
314
kono
parents:
diff changeset
315 /* Returns true if instance PTR is registered by the memory description. */
kono
parents:
diff changeset
316 bool
kono
parents:
diff changeset
317 contains_descriptor_for_instance (const void *ptr);
kono
parents:
diff changeset
318
kono
parents:
diff changeset
319 /* Return descriptor for instance PTR. */
kono
parents:
diff changeset
320 T *
kono
parents:
diff changeset
321 get_descriptor_for_instance (const void *ptr);
kono
parents:
diff changeset
322
kono
parents:
diff changeset
323 /* Register memory allocation descriptor for container PTR which is
kono
parents:
diff changeset
324 described by a memory LOCATION. */
kono
parents:
diff changeset
325 T *
kono
parents:
diff changeset
326 register_descriptor (const void *ptr, mem_location *location);
kono
parents:
diff changeset
327
kono
parents:
diff changeset
328 /* Register memory allocation descriptor for container PTR. ORIGIN identifies
kono
parents:
diff changeset
329 type of container and GGC identifes if the allocation is handled in GGC
kono
parents:
diff changeset
330 memory. Each location is identified by file NAME, LINE in source code and
kono
parents:
diff changeset
331 FUNCTION name. */
kono
parents:
diff changeset
332 T *
kono
parents:
diff changeset
333 register_descriptor (const void *ptr, mem_alloc_origin origin,
kono
parents:
diff changeset
334 bool ggc, const char *name, int line,
kono
parents:
diff changeset
335 const char *function);
kono
parents:
diff changeset
336
kono
parents:
diff changeset
337 /* Register instance overhead identified by PTR pointer. Allocation takes
kono
parents:
diff changeset
338 SIZE bytes. */
kono
parents:
diff changeset
339 T *
kono
parents:
diff changeset
340 register_instance_overhead (size_t size, const void *ptr);
kono
parents:
diff changeset
341
kono
parents:
diff changeset
342 /* For containers (and GGC) where we want to track every instance object,
kono
parents:
diff changeset
343 we register allocation of SIZE bytes, identified by PTR pointer, belonging
kono
parents:
diff changeset
344 to USAGE descriptor. */
kono
parents:
diff changeset
345 void
kono
parents:
diff changeset
346 register_object_overhead (T *usage, size_t size, const void *ptr);
kono
parents:
diff changeset
347
kono
parents:
diff changeset
348 /* Release PTR pointer of SIZE bytes. If REMOVE_FROM_MAP is set to true,
kono
parents:
diff changeset
349 remove the instance from reverse map. */
kono
parents:
diff changeset
350 void
kono
parents:
diff changeset
351 release_instance_overhead (void *ptr, size_t size,
kono
parents:
diff changeset
352 bool remove_from_map = false);
kono
parents:
diff changeset
353
kono
parents:
diff changeset
354 /* Release intance object identified by PTR pointer. */
kono
parents:
diff changeset
355 void
kono
parents:
diff changeset
356 release_object_overhead (void *ptr);
kono
parents:
diff changeset
357
kono
parents:
diff changeset
358 /* Get sum value for ORIGIN type of allocation for the descriptor. */
kono
parents:
diff changeset
359 T
kono
parents:
diff changeset
360 get_sum (mem_alloc_origin origin);
kono
parents:
diff changeset
361
kono
parents:
diff changeset
362 /* Get all tracked instances registered by the description. Items
kono
parents:
diff changeset
363 are filtered by ORIGIN type, LENGTH is return value where we register
kono
parents:
diff changeset
364 the number of elements in the list. If we want to process custom order,
kono
parents:
diff changeset
365 CMP comparator can be provided. */
kono
parents:
diff changeset
366 mem_list_t *
kono
parents:
diff changeset
367 get_list (mem_alloc_origin origin, unsigned *length,
kono
parents:
diff changeset
368 int (*cmp) (const void *first, const void *second) = NULL);
kono
parents:
diff changeset
369
kono
parents:
diff changeset
370 /* Dump all tracked instances of type ORIGIN. If we want to process custom
kono
parents:
diff changeset
371 order, CMP comparator can be provided. */
kono
parents:
diff changeset
372 void dump (mem_alloc_origin origin,
kono
parents:
diff changeset
373 int (*cmp) (const void *first, const void *second) = NULL);
kono
parents:
diff changeset
374
kono
parents:
diff changeset
375 /* Reverse object map used for every object allocation mapping. */
kono
parents:
diff changeset
376 reverse_object_map_t *m_reverse_object_map;
kono
parents:
diff changeset
377
kono
parents:
diff changeset
378 private:
kono
parents:
diff changeset
379 /* Register overhead of SIZE bytes of ORIGIN type. PTR pointer is allocated
kono
parents:
diff changeset
380 in NAME source file, at LINE in source code, in FUNCTION. */
kono
parents:
diff changeset
381 T *register_overhead (size_t size, mem_alloc_origin origin, const char *name,
kono
parents:
diff changeset
382 int line, const char *function, const void *ptr);
kono
parents:
diff changeset
383
kono
parents:
diff changeset
384 /* Allocation location coupled to the description. */
kono
parents:
diff changeset
385 mem_location m_location;
kono
parents:
diff changeset
386
kono
parents:
diff changeset
387 /* Location to usage mapping. */
kono
parents:
diff changeset
388 mem_map_t *m_map;
kono
parents:
diff changeset
389
kono
parents:
diff changeset
390 /* Reverse pointer to usage mapping. */
kono
parents:
diff changeset
391 reverse_mem_map_t *m_reverse_map;
kono
parents:
diff changeset
392 };
kono
parents:
diff changeset
393
kono
parents:
diff changeset
394
kono
parents:
diff changeset
395 /* Returns true if instance PTR is registered by the memory description. */
kono
parents:
diff changeset
396
kono
parents:
diff changeset
397 template <class T>
kono
parents:
diff changeset
398 inline bool
kono
parents:
diff changeset
399 mem_alloc_description<T>::contains_descriptor_for_instance (const void *ptr)
kono
parents:
diff changeset
400 {
kono
parents:
diff changeset
401 return m_reverse_map->get (ptr);
kono
parents:
diff changeset
402 }
kono
parents:
diff changeset
403
kono
parents:
diff changeset
404 /* Return descriptor for instance PTR. */
kono
parents:
diff changeset
405
kono
parents:
diff changeset
406 template <class T>
kono
parents:
diff changeset
407 inline T*
kono
parents:
diff changeset
408 mem_alloc_description<T>::get_descriptor_for_instance (const void *ptr)
kono
parents:
diff changeset
409 {
kono
parents:
diff changeset
410 return m_reverse_map->get (ptr) ? (*m_reverse_map->get (ptr)).usage : NULL;
kono
parents:
diff changeset
411 }
kono
parents:
diff changeset
412
kono
parents:
diff changeset
413
kono
parents:
diff changeset
414 /* Register memory allocation descriptor for container PTR which is
kono
parents:
diff changeset
415 described by a memory LOCATION. */
kono
parents:
diff changeset
416 template <class T>
kono
parents:
diff changeset
417 inline T*
kono
parents:
diff changeset
418 mem_alloc_description<T>::register_descriptor (const void *ptr,
kono
parents:
diff changeset
419 mem_location *location)
kono
parents:
diff changeset
420 {
kono
parents:
diff changeset
421 T *usage = NULL;
kono
parents:
diff changeset
422
kono
parents:
diff changeset
423 T **slot = m_map->get (location);
kono
parents:
diff changeset
424 if (slot)
kono
parents:
diff changeset
425 {
kono
parents:
diff changeset
426 delete location;
kono
parents:
diff changeset
427 usage = *slot;
kono
parents:
diff changeset
428 usage->m_instances++;
kono
parents:
diff changeset
429 }
kono
parents:
diff changeset
430 else
kono
parents:
diff changeset
431 {
kono
parents:
diff changeset
432 usage = new T ();
kono
parents:
diff changeset
433 m_map->put (location, usage);
kono
parents:
diff changeset
434 }
kono
parents:
diff changeset
435
kono
parents:
diff changeset
436 if (!m_reverse_map->get (ptr))
kono
parents:
diff changeset
437 m_reverse_map->put (ptr, mem_usage_pair<T> (usage, 0));
kono
parents:
diff changeset
438
kono
parents:
diff changeset
439 return usage;
kono
parents:
diff changeset
440 }
kono
parents:
diff changeset
441
kono
parents:
diff changeset
442 /* Register memory allocation descriptor for container PTR. ORIGIN identifies
kono
parents:
diff changeset
443 type of container and GGC identifes if the allocation is handled in GGC
kono
parents:
diff changeset
444 memory. Each location is identified by file NAME, LINE in source code and
kono
parents:
diff changeset
445 FUNCTION name. */
kono
parents:
diff changeset
446
kono
parents:
diff changeset
447 template <class T>
kono
parents:
diff changeset
448 inline T*
kono
parents:
diff changeset
449 mem_alloc_description<T>::register_descriptor (const void *ptr,
kono
parents:
diff changeset
450 mem_alloc_origin origin,
kono
parents:
diff changeset
451 bool ggc,
kono
parents:
diff changeset
452 const char *filename,
kono
parents:
diff changeset
453 int line,
kono
parents:
diff changeset
454 const char *function)
kono
parents:
diff changeset
455 {
kono
parents:
diff changeset
456 mem_location *l = new mem_location (origin, ggc, filename, line, function);
kono
parents:
diff changeset
457 return register_descriptor (ptr, l);
kono
parents:
diff changeset
458 }
kono
parents:
diff changeset
459
kono
parents:
diff changeset
460 /* Register instance overhead identified by PTR pointer. Allocation takes
kono
parents:
diff changeset
461 SIZE bytes. */
kono
parents:
diff changeset
462
kono
parents:
diff changeset
463 template <class T>
kono
parents:
diff changeset
464 inline T*
kono
parents:
diff changeset
465 mem_alloc_description<T>::register_instance_overhead (size_t size,
kono
parents:
diff changeset
466 const void *ptr)
kono
parents:
diff changeset
467 {
kono
parents:
diff changeset
468 mem_usage_pair <T> *slot = m_reverse_map->get (ptr);
kono
parents:
diff changeset
469 if (!slot)
kono
parents:
diff changeset
470 {
kono
parents:
diff changeset
471 /* Due to PCH, it can really happen. */
kono
parents:
diff changeset
472 return NULL;
kono
parents:
diff changeset
473 }
kono
parents:
diff changeset
474
kono
parents:
diff changeset
475 T *usage = (*slot).usage;
kono
parents:
diff changeset
476 usage->register_overhead (size);
kono
parents:
diff changeset
477
kono
parents:
diff changeset
478 return usage;
kono
parents:
diff changeset
479 }
kono
parents:
diff changeset
480
kono
parents:
diff changeset
481 /* For containers (and GGC) where we want to track every instance object,
kono
parents:
diff changeset
482 we register allocation of SIZE bytes, identified by PTR pointer, belonging
kono
parents:
diff changeset
483 to USAGE descriptor. */
kono
parents:
diff changeset
484
kono
parents:
diff changeset
485 template <class T>
kono
parents:
diff changeset
486 void
kono
parents:
diff changeset
487 mem_alloc_description<T>::register_object_overhead (T *usage, size_t size,
kono
parents:
diff changeset
488 const void *ptr)
kono
parents:
diff changeset
489 {
kono
parents:
diff changeset
490 /* In case of GGC, it is possible to have already occupied the memory
kono
parents:
diff changeset
491 location. */
kono
parents:
diff changeset
492 m_reverse_object_map->put (ptr, std::pair<T *, size_t> (usage, size));
kono
parents:
diff changeset
493 }
kono
parents:
diff changeset
494
kono
parents:
diff changeset
495 /* Register overhead of SIZE bytes of ORIGIN type. PTR pointer is allocated
kono
parents:
diff changeset
496 in NAME source file, at LINE in source code, in FUNCTION. */
kono
parents:
diff changeset
497
kono
parents:
diff changeset
498 template <class T>
kono
parents:
diff changeset
499 inline T*
kono
parents:
diff changeset
500 mem_alloc_description<T>::register_overhead (size_t size,
kono
parents:
diff changeset
501 mem_alloc_origin origin,
kono
parents:
diff changeset
502 const char *filename,
kono
parents:
diff changeset
503 int line,
kono
parents:
diff changeset
504 const char *function,
kono
parents:
diff changeset
505 const void *ptr)
kono
parents:
diff changeset
506 {
kono
parents:
diff changeset
507 T *usage = register_descriptor (ptr, origin, filename, line, function);
kono
parents:
diff changeset
508 usage->register_overhead (size);
kono
parents:
diff changeset
509
kono
parents:
diff changeset
510 return usage;
kono
parents:
diff changeset
511 }
kono
parents:
diff changeset
512
kono
parents:
diff changeset
513 /* Release PTR pointer of SIZE bytes. */
kono
parents:
diff changeset
514
kono
parents:
diff changeset
515 template <class T>
kono
parents:
diff changeset
516 inline void
kono
parents:
diff changeset
517 mem_alloc_description<T>::release_instance_overhead (void *ptr, size_t size,
kono
parents:
diff changeset
518 bool remove_from_map)
kono
parents:
diff changeset
519 {
kono
parents:
diff changeset
520 mem_usage_pair<T> *slot = m_reverse_map->get (ptr);
kono
parents:
diff changeset
521
kono
parents:
diff changeset
522 if (!slot)
kono
parents:
diff changeset
523 {
kono
parents:
diff changeset
524 /* Due to PCH, it can really happen. */
kono
parents:
diff changeset
525 return;
kono
parents:
diff changeset
526 }
kono
parents:
diff changeset
527
kono
parents:
diff changeset
528 mem_usage_pair<T> usage_pair = *slot;
kono
parents:
diff changeset
529 usage_pair.usage->release_overhead (size);
kono
parents:
diff changeset
530
kono
parents:
diff changeset
531 if (remove_from_map)
kono
parents:
diff changeset
532 m_reverse_map->remove (ptr);
kono
parents:
diff changeset
533 }
kono
parents:
diff changeset
534
kono
parents:
diff changeset
535 /* Release intance object identified by PTR pointer. */
kono
parents:
diff changeset
536
kono
parents:
diff changeset
537 template <class T>
kono
parents:
diff changeset
538 inline void
kono
parents:
diff changeset
539 mem_alloc_description<T>::release_object_overhead (void *ptr)
kono
parents:
diff changeset
540 {
kono
parents:
diff changeset
541 std::pair <T *, size_t> *entry = m_reverse_object_map->get (ptr);
kono
parents:
diff changeset
542 if (entry)
kono
parents:
diff changeset
543 {
kono
parents:
diff changeset
544 entry->first->release_overhead (entry->second);
kono
parents:
diff changeset
545 m_reverse_object_map->remove (ptr);
kono
parents:
diff changeset
546 }
kono
parents:
diff changeset
547 }
kono
parents:
diff changeset
548
kono
parents:
diff changeset
549 /* Default contructor. */
kono
parents:
diff changeset
550
kono
parents:
diff changeset
551 template <class T>
kono
parents:
diff changeset
552 inline
kono
parents:
diff changeset
553 mem_alloc_description<T>::mem_alloc_description ()
kono
parents:
diff changeset
554 {
kono
parents:
diff changeset
555 m_map = new mem_map_t (13, false, false);
kono
parents:
diff changeset
556 m_reverse_map = new reverse_mem_map_t (13, false, false);
kono
parents:
diff changeset
557 m_reverse_object_map = new reverse_object_map_t (13, false, false);
kono
parents:
diff changeset
558 }
kono
parents:
diff changeset
559
kono
parents:
diff changeset
560 /* Default destructor. */
kono
parents:
diff changeset
561
kono
parents:
diff changeset
562 template <class T>
kono
parents:
diff changeset
563 inline
kono
parents:
diff changeset
564 mem_alloc_description<T>::~mem_alloc_description ()
kono
parents:
diff changeset
565 {
kono
parents:
diff changeset
566 for (typename mem_map_t::iterator it = m_map->begin (); it != m_map->end ();
kono
parents:
diff changeset
567 ++it)
kono
parents:
diff changeset
568 {
kono
parents:
diff changeset
569 delete (*it).first;
kono
parents:
diff changeset
570 delete (*it).second;
kono
parents:
diff changeset
571 }
kono
parents:
diff changeset
572
kono
parents:
diff changeset
573 delete m_map;
kono
parents:
diff changeset
574 delete m_reverse_map;
kono
parents:
diff changeset
575 delete m_reverse_object_map;
kono
parents:
diff changeset
576 }
kono
parents:
diff changeset
577
kono
parents:
diff changeset
578 /* Get all tracked instances registered by the description. Items are filtered
kono
parents:
diff changeset
579 by ORIGIN type, LENGTH is return value where we register the number of
kono
parents:
diff changeset
580 elements in the list. If we want to process custom order, CMP comparator
kono
parents:
diff changeset
581 can be provided. */
kono
parents:
diff changeset
582
kono
parents:
diff changeset
583 template <class T>
kono
parents:
diff changeset
584 inline
kono
parents:
diff changeset
585 typename mem_alloc_description<T>::mem_list_t *
kono
parents:
diff changeset
586 mem_alloc_description<T>::get_list (mem_alloc_origin origin, unsigned *length,
kono
parents:
diff changeset
587 int (*cmp) (const void *first, const void *second))
kono
parents:
diff changeset
588 {
kono
parents:
diff changeset
589 /* vec data structure is not used because all vectors generate memory
kono
parents:
diff changeset
590 allocation info a it would create a cycle. */
kono
parents:
diff changeset
591 size_t element_size = sizeof (mem_list_t);
kono
parents:
diff changeset
592 mem_list_t *list = XCNEWVEC (mem_list_t, m_map->elements ());
kono
parents:
diff changeset
593 unsigned i = 0;
kono
parents:
diff changeset
594
kono
parents:
diff changeset
595 for (typename mem_map_t::iterator it = m_map->begin (); it != m_map->end ();
kono
parents:
diff changeset
596 ++it)
kono
parents:
diff changeset
597 if ((*it).first->m_origin == origin)
kono
parents:
diff changeset
598 list[i++] = std::pair<mem_location*, T*> (*it);
kono
parents:
diff changeset
599
kono
parents:
diff changeset
600 qsort (list, i, element_size, cmp == NULL ? T::compare : cmp);
kono
parents:
diff changeset
601 *length = i;
kono
parents:
diff changeset
602
kono
parents:
diff changeset
603 return list;
kono
parents:
diff changeset
604 }
kono
parents:
diff changeset
605
kono
parents:
diff changeset
606 /* Get sum value for ORIGIN type of allocation for the descriptor. */
kono
parents:
diff changeset
607
kono
parents:
diff changeset
608 template <class T>
kono
parents:
diff changeset
609 inline T
kono
parents:
diff changeset
610 mem_alloc_description<T>::get_sum (mem_alloc_origin origin)
kono
parents:
diff changeset
611 {
kono
parents:
diff changeset
612 unsigned length;
kono
parents:
diff changeset
613 mem_list_t *list = get_list (origin, &length);
kono
parents:
diff changeset
614 T sum;
kono
parents:
diff changeset
615
kono
parents:
diff changeset
616 for (unsigned i = 0; i < length; i++)
kono
parents:
diff changeset
617 sum = sum + *list[i].second;
kono
parents:
diff changeset
618
kono
parents:
diff changeset
619 XDELETEVEC (list);
kono
parents:
diff changeset
620
kono
parents:
diff changeset
621 return sum;
kono
parents:
diff changeset
622 }
kono
parents:
diff changeset
623
kono
parents:
diff changeset
624 /* Dump all tracked instances of type ORIGIN. If we want to process custom
kono
parents:
diff changeset
625 order, CMP comparator can be provided. */
kono
parents:
diff changeset
626
kono
parents:
diff changeset
627 template <class T>
kono
parents:
diff changeset
628 inline void
kono
parents:
diff changeset
629 mem_alloc_description<T>::dump (mem_alloc_origin origin,
kono
parents:
diff changeset
630 int (*cmp) (const void *first,
kono
parents:
diff changeset
631 const void *second))
kono
parents:
diff changeset
632 {
kono
parents:
diff changeset
633 unsigned length;
kono
parents:
diff changeset
634
kono
parents:
diff changeset
635 fprintf (stderr, "\n");
kono
parents:
diff changeset
636
kono
parents:
diff changeset
637 mem_list_t *list = get_list (origin, &length, cmp);
kono
parents:
diff changeset
638 T total = get_sum (origin);
kono
parents:
diff changeset
639
kono
parents:
diff changeset
640 T::dump_header (mem_location::get_origin_name (origin));
kono
parents:
diff changeset
641 for (int i = length - 1; i >= 0; i--)
kono
parents:
diff changeset
642 list[i].second->dump (list[i].first, total);
kono
parents:
diff changeset
643
kono
parents:
diff changeset
644 total.dump_footer ();
kono
parents:
diff changeset
645
kono
parents:
diff changeset
646 XDELETEVEC (list);
kono
parents:
diff changeset
647
kono
parents:
diff changeset
648 fprintf (stderr, "\n");
kono
parents:
diff changeset
649 }
kono
parents:
diff changeset
650
kono
parents:
diff changeset
651 #endif // GCC_MEM_STATS_H