Received: by 2002:a25:d7c1:0:0:0:0:0 with SMTP id o184csp3305022ybg; Sun, 20 Oct 2019 10:57:19 -0700 (PDT) X-Google-Smtp-Source: APXvYqzyIAcOY0c0jCX4BcN9qUPkI5I042xkhVi+Hu/NlPWWAamBYbrv8COMzH1Cz7zX7NJBlWAP X-Received: by 2002:a05:6402:21d6:: with SMTP id bi22mr20501314edb.19.1571594239279; Sun, 20 Oct 2019 10:57:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1571594239; cv=none; d=google.com; s=arc-20160816; b=FUuIhXR4W0Eu7vOmkVFuph1lodCvyyo+yjIW1xZ34XXmd4DTdh9H9JQIVJ9YJbGHdE 4Zdgdqe/VJYH5lEb+inT4ucHwfAERNbVcXR4V6iIuNXNov7wAEzC6eOLpLqSBgM42bXW d8gAtHki0qryrVtGhQtxrPgt+VB85GekeIu5cjFLsmuZTbhXsZbE9suum0ffHhRQdLzV pjcSJp6Z2LE97pj7JsmKI/MdRbs+ANhpKdzHHMLmdZPuiJGfmIO5lvENBaFYogQSPDW2 RjOaIjAXFp8KEmWTLNJjL7bCg3d4cBJsvuQoDq8x0PJ4ZDWWxa7yacU0b8JEa4JOfA87 11JA== 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=7YlXXZo/VHXFnZoD5WPrOucUz+i6xllrc4W/tGWpC2o=; b=OZn7WU2B7xasf77jX5DjoA0Jf1R3NofI3CvY/A35rNoizrnfNxeHulAUpQZvH3U2qh sOYBR9bSqswVAYDbTJoHWWaORAlAj2rQ4oJsv7Aeq1eItO6rRWCOTCen3880oC+9efxl snbpSMAfMeMgT/IsouMKeMET0ivQOjoeEOQlHJE/EeH6vup+g9gOjc2VOPAHlybOYCTH vjcL2gQWusDkwc+VmS47uS3cRmJ+Nm+JYnvpH4Gd7FR/jaxmB3x4kLGFQ7oWeh4Z/7Hc gO8Szn9WZG+5HeLuAh2B5LXKita1r9hc5v8jjHPcDLXvd5MSxG47hKKYH2crZjFcXegb yvpw== 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 q17si7346696ejx.139.2019.10.20.10.56.55; Sun, 20 Oct 2019 10:57:19 -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 S1726770AbfJTRwb (ORCPT + 99 others); Sun, 20 Oct 2019 13:52:31 -0400 Received: from mga01.intel.com ([192.55.52.88]:3315 "EHLO mga01.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726596AbfJTRwb (ORCPT ); Sun, 20 Oct 2019 13:52:31 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga008.jf.intel.com ([10.7.209.65]) by fmsmga101.fm.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="190893276" Received: from tassilo.jf.intel.com (HELO tassilo.localdomain) ([10.7.201.137]) by orsmga008.jf.intel.com with ESMTP; 20 Oct 2019 10:52:30 -0700 Received: by tassilo.localdomain (Postfix, from userid 1000) id 2549B300393; 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 6/9] perf stat: Use affinity for closing file descriptors Date: Sun, 20 Oct 2019 10:51:59 -0700 Message-Id: <20191020175202.32456-7-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 Closing a perf fd can also trigger an IPI to the target CPU. Use the same affinity technique as we use for reading/enabling events to closing to optimize the CPU transitions. Before on a large test case with 94 CPUs: % time seconds usecs/call calls errors syscall ------ ----------- ----------- --------- --------- ---------------- 32.56 3.085463 50 61483 close After: 10.54 0.735704 11 61485 close Signed-off-by: Andi Kleen --- tools/perf/lib/evsel.c | 27 +++++++++++++++++++------ tools/perf/lib/include/perf/evsel.h | 1 + tools/perf/util/evlist.c | 31 +++++++++++++++++++++++++++-- tools/perf/util/evsel.h | 1 + 4 files changed, 52 insertions(+), 8 deletions(-) diff --git a/tools/perf/lib/evsel.c b/tools/perf/lib/evsel.c index 5a89857b0381..ea775dacbd2d 100644 --- a/tools/perf/lib/evsel.c +++ b/tools/perf/lib/evsel.c @@ -114,16 +114,23 @@ int perf_evsel__open(struct perf_evsel *evsel, struct perf_cpu_map *cpus, return err; } +static void perf_evsel__close_fd_cpu(struct perf_evsel *evsel, int cpu) +{ + int thread; + + for (thread = 0; thread < xyarray__max_y(evsel->fd); ++thread) { + if (FD(evsel, cpu, thread) >= 0) + close(FD(evsel, cpu, thread)); + FD(evsel, cpu, thread) = -1; + } +} + void perf_evsel__close_fd(struct perf_evsel *evsel) { - int cpu, thread; + int cpu; for (cpu = 0; cpu < xyarray__max_x(evsel->fd); cpu++) - for (thread = 0; thread < xyarray__max_y(evsel->fd); ++thread) { - if (FD(evsel, cpu, thread) >= 0) - close(FD(evsel, cpu, thread)); - FD(evsel, cpu, thread) = -1; - } + perf_evsel__close_fd_cpu(evsel, cpu); } void perf_evsel__free_fd(struct perf_evsel *evsel) @@ -141,6 +148,14 @@ void perf_evsel__close(struct perf_evsel *evsel) perf_evsel__free_fd(evsel); } +void perf_evsel__close_cpu(struct perf_evsel *evsel, int cpu) +{ + if (evsel->fd == NULL) + return; + + perf_evsel__close_fd_cpu(evsel, cpu); +} + int perf_evsel__read_size(struct perf_evsel *evsel) { u64 read_format = evsel->attr.read_format; diff --git a/tools/perf/lib/include/perf/evsel.h b/tools/perf/lib/include/perf/evsel.h index 4388667f265c..ed10a914cd3f 100644 --- a/tools/perf/lib/include/perf/evsel.h +++ b/tools/perf/lib/include/perf/evsel.h @@ -28,6 +28,7 @@ LIBPERF_API void perf_evsel__delete(struct perf_evsel *evsel); LIBPERF_API int perf_evsel__open(struct perf_evsel *evsel, struct perf_cpu_map *cpus, struct perf_thread_map *threads); LIBPERF_API void perf_evsel__close(struct perf_evsel *evsel); +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); diff --git a/tools/perf/util/evlist.c b/tools/perf/util/evlist.c index 27b4b958eddd..b1b29d473a9f 100644 --- a/tools/perf/util/evlist.c +++ b/tools/perf/util/evlist.c @@ -18,6 +18,7 @@ #include "debug.h" #include "units.h" #include // page_size +#include "affinity.h" #include "../perf.h" #include "asm/bug.h" #include "bpf-event.h" @@ -1174,9 +1175,35 @@ void perf_evlist__set_selected(struct evlist *evlist, void evlist__close(struct evlist *evlist) { struct evsel *evsel; + struct affinity affinity; + struct perf_cpu_map *cpus; + int i; + + /* So far record doesn't set this up */ + if (!evlist->core.cpus) { + evlist__for_each_entry_reverse(evlist, evsel) + evsel__close(evsel); + return; + } - evlist__for_each_entry_reverse(evlist, evsel) - evsel__close(evsel); + 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_reverse(evlist, evsel) { + if (evlist__cpu_iter_skip(evsel, cpu)) + continue; + perf_evsel__close_cpu(&evsel->core, evsel->cpu_index); + evlist__cpu_iter_next(evsel); + } + } + evlist__for_each_entry_reverse(evlist, evsel) { + perf_evsel__free_fd(&evsel->core); + perf_evsel__free_id(&evsel->core); + } } static int perf_evlist__create_syswide_maps(struct evlist *evlist) diff --git a/tools/perf/util/evsel.h b/tools/perf/util/evsel.h index cf90019ae744..2e3b011ed09e 100644 --- a/tools/perf/util/evsel.h +++ b/tools/perf/util/evsel.h @@ -391,4 +391,5 @@ static inline bool evsel__has_callchain(const struct evsel *evsel) struct perf_env *perf_evsel__env(struct evsel *evsel); int perf_evsel__store_ids(struct evsel *evsel, struct evlist *evlist); + #endif /* __PERF_EVSEL_H */ -- 2.21.0