Skip to contentSkip to conversation

add thread priority accessors

PATCH Superseded Tyler Retzlaff 3 participants 15 messages May 23, 2022 – Jun 7, 2022

lore
Share & tools
lore
#0 lore raw

cover add thread priority accessors

this series introduces accessors for get and set of thread priority based on original patches from Narcisa Vasile.

Tyler Retzlaff (2):
eal: get/set thread priority per thread identifier test/threads: add unit test for get set priority

 app/test/test_threads.c      |  52 +++++++++++++++++
 lib/eal/include/rte_thread.h |  50 +++++++++++++++++
 lib/eal/unix/rte_thread.c    | 101 +++++++++++++++++++++++++++++++++
 lib/eal/windows/rte_thread.c | 130 +++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 333 insertions(+)
Signature · 1 line
1.8.3.1
#1 lore raw

PATCH 1/2 eal: get/set thread priority per thread identifier

Add functions for setting and getting the priority of a thread. Priorities on multiple platforms are similarly determined by a priority value and a priority class/policy.

Currently in DPDK most threads operate at the OS-default priority level but there are cases when increasing the priority is useful. For example, high performance applications may require elevated priority levels.

For these reasons, EAL will expose two priority levels which are named suggestively "normal" and "realtime_critical" and are computed as follows:

On Linux, the following mapping is created:
RTE_THREAD_PRIORITY_NORMAL corresponds to
* policy SCHED_OTHER
* priority value: (sched_get_priority_min(SCHED_OTHER) +
sched_get_priority_max(SCHED_OTHER))/2;
RTE_THREAD_PRIORITY_REALTIME_CRITICAL corresponds to
* policy SCHED_RR
* priority value: sched_get_priority_max(SCHED_RR);

On Windows, the following mapping is created:
RTE_THREAD_PRIORITY_NORMAL corresponds to
* class NORMAL_PRIORITY_CLASS
* priority THREAD_PRIORITY_NORMAL
RTE_THREAD_PRIORITY_REALTIME_CRITICAL corresponds to
* class REALTIME_PRIORITY_CLASS (when running with privileges)
* class HIGH_PRIORITY_CLASS (when running without privileges)
* priority THREAD_PRIORITY_TIME_CRITICAL

Note that on Linux the resulting priority value will be 0, in accordance to the documentation that mention the value should be 0 for SCHED_OTHER policy.

---
 lib/eal/include/rte_thread.h |  50 +++++++++++++++++
 lib/eal/unix/rte_thread.c    | 101 +++++++++++++++++++++++++++++++++
 lib/eal/windows/rte_thread.c | 130 +++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 281 insertions(+)
lib/eal/include/rte_thread.h +50 -0
index 7888f7a..9e261bf 100644
@@ -31,6 +31,16 @@
 } rte_thread_t;
 
 /**
+ * Thread priority values.
+ */
+enum rte_thread_priority {
+	RTE_THREAD_PRIORITY_NORMAL            = 0,
+	/**< normal thread priority, the default */
+	RTE_THREAD_PRIORITY_REALTIME_CRITICAL = 1,
+	/**< highest thread priority allowed */
+};
+
+/**
  * TLS key type, an opaque pointer.
  */
 typedef struct eal_tls_key *rte_thread_key;
@@ -115,6 +125,46 @@ int rte_thread_get_affinity_by_id(rte_thread_t thread_id,
 #endif /* RTE_HAS_CPUSET */
 
 /**
+ * @warning
+ * @b EXPERIMENTAL: this API may change without prior notice.
+ *
+ * Get the priority of a thread.
+ *
+ * @param thread_id
+ *   Id of the thread for which to get priority.
+ *
+ * @param priority
+ *   Location to store the retrieved priority.
+ *
+ * @return
+ *   On success, return 0.
+ *   On failure, return a positive errno-style error number.
+ */
+__rte_experimental
+int rte_thread_get_priority(rte_thread_t thread_id,
+		enum rte_thread_priority *priority);
+
+/**
+ * @warning
+ * @b EXPERIMENTAL: this API may change without prior notice.
+ *
+ * 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);
+
+/**
  * Create a TLS data key visible to all threads in the process.
  * the created key is later used to get/set a value.
  * and optional destructor can be set to be called when a thread exits.
lib/eal/unix/rte_thread.c +101 -0

113 lines not on this page · raw message

lib/eal/windows/rte_thread.c +130 -0

142 lines not on this page · raw message

Signature · 1 line
1.8.3.1
#2 lore raw

PATCH 2/2 test/threads: add unit test for get set priority

In reply to Tyler Retzlaff

Add unit tests to exercise and demonstrate rte_thread_{get,set}_priority().

---
 app/test/test_threads.c | 52 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 52 insertions(+)
app/test/test_threads.c +52 -0
index e1a2ea5..d369418 100644
@@ -24,6 +24,56 @@
 }
 
 static int
+test_thread_priority(void)
+{
+	pthread_t id;
+	rte_thread_t thread_id;
+	enum rte_thread_priority priority;
+
+	thread_id_ready = 0;
+	RTE_TEST_ASSERT(pthread_create(&id, NULL, thread_main, &thread_id) == 0,
+		"Failed to create thread");
+
+	while (__atomic_load_n(&thread_id_ready, __ATOMIC_ACQUIRE) == 0)
+		;
+
+	priority = RTE_THREAD_PRIORITY_NORMAL;
+	RTE_TEST_ASSERT(rte_thread_set_priority(thread_id, priority) == 0,
+		"Failed to set thread priority");
+	RTE_TEST_ASSERT(rte_thread_get_priority(thread_id, &priority) == 0,
+		"Failed to get thread priority");
+	RTE_TEST_ASSERT(priority == RTE_THREAD_PRIORITY_NORMAL,
+		"Priority set mismatches priority get");
+
+	priority = RTE_THREAD_PRIORITY_REALTIME_CRITICAL;
+#ifndef RTE_EXEC_ENV_WINDOWS
+	RTE_TEST_ASSERT(rte_thread_set_priority(thread_id, priority) == -ENOTSUP,
+		"Priority set to critical should fail");
+	RTE_TEST_ASSERT(rte_thread_get_priority(thread_id, &priority) == 0,
+		"Failed to get thread priority");
+	RTE_TEST_ASSERT(priority == RTE_THREAD_PRIORITY_NORMAL,
+		"Failed set to critical should have retained normal");
+#else
+	RTE_TEST_ASSERT(rte_thread_set_priority(thread_id, priority) == 0,
+		"Priority set to critical should succeed");
+	RTE_TEST_ASSERT(rte_thread_get_priority(thread_id, &priority) == 0,
+		"Failed to get thread priority");
+	RTE_TEST_ASSERT(priority == RTE_THREAD_PRIORITY_REALTIME_CRITICAL,
+		"Priority get set mistmatches priority get");
+#endif
+
+	priority = RTE_THREAD_PRIORITY_NORMAL;
+	RTE_TEST_ASSERT(rte_thread_set_priority(thread_id, priority) == 0,
+		"Failed to set thread priority");
+	RTE_TEST_ASSERT(rte_thread_get_priority(thread_id, &priority) == 0,
+		"Failed to get thread priority");
+	RTE_TEST_ASSERT(priority == RTE_THREAD_PRIORITY_NORMAL,
+		"Priority set mismatches priority get");
+
+	return 0;
+}
+
+static int
 test_thread_affinity(void)
 {
 	pthread_t id;
@@ -31,6 +81,7 @@
 	rte_cpuset_t cpuset0;
 	rte_cpuset_t cpuset1;
 
+	thread_id_ready = 0;
 	RTE_TEST_ASSERT(pthread_create(&id, NULL, thread_main, &thread_id) == 0,
 		"Failed to create thread");
 
@@ -68,6 +119,7 @@
 	.teardown = NULL,
 	.unit_test_cases = {
 		TEST_CASE(test_thread_affinity),
+		TEST_CASE(test_thread_priority),
 		TEST_CASES_END()
 	}
 };
Signature · 1 line
1.8.3.1
#3 lore raw

Re: [PATCH 2/2] test/threads: add unit test for get set priority

Quoted 41 lines · Tyler Retzlaff

Add unit tests to exercise and demonstrate rte_thread_{get,set}_priority().

---
 app/test/test_threads.c | 52 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 52 insertions(+)
app/test/test_threads.c +26 -0
index e1a2ea5..d369418 100644
@@ -24,6 +24,56 @@
 }
 
 static int
+test_thread_priority(void)
+{
+	pthread_t id;
+	rte_thread_t thread_id;
+	enum rte_thread_priority priority;
+
+	thread_id_ready = 0;
+	RTE_TEST_ASSERT(pthread_create(&id, NULL, thread_main, &thread_id) == 0,
+		"Failed to create thread");
+
+	while (__atomic_load_n(&thread_id_ready, __ATOMIC_ACQUIRE) == 0)
+		;
+
+	priority = RTE_THREAD_PRIORITY_NORMAL;
+	RTE_TEST_ASSERT(rte_thread_set_priority(thread_id, priority) == 0,
+		"Failed to set thread priority");
+	RTE_TEST_ASSERT(rte_thread_get_priority(thread_id, &priority) == 0,
+		"Failed to get thread priority");
+	RTE_TEST_ASSERT(priority == RTE_THREAD_PRIORITY_NORMAL,
+		"Priority set mismatches priority get");
+
+	priority = RTE_THREAD_PRIORITY_REALTIME_CRITICAL;
+#ifndef RTE_EXEC_ENV_WINDOWS
+	RTE_TEST_ASSERT(rte_thread_set_priority(thread_id, priority) == -ENOTSUP,
+		"Priority set to critical should fail");
+	RTE_TEST_ASSERT(rte_thread_get_priority(thread_id, &priority) == 0,

app/test/test_threads.c line 62

Quoted 10 lines
app/test/test_threads.c +10 -0
+		"Failed to get thread priority");
+	RTE_TEST_ASSERT(priority == RTE_THREAD_PRIORITY_NORMAL,
+		"Failed set to critical should have retained normal");
+#else
+	RTE_TEST_ASSERT(rte_thread_set_priority(thread_id, priority) == 0,
+		"Priority set to critical should succeed");
+	RTE_TEST_ASSERT(rte_thread_get_priority(thread_id, &priority) == 0,
+		"Failed to get thread priority");
+	RTE_TEST_ASSERT(priority == RTE_THREAD_PRIORITY_REALTIME_CRITICAL,
+		"Priority get set mistmatches priority get");

mistmatches -> mismatches

will fix this when addressing more substantial feedback

Quoted 35 lines
app/test/test_threads.c +16 -0
+#endif
+
+	priority = RTE_THREAD_PRIORITY_NORMAL;
+	RTE_TEST_ASSERT(rte_thread_set_priority(thread_id, priority) == 0,
+		"Failed to set thread priority");
+	RTE_TEST_ASSERT(rte_thread_get_priority(thread_id, &priority) == 0,
+		"Failed to get thread priority");
+	RTE_TEST_ASSERT(priority == RTE_THREAD_PRIORITY_NORMAL,
+		"Priority set mismatches priority get");
+
+	return 0;
+}
+
+static int
 test_thread_affinity(void)
 {
 	pthread_t id;
@@ -31,6 +81,7 @@
 	rte_cpuset_t cpuset0;
 	rte_cpuset_t cpuset1;
 
+	thread_id_ready = 0;
 	RTE_TEST_ASSERT(pthread_create(&id, NULL, thread_main, &thread_id) == 0,
 		"Failed to create thread");
 
@@ -68,6 +119,7 @@
 	.teardown = NULL,
 	.unit_test_cases = {
 		TEST_CASE(test_thread_affinity),
+		TEST_CASE(test_thread_priority),
 		TEST_CASES_END()
 	}
 };
Signature · 1 line
1.8.3.1
#4 lore raw

v2 cover add thread priority accessors

In reply to Tyler Retzlaff

this series introduces accessors for get and set of thread priority based on original patches from Narcisa Vasile.

v2:
* fix typo in test assert string mistmatch -> mismatch
* add new experimental exports names to lib/eal/version.map

Tyler Retzlaff (2):
eal: get/set thread priority per thread identifier test/threads: add unit test for get set priority

 app/test/test_threads.c      |  52 +++++++++++++++++
 lib/eal/include/rte_thread.h |  50 +++++++++++++++++
 lib/eal/unix/rte_thread.c    | 101 +++++++++++++++++++++++++++++++++
 lib/eal/version.map          |   2 +
 lib/eal/windows/rte_thread.c | 130 +++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 335 insertions(+)
Signature · 1 line
1.8.3.1
#5 lore raw

PATCH v2 1/2 eal: get/set thread priority per thread identifier

Add functions for setting and getting the priority of a thread. Priorities on multiple platforms are similarly determined by a priority value and a priority class/policy.

Currently in DPDK most threads operate at the OS-default priority level but there are cases when increasing the priority is useful. For example, high performance applications may require elevated priority levels.

For these reasons, EAL will expose two priority levels which are named suggestively "normal" and "realtime_critical" and are computed as follows:

On Linux, the following mapping is created:
RTE_THREAD_PRIORITY_NORMAL corresponds to
* policy SCHED_OTHER
* priority value: (sched_get_priority_min(SCHED_OTHER) +
sched_get_priority_max(SCHED_OTHER))/2;
RTE_THREAD_PRIORITY_REALTIME_CRITICAL corresponds to
* policy SCHED_RR
* priority value: sched_get_priority_max(SCHED_RR);

On Windows, the following mapping is created:
RTE_THREAD_PRIORITY_NORMAL corresponds to
* class NORMAL_PRIORITY_CLASS
* priority THREAD_PRIORITY_NORMAL
RTE_THREAD_PRIORITY_REALTIME_CRITICAL corresponds to
* class REALTIME_PRIORITY_CLASS (when running with privileges)
* class HIGH_PRIORITY_CLASS (when running without privileges)
* priority THREAD_PRIORITY_TIME_CRITICAL

Note that on Linux the resulting priority value will be 0, in accordance to the documentation that mention the value should be 0 for SCHED_OTHER policy.

---
 lib/eal/include/rte_thread.h |  50 +++++++++++++++++
 lib/eal/unix/rte_thread.c    | 101 +++++++++++++++++++++++++++++++++
 lib/eal/version.map          |   2 +
 lib/eal/windows/rte_thread.c | 130 +++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 283 insertions(+)
lib/eal/include/rte_thread.h +50 -0
index 7888f7a..9e261bf 100644
@@ -31,6 +31,16 @@
 } rte_thread_t;
 
 /**
+ * Thread priority values.
+ */
+enum rte_thread_priority {
+	RTE_THREAD_PRIORITY_NORMAL            = 0,
+	/**< normal thread priority, the default */
+	RTE_THREAD_PRIORITY_REALTIME_CRITICAL = 1,
+	/**< highest thread priority allowed */
+};
+
+/**
  * TLS key type, an opaque pointer.
  */
 typedef struct eal_tls_key *rte_thread_key;
@@ -115,6 +125,46 @@ int rte_thread_get_affinity_by_id(rte_thread_t thread_id,
 #endif /* RTE_HAS_CPUSET */
 
 /**
+ * @warning
+ * @b EXPERIMENTAL: this API may change without prior notice.
+ *
+ * Get the priority of a thread.
+ *
+ * @param thread_id
+ *   Id of the thread for which to get priority.
+ *
+ * @param priority
+ *   Location to store the retrieved priority.
+ *
+ * @return
+ *   On success, return 0.
+ *   On failure, return a positive errno-style error number.
+ */
+__rte_experimental
+int rte_thread_get_priority(rte_thread_t thread_id,
+		enum rte_thread_priority *priority);
+
+/**
+ * @warning
+ * @b EXPERIMENTAL: this API may change without prior notice.
+ *
+ * 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);
+
+/**
  * Create a TLS data key visible to all threads in the process.
  * the created key is later used to get/set a value.
  * and optional destructor can be set to be called when a thread exits.
lib/eal/unix/rte_thread.c +101 -0

113 lines not on this page · raw message

lib/eal/version.map +2 -0
index d49e30b..ab27a4b 100644
@@ -423,8 +423,10 @@ EXPERIMENTAL {
 
 	# added in 22.07
 	rte_thread_get_affinity_by_id;
+	rte_thread_get_priority;
 	rte_thread_self;
 	rte_thread_set_affinity_by_id;
+	rte_thread_set_priority;
 };
 
 INTERNAL {
lib/eal/windows/rte_thread.c +130 -0

142 lines not on this page · raw message

Signature · 1 line
1.8.3.1
#6 lore raw

PATCH v2 2/2 test/threads: add unit test for get set priority

In reply to Tyler Retzlaff

Add unit tests to exercise and demonstrate rte_thread_{get,set}_priority().

---
 app/test/test_threads.c | 52 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 52 insertions(+)
app/test/test_threads.c +52 -0
index e1a2ea5..dbc20e6 100644
@@ -24,6 +24,56 @@
 }
 
 static int
+test_thread_priority(void)
+{
+	pthread_t id;
+	rte_thread_t thread_id;
+	enum rte_thread_priority priority;
+
+	thread_id_ready = 0;
+	RTE_TEST_ASSERT(pthread_create(&id, NULL, thread_main, &thread_id) == 0,
+		"Failed to create thread");
+
+	while (__atomic_load_n(&thread_id_ready, __ATOMIC_ACQUIRE) == 0)
+		;
+
+	priority = RTE_THREAD_PRIORITY_NORMAL;
+	RTE_TEST_ASSERT(rte_thread_set_priority(thread_id, priority) == 0,
+		"Failed to set thread priority");
+	RTE_TEST_ASSERT(rte_thread_get_priority(thread_id, &priority) == 0,
+		"Failed to get thread priority");
+	RTE_TEST_ASSERT(priority == RTE_THREAD_PRIORITY_NORMAL,
+		"Priority set mismatches priority get");
+
+	priority = RTE_THREAD_PRIORITY_REALTIME_CRITICAL;
+#ifndef RTE_EXEC_ENV_WINDOWS
+	RTE_TEST_ASSERT(rte_thread_set_priority(thread_id, priority) == -ENOTSUP,
+		"Priority set to critical should fail");
+	RTE_TEST_ASSERT(rte_thread_get_priority(thread_id, &priority) == 0,
+		"Failed to get thread priority");
+	RTE_TEST_ASSERT(priority == RTE_THREAD_PRIORITY_NORMAL,
+		"Failed set to critical should have retained normal");
+#else
+	RTE_TEST_ASSERT(rte_thread_set_priority(thread_id, priority) == 0,
+		"Priority set to critical should succeed");
+	RTE_TEST_ASSERT(rte_thread_get_priority(thread_id, &priority) == 0,
+		"Failed to get thread priority");
+	RTE_TEST_ASSERT(priority == RTE_THREAD_PRIORITY_REALTIME_CRITICAL,
+		"Priority set mismatches priority get");
+#endif
+
+	priority = RTE_THREAD_PRIORITY_NORMAL;
+	RTE_TEST_ASSERT(rte_thread_set_priority(thread_id, priority) == 0,
+		"Failed to set thread priority");
+	RTE_TEST_ASSERT(rte_thread_get_priority(thread_id, &priority) == 0,
+		"Failed to get thread priority");
+	RTE_TEST_ASSERT(priority == RTE_THREAD_PRIORITY_NORMAL,
+		"Priority set mismatches priority get");
+
+	return 0;
+}
+
+static int
 test_thread_affinity(void)
 {
 	pthread_t id;
@@ -31,6 +81,7 @@
 	rte_cpuset_t cpuset0;
 	rte_cpuset_t cpuset1;
 
+	thread_id_ready = 0;
 	RTE_TEST_ASSERT(pthread_create(&id, NULL, thread_main, &thread_id) == 0,
 		"Failed to create thread");
 
@@ -68,6 +119,7 @@
 	.teardown = NULL,
 	.unit_test_cases = {
 		TEST_CASE(test_thread_affinity),
+		TEST_CASE(test_thread_priority),
 		TEST_CASES_END()
 	}
 };
Signature · 1 line
1.8.3.1
#7 lore raw

v3 cover add thread priority accessors

In reply to Tyler Retzlaff

this series introduces accessors for get and set of thread priority based on original patches from Narcisa Vasile.

v3:
* synchronize exit of thread_main to maintain thread life for duration of affinity and priority tests.
* fix test check of errno return value should be non-negative ENOTSUP instead of -ENOTSUP

v2:
* fix typo in test assert string mistmatch -> mismatch
* add new experimental exports names to lib/eal/version.map

Tyler Retzlaff (2):
eal: get/set thread priority per thread identifier test/threads: add unit test for get set priority

 app/test/test_threads.c      |  57 +++++++++++++++++++
 lib/eal/include/rte_thread.h |  50 +++++++++++++++++
 lib/eal/unix/rte_thread.c    | 101 +++++++++++++++++++++++++++++++++
 lib/eal/version.map          |   2 +
 lib/eal/windows/rte_thread.c | 130 +++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 340 insertions(+)
Signature · 1 line
1.8.3.1
#8 lore raw

PATCH v3 1/2 eal: get/set thread priority per thread identifier

Add functions for setting and getting the priority of a thread. Priorities on multiple platforms are similarly determined by a priority value and a priority class/policy.

Currently in DPDK most threads operate at the OS-default priority level but there are cases when increasing the priority is useful. For example, high performance applications may require elevated priority levels.

For these reasons, EAL will expose two priority levels which are named suggestively "normal" and "realtime_critical" and are computed as follows:

On Linux, the following mapping is created:
RTE_THREAD_PRIORITY_NORMAL corresponds to
* policy SCHED_OTHER
* priority value: (sched_get_priority_min(SCHED_OTHER) +
sched_get_priority_max(SCHED_OTHER))/2;
RTE_THREAD_PRIORITY_REALTIME_CRITICAL corresponds to
* policy SCHED_RR
* priority value: sched_get_priority_max(SCHED_RR);

On Windows, the following mapping is created:
RTE_THREAD_PRIORITY_NORMAL corresponds to
* class NORMAL_PRIORITY_CLASS
* priority THREAD_PRIORITY_NORMAL
RTE_THREAD_PRIORITY_REALTIME_CRITICAL corresponds to
* class REALTIME_PRIORITY_CLASS (when running with privileges)
* class HIGH_PRIORITY_CLASS (when running without privileges)
* priority THREAD_PRIORITY_TIME_CRITICAL

Note that on Linux the resulting priority value will be 0, in accordance to the documentation that mention the value should be 0 for SCHED_OTHER policy.

---
 lib/eal/include/rte_thread.h |  50 +++++++++++++++++
 lib/eal/unix/rte_thread.c    | 101 +++++++++++++++++++++++++++++++++
 lib/eal/version.map          |   2 +
 lib/eal/windows/rte_thread.c | 130 +++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 283 insertions(+)
lib/eal/include/rte_thread.h +50 -0
index 7888f7a..9e261bf 100644
@@ -31,6 +31,16 @@
 } rte_thread_t;
 
 /**
+ * Thread priority values.
+ */
+enum rte_thread_priority {
+	RTE_THREAD_PRIORITY_NORMAL            = 0,
+	/**< normal thread priority, the default */
+	RTE_THREAD_PRIORITY_REALTIME_CRITICAL = 1,
+	/**< highest thread priority allowed */
+};
+
+/**
  * TLS key type, an opaque pointer.
  */
 typedef struct eal_tls_key *rte_thread_key;
@@ -115,6 +125,46 @@ int rte_thread_get_affinity_by_id(rte_thread_t thread_id,
 #endif /* RTE_HAS_CPUSET */
 
 /**
+ * @warning
+ * @b EXPERIMENTAL: this API may change without prior notice.
+ *
+ * Get the priority of a thread.
+ *
+ * @param thread_id
+ *   Id of the thread for which to get priority.
+ *
+ * @param priority
+ *   Location to store the retrieved priority.
+ *
+ * @return
+ *   On success, return 0.
+ *   On failure, return a positive errno-style error number.
+ */
+__rte_experimental
+int rte_thread_get_priority(rte_thread_t thread_id,
+		enum rte_thread_priority *priority);
+
+/**
+ * @warning
+ * @b EXPERIMENTAL: this API may change without prior notice.
+ *
+ * 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);
+
+/**
  * Create a TLS data key visible to all threads in the process.
  * the created key is later used to get/set a value.
  * and optional destructor can be set to be called when a thread exits.
lib/eal/unix/rte_thread.c +101 -0

113 lines not on this page · raw message

lib/eal/version.map +2 -0
index d49e30b..ab27a4b 100644
@@ -423,8 +423,10 @@ EXPERIMENTAL {
 
 	# added in 22.07
 	rte_thread_get_affinity_by_id;
+	rte_thread_get_priority;
 	rte_thread_self;
 	rte_thread_set_affinity_by_id;
+	rte_thread_set_priority;
 };
 
 INTERNAL {
lib/eal/windows/rte_thread.c +130 -0

142 lines not on this page · raw message

Signature · 1 line
1.8.3.1
#9 lore raw

Re: [PATCH v3 1/2] eal: get/set thread priority per thread identifier

Quoted 15 lines · On Tue, 24 May 2022 04:08:36 -0700 Tyler Retzlaff…
lib/eal/unix/rte_thread.c +15 -0
+static int
+thread_map_priority_to_os_value(enum rte_thread_priority eal_pri,
+		int *os_pri, int *pol)
+{
+	/* Clear the output parameters */
+	*os_pri = sched_get_priority_min(SCHED_OTHER) - 1;
+	*pol = -1;
+
+	switch (eal_pri) {
+	case RTE_THREAD_PRIORITY_NORMAL:
+		*pol = SCHED_OTHER;
+
+		/*
+		 * Choose the middle of the range to represent
+		 * the priority 'normal'.

lib/eal/unix/rte_thread.c struct eal_tls_key { · line 43

Quoted 10 lines
lib/eal/unix/rte_thread.c +10 -0
+		 * On Linux, this should be 0, since both
+		 * sched_get_priority_min/_max return 0 for SCHED_OTHER.
+		 */
+		*os_pri = (sched_get_priority_min(SCHED_OTHER) +
+			sched_get_priority_max(SCHED_OTHER))/2;
+		break;
+	case RTE_THREAD_PRIORITY_REALTIME_CRITICAL:
+		*pol = SCHED_RR;
+		*os_pri = sched_get_priority_max(SCHED_RR);
+		break;

Many people have experimented with realtime priorities with DPDK and Linux, and have never heard of any one not having problems.

Recommend that this either be an error add a warning log message that "Setting real time priority may break"

Quoted 6 lines
lib/eal/unix/rte_thread.c +6 -0
+	default:
+		RTE_LOG(DEBUG, EAL, "The requested priority value is invalid.\n");
+		return EINVAL;
+	}
+	return 0;
+}
#10 lore raw

Re: [PATCH v3 1/2] eal: get/set thread priority per thread identifier

Quoted 32 lines · Stephen Hemminger
Quoted 15 lines · On Tue, 24 May 2022 04:08:36 -0700 Tyler Retzlaff…
lib/eal/unix/rte_thread.c +15 -0
+static int
+thread_map_priority_to_os_value(enum rte_thread_priority eal_pri,
+		int *os_pri, int *pol)
+{
+	/* Clear the output parameters */
+	*os_pri = sched_get_priority_min(SCHED_OTHER) - 1;
+	*pol = -1;
+
+	switch (eal_pri) {
+	case RTE_THREAD_PRIORITY_NORMAL:
+		*pol = SCHED_OTHER;
+
+		/*
+		 * Choose the middle of the range to represent
+		 * the priority 'normal'.

lib/eal/unix/rte_thread.c struct eal_tls_key { · line 43

Quoted 10 lines
lib/eal/unix/rte_thread.c +10 -0
+		 * On Linux, this should be 0, since both
+		 * sched_get_priority_min/_max return 0 for SCHED_OTHER.
+		 */
+		*os_pri = (sched_get_priority_min(SCHED_OTHER) +
+			sched_get_priority_max(SCHED_OTHER))/2;
+		break;
+	case RTE_THREAD_PRIORITY_REALTIME_CRITICAL:
+		*pol = SCHED_RR;
+		*os_pri = sched_get_priority_max(SCHED_RR);
+		break;

Many people have experimented with realtime priorities with DPDK and Linux, and have never heard of any one not having problems.

Quoted 2 lines

Recommend that this either be an error add a warning log message that "Setting real time priority may break"

so i went back through the feedback when the priority change was originally introduced. you're request was that you didn't want it to be possible for realtime priority to be set on linux.

this particular function is just about mapping the abstracted representation of realtime priority in eal to the platform specific linux representation.

so if you take a look at the other parts of the patch you will notice that the rte_thread_set_priority() for linux will in fact fail if you try to use realtime priority.

additionally, the unit test exercises that attempting to use realtime priority does fail for linux.

take another look and let me know if you don't agree.

thanks

#11 lore raw

Re: [PATCH v3 1/2] eal: get/set thread priority per thread identifier

Quoted 48 lines · On Wed, 25 May 2022 23:29:57 -0700 Tyler Retzlaff…
Quoted 32 lines · Stephen Hemminger
Quoted 15 lines · On Tue, 24 May 2022 04:08:36 -0700 Tyler Retzlaff…
lib/eal/unix/rte_thread.c +15 -0
+static int
+thread_map_priority_to_os_value(enum rte_thread_priority eal_pri,
+		int *os_pri, int *pol)
+{
+	/* Clear the output parameters */
+	*os_pri = sched_get_priority_min(SCHED_OTHER) - 1;
+	*pol = -1;
+
+	switch (eal_pri) {
+	case RTE_THREAD_PRIORITY_NORMAL:
+		*pol = SCHED_OTHER;
+
+		/*
+		 * Choose the middle of the range to represent
+		 * the priority 'normal'.

lib/eal/unix/rte_thread.c struct eal_tls_key { · line 43

Quoted 10 lines
lib/eal/unix/rte_thread.c +10 -0
+		 * On Linux, this should be 0, since both
+		 * sched_get_priority_min/_max return 0 for SCHED_OTHER.
+		 */
+		*os_pri = (sched_get_priority_min(SCHED_OTHER) +
+			sched_get_priority_max(SCHED_OTHER))/2;
+		break;
+	case RTE_THREAD_PRIORITY_REALTIME_CRITICAL:
+		*pol = SCHED_RR;
+		*os_pri = sched_get_priority_max(SCHED_RR);
+		break;  

Many people have experimented with realtime priorities with DPDK and Linux, and have never heard of any one not having problems.

Quoted 2 lines

Recommend that this either be an error add a warning log message that "Setting real time priority may break"

so i went back through the feedback when the priority change was originally introduced. you're request was that you didn't want it to be possible for realtime priority to be set on linux.

this particular function is just about mapping the abstracted representation of realtime priority in eal to the platform specific linux representation.

so if you take a look at the other parts of the patch you will notice that the rte_thread_set_priority() for linux will in fact fail if you try to use realtime priority.

Quoted 6 lines

additionally, the unit test exercises that attempting to use realtime priority does fail for linux.

take another look and let me know if you don't agree.

thanks

Thanks for clarifying.
Is there any analog for SCHED_DEADLINE worth considering?

#12 lore raw

Re: [PATCH v3 1/2] eal: get/set thread priority per thread identifier

Quoted 3 lines · Stephen Hemminger

Thanks for clarifying.
Is there any analog for SCHED_DEADLINE worth considering?

i hadn't considered it. for now i probably won't since this patch is just a blocker for getting the thread lifetime series up. which is higher priority as i understand.

#13 lore raw

PATCH v3 2/2 test/threads: add unit test for get set priority

In reply to Tyler Retzlaff

Add unit tests to exercise and demonstrate rte_thread_{get,set}_priority().

---
 app/test/test_threads.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 57 insertions(+)
app/test/test_threads.c +57 -0
index e1a2ea5..b9d8b4e 100644
@@ -20,10 +20,65 @@
 	*(rte_thread_t *)arg = rte_thread_self();
 	__atomic_store_n(&thread_id_ready, 1, __ATOMIC_RELEASE);
 
+	while (__atomic_load_n(&thread_id_ready, __ATOMIC_ACQUIRE) == 1)
+		;
+
 	return NULL;
 }
 
 static int
+test_thread_priority(void)
+{
+	pthread_t id;
+	rte_thread_t thread_id;
+	enum rte_thread_priority priority;
+
+	thread_id_ready = 0;
+	RTE_TEST_ASSERT(pthread_create(&id, NULL, thread_main, &thread_id) == 0,
+		"Failed to create thread");
+
+	while (__atomic_load_n(&thread_id_ready, __ATOMIC_ACQUIRE) == 0)
+		;
+
+	priority = RTE_THREAD_PRIORITY_NORMAL;
+	RTE_TEST_ASSERT(rte_thread_set_priority(thread_id, priority) == 0,
+		"Failed to set thread priority");
+	RTE_TEST_ASSERT(rte_thread_get_priority(thread_id, &priority) == 0,
+		"Failed to get thread priority");
+	RTE_TEST_ASSERT(priority == RTE_THREAD_PRIORITY_NORMAL,
+		"Priority set mismatches priority get");
+
+	priority = RTE_THREAD_PRIORITY_REALTIME_CRITICAL;
+#ifndef RTE_EXEC_ENV_WINDOWS
+	RTE_TEST_ASSERT(rte_thread_set_priority(thread_id, priority) == ENOTSUP,
+		"Priority set to critical should fail");
+	RTE_TEST_ASSERT(rte_thread_get_priority(thread_id, &priority) == 0,
+		"Failed to get thread priority");
+	RTE_TEST_ASSERT(priority == RTE_THREAD_PRIORITY_NORMAL,
+		"Failed set to critical should have retained normal");
+#else
+	RTE_TEST_ASSERT(rte_thread_set_priority(thread_id, priority) == 0,
+		"Priority set to critical should succeed");
+	RTE_TEST_ASSERT(rte_thread_get_priority(thread_id, &priority) == 0,
+		"Failed to get thread priority");
+	RTE_TEST_ASSERT(priority == RTE_THREAD_PRIORITY_REALTIME_CRITICAL,
+		"Priority set mismatches priority get");
+#endif
+
+	priority = RTE_THREAD_PRIORITY_NORMAL;
+	RTE_TEST_ASSERT(rte_thread_set_priority(thread_id, priority) == 0,
+		"Failed to set thread priority");
+	RTE_TEST_ASSERT(rte_thread_get_priority(thread_id, &priority) == 0,
+		"Failed to get thread priority");
+	RTE_TEST_ASSERT(priority == RTE_THREAD_PRIORITY_NORMAL,
+		"Priority set mismatches priority get");
+
+	__atomic_store_n(&thread_id_ready, 2, __ATOMIC_RELEASE);
+
+	return 0;
+}
+
+static int
 test_thread_affinity(void)
 {
 	pthread_t id;
@@ -31,6 +86,7 @@
 	rte_cpuset_t cpuset0;
 	rte_cpuset_t cpuset1;
 
+	thread_id_ready = 0;
 	RTE_TEST_ASSERT(pthread_create(&id, NULL, thread_main, &thread_id) == 0,
 		"Failed to create thread");
 
@@ -68,6 +124,7 @@
 	.teardown = NULL,
 	.unit_test_cases = {
 		TEST_CASE(test_thread_affinity),
+		TEST_CASE(test_thread_priority),
 		TEST_CASES_END()
 	}
 };
Signature · 1 line
1.8.3.1
#14 lore raw
In reply to Tyler Retzlaff
Quoted 18 lines · Tyler Retzlaff

this series introduces accessors for get and set of thread priority based on original patches from Narcisa Vasile.

v3:
* synchronize exit of thread_main to maintain thread life for duration of affinity and priority tests.
* fix test check of errno return value should be non-negative ENOTSUP instead of -ENOTSUP

v2:
* fix typo in test assert string mistmatch -> mismatch
* add new experimental exports names to lib/eal/version.map

Tyler Retzlaff (2):
eal: get/set thread priority per thread identifier test/threads: add unit test for get set priority

Quoted 6 lines
 app/test/test_threads.c      |  57 +++++++++++++++++++
 lib/eal/include/rte_thread.h |  50 +++++++++++++++++
 lib/eal/unix/rte_thread.c    | 101 +++++++++++++++++++++++++++++++++
 lib/eal/version.map          |   2 +
 lib/eal/windows/rte_thread.c | 130 +++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 340 insertions(+)

Series applied, thanks.

Signature · 1 line
David Marchand