Received: by 2002:a05:7412:b995:b0:f9:9502:5bb8 with SMTP id it21csp6936750rdb; Tue, 2 Jan 2024 21:13:44 -0800 (PST) X-Google-Smtp-Source: AGHT+IHAX/Ku3zLaNFIbl3SQZzwxoeT1K0EuP6nIPLaKgEjW7lxhVbvRs0lUTgNeUrzeSgBTtFiH X-Received: by 2002:a17:90b:370a:b0:28c:edf2:d5ec with SMTP id mg10-20020a17090b370a00b0028cedf2d5ecmr2350pjb.11.1704258824048; Tue, 02 Jan 2024 21:13:44 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704258824; cv=none; d=google.com; s=arc-20160816; b=wHyZ192qcrg1bvzArp5TtT6I48/381OaMbpbai/4PYREKvWc+UjJ8F7IT3HLDlzowv PqtEbCwKtseFCePzn/HZBjgyOzA55767f3jBoulzO0bfCSijauFaNeargq5AObbijJEx Eh5g5B+4+Ay5nxTZKRaeO+n5od+9R9m/3Ss4Cl6ZVrJIFNv6hovDRSZZ+8txUaAia0us ysBvw1Qz+fkqbEy+TB8uk1VBTMNrEUMdJMZg7UvsU1Dsy9hF0TKX2HSiAwVOwrwEF50u KDN9AW3uu4e8xtSYqTnBKQK5/r0fuX1h5O7qYymqdOa3+K/U/gC5yfC9lf67AnXokNZK MmIA== ARC-Message-Signature: i=1; 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=J8oNQkD+IF9X70W7WTonENWn/fXyAV8WC3tSRa9nOmo=; fh=Io7x3OacpefNRRNKoYAzVE0aWTDwD7FRzyCdD1e3AN4=; b=T28TiX3C6FhhfAE7JT25cGL4cAaNExQfIbvKbbBMyW2nx1nNCTrs20nUnZmRij7+9I OCAV0CVRiOUKRZrChBpAkl9IA8bvKwxB1EiUAnu2YLzoDDRmADCxSKPb3edFjN8BEfbW r1tePco4qrJNn7H4z4JnEAs0YZslhfbn3RNp3Fn08GGTcb5L+nrCS4G9VlyZvCNhMkhS q3YFfxwwK5lg8sJkZjopEnP0qiU0gylFCon91cwQadxiwRo6a4aW34tPR3pMR5mJxpMj K1F15a8VlBGg02hr7in5ig2wT9Y9r81kmP5SobiU4xgd17Ya2VzsKDdvt9hJejChzp8Y 6fog== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=MeqAWR5u; spf=pass (google.com: domain of linux-kernel+bounces-15181-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-15181-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 l3-20020a17090ad10300b0028baf60257csi602863pju.157.2024.01.02.21.13.43 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 02 Jan 2024 21:13:44 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-15181-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=MeqAWR5u; spf=pass (google.com: domain of linux-kernel+bounces-15181-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-15181-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 C4B09284F0F for ; Wed, 3 Jan 2024 05:13:43 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 2A6861B26C; Wed, 3 Jan 2024 05:07:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="MeqAWR5u" X-Original-To: linux-kernel@vger.kernel.org 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 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-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-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.43.0.472.g3155946c3a-goog