Received: by 2002:ab2:6203:0:b0:1f5:f2ab:c469 with SMTP id o3csp2709319lqt; Mon, 22 Apr 2024 21:27:36 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCW+XzDQaypCKWvdXs5nPnMipwL/CpRBS/pMNXRl+Jrdm1YhfF3kHNTlfdo3JO2ww2quesv1n4zgbqjrKRDphsNQo2DIxeXWBp71kIIF2A== X-Google-Smtp-Source: AGHT+IE/Ivkic9khSA36/iaW7yvcX7PtOUF2pq4r5RvU5yNn1TcFtOsIu87iXFGpTvNf0e3U0DTE X-Received: by 2002:a05:6830:140c:b0:6eb:7296:213e with SMTP id v12-20020a056830140c00b006eb7296213emr14337133otp.10.1713846456557; Mon, 22 Apr 2024 21:27:36 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1713846456; cv=pass; d=google.com; s=arc-20160816; b=Cgj9M43lmARWIaGDDEKBnWvRu1V0CbKt7JSRq3xHRDOd96+cC1d/AaMAZdspyT6xl2 DH+NC4P1dxnBqSj90IaRo37TeCULEzWQGaBrvkh0WMPF9bNwE7Dr40GE2X5e2dLoB6oX ErhDTIM4klwXkVhU0vyaNjtvvNigYtRPvqiW/pppiSwPoGTJY/s5kZfX1CZFhu/X4TDU dSt/Z6Ymi8FQT0kq9WcCF/sQpXQHlsklI7CzzTlXKv2mgrMpsX+Y8/LzzaBckjNl6u1w lZAdRSQBi/LUP3GTpLxRVH4iwzZTStaOMjsqmCxoZHd0bStu9YSpD2dhI0MWIJyGkXWv I1NQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=to:from:subject:references:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:in-reply-to:date :dkim-signature; bh=t830lzcH7ZkA8fGoYpxo9R5dnhJpcUV3/G0NAtLbkkI=; fh=4oG9eqKHt/HeZ6ECPSG6ql4kPfMdrcJ8lnRma+SwBYs=; b=dujNNiUW22DDWYPeynH1CVRLztCPSqwf1+48WcG82PVkXBoIlTSkOW08WBgQUch0zm 4OQweGkf4oxw2evTKOQMzdLFh36ZhRHdGefxNLdfu6W/jOSuhftn6LiZl7IP6b51qOdq +mQP2evmy535VSLGkxDdsZbmb4oLQ358tKw2BFfupUEJ6508250vQ9pt840tghBuG0Zf +ASp49MIDeO3IrPYbFd0mg+Zh+9YRbVs9neLbT7ZUVHB7uUl4Xxl7sONYpVrCb3oKnoP /5cN8Ug/ZVXi1KCSnHWcjNXGIxCsZlA67XUY0Tv+2F9XC7TNxfRf6KxtGnNGhSXudBk6 ccbw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=qkTVVeHW; arc=pass (i=1 spf=pass spfdomain=flex--irogers.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-154499-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-154499-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id c5-20020a630d05000000b005f3e0556e31si9049403pgl.863.2024.04.22.21.27.36 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Apr 2024 21:27:36 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-154499-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=qkTVVeHW; arc=pass (i=1 spf=pass spfdomain=flex--irogers.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-154499-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-154499-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 076E728129A for ; Tue, 23 Apr 2024 04:27:34 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id BE4912BAEB; Tue, 23 Apr 2024 04:26:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="qkTVVeHW" Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.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 D7A1423775 for ; Tue, 23 Apr 2024 04:26:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713846391; cv=none; b=hWZw5GbNSFcNng+mdqGXKmle9++elCOcINXTOiTZRH0+Den+4E0d3TskH7Vq2BPv8m4KU5Ninb5qG3bdQzBPbl36vfOE/WHX5ZfWUQ6YL4lM4cEP9OduByyqalKs7IiNSr1OlRo3C6tfdn9vRYb6jVB4rrsXhdLdyyhlv7kUb8c= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713846391; c=relaxed/simple; bh=lIMr2C7pqzDmaOI4kvlL+gVjJA0HX90NyHek76POXmg=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Content-Type; b=hfdPtjZlo9Xte0pKFPpUCnSfoz+h6UeGH0dkgZE0sWpHz2VNesl1JGsveJDk2Ds/AbAMPJ3yc26KXFZCgWCLxMMHVZyC8lvCNwv+g/Z5vfFyuBJ6p+WoJ4gvtF086ZiPaOEdiMbjbWJqNPTPTKRNpmffwIEOjnpfDRW43EYdv/c= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=qkTVVeHW; arc=none smtp.client-ip=209.85.128.202 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 Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-61aecbcb990so99093957b3.1 for ; Mon, 22 Apr 2024 21:26:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1713846389; x=1714451189; 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=t830lzcH7ZkA8fGoYpxo9R5dnhJpcUV3/G0NAtLbkkI=; b=qkTVVeHW1fNCZ7HhC0v5Bzt5d59aZzAUw4BR7iDK1fC3hUOanK63jbZL1dsQlRenJT vldyWsQrPhaVdADo4PeSkl4633lOWQ7aRw/iOO8NVfpmtsY6ehv5BubAinxA1G6/G5Ly TRnEtUwZWQtGIrp2sMycu8w+vMWK1pFebtEvM0sVS0WyHp+bOMDlC1yV4NI7g98tzhAh temy5OebKRkzuh1whSDt7YE6Pz72foPwkkAseLShhIbRXwLqhNzK2pO4dKvbSB2TrIQk jGdbae4FLQUiHUC+YhEWyTkcV1qR+mChTQ7GSuilB7ThsUWc90375D+C0MZH5NpWkWaC wyvg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713846389; x=1714451189; 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=t830lzcH7ZkA8fGoYpxo9R5dnhJpcUV3/G0NAtLbkkI=; b=LiR8FApspbpLSRh4GMSUdasJjXm/t2syxNLK/OGGbyL6e73V30M7V67n4Hm4StfER+ h1tSEhiJq08qAI+NLnX7YIsEtVL42ZIT4k7y300CoqWk/ECHcBq8uw7QjyEjSIpufS1Y PBnPWSTfCFT4rwVzttBRze/G2dswdZzlF0hB21eXiBRC0Yk1wL4rk4c9YLRwoMaMXOV6 H8wCQGD0j0Y2jC67ONwbpnxcKgQbet8TXfSzDRnV6MnNwRoFx6W1TbGj9TgkR662ISqy gM7xWeWdr9jpQWUNHJn2e9AK1YVI65M6WOi4PUsr1HBpHSBWHo1w28XLT47t+r01FBE1 vkHg== X-Forwarded-Encrypted: i=1; AJvYcCUTpRGOKrM1bXvfvEnaauyg/xhyHhPtFjcZbnY65XbHEzVok5izGReg8PqAhVpA7/kRg5+WFLLSm/MktshDPHbPtBMqscoOwgzhpowD X-Gm-Message-State: AOJu0YwzIpPyCRZ/TmZrhzVc5CRIPZvgdGx0ZkvQTMaIdDvb8hFD14Sz KOGfDPCqkviui2eHqm1WkLy7kOmOESx4XJ6U9N0S48Thofv0CY7EGfzeR9w0sxOvqs7jD5GumYL fsid/1w== X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:4ca4:4d48:a7fe:d70c]) (user=irogers job=sendgmr) by 2002:a0d:c803:0:b0:61b:5888:aa59 with SMTP id k3-20020a0dc803000000b0061b5888aa59mr1710937ywd.2.1713846388874; Mon, 22 Apr 2024 21:26:28 -0700 (PDT) Date: Mon, 22 Apr 2024 21:25:58 -0700 In-Reply-To: <20240423042601.2009469-1-irogers@google.com> Message-Id: <20240423042601.2009469-5-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240423042601.2009469-1-irogers@google.com> X-Mailer: git-send-email 2.44.0.769.g3c40516874-goog Subject: [PATCH v4 4/7] perf dsos: Switch hand code to bsearch From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Kan Liang , James Clark , Athira Rajeev , Colin Ian King , nabijaczleweli@nabijaczleweli.xyz, Leo Yan , Song Liu , Ilkka Koskinen , Ben Gainey , K Prateek Nayak , Yanteng Si , Sun Haiyong , Changbin Du , Andi Kleen , Thomas Richter , Masami Hiramatsu , Dima Kogan , zhaimingbing , Paran Lee , Li Dong , Tiezhu Yang , Chengen Du , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org 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 with_hits) return args.have_build_id; } -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 = strcmp(long_name, b->long_name); return rc ?: dso_id__cmp(id, &b->id); } -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 = 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 void *va, const void *vb) return rc; } +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 = vkey; + const struct dso *dso = *((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 = 0, high = dsos->cnt - 1; + struct dsos__key key = { + .long_name = name, + .id = id, + }; + struct dso **res; if (!dsos->sorted) { if (!write_locked) { @@ -162,23 +181,12 @@ static struct dso *__dsos__find_by_longname_id(struct dsos *dsos, dsos->sorted = true; } - /* - * Find node with the matching name - */ - while (low <= high) { - int mid = (low + high) / 2; - struct dso *this = dsos->dsos[mid]; - int rc = __dso__cmp_long_name(name, id, this); + res = bsearch(&key, dsos->dsos, dsos->cnt, sizeof(struct dso *), + dsos__cmp_key_long_name_id); + if (!res) + return NULL; - if (rc == 0) { - return dso__get(this); /* Find matching dso */ - } - if (rc < 0) - high = mid - 1; - else - low = mid + 1; - } - return NULL; + return dso__get(*res); } int __dsos__add(struct dsos *dsos, struct dso *dso) -- 2.44.0.769.g3c40516874-goog