Received: by 2002:a25:d7c1:0:0:0:0:0 with SMTP id o184csp3303254ybg; Sun, 20 Oct 2019 10:54:42 -0700 (PDT) X-Google-Smtp-Source: APXvYqzLGPuIiexJZYxtraPnQb+L1tgGqaDaVYyGXqScbG96V58syk8uwadJMoa8URow+tx+4isv X-Received: by 2002:a17:906:4a8d:: with SMTP id x13mr17817888eju.317.1571594082360; Sun, 20 Oct 2019 10:54:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1571594082; cv=none; d=google.com; s=arc-20160816; b=R0cF4yPkXwqLa1wTmqmnJSZjPz8qwK1FI20vkBmYLdlRFt56//WsdhQjE3w0ZYWeCd e1Sg75e68qwegusHedPZFt5WJrM7qL6bAZZIKzbwZ7MzbU7e0rxt/m6Kdq3PWXtO1jRI bNWj4gSJH5r/Zpnu75AKr6bv4j3rS/ojDiic7qks2PcmiQhNWdhmAQT01aun2Qjgef0F H8iGQ1ulgIoHSncwIJCGStRrnHOqIVkDyD69IjT/dE90sNAM5e/qe41sKpVHfToJ9LvB KSg4cF2I6LgjBNH5fZHkcXkDJ+pDKel7khhiPI12iJpL2e7/ktwjWoiI2+LXi4FMd5E6 jOIw== 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 :references:in-reply-to:message-id:date:subject:cc:to:from; bh=rK9WPvxEUNQSP07CgY5gRHILpsrBP3JPahYO0cwPRlE=; b=DA2EtJUmv/vCyHZhB2PBfQNTe9KNXrnZk5N4ZLuau6nwEjmhQ/Q3k6N7ckjJyMLloD pIFHDRlFSkyvUCIwcuKxxpu2Reu/gQNzwi57Xe8zoddfOZWKYsZ89DHqN0ItxFvwSBFv 9ceoCZYVEeXAvcMiTC0rPXaF6EmDwJWNnxLpwtEj0Qtw7R+I9tgv5WLAxPkRq5vn9T7q vMglpjNrU8EMSBRKvJXTzLgXCP0vmfTi2MnVb1F17BbSK6sUQgMXWUxpO6LbL7QvHRXG NAI0ZSafdj7UwRN70w9vAJhRBe82EXwecIY6eh3dps8wNqm6FOe4kRU747apxRLPs+ET b5aA== ARC-Authentication-Results: i=1; mx.google.com; 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 w20si8659090edc.202.2019.10.20.10.54.19; Sun, 20 Oct 2019 10:54:42 -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; 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 S1726805AbfJTRwb (ORCPT + 99 others); Sun, 20 Oct 2019 13:52:31 -0400 Received: from mga09.intel.com ([134.134.136.24]:24807 "EHLO mga09.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726740AbfJTRwb (ORCPT ); Sun, 20 Oct 2019 13:52:31 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga005.jf.intel.com ([10.7.209.41]) by orsmga102.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 20 Oct 2019 10:52:30 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.67,320,1566889200"; d="scan'208";a="371971244" Received: from tassilo.jf.intel.com (HELO tassilo.localdomain) ([10.7.201.137]) by orsmga005.jf.intel.com with ESMTP; 20 Oct 2019 10:52:30 -0700 Received: by tassilo.localdomain (Postfix, from userid 1000) id 38C8C30034D; Sun, 20 Oct 2019 10:52:30 -0700 (PDT) From: Andi Kleen To: acme@kernel.org Cc: linux-kernel@vger.kernel.org, jolsa@kernel.org, eranian@google.com, kan.liang@linux.intel.com, peterz@infradead.org, Andi Kleen Subject: [PATCH v2 9/9] perf stat: Use affinity for enabling/disabling events Date: Sun, 20 Oct 2019 10:52:02 -0700 Message-Id: <20191020175202.32456-10-andi@firstfloor.org> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20191020175202.32456-1-andi@firstfloor.org> References: <20191020175202.32456-1-andi@firstfloor.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Andi Kleen Restructure event enabling/disabling to use affinity, which minimizes the number of IPIs needed. Before on a large test case with 94 CPUs: % time seconds usecs/call calls errors syscall ------ ----------- ----------- --------- --------- ---------------- 54.65 1.899986 22 84812 660 ioctl after: 39.21 0.930451 10 84796 644 ioctl Signed-off-by: Andi Kleen --- tools/perf/lib/evsel.c | 49 ++++++++++++++++++++-------- tools/perf/lib/include/perf/evsel.h | 2 ++ tools/perf/util/evlist.c | 50 ++++++++++++++++++++++++++--- tools/perf/util/evsel.c | 13 ++++++++ tools/perf/util/evsel.h | 2 ++ 5 files changed, 98 insertions(+), 18 deletions(-) diff --git a/tools/perf/lib/evsel.c b/tools/perf/lib/evsel.c index ea775dacbd2d..89ddfade0b96 100644 --- a/tools/perf/lib/evsel.c +++ b/tools/perf/lib/evsel.c @@ -198,38 +198,61 @@ int perf_evsel__read(struct perf_evsel *evsel, int cpu, int thread, } static int perf_evsel__run_ioctl(struct perf_evsel *evsel, - int ioc, void *arg) + int ioc, void *arg, + int cpu) { - int cpu, thread; + int thread; - for (cpu = 0; cpu < xyarray__max_x(evsel->fd); cpu++) { - for (thread = 0; thread < xyarray__max_y(evsel->fd); thread++) { - int fd = FD(evsel, cpu, thread), - err = ioctl(fd, ioc, arg); + for (thread = 0; thread < xyarray__max_y(evsel->fd); thread++) { + int fd = FD(evsel, cpu, thread), + err = ioctl(fd, ioc, arg); - if (err) - return err; - } + if (err) + return err; } return 0; } +int perf_evsel__enable_cpu(struct perf_evsel *evsel, int cpu) +{ + return perf_evsel__run_ioctl(evsel, PERF_EVENT_IOC_ENABLE, 0, cpu); +} + int perf_evsel__enable(struct perf_evsel *evsel) { - return perf_evsel__run_ioctl(evsel, PERF_EVENT_IOC_ENABLE, 0); + int i; + int err = 0; + + for (i = 0; i < evsel->cpus->nr && !err; i++) + err = perf_evsel__run_ioctl(evsel, PERF_EVENT_IOC_ENABLE, 0, i); + return err; +} + +int perf_evsel__disable_cpu(struct perf_evsel *evsel, int cpu) +{ + return perf_evsel__run_ioctl(evsel, PERF_EVENT_IOC_DISABLE, 0, cpu); } int perf_evsel__disable(struct perf_evsel *evsel) { - return perf_evsel__run_ioctl(evsel, PERF_EVENT_IOC_DISABLE, 0); + int i; + int err = 0; + + for (i = 0; i < evsel->cpus->nr && !err; i++) + err = perf_evsel__run_ioctl(evsel, PERF_EVENT_IOC_DISABLE, 0, i); + return err; } int perf_evsel__apply_filter(struct perf_evsel *evsel, const char *filter) { - return perf_evsel__run_ioctl(evsel, + int err = 0, i; + + for (i = 0; i < evsel->cpus->nr && !err; i++) + err = perf_evsel__run_ioctl(evsel, PERF_EVENT_IOC_SET_FILTER, - (void *)filter); + (void *)filter, i); + return err; } struct perf_cpu_map *perf_evsel__cpus(struct perf_evsel *evsel) diff --git a/tools/perf/lib/include/perf/evsel.h b/tools/perf/lib/include/perf/evsel.h index ed10a914cd3f..db31e512a120 100644 --- a/tools/perf/lib/include/perf/evsel.h +++ b/tools/perf/lib/include/perf/evsel.h @@ -32,7 +32,9 @@ LIBPERF_API void perf_evsel__close_cpu(struct perf_evsel *evsel, int cpu); LIBPERF_API int perf_evsel__read(struct perf_evsel *evsel, int cpu, int thread, struct perf_counts_values *count); LIBPERF_API int perf_evsel__enable(struct perf_evsel *evsel); +LIBPERF_API int perf_evsel__enable_cpu(struct perf_evsel *evsel, int cpu); LIBPERF_API int perf_evsel__disable(struct perf_evsel *evsel); +LIBPERF_API int perf_evsel__disable_cpu(struct perf_evsel *evsel, int cpu); LIBPERF_API struct perf_cpu_map *perf_evsel__cpus(struct perf_evsel *evsel); LIBPERF_API struct perf_thread_map *perf_evsel__threads(struct perf_evsel *evsel); LIBPERF_API struct perf_event_attr *perf_evsel__attr(struct perf_evsel *evsel); diff --git a/tools/perf/util/evlist.c b/tools/perf/util/evlist.c index bcb8a3670f3f..55f38a71ad30 100644 --- a/tools/perf/util/evlist.c +++ b/tools/perf/util/evlist.c @@ -378,26 +378,66 @@ void evlist__cpu_iter_next(struct evsel *ev) void evlist__disable(struct evlist *evlist) { struct evsel *pos; + struct affinity affinity; + struct perf_cpu_map *cpus; + int i; + if (affinity__setup(&affinity) < 0) + return; + + cpus = evlist__cpu_iter_start(evlist); + for (i = 0; i < cpus->nr; i++) { + int cpu = cpus->map[i]; + affinity__set(&affinity, cpu); + + evlist__for_each_entry(evlist, pos) { + if (evlist__cpu_iter_skip(pos, cpu)) + continue; + if (pos->disabled || !perf_evsel__is_group_leader(pos) || !pos->core.fd) + continue; + evsel__disable_cpu(pos, pos->cpu_index); + evlist__cpu_iter_next(pos); + } + } + affinity__cleanup(&affinity); evlist__for_each_entry(evlist, pos) { - if (pos->disabled || !perf_evsel__is_group_leader(pos) || !pos->core.fd) + if (!perf_evsel__is_group_leader(pos) || !pos->core.fd) continue; - evsel__disable(pos); + pos->disabled = true; } - evlist->enabled = false; } void evlist__enable(struct evlist *evlist) { struct evsel *pos; + struct affinity affinity; + struct perf_cpu_map *cpus; + int i; + + if (affinity__setup(&affinity) < 0) + return; + + cpus = evlist__cpu_iter_start(evlist); + for (i = 0; i < cpus->nr; i++) { + int cpu = cpus->map[i]; + affinity__set(&affinity, cpu); + evlist__for_each_entry(evlist, pos) { + if (evlist__cpu_iter_skip(pos, cpu)) + continue; + if (!perf_evsel__is_group_leader(pos) || !pos->core.fd) + continue; + evsel__enable_cpu(pos, pos->cpu_index); + evlist__cpu_iter_next(pos); + } + } + affinity__cleanup(&affinity); evlist__for_each_entry(evlist, pos) { if (!perf_evsel__is_group_leader(pos) || !pos->core.fd) continue; - evsel__enable(pos); + pos->disabled = false; } - evlist->enabled = true; } diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c index 7106f9a067df..79050a6f4991 100644 --- a/tools/perf/util/evsel.c +++ b/tools/perf/util/evsel.c @@ -1205,13 +1205,26 @@ int perf_evsel__append_addr_filter(struct evsel *evsel, const char *filter) return perf_evsel__append_filter(evsel, "%s,%s", filter); } +/* Caller has to clear disabled after going through all CPUs. */ +int evsel__enable_cpu(struct evsel *evsel, int cpu) +{ + int err = perf_evsel__enable_cpu(&evsel->core, cpu); + return err; +} + int evsel__enable(struct evsel *evsel) { int err = perf_evsel__enable(&evsel->core); if (!err) evsel->disabled = false; + return err; +} +/* Caller has to set disabled after going through all CPUs. */ +int evsel__disable_cpu(struct evsel *evsel, int cpu) +{ + int err = perf_evsel__disable_cpu(&evsel->core, cpu); return err; } diff --git a/tools/perf/util/evsel.h b/tools/perf/util/evsel.h index 9fc9f6698aa4..15977bbe7b63 100644 --- a/tools/perf/util/evsel.h +++ b/tools/perf/util/evsel.h @@ -222,8 +222,10 @@ int perf_evsel__set_filter(struct evsel *evsel, const char *filter); int perf_evsel__append_tp_filter(struct evsel *evsel, const char *filter); int perf_evsel__append_addr_filter(struct evsel *evsel, const char *filter); +int evsel__enable_cpu(struct evsel *evsel, int cpu); int evsel__enable(struct evsel *evsel); int evsel__disable(struct evsel *evsel); +int evsel__disable_cpu(struct evsel *evsel, int cpu); int perf_evsel__open_per_cpu(struct evsel *evsel, struct perf_cpu_map *cpus, -- 2.21.0