From patchwork Thu Nov 2 17:57:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 161037 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:8f47:0:b0:403:3b70:6f57 with SMTP id j7csp537528vqu; Thu, 2 Nov 2023 11:03:55 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFjMT12B93Mc12zn6M7Nc5zzxNRzsGAXk0NWWw2Gcp4Wd6l0j0WYnB72PN/JPcc1R4SfAmU X-Received: by 2002:a17:903:11c4:b0:1cc:6e8f:c14d with SMTP id q4-20020a17090311c400b001cc6e8fc14dmr8621382plh.50.1698948235548; Thu, 02 Nov 2023 11:03:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698948235; cv=none; d=google.com; s=arc-20160816; b=oBI1kjGLCjUOXwiMjDffi7WZYddXgLx1Xx2qwfuTRH3bbJFFCYpRTe3zKbYqFt11Xx mV/zdPCJGsyTmoZsED2QGySFgTKcs+H6khgs/LMCf+xEKi6eKX+YMFY8HcwU314XWKtT 4N6aqkjzqLfOdph0O+ttvtAdqgAo7ohTVbqNfttabaeyTtYAaQV9Wt63MJJOyWymQHCM J3DGDV5gTyegEkWRkkMUO9l90TBLOR8mRNUHbML4ltI0CKufk7tRl9ehvrfzGvunEtI7 yCcUrJwG17i7D2xT9wYJjqyAKUnsGxQph2vkJ/ejgEpt2EgJu+xFZwyf8kts8iZy5Pjq CSbw== 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 2620:137:e000::3:2 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 agentk.vger.email (agentk.vger.email. [2620:137:e000::3:2]) by mx.google.com with ESMTPS id n10-20020a170902d2ca00b001cc33933e45si72941plc.42.2023.11.02.11.03.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 Nov 2023 11:03:55 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) client-ip=2620:137:e000::3:2; 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 2620:137:e000::3:2 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 agentk.vger.email (Postfix) with ESMTP id 2CBAE8135BD2; Thu, 2 Nov 2023 11:03:46 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345752AbjKBSDU (ORCPT + 35 others); Thu, 2 Nov 2023 14:03:20 -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 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 agentk.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 (agentk.vger.email [0.0.0.0]); Thu, 02 Nov 2023 11:03:46 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1781476344780855648 X-GMAIL-MSGID: 1781476344780855648 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)