From patchwork Thu Jul 7 18:34:49 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Mattias_R=C3=B6nnblom?= X-Patchwork-Id: 113790 X-Patchwork-Delegate: thomas@monjalon.net Return-Path: X-Original-To: patchwork@inbox.dpdk.org Delivered-To: patchwork@inbox.dpdk.org Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 29D0DA0543; Thu, 7 Jul 2022 20:35:11 +0200 (CEST) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id F18ED40A7B; Thu, 7 Jul 2022 20:35:10 +0200 (CEST) Received: from mail.lysator.liu.se (mail.lysator.liu.se [130.236.254.3]) by mails.dpdk.org (Postfix) with ESMTP id 84C704069D; Thu, 7 Jul 2022 20:35:10 +0200 (CEST) Received: from mail.lysator.liu.se (localhost [127.0.0.1]) by mail.lysator.liu.se (Postfix) with ESMTP id 4527E11442; Thu, 7 Jul 2022 20:35:10 +0200 (CEST) Received: by mail.lysator.liu.se (Postfix, from userid 1004) id 43D45112BC; Thu, 7 Jul 2022 20:35:10 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on hermod.lysator.liu.se X-Spam-Level: X-Spam-Status: No, score=-1.8 required=5.0 tests=ALL_TRUSTED,AWL, T_SCC_BODY_TEXT_LINE autolearn=disabled version=3.4.6 X-Spam-Score: -1.8 Received: from isengard.friendlyfire.se (unknown [62.63.215.114]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mail.lysator.liu.se (Postfix) with ESMTPSA id 7A38C10F6E; Thu, 7 Jul 2022 20:35:06 +0200 (CEST) From: =?utf-8?q?Mattias_R=C3=B6nnblom?= To: olivier.matz@6wind.com Cc: Emil Berg , bruce.richardson@intel.com, stephen@networkplumber.org, stable@dpdk.org, bugzilla@dpdk.org, dev@dpdk.org, onar.olsen@ericsson.com, =?utf-8?q?Morten_Br=C3=B8rup?= , =?utf-8?q?Mattia?= =?utf-8?q?s_R=C3=B6nnblom?= Subject: [PATCH 1/2] app/test: add cksum performance test Date: Thu, 7 Jul 2022 20:34:49 +0200 Message-Id: <20220707183450.3203361-1-hofors@lysator.liu.se> X-Mailer: git-send-email 2.25.1 In-Reply-To: <98CBD80474FA8B44BF855DF32C47DC35D87189@smartserver.smartshare.dk> References: <98CBD80474FA8B44BF855DF32C47DC35D87189@smartserver.smartshare.dk> MIME-Version: 1.0 X-Virus-Scanned: ClamAV using ClamSMTP X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org From: Mattias Rönnblom Add performance test for the rte_raw_cksum() function, which delegates the actual work to __rte_raw_cksum(), which in turn is used by other functions in need of Internet checksum calculation. Signed-off-by: Mattias Rönnblom --- MAINTAINERS | 1 + app/test/meson.build | 1 + app/test/test_cksum_perf.c | 118 +++++++++++++++++++++++++++++++++++++ 3 files changed, 120 insertions(+) create mode 100644 app/test/test_cksum_perf.c diff --git a/MAINTAINERS b/MAINTAINERS index c923712946..2a4c99e05a 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -1414,6 +1414,7 @@ Network headers M: Olivier Matz F: lib/net/ F: app/test/test_cksum.c +F: app/test/test_cksum_perf.c Packet CRC M: Jasvinder Singh diff --git a/app/test/meson.build b/app/test/meson.build index 431c5bd318..191db03d1d 100644 --- a/app/test/meson.build +++ b/app/test/meson.build @@ -18,6 +18,7 @@ test_sources = files( 'test_bpf.c', 'test_byteorder.c', 'test_cksum.c', + 'test_cksum_perf.c', 'test_cmdline.c', 'test_cmdline_cirbuf.c', 'test_cmdline_etheraddr.c', diff --git a/app/test/test_cksum_perf.c b/app/test/test_cksum_perf.c new file mode 100644 index 0000000000..d27e7f893a --- /dev/null +++ b/app/test/test_cksum_perf.c @@ -0,0 +1,118 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * Copyright(c) 2022 Ericsson AB + */ + +#include + +#include +#include +#include +#include +#include + +#include "test.h" + +#define NUM_BLOCKS (10) +#define ITERATIONS (1000000) + +static const size_t data_sizes[] = { 20, 21, 100, 101, 1500, 1501 }; + +static __rte_noinline uint16_t +do_rte_raw_cksum(const void *buf, size_t len) +{ + return rte_raw_cksum(buf, len); +} + +static void +init_block(void *buf, size_t len) +{ + size_t i; + + for (i = 0; i < len; i++) + ((char *)buf)[i] = (uint8_t)rte_rand(); +} + +static int +test_cksum_perf_size_alignment(size_t block_size, bool aligned) +{ + char *data[NUM_BLOCKS]; + char *blocks[NUM_BLOCKS]; + unsigned int i; + uint64_t start; + uint64_t end; + /* Floating point to handle low (pseudo-)TSC frequencies */ + double block_latency; + double byte_latency; + volatile uint64_t sum = 0; + + for (i = 0; i < NUM_BLOCKS; i++) { + data[i] = rte_malloc(NULL, block_size + 1, 0); + + if (data[i] == NULL) { + printf("Failed to allocate memory for block\n"); + return TEST_FAILED; + } + + init_block(data[i], block_size + 1); + + blocks[i] = aligned ? data[i] : data[i] + 1; + } + + start = rte_rdtsc(); + + for (i = 0; i < ITERATIONS; i++) { + unsigned int j; + for (j = 0; j < NUM_BLOCKS; j++) + sum += do_rte_raw_cksum(blocks[j], block_size); + } + + end = rte_rdtsc(); + + block_latency = (end - start) / (double)(ITERATIONS * NUM_BLOCKS); + byte_latency = block_latency / block_size; + + printf("%-9s %10zd %19.1f %16.2f\n", aligned ? "Aligned" : "Unaligned", + block_size, block_latency, byte_latency); + + for (i = 0; i < NUM_BLOCKS; i++) + rte_free(data[i]); + + return TEST_SUCCESS; +} + +static int +test_cksum_perf_size(size_t block_size) +{ + int rc; + + rc = test_cksum_perf_size_alignment(block_size, true); + if (rc != TEST_SUCCESS) + return rc; + + rc = test_cksum_perf_size_alignment(block_size, false); + + return rc; +} + +static int +test_cksum_perf(void) +{ + uint16_t i; + + printf("### rte_raw_cksum() performance ###\n"); + printf("Alignment Block size TSC cycles/block TSC cycles/byte\n"); + + for (i = 0; i < RTE_DIM(data_sizes); i++) { + int rc; + + rc = test_cksum_perf_size(data_sizes[i]); + if (rc != TEST_SUCCESS) + return rc; + } + + return TEST_SUCCESS; +} + + +REGISTER_TEST_COMMAND(cksum_perf_autotest, test_cksum_perf); + From patchwork Thu Jul 7 18:34:50 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Mattias_R=C3=B6nnblom?= X-Patchwork-Id: 113791 X-Patchwork-Delegate: thomas@monjalon.net Return-Path: X-Original-To: patchwork@inbox.dpdk.org Delivered-To: patchwork@inbox.dpdk.org Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 45A5FA0543; Thu, 7 Jul 2022 20:35:17 +0200 (CEST) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 097DD42836; Thu, 7 Jul 2022 20:35:12 +0200 (CEST) Received: from mail.lysator.liu.se (mail.lysator.liu.se [130.236.254.3]) by mails.dpdk.org (Postfix) with ESMTP id AC21840A7B; Thu, 7 Jul 2022 20:35:10 +0200 (CEST) Received: from mail.lysator.liu.se (localhost [127.0.0.1]) by mail.lysator.liu.se (Postfix) with ESMTP id 90CC4113BA; Thu, 7 Jul 2022 20:35:10 +0200 (CEST) Received: by mail.lysator.liu.se (Postfix, from userid 1004) id 8F855112BF; Thu, 7 Jul 2022 20:35:10 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on hermod.lysator.liu.se X-Spam-Level: X-Spam-Status: No, score=-1.8 required=5.0 tests=ALL_TRUSTED,AWL, T_SCC_BODY_TEXT_LINE autolearn=disabled version=3.4.6 X-Spam-Score: -1.8 Received: from isengard.friendlyfire.se (unknown [62.63.215.114]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mail.lysator.liu.se (Postfix) with ESMTPSA id 3C3F811345; Thu, 7 Jul 2022 20:35:09 +0200 (CEST) From: =?utf-8?q?Mattias_R=C3=B6nnblom?= To: olivier.matz@6wind.com Cc: Emil Berg , bruce.richardson@intel.com, stephen@networkplumber.org, stable@dpdk.org, bugzilla@dpdk.org, dev@dpdk.org, onar.olsen@ericsson.com, =?utf-8?q?Morten_Br=C3=B8rup?= , =?utf-8?q?Mattia?= =?utf-8?q?s_R=C3=B6nnblom?= Subject: [PATCH 2/2] net: have checksum routines accept unaligned data Date: Thu, 7 Jul 2022 20:34:50 +0200 Message-Id: <20220707183450.3203361-2-hofors@lysator.liu.se> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220707183450.3203361-1-hofors@lysator.liu.se> References: <98CBD80474FA8B44BF855DF32C47DC35D87189@smartserver.smartshare.dk> <20220707183450.3203361-1-hofors@lysator.liu.se> MIME-Version: 1.0 X-Virus-Scanned: ClamAV using ClamSMTP X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org From: Mattias Rönnblom __rte_raw_cksum() (used by rte_raw_cksum() among others) accessed its data through an uint16_t pointer, which allowed the compiler to assume the data was 16-bit aligned. This in turn would, with certain architectures and compiler flag combinations, result in code with SIMD load or store instructions with restrictions on data alignment. This patch keeps the old algorithm, but data is read using memcpy() instead of direct pointer access, forcing the compiler to always generate code that handles unaligned input. The __may_alias__ GCC attribute is no longer needed. The data on which the Internet checksum functions operates are almost always 16-bit aligned, but there are exceptions. In particular, the PDCP protocol header may (literally) have an odd size. Performance impact seems to range from none to a very slight regression. Bugzilla ID: 1035 Cc: stable@dpdk.org Signed-off-by: Mattias Rönnblom Reviewed-by: Morten Brørup --- lib/net/rte_ip.h | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/lib/net/rte_ip.h b/lib/net/rte_ip.h index b502481670..a9e6251f14 100644 --- a/lib/net/rte_ip.h +++ b/lib/net/rte_ip.h @@ -160,18 +160,23 @@ rte_ipv4_hdr_len(const struct rte_ipv4_hdr *ipv4_hdr) static inline uint32_t __rte_raw_cksum(const void *buf, size_t len, uint32_t sum) { - /* extend strict-aliasing rules */ - typedef uint16_t __attribute__((__may_alias__)) u16_p; - const u16_p *u16_buf = (const u16_p *)buf; - const u16_p *end = u16_buf + len / sizeof(*u16_buf); + const void *end; - for (; u16_buf != end; ++u16_buf) - sum += *u16_buf; + for (end = RTE_PTR_ADD(buf, (len/sizeof(uint16_t)) * sizeof(uint16_t)); + buf != end; buf = RTE_PTR_ADD(buf, sizeof(uint16_t))) { + uint16_t v; + + memcpy(&v, buf, sizeof(uint16_t)); + sum += v; + } /* if length is odd, keeping it byte order independent */ if (unlikely(len % 2)) { + uint8_t last; uint16_t left = 0; - *(unsigned char *)&left = *(const unsigned char *)end; + + memcpy(&last, end, 1); + *(unsigned char *)&left = last; sum += left; }