Mesa (master): Linux: Change minimum priority threads from SCHED_IDLE to nice 19 SCHED_BATCH.

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Fri May 8 10:35:15 UTC 2020


Module: Mesa
Branch: master
Commit: 78d267e6dad9dbcc9e39d7d388075d15e67ba3c3
URL:    http://cgit.freedesktop.org/mesa/mesa/commit/?id=78d267e6dad9dbcc9e39d7d388075d15e67ba3c3

Author: Con Kolivas <kernel at kolivas.org>
Date:   Wed May  6 18:03:37 2020 +1000

Linux: Change minimum priority threads from SCHED_IDLE to nice 19 SCHED_BATCH.

SCHED_IDLE on linux can lead to extraordinarily long periods of no scheduling
leading to starvation of minimum priority threads for such an extended period
that it can eventually lead to GUI stalls. Switch to renicing the threads to
the lowest priority and use the SCHED_BATCH scheduling policy which is a hint
to the scheduler that this is latency insensitive thread instead. This change
has been confirmed to address unexpected GUI related stalls in mesa
applications across a range of different linux kernels.

Signed-off-by: Con Kolivas <kernel at kolivas.org>
Reviewed-by: Marek Olšák <marek.olsak at amd.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/4912>

---

 src/util/u_queue.c | 21 ++++++++++++++++++---
 1 file changed, 18 insertions(+), 3 deletions(-)

diff --git a/src/util/u_queue.c b/src/util/u_queue.c
index 4049a4c7692..8f6dc08b332 100644
--- a/src/util/u_queue.c
+++ b/src/util/u_queue.c
@@ -33,6 +33,13 @@
 #include "util/u_thread.h"
 #include "u_process.h"
 
+#if defined(__linux__)
+#include <sys/time.h>
+#include <sys/resource.h>
+#include <sys/syscall.h>
+#endif
+
+
 /* Define 256MB */
 #define S_256MB (256 * 1024 * 1024)
 
@@ -258,6 +265,13 @@ util_queue_thread_func(void *input)
    }
 #endif
 
+#if defined(__linux__)
+   if (queue->flags & UTIL_QUEUE_INIT_USE_MINIMUM_PRIORITY) {
+      /* The nice() function can only set a maximum of 19. */
+      setpriority(PRIO_PROCESS, syscall(SYS_gettid), 19);
+   }
+#endif
+
    if (strlen(queue->name) > 0) {
       char name[16];
       snprintf(name, sizeof(name), "%s%i", queue->name, thread_index);
@@ -331,16 +345,17 @@ util_queue_create_thread(struct util_queue *queue, unsigned index)
    }
 
    if (queue->flags & UTIL_QUEUE_INIT_USE_MINIMUM_PRIORITY) {
-#if defined(__linux__) && defined(SCHED_IDLE)
+#if defined(__linux__) && defined(SCHED_BATCH)
       struct sched_param sched_param = {0};
 
       /* The nice() function can only set a maximum of 19.
-       * SCHED_IDLE is the same as nice = 20.
+       * SCHED_BATCH gives the scheduler a hint that this is a latency
+       * insensitive thread.
        *
        * Note that Linux only allows decreasing the priority. The original
        * priority can't be restored.
        */
-      pthread_setschedparam(queue->threads[index], SCHED_IDLE, &sched_param);
+      pthread_setschedparam(queue->threads[index], SCHED_BATCH, &sched_param);
 #endif
    }
    return true;



More information about the mesa-commit mailing list