From nobody Wed Dec 17 12:44:25 2025 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 4938AC4167B for ; Mon, 27 Nov 2023 22:15:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233929AbjK0WPK (ORCPT ); Mon, 27 Nov 2023 17:15:10 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43228 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233739AbjK0WOM (ORCPT ); Mon, 27 Nov 2023 17:14:12 -0500 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C7BA63873 for ; Mon, 27 Nov 2023 14:11:09 -0800 (PST) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-db403a0f25eso5806003276.1 for ; Mon, 27 Nov 2023 14:11:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1701123068; x=1701727868; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=DQ1JTb4irNPUQ3W+I5RN9UtOSGB74icrgqG3j5gtDIA=; b=IiymtfqsCTWA344n2eS1Nk4416DMhcZbHTG3e7LwsX0W3reNsghB/jnBMGn0GaqDAy iNkyWONjzTLaVqowf/9oNRhWHyBrmLyqJRTK2QPhIOlhm2ZRJKtzERQwZvrMdgARFuv9 64m4tYgidZr7WXJZuY8yyiabMwS2ya4me88sh449nRahBeoobP+fVzCveHDdcvMXn79r N7JaSmWGsMeIc755/nmjLPIhLUAmEg6aXRTpF57YMAvRQu9lTyufgDuNvdyiLmegjcqZ ZBSUid312LBZxkIWAy/k3kPy/5sodhtrgl28Ywalz5fpNBDWwj6Z6E/fisbaISQhxgtV 8zDg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701123068; x=1701727868; 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=DQ1JTb4irNPUQ3W+I5RN9UtOSGB74icrgqG3j5gtDIA=; b=DOGHnCbiOcGUghodBzVnX5sslQJ5JPKTo/nOGUoVFiCe00ow3afO+EbrfvFy2JdkAS xvcqVCfLy7IqHxklsp4Pvze8e/MvCxVwk3dIvtTIAp+FB2uCVBOWmQc+6mwjnJAudfx6 Hl09L5h4JzzEGyplP3/YP4eyagq+K0+bXKT/z/Ez1NtH3BIUoCH4d7GvGTfGYPCNn10s vMypcFj/lsi91djshNDBR8t2e11fgnDlkyD174eSYLeGLcOJbabr9MVNLdGURL5G2gYd 1QT7LGXQVU3FF6/98QNALUGEyZUToT4Zb6yueoqZZNenxEVlRqVuWHsFb4sLQzLmM1M9 NFgA== X-Gm-Message-State: AOJu0YzHZ4lsElBpX+8+rqpcbMzl9SND/7oqmqVnSSQz8OEOwysmcIfK ORbZJapogzGT00tDL33wIWPBRIonN0Mx X-Google-Smtp-Source: AGHT+IE+8YySi+0nTPwTSjYrIN1qlX7gzzkH2PtTDJjMfuM0U2AzSv7o0h++plW+VNKV2KUZw1OdyeeuYM5n X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:829:6e77:9093:f39b]) (user=irogers job=sendgmr) by 2002:a25:ca04:0:b0:da3:a91c:7356 with SMTP id a4-20020a25ca04000000b00da3a91c7356mr342652ybg.8.1701123068648; Mon, 27 Nov 2023 14:11:08 -0800 (PST) Date: Mon, 27 Nov 2023 14:08:58 -0800 In-Reply-To: <20231127220902.1315692-1-irogers@google.com> Message-Id: <20231127220902.1315692-47-irogers@google.com> Mime-Version: 1.0 References: <20231127220902.1315692-1-irogers@google.com> X-Mailer: git-send-email 2.43.0.rc1.413.gea7ed67945-goog Subject: [PATCH v5 46/50] perf dsos: Switch hand code to bsearch From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Nick Terrell , Kan Liang , Andi Kleen , Kajol Jain , Athira Rajeev , Huacai Chen , Masami Hiramatsu , Vincent Whitchurch , "Steinar H. Gunderson" , Liam Howlett , Miguel Ojeda , Colin Ian King , Dmitrii Dolgov <9erthalion6@gmail.com>, Yang Jihong , Ming Wang , James Clark , K Prateek Nayak , Sean Christopherson , Leo Yan , Ravi Bangoria , German Gomez , Changbin Du , Paolo Bonzini , Li Dong , Sandipan Das , liuwenyu , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, Guilherme Amadio Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Switch to using the bsearch library function rather than having a hand written binary search. Const-ify some static functions to avoid compiler warnings. Signed-off-by: Ian Rogers --- tools/perf/util/dsos.c | 46 +++++++++++++++++++++++++----------------- 1 file changed, 27 insertions(+), 19 deletions(-) diff --git a/tools/perf/util/dsos.c b/tools/perf/util/dsos.c index e4110438841b..23c3fe4f2abb 100644 --- a/tools/perf/util/dsos.c +++ b/tools/perf/util/dsos.c @@ -107,13 +107,15 @@ bool dsos__read_build_ids(struct dsos *dsos, bool wit= h_hits) return args.have_build_id; } =20 -static int __dso__cmp_long_name(const char *long_name, struct dso_id *id, = struct dso *b) +static int __dso__cmp_long_name(const char *long_name, const struct dso_id= *id, + const struct dso *b) { int rc =3D strcmp(long_name, b->long_name); return rc ?: dso_id__cmp(id, &b->id); } =20 -static int __dso__cmp_short_name(const char *short_name, struct dso_id *id= , struct dso *b) +static int __dso__cmp_short_name(const char *short_name, const struct dso_= id *id, + const struct dso *b) { int rc =3D strcmp(short_name, b->short_name); return rc ?: dso_id__cmp(id, &b->id); @@ -133,6 +135,19 @@ static int dsos__cmp_long_name_id_short_name(const voi= d *va, const void *vb) return rc; } =20 +struct dsos__key { + const char *long_name; + const struct dso_id *id; +}; + +static int dsos__cmp_key_long_name_id(const void *vkey, const void *vdso) +{ + const struct dsos__key *key =3D vkey; + const struct dso *dso =3D *((const struct dso **)vdso); + + return __dso__cmp_long_name(key->long_name, key->id, dso); +} + /* * Find a matching entry and/or link current entry to RB tree. * Either one of the dso or name parameter must be non-NULL or the @@ -143,7 +158,11 @@ static struct dso *__dsos__find_by_longname_id(struct = dsos *dsos, struct dso_id *id, bool write_locked) { - int low =3D 0, high =3D dsos->cnt - 1; + struct dsos__key key =3D { + .long_name =3D name, + .id =3D id, + }; + struct dso **res; =20 if (!dsos->sorted) { if (!write_locked) { @@ -162,23 +181,12 @@ static struct dso *__dsos__find_by_longname_id(struct= dsos *dsos, dsos->sorted =3D true; } =20 - /* - * Find node with the matching name - */ - while (low <=3D high) { - int mid =3D (low + high) / 2; - struct dso *this =3D dsos->dsos[mid]; - int rc =3D __dso__cmp_long_name(name, id, this); + res =3D bsearch(&key, dsos->dsos, dsos->cnt, sizeof(struct dso *), + dsos__cmp_key_long_name_id); + if (!res) + return NULL; =20 - if (rc =3D=3D 0) { - return dso__get(this); /* Find matching dso */ - } - if (rc < 0) - high =3D mid - 1; - else - low =3D mid + 1; - } - return NULL; + return dso__get(*res); } =20 int __dsos__add(struct dsos *dsos, struct dso *dso) --=20 2.43.0.rc1.413.gea7ed67945-goog