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 AECAAA0A02;
	Thu, 25 Mar 2021 04:48:17 +0100 (CET)
Received: from [217.70.189.124] (localhost [127.0.0.1])
	by mails.dpdk.org (Postfix) with ESMTP id 67AEB140E36;
	Thu, 25 Mar 2021 04:47:38 +0100 (CET)
Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182])
 by mails.dpdk.org (Postfix) with ESMTP id C43CC140DE3
 for <dev@dpdk.org>; Thu, 25 Mar 2021 04:47:27 +0100 (CET)
Received: by linux.microsoft.com (Postfix, from userid 1059)
 id 3EDAB20B5686; Wed, 24 Mar 2021 20:47:26 -0700 (PDT)
DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 3EDAB20B5686
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com;
 s=default; t=1616644046;
 bh=Rn+qpuRBNtmx5USkI1N5ebNQ7a/SSLIkuSW1siGKkGE=;
 h=From:To:Cc:Subject:Date:In-Reply-To:References:From;
 b=bIv4CcOvUL+hHzms5+iAMPLM3nu5/0D3cpyii382qY22KMzTNWREoI6XAoFihNsWN
 eBO6Ai/ymhaBcsv7XRqFe3j7lpwxgX8saYQkJnT98JbqFCa8DvEc5vVyP0CmOxQNrt
 hiYA6l1Sd7CXfshoiSsT9rlIe35h+ztVQqoh6bKk=
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: Wed, 24 Mar 2021 20:47:01 -0700
Message-Id: <1616644026-25432-6-git-send-email-navasile@linux.microsoft.com>
X-Mailer: git-send-email 1.8.3.1
In-Reply-To: <1616644026-25432-1-git-send-email-navasile@linux.microsoft.com>
References: <1616458835-28502-1-git-send-email-navasile@linux.microsoft.com>
 <1616644026-25432-1-git-send-email-navasile@linux.microsoft.com>
Subject: [dpdk-dev] [PATCH v3 05/10] eal: implement thread priority
 management functions
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>

Add function for setting the priority for a thread.

Signed-off-by: Narcisa Vasile <navasile@microsoft.com>
---
 lib/librte_eal/common/rte_thread.c  | 25 ++++++++++
 lib/librte_eal/include/rte_thread.h | 17 +++++++
 lib/librte_eal/windows/rte_thread.c | 76 +++++++++++++++++++++++++++++
 3 files changed, 118 insertions(+)

diff --git a/lib/librte_eal/common/rte_thread.c b/lib/librte_eal/common/rte_thread.c
index 9df98263e..9e0d710dd 100644
--- a/lib/librte_eal/common/rte_thread.c
+++ b/lib/librte_eal/common/rte_thread.c
@@ -42,6 +42,31 @@ int rte_thread_get_affinity_by_id(rte_thread_t threadid, size_t cpuset_size,
 	return pthread_getaffinity_np(threadid, cpuset_size, cpuset);
 }
 
+int
+rte_thread_set_priority(rte_thread_t thread_id,
+		enum rte_thread_priority priority)
+{
+	int policy;
+	struct sched_param param = {
+		.sched_priority = 0,
+	};
+
+
+	if (priority == RTE_THREAD_PRIORITY_REALTIME_CRITICAL) {
+		policy = SCHED_RR;
+		param.sched_priority = priority;
+	} else if (priority == RTE_THREAD_PRIORITY_NORMAL) {
+		policy = SCHED_OTHER;
+		param.sched_priority = priority;
+	} else {
+		RTE_LOG(DEBUG, EAL, "Invalid priority to set."
+				    "Defaulting to priority 'normal'.\n");
+		policy = SCHED_OTHER;
+	}
+
+	return pthread_setschedparam(thread_id, policy, &param);
+}
+
 int
 rte_thread_attr_init(rte_thread_attr_t *attr)
 {
diff --git a/lib/librte_eal/include/rte_thread.h b/lib/librte_eal/include/rte_thread.h
index 2e54b1da3..999c6d71d 100644
--- a/lib/librte_eal/include/rte_thread.h
+++ b/lib/librte_eal/include/rte_thread.h
@@ -114,6 +114,23 @@ __rte_experimental
 int rte_thread_get_affinity_by_id(rte_thread_t thread_id, size_t cpuset_size,
 				  rte_cpuset_t *cpuset);
 
+/**
+ * Set the priority of a thread.
+ *
+ * @param thread_id
+ *    Id of the thread for which to set priority.
+ *
+ * @param priority
+ *   Priority value to be set.
+ *
+ * @return
+ *   On success, return 0.
+ *   On failure, return a positive errno-style error number.
+ */
+__rte_experimental
+int rte_thread_set_priority(rte_thread_t thread_id,
+			    enum rte_thread_priority priority);
+
 /**
  * Initialize the attributes of a thread.
  * These attributes can be passed to the rte_thread_create() function
diff --git a/lib/librte_eal/windows/rte_thread.c b/lib/librte_eal/windows/rte_thread.c
index 865b1b781..3fb283ecb 100644
--- a/lib/librte_eal/windows/rte_thread.c
+++ b/lib/librte_eal/windows/rte_thread.c
@@ -193,6 +193,82 @@ rte_thread_get_affinity_by_id(rte_thread_t thread_id, size_t cpuset_size,
 	return ret;
 }
 
+static HANDLE
+get_process_handle_from_thread_handle(HANDLE thread_handle)
+{
+	DWORD process_id = 0;
+
+	process_id = GetProcessIdOfThread(thread_handle);
+	if (process_id == 0) {
+		RTE_LOG_WIN32_ERR("GetProcessIdOfThread()");
+		return NULL;
+	}
+
+	return OpenProcess(PROCESS_SET_INFORMATION, FALSE, process_id);
+}
+
+int
+rte_thread_set_priority(rte_thread_t thread_id,
+			enum rte_thread_priority priority)
+{
+	HANDLE thread_handle = NULL;
+	HANDLE process_handle = NULL;
+	DWORD priority_class = NORMAL_PRIORITY_CLASS;
+	int ret = 0;
+
+	thread_handle = OpenThread(THREAD_SET_INFORMATION |
+				   THREAD_QUERY_INFORMATION, FALSE, thread_id);
+	if (thread_handle == NULL) {
+		ret = rte_thread_translate_win32_error(GetLastError());
+		RTE_LOG_WIN32_ERR("OpenThread()");
+		goto cleanup;
+	}
+
+	switch (priority) {
+
+	case RTE_THREAD_PRIORITY_REALTIME_CRITICAL:
+		priority_class = REALTIME_PRIORITY_CLASS;
+		break;
+
+	case RTE_THREAD_PRIORITY_NORMAL:
+	/* FALLTHROUGH */
+	default:
+		priority_class = NORMAL_PRIORITY_CLASS;
+		priority = RTE_THREAD_PRIORITY_NORMAL;
+		break;
+	}
+
+	process_handle = get_process_handle_from_thread_handle(thread_handle);
+	if (process_handle == NULL) {
+		ret = rte_thread_translate_win32_error(GetLastError());
+		RTE_LOG_WIN32_ERR("get_process_handle_from_thread_handle()");
+		goto cleanup;
+	}
+
+	if (!SetPriorityClass(process_handle, priority_class)) {
+		ret = rte_thread_translate_win32_error(GetLastError());
+		RTE_LOG_WIN32_ERR("SetPriorityClass()");
+		goto cleanup;
+	}
+
+	if (!SetThreadPriority(thread_handle, priority)) {
+		ret = rte_thread_translate_win32_error(GetLastError());
+		RTE_LOG_WIN32_ERR("SetThreadPriority()");
+		goto cleanup;
+	}
+
+cleanup:
+	if (thread_handle != NULL) {
+		CloseHandle(thread_handle);
+		thread_handle = NULL;
+	}
+	if (process_handle != NULL) {
+		CloseHandle(process_handle);
+		process_handle = NULL;
+	}
+	return ret;
+}
+
 int
 rte_thread_attr_init(rte_thread_attr_t *attr)
 {
-- 
2.30.0.vfs.0.2