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 37AAEA034F;
	Wed, 10 Nov 2021 04:02:33 +0100 (CET)
Received: from [217.70.189.124] (localhost [127.0.0.1])
	by mails.dpdk.org (Postfix) with ESMTP id DDE8141148;
	Wed, 10 Nov 2021 04:01:55 +0100 (CET)
Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182])
 by mails.dpdk.org (Postfix) with ESMTP id AA23640142
 for <dev@dpdk.org>; Wed, 10 Nov 2021 04:01:47 +0100 (CET)
Received: by linux.microsoft.com (Postfix, from userid 1059)
 id 61AC120C3556; Tue,  9 Nov 2021 19:01:46 -0800 (PST)
DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 61AC120C3556
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com;
 s=default; t=1636513306;
 bh=WiHz9g7vxCXQttKim+p6bypEuc/qJO21dnZH0uiYMAE=;
 h=From:To:Cc:Subject:Date:In-Reply-To:References:From;
 b=OiKzorPK+1IsaxFYC6GbZD5Zr5rI2aMOaEoH9tpsbgqn+PLtL0cjQzRt78duCCZOh
 ATcacEQZFCE90b5qeHbmUqrt5N0BCznDtRU06Hwc63eFeBUa2pGR8odWpi4zOMtfP5
 lt6iIXrt5aOT/NC1APsg3+Qe3nJ/ZzRLNrFrd9qs=
From: Narcisa Ana Maria Vasile <navasile@linux.microsoft.com>
To: dev@dpdk.org, thomas@monjalon.net, dmitry.kozliuk@gmail.com,
 khot@microsoft.com, navasile@microsoft.com, dmitrym@microsoft.com,
 roretzla@microsoft.com, talshn@nvidia.com, ocardona@microsoft.com
Cc: bruce.richardson@intel.com, david.marchand@redhat.com,
 pallavi.kadam@intel.com
Date: Tue,  9 Nov 2021 19:01:37 -0800
Message-Id: <1636513302-7359-9-git-send-email-navasile@linux.microsoft.com>
X-Mailer: git-send-email 1.8.3.1
In-Reply-To: <1636513302-7359-1-git-send-email-navasile@linux.microsoft.com>
References: <1633765318-28356-1-git-send-email-navasile@linux.microsoft.com>
 <1636513302-7359-1-git-send-email-navasile@linux.microsoft.com>
Subject: [dpdk-dev] [PATCH v17 08/13] app/test: add unit tests for thread
 attributes
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: Narcisa Vasile <navasile@microsoft.com>

As a new API for threading is introduced,
a set of unit tests have been added to test the new interface.

Verify that affinity and priority can be set successfully.

Signed-off-by: Narcisa Vasile <navasile@microsoft.com>
---
 app/test/test_threads.c | 125 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 125 insertions(+)

diff --git a/app/test/test_threads.c b/app/test/test_threads.c
index e7ee50741c..53e5892793 100644
--- a/app/test/test_threads.c
+++ b/app/test/test_threads.c
@@ -44,12 +44,137 @@ test_thread_self(void)
 	return 0;
 }
 
+struct thread_affinity_ctx {
+	rte_cpuset_t *cpuset;
+	unsigned int result;
+};
+
+static void *
+thread_loop_attributes_affinity(void *arg)
+{
+	struct thread_affinity_ctx *ctx = arg;
+	rte_cpuset_t cpuset;
+	size_t i;
+
+	ctx->result = 0;
+
+	CPU_ZERO(&cpuset);
+	if (rte_thread_get_affinity_by_id(rte_thread_self(), &cpuset) != 0) {
+		ctx->result = 1;
+		rte_log(RTE_LOG_DEBUG, threads_logtype_test, "Failed to get thread affinity!");
+		return NULL;
+	}
+
+	/*
+	 * Check that the thread is not running on CPUs which were not
+	 * specified in the affinity mask. Note that the CPU mask
+	 * retrieved above can be different than the original mask specified
+	 * with rte_thread_attr_set_affinity(), since some CPUs may not be
+	 * available on the system.
+	 */
+	for (i = 0; i < CPU_SETSIZE; ++i) {
+		if (!CPU_ISSET(i, ctx->cpuset) && CPU_ISSET(i, &cpuset)) {
+			ctx->result = 1;
+			rte_log(RTE_LOG_DEBUG, threads_logtype_test, "CPU %zu should not be set for this thread!\n",
+					i);
+			return NULL;
+		}
+	}
+
+	return NULL;
+}
+
+static int
+test_thread_attributes_affinity(void)
+{
+	rte_thread_t threads_ids[THREADS_COUNT];
+	struct thread_affinity_ctx ctx[THREADS_COUNT] = {};
+	rte_thread_attr_t attr;
+	rte_cpuset_t cpuset;
+	size_t i;
+	int ret = 0;
+
+	ret = rte_thread_attr_init(&attr);
+	RTE_TEST_ASSERT(ret == 0, "Failed to initialize thread attributes!");
+
+	CPU_ZERO(&cpuset);
+	ret = rte_thread_get_affinity_by_id(rte_thread_self(), &cpuset);
+	RTE_TEST_ASSERT(ret == 0, "Failed to get main thread affinity!");
+
+	ret = rte_thread_attr_set_affinity(&attr, &cpuset);
+	RTE_TEST_ASSERT(ret == 0, "Failed to set thread attributes!");
+
+	for (i = 0; i < THREADS_COUNT; ++i) {
+		ctx[i].cpuset = &cpuset;
+		ret = rte_thread_create(&threads_ids[i], &attr,
+				thread_loop_attributes_affinity, &ctx[i]);
+		RTE_TEST_ASSERT(ret == 0, "Failed to create threads!");
+	}
+
+	for (i = 0; i < THREADS_COUNT; ++i) {
+		ret = rte_thread_join(threads_ids[i], NULL);
+		RTE_TEST_ASSERT(ret == 0, "Failed to join threads!");
+
+		RTE_TEST_ASSERT_EQUAL(ctx[i].result, 0, "Unexpected thread affinity!");
+	}
+
+	return ret;
+}
+
+static void *
+thread_loop_priority(void *arg)
+{
+	int ret;
+	enum rte_thread_priority priority;
+	int *result = arg;
+
+	*result = 1;
+	ret = rte_thread_get_priority(rte_thread_self(), &priority);
+	if (ret != 0 || priority != RTE_THREAD_PRIORITY_NORMAL)
+		*result = 2;
+
+	return NULL;
+}
+
+static int
+test_thread_attributes_priority(void)
+{
+	rte_thread_t threads_ids[THREADS_COUNT];
+	rte_thread_attr_t attr;
+	size_t i;
+	int ret = 0;
+	int results[THREADS_COUNT] = {};
+
+	ret = rte_thread_attr_init(&attr);
+	RTE_TEST_ASSERT(ret == 0, "Failed to initialize  thread attributes!");
+
+	ret = rte_thread_attr_set_priority(&attr, RTE_THREAD_PRIORITY_NORMAL);
+	RTE_TEST_ASSERT(ret == 0, "Failed to set thread priority!");
+
+	for (i = 0; i < THREADS_COUNT; ++i) {
+		ret = rte_thread_create(&threads_ids[i], &attr,
+				thread_loop_priority, &results[i]);
+		RTE_TEST_ASSERT(ret == 0, "Failed to create threads!");
+	}
+
+	for (i = 0; i < THREADS_COUNT; ++i) {
+		ret = rte_thread_join(threads_ids[i], NULL);
+		RTE_TEST_ASSERT(ret == 0, "Failed to join threads!");
+
+		RTE_TEST_ASSERT_EQUAL(results[i], 1, "Unexpected priority value!");
+	}
+
+	return ret;
+}
+
 static struct unit_test_suite threads_test_suite = {
 	.suite_name = "threads autotest",
 	.setup = NULL,
 	.teardown = NULL,
 	.unit_test_cases = {
 			TEST_CASE(test_thread_self),
+			TEST_CASE(test_thread_attributes_affinity),
+			TEST_CASE(test_thread_attributes_priority),
 			TEST_CASES_END()
 	}
 };
-- 
2.31.0.vfs.0.1