mem-stats.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  1. /* A memory statistics tracking infrastructure.
  2. Copyright (C) 2015-2019 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. struct mem_location
  27. {
  28. /* Default constructor. */
  29. inline
  30. mem_location () {}
  31. /* Constructor. */
  32. inline
  33. mem_location (mem_alloc_origin origin, bool ggc,
  34. const char *filename = NULL, int line = 0,
  35. const char *function = NULL):
  36. m_filename (filename), m_function (function), m_line (line), m_origin
  37. (origin), m_ggc (ggc) {}
  38. /* Copy constructor. */
  39. inline
  40. mem_location (mem_location &other): m_filename (other.m_filename),
  41. m_function (other.m_function), m_line (other.m_line),
  42. m_origin (other.m_origin), m_ggc (other.m_ggc) {}
  43. /* Compute hash value based on file name, function name and line in
  44. source code. As there is just a single pointer registered for every
  45. constant that points to e.g. the same file name, we can use hash
  46. of the pointer. */
  47. hashval_t
  48. hash ()
  49. {
  50. inchash::hash hash;
  51. hash.add_ptr (m_filename);
  52. hash.add_ptr (m_function);
  53. hash.add_int (m_line);
  54. return hash.end ();
  55. }
  56. /* Return true if the memory location is equal to OTHER. */
  57. int
  58. equal (mem_location &other)
  59. {
  60. return m_filename == other.m_filename && m_function == other.m_function
  61. && m_line == other.m_line;
  62. }
  63. /* Return trimmed filename for the location. */
  64. inline const char *
  65. get_trimmed_filename ()
  66. {
  67. const char *s1 = m_filename;
  68. const char *s2;
  69. while ((s2 = strstr (s1, "gcc/")))
  70. s1 = s2 + 4;
  71. return s1;
  72. }
  73. inline char *
  74. to_string ()
  75. {
  76. unsigned l = strlen (get_trimmed_filename ()) + strlen (m_function)
  77. + LOCATION_LINE_EXTRA_SPACE;
  78. char *s = XNEWVEC (char, l);
  79. sprintf (s, "%s:%i (%s)", get_trimmed_filename (),
  80. m_line, m_function);
  81. s[MIN (LOCATION_LINE_WIDTH, l - 1)] = '\0';
  82. return s;
  83. }
  84. /* Return display name associated to ORIGIN type. */
  85. static const char *
  86. get_origin_name (mem_alloc_origin origin)
  87. {
  88. return mem_alloc_origin_names[(unsigned) origin];
  89. }
  90. /* File name of source code. */
  91. const char *m_filename;
  92. /* Funcation name. */
  93. const char *m_function;
  94. /* Line number in source code. */
  95. int m_line;
  96. /* Origin type. */
  97. mem_alloc_origin m_origin;
  98. /* Flag if used by GGC allocation. */
  99. bool m_ggc;
  100. };
  101. /* Memory usage register to a memory location. */
  102. struct mem_usage
  103. {
  104. /* Default constructor. */
  105. mem_usage (): m_allocated (0), m_times (0), m_peak (0), m_instances (1) {}
  106. /* Constructor. */
  107. mem_usage (size_t allocated, size_t times, size_t peak, size_t instances = 0):
  108. m_allocated (allocated), m_times (times), m_peak (peak),
  109. m_instances (instances) {}
  110. /* Register overhead of SIZE bytes. */
  111. inline void
  112. register_overhead (size_t size)
  113. {
  114. m_allocated += size;
  115. m_times++;
  116. if (m_peak < m_allocated)
  117. m_peak = m_allocated;
  118. }
  119. /* Release overhead of SIZE bytes. */
  120. inline void
  121. release_overhead (size_t size)
  122. {
  123. gcc_assert (size <= m_allocated);
  124. m_allocated -= size;
  125. }
  126. /* Sum the usage with SECOND usage. */
  127. mem_usage
  128. operator+ (const mem_usage &second)
  129. {
  130. return mem_usage (m_allocated + second.m_allocated,
  131. m_times + second.m_times,
  132. m_peak + second.m_peak,
  133. m_instances + second.m_instances);
  134. }
  135. /* Equality operator. */
  136. inline bool
  137. operator== (const mem_usage &second) const
  138. {
  139. return (m_allocated == second.m_allocated
  140. && m_peak == second.m_peak
  141. && m_times == second.m_times);
  142. }
  143. /* Comparison operator. */
  144. inline bool
  145. operator< (const mem_usage &second) const
  146. {
  147. if (*this == second)
  148. return false;
  149. return (m_allocated == second.m_allocated ?
  150. (m_peak == second.m_peak ? m_times < second.m_times
  151. : m_peak < second.m_peak) : m_allocated < second.m_allocated);
  152. }
  153. /* Compare wrapper used by qsort method. */
  154. static int
  155. compare (const void *first, const void *second)
  156. {
  157. typedef std::pair<mem_location *, mem_usage *> mem_pair_t;
  158. const mem_pair_t f = *(const mem_pair_t *)first;
  159. const mem_pair_t s = *(const mem_pair_t *)second;
  160. if (*f.second == *s.second)
  161. return 0;
  162. return *f.second < *s.second ? 1 : -1;
  163. }
  164. /* Dump usage coupled to LOC location, where TOTAL is sum of all rows. */
  165. inline void
  166. dump (mem_location *loc, mem_usage &total) const
  167. {
  168. char *location_string = loc->to_string ();
  169. fprintf (stderr, "%-48s " PRsa (9) ":%5.1f%%"
  170. PRsa (9) PRsa (9) ":%5.1f%%%10s\n",
  171. location_string, SIZE_AMOUNT (m_allocated),
  172. get_percent (m_allocated, total.m_allocated),
  173. SIZE_AMOUNT (m_peak), SIZE_AMOUNT (m_times),
  174. get_percent (m_times, total.m_times), loc->m_ggc ? "ggc" : "heap");
  175. free (location_string);
  176. }
  177. /* Dump footer. */
  178. inline void
  179. dump_footer () const
  180. {
  181. fprintf (stderr, "%s" PRsa (53) PRsa (26) "\n", "Total",
  182. SIZE_AMOUNT (m_allocated), SIZE_AMOUNT (m_times));
  183. }
  184. /* Return fraction of NOMINATOR and DENOMINATOR in percent. */
  185. static inline float
  186. get_percent (size_t nominator, size_t denominator)
  187. {
  188. return denominator == 0 ? 0.0f : nominator * 100.0 / denominator;
  189. }
  190. /* Print line made of dashes. */
  191. static inline void
  192. print_dash_line (size_t count = 140)
  193. {
  194. while (count--)
  195. fputc ('-', stderr);
  196. fputc ('\n', stderr);
  197. }
  198. /* Dump header with NAME. */
  199. static inline void
  200. dump_header (const char *name)
  201. {
  202. fprintf (stderr, "%-48s %11s%16s%10s%17s\n", name, "Leak", "Peak",
  203. "Times", "Type");
  204. }
  205. /* Current number of allocated bytes. */
  206. size_t m_allocated;
  207. /* Number of allocations. */
  208. size_t m_times;
  209. /* Peak allocation in bytes. */
  210. size_t m_peak;
  211. /* Number of container instances. */
  212. size_t m_instances;
  213. };
  214. /* Memory usage pair that connectes memory usage and number
  215. of allocated bytes. */
  216. template <class T>
  217. struct mem_usage_pair
  218. {
  219. mem_usage_pair (T *usage_, size_t allocated_): usage (usage_),
  220. allocated (allocated_) {}
  221. T *usage;
  222. size_t allocated;
  223. };
  224. /* Memory allocation description. */
  225. template <class T>
  226. class mem_alloc_description
  227. {
  228. public:
  229. struct mem_location_hash : nofree_ptr_hash <mem_location>
  230. {
  231. static hashval_t
  232. hash (value_type l)
  233. {
  234. inchash::hash hstate;
  235. hstate.add_ptr ((const void *)l->m_filename);
  236. hstate.add_ptr (l->m_function);
  237. hstate.add_int (l->m_line);
  238. return hstate.end ();
  239. }
  240. static bool
  241. equal (value_type l1, value_type l2)
  242. {
  243. return (l1->m_filename == l2->m_filename
  244. && l1->m_function == l2->m_function
  245. && l1->m_line == l2->m_line);
  246. }
  247. };
  248. /* Internal class type definitions. */
  249. typedef hash_map <mem_location_hash, T *> mem_map_t;
  250. typedef hash_map <const void *, mem_usage_pair<T> > reverse_mem_map_t;
  251. typedef hash_map <const void *, std::pair<T *, size_t> > reverse_object_map_t;
  252. typedef std::pair <mem_location *, T *> mem_list_t;
  253. /* Default contructor. */
  254. mem_alloc_description ();
  255. /* Default destructor. */
  256. ~mem_alloc_description ();
  257. /* Returns true if instance PTR is registered by the memory description. */
  258. bool contains_descriptor_for_instance (const void *ptr);
  259. /* Return descriptor for instance PTR. */
  260. T *get_descriptor_for_instance (const void *ptr);
  261. /* Register memory allocation descriptor for container PTR which is
  262. described by a memory LOCATION. */
  263. T *register_descriptor (const void *ptr, mem_location *location);
  264. /* Register memory allocation descriptor for container PTR. ORIGIN identifies
  265. type of container and GGC identifes if the allocation is handled in GGC
  266. memory. Each location is identified by file NAME, LINE in source code and
  267. FUNCTION name. */
  268. T *register_descriptor (const void *ptr, mem_alloc_origin origin,
  269. bool ggc, const char *name, int line,
  270. const char *function);
  271. /* Register instance overhead identified by PTR pointer. Allocation takes
  272. SIZE bytes. */
  273. T *register_instance_overhead (size_t size, const void *ptr);
  274. /* For containers (and GGC) where we want to track every instance object,
  275. we register allocation of SIZE bytes, identified by PTR pointer, belonging
  276. to USAGE descriptor. */
  277. void register_object_overhead (T *usage, size_t size, const void *ptr);
  278. /* Release PTR pointer of SIZE bytes. If REMOVE_FROM_MAP is set to true,
  279. remove the instance from reverse map. Return memory usage that belongs
  280. to this memory description. */
  281. T *release_instance_overhead (void *ptr, size_t size,
  282. bool remove_from_map = false);
  283. /* Release instance object identified by PTR pointer. */
  284. void release_object_overhead (void *ptr);
  285. /* Unregister a memory allocation descriptor registered with
  286. register_descriptor (remove from reverse map), unless it is
  287. unregistered through release_instance_overhead with
  288. REMOVE_FROM_MAP = true. */
  289. void unregister_descriptor (void *ptr);
  290. /* Get sum value for ORIGIN type of allocation for the descriptor. */
  291. T get_sum (mem_alloc_origin origin);
  292. /* Get all tracked instances registered by the description. Items
  293. are filtered by ORIGIN type, LENGTH is return value where we register
  294. the number of elements in the list. If we want to process custom order,
  295. CMP comparator can be provided. */
  296. mem_list_t *get_list (mem_alloc_origin origin, unsigned *length,
  297. int (*cmp) (const void *first,
  298. const void *second) = NULL);
  299. /* Dump all tracked instances of type ORIGIN. If we want to process custom
  300. order, CMP comparator can be provided. */
  301. void dump (mem_alloc_origin origin,
  302. int (*cmp) (const void *first, const void *second) = NULL);
  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. if (entry)
  440. {
  441. entry->first->release_overhead (entry->second);
  442. m_reverse_object_map->remove (ptr);
  443. }
  444. }
  445. /* Unregister a memory allocation descriptor registered with
  446. register_descriptor (remove from reverse map), unless it is
  447. unregistered through release_instance_overhead with
  448. REMOVE_FROM_MAP = true. */
  449. template <class T>
  450. inline void
  451. mem_alloc_description<T>::unregister_descriptor (void *ptr)
  452. {
  453. m_reverse_map->remove (ptr);
  454. }
  455. /* Default contructor. */
  456. template <class T>
  457. inline
  458. mem_alloc_description<T>::mem_alloc_description ()
  459. {
  460. m_map = new mem_map_t (13, false, false);
  461. m_reverse_map = new reverse_mem_map_t (13, false, false);
  462. m_reverse_object_map = new reverse_object_map_t (13, false, false);
  463. }
  464. /* Default destructor. */
  465. template <class T>
  466. inline
  467. mem_alloc_description<T>::~mem_alloc_description ()
  468. {
  469. for (typename mem_map_t::iterator it = m_map->begin (); it != m_map->end ();
  470. ++it)
  471. {
  472. delete (*it).first;
  473. delete (*it).second;
  474. }
  475. delete m_map;
  476. delete m_reverse_map;
  477. delete m_reverse_object_map;
  478. }
  479. /* Get all tracked instances registered by the description. Items are filtered
  480. by ORIGIN type, LENGTH is return value where we register the number of
  481. elements in the list. If we want to process custom order, CMP comparator
  482. can be provided. */
  483. template <class T>
  484. inline
  485. typename mem_alloc_description<T>::mem_list_t *
  486. mem_alloc_description<T>::get_list (mem_alloc_origin origin, unsigned *length,
  487. int (*cmp) (const void *first,
  488. const void *second))
  489. {
  490. /* vec data structure is not used because all vectors generate memory
  491. allocation info a it would create a cycle. */
  492. size_t element_size = sizeof (mem_list_t);
  493. mem_list_t *list = XCNEWVEC (mem_list_t, m_map->elements ());
  494. unsigned i = 0;
  495. for (typename mem_map_t::iterator it = m_map->begin (); it != m_map->end ();
  496. ++it)
  497. if ((*it).first->m_origin == origin)
  498. list[i++] = std::pair<mem_location*, T*> (*it);
  499. qsort (list, i, element_size, cmp == NULL ? T::compare : cmp);
  500. *length = i;
  501. return list;
  502. }
  503. /* Get sum value for ORIGIN type of allocation for the descriptor. */
  504. template <class T>
  505. inline T
  506. mem_alloc_description<T>::get_sum (mem_alloc_origin origin)
  507. {
  508. unsigned length;
  509. mem_list_t *list = get_list (origin, &length);
  510. T sum;
  511. for (unsigned i = 0; i < length; i++)
  512. sum = sum + *list[i].second;
  513. XDELETEVEC (list);
  514. return sum;
  515. }
  516. /* Dump all tracked instances of type ORIGIN. If we want to process custom
  517. order, CMP comparator can be provided. */
  518. template <class T>
  519. inline void
  520. mem_alloc_description<T>::dump (mem_alloc_origin origin,
  521. int (*cmp) (const void *first,
  522. const void *second))
  523. {
  524. unsigned length;
  525. fprintf (stderr, "\n");
  526. mem_list_t *list = get_list (origin, &length, cmp);
  527. T total = get_sum (origin);
  528. T::print_dash_line ();
  529. T::dump_header (mem_location::get_origin_name (origin));
  530. T::print_dash_line ();
  531. for (int i = length - 1; i >= 0; i--)
  532. list[i].second->dump (list[i].first, total);
  533. T::print_dash_line ();
  534. T::dump_header (mem_location::get_origin_name (origin));
  535. T::print_dash_line ();
  536. total.dump_footer ();
  537. T::print_dash_line ();
  538. XDELETEVEC (list);
  539. fprintf (stderr, "\n");
  540. }
  541. #endif // GCC_MEM_STATS_H