From patchwork Thu Dec 7 01:17:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 174856 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp4488993vqy; Wed, 6 Dec 2023 17:22:09 -0800 (PST) X-Google-Smtp-Source: AGHT+IEyTBfLuFm9j8EbPLHiDFNdgprTic7XV54/3mCGWzZoGi3IojspjMl39rUaE0tfyrk8II+e X-Received: by 2002:a17:90b:1b03:b0:286:1df1:25be with SMTP id nu3-20020a17090b1b0300b002861df125bemr1552141pjb.19.1701912128957; Wed, 06 Dec 2023 17:22:08 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701912128; cv=none; d=google.com; s=arc-20160816; b=p1HVejDo4n65iJC43gl1PqksnlJJ35IqHMg9rN6aHN5aNbf0Bq7ZdybvDiQfbUETkF DZuxcFgwgK0c7W65DZEazPKfX7G3/4h6JPpn3kSwAST026m425HoDeEpFu2xoc5/KPNf iJ0En5cQHGq6s2ma/SWtsQplSMsmAxItSCB6OINKV/31kwpJ0dmC4GWL655E3XzUDQWJ MxaLCRozVzAM0bim7CsKomlNQHK8fCptocADPMpycFS3too+pJAAp64QqD6vCPoSqdcL iDnCi9lmRuxa182Y61xVZ03ms+4PnOZ0JI/+Q/IA+DAsv9h8h/ejH09z12hSao7CYhcv RbWw== 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=LfZhXz3aJqIR3B942uDWzE+/1mlKepIUmUSXOJMumf4=; fh=Io7x3OacpefNRRNKoYAzVE0aWTDwD7FRzyCdD1e3AN4=; b=TpoOAS707+UiBGkhmUdQC2I9V2SNrZPiW8DFt34bm2kkY6tI2ek+5t9y69V1jHvDsK wOtQhVDpxgaQfRD2dkgo2VGZDNpG+G45NaS354MwicDH+kmzcH/h4rZ3EKBMYm0Afu5a SbTIRVD40eJWxI2XbcrE6Wdcj2l22zGmu28Ykfw4mts4JWnyRdPnFZbE9P+gNbKZfzXZ GIwRUWfXRCXFB61/RCud1kFRHUZZp9QCws2yBaErtxjNmTLAr7++umnw/mFrOInXbOeT rhNtFvh65yUUvxLn1nd2TBcBDYYeIYRK1jDIJ33zptqWqvmbgZ4lQlegyViK/2sCQQGV 28EA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=JCi8naAa; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 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 howler.vger.email (howler.vger.email. [2620:137:e000::3:4]) by mx.google.com with ESMTPS id gq18-20020a17090b105200b002802d12083fsi93790pjb.54.2023.12.06.17.22.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Dec 2023 17:22:08 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) client-ip=2620:137:e000::3:4; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=JCi8naAa; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 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 1B4B680977BC; Wed, 6 Dec 2023 17:21:18 -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 S1442966AbjLGBUs (ORCPT + 99 others); Wed, 6 Dec 2023 20:20:48 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60642 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1443054AbjLGBUC (ORCPT ); Wed, 6 Dec 2023 20:20:02 -0500 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 60D2C30D0 for ; Wed, 6 Dec 2023 17:19:10 -0800 (PST) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-5d064f9e2a1so1917687b3.1 for ; Wed, 06 Dec 2023 17:19:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1701911949; x=1702516749; 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=LfZhXz3aJqIR3B942uDWzE+/1mlKepIUmUSXOJMumf4=; b=JCi8naAa/FIYwAjXY4IwCJVGLyiAohN1z3QpUtrfdX+ujw/8ya3r94BJuEsn5a6MwZ LFyrvLM5BPPundoEly2CSqTgjwTSDVLtCQH24uEKq2YqPmeW7XuwSbwtpOIJhFA+mMji qA2Hvoz4ZGrd8QEMqdDOMwhNz2L1bKG8F+zw+9p+poeyZr/E7oGpN+v1SQdwJMw6g5XI QR3hFwqsbydVeHRRsh3u0qyj/+rPLSty2/1gLBC485SwxZrve8iNda4xie2M8zxKp7EM wDT1A3hfcmHOh1vsU6lBbi/JPnnUO9qKuK7AZOJJ3O/akpNtdgra0IKE6XQqiniwORUm Vrcg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701911949; x=1702516749; 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=LfZhXz3aJqIR3B942uDWzE+/1mlKepIUmUSXOJMumf4=; b=J6KAAf5CCXnvmDSbGeV0+pZzoKUzLCKTHVbpiWkpXP7S9K9lBpvqNtDQkLb2B1kRIs 9kCRfDFh4uQ+gk+dTyk6m7o471NytFd3e8tJiEGWENj54uWxZAur+66gXf1ocsK1gMI7 estmWQyYn6SReX5VldgLCrcqQOyaJnTVlxEjb2EOmP4AVnvo35uMeay5BfcNXrr3YRoJ ZAK8x9xkW/X/wI2tE8KIQJN4H6tjV1qfgLDUJB7HjO+3W5HnOOh+GrC1Gd+VCDlB3wsb Jr3jThONqW7JUSFzXgizupaNJmXKjoA0j0Zipj6xnQNz/ZTTdKhS7dWlKd4l/IIVurgn 9iYQ== X-Gm-Message-State: AOJu0Yz8Xhi+tVpahQf1op5RArzyPz82vpJo8RhpWys1TWfAZrn8DYw8 diWOnUQDLrgqI94H2rq7+l/i1jqckl7E X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:35bf:293e:7696:34e9]) (user=irogers job=sendgmr) by 2002:a81:be16:0:b0:5c9:c698:cd34 with SMTP id i22-20020a81be16000000b005c9c698cd34mr31433ywn.2.1701911949131; Wed, 06 Dec 2023 17:19:09 -0800 (PST) Date: Wed, 6 Dec 2023 17:17:18 -0800 In-Reply-To: <20231207011722.1220634-1-irogers@google.com> Message-Id: <20231207011722.1220634-45-irogers@google.com> Mime-Version: 1.0 References: <20231207011722.1220634-1-irogers@google.com> X-Mailer: git-send-email 2.43.0.rc2.451.g8631bc7472-goog Subject: [PATCH v6 44/47] 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 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]); Wed, 06 Dec 2023 17:21:19 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1784584212532513543 X-GMAIL-MSGID: 1784584212532513543 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)