Received: by 2002:a05:6a10:1d13:0:0:0:0 with SMTP id pp19csp278775pxb; Sat, 21 Aug 2021 02:22:43 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw+H3pjg1HfKGII8oOZPVxOdybZqfZjo7w5WAjq4ttm6xVu2Li+qESsm6ZBJ3b+gDCzdvWp X-Received: by 2002:a05:6e02:1250:: with SMTP id j16mr16933902ilq.215.1629537763594; Sat, 21 Aug 2021 02:22:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1629537763; cv=none; d=google.com; s=arc-20160816; b=hNEtVe2MjGv1hBL2LzyTba33MF49qf9gGTO0V4DZj03O6Wia1xQuABoawEbJJLvHqj B4outc4WvyJhClm81avC2Ecju0csaZpx82JnTLYTJZgvKfDjEvAiwUj94Za7+eZZH/TA 0yKwu70E5jEoAPNBFTnxv/SucewmeTuIypEaSpPmy/k5f6plbo4jU0BhoNGqB7SyAcQx Br2Ol7VmJzpjgRYPhqAcBCiconpXKRTEbo61bu6buJi2aKLsO3ik2WVWK9y1nferA3Qi 611NB1aCj7HOfWJ5p2QETaKCPAutbDUMzv6AKRbLWwzJqkTWYjQwK39223C/dgMm3xU8 8VAw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=nYCZWFJIc++fa6HHlc4YfaHaR0UFJ1cN64ckPkTe/mE=; b=c8c6bEq1HeldoEPzwiZSrsNGfoRwwuR3Rq5s6mHJbD0EyYShCIf/FSJsJ2lTYuja1i Y+wzcQGZ0etvXsqkqUNf+PY2E3kB3AGYZMHj/yruatisRfNZewvSAfcquy19oHhlO0B1 OE2jhjdvO0rr+Z3L0ygW/tHfRF+vdoYE2Vu5ACywLN6w2i46+WFZUO8+2HNH9UwPLpmc hMxH7FfTn28DggPDoNdHgvlYG7bjGA/nuMW2PcUmoq5fVWXqo+8fWciTSnXTjZYva5O3 jXBdxmzAwgcdH3rP0Zo7NUyIhlWDJ92pcDyT9DDCpUm0SqyIcIZRjjjFRV9WfCYSb4vc LERw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=YnreBb68; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id y59si9336080jah.22.2021.08.21.02.22.32; Sat, 21 Aug 2021 02:22:43 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=YnreBb68; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234124AbhHUJVQ (ORCPT + 99 others); Sat, 21 Aug 2021 05:21:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34094 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234134AbhHUJUs (ORCPT ); Sat, 21 Aug 2021 05:20:48 -0400 Received: from mail-ej1-x62d.google.com (mail-ej1-x62d.google.com [IPv6:2a00:1450:4864:20::62d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0B729C0612E7; Sat, 21 Aug 2021 02:20:02 -0700 (PDT) Received: by mail-ej1-x62d.google.com with SMTP id w5so25224867ejq.2; Sat, 21 Aug 2021 02:20:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=nYCZWFJIc++fa6HHlc4YfaHaR0UFJ1cN64ckPkTe/mE=; b=YnreBb68LiGNZPOjvwGEyR6jPj/i+sVhr1Z7zn2xSsRcwjbhgJN+AXLDnUEbfIHyJK Mm+RFqaxbHNsKJbj0rP/6inIPRCoJInpdOcV3tHEWWaV/BiywMyIBlG+TbIjp0e3FaLb VUZ+twcHjZnYLHiD6CQxRVjvFFct6zJkUFiX5T8bX6LTPNngQJHhdq+wNlApAYxA8BPn ckEHKjIBcA51FJG9ODsna2tkPVCNvsD1IA3Tx8igZJ8stXn5tvF+CJOEU2viagGGigkT l5iEjyQ3ZzYaLfkfwjaKjs08ScoC4hC4ClAvWenZfPwd5nZ/J8dffIDweqXnccbMYskQ oXzg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=nYCZWFJIc++fa6HHlc4YfaHaR0UFJ1cN64ckPkTe/mE=; b=q8lXm4qas2wK8DfdtdpE/mUqEcioWbeZDFFPneJGNbWOIHZTkPmY3J3xiD+cHzZMVC f172eznvD9ZAqpHrBX2BFIlFvn24FOotrBuT71zLoo3OjtrJ5h8vo0ufKzUsmtRPvSPE CRAxZh/7nJUVO6FEdlyAXB1DzHOxO3xiUUp9VDAL532i0/Etbxv067DGUsb7k3tNqcrJ B0/IMvpVQFl7vAcuu27Rj3OU0OrbOIL3lYujWa7dXKAX1WuOI1M5tdXZHudU+ibHnsFn Cv2/BRP20l0SphLDHxS6TbhXMvl5xZSYPh+zxtxL4xmIS2PKYuDCHdNE4lVg7KFaxBuO IVlQ== X-Gm-Message-State: AOAM530V56Nya0ecnwCkTQheSOvbMC8rOmbS73OXzZyD66rIwheGroOs gt+0I4d32aziUmlMc+1XtXY= X-Received: by 2002:a17:906:408d:: with SMTP id u13mr26070763ejj.344.1629537600562; Sat, 21 Aug 2021 02:20:00 -0700 (PDT) Received: from honeypot.. (mob-176-243-254-204.net.vodafone.it. [176.243.254.204]) by smtp.googlemail.com with ESMTPSA id a25sm4003878ejv.91.2021.08.21.02.19.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 21 Aug 2021 02:20:00 -0700 (PDT) From: Riccardo Mancini To: Arnaldo Carvalho de Melo Cc: Ian Rogers , Namhyung Kim , Peter Zijlstra , Ingo Molnar , Mark Rutland , Jiri Olsa , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, Riccardo Mancini Subject: [RFC PATCH v1 10/37] perf evlist: add multithreading helper Date: Sat, 21 Aug 2021 11:19:16 +0200 Message-Id: <34d8ec75acb56fbbbef08fee8be8404015e682cd.1629490974.git.rickyman7@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This patch adds the function evlist__for_each_evsel_cpu, which executes the given function on each evsel, for each cpu. If perf_singlethreaded is unset, this function will use a workqueue to execute the function. This helper function will be used in the following patches. Signed-off-by: Riccardo Mancini --- tools/perf/util/evlist.c | 117 +++++++++++++++++++++++++++++++++++++++ tools/perf/util/evlist.h | 14 +++++ 2 files changed, 131 insertions(+) diff --git a/tools/perf/util/evlist.c b/tools/perf/util/evlist.c index 3d55d9a53b9f4875..f9fdbd85a163ee97 100644 --- a/tools/perf/util/evlist.c +++ b/tools/perf/util/evlist.c @@ -27,6 +27,8 @@ #include "util/perf_api_probe.h" #include "util/evsel_fprintf.h" #include "util/evlist-hybrid.h" +#include "util/util.h" +#include "util/workqueue/workqueue.h" #include #include #include @@ -41,6 +43,7 @@ #include #include +#include #include #include #include @@ -369,6 +372,120 @@ static int evlist__is_enabled(struct evlist *evlist) return false; } +struct evlist_work { + struct work_struct work; + struct evlist *evlist; + int cpu; + evsel__cpu_func func; + void *args; + int ret; +}; + +static void evlist__for_each_evsel_cpu_thread_func(struct work_struct *_work) +{ + struct evlist_work *work = container_of(_work, struct evlist_work, work); + int cpu_idx, ret, err = 0; + struct evsel *pos; + + work->ret = 0; + evlist__for_each_entry(work->evlist, pos) { + cpu_idx = evsel__find_cpu(pos, work->cpu); + if (cpu_idx < 0) + continue; + ret = work->func(work->evlist, pos, cpu_idx, work->args); + if (ret) { + work->ret = ret; + if (err < 0) // error + return; + } + } +} + +static int evlist__for_each_evsel_cpu_multithreaded(struct evlist *evlist, + evsel__cpu_func func, void *args) +{ + int i, cpu, ret; + struct evlist_work *works; + char errbuf[WORKQUEUE_STRERR_BUFSIZE]; + + works = calloc(perf_cpu_map__nr(evlist->core.all_cpus), sizeof(*works)); + perf_cpu_map__for_each_cpu(cpu, i, evlist->core.all_cpus) { + init_work(&works[i].work); + works[i].evlist = evlist; + works[i].work.func = evlist__for_each_evsel_cpu_thread_func; + works[i].cpu = cpu; + works[i].func = func; + works[i].args = args; + works[i].ret = 0; + + ret = schedule_work_on(cpu, &works[i].work); + if (ret) { + workqueue_strerror(global_wq, ret, errbuf, sizeof(errbuf)); + pr_debug("schedule_work: %s\n", errbuf); + break; + } + } + + ret = flush_scheduled_work(); + if (ret) { + workqueue_strerror(global_wq, ret, errbuf, sizeof(errbuf)); + pr_debug("flush_scheduled_work: %s\n", errbuf); + goto out; + } + + perf_cpu_map__for_each_cpu(cpu, i, evlist->core.all_cpus) { + if (works[i].ret) { + ret = works[i].ret; + if (works[i].ret < 0) // error + goto out; + } + } +out: + free(works); + + return ret; +} + +static int evlist__for_each_evsel_cpu_singlethreaded(struct evlist *evlist, + evsel__cpu_func func, void *args) +{ + int ret, err = 0, i, cpu, cpu_idx; + struct affinity affinity; + struct evsel *pos; + + if (affinity__setup(&affinity) < 0) + return -1; + + evlist__for_each_cpu(evlist, i, cpu) { + affinity__set(&affinity, cpu); + + evlist__for_each_entry(evlist, pos) { + cpu_idx = evsel__find_cpu(pos, cpu); + if (cpu_idx < 0) + continue; + ret = func(evlist, pos, cpu_idx, args); + if (ret) { + err = ret; + if (err < 0) // error + goto out; + } + } + } + +out: + affinity__cleanup(&affinity); + return err; +} + +int evlist__for_each_evsel_cpu(struct evlist *evlist, evsel__cpu_func func, void *args) +{ + if (perf_singlethreaded) + return evlist__for_each_evsel_cpu_singlethreaded(evlist, func, args); + else + return evlist__for_each_evsel_cpu_multithreaded(evlist, func, args); + +} + static void __evlist__disable(struct evlist *evlist, char *evsel_name) { struct evsel *pos; diff --git a/tools/perf/util/evlist.h b/tools/perf/util/evlist.h index fde893170c7ba6d2..5f24a45d4e3cf30a 100644 --- a/tools/perf/util/evlist.h +++ b/tools/perf/util/evlist.h @@ -363,4 +363,18 @@ struct evsel *evlist__find_evsel(struct evlist *evlist, int idx); int evlist__scnprintf_evsels(struct evlist *evlist, size_t size, char *bf); void evlist__check_mem_load_aux(struct evlist *evlist); + +/** + * evsel__cpu_func - function to run on each evsel for each cpu + * @evlist: the parent evlist + * @evsel: the processed evsel + * @cpu: index of the cpu in evsel->core.cpus + * @args: additional custom arguments + * + * Returns: + * A negative value is considered as an error. + * A positive value will be propagated to evlist__for_each_evsel_cpu. + */ +typedef int (*evsel__cpu_func)(struct evlist *evlist, struct evsel *evsel, int cpu, void *args); +int evlist__for_each_evsel_cpu(struct evlist *evlist, evsel__cpu_func func, void *args); #endif /* __PERF_EVLIST_H */ -- 2.31.1