Received: by 2002:a5b:505:0:0:0:0:0 with SMTP id o5csp1047992ybp; Thu, 17 Oct 2019 07:19:51 -0700 (PDT) X-Google-Smtp-Source: APXvYqzdLG0pj+wJhZICroIRhdhcPWMeBFwbopGR2errA8dzKFMVQhwJ2VaRgRUiqYDxV61Xyq0o X-Received: by 2002:a17:906:184e:: with SMTP id w14mr3744266eje.10.1571321991266; Thu, 17 Oct 2019 07:19:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1571321991; cv=none; d=google.com; s=arc-20160816; b=nTr7ddSQ7h0dIDUtcBBnorvKmQY0nlL/eGeE2eWUfBJjlaqvwN+wxXcGisCTsPFnjv x6DGCpBqq9yGoJpTW5KoytxvlKUn/5Irp4A7le6x8Zr38anHxeDGy9GOHLCj66tBxI/h V3cEWwAc54b0HR1b7M5pNIotvEGlBpNq7XSp9/AxqVXh6EQsWK4ttRFmNfEKVWK3paee e6nC3Psqua6TiIWE83F6CczNWdAPpABxRu2OKYWDMf7AafIaJ0D1jTYy/HL9/rGH2oDZ hepvT0TYNbeAjdt4vi+zRXJcE74/zrD7IHSiVdRECbwJSnk+6j8YE+evi5ysx4Ig0j3W fwdw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=nwZKQ03edb5WLfmaTGtvvdtmaD5kpl4xSB6rJW2Zf6M=; b=aRr9+BbpgOqXGNHBwrYPzbGXc5eDXcR6a2aA+fVXGPvp/kdVzVXJH8FmfmWTlA5IwQ hjdj3qVwxnlnj3ggObWwyRaVqsafWRCiD0t+LVpJjduJAyjhyjV3KSWUUMvy2jVfD1KF SS8CW0mirrd+dBoiolJRmfBnw9YF1HB2RWtd1qfOns3wh6BJa7P5zsOrndEuUc2UVQoz usNAkgoBmddq9IDruLH3uYd/5Y5uzEEI6OVgTFNbN6q/p5Xk3QPn5Zg5gZKQJ6UlzB6k TVEi4SvxT4YUu9szRLj2MP72fUYMpUgZbcO/T/eWF++9Sqi+BYAY9flF6X2xUv9Q6kAl zESg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b="IV/eFqhU"; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id b27si1572388ede.233.2019.10.17.07.19.28; Thu, 17 Oct 2019 07:19:51 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b="IV/eFqhU"; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2387436AbfJPVyj (ORCPT + 99 others); Wed, 16 Oct 2019 17:54:39 -0400 Received: from mail.kernel.org ([198.145.29.99]:44290 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2394985AbfJPVyZ (ORCPT ); Wed, 16 Oct 2019 17:54:25 -0400 Received: from localhost (unknown [192.55.54.58]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 521A421D7E; Wed, 16 Oct 2019 21:54:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1571262863; bh=h6wRVoLPUSm/SKyRy9VEyl8Eo7KmTBlKk3hM820NPoo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=IV/eFqhUMJAMqOQYS5e7k4r/UZkG9WNyEbdMKIUncuODupcY3N1EsUuWPMY7hUX2G RqWufyrDo/M558x0VMYMsJVEZUPHQ8ejyysOick6eaS5p4ZktoqQVc1lHT7M5eFvSL YUeF1ruYPJjDHhxiuTUCLki3Nt05R2+vwBX5fD30= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Srikar Dronamraju , Jiri Olsa , Arnaldo Carvalho de Melo , Ravi Bangoria , Namhyung Kim , "Naveen N. Rao" , Stephane Eranian , Sasha Levin Subject: [PATCH 4.9 29/92] perf stat: Reset previous counts on repeat with interval Date: Wed, 16 Oct 2019 14:50:02 -0700 Message-Id: <20191016214824.135274289@linuxfoundation.org> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20191016214759.600329427@linuxfoundation.org> References: <20191016214759.600329427@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Srikar Dronamraju [ Upstream commit b63fd11cced17fcb8e133def29001b0f6aaa5e06 ] When using 'perf stat' with repeat and interval option, it shows wrong values for events. The wrong values will be shown for the first interval on the second and subsequent repetitions. Without the fix: # perf stat -r 3 -I 2000 -e faults -e sched:sched_switch -a sleep 5 2.000282489 53 faults 2.000282489 513 sched:sched_switch 4.005478208 3,721 faults 4.005478208 2,666 sched:sched_switch 5.025470933 395 faults 5.025470933 1,307 sched:sched_switch 2.009602825 1,84,46,74,40,73,70,95,47,520 faults <------ 2.009602825 1,84,46,74,40,73,70,95,49,568 sched:sched_switch <------ 4.019612206 4,730 faults 4.019612206 2,746 sched:sched_switch 5.039615484 3,953 faults 5.039615484 1,496 sched:sched_switch 2.000274620 1,84,46,74,40,73,70,95,47,520 faults <------ 2.000274620 1,84,46,74,40,73,70,95,47,520 sched:sched_switch <------ 4.000480342 4,282 faults 4.000480342 2,303 sched:sched_switch 5.000916811 1,322 faults 5.000916811 1,064 sched:sched_switch # prev_raw_counts is allocated when using intervals. This is used when calculating the difference in the counts of events when using interval. The current counts are stored in prev_raw_counts to calculate the differences in the next iteration. On the first interval of the second and subsequent repetitions, prev_raw_counts would be the values stored in the last interval of the previous repetitions, while the current counts will only be for the first interval of the current repetition. Hence there is a possibility of events showing up as big number. Fix this by resetting prev_raw_counts whenever perf stat repeats the command. With the fix: # perf stat -r 3 -I 2000 -e faults -e sched:sched_switch -a sleep 5 2.019349347 2,597 faults 2.019349347 2,753 sched:sched_switch 4.019577372 3,098 faults 4.019577372 2,532 sched:sched_switch 5.019415481 1,879 faults 5.019415481 1,356 sched:sched_switch 2.000178813 8,468 faults 2.000178813 2,254 sched:sched_switch 4.000404621 7,440 faults 4.000404621 1,266 sched:sched_switch 5.040196079 2,458 faults 5.040196079 556 sched:sched_switch 2.000191939 6,870 faults 2.000191939 1,170 sched:sched_switch 4.000414103 541 faults 4.000414103 902 sched:sched_switch 5.000809863 450 faults 5.000809863 364 sched:sched_switch # Committer notes: This was broken since the cset introducing the --interval feature, i.e. --repeat + --interval wasn't tested at that point, add the Fixes tag so that automatic scripts can pick this up. Fixes: 13370a9b5bb8 ("perf stat: Add interval printing") Signed-off-by: Srikar Dronamraju Acked-by: Jiri Olsa Tested-by: Arnaldo Carvalho de Melo Tested-by: Ravi Bangoria Cc: Namhyung Kim Cc: Naveen N. Rao Cc: Stephane Eranian Cc: stable@vger.kernel.org # v3.9+ Link: http://lore.kernel.org/lkml/20190904094738.9558-2-srikar@linux.vnet.ibm.com [ Fixed up conflicts with libperf, i.e. some perf_{evsel,evlist} lost the 'perf' prefix ] Signed-off-by: Arnaldo Carvalho de Melo Signed-off-by: Sasha Levin --- tools/perf/builtin-stat.c | 3 +++ tools/perf/util/stat.c | 17 +++++++++++++++++ tools/perf/util/stat.h | 1 + 3 files changed, 21 insertions(+) diff --git a/tools/perf/builtin-stat.c b/tools/perf/builtin-stat.c index e55dbceadad6c..5cb58f3afa355 100644 --- a/tools/perf/builtin-stat.c +++ b/tools/perf/builtin-stat.c @@ -2564,6 +2564,9 @@ int cmd_stat(int argc, const char **argv, const char *prefix __maybe_unused) fprintf(output, "[ perf stat: executing run #%d ... ]\n", run_idx + 1); + if (run_idx != 0) + perf_evlist__reset_prev_raw_counts(evsel_list); + status = run_perf_stat(argc, argv); if (forever && status != -1 && !interval) { print_counters(NULL, argc, argv); diff --git a/tools/perf/util/stat.c b/tools/perf/util/stat.c index 39345c2ddfc22..d4f872f1750e6 100644 --- a/tools/perf/util/stat.c +++ b/tools/perf/util/stat.c @@ -145,6 +145,15 @@ static void perf_evsel__free_prev_raw_counts(struct perf_evsel *evsel) evsel->prev_raw_counts = NULL; } +static void perf_evsel__reset_prev_raw_counts(struct perf_evsel *evsel) +{ + if (evsel->prev_raw_counts) { + evsel->prev_raw_counts->aggr.val = 0; + evsel->prev_raw_counts->aggr.ena = 0; + evsel->prev_raw_counts->aggr.run = 0; + } +} + static int perf_evsel__alloc_stats(struct perf_evsel *evsel, bool alloc_raw) { int ncpus = perf_evsel__nr_cpus(evsel); @@ -195,6 +204,14 @@ void perf_evlist__reset_stats(struct perf_evlist *evlist) } } +void perf_evlist__reset_prev_raw_counts(struct perf_evlist *evlist) +{ + struct perf_evsel *evsel; + + evlist__for_each_entry(evlist, evsel) + perf_evsel__reset_prev_raw_counts(evsel); +} + static void zero_per_pkg(struct perf_evsel *counter) { if (counter->per_pkg_mask) diff --git a/tools/perf/util/stat.h b/tools/perf/util/stat.h index c29bb94c48a4b..b8845aceac31a 100644 --- a/tools/perf/util/stat.h +++ b/tools/perf/util/stat.h @@ -94,6 +94,7 @@ void perf_stat__print_shadow_stats(struct perf_evsel *evsel, int perf_evlist__alloc_stats(struct perf_evlist *evlist, bool alloc_raw); void perf_evlist__free_stats(struct perf_evlist *evlist); void perf_evlist__reset_stats(struct perf_evlist *evlist); +void perf_evlist__reset_prev_raw_counts(struct perf_evlist *evlist); int perf_stat_process_counter(struct perf_stat_config *config, struct perf_evsel *counter); -- 2.20.1