aboutsummaryrefslogtreecommitdiff
path: root/gcc/mem-stats.h
diff options
context:
space:
mode:
authorMartin Liska <mliska@suse.cz>2015-05-27 14:47:22 +0200
committerMartin Liska <marxin@gcc.gnu.org>2015-05-27 12:47:22 +0000
commit2d44c7dea7c8d2917eec28a5e08e8f405aad8c3e (patch)
tree4505b8171553fd7e780bb28532a4986cd72b6f4f /gcc/mem-stats.h
parent151fbaac5c78c4e9b749fc9e916f78b0ba63b153 (diff)
downloadgcc-2d44c7dea7c8d2917eec28a5e08e8f405aad8c3e.zip
gcc-2d44c7dea7c8d2917eec28a5e08e8f405aad8c3e.tar.gz
gcc-2d44c7dea7c8d2917eec28a5e08e8f405aad8c3e.tar.bz2
New memory allocation statistics infrastructure.
* Makefile.in: Add additional dependencies related to memory report enhancement. * alloc-pool.c (allocate_pool_descriptor): Use new ctor. * bitmap.c (struct bitmap_descriptor_d): Remove. (struct loc): Likewise. (struct bitmap_desc_hasher): Likewise. (bitmap_desc_hasher::hash): Likewise. (bitmap_desc_hasher::equal): Likewise. (get_bitmap_descriptor): Likewise. (bitmap_register): User new memory descriptor API. (register_overhead): Likewise. (bitmap_find_bit): Register nsearches and search_iter statistics. (struct bitmap_output_info): Remove. (print_statistics): Likewise. (dump_bitmap_statistics): Use new memory descriptor. * bitmap.h (struct bitmap_usage): New class. * genmatch.c: Extend header file inclusion. * genpreds.c: Likewise. * ggc-common.c (struct ggc_usage): New class. (struct ggc_loc_desc_hasher): Remove. (ggc_loc_desc_hasher::hash): Likewise. (ggc_loc_desc_hasher::equal): Likewise. (struct ggc_ptr_hash_entry): Likewise. (struct ptr_hash_hasher): Likewise. (ptr_hash_hasher::hash): Likewise. (ptr_hash_hasher::equal): Likewise. (make_loc_descriptor): Likewise. (ggc_prune_ptr): Likewise. (dump_ggc_loc_statistics): Use new memory descriptor. (ggc_record_overhead): Likewise. (ggc_free_overhead): Likewise. (final_cmp_statistic): Remove. (cmp_statistic): Likewise. (ggc_add_statistics): Liekwise. (ggc_prune_overhead_list): Likewise. * hash-map-traits.h: New file. * hash-map.h (struct default_hashmap_traits): Move the traits to a separate header file. * hash-set.h: Pass memory statistics info to ctor. * hash-table.c (void dump_hash_table_loc_statistics): New function. * hash-table.h (hash_table::hash_table): Add new ctor arguments. (hash_table::~hash_table): Register memory release operation. (hash_table::alloc_entries): Handle memory allocation operation. (hash_table::expand): Likewise. * inchash.c (iterative_hash_hashval_t): Move implementation to header file. (iterative_hash_host_wide_int): Likewise. * inchash.h (class hash): Likewise. * mem-stats-traits.h: New file. * mem-stats.h: New file. (mem_location): Add new class. (mem_usage): Likewise. (mem_alloc_description): Likewise. * sese.c: Add new header file inclusision. * toplev.c (dump_memory_report): Add report for hash_table, hash_map and hash_set. * tree-sra.c: Add new header file inclusision. * vec.c (struct vec_descriptor): Remove. (hash_descriptor): Likewise. (struct vec_usage): Likewise. (struct ptr_hash_entry): Likewise. (hash_ptr): Likewise. (eq_ptr): Likewise. (vec_prefix::register_overhead): Use new memory descriptor API. (vec_prefix::release_overhead): Likewise. (add_statistics): Remove. (dump_vec_loc_statistics): Use new memory descriptor API. * vec.h (struct vec_prefix): Likewise. (va_heap::reserve): Likewise. (va_heap::release): Likewise. * emit-rtl.c (gen_raw_REG): Fix passing MEM_STAT. From-SVN: r223748
Diffstat (limited to 'gcc/mem-stats.h')
-rw-r--r--gcc/mem-stats.h550
1 files changed, 550 insertions, 0 deletions
diff --git a/gcc/mem-stats.h b/gcc/mem-stats.h
new file mode 100644
index 0000000..ac47231
--- /dev/null
+++ b/gcc/mem-stats.h
@@ -0,0 +1,550 @@
+#ifndef GCC_MEM_STATS_H
+#define GCC_MEM_STATS_H
+
+#include "hash-map-traits.h"
+#include "inchash.h"
+#include "mem-stats-traits.h"
+#include "vec.h"
+
+/* Forward declaration. */
+template<typename Key, typename Value,
+ typename Traits = default_hashmap_traits>
+class hash_map;
+
+/* Memory allocation location. */
+struct mem_location
+{
+ /* Default constructor. */
+ inline mem_location () {}
+
+ /* Constructor. */
+ inline mem_location (const char *filename, const char *function, int line,
+ mem_alloc_origin origin, bool ggc):
+ m_filename (filename), m_function (function), m_line (line), m_origin
+ (origin), m_ggc (ggc) {}
+
+ /* Compute hash value based on file name, function name and line in
+ source code. As there is just a single pointer registered for every
+ constant that points to e.g. the same file name, we can use hash
+ of the pointer. */
+ hashval_t hash ()
+ {
+ inchash::hash hash;
+
+ hash.add_ptr (m_filename);
+ hash.add_ptr (m_function);
+ hash.add_int (m_line);
+
+ return hash.end ();
+ }
+
+ /* Return true if the memory location is equal to OTHER. */
+ int equal (mem_location &other)
+ {
+ return m_filename == other.m_filename && m_function == other.m_function
+ && m_line == other.m_line;
+ }
+
+ /* Return trimmed filename for the location. */
+ inline const char *get_trimmed_filename ()
+ {
+ const char *s1 = m_filename;
+ const char *s2;
+
+ while ((s2 = strstr (s1, "gcc/")))
+ s1 = s2 + 4;
+
+ return s1;
+ }
+
+ /* Return display name associated to ORIGIN type. */
+ static const char *get_origin_name (mem_alloc_origin origin)
+ {
+ return mem_alloc_origin_names[(unsigned) origin];
+ }
+
+ /* File name of source code. */
+ const char *m_filename;
+ /* Funcation name. */
+ const char *m_function;
+ /* Line number in source code. */
+ int m_line;
+ /* Origin type. */
+ mem_alloc_origin m_origin;
+ /* Flag if used by GGC allocation. */
+ bool m_ggc;
+};
+
+/* Memory usage register to a memory location. */
+struct mem_usage
+{
+ /* Default constructor. */
+ mem_usage (): m_allocated (0), m_times (0), m_peak (0) {}
+
+ /* Constructor. */
+ mem_usage (size_t allocated, size_t times, size_t peak):
+ m_allocated (allocated), m_times (times), m_peak (peak) {}
+
+ /* Register overhead of SIZE bytes. */
+ inline void register_overhead (size_t size)
+ {
+ m_allocated += size;
+ m_times++;
+
+ if (m_peak < m_allocated)
+ m_peak = m_allocated;
+ }
+
+ /* Release overhead of SIZE bytes. */
+ inline void release_overhead (size_t size)
+ {
+ gcc_assert (size <= m_allocated);
+
+ m_allocated -= size;
+ }
+
+ /* Sum the usage with SECOND usage. */
+ mem_usage operator+ (const mem_usage &second)
+ {
+ return mem_usage (m_allocated + second.m_allocated,
+ m_times + second.m_times,
+ m_peak + second.m_peak);
+ }
+
+ /* Comparison operator. */
+ inline bool operator< (const mem_usage &second) const
+ {
+ return (m_allocated == second.m_allocated ?
+ (m_peak == second.m_peak ? m_times < second.m_times
+ : m_peak < second.m_peak) : m_allocated < second.m_allocated);
+ }
+
+ /* Compare wrapper used by qsort method. */
+ static int compare (const void *first, const void *second)
+ {
+ typedef std::pair<mem_location *, mem_usage *> mem_pair_t;
+
+ const mem_pair_t f = *(const mem_pair_t *)first;
+ const mem_pair_t s = *(const mem_pair_t *)second;
+
+ return (*f.second) < (*s.second);
+ }
+
+ /* Dump usage coupled to LOC location, where TOTAL is sum of all rows. */
+ inline void dump (mem_location *loc, mem_usage &total) const
+ {
+ char s[4096];
+ sprintf (s, "%s:%i (%s)", loc->get_trimmed_filename (),
+ loc->m_line, loc->m_function);
+
+ s[48] = '\0';
+
+ fprintf (stderr, "%-48s %10li:%5.1f%%%10li%10li:%5.1f%%%10s\n", s,
+ (long)m_allocated, get_percent (m_allocated, total.m_allocated),
+ (long)m_peak, (long)m_times,
+ get_percent (m_times, total.m_times), loc->m_ggc ? "ggc" : "heap");
+ }
+
+ /* Dump footer. */
+ inline void dump_footer ()
+ {
+ print_dash_line ();
+ fprintf (stderr, "%s%54li%27li\n", "Total", (long)m_allocated,
+ (long)m_times);
+ print_dash_line ();
+ }
+
+ /* Return fraction of NOMINATOR and DENOMINATOR in percent. */
+ static inline float get_percent (size_t nominator, size_t denominator)
+ {
+ return denominator == 0 ? 0.0f : nominator * 100.0 / denominator;
+ }
+
+ /* Print line made of dashes. */
+ static inline void print_dash_line ()
+ {
+ fprintf (stderr, "%s\n", std::string (128, '-').c_str ());
+ }
+
+ /* Dump header with NAME. */
+ static inline void dump_header (const char *name)
+ {
+ fprintf (stderr, "%-48s %11s%16s%10s%17s\n", name, "Leak", "Peak",
+ "Times", "Type");
+ print_dash_line ();
+ }
+
+ /* Current number of allocated bytes. */
+ size_t m_allocated;
+ /* Number of allocations. */
+ size_t m_times;
+ /* Peak allocation in bytes. */
+ size_t m_peak;
+};
+
+/* Memory usage pair that connectes memory usage and number
+ of allocated bytes. */
+template <class T>
+struct mem_usage_pair
+{
+ mem_usage_pair (T *usage_, size_t allocated_): usage (usage_),
+ allocated (allocated_) {}
+
+ T *usage;
+ size_t allocated;
+};
+
+/* Memory allocation description. */
+template <class T>
+class mem_alloc_description
+{
+public:
+ struct mem_alloc_hashmap_traits: default_hashmap_traits
+ {
+ static hashval_t
+ hash (const mem_location *l)
+ {
+ inchash::hash hstate;
+
+ hstate.add_ptr ((const void *)l->m_filename);
+ hstate.add_ptr (l->m_function);
+ hstate.add_int (l->m_line);
+
+ return hstate.end ();
+ }
+
+ static bool
+ equal_keys (const mem_location *l1, const mem_location *l2)
+ {
+ return l1->m_filename == l2->m_filename
+ && l1->m_function == l2->m_function
+ && l1->m_line == l2->m_line;
+ }
+ };
+
+ /* Internal class type definitions. */
+ typedef hash_map <mem_location *, T *, mem_alloc_hashmap_traits> mem_map_t;
+ typedef hash_map <const void *, mem_usage_pair<T>, default_hashmap_traits>
+ reverse_mem_map_t;
+ typedef hash_map <const void *, std::pair<T *, size_t> > reverse_object_map_t;
+ typedef std::pair <mem_location *, T *> mem_list_t;
+
+ /* Default contructor. */
+ mem_alloc_description ();
+
+ /* Default destructor. */
+ ~mem_alloc_description ();
+
+ /* Returns true if instance PTR is registered by the memory description. */
+ bool contains_descriptor_for_instance (const void *ptr);
+
+ /* Return descriptor for instance PTR. */
+ T *get_descriptor_for_instance (const void *ptr);
+
+ /* Register memory allocation descriptor for container PTR. ORIGIN identifies
+ type of container and GGC identifes if the allocation is handled in GGC
+ memory. Each location is identified by file NAME, LINE in source code and
+ FUNCTION name. */
+ T *register_descriptor (const void *ptr, mem_alloc_origin origin,
+ bool ggc, const char *name, int line,
+ const char *function);
+
+ /* Register instance overhead identified by PTR pointer. Allocation takes
+ SIZE bytes. */
+ T *register_instance_overhead (size_t size, const void *ptr);
+
+ /* For containers (and GGC) where we want to track every instance object,
+ we register allocation of SIZE bytes, identified by PTR pointer, belonging
+ to USAGE descriptor. */
+ void register_object_overhead (T *usage, size_t size, const void *ptr);
+
+ /* Release PTR pointer of SIZE bytes. If REMOVE_FROM_MAP is set to true,
+ remove the instance from reverse map. */
+ void release_instance_overhead (void *ptr, size_t size,
+ bool remove_from_map = false);
+
+ /* Release intance object identified by PTR pointer. */
+ void release_object_overhead (void *ptr);
+
+ /* Get sum value for ORIGIN type of allocation for the descriptor. */
+ T get_sum (mem_alloc_origin origin);
+
+ /* Get all tracked instances registered by the description. Items
+ are filtered by ORIGIN type, LENGTH is return value where we register
+ the number of elements in the list. If we want to process custom order,
+ CMP comparator can be provided. */
+ mem_list_t *get_list (mem_alloc_origin origin, unsigned *length,
+ int (*cmp) (const void *first, const void *second)
+ = NULL);
+
+ /* Dump all tracked instances of type ORIGIN. If we want to process custom
+ order, CMP comparator can be provided. */
+ void dump (mem_alloc_origin origin,
+ int (*cmp) (const void *first, const void *second) = NULL);
+
+ /* Reverse object map used for every object allocation mapping. */
+ reverse_object_map_t *m_reverse_object_map;
+
+private:
+ /* Register overhead of SIZE bytes of ORIGIN type. PTR pointer is allocated
+ in NAME source file, at LINE in source code, in FUNCTION. */
+ T *register_overhead (size_t size, mem_alloc_origin origin, const char *name,
+ int line, const char *function, const void *ptr);
+
+ /* Allocation location coupled to the description. */
+ mem_location m_location;
+
+ /* Location to usage mapping. */
+ mem_map_t *m_map;
+
+ /* Reverse pointer to usage mapping. */
+ reverse_mem_map_t *m_reverse_map;
+};
+
+#include "hash-map.h"
+
+/* Returns true if instance PTR is registered by the memory description. */
+
+template <class T>
+inline bool
+mem_alloc_description<T>::contains_descriptor_for_instance (const void *ptr)
+{
+ return m_reverse_map->get (ptr);
+}
+
+/* Return descriptor for instance PTR. */
+
+template <class T>
+inline T*
+mem_alloc_description<T>::get_descriptor_for_instance (const void *ptr)
+{
+ return m_reverse_map->get (ptr) ? (*m_reverse_map->get (ptr)).usage : NULL;
+}
+
+/* Register memory allocation descriptor for container PTR. ORIGIN identifies
+ type of container and GGC identifes if the allocation is handled in GGC
+ memory. Each location is identified by file NAME, LINE in source code and
+ FUNCTION name. */
+
+template <class T>
+inline T*
+mem_alloc_description<T>::register_descriptor (const void *ptr,
+ mem_alloc_origin origin,
+ bool ggc,
+ const char *filename,
+ int line,
+ const char *function)
+{
+ mem_location *l = new mem_location (filename, function, line, origin, ggc);
+ T *usage = NULL;
+
+ T **slot = m_map->get (l);
+ if (slot)
+ {
+ delete l;
+ usage = *slot;
+ }
+ else
+ {
+ usage = new T ();
+ m_map->put (l, usage);
+ }
+
+ if (!m_reverse_map->get (ptr))
+ m_reverse_map->put (ptr, mem_usage_pair<T> (usage, 0));
+
+ return usage;
+}
+
+/* Register instance overhead identified by PTR pointer. Allocation takes
+ SIZE bytes. */
+
+template <class T>
+inline T*
+mem_alloc_description<T>::register_instance_overhead (size_t size,
+ const void *ptr)
+{
+ mem_usage_pair <T> *slot = m_reverse_map->get (ptr);
+ if (!slot)
+ {
+ /* Due to PCH, it can really happen. */
+ return NULL;
+ }
+
+ T *usage = (*slot).usage;
+ usage->register_overhead (size);
+
+ return usage;
+}
+
+/* For containers (and GGC) where we want to track every instance object,
+ we register allocation of SIZE bytes, identified by PTR pointer, belonging
+ to USAGE descriptor. */
+
+template <class T>
+void
+mem_alloc_description<T>::register_object_overhead (T *usage, size_t size,
+ const void *ptr)
+{
+ /* In case of GGC, it is possible to have already occupied the memory
+ location. */
+ m_reverse_object_map->put (ptr, std::pair<T *, size_t> (usage, size));
+}
+
+/* Register overhead of SIZE bytes of ORIGIN type. PTR pointer is allocated
+ in NAME source file, at LINE in source code, in FUNCTION. */
+
+template <class T>
+inline T*
+mem_alloc_description<T>::register_overhead (size_t size,
+ mem_alloc_origin origin,
+ const char *filename,
+ int line,
+ const char *function,
+ const void *ptr)
+{
+ T *usage = register_descriptor (ptr, origin, filename, line, function);
+ usage->register_overhead (size);
+
+ return usage;
+}
+
+/* Release PTR pointer of SIZE bytes. */
+
+template <class T>
+inline void
+mem_alloc_description<T>::release_instance_overhead (void *ptr, size_t size,
+ bool remove_from_map)
+{
+ mem_usage_pair<T> *slot = m_reverse_map->get (ptr);
+
+ if (!slot)
+ {
+ /* Due to PCH, it can really happen. */
+ return;
+ }
+
+ mem_usage_pair<T> usage_pair = *slot;
+ usage_pair.usage->release_overhead (size);
+
+ if (remove_from_map)
+ m_reverse_map->remove (ptr);
+}
+
+/* Release intance object identified by PTR pointer. */
+
+template <class T>
+inline void
+mem_alloc_description<T>::release_object_overhead (void *ptr)
+{
+ std::pair <T *, size_t> *entry = m_reverse_object_map->get (ptr);
+ if (entry)
+ {
+ entry->first->release_overhead (entry->second);
+ m_reverse_object_map->remove (ptr);
+ }
+}
+
+/* Default contructor. */
+
+template <class T>
+inline
+mem_alloc_description<T>::mem_alloc_description ()
+{
+ m_map = new mem_map_t (13, false, false);
+ m_reverse_map = new reverse_mem_map_t (13, false, false);
+ m_reverse_object_map = new reverse_object_map_t (13, false, false);
+}
+
+/* Default destructor. */
+
+template <class T>
+inline
+mem_alloc_description<T>::~mem_alloc_description ()
+{
+ for (typename mem_map_t::iterator it = m_map->begin (); it != m_map->end ();
+ ++it)
+ {
+ delete (*it).first;
+ delete (*it).second;
+ }
+
+ delete m_map;
+ delete m_reverse_map;
+ delete m_reverse_object_map;
+}
+
+/* Get all tracked instances registered by the description. Items are filtered
+ by ORIGIN type, LENGTH is return value where we register the number of
+ elements in the list. If we want to process custom order, CMP comparator
+ can be provided. */
+
+template <class T>
+inline
+typename mem_alloc_description<T>::mem_list_t *
+mem_alloc_description<T>::get_list (mem_alloc_origin origin, unsigned *length,
+ int (*cmp) (const void *first, const void *second))
+{
+ /* vec data structure is not used because all vectors generate memory
+ allocation info a it would create a cycle. */
+ size_t element_size = sizeof (mem_list_t);
+ mem_list_t *list = XCNEWVEC (mem_list_t, m_map->elements ());
+ unsigned i = 0;
+
+ for (typename mem_map_t::iterator it = m_map->begin (); it != m_map->end ();
+ ++it)
+ if ((*it).first->m_origin == origin)
+ list[i++] = std::pair<mem_location*, T*> (*it);
+
+ qsort (list, i, element_size, cmp == NULL ? T::compare : cmp);
+ *length = i;
+
+ return list;
+}
+
+/* Get sum value for ORIGIN type of allocation for the descriptor. */
+
+template <class T>
+inline T
+mem_alloc_description<T>::get_sum (mem_alloc_origin origin)
+{
+ unsigned length;
+ mem_list_t *list = get_list (origin, &length);
+ T sum;
+
+ for (unsigned i = 0; i < length; i++)
+ sum = sum + *list[i].second;
+
+ XDELETEVEC (list);
+
+ return sum;
+}
+
+/* Dump all tracked instances of type ORIGIN. If we want to process custom
+ order, CMP comparator can be provided. */
+
+template <class T>
+inline void
+mem_alloc_description<T>::dump (mem_alloc_origin origin,
+ int (*cmp) (const void *first,
+ const void *second))
+{
+ unsigned length;
+
+ fprintf (stderr, "\n");
+
+ mem_list_t *list = get_list (origin, &length, cmp);
+ T total = get_sum (origin);
+
+ T::dump_header (mem_location::get_origin_name (origin));
+ for (int i = length - 1; i >= 0; i--)
+ list[i].second->dump (list[i].first, total);
+
+ total.dump_footer ();
+
+ XDELETEVEC (list);
+
+ fprintf (stderr, "\n");
+}
+
+#endif // GCC_MEM_STATS_H