From nobody Fri Dec 26 21:32:46 2025 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 139A31A73C for ; Wed, 3 Jan 2024 05:07:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="MeqAWR5u" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-dbe02d0c945so3692134276.3 for ; Tue, 02 Jan 2024 21:07:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1704258457; x=1704863257; 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=J8oNQkD+IF9X70W7WTonENWn/fXyAV8WC3tSRa9nOmo=; b=MeqAWR5u6c7zqt5ovu/TnEXAHg8fp7sLpIX8aZmPBrsFWcx3VO0sfctwN+4Wj1fe5v cFY209KipvXksWxzGn/+fZGsQuPv0GhuwBL7aMUJjKIl9tGfdjnLFW5fkFVEHtdEWxsr DLM3KwWMSTAGeJtvWl6hzggtfITE2ncpAubcQSTnk6NeP9qccF0Ye7UZHp8qv3eKtn5G +vDpRRxs1ui9TKkqcCrdj0wDzq3Ns/ylb7yVXla/QSkZxzwGgQ5kY+z9Lewl8WKevSDt NmFjS8clM5LTRCj47SuWpsajKYjjt6tD1UKUdOhiGskPt3LslkxA4GusM5tAFdWlkvFt GGNw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704258457; x=1704863257; 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=J8oNQkD+IF9X70W7WTonENWn/fXyAV8WC3tSRa9nOmo=; b=iwBUH+bNsHyb7DRCpRzg5uIvoCl1erwOZmGAsRlZgwBCFXJEVcbcxciqaqdnIto9Q2 3ta/kW56EEVgqkDeyCwtqURx1VuqvF5Qnatf/glPUkGFd+AywhGY5wd/imEkQAB3UwOC mVk4nj4+7YNjsVV+UWhltf1vu1b0I6GmypNXYvx+OwrLAmtJFt0iYXe4y931j0KP7NEZ 6f1Nwt4YwmbvPpgdo923Q3qrKkD0oD4XJjhZmFI4HMWVPmcyF8ufbOJOEM/Vw3zqvbw6 zdpT0bWM8dR84ceY2WIi0FpWhTxMpfJpkvSBSliXmJqzeOgQ90WBtUiwK24SSBUtpwi0 uuBg== X-Gm-Message-State: AOJu0Yzh7pa9TxsULKKIPQjmID6XjAjl2v3XtP7vbf4nJ1KMrksWNL8T X1K5c0lr0T25kFQsokaabQ//X5MYY35u6lVDqmw= X-Google-Smtp-Source: AGHT+IGUcfh06NQrQOrGHM1kFZoibVVPP/eE7poDoTE0im+m9TccKMM2mqxqGmRQR6l4v1SZGlfNDLU9oy3l X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:e2bd:f1f6:8ea6:8d6c]) (user=irogers job=sendgmr) by 2002:a05:6902:100e:b0:dbe:30cd:8fcb with SMTP id w14-20020a056902100e00b00dbe30cd8fcbmr298094ybt.0.1704258457094; Tue, 02 Jan 2024 21:07:37 -0800 (PST) Date: Tue, 2 Jan 2024 21:06:32 -0800 In-Reply-To: <20240103050635.391888-1-irogers@google.com> Message-Id: <20240103050635.391888-23-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240103050635.391888-1-irogers@google.com> X-Mailer: git-send-email 2.43.0.472.g3155946c3a-goog Subject: [PATCH v7 22/25] 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 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.472.g3155946c3a-goog