public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Martin Liska <marxin@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc(refs/users/marxin/heads/gcov-dump-stable-opt)] gcov-dump: add --stable option
Date: Mon,  1 Aug 2022 13:51:54 +0000 (GMT)	[thread overview]
Message-ID: <20220801135154.8B972385841F@sourceware.org> (raw)

https://gcc.gnu.org/g:94c10e9b9088c22ad592d9ffa1537672bc6e24df

commit 94c10e9b9088c22ad592d9ffa1537672bc6e24df
Author: Martin Liska <mliska@suse.cz>
Date:   Mon Aug 1 15:50:43 2022 +0200

    gcov-dump: add --stable option
    
    The option prints TOP N counters in a stable format
    usage for comparison (diff).
    
    gcc/ChangeLog:
    
            * doc/gcov-dump.texi: Document the new option.
            * gcov-dump.cc (main): Parse the new option.
            (print_usage): Show the option.
            (tag_counters): Sort key:value pairs of TOP N counter.

Diff:
---
 gcc/doc/gcov-dump.texi |  5 +++++
 gcc/gcov-dump.cc       | 61 ++++++++++++++++++++++++++++++++++++++++++++------
 2 files changed, 59 insertions(+), 7 deletions(-)

diff --git a/gcc/doc/gcov-dump.texi b/gcc/doc/gcov-dump.texi
index 4f4e355dd28..0491ab17bc1 100644
--- a/gcc/doc/gcov-dump.texi
+++ b/gcc/doc/gcov-dump.texi
@@ -62,6 +62,7 @@ gcov-dump [@option{-v}|@option{--version}]
      [@option{-l}|@option{--long}]
      [@option{-p}|@option{--positions}]
      [@option{-r}|@option{--raw}]
+     [@option{-s}|@option{--stable}]
      @var{gcovfiles}
 @c man end
 @end ignore
@@ -85,6 +86,10 @@ Dump positions of records.
 @itemx --raw
 Print content records in raw format.
 
+@item -s
+@itemx --stable
+Print content in stable format usable for comparison.
+
 @item -v
 @itemx --version
 Display the @command{gcov-dump} version number (on the standard output),
diff --git a/gcc/gcov-dump.cc b/gcc/gcov-dump.cc
index 0804c794e9e..85b1be8859e 100644
--- a/gcc/gcov-dump.cc
+++ b/gcc/gcov-dump.cc
@@ -28,6 +28,10 @@ along with Gcov; see the file COPYING3.  If not see
 #include "gcov-io.h"
 #include "gcov-io.cc"
 
+#include <vector>
+
+using namespace std;
+
 static void dump_gcov_file (const char *);
 static void print_prefix (const char *, unsigned, gcov_position_t);
 static void print_usage (void);
@@ -50,6 +54,7 @@ typedef struct tag_format
 static int flag_dump_contents = 0;
 static int flag_dump_positions = 0;
 static int flag_dump_raw = 0;
+static int flag_dump_stable = 0;
 
 static const struct option options[] =
 {
@@ -57,7 +62,9 @@ static const struct option options[] =
   { "version",              no_argument,       NULL, 'v' },
   { "long",                 no_argument,       NULL, 'l' },
   { "positions",	    no_argument,       NULL, 'o' },
-  { 0, 0, 0, 0 }
+  { "raw",		    no_argument,       NULL, 'r' },
+  { "stable",		    no_argument,       NULL, 's' },
+  {}
 };
 
 #define VALUE_PADDING_PREFIX "              "
@@ -96,7 +103,7 @@ main (int argc ATTRIBUTE_UNUSED, char **argv)
 
   diagnostic_initialize (global_dc, 0);
 
-  while ((opt = getopt_long (argc, argv, "hlprvw", options, NULL)) != -1)
+  while ((opt = getopt_long (argc, argv, "hlprsvw", options, NULL)) != -1)
     {
       switch (opt)
 	{
@@ -115,6 +122,9 @@ main (int argc ATTRIBUTE_UNUSED, char **argv)
 	case 'r':
 	  flag_dump_raw = 1;
 	  break;
+	case 's':
+	  flag_dump_stable = 1;
+	  break;
 	default:
 	  fprintf (stderr, "unknown flag `%c'\n", opt);
 	}
@@ -134,6 +144,8 @@ print_usage (void)
   printf ("  -l, --long           Dump record contents too\n");
   printf ("  -p, --positions      Dump record positions\n");
   printf ("  -r, --raw            Print content records in raw format\n");
+  printf ("  -s, --stable         Print content in stable "
+	  "format usable for comparison\n");
   printf ("  -v, --version        Print version number\n");
   printf ("\nFor bug reporting instructions, please see:\n%s.\n",
 	   bug_report_url);
@@ -439,16 +451,52 @@ tag_counters (const char *filename ATTRIBUTE_UNUSED,
   int n_counts = GCOV_TAG_COUNTER_NUM (length);
   bool has_zeros = n_counts < 0;
   n_counts = abs (n_counts);
+  unsigned counter_idx = GCOV_COUNTER_FOR_TAG (tag);
 
   printf (" %s %u counts%s",
-	  counter_names[GCOV_COUNTER_FOR_TAG (tag)], n_counts,
+	  counter_names[counter_idx], n_counts,
 	  has_zeros ? " (all zero)" : "");
   if (flag_dump_contents)
     {
+      vector<gcov_type> counters;
       for (int ix = 0; ix != n_counts; ix++)
+	counters.push_back (has_zeros ? 0 : gcov_read_counter ());
+
+      /* Make stable sort for TOP N counters.  */
+      if (flag_dump_stable)
+	if (counter_idx == GCOV_COUNTER_V_INDIR
+	    || counter_idx == GCOV_COUNTER_V_TOPN)
+	  {
+	    unsigned start = 0;
+	    while (start < counters.size ())
+	      {
+		unsigned n = counters[start + 1];
+
+		/* Use bubble sort.  */
+		for (unsigned i = 1; i <= n; ++i)
+		  for (unsigned j = i; j <= n; ++j)
+		    {
+		      gcov_type key1 = counters[start + 2 * i];
+		      gcov_type value1 = counters[start + 2 * i + 1];
+		      gcov_type key2 = counters[start + 2 * j];
+		      gcov_type value2 = counters[start + 2 * j + 1];
+
+		      if (value1 < value2 || (value1 == value2 && key1 < key2))
+			{
+			  std::swap (counters[start + 2 * i],
+				     counters[start + 2 * j]);
+			  std::swap (counters[start + 2 * i + 1],
+				     counters[start + 2 * j + 1]);
+			}
+		    }
+		start += 2 * (n + 1);
+	      }
+	    if (start != counters.size ())
+	      abort ();
+	  }
+
+      for (unsigned ix = 0; ix < counters.size (); ++ix)
 	{
-	  gcov_type count;
-
 	  if (flag_dump_raw)
 	    {
 	      if (ix == 0)
@@ -461,8 +509,7 @@ tag_counters (const char *filename ATTRIBUTE_UNUSED,
 	      printf (VALUE_PADDING_PREFIX VALUE_PREFIX, ix);
 	    }
 
-	  count = has_zeros ? 0 : gcov_read_counter ();
-	  printf ("%" PRId64 " ", count);
+	  printf ("%" PRId64 " ", counters[ix]);
 	}
     }
 }


                 reply	other threads:[~2022-08-01 13:51 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20220801135154.8B972385841F@sourceware.org \
    --to=marxin@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    /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).