From patchwork Fri May 25 04:07:35 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Guo X-Patchwork-Id: 920219 Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Received: from lists.ozlabs.org (lists.ozlabs.org [203.11.71.2]) (using TLSv1.2 with cipher ADH-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 40sXyY3T8Xz9s0q for ; Fri, 25 May 2018 14:17:09 +1000 (AEST) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="SUfSo779"; dkim-atps=neutral Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) by lists.ozlabs.org (Postfix) with ESMTP id 40sXyY1tyZzDrSt for ; Fri, 25 May 2018 14:17:09 +1000 (AEST) Authentication-Results: lists.ozlabs.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: lists.ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="SUfSo779"; dkim-atps=neutral X-Original-To: linuxppc-dev@lists.ozlabs.org Delivered-To: linuxppc-dev@lists.ozlabs.org Authentication-Results: lists.ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=gmail.com (client-ip=2607:f8b0:400e:c00::242; helo=mail-pf0-x242.google.com; envelope-from=wei.guo.simon@gmail.com; receiver=) Authentication-Results: lists.ozlabs.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: lists.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="SUfSo779"; dkim-atps=neutral Received: from mail-pf0-x242.google.com (mail-pf0-x242.google.com [IPv6:2607:f8b0:400e:c00::242]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 40sXlz2QlXzF1fr for ; Fri, 25 May 2018 14:07:59 +1000 (AEST) Received: by mail-pf0-x242.google.com with SMTP id p14-v6so1912699pfh.9 for ; Thu, 24 May 2018 21:07:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=AVVqYrThuz3x+jIG0sdhvrdZ5GaRac0Au+ixoQuSyHE=; b=SUfSo779YJ4Ma00lglDxF4Mg5lkvpJJB5AuG1/jwzJcODthapz8ubdelJH1uRSGzDH stTusZAd9chys/dQruo0DYDtu60ZAzICGJnUdaZvYCEsvGZgQX/BA0ycDjstJm4Z5TVi BkwTWlVBPVg7Hk+od+zZjLVRGGFSCUm/mEy6fkYMka5Vhh3y92H6WO1Xaf9XnJFQUFwC O1zFDYiCaWPUlhHkaK6D5tzVHBk5zNE76QDRZroqjhZZXpquk1xJN9qCrpcTSR/RStDs aDo3dzJuY3GIrZxNJIdyUr7/vNi9XybO9jsiExxH61kF7/mWTmCx59J9r4+1pOhJxacS mOsQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=AVVqYrThuz3x+jIG0sdhvrdZ5GaRac0Au+ixoQuSyHE=; b=UTFn4h4hmic1m7jThOGXxJN9wkTMOlF12yz1emw/wLkVcVLvyjqhymjnRZF4ZR4niF 4G8/MXL60Eq4+OJXHn3glxloNBju7qdR/QKcjKtAIG0YMLAGMsZGPdpuEuQQz72efmzk z5KkAtvloQahZEax5jBBEnS4kDxkSyk5Jhrlzlr9xzeAX1OCeeIWZyqCrNWKgiRDxbNP vY3f3fN/MqUNhchagZU2uUa3mBZE8JWTalync/hqKAlSqjDUSn3wosKwfp2O1AyTSClE NXpsQA4Isavm47G06+0evuxbMuW5PUi9LKmmaGEz3KjEj3TOLyOsJ/NB6e0O0DAYnWfR akKQ== X-Gm-Message-State: ALKqPwefOt0zz7IbEKqZisSIPTSit3UGo7HNkujIuSOPdHXtC5fZ1M83 HU5vvFa0IrMm3FTYSwyIl7mOTA== X-Google-Smtp-Source: AB8JxZo9DnZGrhlQ9rMLTh06Oo6gcNqgBBlN71hk3YbKhw+iXHLKJrVeHjt7W0ZomvZjBIfVTpFQQQ== X-Received: by 2002:a63:b307:: with SMTP id i7-v6mr659754pgf.448.1527221277345; Thu, 24 May 2018 21:07:57 -0700 (PDT) Received: from simonLocalRHEL7.cn.ibm.com ([112.73.0.89]) by smtp.gmail.com with ESMTPSA id b13-v6sm30138808pgu.54.2018.05.24.21.07.54 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 24 May 2018 21:07:56 -0700 (PDT) From: wei.guo.simon@gmail.com To: linuxppc-dev@lists.ozlabs.org Subject: [PATCH v6 3/4] powerpc/64: add 32 bytes prechecking before using VMX optimization on memcmp() Date: Fri, 25 May 2018 12:07:35 +0800 Message-Id: <1527221256-17029-4-git-send-email-wei.guo.simon@gmail.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1527221256-17029-1-git-send-email-wei.guo.simon@gmail.com> References: <1527221256-17029-1-git-send-email-wei.guo.simon@gmail.com> X-BeenThere: linuxppc-dev@lists.ozlabs.org X-Mailman-Version: 2.1.26 Precedence: list List-Id: Linux on PowerPC Developers Mail List List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: "Naveen N. Rao" , Simon Guo , Cyril Bur Errors-To: linuxppc-dev-bounces+patchwork-incoming=ozlabs.org@lists.ozlabs.org Sender: "Linuxppc-dev" From: Simon Guo This patch is based on the previous VMX patch on memcmp(). To optimize ppc64 memcmp() with VMX instruction, we need to think about the VMX penalty brought with: If kernel uses VMX instruction, it needs to save/restore current thread's VMX registers. There are 32 x 128 bits VMX registers in PPC, which means 32 x 16 = 512 bytes for load and store. The major concern regarding the memcmp() performance in kernel is KSM, who will use memcmp() frequently to merge identical pages. So it will make sense to take some measures/enhancement on KSM to see whether any improvement can be done here. Cyril Bur indicates that the memcmp() for KSM has a higher possibility to fail (unmatch) early in previous bytes in following mail. https://patchwork.ozlabs.org/patch/817322/#1773629 And I am taking a follow-up on this with this patch. Per some testing, it shows KSM memcmp() will fail early at previous 32 bytes. More specifically: - 76% cases will fail/unmatch before 16 bytes; - 83% cases will fail/unmatch before 32 bytes; - 84% cases will fail/unmatch before 64 bytes; So 32 bytes looks a better choice than other bytes for pre-checking. The early failure is also true for memcmp() for non-KSM case. With a non-typical call load, it shows ~73% cases fail before first 32 bytes. This patch adds a 32 bytes pre-checking firstly before jumping into VMX operations, to avoid the unnecessary VMX penalty. It is not limited to KSM case. And the testing shows ~20% improvement on memcmp() average execution time with this patch. And note the 32B pre-checking is only performed when the compare size is long enough (>=4K currently) to allow VMX operation. The detail data and analysis is at: https://github.com/justdoitqd/publicFiles/blob/master/memcmp/README.md Signed-off-by: Simon Guo --- arch/powerpc/lib/memcmp_64.S | 50 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 42 insertions(+), 8 deletions(-) diff --git a/arch/powerpc/lib/memcmp_64.S b/arch/powerpc/lib/memcmp_64.S index 4ba7bb6..96eb08b 100644 --- a/arch/powerpc/lib/memcmp_64.S +++ b/arch/powerpc/lib/memcmp_64.S @@ -403,8 +403,27 @@ _GLOBAL(memcmp) #ifdef CONFIG_ALTIVEC .Lsameoffset_vmx_cmp: /* Enter with src/dst addrs has the same offset with 8 bytes - * align boundary + * align boundary. + * + * There is an optimization based on following fact: memcmp() + * prones to fail early at the first 32 bytes. + * Before applying VMX instructions which will lead to 32x128bits + * VMX regs load/restore penalty, we compare the first 32 bytes + * so that we can catch the ~80% fail cases. */ + + li r0,4 + mtctr r0 +.Lsameoffset_prechk_32B_loop: + LD rA,0,r3 + LD rB,0,r4 + cmpld cr0,rA,rB + addi r3,r3,8 + addi r4,r4,8 + bne cr0,.LcmpAB_lightweight + addi r5,r5,-8 + bdnz .Lsameoffset_prechk_32B_loop + ENTER_VMX_OPS beq cr1,.Llong_novmx_cmp @@ -481,13 +500,6 @@ _GLOBAL(memcmp) #endif .Ldiffoffset_8bytes_make_align_start: -#ifdef CONFIG_ALTIVEC - /* only do vmx ops when the size equal or greater than 4K bytes */ - cmpdi cr5,r5,VMX_THRESH - bge cr5,.Ldiffoffset_vmx_cmp -.Ldiffoffset_novmx_cmp: -#endif - /* now try to align s1 with 8 bytes */ andi. r6,r3,0x7 rlwinm r6,r6,3,0,28 @@ -512,6 +524,13 @@ _GLOBAL(memcmp) .Ldiffoffset_align_s1_8bytes: /* now s1 is aligned with 8 bytes. */ +#ifdef CONFIG_ALTIVEC + /* only do vmx ops when the size is equal or greater than 4K bytes */ + cmpdi cr5,r5,VMX_THRESH + bge cr5,.Ldiffoffset_vmx_cmp +.Ldiffoffset_novmx_cmp: +#endif + cmpdi cr5,r5,31 ble cr5,.Lcmp_lt32bytes @@ -523,6 +542,21 @@ _GLOBAL(memcmp) #ifdef CONFIG_ALTIVEC .Ldiffoffset_vmx_cmp: + /* perform a 32 bytes pre-checking before + * enable VMX operations. + */ + li r0,4 + mtctr r0 +.Ldiffoffset_prechk_32B_loop: + LD rA,0,r3 + LD rB,0,r4 + cmpld cr0,rA,rB + addi r3,r3,8 + addi r4,r4,8 + bne cr0,.LcmpAB_lightweight + addi r5,r5,-8 + bdnz .Ldiffoffset_prechk_32B_loop + ENTER_VMX_OPS beq cr1,.Ldiffoffset_novmx_cmp