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: 343 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:6a10:b5d6:b0:2b9:3548:2db5 with SMTP id v22csp2823849pxt; Tue, 2 Aug 2022 01:03:11 -0700 (PDT) X-Google-Smtp-Source: AGRyM1tKuVgbo0OmF1JjdS4E9zR+AKMBjjDzsEoIfy0IKtXqOWxkOcaCNkcCNHWAMVnqnf2Czuoo X-Received: by 2002:a17:907:6089:b0:72f:56db:ccb6 with SMTP id ht9-20020a170907608900b0072f56dbccb6mr14981431ejc.318.1659427390960; Tue, 02 Aug 2022 01:03:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1659427390; cv=none; d=google.com; s=arc-20160816; b=X9IeC6vkMg0HET2cZ3c1r7wEYlvx1A6hb4TThz98XNUcH+H4QX0iqx+rSjdM5a4J3J KEwH3xr6fxAdgjI1K8wspAfADFfzF4ut4Ws9av25zAiFU+YyT52kLjd5XzaUDuG84cbs tMssAULwjGGI0gY8EKu0qHr688D9R1YtfvuPjf3kDJZhAZKvT32CQ60teYD1pJmd77D1 Dx8BEUZmIHtJoVH84u6h0yiLbpv3tAZhRDtEO+DKoG3RLSKVM7JgM/9gXoXzin+3JlzI EqcpkTtkYRafRclzYlnPFjtaeDQV2cjcS0KGaqziN6bh8UK5yKBQDpGbDUYifUPtINJ+ bc7A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :content-language:to:subject:from:user-agent:mime-version:date :message-id:dkim-signature:dkim-signature:dmarc-filter:delivered-to; bh=Z9UbkisIOQhmjMXRPdQMBAOdV+c4EUbX0KyC93+jeAI=; b=ZdAzPqZK1axLfHTwDcvn9I+oitqKIYv3gxtjVZpP9GE8V3KoUvLXavyDg9993IfXpW SiWYVdwhL761IhwVWk/gvN1gouGqwOkvNK6TdWmRe1ZvkmEYtO0Lpc6iJkRpWAh8uc5l k8iX/kPBUrhRwOBPLnIt1RmlPN2wVdjSPa9aphdK8hjpFxEQfEBhJMYfjJ24dpXZO4JG hon2QvudBZ5Ox3/Uez/6ps3apIWteMbO3yeMuOVFe89eZzMEYO3U5jN1qF4IDGBLhmqU ttA69EZlq4+zS1w+abBHigs0Ptx+PYViDGrk+X59VherXUsmzNEoYDlTs8JubyLrAS/r tibg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.cz header.s=susede2_rsa header.b="fXM83h/I"; dkim=neutral (no key) header.i=@suse.cz header.b=sEWnUzrh; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id m20-20020a170906581400b007156fe1aa6asi11613713ejq.650.2022.08.02.01.03.10 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 02 Aug 2022 01:03:10 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@suse.cz header.s=susede2_rsa header.b="fXM83h/I"; dkim=neutral (no key) header.i=@suse.cz header.b=sEWnUzrh; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id F075C3852743 for ; Tue, 2 Aug 2022 08:03:08 +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+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1740035735724308589?= X-GMAIL-MSGID: =?utf-8?q?1740035735724308589?= 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]); } } }