From patchwork Tue Aug 2 08:02:39 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Martin_Li=C5=A1ka?= X-Patchwork-Id: 56483 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A7835385381B for ; Tue, 2 Aug 2022 08:03:00 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from smtp-out2.suse.de (smtp-out2.suse.de [IPv6:2001:67c:2178:6::1d]) by sourceware.org (Postfix) with ESMTPS id BC59A3858D37 for ; Tue, 2 Aug 2022 08:02:42 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org BC59A3858D37 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=suse.cz Authentication-Results: sourceware.org; spf=fail smtp.mailfrom=suse.cz Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 68B45203F5 for ; Tue, 2 Aug 2022 08:02:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1659427360; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=Z9UbkisIOQhmjMXRPdQMBAOdV+c4EUbX0KyC93+jeAI=; b=fXM83h/ImywdffqWW1+BbBotFKYWc52YtGQPJe8QXD2a0FbY/nnx2Vi5BDUh+qjCyUqmT9 DDT6eEepgyYJWgXpxtHXIWRbVLS0fGrSVj7JqVA7KeWlB+3fID/zHvDxiNZyCSNehsESfg Corwe4iEUAnABJfZrN/iXmmTzR4cCzU= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1659427360; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=Z9UbkisIOQhmjMXRPdQMBAOdV+c4EUbX0KyC93+jeAI=; b=sEWnUzrh30GkMbiqnc/eepPnA65BE8PcW2dIsyu7XL56lUEe4D+mVe3ecw20rrFeSp8fx+ 5kjkRZVkwAyhT1BA== Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 5942C13A8E for ; Tue, 2 Aug 2022 08:02:40 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id OK8kECDa6GIZPwAAMHmgww (envelope-from ) for ; Tue, 02 Aug 2022 08:02:40 +0000 Message-ID: <2b638b91-a9e3-8d69-f1f6-1eaf222552fe@suse.cz> Date: Tue, 2 Aug 2022 10:02:39 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.11.0 From: =?utf-8?q?Martin_Li=C5=A1ka?= Subject: [PATCH] gcov-dump: add --stable option To: gcc-patches@gcc.gnu.org Content-Language: en-US X-Spam-Status: No, score=-11.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, SPF_HELO_NONE, SPF_SOFTFAIL, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" The option prints TOP N counters in a stable format usage for comparison (diff). Will install the patch if there are no objections. Patch can bootstrap on x86_64-linux-gnu and survives regression tests. Martin 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. --- 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 + +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 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]); } } }