Received: by 2002:a05:7412:419a:b0:f3:1519:9f41 with SMTP id i26csp3430406rdh; Mon, 27 Nov 2023 14:15:51 -0800 (PST) X-Google-Smtp-Source: AGHT+IF/PqetDhjkdnSxDECtYefPrnGttgSR/qPY8sx96bycNTzdFT8TQ44LZOvR1oSFdhMUQfTg X-Received: by 2002:a05:6a20:1587:b0:189:c987:76c5 with SMTP id h7-20020a056a20158700b00189c98776c5mr18012543pzj.43.1701123351572; Mon, 27 Nov 2023 14:15:51 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701123351; cv=none; d=google.com; s=arc-20160816; b=VTCUT/CoqCY6V2J4fMpcZ4BGTAexz0iOyXcosmB21w1TLukEtZQ8YJu6qKBUix3Jpc 40I1TT42+U2um6kb9YukLbNsMyk0kpRyR/7Cw4tuhslhLZf+57OQooCbZd96geAhI709 u+lQxWe8qgWByaIp0SSTl7y5y7n+uAAV1CvhuVY3Uw7Tgtvpn5ZygNVFgoK5ufH0eJL6 sw5SCKmu1Xnk85ISgy6ARcat/IVaxVXIdpAjrFrpJqzU/UQ/U4qxKjVo/odN95b46jDK fL2y5VMLptSwRiuJgdGrOvPwg6cJFBPskzxhd7Kw0MZwzpuF8H1T2jqIlvO7ry+xZy/1 eR3g== 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=DQ1JTb4irNPUQ3W+I5RN9UtOSGB74icrgqG3j5gtDIA=; fh=Io7x3OacpefNRRNKoYAzVE0aWTDwD7FRzyCdD1e3AN4=; b=cXvJ1U2FCNtcoicKKmIwyJCRyAHWieXiVmlYb8zB2vI63kIUJyoERuZLNcpkm2nwvm jXJuBZmKxKk1vhEmQoutPFL+tQT9rAdawn+uxQ2tsOdhbLDB44a4ZJkux/hGIOgiTKPd mP2EXmR4FscMYKhiJ3FeazR3djrdt8q8pKz98thQOtAj7OtkUdMBE0FglEyb+k150LP1 jMpeh3Av6NLvVMmJjYtzOQwhRkra9E8CoL3zxBwrTvTMa0nLVQ90eqdWx1lVDuCF07m8 Gr8gmQs6kHS98asLTYhM8DCa+YVRT/yXtT9OerkRsgpk7hANPNXq3wbDjPjgNCE04HEq SHxg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=Iiymtfqs; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 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 howler.vger.email (howler.vger.email. [23.128.96.34]) by mx.google.com with ESMTPS id e7-20020a63f547000000b005c200b11ba3si10443864pgk.888.2023.11.27.14.15.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Nov 2023 14:15:51 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) client-ip=23.128.96.34; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=Iiymtfqs; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 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 howler.vger.email (Postfix) with ESMTP id 5800282250B8; Mon, 27 Nov 2023 14:15:39 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233629AbjK0WPI (ORCPT + 99 others); Mon, 27 Nov 2023 17:15:08 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43228 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233739AbjK0WOM (ORCPT ); Mon, 27 Nov 2023 17:14:12 -0500 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C7BA63873 for ; Mon, 27 Nov 2023 14:11:09 -0800 (PST) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-db403a0f25eso5806003276.1 for ; Mon, 27 Nov 2023 14:11:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1701123068; x=1701727868; 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=DQ1JTb4irNPUQ3W+I5RN9UtOSGB74icrgqG3j5gtDIA=; b=IiymtfqsCTWA344n2eS1Nk4416DMhcZbHTG3e7LwsX0W3reNsghB/jnBMGn0GaqDAy iNkyWONjzTLaVqowf/9oNRhWHyBrmLyqJRTK2QPhIOlhm2ZRJKtzERQwZvrMdgARFuv9 64m4tYgidZr7WXJZuY8yyiabMwS2ya4me88sh449nRahBeoobP+fVzCveHDdcvMXn79r N7JaSmWGsMeIc755/nmjLPIhLUAmEg6aXRTpF57YMAvRQu9lTyufgDuNvdyiLmegjcqZ ZBSUid312LBZxkIWAy/k3kPy/5sodhtrgl28Ywalz5fpNBDWwj6Z6E/fisbaISQhxgtV 8zDg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701123068; x=1701727868; 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=DQ1JTb4irNPUQ3W+I5RN9UtOSGB74icrgqG3j5gtDIA=; b=DOGHnCbiOcGUghodBzVnX5sslQJ5JPKTo/nOGUoVFiCe00ow3afO+EbrfvFy2JdkAS xvcqVCfLy7IqHxklsp4Pvze8e/MvCxVwk3dIvtTIAp+FB2uCVBOWmQc+6mwjnJAudfx6 Hl09L5h4JzzEGyplP3/YP4eyagq+K0+bXKT/z/Ez1NtH3BIUoCH4d7GvGTfGYPCNn10s vMypcFj/lsi91djshNDBR8t2e11fgnDlkyD174eSYLeGLcOJbabr9MVNLdGURL5G2gYd 1QT7LGXQVU3FF6/98QNALUGEyZUToT4Zb6yueoqZZNenxEVlRqVuWHsFb4sLQzLmM1M9 NFgA== X-Gm-Message-State: AOJu0YzHZ4lsElBpX+8+rqpcbMzl9SND/7oqmqVnSSQz8OEOwysmcIfK ORbZJapogzGT00tDL33wIWPBRIonN0Mx X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:829:6e77:9093:f39b]) (user=irogers job=sendgmr) by 2002:a25:ca04:0:b0:da3:a91c:7356 with SMTP id a4-20020a25ca04000000b00da3a91c7356mr342652ybg.8.1701123068648; Mon, 27 Nov 2023 14:11:08 -0800 (PST) Date: Mon, 27 Nov 2023 14:08:58 -0800 In-Reply-To: <20231127220902.1315692-1-irogers@google.com> Message-Id: <20231127220902.1315692-47-irogers@google.com> Mime-Version: 1.0 References: <20231127220902.1315692-1-irogers@google.com> X-Mailer: git-send-email 2.43.0.rc1.413.gea7ed67945-goog Subject: [PATCH v5 46/50] 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" 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 howler.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 (howler.vger.email [0.0.0.0]); Mon, 27 Nov 2023 14:15:39 -0800 (PST) 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.rc1.413.gea7ed67945-goog