aboutsummaryrefslogtreecommitdiff
path: root/libcpp
diff options
context:
space:
mode:
authorTom Tromey <tromey@redhat.com>2011-10-17 09:59:52 +0000
committerDodji Seketeli <dodji@gcc.gnu.org>2011-10-17 11:59:52 +0200
commit64a1a422dba4b0c09ad38310e32a223f2e16ed9d (patch)
treee11866266cf98c8e56a5c72d6c5ab06707407dc8 /libcpp
parent847e697a240e6d30335335c62084743978fa2084 (diff)
downloadgcc-64a1a422dba4b0c09ad38310e32a223f2e16ed9d.zip
gcc-64a1a422dba4b0c09ad38310e32a223f2e16ed9d.tar.gz
gcc-64a1a422dba4b0c09ad38310e32a223f2e16ed9d.tar.bz2
Add line map statistics to -fmem-report output
This patch adds statistics about line maps' memory consumption and macro expansion to the output of -fmem-report. It has been useful in trying to reduce the memory consumption of the macro maps support. Co-Authored-By: Dodji Seketeli <dodji@redhat.com> From-SVN: r180085
Diffstat (limited to 'libcpp')
-rw-r--r--libcpp/ChangeLog12
-rw-r--r--libcpp/include/line-map.h21
-rw-r--r--libcpp/line-map.c63
-rw-r--r--libcpp/macro.c29
4 files changed, 121 insertions, 4 deletions
diff --git a/libcpp/ChangeLog b/libcpp/ChangeLog
index bd86466..fad0da6 100644
--- a/libcpp/ChangeLog
+++ b/libcpp/ChangeLog
@@ -1,6 +1,18 @@
2011-10-15 Tom Tromey <tromey@redhat.com>
Dodji Seketeli <dodji@redhat.com>
+ * line-map.h (struct linemap_stats): Declare new struct.
+ (linemap_get_statistics): Declare ...
+ * line-map.c (linemap_get_statistics): ... new function.
+ * macro.c (num_expanded_macros_counter, num_macro_tokens_counter):
+ Declare new counters.
+ (enter_macro_context, replace_args): Update
+ num_macro_tokens_counter.
+ (cpp_get_token_1): Update num_expanded_macros_counter.
+
+2011-10-15 Tom Tromey <tromey@redhat.com>
+ Dodji Seketeli <dodji@redhat.com>
+
* include/cpplib.h (struct cpp_options)<debug>: New struct member.
* include/line-map.h (linemap_dump_location): Declare ...
* line-map.c (linemap_dump_location): ... new function.
diff --git a/libcpp/include/line-map.h b/libcpp/include/line-map.h
index 04a523c..572e330 100644
--- a/libcpp/include/line-map.h
+++ b/libcpp/include/line-map.h
@@ -675,6 +675,27 @@ expanded_location linemap_expand_location_full (struct line_maps *,
source_location loc,
enum location_resolution_kind lrk);
+/* Statistics about maps allocation and usage as returned by
+ linemap_get_statistics. */
+struct linemap_stats
+{
+ size_t num_ordinary_maps_allocated;
+ size_t num_ordinary_maps_used;
+ size_t ordinary_maps_allocated_size;
+ size_t ordinary_maps_used_size;
+ size_t num_expanded_macros;
+ size_t num_macro_tokens;
+ size_t num_macro_maps_used;
+ size_t macro_maps_allocated_size;
+ size_t macro_maps_used_size;
+ size_t macro_maps_locations_size;
+ size_t duplicated_macro_maps_locations_size;
+};
+
+/* Compute and return statistics about the memory consumption of some
+ parts of the line table SET. */
+void linemap_get_statistics (struct line_maps *, struct linemap_stats *);
+
/* Dump debugging information about source location LOC into the file
stream STREAM. SET is the line map set LOC comes from. */
void linemap_dump_location (struct line_maps *, source_location, FILE *);
diff --git a/libcpp/line-map.c b/libcpp/line-map.c
index 3dbaeab..9086b3e 100644
--- a/libcpp/line-map.c
+++ b/libcpp/line-map.c
@@ -46,6 +46,10 @@ static source_location linemap_macro_loc_to_exp_point (struct line_maps *,
source_location,
const struct line_map **);
+/* Counters defined in macro.c. */
+extern unsigned num_expanded_macros_counter;
+extern unsigned num_macro_tokens_counter;
+
/* Initialize a line map set. */
void
@@ -1143,3 +1147,62 @@ linemap_dump_location (struct line_maps *set,
fprintf (stream, "{P:%s;F:%s;L:%d;C:%d;S:%d;M:%p;E:%d,LOC:%d}",
path, from, l, c, s, (void*)map, e, loc);
}
+
+/* Compute and return statistics about the memory consumption of some
+ parts of the line table SET. */
+
+void
+linemap_get_statistics (struct line_maps *set,
+ struct linemap_stats *s)
+{
+ size_t ordinary_maps_allocated_size, ordinary_maps_used_size,
+ macro_maps_allocated_size, macro_maps_used_size,
+ macro_maps_locations_size = 0, duplicated_macro_maps_locations_size = 0;
+
+ struct line_map *cur_map;
+
+ ordinary_maps_allocated_size =
+ LINEMAPS_ORDINARY_ALLOCATED (set) * sizeof (struct line_map);
+
+ ordinary_maps_used_size =
+ LINEMAPS_ORDINARY_USED (set) * sizeof (struct line_map);
+
+ macro_maps_allocated_size =
+ LINEMAPS_MACRO_ALLOCATED (set) * sizeof (struct line_map);
+
+ for (cur_map = LINEMAPS_MACRO_MAPS (set);
+ cur_map && cur_map <= LINEMAPS_LAST_MACRO_MAP (set);
+ ++cur_map)
+ {
+ unsigned i;
+
+ linemap_assert (linemap_macro_expansion_map_p (cur_map));
+
+ macro_maps_locations_size +=
+ 2 * MACRO_MAP_NUM_MACRO_TOKENS (cur_map) * sizeof (source_location);
+
+ for (i = 0; i < 2 * MACRO_MAP_NUM_MACRO_TOKENS (cur_map); i += 2)
+ {
+ if (MACRO_MAP_LOCATIONS (cur_map)[i] ==
+ MACRO_MAP_LOCATIONS (cur_map)[i + 1])
+ duplicated_macro_maps_locations_size +=
+ sizeof (source_location);
+ }
+ }
+
+ macro_maps_used_size =
+ LINEMAPS_MACRO_USED (set) * sizeof (struct line_map);
+
+ s->num_ordinary_maps_allocated = LINEMAPS_ORDINARY_ALLOCATED (set);
+ s->num_ordinary_maps_used = LINEMAPS_ORDINARY_USED (set);
+ s->ordinary_maps_allocated_size = ordinary_maps_allocated_size;
+ s->ordinary_maps_used_size = ordinary_maps_used_size;
+ s->num_expanded_macros = num_expanded_macros_counter;
+ s->num_macro_tokens = num_macro_tokens_counter;
+ s->num_macro_maps_used = LINEMAPS_MACRO_USED (set);
+ s->macro_maps_allocated_size = macro_maps_allocated_size;
+ s->macro_maps_locations_size = macro_maps_locations_size;
+ s->macro_maps_used_size = macro_maps_used_size;
+ s->duplicated_macro_maps_locations_size =
+ duplicated_macro_maps_locations_size;
+}
diff --git a/libcpp/macro.c b/libcpp/macro.c
index d760383..2d0eeaa 100644
--- a/libcpp/macro.c
+++ b/libcpp/macro.c
@@ -165,6 +165,13 @@ static void consume_next_token_from_context (cpp_reader *pfile,
source_location *);
static const cpp_token* cpp_get_token_1 (cpp_reader *, source_location *);
+/* Statistical counter tracking the number of macros that got
+ expanded. */
+unsigned num_expanded_macros_counter = 0;
+/* Statistical counter tracking the total number tokens resulting
+ from macro expansion. */
+unsigned num_macro_tokens_counter = 0;
+
/* Emits a warning if NODE is a macro defined in the main file that
has not been used. */
int
@@ -1082,10 +1089,15 @@ enter_macro_context (cpp_reader *pfile, cpp_hashnode *node,
(const cpp_token **)
macro_tokens->base,
count);
+ num_macro_tokens_counter += count;
}
else
- _cpp_push_token_context (pfile, node, macro->exp.tokens,
- macro_real_token_count (macro));
+ {
+ unsigned tokens_count = macro_real_token_count (macro);
+ _cpp_push_token_context (pfile, node, macro->exp.tokens,
+ tokens_count);
+ num_macro_tokens_counter += tokens_count;
+ }
}
if (pragma_buff)
@@ -1095,13 +1107,18 @@ enter_macro_context (cpp_reader *pfile, cpp_hashnode *node,
padding_token (pfile, result), 1);
do
{
+ unsigned tokens_count;
_cpp_buff *tail = pragma_buff->next;
pragma_buff->next = NULL;
+ tokens_count = ((const cpp_token **) BUFF_FRONT (pragma_buff)
+ - (const cpp_token **) pragma_buff->base);
push_ptoken_context (pfile, NULL, pragma_buff,
(const cpp_token **) pragma_buff->base,
- ((const cpp_token **) BUFF_FRONT (pragma_buff)
- - (const cpp_token **) pragma_buff->base));
+ tokens_count);
pragma_buff = tail;
+ if (!CPP_OPTION (pfile, track_macro_expansion))
+ num_macro_tokens_counter += tokens_count;
+
}
while (pragma_buff != NULL);
return 2;
@@ -1711,6 +1728,8 @@ replace_args (cpp_reader *pfile, cpp_hashnode *node, cpp_macro *macro,
else
push_ptoken_context (pfile, node, buff, first,
tokens_buff_count (buff));
+
+ num_macro_tokens_counter += tokens_buff_count (buff);
}
/* Return a special padding token, with padding inherited from SOURCE. */
@@ -2240,6 +2259,8 @@ cpp_get_token_1 (cpp_reader *pfile, source_location *location)
}
else
{
+ if (pfile->context->c.macro)
+ ++num_expanded_macros_counter;
_cpp_pop_context (pfile);
if (pfile->state.in_directive)
continue;