From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <dev-bounces@dpdk.org>
Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124])
	by inbox.dpdk.org (Postfix) with ESMTP id CE4E3A0C4C;
	Tue, 21 Sep 2021 10:17:17 +0200 (CEST)
Received: from [217.70.189.124] (localhost [127.0.0.1])
	by mails.dpdk.org (Postfix) with ESMTP id 53521410FE;
	Tue, 21 Sep 2021 10:17:06 +0200 (CEST)
Received: from AZHDRRW-EX02.NVIDIA.COM (azhdrrw-ex02.nvidia.com
 [20.64.145.131]) by mails.dpdk.org (Postfix) with ESMTP id AA99D410F8
 for <dev@dpdk.org>; Tue, 21 Sep 2021 10:17:02 +0200 (CEST)
Received: from NAM11-CO1-obe.outbound.protection.outlook.com (104.47.56.171)
 by mxs.oss.nvidia.com (10.13.234.37) with Microsoft SMTP Server
 (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id
 15.2.858.15; Tue, 21 Sep 2021 01:17:01 -0700
ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none;
 b=Z+mSrmpPIHaCCPlLlJcfCfzbqoOzCcwgggKDxvYn25qLrBeAuqyRnhLTzXXm1FLEIFAF3Vz2A4a7M2G51x+KJx23k+aHbciGhJSdRgc1WnGOs5XZ8LzGAVBCtSEF18oc9CQ7N1WnTC5Pc29kEAmbQ6Wmwar4h+cIS8b/Fnw4rBvHfw46b0rdVOdHuKyhKYhK4DPj5V3iXHzMzEgkahcb/+8HUOinn9AMx6Qi1KK7T8Kchk5u3+5NO9Fb5mdbKyWq6E+eMJsnfR4t+VCsos+iPo38n52mYVILrGa4/89NGW6q7BUBg7D09P/1GJ1fYqwQ35d9P78hmUsLSz8Ng60nyA==
ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; 
 s=arcselector9901;
 h=From:Date:Subject:Message-ID:Content-Type:MIME-Version; 
 bh=Zn9i7bfX3Lt4DCmVUPxHH2VbFndJc3jgrRYOpS4F+Ds=;
 b=FwuVZOoaYyG+AF2pEj5EB7CSuH9SKtdxXVtGI47/P2V0K2xxb1FaGDNLPULnXtT/GkMKT9FXGhqsJB+t6bJ3xBVkXRUOciBvzoalgRmwz58z9/E3D7icsZn/MrfYTwIflIgPPtaR290qswwjMyC8HFRrpGRqdpGeVjs9aSS6QRlFsWmQPp9UZjGAta1jdVVPMX1GUcrtGG+0g/3PhkwiCN3/jBCjkjVDgXlTdadtOob5vZYfuFoJYUQqTaFPBk7iL+YSl2H4PbSsJz+3Rfd/ga1yghaujX9KAEOKSsYkQj7bnpvN3KnxrKeNG7MgAFBwWbYBtt9Sre6ecAExKIPaKQ==
ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is
 216.228.112.34) smtp.rcpttodomain=intel.com smtp.mailfrom=nvidia.com;
 dmarc=pass (p=quarantine sp=none pct=100) action=none header.from=nvidia.com; 
 dkim=none (message not signed); arc=none
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com;
 s=selector2;
 h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck;
 bh=Zn9i7bfX3Lt4DCmVUPxHH2VbFndJc3jgrRYOpS4F+Ds=;
 b=HFoivQz/VXl276piKWT6Jln+C2hV22LnlWowgJB6VW7/ufcwNNRs/eL+pt5+GsPQ67GBXXGbaNJ0mEH/GYrZG+6gwQRsQh3+cQJNXrzULiB4vlsjZUGZEjUfkTcMYJ33Op0FFRCX+fNJIfcI1IcFjqkZd09kcelCYDVaZRQawm5KpqfuZzX1VMmiYAKgiToEEkb6SjosF+CnvccM4hdrhgaov2+chlH6vAff5utAQc4uqK/y5ZcHXFKIl6cjl0ZEUS6X2IP3sckgVEDw0LemQIcug83Ibb6MsXh+Bo3FwxmJ63xYyU2QL+mZFnRlf8A3HafpxRSKDMM6fLML82+UkA==
Received: from DM6PR02CA0064.namprd02.prod.outlook.com (2603:10b6:5:177::41)
 by DM6PR12MB3692.namprd12.prod.outlook.com (2603:10b6:5:14a::33) with
 Microsoft SMTP Server (version=TLS1_2,
 cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4523.14; Tue, 21 Sep
 2021 08:16:59 +0000
Received: from DM6NAM11FT054.eop-nam11.prod.protection.outlook.com
 (2603:10b6:5:177:cafe::5) by DM6PR02CA0064.outlook.office365.com
 (2603:10b6:5:177::41) with Microsoft SMTP Server (version=TLS1_2,
 cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4523.14 via Frontend
 Transport; Tue, 21 Sep 2021 08:16:59 +0000
X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.112.34)
 smtp.mailfrom=nvidia.com; intel.com; dkim=none (message not signed)
 header.d=none;intel.com; dmarc=pass action=none header.from=nvidia.com;
Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates
 216.228.112.34 as permitted sender) receiver=protection.outlook.com;
 client-ip=216.228.112.34; helo=mail.nvidia.com;
Received: from mail.nvidia.com (216.228.112.34) by
 DM6NAM11FT054.mail.protection.outlook.com (10.13.173.95) with Microsoft SMTP
 Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id
 15.20.4523.14 via Frontend Transport; Tue, 21 Sep 2021 08:16:59 +0000
Received: from DRHQMAIL107.nvidia.com (10.27.9.16) by HQMAIL107.nvidia.com
 (172.20.187.13) with Microsoft SMTP Server (TLS) id 15.0.1497.18; Tue, 21 Sep
 2021 08:16:58 +0000
Received: from nvidia.com (172.20.187.5) by DRHQMAIL107.nvidia.com
 (10.27.9.16) with Microsoft SMTP Server (TLS) id 15.0.1497.18; Tue, 21 Sep
 2021 08:16:57 +0000
From: <dkozlyuk@oss.nvidia.com>
To: <dev@dpdk.org>
CC: Anatoly Burakov <anatoly.burakov@intel.com>, Dmitry Kozlyuk
 <dkozlyuk@oss.nvidia.com>, Viacheslav Ovsiienko <viacheslavo@oss.nvidia.com>
Date: Tue, 21 Sep 2021 11:16:32 +0300
Message-ID: <20210921081632.858873-4-dkozlyuk@nvidia.com>
X-Mailer: git-send-email 2.25.1
In-Reply-To: <20210921081632.858873-1-dkozlyuk@nvidia.com>
References: <20210920125302.820273-1-dkozlyuk@nvidia.com>
 <20210921081632.858873-1-dkozlyuk@nvidia.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Content-Type: text/plain
X-Originating-IP: [172.20.187.5]
X-ClientProxiedBy: HQMAIL111.nvidia.com (172.20.187.18) To
 DRHQMAIL107.nvidia.com (10.27.9.16)
X-EOPAttributedMessage: 0
X-MS-PublicTrafficType: Email
X-MS-Office365-Filtering-Correlation-Id: 46a8ab5d-cb9a-4fc2-84dc-08d97cd82f1e
X-MS-TrafficTypeDiagnostic: DM6PR12MB3692:
X-Microsoft-Antispam-PRVS: <DM6PR12MB36927DC61F138128D4EB26D5B9A19@DM6PR12MB3692.namprd12.prod.outlook.com>
X-MS-Exchange-Transport-Forked: True
X-MS-Oob-TLC-OOBClassifiers: OLM:1923;
X-MS-Exchange-SenderADCheck: 1
X-MS-Exchange-AntiSpam-Relay: 0
X-Microsoft-Antispam: BCL:0;
X-Microsoft-Antispam-Message-Info: sdCR5REtcVZBH26F5VP9bdyOEUzBF4dD+lDhw0YnITrFg0cNnjfoC+XyQP8cm0HlxYjw7M6Yj0a6vN56/8q+dvaDg23PRQ4gILpDS56RKn5ARtrT8bYA20TZQdmVjWq92S7kndrdPyimq/RsgCiX4bsprR32lcWx2jsLckVqt37fPBYzjJdklzqimXvFavOmecfWFP7f0lCN59oRuysx6bF1oOc3694dNCF6xPKI01ojBEWWvBtSbvTucCpJw9N3bl1lNrVw7tqOO78qBMeXxRQDeZr3VRthmLq+RYcDcMyWHCGuaQaOkz/2txdiYELFjEDBdosPTlD82mcdd0oQnKrfuj3KMNR/gqwoSzDU4nh7X4OyZzBZVp0nK2GjKJbCj1i06Tr8XquRP8qQgXWPAsvRj038xfanyNsfZH2vjXVX1Zk8sZLHFVSnsB/1g6cuRQ9xb078idyjCkNZZ2/mTfSdZ9cAzJH1yd9yFqBuq/KyzqnGb94+iwEk+n72R7yJbUgE3HBUA+R1LmjgAhZdIQ3GK5SLchQqsKCjsDcOkN6U3HU/yCxOGU6ohnfTzhVnX1WA4yqTT6h8U/cP4H10PvSpwgLICD+Jks+BCvzuOj1CcD40VgKGKnOl9jKGA2MMWsNOCJL2Mm2nEJlyDZ3Li8GtDsM/6ye4uAFIJYTYknUhAcknyjaBTEVAIhBRJ75R
X-Forefront-Antispam-Report: CIP:216.228.112.34; CTRY:US; LANG:en; SCL:1; SRV:;
 IPV:NLI; SFV:NSPM; H:mail.nvidia.com; PTR:schybrid03.nvidia.com; CAT:NONE;
 SFS:(4636009)(36840700001)(46966006)(1076003)(2616005)(107886003)(4326008)(186003)(16526019)(316002)(2876002)(2906002)(336012)(36756003)(36906005)(54906003)(7636003)(70206006)(70586007)(82310400003)(6666004)(7696005)(26005)(8676002)(426003)(6916009)(47076005)(508600001)(6286002)(86362001)(36860700001)(55016002)(5660300002)(356005)(8936002);
 DIR:OUT; SFP:1101; 
X-MS-Exchange-CrossTenant-OriginalArrivalTime: 21 Sep 2021 08:16:59.3156 (UTC)
X-MS-Exchange-CrossTenant-Network-Message-Id: 46a8ab5d-cb9a-4fc2-84dc-08d97cd82f1e
X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a
X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a; Ip=[216.228.112.34];
 Helo=[mail.nvidia.com]
X-MS-Exchange-CrossTenant-AuthSource: DM6NAM11FT054.eop-nam11.prod.protection.outlook.com
X-MS-Exchange-CrossTenant-AuthAs: Anonymous
X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem
X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR12MB3692
Subject: [dpdk-dev] [PATCH v5 3/3] app/test: add allocator performance
 autotest
X-BeenThere: dev@dpdk.org
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: DPDK patches and discussions <dev.dpdk.org>
List-Unsubscribe: <https://mails.dpdk.org/options/dev>,
 <mailto:dev-request@dpdk.org?subject=unsubscribe>
List-Archive: <http://mails.dpdk.org/archives/dev/>
List-Post: <mailto:dev@dpdk.org>
List-Help: <mailto:dev-request@dpdk.org?subject=help>
List-Subscribe: <https://mails.dpdk.org/listinfo/dev>,
 <mailto:dev-request@dpdk.org?subject=subscribe>
Errors-To: dev-bounces@dpdk.org
Sender: "dev" <dev-bounces@dpdk.org>

From: Dmitry Kozlyuk <dkozlyuk@nvidia.com>

Memory allocator performance is crucial to applications that deal
with large amount of memory or allocate frequently. DPDK allocator
performance is affected by EAL options, API used and, at least,
allocation size. New autotest is intended to be run with different
EAL options. It measures performance with a range of sizes
for dirrerent APIs: rte_malloc, rte_zmalloc, and rte_memzone_reserve.

Work distribution between allocation and deallocation depends on EAL
options. The test prints both times and total time to ease comparison.

Memory can be filled with zeroes at different points of allocation path,
but it always takes considerable fraction of overall timing. This is why
the test measures filling speed and prints how long clearing would take
for each size as a hint.

Signed-off-by: Dmitry Kozlyuk <dkozlyuk@nvidia.com>
Reviewed-by: Viacheslav Ovsiienko <viacheslavo@nvidia.com>
---
 app/test/meson.build        |   2 +
 app/test/test_malloc_perf.c | 157 ++++++++++++++++++++++++++++++++++++
 2 files changed, 159 insertions(+)
 create mode 100644 app/test/test_malloc_perf.c

diff --git a/app/test/meson.build b/app/test/meson.build
index a7611686ad..a48dc79463 100644
--- a/app/test/meson.build
+++ b/app/test/meson.build
@@ -84,6 +84,7 @@ test_sources = files(
         'test_lpm6_perf.c',
         'test_lpm_perf.c',
         'test_malloc.c',
+        'test_malloc_perf.c',
         'test_mbuf.c',
         'test_member.c',
         'test_member_perf.c',
@@ -281,6 +282,7 @@ fast_tests = [
 
 perf_test_names = [
         'ring_perf_autotest',
+        'malloc_perf_autotest',
         'mempool_perf_autotest',
         'memcpy_perf_autotest',
         'hash_perf_autotest',
diff --git a/app/test/test_malloc_perf.c b/app/test/test_malloc_perf.c
new file mode 100644
index 0000000000..4435894095
--- /dev/null
+++ b/app/test/test_malloc_perf.c
@@ -0,0 +1,157 @@
+#include <inttypes.h>
+#include <string.h>
+#include <rte_cycles.h>
+#include <rte_errno.h>
+#include <rte_malloc.h>
+#include <rte_memzone.h>
+
+#include "test.h"
+
+typedef void * (alloc_t)(const char *name, size_t size, unsigned int align);
+typedef void (free_t)(void *addr);
+
+static const uint64_t KB = 1 << 10;
+static const uint64_t GB = 1 << 30;
+
+static double
+tsc_to_us(uint64_t tsc, size_t runs)
+{
+	return (double)tsc / rte_get_tsc_hz() * US_PER_S / runs;
+}
+
+static int
+test_memset_perf(double *us_per_gb)
+{
+	static const size_t RUNS = 20;
+
+	void *ptr;
+	size_t i;
+	uint64_t tsc;
+
+	puts("Performance: memset");
+
+	ptr = rte_malloc(NULL, GB, 0);
+	if (ptr == NULL) {
+		printf("rte_malloc(size=%"PRIx64") failed\n", GB);
+		return -1;
+	}
+
+	tsc = rte_rdtsc_precise();
+	for (i = 0; i < RUNS; i++)
+		memset(ptr, 0, GB);
+	tsc = rte_rdtsc_precise() - tsc;
+
+	*us_per_gb = tsc_to_us(tsc, RUNS);
+	printf("Result: %f.3 GiB/s <=> %.2f us/MiB\n",
+			US_PER_S / *us_per_gb, *us_per_gb / KB);
+
+	rte_free(ptr);
+	putchar('\n');
+	return 0;
+}
+
+static int
+test_alloc_perf(const char *name, alloc_t *alloc_fn, free_t free_fn,
+		size_t max_runs, double memset_gb_us)
+{
+	static const size_t SIZES[] = {
+			1 << 6, 1 << 7, 1 << 10, 1 << 12, 1 << 16, 1 << 20,
+			1 << 21, 1 << 22, 1 << 24, 1 << 30 };
+
+	size_t i, j;
+	void **ptrs;
+
+	printf("Performance: %s\n", name);
+
+	ptrs = calloc(max_runs, sizeof(ptrs[0]));
+	if (ptrs == NULL) {
+		puts("Cannot allocate memory for pointers");
+		return -1;
+	}
+
+	printf("%12s%8s%12s%12s%12s%12s\n",
+			"Size (B)", "Runs", "Alloc (us)", "Free (us)",
+			"Total (us)", "memset (us)");
+	for (i = 0; i < RTE_DIM(SIZES); i++) {
+		size_t size = SIZES[i];
+		size_t runs_done;
+		uint64_t tsc_start, tsc_alloc, tsc_free;
+		double alloc_time, free_time, memset_time;
+
+		tsc_start = rte_rdtsc_precise();
+		for (j = 0; j < max_runs; j++) {
+			ptrs[j] = alloc_fn(NULL, size, 0);
+			if (ptrs[j] == NULL)
+				break;
+		}
+		tsc_alloc = rte_rdtsc_precise() - tsc_start;
+
+		if (j == 0) {
+			printf("%12zu Interrupted: out of memory.\n", size);
+			break;
+		}
+		runs_done = j;
+
+		tsc_start = rte_rdtsc_precise();
+		for (j = 0; j < runs_done && ptrs[j] != NULL; j++)
+			free_fn(ptrs[j]);
+		tsc_free = rte_rdtsc_precise() - tsc_start;
+
+		alloc_time = tsc_to_us(tsc_alloc, runs_done);
+		free_time = tsc_to_us(tsc_free, runs_done);
+		memset_time = memset_gb_us * size / GB;
+		printf("%12zu%8zu%12.2f%12.2f%12.2f%12.2f\n",
+				size, runs_done, alloc_time, free_time,
+				alloc_time + free_time, memset_time);
+
+		memset(ptrs, 0, max_runs * sizeof(ptrs[0]));
+	}
+
+	free(ptrs);
+	putchar('\n');
+	return 0;
+}
+
+static void *
+memzone_alloc(const char *name __rte_unused, size_t size, unsigned int align)
+{
+	const struct rte_memzone *mz;
+	char gen_name[RTE_MEMZONE_NAMESIZE];
+
+	snprintf(gen_name, sizeof(gen_name), "test-mz-%"PRIx64, rte_rdtsc());
+	mz = rte_memzone_reserve_aligned(gen_name, size, SOCKET_ID_ANY,
+			RTE_MEMZONE_1GB | RTE_MEMZONE_SIZE_HINT_ONLY, align);
+	return (void *)(uintptr_t)mz;
+}
+
+static void
+memzone_free(void *addr)
+{
+	rte_memzone_free((struct rte_memzone *)addr);
+}
+
+static int
+test_malloc_perf(void)
+{
+	static const size_t MAX_RUNS = 10000;
+
+	double memset_gb_us;
+
+	if (test_memset_perf(&memset_gb_us) < 0)
+		return -1;
+
+	if (test_alloc_perf("rte_malloc", rte_malloc, rte_free,
+			MAX_RUNS, memset_gb_us) < 0)
+		return -1;
+	if (test_alloc_perf("rte_zmalloc", rte_zmalloc, rte_free,
+			MAX_RUNS, memset_gb_us) < 0)
+		return -1;
+
+	if (test_alloc_perf("rte_memzone_reserve", memzone_alloc, memzone_free,
+			RTE_MAX_MEMZONE - 1, memset_gb_us) < 0)
+		return -1;
+
+	return 0;
+}
+
+REGISTER_TEST_COMMAND(malloc_perf_autotest, test_malloc_perf);
-- 
2.25.1