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 4B72546053;
	Fri, 17 Jan 2025 15:41:20 +0100 (CET)
Received: from mails.dpdk.org (localhost [127.0.0.1])
	by mails.dpdk.org (Postfix) with ESMTP id D8E93402B1;
	Fri, 17 Jan 2025 15:41:19 +0100 (CET)
Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.7])
 by mails.dpdk.org (Postfix) with ESMTP id 6DFD940294;
 Fri, 17 Jan 2025 15:41:18 +0100 (CET)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple;
 d=intel.com; i=@intel.com; q=dns/txt; s=Intel;
 t=1737124878; x=1768660878;
 h=from:to:cc:subject:date:message-id:in-reply-to:
 references:mime-version:content-transfer-encoding;
 bh=ouwXoA8txzgsnGt9Vae+7+bruw429Mb2b9E/PDyX/j4=;
 b=jQaVNAzJ36FPsWtdWe/DXe5Qg71v4si0lkwrqs325R1QbV5WGF29OoWj
 Qd8dtEGUZKp2DPoBXgBtZEsPKE2oKnWR4h63tyfqTxJb3W43+M0k8sOUa
 M67lWEPqSImuDQ+iZKTGBIog4R5rVH4XUGXpo7UrTyB63luyxrTulWF3a
 vTtoQW2whwqAWMVd+EYTDal4UYDWp9M1+B3aVsFNoCxCAW05475PerN9g
 ULH/aNwuIcNu0EyYPdRPQ4LmyavPL6q2ii+6OyVMSV1MSRHj7HkLg5MjH
 yL2qTkOwP6VZgFficyvogRgGeLaVCH+gyRmByhWGVw17EgV7WLsTUcB/u Q==;
X-CSE-ConnectionGUID: UFDF+ieoSkm5M427SpX9tQ==
X-CSE-MsgGUID: vBqUNBGuQ9C7yUr7VnrFtg==
X-IronPort-AV: E=McAfee;i="6700,10204,11318"; a="62931726"
X-IronPort-AV: E=Sophos;i="6.13,212,1732608000"; d="scan'208";a="62931726"
Received: from fmviesa005.fm.intel.com ([10.60.135.145])
 by fmvoesa101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384;
 17 Jan 2025 06:41:17 -0800
X-CSE-ConnectionGUID: u3Tg68+tTWSZSjS4lBO71A==
X-CSE-MsgGUID: tTZ4t10iRCqom+s0JkP0rA==
X-ExtLoop1: 1
X-IronPort-AV: E=Sophos;i="6.12,224,1728975600"; d="scan'208";a="110454270"
Received: from silpixa00401197coob.ir.intel.com (HELO
 silpixa00401385.ir.intel.com) ([10.237.214.45])
 by fmviesa005.fm.intel.com with ESMTP; 17 Jan 2025 06:41:16 -0800
From: Bruce Richardson <bruce.richardson@intel.com>
To: dev@dpdk.org
Cc: Bruce Richardson <bruce.richardson@intel.com>,
	stable@dpdk.org
Subject: [PATCH v3] test: improve resiliency of malloc autotest
Date: Fri, 17 Jan 2025 14:40:40 +0000
Message-ID: <20250117144112.2544963-1-bruce.richardson@intel.com>
X-Mailer: git-send-email 2.43.0
In-Reply-To: <20250117125912.985475-1-bruce.richardson@intel.com>
References: <20250117125912.985475-1-bruce.richardson@intel.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
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

The test case "test_multi_alloc_statistics" was brittle in that it did
some allocations and frees and then checked statistics without
considering the initial state of the malloc heaps. This meant that,
depending on what allocations/frees were done beforehand, the test can
sometimes fail.

We can improve resiliency by running the test using a new malloc heap,
which means it is unaffected by any previous allocations.

Bugzilla ID: 1579
Fixes: a40a1f8231b4 ("app: various tests update")
Cc: stable@dpdk.org

Signed-off-by: Bruce Richardson <bruce.richardson@intel.com>
---
v3:
* switched allocation from mmap to malloc allowing it work on windows
* use explicit alignment of the malloc return value to ensure memory
  added to heap is page-aligned.

v2:
* removed unnecessary extra include
* only added new code for non-windows, since using mmap for allocation.
---
 app/test/test_malloc.c | 35 +++++++++++++++++++++++++++++++++++
 1 file changed, 35 insertions(+)

diff --git a/app/test/test_malloc.c b/app/test/test_malloc.c
index 02a7d8ef20..9e73c0da09 100644
--- a/app/test/test_malloc.c
+++ b/app/test/test_malloc.c
@@ -25,6 +25,7 @@
 #include <rte_malloc.h>
 #include <rte_cycles.h>
 #include <rte_random.h>
+#include <rte_eal_paging.h>
 #include <rte_string_fns.h>
 
 #define N 10000
@@ -272,6 +273,34 @@ test_multi_alloc_statistics(void)
 	size_t size = 2048;
 	int align = 1024;
 	int overhead = 0;
+	const size_t pgsz = rte_mem_page_size();
+	const size_t heap_size = (1 << 21);
+
+	if (pgsz < heap_size) {
+		printf("Page size is smaller than heap size\n");
+		return TEST_SKIPPED;
+	}
+
+	if (rte_malloc_heap_create(__func__) != 0) {
+		printf("Failed to create test malloc heap\n");
+		return -1;
+	}
+	/* Allocate some memory using malloc and add it to our test heap. */
+	void *unaligned_memory = malloc(heap_size + pgsz);
+	if (unaligned_memory == NULL) {
+		printf("Failed to allocate memory\n");
+		return -1;
+	}
+	void *memory = RTE_PTR_ALIGN(unaligned_memory, pgsz);
+	if (rte_malloc_heap_memory_add(__func__, memory, heap_size, NULL, 1, heap_size) != 0) {
+		printf("Failed to add memory to heap\n");
+		return -1;
+	}
+	socket = rte_malloc_heap_get_socket(__func__);
+	if (socket < 0) {
+		printf("Failed to get socket for test malloc heap.\n");
+		return -1;
+	}
 
 	/* Dynamically calculate the overhead by allocating one cacheline and
 	 * then comparing what was allocated from the heap.
@@ -371,6 +400,12 @@ test_multi_alloc_statistics(void)
 		printf("Malloc statistics are incorrect - freed alloc\n");
 		return -1;
 	}
+
+	/* cleanup */
+	rte_malloc_heap_memory_remove(__func__, memory, heap_size);
+	rte_malloc_heap_destroy(__func__);
+	free(unaligned_memory);
+
 	return 0;
 }
 
-- 
2.43.0