From patchwork Wed Jan 3 05:06:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 184626 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:6f82:b0:100:9c79:88ff with SMTP id tb2csp4838957dyb; Tue, 2 Jan 2024 21:16:00 -0800 (PST) X-Google-Smtp-Source: AGHT+IGA2GpIp6GiSta/gdIDQFxTJggPgHdbDrj6dLjT3pUPJkwiiychrucPWCnCAO2pZj4gGHxi X-Received: by 2002:a05:6a20:7345:b0:197:4d7e:c606 with SMTP id v5-20020a056a20734500b001974d7ec606mr2234719pzc.10.1704258960351; Tue, 02 Jan 2024 21:16:00 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704258960; cv=none; d=google.com; s=arc-20160816; b=RkDof9PlgKWusdcW7buAK/WlZXw/9t5K50WGZuRBZ0+DSh+5LYiKzZupwAQppG518G V2+lb1V4uu6j0tC3b0xfPlfZvT6RS+4hha/rUHUX2ZnM/0Z1fDu9U+bozRIKqSWiaL6/ ooxz/3zTxoEwTuAHYPi1AsE+MHcju0Xaw4jPzAtB1tFbS1EXUg5aiu/MydhgDdiAHVop ybULoR8ZFN6iHyecyiZp4w38hdhbBNKISBuenzPVhUtMqX05vBhQrajOPb9tNTC26cXc F4snc7bEOkZMsgRIFWAgnTJZT2GmpzIZ2SzcnNXIqlptIylsqvihcW4JiQbj4scBIs8l n1+w== 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-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-15181-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id h2-20020a63f902000000b005c5e2488c32si18221071pgi.405.2024.01.02.21.15.59 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 02 Jan 2024 21:16:00 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-15181-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; 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-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-15181-ouuuleilei=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 sy.mirrors.kernel.org (Postfix) with ESMTPS id F2D05B23610 for ; Wed, 3 Jan 2024 05:13:54 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id E61C71B29C; Wed, 3 Jan 2024 05:07:41 +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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1787045043159537080 X-GMAIL-MSGID: 1787045043159537080 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)