From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 6FAC1A0A02; Sat, 27 Mar 2021 00:53:46 +0100 (CET) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 3648C140D9C; Sat, 27 Mar 2021 00:53:18 +0100 (CET) Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by mails.dpdk.org (Postfix) with ESMTP id 17B7640692 for ; Sat, 27 Mar 2021 00:53:09 +0100 (CET) Received: by linux.microsoft.com (Postfix, from userid 1059) id 90E5A20B5684; Fri, 26 Mar 2021 16:53:07 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 90E5A20B5684 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1616802787; bh=UtvKTMIkuTRpKWcpXKgS8TTJmkQMJl9YP3YXhXMf7VY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=lkyVg6kver4k8rGY1jKlAkkD292aO8Htb5rY4gOV81tRUb54pHA4wbbMHEG6JBRjH 1QwbeLCrOKyHriNCj79B3RaZ3k9j/95FFSTjr7sqVUz5eugbg70n0euktFmE9c93BS GJiu3KkqYQBqnSJwI4+Ku6SzZb0VwlaM6mFxtU0M= From: Narcisa Ana Maria Vasile 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: Fri, 26 Mar 2021 16:52:46 -0700 Message-Id: <1616802771-31578-6-git-send-email-navasile@linux.microsoft.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1616802771-31578-1-git-send-email-navasile@linux.microsoft.com> References: <1616644026-25432-11-git-send-email-navasile@linux.microsoft.com> <1616802771-31578-1-git-send-email-navasile@linux.microsoft.com> Subject: [dpdk-dev] [PATCH v4 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" From: Narcisa Vasile Add function for setting the priority for a thread. Signed-off-by: Narcisa Vasile --- 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 4f93e3ff1..26c5b1f3c 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, ¶m); +} + 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 ed8330f81..34948dd90 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 d60a3ded5..544aaf33d 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