Received: by 2002:ab2:1689:0:b0:1f7:5705:b850 with SMTP id d9csp1556686lqa; Mon, 29 Apr 2024 11:47:48 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVhNe59RWMAcM6ATGSnRwovvxfp9QMW5ydgIihTElmbsV4Tmt2ivoidnKNNDgsYNC1L5QarFBbolupovqKQnOo3ly3CphQ0crR0qGuA1w== X-Google-Smtp-Source: AGHT+IEfqzCcBWwTrDK/1QwvC3ttKfYXQtaudk8YfLCNQC6nzSUVVTXOXaZ/G3m4tTNtartKG0yU X-Received: by 2002:a05:6a00:398d:b0:6ea:ad01:354f with SMTP id fi13-20020a056a00398d00b006eaad01354fmr12783773pfb.18.1714416468396; Mon, 29 Apr 2024 11:47:48 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1714416468; cv=pass; d=google.com; s=arc-20160816; b=tmfaNOhTwaH+gZMFgPStDuoY2w9e4zIa1J0NouzpK8RpMXQ8FARgmMyxlO6kzNOkJi jVuIYMENRGU7bmzo7s0SPWaBpeJQ/qzfx4Dj0XCCdGljTDnOhuQw9Zc1TI6zBTccFruA m/r3tT9rQIw3wp4M+by2ZNgaDJ1U2jJ7qy/7M4fvtc1DP0COSTW5IcihwVPNVb2/uKts lpKYa2vkbPr2ppTzGXMkwIqj8KmiMWAcHbciuVpiGp7CCM1+3wTsve9SZ7UVZjaRG15M qARl8FOsFWkz6QRPeib8izKUL/8A6N87APlsRpt1qjSwIMpWZ1N0VhqfUE76a94fb8Lm S2UQ== ARC-Message-Signature: i=2; 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=t830lzcH7ZkA8fGoYpxo9R5dnhJpcUV3/G0NAtLbkkI=; fh=HND2xn9tMnhF5k0br8jcJf+0JXkBEgJbO8Pj5KKuxKg=; b=CAU1BjiUiwwLaPeAFEupvaAPQY06j/SrPAlGW55dFDEdnAmrvWneUoWkYwONNd85Lp xHaKTCTZhkA/1+qX5Sv0QUHMAxmIgwkVTNTDaefhMxx729pFRVyVdxH/mgeHrJ+Kc3oN +nlY0fC8DLCs/Uq0+J9CknR/YN8sbsAwOpCmz6o1qr41hQEMimX6uXmZbmk33lIU8via ViFJZYD4ZmQG2uSB3Ww03wEAODVBVU/CSxhPn5FzF1h9hie4G056C7HrLA9PRjinlYTg ukP/png/YZBr/BkpyuKJejAjMpHxGeAedG31BC9KLHeIINtpu3g1RG9N7AX26fe2Awde oSRA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=RAsCQMof; arc=pass (i=1 spf=pass spfdomain=flex--irogers.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-162851-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-162851-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id k14-20020aa7820e000000b006ecfc9f0c78si20158438pfi.267.2024.04.29.11.47.48 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 29 Apr 2024 11:47:48 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-162851-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=RAsCQMof; arc=pass (i=1 spf=pass spfdomain=flex--irogers.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-162851-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-162851-linux.lists.archive=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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 0AB4F283ED3 for ; Mon, 29 Apr 2024 18:47:48 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 8408813C685; Mon, 29 Apr 2024 18:46:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="RAsCQMof" Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) (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 4AFCF13BC32 for ; Mon, 29 Apr 2024 18:46:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714416397; cv=none; b=Tsdq8ecy/QunBnjiweQuDZ0YEXovymknR/8m0fCLDRcQuXe61FY+SGJTM6YTiHXVtzVmZ8wDrlZeq6e6YRVgjcvw2YXtodDeQk9QTZ4wl85r3Z//0s8VAlCv2wDnBPbXsvt9Yya2lPNJUdDGctiTqUs3vLoCJGQeWHlTMxTOYzc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714416397; c=relaxed/simple; bh=lIMr2C7pqzDmaOI4kvlL+gVjJA0HX90NyHek76POXmg=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Content-Type; b=UrorF6Oei/haef21mmj63c7pwVQn3/pMBZ00gL1TH7mAWwbzWdZNj9POokRfG0iEz/Aed8HAYVaT3CENHtbfV36sQkzh3plFTX93d8zqUOfTSjFEq06Jn5bNGHgFZlwhfJSxYx++uE6PkP8vuZbfmnvhJj3XLhFOIIYlreGpZu4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=RAsCQMof; arc=none smtp.client-ip=209.85.128.201 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-yw1-f201.google.com with SMTP id 00721157ae682-61be325413eso6906307b3.1 for ; Mon, 29 Apr 2024 11:46:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1714416394; x=1715021194; 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=t830lzcH7ZkA8fGoYpxo9R5dnhJpcUV3/G0NAtLbkkI=; b=RAsCQMofKX/qnXwz+B98pphpFrLRT+OcKCBriURhKFpGWWrfVOT7lxZwoZaukH0Tch Dr5qdwSTeo5LjLmaOhJb9XsBEWZMTaSuLcLtDnOKMhibwz1JPV2KZF+1lte8lxYEqlUy eZTAjrvuTdzxMEqC61hTxGsXW0EisO3/0uhtUWhixxrsCp/a0G+lT3SdnHZEwIHG7FN2 kNfG2dH9LLYJ/wlWJhr1sTEA8vTg6slmnQ+FVFTxyEzC9wOu7q72CIp86wZOpHOHj7Oo 2Qk9yW8ylBRieadHqcwzSo83VLuTqYSpFdP2HEaIDJWsN5PQHatMXDhswa0F/mCP3LpD 6xrw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714416394; x=1715021194; 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=t830lzcH7ZkA8fGoYpxo9R5dnhJpcUV3/G0NAtLbkkI=; b=V5BVZ2qZ1H6ukUs2qGpPDHlHk7+prr4kFDLkFrLc3d09kuXIQ+UVMuDxi7IvtgyDcz ppxBN0vktiI9eJb591QgkgOsQJiL6ragb60PDJP3g1yWwcIAqoFOSZ7DGCQZXVt8gUWI 7rw3a66O5nlxOC/CGbD88LBd/I3E6cfhMS30GIc1pvjVFf1spqU6sbg3TTHKh8gs29lV 2GbF3xjABL81pq4UFOtxuzSXC2zCgvdFK25gYRe2fRV3JURCp7C9ba3tmovY0P8+Y1ol sjQGM+6vxThbrCgfkFIAI3i9jX7YcnuOaV8v40BFNY8oanpOxpLSUFzXP/Z0XyEBl7s6 0QFA== X-Forwarded-Encrypted: i=1; AJvYcCWhUgSTU+WwXmjNsCzq894JEgVUP1YHIMSsUf4LwaFmJCBkz68ppC+MPco3PaqTyG+xGZI/2yQPbj6lf7JLaxM7SDxVAe7sIRJwx1KO X-Gm-Message-State: AOJu0YzRAPE8nhBUsziZ/jvJFnY6zFi/m/FdGvNn4utzwB0WAKTSrNGd wN9/dlZn0C30ixwrz0PbIrX7D1Qi19e8ALfnDFZ5t/BSxuGOSJbKlepUour7Ae8ZRKqPwnMknUc AxfJZ3g== X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:c137:aa10:25e1:8f1e]) (user=irogers job=sendgmr) by 2002:a0d:e2c3:0:b0:61a:d420:3b57 with SMTP id l186-20020a0de2c3000000b0061ad4203b57mr112870ywe.0.1714416394201; Mon, 29 Apr 2024 11:46:34 -0700 (PDT) Date: Mon, 29 Apr 2024 11:46:11 -0700 In-Reply-To: <20240429184614.1224041-1-irogers@google.com> Message-Id: <20240429184614.1224041-5-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240429184614.1224041-1-irogers@google.com> X-Mailer: git-send-email 2.44.0.769.g3c40516874-goog Subject: [PATCH v5 4/7] perf dsos: Switch hand code to bsearch From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Kan Liang , James Clark , Athira Rajeev , Colin Ian King , nabijaczleweli@nabijaczleweli.xyz, Leo Yan , Song Liu , Ilkka Koskinen , Ben Gainey , K Prateek Nayak , Yanteng Si , Sun Haiyong , Changbin Du , Andi Kleen , Thomas Richter , Masami Hiramatsu , Dima Kogan , zhaimingbing , Paran Lee , Li Dong , Tiezhu Yang , Yang Jihong , Chengen Du , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" 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.44.0.769.g3c40516874-goog