mem-stats.h 18 KB

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