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