From nobody Sun Dec 28 17:29:12 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 E4902C4167B for ; Thu, 7 Dec 2023 01:20:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1443056AbjLGBUu (ORCPT ); Wed, 6 Dec 2023 20:20:50 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60642 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1443054AbjLGBUC (ORCPT ); Wed, 6 Dec 2023 20:20:02 -0500 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 60D2C30D0 for ; Wed, 6 Dec 2023 17:19:10 -0800 (PST) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-5d064f9e2a1so1917687b3.1 for ; Wed, 06 Dec 2023 17:19:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1701911949; x=1702516749; 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=LfZhXz3aJqIR3B942uDWzE+/1mlKepIUmUSXOJMumf4=; b=JCi8naAa/FIYwAjXY4IwCJVGLyiAohN1z3QpUtrfdX+ujw/8ya3r94BJuEsn5a6MwZ LFyrvLM5BPPundoEly2CSqTgjwTSDVLtCQH24uEKq2YqPmeW7XuwSbwtpOIJhFA+mMji qA2Hvoz4ZGrd8QEMqdDOMwhNz2L1bKG8F+zw+9p+poeyZr/E7oGpN+v1SQdwJMw6g5XI QR3hFwqsbydVeHRRsh3u0qyj/+rPLSty2/1gLBC485SwxZrve8iNda4xie2M8zxKp7EM wDT1A3hfcmHOh1vsU6lBbi/JPnnUO9qKuK7AZOJJ3O/akpNtdgra0IKE6XQqiniwORUm Vrcg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701911949; x=1702516749; 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=LfZhXz3aJqIR3B942uDWzE+/1mlKepIUmUSXOJMumf4=; b=J6KAAf5CCXnvmDSbGeV0+pZzoKUzLCKTHVbpiWkpXP7S9K9lBpvqNtDQkLb2B1kRIs 9kCRfDFh4uQ+gk+dTyk6m7o471NytFd3e8tJiEGWENj54uWxZAur+66gXf1ocsK1gMI7 estmWQyYn6SReX5VldgLCrcqQOyaJnTVlxEjb2EOmP4AVnvo35uMeay5BfcNXrr3YRoJ ZAK8x9xkW/X/wI2tE8KIQJN4H6tjV1qfgLDUJB7HjO+3W5HnOOh+GrC1Gd+VCDlB3wsb Jr3jThONqW7JUSFzXgizupaNJmXKjoA0j0Zipj6xnQNz/ZTTdKhS7dWlKd4l/IIVurgn 9iYQ== X-Gm-Message-State: AOJu0Yz8Xhi+tVpahQf1op5RArzyPz82vpJo8RhpWys1TWfAZrn8DYw8 diWOnUQDLrgqI94H2rq7+l/i1jqckl7E X-Google-Smtp-Source: AGHT+IEFGIHjxtwtssSHBNP1N9AtvROXQnmmbb4cujCd65lj2pKmwKkxBtnatTH/s/39At1DrwlShvskYDh9 X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:35bf:293e:7696:34e9]) (user=irogers job=sendgmr) by 2002:a81:be16:0:b0:5c9:c698:cd34 with SMTP id i22-20020a81be16000000b005c9c698cd34mr31433ywn.2.1701911949131; Wed, 06 Dec 2023 17:19:09 -0800 (PST) Date: Wed, 6 Dec 2023 17:17:18 -0800 In-Reply-To: <20231207011722.1220634-1-irogers@google.com> Message-Id: <20231207011722.1220634-45-irogers@google.com> Mime-Version: 1.0 References: <20231207011722.1220634-1-irogers@google.com> X-Mailer: git-send-email 2.43.0.rc2.451.g8631bc7472-goog Subject: [PATCH v6 44/47] 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.rc2.451.g8631bc7472-goog