Received: by 2002:a05:7412:b795:b0:e2:908c:2ebd with SMTP id iv21csp557348rdb; Thu, 2 Nov 2023 11:04:02 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFDocmcHw/BwqdtvID9fIiPdL41gHlmugd+wO5xU+d/T6vxMF3nHcQRz8RnXGq+NxcmEr2c X-Received: by 2002:a17:90a:2a4d:b0:27d:6dfb:405c with SMTP id d13-20020a17090a2a4d00b0027d6dfb405cmr14307210pjg.42.1698948241853; Thu, 02 Nov 2023 11:04:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698948241; cv=none; d=google.com; s=arc-20160816; b=hFvdfYC5hN8WyyrVnbKTd1r+npqU3U7rEyBccM9ohabHamxp98JH4RbYfVb7+WQlOL 1g7qhU+TRgjQfXgM9DAFuEdf6bIw/7TsQhutSFIwSi8Me+rpfpKFCFVB4KUlauy2VkVc O0f9kyj29JVNkJ3bWEXdxhn2sapZzVUCVNNAGdxjZEtFjxGrDolxQLsz3Q8/F7dLAMKG 7rH4WkfPp+2UlLtVnwVHryxoMmokTUlyGh3LUQ/CehE/Ocu/c0LKCMA2Xzwp9NSVvy3m 01EIVZh85HjTNZ2jfLbwj60Y2oQb+kYCKWKug7QBc2JeGgagwGTraBYOdQYeZWvbcygK OHqg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=D7T69bsAUNRjcM3qmyt9J8wOUEZXUtACXk4aBD8XhGs=; fh=z1KcSqUpYQ9oC4uLSeIkhAYTnJ2bvP0QbNW2xqV5NqA=; b=gmX8OBRJk/nt+KebUYYMOnoSQ9d4RKmER1e4YP+2RdH1MN2xLIy+BHyA+81P37hD7f nrw0WXw848XrU6KHLxXO+MTm7WlydMaq7Mt63KFCUFaU0npI37mx9uVmDAyLOR7nnADD 7ygvVIHo+KvXilez0tjDLCcwrGvvZUMuhB7+vunPhjniPrR280QYoEVU+8FZHoY4Quh+ 7IdhWC3g6YMOKrcWaApBSxBG1DCMuiF8J3cBicYSKjPRM9bjDjt9BlwOASxVCihH0RS7 bMKp/WCK64DiQDVbjTFwg4BJdWDuUgu89uR8fmoV4s+3IbuRL7uAaNC1yYgtx0u21zRq WRtw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=gzGiV9oH; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from lipwig.vger.email (lipwig.vger.email. [23.128.96.33]) by mx.google.com with ESMTPS id gj20-20020a17090b109400b0027d2c3f25a7si110158pjb.80.2023.11.02.11.04.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 Nov 2023 11:04:01 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) client-ip=23.128.96.33; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=gzGiV9oH; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id 5B51C821579A; Thu, 2 Nov 2023 11:03:52 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345639AbjKBSDT (ORCPT + 99 others); Thu, 2 Nov 2023 14:03:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55996 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234746AbjKBSDL (ORCPT ); Thu, 2 Nov 2023 14:03:11 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 93C3D26A4 for ; Thu, 2 Nov 2023 10:59:49 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-da13698a6d3so1540245276.0 for ; Thu, 02 Nov 2023 10:59:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698947988; x=1699552788; 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=D7T69bsAUNRjcM3qmyt9J8wOUEZXUtACXk4aBD8XhGs=; b=gzGiV9oHebdm3n2Yr9GnfEx3osID6i9Kt3ONaGv6Pc/RCO9mktruKT9X9wzTwFWwCo kqIEZ7/EsHsgVuHM0iVacE8MIAzB5ssOuDGqr7JN1ALOPnRWKZct+dNBTVy+81xUElM5 Oi7K0EfeoiAzSZnLOO60qeZVvGr1kQQ029+ErDgXfZKQ1qHZ0VpZG7aGmidYHHF+93KS cJZrUwd51CE7AN0NYSEx+rjt5eMQPGXA+p3AnbGVLy36CDBSQtu/YOafp7t9sLO74cB4 4is/qP4otDRjkVA7wbgUYJFOtQ5Fy6lvmkX+mnnYyHEXxegCQIuxFtNgvFLD9q73Gsc4 EdDw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698947988; x=1699552788; 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=D7T69bsAUNRjcM3qmyt9J8wOUEZXUtACXk4aBD8XhGs=; b=jOvdGX8UETV2djZqjeN37UihTosJW3BhwaR6Ks9+wEw57AUss4KiPBS1StRsjhRWnf 2IGoUI5KFxLmfs6nznsjiamg2C9w718G5tVfR+SZMZjMJatjhlkRn02TD/wcq4VrNASS Xit4Z4EMyfCa9FzJCOoDCkfwjmPWhary8cUtlPW1Si2IHbsrULk06Mw7zdzG7gcz1Naa yc38I23ZErkPGQBeOqzkvkb82JtjHLjH0RfPqA3ZT/sTckN/tqDtEbDAqcLd9t7+ctDN 9r+PmuV9O1J7AtTPsDPwALKxkdXYH9QDlsUicYJCIjwl5d2qHLxNVff8yvzGy4tFKEHr 4TjQ== X-Gm-Message-State: AOJu0Ywztu4Kzb9ONkzl0BkhKDkTC5Ng36hyyHpYRtlmf+rhzzrrbcpm Dj2i3SDI8BfYcGNxsenxEqUKxv7J25Hf X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:bb34:df9c:836c:afca]) (user=irogers job=sendgmr) by 2002:a25:84d1:0:b0:da0:cbe9:6bb5 with SMTP id x17-20020a2584d1000000b00da0cbe96bb5mr382618ybm.11.1698947988239; Thu, 02 Nov 2023 10:59:48 -0700 (PDT) Date: Thu, 2 Nov 2023 10:57:31 -0700 In-Reply-To: <20231102175735.2272696-1-irogers@google.com> Message-Id: <20231102175735.2272696-50-irogers@google.com> Mime-Version: 1.0 References: <20231102175735.2272696-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.869.gea05f2083d-goog Subject: [PATCH v4 49/53] 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 Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Thu, 02 Nov 2023 11:03:52 -0700 (PDT) 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.42.0.869.gea05f2083d-goog