Received: by 2002:ab2:6857:0:b0:1ef:ffd0:ce49 with SMTP id l23csp568585lqp; Thu, 21 Mar 2024 09:06:00 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCV9DZ9ExnAdLrzRV9L8vXw2wtlMi/RjxkeIVLYS2veSirojeR5+NrJ7agIk/lgNZHrwXQa+YiQsgu1DLaqn2AwHsytDLZK9WAJg3wjQ3w== X-Google-Smtp-Source: AGHT+IHL4HmT54eW1WiwdpSQtvYMaQkt7UcSTz4ZRmcA9QB4kjcncakLutv9mYIqXHyGymFiFxQo X-Received: by 2002:a05:6a00:3cc6:b0:6e9:38d0:5019 with SMTP id ln6-20020a056a003cc600b006e938d05019mr4096663pfb.0.1711037160182; Thu, 21 Mar 2024 09:06:00 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711037160; cv=pass; d=google.com; s=arc-20160816; b=T6VloYB/0Eebw5RQbkX8H5wmpsdNUZ+tdi4iAEVThNbmZFbB7TzjrTZJ8ULNRkIEba AjwWztjwwuslvc1Ev20BDq18wMRtVTJ5d0kCcpFPLrQh0V4r1b1PUZtbUxYvbpSfL0iR OtoDWVizIPc01T8rfE6c+qcB6Z4LUbMMZh97MR8+iCI7mdon99n68v3vbop/wn+CzBIF nWi3JOOHv4czs4zvmDYJmq3S4spd2tus+YOuhFhPSiKLWfuDtBG7AJ59R4P8lY9YEcW9 tr/zSZui7lOsp/fmNA3dO9Ac9Ej+9kukAavx5/mdOrYAgKWF8OTu1SsKKUEYlPBgV8yG K67A== 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=o76ugCV3An0K5fdYO1n9nYRvHF1/7Rh4K9YTwb1u+VI=; fh=tubqaBFGe2VB5x+Bg4qjMuHDrqjHqn9ZtG8DReEfgWU=; b=U+8GcklTtY/JbmI2bCXNl4kkREnVJq47bnBHVC+uvY0FB9WF23RJ206vCtFbgHKEw1 t7ZTSt9XjyAvMmlW6cc7w+nHhU5ByMAh5Kpfo6wMZJHzGDVtzI7+uzWNzGMdCaVPslwR JESbeXQmGi11UuzA/lKCV3MINm/OyY2NYQZwKmfyVBqm1OP57oUMyH8q+xCc+0F198nN NARR55Ny3T5PajBJehg3lRLuV2s/AxK8lEfZmDobUVQ3ktcrtsu8z0KQNjNAGyELW7Zj /ajcxEqfHkhG+JukxYRDzfKQqIUKozaOjPMpKMEcs93Fd4vOm8QANs00aA1rsyRAr8z0 tGwQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=HeYmSESZ; 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-110305-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-110305-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. [139.178.88.99]) by mx.google.com with ESMTPS id e24-20020a63db18000000b005dc1b73dd16si39092pgg.372.2024.03.21.09.05.59 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Mar 2024 09:06:00 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-110305-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=HeYmSESZ; 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-110305-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-110305-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 84B02282A47 for ; Thu, 21 Mar 2024 16:05:59 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id B2C1E12DD89; Thu, 21 Mar 2024 16:03:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="HeYmSESZ" Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) (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 D608D12D752 for ; Thu, 21 Mar 2024 16:03:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711037015; cv=none; b=rqdf2R5azPeqEx9c5Gqu6mJaFJE+BZTwsQsw6SrL+oRHvG6U8kgcjVrVQlKMam28O7B2XRjEvof5aEN8jlkqStNLDmgnaiMTeiTMlxAWiQdsryK/NL8aNL30YEuuRfNeU/q42RCGDHOW0GEWgkS9CvzaOIJeEupoIPfXDdwHs98= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711037015; c=relaxed/simple; bh=FdcVwa6jh1vhtevvSVIZ1eIHhvgiOynPZiHgwCY919Q=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Content-Type; b=XIKWXh2qcOCp2RwJcuc0py7OUvBkgoJdDmDXdB+Wk92lP1Jh4C9ykxyRMA8IjA7ccKRn13g/2CiEjm9GHbjheNeZgXezxzZs76+ZdGr45YWRZ8C1yPYb184rOirdGtZAdMcPbn9NOE7IWyYNqKzxBu+vVsJC7LkYT91vXCVZU+k= 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=HeYmSESZ; arc=none smtp.client-ip=209.85.219.201 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-yb1-f201.google.com with SMTP id 3f1490d57ef6-dcc58cddb50so1692207276.0 for ; Thu, 21 Mar 2024 09:03:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1711037013; x=1711641813; 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=o76ugCV3An0K5fdYO1n9nYRvHF1/7Rh4K9YTwb1u+VI=; b=HeYmSESZn5x3DIPRLeBucraWCq/bgEFJHR0q2EN4n4ZyOefkgs+t67EjNci57gdvc5 VunoEHR/CK5PWNDJP0UknfcdcqJBZ19QaDhtOe1IBzhYHAa/ta2Dp9wVUgeVmbFAEGQB LGfmDzG7ODXQfpYF/GiAPreieJtOAfwGHfHyJDAGXqyxeR3Sbc3bm8IhzZ+/tzKRQrht yqxVqcPYtcElRljCpa0Svli9eee4H+pyU4ayjq317O2krAa1FlwaNhfZTpuIed60Cstn N6TlQS5e3Quw4AiPaV7pCxBt5Ptigynp1zHdi1i5DxmmdxibNNKbLBubULfy7CTFEYBC friQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711037013; x=1711641813; 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=o76ugCV3An0K5fdYO1n9nYRvHF1/7Rh4K9YTwb1u+VI=; b=wvSMfhKLfPqFPSI856m+g1M1rlZqxBtjJYNzCQwnrrKXXhvyJLWACzhcTkPVFQBxTT okr7HpSQ+yTgLdHR9T704//j8s0x2JHMqynTGK+edrQP4U2P6R5kp6magDiwEVjTgH+U UJBgCfQwxxLv/fieb0axoz87j3q0bY1Zr4JsGC4fLn+5xbbs/hqsJSk6H4rYZHO9IDJd Vew9Aj/vxb7H8geevNI8Ee5lJMi6tpr1i5RlOara5KafWQdzCpfTTOb0ll3lylN9N5Kr /R/JHPqQBExXnAZPP4jwkQxlvRIFfmeLdbCsr4zTGK4pHgAkj2LYcjqL3ihlsQ6LmkC8 hHPg== X-Forwarded-Encrypted: i=1; AJvYcCUE521fMS0tl6ixgDXTg/aS2hyx3ZVdQHvRrCaNYZNRBIAlR3i0jRgLGKJji80TlCkbjCo6d2JNB3RjYqDLIHbrBiz6bwUtBhvDGshI X-Gm-Message-State: AOJu0YzaVw8zPh20lAfo/wVqV+ABHQS3F1KLGnGmJ1RpTzT2PJopX2M0 CMSNa2tjDAbsy1o0qKESCd03SeRg6WtHBNOcu3hBKNkauVMokuopOPd3VVRaovRJ2tH33MciSH/ PASe63A== X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:7f3:cf74:42c4:c0a]) (user=irogers job=sendgmr) by 2002:a05:6902:18c3:b0:dc6:cafd:dce5 with SMTP id ck3-20020a05690218c300b00dc6cafddce5mr5443997ybb.12.1711037012790; Thu, 21 Mar 2024 09:03:32 -0700 (PDT) Date: Thu, 21 Mar 2024 09:02:57 -0700 In-Reply-To: <20240321160300.1635121-1-irogers@google.com> Message-Id: <20240321160300.1635121-11-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240321160300.1635121-1-irogers@google.com> X-Mailer: git-send-email 2.44.0.396.g6e790dbe36-goog Subject: [PATCH v2 10/13] 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 , James Clark , Athira Rajeev , Colin Ian King , "=?UTF-8?q?Ahelenia=20Ziemia=C5=84ska?=" , Leo Yan , Song Liu , Miguel Ojeda , Liam Howlett , Ilkka Koskinen , Ben Gainey , K Prateek Nayak , Kan Liang , Yanteng Si , Ravi Bangoria , Sun Haiyong , Changbin Du , Masami Hiramatsu , zhaimingbing , Paran Lee , Li Dong , elfring@users.sourceforge.net, Andi Kleen , Markus Elfring , Chengen Du , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, bpf@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.396.g6e790dbe36-goog