Received: by 2002:a05:7412:f690:b0:e2:908c:2ebd with SMTP id ej16csp65883rdb; Wed, 18 Oct 2023 18:42:56 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE2cIXLBYvQtVKR+qjAQOql/eSzOGv0UsNT9k9gd77lS9/6QOKZ7mNCHOPZilowUk6znF49 X-Received: by 2002:a17:903:8a:b0:1c6:b83:4720 with SMTP id o10-20020a170903008a00b001c60b834720mr951487pld.63.1697679776569; Wed, 18 Oct 2023 18:42:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697679776; cv=none; d=google.com; s=arc-20160816; b=pPbZg505cio5CAi0pL2jXeD7qVEzJ06tnjx98RFXWlZSm36GprW392sDx7sfMSnKe/ PtXx9imw1lpS1345aOT9paroPdOtc1pCn7z/3GqOrLElFhaRFlEJFpQ0HtvkBBVME9NK zPv0ySFvR/4HpbeGBfoq/YAn0ZqneL4gVnZEnOYUoBPmVwhW2Y0QU1BkyJiWSHUTXp6Y hM6QY6pkKGTTNrvSJ3jDt4g0vZuBnhULMM7iQGaKCxHz/9+cRvV+r9Olb991XB66QvU9 MtkOr6DWoAa9pjVVyZUaDZAS6L0wgSyRU9o2ydivKV8m9BueBxkT9Ca/I6BXrp/ACLmV uNMA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:message-id:date:references :in-reply-to:subject:cc:to:from:dkim-signature; bh=rcgLsCMh3CD3vRI4ShbvfIDHvwd/kiT0qtTFLhZgPgY=; fh=i9F8xtfNsBTfHQawhEXvdS9gNLVifXthqEQKuknHuRc=; b=LWEfaCv17y5bjGlvIH5Ye3OXma9EPocXbayXKv+J4k2+6/L9OGhchEZYrk9Xe9zeT0 BM/XaMO6c7EDAIpgR4/OXRLEg/1SVcsAF6DF0T0K2RK2nZRf77UrpNjfNwHO/ESn/lO3 kVuja+RtyDCDwm8NW07tTzGlrem1GVIIlNTFCLMQANFdSpsflQEdob637I3gQE1KVHTI nFuNFP8W26gUq7Ev7PMAChFt7ZqEZ3Y7Yb9MAZZJTTHitx5fzdPXtcjrK9eaww8G+u5p t1cOqDZ9gSGsOMXJbkYb5ixQJTJOaT6F48aLXer8Z0s4BsuDfLn++vzQPpEkIdTnUnL4 X4Pg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ellerman.id.au header.s=201909 header.b=bUb3KkuP; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from agentk.vger.email (agentk.vger.email. [23.128.96.32]) by mx.google.com with ESMTPS id u14-20020a170903124e00b001c383a64ebesi1165156plh.319.2023.10.18.18.42.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 18 Oct 2023 18:42:56 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) client-ip=23.128.96.32; Authentication-Results: mx.google.com; dkim=pass header.i=@ellerman.id.au header.s=201909 header.b=bUb3KkuP; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id 0EBF780A5328; Wed, 18 Oct 2023 18:42:50 -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 S231950AbjJSBlu (ORCPT + 99 others); Wed, 18 Oct 2023 21:41:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55840 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229632AbjJSBlu (ORCPT ); Wed, 18 Oct 2023 21:41:50 -0400 Received: from gandalf.ozlabs.org (mail.ozlabs.org [IPv6:2404:9400:2221:ea00::3]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4E7DEEA for ; Wed, 18 Oct 2023 18:41:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ellerman.id.au; s=201909; t=1697679705; bh=rcgLsCMh3CD3vRI4ShbvfIDHvwd/kiT0qtTFLhZgPgY=; h=From:To:Cc:Subject:In-Reply-To:References:Date:From; b=bUb3KkuPpi1w+6+aq6gMa4pad4axqkmy7b3TcZrAQeoelxzeuobzdLKs8jODqa/HE PEAJJz8wVJxYiqyPq7q0EDiOmK3jOTrj14oMmGIdHC+zJpKLXwFADJ/HdIgN6hoPtX 22eBVEsFmCYZp13Yi+nK9ND4Ni2b0gwrTTATN+9I+6rcHUOFec3gUDrha9UYnZ8xl6 vxW88nBY3n37C/yASAptzW1BMG/JS2Dps85e6kHzGlmEZZsjbe43hgBxoBtOHr7eov 7+A/Wuh5nwKxq0hk++6TvmqjWoV5zYlSBNIJs1FyRoKR/U5xUILGo3a1v3ZSn7XcVK VSp4QjJkbkwIw== Received: from authenticated.ozlabs.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mail.ozlabs.org (Postfix) with ESMTPSA id 4S9r6d422Jz4x1w; Thu, 19 Oct 2023 12:41:45 +1100 (AEDT) From: Michael Ellerman To: Kuan-Wei Chiu Cc: npiggin@gmail.com, christophe.leroy@csgroup.eu, linuxppc-dev@lists.ozlabs.org, linux-kernel@vger.kernel.org, Kuan-Wei Chiu Subject: Re: [PATCH] powerpc/perf: Optimize find_alternatives_list() using binary search In-Reply-To: <20231013175714.2142775-1-visitorckw@gmail.com> References: <20231013175714.2142775-1-visitorckw@gmail.com> Date: Thu, 19 Oct 2023 12:41:45 +1100 Message-ID: <871qdr75ie.fsf@mail.lhotse> MIME-Version: 1.0 Content-Type: text/plain X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS 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]); Wed, 18 Oct 2023 18:42:50 -0700 (PDT) Kuan-Wei Chiu writes: > This patch improves the performance of event alternative lookup by > replacing the previous linear search with a more efficient binary > search. This change reduces the time complexity for the search process > from O(n) to O(log(n)). A pre-sorted table of event values and their > corresponding indices has been introduced to expedite the search > process. Thanks for the patch. How did you test this? I assume you don't have a Power6 machine lying around? :) cheers > diff --git a/arch/powerpc/perf/power6-pmu.c b/arch/powerpc/perf/power6-pmu.c > index 5729b6e059de..b6030ea130eb 100644 > --- a/arch/powerpc/perf/power6-pmu.c > +++ b/arch/powerpc/perf/power6-pmu.c > @@ -335,25 +335,34 @@ static const unsigned int event_alternatives[][MAX_ALT] = { > { 0x3000fe, 0x400056 }, /* PM_DATA_FROM_L3MISS */ > }; > > -/* > - * This could be made more efficient with a binary search on > - * a presorted list, if necessary > - */ > static int find_alternatives_list(u64 event) > { > - int i, j; > - unsigned int alt; > - > - for (i = 0; i < ARRAY_SIZE(event_alternatives); ++i) { > - if (event < event_alternatives[i][0]) > - return -1; > - for (j = 0; j < MAX_ALT; ++j) { > - alt = event_alternatives[i][j]; > - if (!alt || event < alt) > - break; > - if (event == alt) > - return i; > - } > + const unsigned int presort_event_table[] = { > + 0x0130e8, 0x080080, 0x080088, 0x10000a, 0x10000b, 0x10000d, 0x10000e, > + 0x100010, 0x10001a, 0x100026, 0x100054, 0x100056, 0x1000f0, 0x1000f8, > + 0x1000fc, 0x200008, 0x20000e, 0x200010, 0x200012, 0x200054, 0x2000f0, > + 0x2000f2, 0x2000f4, 0x2000f5, 0x2000f6, 0x2000f8, 0x2000fc, 0x2000fe, > + 0x2d0030, 0x30000a, 0x30000c, 0x300010, 0x300012, 0x30001a, 0x300056, > + 0x3000f0, 0x3000f2, 0x3000f6, 0x3000f8, 0x3000fc, 0x3000fe, 0x400006, > + 0x400007, 0x40000a, 0x40000e, 0x400010, 0x400018, 0x400056, 0x4000f0, > + 0x4000f8, 0x600005}; > + const unsigned int event_index_table[] = { > + 0, 1, 2, 3, 4, 1, 5, 6, 7, 8, 9, 10, 11, 12, 13, 12, 14, > + 7, 15, 2, 9, 16, 3, 4, 0, 17, 10, 18, 19, 20, 1, 17, 15, 19, > + 18, 2, 16, 21, 8, 0, 22, 13, 14, 11, 21, 5, 20, 22, 1, 6, 3}; > + int lo = 0; > + int hi = ARRAY_SIZE(presort_event_table) - 1; > + > + while (lo <= hi) { > + int mid = lo + (hi - lo) / 2; > + unsigned int alt = presort_event_table[mid]; > + > + if (alt < event) > + lo = mid + 1; > + else if (alt > event) > + hi = mid - 1; > + else > + return event_index_table[mid]; > } > return -1; > } > -- > 2.25.1