From: Dodji Seketeli <dodji@redhat.com>
To: gcc-patches@gcc.gnu.org
Cc: tromey@redhat.com, gdr@integrable-solutions.net,
joseph@codesourcery.com, burnus@net-b.de, charlet@act-europe.fr,
paolo@gnu.org, jason@redhat.com
Subject: [PATCH 5/7] Add line map statistics to -fmem-report output
Date: Sat, 16 Jul 2011 14:39:00 -0000 [thread overview]
Message-ID: <944ff7fc2917a6c7972be491d5ae31094cd33b00.1310824121.git.dodji@redhat.com> (raw)
In-Reply-To: <cover.1310812267.git.dodji@redhat.com>
In-Reply-To: <cover.1310824120.git.dodji@redhat.com>
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.
Tested on x86_64-unknown-linux-gnu against trunk.
gcc/
* input.c (ONE_K, ONE_M, SCALE, STAT_LABEL, FORMAT_AMOUNT): New
macros.
(num_expanded_macros_counter, num_macro_tokens_counter): Declare
new counters.
(dump_line_table_statistics): Define new function.
* input.h (dump_line_table_statistics): Declare new function.
* toplev.c (dump_memory_report): Call dump_line_table_statistics.
libcpp/
* 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.
---
gcc/input.c | 83 +++++++++++++++++++++++++++++++++++++++++++++
gcc/input.h | 2 +
gcc/toplev.c | 1 +
libcpp/include/line-map.h | 17 +++++++++
libcpp/line-map.c | 62 +++++++++++++++++++++++++++++++++
libcpp/macro.c | 29 +++++++++++++--
6 files changed, 190 insertions(+), 4 deletions(-)
diff --git a/gcc/input.c b/gcc/input.c
index f3c7bfa..614db3e 100644
--- a/gcc/input.c
+++ b/gcc/input.c
@@ -61,3 +61,86 @@ expand_location (source_location loc)
}
return xloc;
}
+
+#define ONE_K 1024
+#define ONE_M ONE_K * ONE_K
+
+/* Display a number as an integer multiple of either:
+ - 1024, if said integer is >= to 10 K (in base 2)
+ - 1024 * 1024, if said integer is >= 10 M in (base 2)
+ */
+#define SCALE(x) ((unsigned long) ((x) < 10 * ONE_K \
+ ? (x) \
+ : ((x) < 10 * ONE_M \
+ ? (x) / ONE_K \
+ : (x) / (ONE_M))))
+
+/* For a given integer, display either:
+ - the character 'k', if the number is higher than 10 K (in base 2)
+ but strictly lower than 10 M (in base 2)
+ - the character 'M' if the number is higher than 10 M (in base2)
+ - the charcter ' ' if the number is strictly lower than 10 K */
+#define STAT_LABEL(x) ((x) < 10 * ONE_K ? ' ' : ((x) < 10 * ONE_M ? 'k' : 'M'))
+
+/* Display an integer amount as multiple of 1K or 1M (in base 2à).
+ Display the correct unit (either k, M, or ' ') after the amout, as
+ well. */
+#define FORMAT_AMOUNT(size) SCALE (size), STAT_LABEL (size)
+
+/* Counters defined in libcpp's macro.c. */
+extern unsigned num_expanded_macros_counter;
+extern unsigned num_macro_tokens_counter;
+
+/* Dump statistics to stderr about the memory usage of the line_table
+ set of line maps. This also displays some statistics about macro
+ expansion. */
+
+void
+dump_line_table_statistics (void)
+{
+ struct linemap_stats s;
+
+ memset (&s, 0, sizeof (s));
+
+ fprintf (stderr, "Number of expanded macros: %u\n",
+ num_expanded_macros_counter);
+ if (num_expanded_macros_counter != 0)
+ fprintf (stderr, "Average number of tokens per macro expansion: %u\n",
+ num_macro_tokens_counter / num_expanded_macros_counter);
+ linemap_get_statistics (line_table, &s);
+
+ fprintf (stderr,
+ "\nLine Table allocations during the "
+ "compilation process\n");
+ fprintf (stderr, "Total allocated maps size: %5lu%c\n",
+ SCALE (s.total_allocated_map_size),
+ STAT_LABEL (s.total_allocated_map_size));
+ fprintf (stderr, "Total used maps size: %5lu%c\n",
+ SCALE (s.total_used_map_size),
+ STAT_LABEL (s.total_used_map_size));
+ fprintf (stderr, "Ordinary map used size: %5lu%c\n",
+ SCALE (s.ordinary_maps_used_size),
+ STAT_LABEL (s.ordinary_maps_used_size));
+ fprintf (stderr, "Macro maps used size: %5lu%c\n",
+ SCALE (s.macro_maps_used_size),
+ STAT_LABEL (s.macro_maps_used_size));
+ fprintf (stderr, "Number of ordinary maps allocated: %5lu%c\n",
+ SCALE (s.num_ordinary_maps_allocated),
+ STAT_LABEL (s.num_ordinary_maps_allocated));
+ fprintf (stderr, "Number of ordinary maps used: %5lu%c\n",
+ SCALE (s.num_ordinary_maps_used),
+ STAT_LABEL (s.num_ordinary_maps_used));
+ fprintf (stderr, "Number of macro maps used: %5lu%c\n",
+ SCALE (s.num_macro_maps_used),
+ STAT_LABEL (s.num_macro_maps_used));
+ fprintf (stderr, "Ordinary maps allocated size: %5lu%c\n",
+ SCALE (s.ordinary_maps_allocated_size),
+ STAT_LABEL (s.ordinary_maps_allocated_size));
+ fprintf (stderr, "Macro maps locations size: %5lu%c\n",
+ SCALE (s.macro_maps_locations_size),
+ STAT_LABEL (s.macro_maps_locations_size));
+ fprintf (stderr, "Duplicated maps locations size: %5lu%c\n",
+ SCALE (s.duplicated_macro_maps_locations_size),
+ STAT_LABEL (s.duplicated_macro_maps_locations_size));
+ fprintf (stderr, "\n");
+}
diff --git a/gcc/input.h b/gcc/input.h
index 835c95a..ca122b5 100644
--- a/gcc/input.h
+++ b/gcc/input.h
@@ -55,4 +55,6 @@ extern location_t input_location;
((linemap_location_in_system_header_p (line_table, LOC)))
#define in_system_header (in_system_header_at (input_location))
+void dump_line_table_statistics (void);
+
#endif
diff --git a/gcc/toplev.c b/gcc/toplev.c
index 9a98c5f..6626e7a 100644
--- a/gcc/toplev.c
+++ b/gcc/toplev.c
@@ -1788,6 +1788,7 @@ target_reinit (void)
void
dump_memory_report (bool final)
{
+ dump_line_table_statistics ();
ggc_print_statistics ();
stringpool_statistics ();
dump_tree_statistics ();
diff --git a/libcpp/include/line-map.h b/libcpp/include/line-map.h
index ba937d6..58ad98c 100644
--- a/libcpp/include/line-map.h
+++ b/libcpp/include/line-map.h
@@ -837,6 +837,23 @@ expanded_location linemap_expand_location_full (struct line_maps *,
enum location_resolution_kind lrk,
source_location *resulting_loc,
const struct line_map **loc_map);
+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_macro_maps_used;
+ size_t macro_maps_used_size;
+ size_t macro_maps_locations_size;
+ size_t duplicated_macro_maps_locations_size;
+ size_t total_allocated_map_size;
+ size_t total_used_map_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. */
diff --git a/libcpp/line-map.c b/libcpp/line-map.c
index 2bf743c..d69160c 100644
--- a/libcpp/line-map.c
+++ b/libcpp/line-map.c
@@ -836,3 +836,65 @@ 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);
}
+
+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,
+ total_allocated_map_size, total_used_map_size;
+ 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)
+ + macro_maps_locations_size;
+
+ total_used_map_size = ordinary_maps_used_size + macro_maps_used_size;
+
+ total_allocated_map_size =
+ ordinary_maps_allocated_size + macro_maps_allocated_size +
+ macro_maps_locations_size;
+
+ 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_macro_maps_used = LINEMAPS_MACRO_USED (set);
+ s->macro_maps_used_size = macro_maps_used_size;
+ s->macro_maps_locations_size = macro_maps_locations_size;
+ s->duplicated_macro_maps_locations_size =
+ duplicated_macro_maps_locations_size;
+ s->total_allocated_map_size = total_allocated_map_size;
+ s->total_used_map_size = total_used_map_size;
+}
diff --git a/libcpp/macro.c b/libcpp/macro.c
index 936e1b4..e4f941b 100644
--- a/libcpp/macro.c
+++ b/libcpp/macro.c
@@ -173,6 +173,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
@@ -1088,10 +1095,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)
@@ -1101,13 +1113,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;
@@ -1706,6 +1723,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. */
@@ -2250,6 +2269,8 @@ cpp_get_token_1 (cpp_reader *pfile, source_location *location)
}
else
{
+ if (pfile->context->macro)
+ ++num_expanded_macros_counter;
_cpp_pop_context (pfile);
if (pfile->state.in_directive)
continue;
--
1.7.6
next prev parent reply other threads:[~2011-07-16 14:38 UTC|newest]
Thread overview: 134+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-12-10 11:27 [PATCH 0/6] Tracking locations of tokens resulting from macro expansion Dodji Seketeli
2010-12-10 11:16 ` [PATCH 0/6] *** SUBJECT HERE *** Dodji Seketeli
2010-12-10 12:56 ` Dave Korn
2010-12-10 11:16 ` [PATCH 4/6] Support -fdebug-cpp option Dodji Seketeli
2010-12-10 11:27 ` [PATCH 3/6] Emit macro expansion related diagnostics Dodji Seketeli
2010-12-13 15:25 ` Paolo Bonzini
2010-12-13 15:38 ` Paolo Bonzini
2010-12-13 16:30 ` Manuel López-Ibáñez
2010-12-14 7:24 ` Dodji Seketeli
2010-12-14 7:28 ` Gabriel Dos Reis
2010-12-14 8:40 ` Dodji Seketeli
2010-12-14 9:38 ` Gabriel Dos Reis
2010-12-14 9:42 ` Dodji Seketeli
2010-12-14 9:48 ` Gabriel Dos Reis
2010-12-14 7:28 ` Dodji Seketeli
2010-12-14 8:19 ` Gabriel Dos Reis
2010-12-14 8:31 ` Paolo Bonzini
2010-12-14 9:23 ` Dodji Seketeli
2010-12-10 11:27 ` [PATCH 5/6] Add line map statistics to -fmem-report output Dodji Seketeli
2010-12-21 7:30 ` Gabriel Dos Reis
2011-04-13 20:08 ` Dodji Seketeli
2010-12-10 11:53 ` [PATCH 1/6] Linemap infrastructure for virtual locations Dodji Seketeli
2011-01-06 16:48 ` Tom Tromey
2011-04-12 14:39 ` Dodji Seketeli
2011-04-14 14:46 ` Tom Tromey
2010-12-10 12:27 ` [PATCH 2/6] Generate virtual locations for tokens Dodji Seketeli
2010-12-10 12:33 ` [PATCH 6/6] Kill pedantic warnings on system headers macros Dodji Seketeli
2010-12-10 12:52 ` [PATCH 0/6] Tracking locations of tokens resulting from macro expansion Gabriel Dos Reis
2010-12-10 18:22 ` Dodji Seketeli
2010-12-10 16:59 ` Jeff Law
2010-12-10 19:00 ` Dodji Seketeli
2010-12-13 15:10 ` Jeff Law
2010-12-13 16:35 ` Gabriel Dos Reis
2010-12-14 9:24 ` Dodji Seketeli
2010-12-14 9:40 ` Gabriel Dos Reis
2010-12-14 9:45 ` Dodji Seketeli
2011-07-16 14:38 ` [PATCH 0/7] " Dodji Seketeli
[not found] ` <cover.1310824120.git.dodji@redhat.com>
2011-07-16 14:38 ` [PATCH 3/7] Emit macro expansion related diagnostics Dodji Seketeli
2011-08-04 15:32 ` Dodji Seketeli
2011-09-12 21:54 ` Jason Merrill
2011-09-16 8:19 ` Dodji Seketeli
2011-09-17 21:27 ` Jason Merrill
2011-09-19 14:37 ` Dodji Seketeli
2011-09-19 19:47 ` Jason Merrill
2011-09-19 21:27 ` Jason Merrill
2011-09-20 8:47 ` Dodji Seketeli
2011-09-20 14:12 ` Jason Merrill
2011-09-20 14:12 ` Dodji Seketeli
2011-09-21 2:51 ` Jason Merrill
2011-09-21 19:09 ` Dodji Seketeli
2011-09-22 15:32 ` Jason Merrill
2011-09-26 21:11 ` Dodji Seketeli
2011-09-26 22:30 ` Jason Merrill
2011-09-27 18:43 ` Dodji Seketeli
2011-09-29 7:05 ` Jason Merrill
2011-09-29 19:20 ` Dodji Seketeli
2011-09-29 21:25 ` Jason Merrill
2011-09-29 23:33 ` Dodji Seketeli
2011-09-30 15:56 ` Jason Merrill
2011-09-30 21:04 ` Jason Merrill
2011-10-03 22:50 ` Dodji Seketeli
2011-10-04 19:59 ` Jason Merrill
2011-10-04 20:35 ` Dodji Seketeli
2011-10-03 20:09 ` Dodji Seketeli
2011-10-04 20:03 ` Jason Merrill
2011-10-04 20:28 ` Dodji Seketeli
2011-09-20 0:08 ` Dodji Seketeli
2011-07-16 14:38 ` [PATCH 6/7] Kill pedantic warnings on system headers macros Dodji Seketeli
2011-09-12 22:09 ` Jason Merrill
2011-09-16 11:25 ` Dodji Seketeli
2011-09-17 22:34 ` Jason Merrill
2011-09-18 18:59 ` Dodji Seketeli
2011-07-16 14:39 ` [PATCH 4/7] Support -fdebug-cpp option Dodji Seketeli
2011-08-21 11:02 ` Alexandre Oliva
2011-08-21 11:40 ` Jakub Jelinek
2011-08-22 14:45 ` Tom Tromey
2011-08-22 15:22 ` Jakub Jelinek
2011-08-23 19:52 ` Dodji Seketeli
2011-08-24 15:11 ` Tom Tromey
2011-09-12 22:07 ` Jason Merrill
2011-09-16 8:23 ` Dodji Seketeli
2011-09-17 22:01 ` Jason Merrill
2011-07-16 14:39 ` Dodji Seketeli [this message]
2011-09-12 22:07 ` [PATCH 5/7] Add line map statistics to -fmem-report output Jason Merrill
2011-09-16 8:29 ` Dodji Seketeli
2011-09-17 22:05 ` Jason Merrill
2011-09-20 12:10 ` Dodji Seketeli
2011-09-20 14:08 ` Jason Merrill
2011-07-16 15:25 ` [PATCH 2/7] Generate virtual locations for tokens Dodji Seketeli
2011-08-09 15:30 ` Dodji Seketeli
2011-09-12 21:15 ` Jason Merrill
2011-09-14 10:01 ` Dodji Seketeli
2011-09-14 22:56 ` Jason Merrill
2011-09-18 13:44 ` Dodji Seketeli
2011-09-19 22:31 ` Jason Merrill
2011-09-21 14:55 ` Dodji Seketeli
2011-09-22 17:10 ` Jason Merrill
2011-09-26 14:47 ` Dodji Seketeli
2011-09-26 20:39 ` Jason Merrill
2011-09-28 3:23 ` Dodji Seketeli
2011-09-28 14:49 ` Jason Merrill
2011-09-28 21:24 ` Dodji Seketeli
2011-09-28 21:45 ` Jason Merrill
2011-09-29 5:49 ` Dodji Seketeli
2011-07-16 15:28 ` [PATCH 1/7] Linemap infrastructure for virtual locations Dodji Seketeli
2011-07-18 22:06 ` Jason Merrill
2011-07-19 10:47 ` Dodji Seketeli
2011-07-19 17:26 ` Jason Merrill
2011-07-19 18:03 ` Dodji Seketeli
2011-07-19 23:37 ` Jason Merrill
2011-07-30 6:20 ` Jason Merrill
2011-08-01 18:54 ` Dodji Seketeli
2011-08-01 4:42 ` Jason Merrill
2011-08-02 4:48 ` Jason Merrill
2011-08-04 15:28 ` Dodji Seketeli
2011-08-04 21:30 ` Jason Merrill
2011-08-05 17:12 ` Dodji Seketeli
2011-08-05 17:31 ` Jason Merrill
2011-08-09 14:56 ` Dodji Seketeli
2011-08-19 8:46 ` Jason Merrill
2011-08-19 14:43 ` Tom Tromey
2011-09-01 10:37 ` Dodji Seketeli
2011-09-07 19:26 ` Jason Merrill
2011-09-08 12:41 ` Dodji Seketeli
2011-09-09 7:45 ` Jason Merrill
2011-09-09 8:57 ` Jason Merrill
2011-07-16 15:34 ` [PATCH 7/7] Reduce memory waste due to non-power-of-2 allocs Dodji Seketeli
2011-09-12 22:25 ` Jason Merrill
2011-09-17 13:41 ` Dodji Seketeli
2011-09-17 22:22 ` Jason Merrill
2011-09-18 22:30 ` Dodji Seketeli
2011-09-19 6:51 ` Laurynas Biveinis
2011-07-16 16:47 ` [PATCH 0/7] Tracking locations of tokens resulting from macro expansion Tobias Burnus
2011-07-16 17:57 ` Dodji Seketeli
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=944ff7fc2917a6c7972be491d5ae31094cd33b00.1310824121.git.dodji@redhat.com \
--to=dodji@redhat.com \
--cc=burnus@net-b.de \
--cc=charlet@act-europe.fr \
--cc=gcc-patches@gcc.gnu.org \
--cc=gdr@integrable-solutions.net \
--cc=jason@redhat.com \
--cc=joseph@codesourcery.com \
--cc=paolo@gnu.org \
--cc=tromey@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).