annotate gcc/mem-stats.h @ 145:1830386684a0

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