annotate gcc/mem-stats.h @ 127:4c56639505ff

fix function.c and add CbC-example Makefile
author mir3636
date Wed, 11 Apr 2018 18:46:58 +0900
parents 04ced10e8804
children 84e7813d76e9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 /* A memory statistics tracking infrastructure.
kono
parents:
diff changeset
2 Copyright (C) 2015-2017 Free Software Foundation, Inc.
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
kono
parents:
diff changeset
166 /* Comparison operator. */
kono
parents:
diff changeset
167 inline bool
kono
parents:
diff changeset
168 operator< (const mem_usage &second) const
kono
parents:
diff changeset
169 {
kono
parents:
diff changeset
170 return (m_allocated == second.m_allocated ?
kono
parents:
diff changeset
171 (m_peak == second.m_peak ? m_times < second.m_times
kono
parents:
diff changeset
172 : m_peak < second.m_peak) : m_allocated < second.m_allocated);
kono
parents:
diff changeset
173 }
kono
parents:
diff changeset
174
kono
parents:
diff changeset
175 /* Compare wrapper used by qsort method. */
kono
parents:
diff changeset
176 static int
kono
parents:
diff changeset
177 compare (const void *first, const void *second)
kono
parents:
diff changeset
178 {
kono
parents:
diff changeset
179 typedef std::pair<mem_location *, mem_usage *> mem_pair_t;
kono
parents:
diff changeset
180
kono
parents:
diff changeset
181 const mem_pair_t f = *(const mem_pair_t *)first;
kono
parents:
diff changeset
182 const mem_pair_t s = *(const mem_pair_t *)second;
kono
parents:
diff changeset
183
kono
parents:
diff changeset
184 return (*f.second) < (*s.second);
kono
parents:
diff changeset
185 }
kono
parents:
diff changeset
186
kono
parents:
diff changeset
187 /* Dump usage coupled to LOC location, where TOTAL is sum of all rows. */
kono
parents:
diff changeset
188 inline void
kono
parents:
diff changeset
189 dump (mem_location *loc, mem_usage &total) const
kono
parents:
diff changeset
190 {
kono
parents:
diff changeset
191 char *location_string = loc->to_string ();
kono
parents:
diff changeset
192
kono
parents:
diff changeset
193 fprintf (stderr, "%-48s %10" PRIu64 ":%5.1f%%"
kono
parents:
diff changeset
194 "%10" PRIu64 "%10" PRIu64 ":%5.1f%%%10s\n",
kono
parents:
diff changeset
195 location_string, (uint64_t)m_allocated,
kono
parents:
diff changeset
196 get_percent (m_allocated, total.m_allocated),
kono
parents:
diff changeset
197 (uint64_t)m_peak, (uint64_t)m_times,
kono
parents:
diff changeset
198 get_percent (m_times, total.m_times), loc->m_ggc ? "ggc" : "heap");
kono
parents:
diff changeset
199
kono
parents:
diff changeset
200 free (location_string);
kono
parents:
diff changeset
201 }
kono
parents:
diff changeset
202
kono
parents:
diff changeset
203 /* Dump footer. */
kono
parents:
diff changeset
204 inline void
kono
parents:
diff changeset
205 dump_footer () const
kono
parents:
diff changeset
206 {
kono
parents:
diff changeset
207 print_dash_line ();
kono
parents:
diff changeset
208 fprintf (stderr, "%s%54" PRIu64 "%27" PRIu64 "\n", "Total",
kono
parents:
diff changeset
209 (uint64_t)m_allocated, (uint64_t)m_times);
kono
parents:
diff changeset
210 print_dash_line ();
kono
parents:
diff changeset
211 }
kono
parents:
diff changeset
212
kono
parents:
diff changeset
213 /* Return fraction of NOMINATOR and DENOMINATOR in percent. */
kono
parents:
diff changeset
214 static inline float
kono
parents:
diff changeset
215 get_percent (size_t nominator, size_t denominator)
kono
parents:
diff changeset
216 {
kono
parents:
diff changeset
217 return denominator == 0 ? 0.0f : nominator * 100.0 / denominator;
kono
parents:
diff changeset
218 }
kono
parents:
diff changeset
219
kono
parents:
diff changeset
220 /* Print line made of dashes. */
kono
parents:
diff changeset
221 static inline void
kono
parents:
diff changeset
222 print_dash_line (size_t count = 140)
kono
parents:
diff changeset
223 {
kono
parents:
diff changeset
224 while (count--)
kono
parents:
diff changeset
225 fputc ('-', stderr);
kono
parents:
diff changeset
226 fputc ('\n', stderr);
kono
parents:
diff changeset
227 }
kono
parents:
diff changeset
228
kono
parents:
diff changeset
229 /* Dump header with NAME. */
kono
parents:
diff changeset
230 static inline void
kono
parents:
diff changeset
231 dump_header (const char *name)
kono
parents:
diff changeset
232 {
kono
parents:
diff changeset
233 fprintf (stderr, "%-48s %11s%16s%10s%17s\n", name, "Leak", "Peak",
kono
parents:
diff changeset
234 "Times", "Type");
kono
parents:
diff changeset
235 print_dash_line ();
kono
parents:
diff changeset
236 }
kono
parents:
diff changeset
237
kono
parents:
diff changeset
238 /* Current number of allocated bytes. */
kono
parents:
diff changeset
239 size_t m_allocated;
kono
parents:
diff changeset
240 /* Number of allocations. */
kono
parents:
diff changeset
241 size_t m_times;
kono
parents:
diff changeset
242 /* Peak allocation in bytes. */
kono
parents:
diff changeset
243 size_t m_peak;
kono
parents:
diff changeset
244 /* Number of container instances. */
kono
parents:
diff changeset
245 size_t m_instances;
kono
parents:
diff changeset
246 };
kono
parents:
diff changeset
247
kono
parents:
diff changeset
248 /* Memory usage pair that connectes memory usage and number
kono
parents:
diff changeset
249 of allocated bytes. */
kono
parents:
diff changeset
250 template <class T>
kono
parents:
diff changeset
251 struct mem_usage_pair
kono
parents:
diff changeset
252 {
kono
parents:
diff changeset
253 mem_usage_pair (T *usage_, size_t allocated_): usage (usage_),
kono
parents:
diff changeset
254 allocated (allocated_) {}
kono
parents:
diff changeset
255
kono
parents:
diff changeset
256 T *usage;
kono
parents:
diff changeset
257 size_t allocated;
kono
parents:
diff changeset
258 };
kono
parents:
diff changeset
259
kono
parents:
diff changeset
260 /* Memory allocation description. */
kono
parents:
diff changeset
261 template <class T>
kono
parents:
diff changeset
262 class mem_alloc_description
kono
parents:
diff changeset
263 {
kono
parents:
diff changeset
264 public:
kono
parents:
diff changeset
265 struct mem_location_hash : nofree_ptr_hash <mem_location>
kono
parents:
diff changeset
266 {
kono
parents:
diff changeset
267 static hashval_t
kono
parents:
diff changeset
268 hash (value_type l)
kono
parents:
diff changeset
269 {
kono
parents:
diff changeset
270 inchash::hash hstate;
kono
parents:
diff changeset
271
kono
parents:
diff changeset
272 hstate.add_ptr ((const void *)l->m_filename);
kono
parents:
diff changeset
273 hstate.add_ptr (l->m_function);
kono
parents:
diff changeset
274 hstate.add_int (l->m_line);
kono
parents:
diff changeset
275
kono
parents:
diff changeset
276 return hstate.end ();
kono
parents:
diff changeset
277 }
kono
parents:
diff changeset
278
kono
parents:
diff changeset
279 static bool
kono
parents:
diff changeset
280 equal (value_type l1, value_type l2)
kono
parents:
diff changeset
281 {
kono
parents:
diff changeset
282 return l1->m_filename == l2->m_filename
kono
parents:
diff changeset
283 && l1->m_function == l2->m_function
kono
parents:
diff changeset
284 && l1->m_line == l2->m_line;
kono
parents:
diff changeset
285 }
kono
parents:
diff changeset
286 };
kono
parents:
diff changeset
287
kono
parents:
diff changeset
288 /* Internal class type definitions. */
kono
parents:
diff changeset
289 typedef hash_map <mem_location_hash, T *> mem_map_t;
kono
parents:
diff changeset
290 typedef hash_map <const void *, mem_usage_pair<T> > reverse_mem_map_t;
kono
parents:
diff changeset
291 typedef hash_map <const void *, std::pair<T *, size_t> > reverse_object_map_t;
kono
parents:
diff changeset
292 typedef std::pair <mem_location *, T *> mem_list_t;
kono
parents:
diff changeset
293
kono
parents:
diff changeset
294 /* Default contructor. */
kono
parents:
diff changeset
295 mem_alloc_description ();
kono
parents:
diff changeset
296
kono
parents:
diff changeset
297 /* Default destructor. */
kono
parents:
diff changeset
298 ~mem_alloc_description ();
kono
parents:
diff changeset
299
kono
parents:
diff changeset
300 /* Returns true if instance PTR is registered by the memory description. */
kono
parents:
diff changeset
301 bool
kono
parents:
diff changeset
302 contains_descriptor_for_instance (const void *ptr);
kono
parents:
diff changeset
303
kono
parents:
diff changeset
304 /* Return descriptor for instance PTR. */
kono
parents:
diff changeset
305 T *
kono
parents:
diff changeset
306 get_descriptor_for_instance (const void *ptr);
kono
parents:
diff changeset
307
kono
parents:
diff changeset
308 /* Register memory allocation descriptor for container PTR which is
kono
parents:
diff changeset
309 described by a memory LOCATION. */
kono
parents:
diff changeset
310 T *
kono
parents:
diff changeset
311 register_descriptor (const void *ptr, mem_location *location);
kono
parents:
diff changeset
312
kono
parents:
diff changeset
313 /* Register memory allocation descriptor for container PTR. ORIGIN identifies
kono
parents:
diff changeset
314 type of container and GGC identifes if the allocation is handled in GGC
kono
parents:
diff changeset
315 memory. Each location is identified by file NAME, LINE in source code and
kono
parents:
diff changeset
316 FUNCTION name. */
kono
parents:
diff changeset
317 T *
kono
parents:
diff changeset
318 register_descriptor (const void *ptr, mem_alloc_origin origin,
kono
parents:
diff changeset
319 bool ggc, const char *name, int line,
kono
parents:
diff changeset
320 const char *function);
kono
parents:
diff changeset
321
kono
parents:
diff changeset
322 /* Register instance overhead identified by PTR pointer. Allocation takes
kono
parents:
diff changeset
323 SIZE bytes. */
kono
parents:
diff changeset
324 T *
kono
parents:
diff changeset
325 register_instance_overhead (size_t size, const void *ptr);
kono
parents:
diff changeset
326
kono
parents:
diff changeset
327 /* For containers (and GGC) where we want to track every instance object,
kono
parents:
diff changeset
328 we register allocation of SIZE bytes, identified by PTR pointer, belonging
kono
parents:
diff changeset
329 to USAGE descriptor. */
kono
parents:
diff changeset
330 void
kono
parents:
diff changeset
331 register_object_overhead (T *usage, size_t size, const void *ptr);
kono
parents:
diff changeset
332
kono
parents:
diff changeset
333 /* Release PTR pointer of SIZE bytes. If REMOVE_FROM_MAP is set to true,
kono
parents:
diff changeset
334 remove the instance from reverse map. */
kono
parents:
diff changeset
335 void
kono
parents:
diff changeset
336 release_instance_overhead (void *ptr, size_t size,
kono
parents:
diff changeset
337 bool remove_from_map = false);
kono
parents:
diff changeset
338
kono
parents:
diff changeset
339 /* Release intance object identified by PTR pointer. */
kono
parents:
diff changeset
340 void
kono
parents:
diff changeset
341 release_object_overhead (void *ptr);
kono
parents:
diff changeset
342
kono
parents:
diff changeset
343 /* Get sum value for ORIGIN type of allocation for the descriptor. */
kono
parents:
diff changeset
344 T
kono
parents:
diff changeset
345 get_sum (mem_alloc_origin origin);
kono
parents:
diff changeset
346
kono
parents:
diff changeset
347 /* Get all tracked instances registered by the description. Items
kono
parents:
diff changeset
348 are filtered by ORIGIN type, LENGTH is return value where we register
kono
parents:
diff changeset
349 the number of elements in the list. If we want to process custom order,
kono
parents:
diff changeset
350 CMP comparator can be provided. */
kono
parents:
diff changeset
351 mem_list_t *
kono
parents:
diff changeset
352 get_list (mem_alloc_origin origin, unsigned *length,
kono
parents:
diff changeset
353 int (*cmp) (const void *first, const void *second) = NULL);
kono
parents:
diff changeset
354
kono
parents:
diff changeset
355 /* Dump all tracked instances of type ORIGIN. If we want to process custom
kono
parents:
diff changeset
356 order, CMP comparator can be provided. */
kono
parents:
diff changeset
357 void dump (mem_alloc_origin origin,
kono
parents:
diff changeset
358 int (*cmp) (const void *first, const void *second) = NULL);
kono
parents:
diff changeset
359
kono
parents:
diff changeset
360 /* Reverse object map used for every object allocation mapping. */
kono
parents:
diff changeset
361 reverse_object_map_t *m_reverse_object_map;
kono
parents:
diff changeset
362
kono
parents:
diff changeset
363 private:
kono
parents:
diff changeset
364 /* Register overhead of SIZE bytes of ORIGIN type. PTR pointer is allocated
kono
parents:
diff changeset
365 in NAME source file, at LINE in source code, in FUNCTION. */
kono
parents:
diff changeset
366 T *register_overhead (size_t size, mem_alloc_origin origin, const char *name,
kono
parents:
diff changeset
367 int line, const char *function, const void *ptr);
kono
parents:
diff changeset
368
kono
parents:
diff changeset
369 /* Allocation location coupled to the description. */
kono
parents:
diff changeset
370 mem_location m_location;
kono
parents:
diff changeset
371
kono
parents:
diff changeset
372 /* Location to usage mapping. */
kono
parents:
diff changeset
373 mem_map_t *m_map;
kono
parents:
diff changeset
374
kono
parents:
diff changeset
375 /* Reverse pointer to usage mapping. */
kono
parents:
diff changeset
376 reverse_mem_map_t *m_reverse_map;
kono
parents:
diff changeset
377 };
kono
parents:
diff changeset
378
kono
parents:
diff changeset
379
kono
parents:
diff changeset
380 /* Returns true if instance PTR is registered by the memory description. */
kono
parents:
diff changeset
381
kono
parents:
diff changeset
382 template <class T>
kono
parents:
diff changeset
383 inline bool
kono
parents:
diff changeset
384 mem_alloc_description<T>::contains_descriptor_for_instance (const void *ptr)
kono
parents:
diff changeset
385 {
kono
parents:
diff changeset
386 return m_reverse_map->get (ptr);
kono
parents:
diff changeset
387 }
kono
parents:
diff changeset
388
kono
parents:
diff changeset
389 /* Return descriptor for instance PTR. */
kono
parents:
diff changeset
390
kono
parents:
diff changeset
391 template <class T>
kono
parents:
diff changeset
392 inline T*
kono
parents:
diff changeset
393 mem_alloc_description<T>::get_descriptor_for_instance (const void *ptr)
kono
parents:
diff changeset
394 {
kono
parents:
diff changeset
395 return m_reverse_map->get (ptr) ? (*m_reverse_map->get (ptr)).usage : NULL;
kono
parents:
diff changeset
396 }
kono
parents:
diff changeset
397
kono
parents:
diff changeset
398
kono
parents:
diff changeset
399 /* Register memory allocation descriptor for container PTR which is
kono
parents:
diff changeset
400 described by a memory LOCATION. */
kono
parents:
diff changeset
401 template <class T>
kono
parents:
diff changeset
402 inline T*
kono
parents:
diff changeset
403 mem_alloc_description<T>::register_descriptor (const void *ptr,
kono
parents:
diff changeset
404 mem_location *location)
kono
parents:
diff changeset
405 {
kono
parents:
diff changeset
406 T *usage = NULL;
kono
parents:
diff changeset
407
kono
parents:
diff changeset
408 T **slot = m_map->get (location);
kono
parents:
diff changeset
409 if (slot)
kono
parents:
diff changeset
410 {
kono
parents:
diff changeset
411 delete location;
kono
parents:
diff changeset
412 usage = *slot;
kono
parents:
diff changeset
413 usage->m_instances++;
kono
parents:
diff changeset
414 }
kono
parents:
diff changeset
415 else
kono
parents:
diff changeset
416 {
kono
parents:
diff changeset
417 usage = new T ();
kono
parents:
diff changeset
418 m_map->put (location, usage);
kono
parents:
diff changeset
419 }
kono
parents:
diff changeset
420
kono
parents:
diff changeset
421 if (!m_reverse_map->get (ptr))
kono
parents:
diff changeset
422 m_reverse_map->put (ptr, mem_usage_pair<T> (usage, 0));
kono
parents:
diff changeset
423
kono
parents:
diff changeset
424 return usage;
kono
parents:
diff changeset
425 }
kono
parents:
diff changeset
426
kono
parents:
diff changeset
427 /* Register memory allocation descriptor for container PTR. ORIGIN identifies
kono
parents:
diff changeset
428 type of container and GGC identifes if the allocation is handled in GGC
kono
parents:
diff changeset
429 memory. Each location is identified by file NAME, LINE in source code and
kono
parents:
diff changeset
430 FUNCTION name. */
kono
parents:
diff changeset
431
kono
parents:
diff changeset
432 template <class T>
kono
parents:
diff changeset
433 inline T*
kono
parents:
diff changeset
434 mem_alloc_description<T>::register_descriptor (const void *ptr,
kono
parents:
diff changeset
435 mem_alloc_origin origin,
kono
parents:
diff changeset
436 bool ggc,
kono
parents:
diff changeset
437 const char *filename,
kono
parents:
diff changeset
438 int line,
kono
parents:
diff changeset
439 const char *function)
kono
parents:
diff changeset
440 {
kono
parents:
diff changeset
441 mem_location *l = new mem_location (origin, ggc, filename, line, function);
kono
parents:
diff changeset
442 return register_descriptor (ptr, l);
kono
parents:
diff changeset
443 }
kono
parents:
diff changeset
444
kono
parents:
diff changeset
445 /* Register instance overhead identified by PTR pointer. Allocation takes
kono
parents:
diff changeset
446 SIZE bytes. */
kono
parents:
diff changeset
447
kono
parents:
diff changeset
448 template <class T>
kono
parents:
diff changeset
449 inline T*
kono
parents:
diff changeset
450 mem_alloc_description<T>::register_instance_overhead (size_t size,
kono
parents:
diff changeset
451 const void *ptr)
kono
parents:
diff changeset
452 {
kono
parents:
diff changeset
453 mem_usage_pair <T> *slot = m_reverse_map->get (ptr);
kono
parents:
diff changeset
454 if (!slot)
kono
parents:
diff changeset
455 {
kono
parents:
diff changeset
456 /* Due to PCH, it can really happen. */
kono
parents:
diff changeset
457 return NULL;
kono
parents:
diff changeset
458 }
kono
parents:
diff changeset
459
kono
parents:
diff changeset
460 T *usage = (*slot).usage;
kono
parents:
diff changeset
461 usage->register_overhead (size);
kono
parents:
diff changeset
462
kono
parents:
diff changeset
463 return usage;
kono
parents:
diff changeset
464 }
kono
parents:
diff changeset
465
kono
parents:
diff changeset
466 /* For containers (and GGC) where we want to track every instance object,
kono
parents:
diff changeset
467 we register allocation of SIZE bytes, identified by PTR pointer, belonging
kono
parents:
diff changeset
468 to USAGE descriptor. */
kono
parents:
diff changeset
469
kono
parents:
diff changeset
470 template <class T>
kono
parents:
diff changeset
471 void
kono
parents:
diff changeset
472 mem_alloc_description<T>::register_object_overhead (T *usage, size_t size,
kono
parents:
diff changeset
473 const void *ptr)
kono
parents:
diff changeset
474 {
kono
parents:
diff changeset
475 /* In case of GGC, it is possible to have already occupied the memory
kono
parents:
diff changeset
476 location. */
kono
parents:
diff changeset
477 m_reverse_object_map->put (ptr, std::pair<T *, size_t> (usage, size));
kono
parents:
diff changeset
478 }
kono
parents:
diff changeset
479
kono
parents:
diff changeset
480 /* Register overhead of SIZE bytes of ORIGIN type. PTR pointer is allocated
kono
parents:
diff changeset
481 in NAME source file, at LINE in source code, in FUNCTION. */
kono
parents:
diff changeset
482
kono
parents:
diff changeset
483 template <class T>
kono
parents:
diff changeset
484 inline T*
kono
parents:
diff changeset
485 mem_alloc_description<T>::register_overhead (size_t size,
kono
parents:
diff changeset
486 mem_alloc_origin origin,
kono
parents:
diff changeset
487 const char *filename,
kono
parents:
diff changeset
488 int line,
kono
parents:
diff changeset
489 const char *function,
kono
parents:
diff changeset
490 const void *ptr)
kono
parents:
diff changeset
491 {
kono
parents:
diff changeset
492 T *usage = register_descriptor (ptr, origin, filename, line, function);
kono
parents:
diff changeset
493 usage->register_overhead (size);
kono
parents:
diff changeset
494
kono
parents:
diff changeset
495 return usage;
kono
parents:
diff changeset
496 }
kono
parents:
diff changeset
497
kono
parents:
diff changeset
498 /* Release PTR pointer of SIZE bytes. */
kono
parents:
diff changeset
499
kono
parents:
diff changeset
500 template <class T>
kono
parents:
diff changeset
501 inline void
kono
parents:
diff changeset
502 mem_alloc_description<T>::release_instance_overhead (void *ptr, size_t size,
kono
parents:
diff changeset
503 bool remove_from_map)
kono
parents:
diff changeset
504 {
kono
parents:
diff changeset
505 mem_usage_pair<T> *slot = m_reverse_map->get (ptr);
kono
parents:
diff changeset
506
kono
parents:
diff changeset
507 if (!slot)
kono
parents:
diff changeset
508 {
kono
parents:
diff changeset
509 /* Due to PCH, it can really happen. */
kono
parents:
diff changeset
510 return;
kono
parents:
diff changeset
511 }
kono
parents:
diff changeset
512
kono
parents:
diff changeset
513 mem_usage_pair<T> usage_pair = *slot;
kono
parents:
diff changeset
514 usage_pair.usage->release_overhead (size);
kono
parents:
diff changeset
515
kono
parents:
diff changeset
516 if (remove_from_map)
kono
parents:
diff changeset
517 m_reverse_map->remove (ptr);
kono
parents:
diff changeset
518 }
kono
parents:
diff changeset
519
kono
parents:
diff changeset
520 /* Release intance object identified by PTR pointer. */
kono
parents:
diff changeset
521
kono
parents:
diff changeset
522 template <class T>
kono
parents:
diff changeset
523 inline void
kono
parents:
diff changeset
524 mem_alloc_description<T>::release_object_overhead (void *ptr)
kono
parents:
diff changeset
525 {
kono
parents:
diff changeset
526 std::pair <T *, size_t> *entry = m_reverse_object_map->get (ptr);
kono
parents:
diff changeset
527 if (entry)
kono
parents:
diff changeset
528 {
kono
parents:
diff changeset
529 entry->first->release_overhead (entry->second);
kono
parents:
diff changeset
530 m_reverse_object_map->remove (ptr);
kono
parents:
diff changeset
531 }
kono
parents:
diff changeset
532 }
kono
parents:
diff changeset
533
kono
parents:
diff changeset
534 /* Default contructor. */
kono
parents:
diff changeset
535
kono
parents:
diff changeset
536 template <class T>
kono
parents:
diff changeset
537 inline
kono
parents:
diff changeset
538 mem_alloc_description<T>::mem_alloc_description ()
kono
parents:
diff changeset
539 {
kono
parents:
diff changeset
540 m_map = new mem_map_t (13, false, false);
kono
parents:
diff changeset
541 m_reverse_map = new reverse_mem_map_t (13, false, false);
kono
parents:
diff changeset
542 m_reverse_object_map = new reverse_object_map_t (13, false, false);
kono
parents:
diff changeset
543 }
kono
parents:
diff changeset
544
kono
parents:
diff changeset
545 /* Default destructor. */
kono
parents:
diff changeset
546
kono
parents:
diff changeset
547 template <class T>
kono
parents:
diff changeset
548 inline
kono
parents:
diff changeset
549 mem_alloc_description<T>::~mem_alloc_description ()
kono
parents:
diff changeset
550 {
kono
parents:
diff changeset
551 for (typename mem_map_t::iterator it = m_map->begin (); it != m_map->end ();
kono
parents:
diff changeset
552 ++it)
kono
parents:
diff changeset
553 {
kono
parents:
diff changeset
554 delete (*it).first;
kono
parents:
diff changeset
555 delete (*it).second;
kono
parents:
diff changeset
556 }
kono
parents:
diff changeset
557
kono
parents:
diff changeset
558 delete m_map;
kono
parents:
diff changeset
559 delete m_reverse_map;
kono
parents:
diff changeset
560 delete m_reverse_object_map;
kono
parents:
diff changeset
561 }
kono
parents:
diff changeset
562
kono
parents:
diff changeset
563 /* Get all tracked instances registered by the description. Items are filtered
kono
parents:
diff changeset
564 by ORIGIN type, LENGTH is return value where we register the number of
kono
parents:
diff changeset
565 elements in the list. If we want to process custom order, CMP comparator
kono
parents:
diff changeset
566 can be provided. */
kono
parents:
diff changeset
567
kono
parents:
diff changeset
568 template <class T>
kono
parents:
diff changeset
569 inline
kono
parents:
diff changeset
570 typename mem_alloc_description<T>::mem_list_t *
kono
parents:
diff changeset
571 mem_alloc_description<T>::get_list (mem_alloc_origin origin, unsigned *length,
kono
parents:
diff changeset
572 int (*cmp) (const void *first, const void *second))
kono
parents:
diff changeset
573 {
kono
parents:
diff changeset
574 /* vec data structure is not used because all vectors generate memory
kono
parents:
diff changeset
575 allocation info a it would create a cycle. */
kono
parents:
diff changeset
576 size_t element_size = sizeof (mem_list_t);
kono
parents:
diff changeset
577 mem_list_t *list = XCNEWVEC (mem_list_t, m_map->elements ());
kono
parents:
diff changeset
578 unsigned i = 0;
kono
parents:
diff changeset
579
kono
parents:
diff changeset
580 for (typename mem_map_t::iterator it = m_map->begin (); it != m_map->end ();
kono
parents:
diff changeset
581 ++it)
kono
parents:
diff changeset
582 if ((*it).first->m_origin == origin)
kono
parents:
diff changeset
583 list[i++] = std::pair<mem_location*, T*> (*it);
kono
parents:
diff changeset
584
kono
parents:
diff changeset
585 qsort (list, i, element_size, cmp == NULL ? T::compare : cmp);
kono
parents:
diff changeset
586 *length = i;
kono
parents:
diff changeset
587
kono
parents:
diff changeset
588 return list;
kono
parents:
diff changeset
589 }
kono
parents:
diff changeset
590
kono
parents:
diff changeset
591 /* Get sum value for ORIGIN type of allocation for the descriptor. */
kono
parents:
diff changeset
592
kono
parents:
diff changeset
593 template <class T>
kono
parents:
diff changeset
594 inline T
kono
parents:
diff changeset
595 mem_alloc_description<T>::get_sum (mem_alloc_origin origin)
kono
parents:
diff changeset
596 {
kono
parents:
diff changeset
597 unsigned length;
kono
parents:
diff changeset
598 mem_list_t *list = get_list (origin, &length);
kono
parents:
diff changeset
599 T sum;
kono
parents:
diff changeset
600
kono
parents:
diff changeset
601 for (unsigned i = 0; i < length; i++)
kono
parents:
diff changeset
602 sum = sum + *list[i].second;
kono
parents:
diff changeset
603
kono
parents:
diff changeset
604 XDELETEVEC (list);
kono
parents:
diff changeset
605
kono
parents:
diff changeset
606 return sum;
kono
parents:
diff changeset
607 }
kono
parents:
diff changeset
608
kono
parents:
diff changeset
609 /* Dump all tracked instances of type ORIGIN. If we want to process custom
kono
parents:
diff changeset
610 order, CMP comparator can be provided. */
kono
parents:
diff changeset
611
kono
parents:
diff changeset
612 template <class T>
kono
parents:
diff changeset
613 inline void
kono
parents:
diff changeset
614 mem_alloc_description<T>::dump (mem_alloc_origin origin,
kono
parents:
diff changeset
615 int (*cmp) (const void *first,
kono
parents:
diff changeset
616 const void *second))
kono
parents:
diff changeset
617 {
kono
parents:
diff changeset
618 unsigned length;
kono
parents:
diff changeset
619
kono
parents:
diff changeset
620 fprintf (stderr, "\n");
kono
parents:
diff changeset
621
kono
parents:
diff changeset
622 mem_list_t *list = get_list (origin, &length, cmp);
kono
parents:
diff changeset
623 T total = get_sum (origin);
kono
parents:
diff changeset
624
kono
parents:
diff changeset
625 T::dump_header (mem_location::get_origin_name (origin));
kono
parents:
diff changeset
626 for (int i = length - 1; i >= 0; i--)
kono
parents:
diff changeset
627 list[i].second->dump (list[i].first, total);
kono
parents:
diff changeset
628
kono
parents:
diff changeset
629 total.dump_footer ();
kono
parents:
diff changeset
630
kono
parents:
diff changeset
631 XDELETEVEC (list);
kono
parents:
diff changeset
632
kono
parents:
diff changeset
633 fprintf (stderr, "\n");
kono
parents:
diff changeset
634 }
kono
parents:
diff changeset
635
kono
parents:
diff changeset
636 #endif // GCC_MEM_STATS_H