Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp157078rwd; Wed, 24 May 2023 15:56:54 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ631nzXoQ2X78QTHeydS1NfAOTv/G5HN4FxMQuWKNTzu6aRbTMVX96fbmCDDArW+HMHCckx X-Received: by 2002:a17:90b:33ce:b0:255:ea24:e0e7 with SMTP id lk14-20020a17090b33ce00b00255ea24e0e7mr2064511pjb.0.1684969014547; Wed, 24 May 2023 15:56:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684969014; cv=none; d=google.com; s=arc-20160816; b=lG0pwkp8GB97M1XQ8OIjm3W1buuxumhBFAXDs2uTpa7/+9ykb0xVjOQNjPArD4IAXj InL3BvkB3TIOLy2/IZGr9XR+jkHv2fBCFmAtMg9ZfohfxaY8tuIfFAi92cAfeSTqeniL uIQGYVttUM5NjlYSygxFugTJ4hYNHCfY3oaTgWalbTDu3Tgh5gdDkqAtFcWfBDoikWL+ TAY/s1kpw3/UcSZEmXck1PbMK1mEYDSm3r37Tzc9PT49HMJepp6ZoXVtiGFRBoCD8Ysa r0rk0hgiy3FWFLOU0lNiSNfyd4CDpy0gBAHMUP1Fq+BlgvVKgYOrVQEg3jcZvT0DKrtO kWUg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=oOYhuvl7j4rILtDYoGdkPuYJrVXEqJknY6wScK522SU=; b=rvhmX7cSOl5kCM/CdlyKaBpGzIH7db6/pXdtbz5fMvS29xlITKKKMFgi+kzpnJe2VJ 6zXkFUCFPJXolU6OHmtoMocIAWFerrpcpUjiRk9QtD35TAipFGUJh7ptJf03lY27zTcI zazhAXVs9gMPVKLGBCKArh20y7vfenzxwC9wzf8G0DFuM/eOo7KAdviZNp1T37xpcO/p qWuyxc2cRC5vC7iL2NwnTX5CVrQO+AskOm6bTMG+o7LSmgDInTZ6qFzMVqbgX/UhkLVc ysrCQEyJQRdHisi7BqLCfBJedLQa+YO7o+xSVGUOEyfXkvscuXiWhXPHxVLgcxmVFqzz pjXQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=67+9ALB3; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id h16-20020a17090adb9000b00250abbb4fa5si2177640pjv.8.2023.05.24.15.56.39; Wed, 24 May 2023 15:56:54 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=67+9ALB3; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236597AbjEXWSp (ORCPT + 99 others); Wed, 24 May 2023 18:18:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45734 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236518AbjEXWSj (ORCPT ); Wed, 24 May 2023 18:18:39 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6E2B113A for ; Wed, 24 May 2023 15:18:38 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-ba81b37d9d2so2863426276.3 for ; Wed, 24 May 2023 15:18:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1684966717; x=1687558717; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=oOYhuvl7j4rILtDYoGdkPuYJrVXEqJknY6wScK522SU=; b=67+9ALB3eZ2ZAz2DQQxqS9HpMTTA2RMnCLODfUbzvIf2IN2exBLSJWH+utQF2kJCC3 8Yeuq5eqt/fW0Vd9MzYgXMa/brXXO+Slc7bNtXryqVvjvwL+Qc1gaGBkowPvoKd65BV3 lCP1CT9xWxjoBgKR2Bqo4rUqoxCk1c7/t26JsaF0pdbhm6SqVGo2b7w9exP17WpU+JTQ 2G2JR47bt4R6ZVVhayAnsWyUb3PgyHFpGFMlg1kwkBxY6w7a56HuLqkO0OrrH5m8ezp3 wHDKWhIwtPsBTCa7h/ETGkqI2utZVjWAeD+/lzVCFTgJeY2ZL1N1pYXxkx0Y51fKmrhG fuew== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684966717; x=1687558717; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=oOYhuvl7j4rILtDYoGdkPuYJrVXEqJknY6wScK522SU=; b=ClbNsVNXhxnNB1WMdFrQNcYY5nCwNwNUrGuzdY+r+FS/RlDNMkAlub/u08LtnVPncM xViLITJO0c0O0J3m5jLg+WiTe2wzdVhvfxlClIEgspa2sJ3Js+0qf6OHhX2MrmX/TA7R jk326C1bPmvzCWaY6cyQAhJEqUQeHEHGr5Gf+fL++oFHVc7/lASdU6uX5FblLdDY9Wom Pqj2lNNclL3cGzQRRBEz75W8tLTXH1Z8mzAWSNYpU+NgfCNylPtd9wNPOI+lTCjjKit8 M5npBl29icrdry5ujZDYXjHD4b7GiN6fobKSAPTM3CCl2x2uhkiBjRjUZM0QrBh6KXeR /KTQ== X-Gm-Message-State: AC+VfDwoDInOKA3BFzdyHKwksZVCE2iLM/C/c7TUIWKhbPfBLP+GZj6v m9J9G+fhj8YILrKkz1+tIaMv3KN6mHSk X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:7aa7:3d2d:76ae:8e96]) (user=irogers job=sendgmr) by 2002:a05:6902:1615:b0:ba7:8099:c5f2 with SMTP id bw21-20020a056902161500b00ba78099c5f2mr588863ybb.8.1684966717718; Wed, 24 May 2023 15:18:37 -0700 (PDT) Date: Wed, 24 May 2023 15:17:57 -0700 In-Reply-To: <20230524221831.1741381-1-irogers@google.com> Message-Id: <20230524221831.1741381-2-irogers@google.com> Mime-Version: 1.0 References: <20230524221831.1741381-1-irogers@google.com> X-Mailer: git-send-email 2.40.1.698.g37aff9b760-goog Subject: [PATCH v3 01/35] perf cpumap: Add intersect function From: Ian Rogers To: Suzuki K Poulose , Mike Leach , Leo Yan , John Garry , Will Deacon , James Clark , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Kajol Jain , Jing Zhang , Kan Liang , Zhengjun Xing , Ravi Bangoria , Madhavan Srinivasan , Athira Rajeev , Ming Wang , Huacai Chen , Sandipan Das , Dmitrii Dolgov <9erthalion6@gmail.com>, Sean Christopherson , Ali Saidi , Rob Herring , Thomas Richter , Kang Minchul , linux-kernel@vger.kernel.org, coresight@lists.linaro.org, linux-arm-kernel@lists.infradead.org, linux-perf-users@vger.kernel.org Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The merge function gives the union of two cpu maps. Add an intersect function which is necessary, for example, when intersecting a PMUs supported CPUs with user requested. Signed-off-by: Ian Rogers --- tools/lib/perf/cpumap.c | 35 ++++++++++++++++++++++++ tools/lib/perf/include/perf/cpumap.h | 2 ++ tools/perf/tests/builtin-test.c | 1 + tools/perf/tests/cpumap.c | 41 ++++++++++++++++++++++++++++ tools/perf/tests/tests.h | 1 + 5 files changed, 80 insertions(+) diff --git a/tools/lib/perf/cpumap.c b/tools/lib/perf/cpumap.c index 1229b18bcdb1..d4f3a1a12522 100644 --- a/tools/lib/perf/cpumap.c +++ b/tools/lib/perf/cpumap.c @@ -402,3 +402,38 @@ 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 perf_cpu_map__get(orig); + if (perf_cpu_map__is_subset(orig, other)) + return perf_cpu_map__get(other); + + 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); + return merged; +} diff --git a/tools/lib/perf/include/perf/cpumap.h b/tools/lib/perf/include/perf/cpumap.h index 3f43f770cdac..0466c4216fbb 100644 --- a/tools/lib/perf/include/perf/cpumap.h +++ b/tools/lib/perf/include/perf/cpumap.h @@ -20,6 +20,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); diff --git a/tools/perf/tests/builtin-test.c b/tools/perf/tests/builtin-test.c index b89d69afcef0..eef400025fca 100644 --- a/tools/perf/tests/builtin-test.c +++ b/tools/perf/tests/builtin-test.c @@ -97,6 +97,7 @@ static struct test_suite *generic_tests[] = { &suite__backward_ring_buffer, &suite__cpu_map_print, &suite__cpu_map_merge, + &suite__cpu_map_intersect, &suite__sdt_event, &suite__is_printable_array, &suite__bitmap_print, diff --git a/tools/perf/tests/cpumap.c b/tools/perf/tests/cpumap.c index b1a924314e09..92232978fe5e 100644 --- a/tools/perf/tests/cpumap.c +++ b/tools/perf/tests/cpumap.c @@ -171,6 +171,47 @@ static int test__cpu_map_merge(struct test_suite *test __maybe_unused, int subte return 0; } +static int __test__cpu_map_intersect(const char *lhs, const char *rhs, int nr, const char *expected) +{ + struct perf_cpu_map *a = perf_cpu_map__new(lhs); + struct perf_cpu_map *b = perf_cpu_map__new(rhs); + struct perf_cpu_map *c = perf_cpu_map__intersect(a, b); + char buf[100]; + + TEST_ASSERT_EQUAL("failed to intersect map: bad nr", perf_cpu_map__nr(c), nr); + cpu_map__snprint(c, buf, sizeof(buf)); + TEST_ASSERT_VAL("failed to intersect map: bad result", !strcmp(buf, expected)); + perf_cpu_map__put(a); + perf_cpu_map__put(b); + perf_cpu_map__put(c); + return 0; +} + +static int test__cpu_map_intersect(struct test_suite *test __maybe_unused, + int subtest __maybe_unused) +{ + int ret; + + ret = __test__cpu_map_intersect("4,2,1", "4,5,7", 1, "4"); + if (ret) + return ret; + ret = __test__cpu_map_intersect("1-8", "6-9", 3, "6-8"); + if (ret) + return ret; + ret = __test__cpu_map_intersect("1-8,12-20", "6-9,15", 4, "6-8,15"); + if (ret) + return ret; + ret = __test__cpu_map_intersect("4,2,1", "1", 1, "1"); + if (ret) + return ret; + ret = __test__cpu_map_intersect("1", "4,2,1", 1, "1"); + if (ret) + return ret; + ret = __test__cpu_map_intersect("1", "1", 1, "1"); + return ret; +} + DEFINE_SUITE("Synthesize cpu map", cpu_map_synthesize); DEFINE_SUITE("Print cpu map", cpu_map_print); DEFINE_SUITE("Merge cpu map", cpu_map_merge); +DEFINE_SUITE("Intersect cpu map", cpu_map_intersect); diff --git a/tools/perf/tests/tests.h b/tools/perf/tests/tests.h index 9a0f3904e53d..b4e54f08bc39 100644 --- a/tools/perf/tests/tests.h +++ b/tools/perf/tests/tests.h @@ -127,6 +127,7 @@ DECLARE_SUITE(event_times); DECLARE_SUITE(backward_ring_buffer); DECLARE_SUITE(cpu_map_print); DECLARE_SUITE(cpu_map_merge); +DECLARE_SUITE(cpu_map_intersect); DECLARE_SUITE(sdt_event); DECLARE_SUITE(is_printable_array); DECLARE_SUITE(bitmap_print); -- 2.40.1.698.g37aff9b760-goog