From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 92AEFC433EF for ; Mon, 28 Mar 2022 21:25:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229660AbiC1V1a (ORCPT ); Mon, 28 Mar 2022 17:27:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54658 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229546AbiC1V11 (ORCPT ); Mon, 28 Mar 2022 17:27:27 -0400 Received: from mail-wm1-x32f.google.com (mail-wm1-x32f.google.com [IPv6:2a00:1450:4864:20::32f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5F492137F76 for ; Mon, 28 Mar 2022 14:25:36 -0700 (PDT) Received: by mail-wm1-x32f.google.com with SMTP id h16so9162403wmd.0 for ; Mon, 28 Mar 2022 14:25:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=1G+GATea/gEtUw/pWJqQ7Ixi4GCktSB1NgpSqf6SUpc=; b=ff3pbwSNU4OAWweoWrN7aL23MQscP4KSYMsHZHsOC017RgC7WbeViHy1tfJAFmN/lQ qFp8xCMw3u+dl/WdgmtmFbZqfFMl58je1HZUNky8io5xTnbaPRkXQ0p9+/bqFsnrV13e 87CnLMXfFAyqOPugLMqgnQXA6f0hv+SOIMNb34gnrh6Uz6AI78Fkp4AuyeHzB0d6uLcG 24glXp8ngdRF5tzOTnN1VkSFOoL4g8eAO23bcA6Az6CbvyuOLOjBzyYj9FiQnp3FzjIy Oa0ziPsOihA8dfCKzeX/IHsSZgFu39XCtQjS47BWtppn9IvP/uTn5PoEVWvC2O2FdHiy 1q2A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=1G+GATea/gEtUw/pWJqQ7Ixi4GCktSB1NgpSqf6SUpc=; b=eAthPBbflFxEfo6PtuVsqMWuPstaYcIhoRnuHjwTt9dhbhKa4WXoxNcwCKK+z3iO+h Mjkh2hDfwLmwxARX8+aeFzHyn1uPX6ZyQVuuLGvRTjiW5WpTMXlqbDX2+PZfxUpOFpu1 5OEcbF9jBrqpYEyYtW/oL07xuep57ZTeFnOwz8YsHyveaETguDbl+VsyesJiKRatX554 0gDcFALk1RIq7lnvDiBNVVpb96dx9KGi8hdbToDet/T1fAlJKjzpMy19CmrZ/5RmLDmB axR1wYako2h23d2TA3mUeJQkZ7Ld8tiCXo50r4KZC9L742NbMjAkPw7HxIENDyfptgBt ogTA== X-Gm-Message-State: AOAM532clDbQb5KxlSvDq7ZdXzlwGjHfIYwKXEt1lC439RokYCLeyb27 KwtMe8jX/v6wN3M9+sceeqaMd7fYn20mihZMFATR3Q== X-Google-Smtp-Source: ABdhPJxqDLS9Je1WTyYK6Q9CxOUbLtpYQHy7lZgCBXI5DykbKcNLXZhVSdUAPUvSuW//+FNsHKaW+O1uZ4s6yxxHfHY= X-Received: by 2002:a05:600c:2905:b0:381:67e2:3992 with SMTP id i5-20020a05600c290500b0038167e23992mr1686003wmd.182.1648502734235; Mon, 28 Mar 2022 14:25:34 -0700 (PDT) MIME-Version: 1.0 References: <20220328062414.1893550-1-irogers@google.com> <20220328062414.1893550-4-irogers@google.com> In-Reply-To: From: Ian Rogers Date: Mon, 28 Mar 2022 14:25:21 -0700 Message-ID: Subject: Re: [PATCH 3/5] perf cpumap: Add intersect function. To: Arnaldo Carvalho de Melo Cc: Arnaldo Carvalho de Melo , Peter Zijlstra , Ingo Molnar , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Mathieu Poirier , Suzuki K Poulose , Mike Leach , Leo Yan , John Garry , Will Deacon , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Kajol Jain , James Clark , German Gomez , Adrian Hunter , Riccardo Mancini , Andi Kleen , Alexey Bayduraev , Alexander Antonov , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, coresight@lists.linaro.org, linux-arm-kernel@lists.infradead.org, netdev@vger.kernel.org, bpf@vger.kernel.org, Stephane Eranian Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Mar 28, 2022 at 2:00 PM Arnaldo Carvalho de Melo wrote: > > > > On March 28, 2022 5:54:06 PM GMT-03:00, Ian Rogers wrote: > >On Mon, Mar 28, 2022 at 1:28 PM Arnaldo Carvalho de Melo > > wrote: > >> > >> Em Sun, Mar 27, 2022 at 11:24:12PM -0700, Ian Rogers escreveu: > >> > The merge function gives the union of two cpu maps. Add an intersect > >> > function which will be used in the next change. > >> > > >> > Signed-off-by: Ian Rogers > >> > --- > >> > tools/lib/perf/cpumap.c | 38 ++++++++++++++++++++++++++++ > >> > tools/lib/perf/include/perf/cpumap.h | 2 ++ > >> > 2 files changed, 40 insertions(+) > >> > > >> > diff --git a/tools/lib/perf/cpumap.c b/tools/lib/perf/cpumap.c > >> > index 953bc50b0e41..56b4d213039f 100644 > >> > --- a/tools/lib/perf/cpumap.c > >> > +++ b/tools/lib/perf/cpumap.c > >> > @@ -393,3 +393,41 @@ struct perf_cpu_map *perf_cpu_map__merge(struct perf_cpu_map *orig, > >> > perf_cpu_map__put(orig); > >> > return merged; > >> > } > >> > + > >> > +struct perf_cpu_map *perf_cpu_map__intersect(struct perf_cpu_map *orig, > >> > + struct perf_cpu_map *other) > >> > +{ > >> > + struct perf_cpu *tmp_cpus; > >> > + int tmp_len; > >> > + int i, j, k; > >> > + struct perf_cpu_map *merged = NULL; > >> > + > >> > + if (perf_cpu_map__is_subset(other, orig)) > >> > + return orig; > >> > + if (perf_cpu_map__is_subset(orig, other)) { > >> > + perf_cpu_map__put(orig); > >> > >> Why this put(orig)? > > > >As with merge, if orig isn't returned then it is put. > > For merge I can see it dropping a reference, i.e. get b and merge it into a, after that b was "consumed" > > But for intersect? The current use case is the intersect of all online CPUs with the merge of all CPU maps from evsels. So we can generally just reuse all_cpus, or the common case of both maps contain every CPU. I think the pattern makes code like: evlist->cpus = perf_cpu_map__intersect(evlist->cpus, other); not quite as messy, as without the put you need: tmp = perf_cpu_map__intersect(evlist->cpus, other); perf_cpu_map__put(evlist->cpus); evlist->cpus = tmp; I'm somewhat agnostic on what the API should be, but it'd be nice if merge and intersect behaved in a similar way. Thanks, Ian > > > >> > + return perf_cpu_map__get(other); > >> > >> And why the get here and not on the first if? > > > >The first argument orig is either put or returned while the second may > >be returned only if the reference count is incremented. We could > >change the API for merge and intersect to put both arguments, or to > >not put either argument. > > > >Thanks, > >Ian > > > >> > + } > >> > + > >> > + tmp_len = max(orig->nr, other->nr); > >> > + tmp_cpus = malloc(tmp_len * sizeof(struct perf_cpu)); > >> > + if (!tmp_cpus) > >> > + return NULL; > >> > + > >> > + i = j = k = 0; > >> > + while (i < orig->nr && j < other->nr) { > >> > + if (orig->map[i].cpu < other->map[j].cpu) > >> > + i++; > >> > + else if (orig->map[i].cpu > other->map[j].cpu) > >> > + j++; > >> > + else { > >> > + j++; > >> > + tmp_cpus[k++] = orig->map[i++]; > >> > + } > >> > + } > >> > + if (k) > >> > + merged = cpu_map__trim_new(k, tmp_cpus); > >> > + free(tmp_cpus); > >> > + perf_cpu_map__put(orig); > >> > + return merged; > >> > +} > >> > diff --git a/tools/lib/perf/include/perf/cpumap.h b/tools/lib/perf/include/perf/cpumap.h > >> > index 4a2edbdb5e2b..a2a7216c0b78 100644 > >> > --- a/tools/lib/perf/include/perf/cpumap.h > >> > +++ b/tools/lib/perf/include/perf/cpumap.h > >> > @@ -19,6 +19,8 @@ LIBPERF_API struct perf_cpu_map *perf_cpu_map__read(FILE *file); > >> > LIBPERF_API struct perf_cpu_map *perf_cpu_map__get(struct perf_cpu_map *map); > >> > LIBPERF_API struct perf_cpu_map *perf_cpu_map__merge(struct perf_cpu_map *orig, > >> > struct perf_cpu_map *other); > >> > +LIBPERF_API struct perf_cpu_map *perf_cpu_map__intersect(struct perf_cpu_map *orig, > >> > + struct perf_cpu_map *other); > >> > LIBPERF_API void perf_cpu_map__put(struct perf_cpu_map *map); > >> > LIBPERF_API struct perf_cpu perf_cpu_map__cpu(const struct perf_cpu_map *cpus, int idx); > >> > LIBPERF_API int perf_cpu_map__nr(const struct perf_cpu_map *cpus); > >> > -- > >> > 2.35.1.1021.g381101b075-goog > >> > >> -- > >> > >> - Arnaldo From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id B2FE1C433F5 for ; Mon, 28 Mar 2022 21:27:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:Subject:Message-ID:Date:From: In-Reply-To:References:MIME-Version:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=D8wxS25YinCF+ei+ysxpn1y2zTDljOe8g1cVWYQvdio=; b=UErX9sA8PUODPD QPkXtKBdQwM5uctQFR796f6C0lKLqvOMd/cRqTgTUQ5dKrBxF87/cmFD4j2RzuFfzq7L9dsSGxUKj da1K/6L5WKWjLE/PTUzC2wASo4bcFN3ZkRMiWY1LHqm7q5DTeUu2DeRf8eCPtm6JecBfBThmKgAC2 6a6ExrDymkRw6621zNctC6dq4V2EayzV8ugqVLmfN1HabPZgpO/gAXtAwdoWNShC79TH7scMppI+Y lYVJLYgkO6aPUF0it07XsstBfPhExr35gzfw8uvkrcKOlgulrQJKO1kOP0aIVEFDEpU4wBIRuc44X LHi9s1R1aGOMNJZinPig==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1nYwrr-00AFqH-O7; Mon, 28 Mar 2022 21:25:39 +0000 Received: from mail-wm1-x332.google.com ([2a00:1450:4864:20::332]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1nYwro-00AFpf-F9 for linux-arm-kernel@lists.infradead.org; Mon, 28 Mar 2022 21:25:38 +0000 Received: by mail-wm1-x332.google.com with SMTP id p189so9159152wmp.3 for ; Mon, 28 Mar 2022 14:25:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=1G+GATea/gEtUw/pWJqQ7Ixi4GCktSB1NgpSqf6SUpc=; b=ff3pbwSNU4OAWweoWrN7aL23MQscP4KSYMsHZHsOC017RgC7WbeViHy1tfJAFmN/lQ qFp8xCMw3u+dl/WdgmtmFbZqfFMl58je1HZUNky8io5xTnbaPRkXQ0p9+/bqFsnrV13e 87CnLMXfFAyqOPugLMqgnQXA6f0hv+SOIMNb34gnrh6Uz6AI78Fkp4AuyeHzB0d6uLcG 24glXp8ngdRF5tzOTnN1VkSFOoL4g8eAO23bcA6Az6CbvyuOLOjBzyYj9FiQnp3FzjIy Oa0ziPsOihA8dfCKzeX/IHsSZgFu39XCtQjS47BWtppn9IvP/uTn5PoEVWvC2O2FdHiy 1q2A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=1G+GATea/gEtUw/pWJqQ7Ixi4GCktSB1NgpSqf6SUpc=; b=2xxSbEKYEKNlMfpWEZoJQZMmBVqMXrgTJDU3u4QU2g5ILavf9wM3/oI3Cd02urp7MH tw/pPCpP1steqFpI7YQ2ZLh9uYNXI17HSj+HY48Ztvjy57jTJYItuGXUkp26UKoBELqs YXI9KGJBsdmEIomb5mN9UKSHBQKeaX/oeviCgXdAJGwvEW+y2gGwjHNbmukqTaJRoJzq EC4VvxXG+vRjy6OGBHXZhRPS6QcidxLYEzOUI9/jCo/ikrftc8HPqxgOLDuFglCyTMG2 dxJewAt1oHjv9Z43cmCCZzemtnFxFb2zC0RY2z5js4hMNwuiWb8v+MOeZ2lRhznqgYjc ylQQ== X-Gm-Message-State: AOAM5321TeiUhZd89ckMcRCLhCwERlroP4H0DzwmndkY0Gs2aXplB/HY wb03vgvRzbNXRoo04OEHPY/BtSm0GrLoOnT/9sizIA== X-Google-Smtp-Source: ABdhPJxqDLS9Je1WTyYK6Q9CxOUbLtpYQHy7lZgCBXI5DykbKcNLXZhVSdUAPUvSuW//+FNsHKaW+O1uZ4s6yxxHfHY= X-Received: by 2002:a05:600c:2905:b0:381:67e2:3992 with SMTP id i5-20020a05600c290500b0038167e23992mr1686003wmd.182.1648502734235; Mon, 28 Mar 2022 14:25:34 -0700 (PDT) MIME-Version: 1.0 References: <20220328062414.1893550-1-irogers@google.com> <20220328062414.1893550-4-irogers@google.com> In-Reply-To: From: Ian Rogers Date: Mon, 28 Mar 2022 14:25:21 -0700 Message-ID: Subject: Re: [PATCH 3/5] perf cpumap: Add intersect function. To: Arnaldo Carvalho de Melo Cc: Arnaldo Carvalho de Melo , Peter Zijlstra , Ingo Molnar , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Mathieu Poirier , Suzuki K Poulose , Mike Leach , Leo Yan , John Garry , Will Deacon , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Kajol Jain , James Clark , German Gomez , Adrian Hunter , Riccardo Mancini , Andi Kleen , Alexey Bayduraev , Alexander Antonov , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, coresight@lists.linaro.org, linux-arm-kernel@lists.infradead.org, netdev@vger.kernel.org, bpf@vger.kernel.org, Stephane Eranian X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220328_142536_539777_7C3A99AA X-CRM114-Status: GOOD ( 33.03 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org On Mon, Mar 28, 2022 at 2:00 PM Arnaldo Carvalho de Melo wrote: > > > > On March 28, 2022 5:54:06 PM GMT-03:00, Ian Rogers wrote: > >On Mon, Mar 28, 2022 at 1:28 PM Arnaldo Carvalho de Melo > > wrote: > >> > >> Em Sun, Mar 27, 2022 at 11:24:12PM -0700, Ian Rogers escreveu: > >> > The merge function gives the union of two cpu maps. Add an intersect > >> > function which will be used in the next change. > >> > > >> > Signed-off-by: Ian Rogers > >> > --- > >> > tools/lib/perf/cpumap.c | 38 ++++++++++++++++++++++++++++ > >> > tools/lib/perf/include/perf/cpumap.h | 2 ++ > >> > 2 files changed, 40 insertions(+) > >> > > >> > diff --git a/tools/lib/perf/cpumap.c b/tools/lib/perf/cpumap.c > >> > index 953bc50b0e41..56b4d213039f 100644 > >> > --- a/tools/lib/perf/cpumap.c > >> > +++ b/tools/lib/perf/cpumap.c > >> > @@ -393,3 +393,41 @@ struct perf_cpu_map *perf_cpu_map__merge(struct perf_cpu_map *orig, > >> > perf_cpu_map__put(orig); > >> > return merged; > >> > } > >> > + > >> > +struct perf_cpu_map *perf_cpu_map__intersect(struct perf_cpu_map *orig, > >> > + struct perf_cpu_map *other) > >> > +{ > >> > + struct perf_cpu *tmp_cpus; > >> > + int tmp_len; > >> > + int i, j, k; > >> > + struct perf_cpu_map *merged = NULL; > >> > + > >> > + if (perf_cpu_map__is_subset(other, orig)) > >> > + return orig; > >> > + if (perf_cpu_map__is_subset(orig, other)) { > >> > + perf_cpu_map__put(orig); > >> > >> Why this put(orig)? > > > >As with merge, if orig isn't returned then it is put. > > For merge I can see it dropping a reference, i.e. get b and merge it into a, after that b was "consumed" > > But for intersect? The current use case is the intersect of all online CPUs with the merge of all CPU maps from evsels. So we can generally just reuse all_cpus, or the common case of both maps contain every CPU. I think the pattern makes code like: evlist->cpus = perf_cpu_map__intersect(evlist->cpus, other); not quite as messy, as without the put you need: tmp = perf_cpu_map__intersect(evlist->cpus, other); perf_cpu_map__put(evlist->cpus); evlist->cpus = tmp; I'm somewhat agnostic on what the API should be, but it'd be nice if merge and intersect behaved in a similar way. Thanks, Ian > > > >> > + return perf_cpu_map__get(other); > >> > >> And why the get here and not on the first if? > > > >The first argument orig is either put or returned while the second may > >be returned only if the reference count is incremented. We could > >change the API for merge and intersect to put both arguments, or to > >not put either argument. > > > >Thanks, > >Ian > > > >> > + } > >> > + > >> > + tmp_len = max(orig->nr, other->nr); > >> > + tmp_cpus = malloc(tmp_len * sizeof(struct perf_cpu)); > >> > + if (!tmp_cpus) > >> > + return NULL; > >> > + > >> > + i = j = k = 0; > >> > + while (i < orig->nr && j < other->nr) { > >> > + if (orig->map[i].cpu < other->map[j].cpu) > >> > + i++; > >> > + else if (orig->map[i].cpu > other->map[j].cpu) > >> > + j++; > >> > + else { > >> > + j++; > >> > + tmp_cpus[k++] = orig->map[i++]; > >> > + } > >> > + } > >> > + if (k) > >> > + merged = cpu_map__trim_new(k, tmp_cpus); > >> > + free(tmp_cpus); > >> > + perf_cpu_map__put(orig); > >> > + return merged; > >> > +} > >> > diff --git a/tools/lib/perf/include/perf/cpumap.h b/tools/lib/perf/include/perf/cpumap.h > >> > index 4a2edbdb5e2b..a2a7216c0b78 100644 > >> > --- a/tools/lib/perf/include/perf/cpumap.h > >> > +++ b/tools/lib/perf/include/perf/cpumap.h > >> > @@ -19,6 +19,8 @@ LIBPERF_API struct perf_cpu_map *perf_cpu_map__read(FILE *file); > >> > LIBPERF_API struct perf_cpu_map *perf_cpu_map__get(struct perf_cpu_map *map); > >> > LIBPERF_API struct perf_cpu_map *perf_cpu_map__merge(struct perf_cpu_map *orig, > >> > struct perf_cpu_map *other); > >> > +LIBPERF_API struct perf_cpu_map *perf_cpu_map__intersect(struct perf_cpu_map *orig, > >> > + struct perf_cpu_map *other); > >> > LIBPERF_API void perf_cpu_map__put(struct perf_cpu_map *map); > >> > LIBPERF_API struct perf_cpu perf_cpu_map__cpu(const struct perf_cpu_map *cpus, int idx); > >> > LIBPERF_API int perf_cpu_map__nr(const struct perf_cpu_map *cpus); > >> > -- > >> > 2.35.1.1021.g381101b075-goog > >> > >> -- > >> > >> - Arnaldo _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel