[tracker-miners/sam/sched-idle-fix] Use pthreads API to set SCHED_IDLE scheduling policy



commit d85f3e858b5e00a5e06f7101ff4cee47d178cdff
Author: Sam Thursfield <sam afuera me uk>
Date:   Thu Jan 9 11:55:34 2020 +0100

    Use pthreads API to set SCHED_IDLE scheduling policy
    
    Previously we were using the GLIBC sched_setscheduler() function.
    This is Linux-only.  We now use the pthread_setschedparam() function
    from pthreads, which is portable beyond Linux and works with
    musl libc as well as GLIBC.
    
    While the documentation for sched_setscheduler() claims that it affects
    the whole process, in fact it only affects the calling thread - see
    https://www.openwall.com/lists/musl/2016/03/01/5 for details

 src/libtracker-miners-common/tracker-sched.c | 57 +++++++++-------------------
 1 file changed, 18 insertions(+), 39 deletions(-)
---
diff --git a/src/libtracker-miners-common/tracker-sched.c b/src/libtracker-miners-common/tracker-sched.c
index 5c9efb6b5..b7a3bc232 100644
--- a/src/libtracker-miners-common/tracker-sched.c
+++ b/src/libtracker-miners-common/tracker-sched.c
@@ -1,5 +1,6 @@
 /*
  * Copyright (C) 2011, Nokia <ivan frade nokia com>
+ * Copyright (C) 2020, Sam Thursfield <sam afuera me uk>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -19,59 +20,37 @@
 
 #include "config-miners.h"
 
-#ifdef __linux__
-
-#include <errno.h>
-#include <sched.h>
+#include <pthread.h>
 
 #include "tracker-sched.h"
 
+/* Sets the priority of the current thread to SCHED_IDLE.
+ *
+ * Threads spawned from a SCHED_IDLE thread will inherit the same priority,
+ * so you just need to call this function when the main thread starts to
+ * set priority for the whole process.
+ */
 gboolean
 tracker_sched_idle (void)
 {
        struct sched_param sp;
+       int result, policy;
 
-       /* Set process scheduling parameters:
-        * This is used so we don't steal scheduling priority from
-        * the most important applications - like the phone
-        * application which has a real time requirement here. This
-        * is detailed in Nokia bug #95573
-        */
-       g_message ("Setting scheduler policy to SCHED_IDLE");
+       result = pthread_getschedparam (pthread_self (), &policy, &sp);
 
-       if (sched_getparam (0, &sp) == 0) {
-               if (sched_setscheduler (0, SCHED_IDLE, &sp) != 0) {
-//LCOV_EXCL_START
-                       const gchar *str = g_strerror (errno);
+       if (result == 0) {
+               result = pthread_setschedparam (pthread_self(), SCHED_IDLE, &sp);
+       }
 
-                       g_warning ("Could not set scheduler policy, %s",
-                                  str ? str : "no error given");
+       if (result == 0) {
+               g_message ("Set scheduler policy to SCHED_IDLE");
 
-                       return FALSE;
-               }
+               return TRUE;
        } else {
-               const gchar *str = g_strerror (errno);
+               const gchar *str = g_strerror (result);
 
-               g_warning ("Could not get scheduler policy, %s",
-                          str ? str : "no error given");
+               g_message ("Error setting scheduler policy: %s", str ? str : "no error given");
 
                return FALSE;
        }
-//LCOV_EXCL_END
-
-       return TRUE;
 }
-
-#else /* __linux__ */
-
-#include <glib.h>
-
-#include "tracker-sched.h"
-
-gboolean
-tracker_sched_idle (void)
-{
-       return TRUE;
-}
-
-#endif /* __linux__ */


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]