[chrony-dev] [PATCH] Use pthread_setschedparam instead of sched_setscheduler

[ Thread Index | Date Index | More chrony.tuxfamily.org/chrony-dev Archives ]


Hi,

I ran into an issue with musl libc on Linux where sched_setscheduler
isn't implemented. Overall, it seems that pthread_setschedparam is
more widely supported across different C libraries and OSs. For
chrony's purposes on Linux, it should make no difference which call is
used.

Below is the simplest diff I could make, but cleaning up the
dependency check for pthreads is probably in order. I could also work
on a more general patch that brings real-time scheduling to any posix
platform.

I tested on Arch x86_64 Linux with glibc and buildroot ARM Linux with musl libc.

Regards,
Stefan


diff --git a/configure b/configure
index 486b0bc..565b888 100755
--- a/configure
+++ b/configure
@@ -786,13 +786,14 @@ fi

 if [ $try_setsched = "1" ] && \
   test_code \
-    'sched_setscheduler()' \
+    'pthread_setschedparam()' \
     'sched.h' '' '' '
      struct sched_param sched;
      sched_get_priority_max(SCHED_FIFO);
-     sched_setscheduler(0, SCHED_FIFO, &sched);'
+     pthread_setschedparam(pthread_self(), SCHED_FIFO, &sched);'
 then
-  add_def HAVE_SCHED_SETSCHEDULER
+  add_def HAVE_PTHREAD_SETSCHEDPARAM
+  MYCFLAGS="$MYCFLAGS -pthread"
 fi

 if [ $try_lockmem = "1" ] && \
diff --git a/sys.c b/sys.c
index 4d68b37..2c42db1 100644
--- a/sys.c
+++ b/sys.c
@@ -124,7 +124,7 @@ void SYS_EnableSystemCallFilter(int level)

 void SYS_SetScheduler(int SchedPriority)
 {
-#if defined(LINUX) && defined(HAVE_SCHED_SETSCHEDULER)
+#if defined(LINUX) && defined(HAVE_PTHREAD_SETSCHEDPARAM)
   SYS_Linux_SetScheduler(SchedPriority);
 #elif defined(MACOSX)
   SYS_MacOSX_SetScheduler(SchedPriority);
diff --git a/sys_linux.c b/sys_linux.c
index 6ae7c0d..c339f09 100644
--- a/sys_linux.c
+++ b/sys_linux.c
@@ -33,8 +33,8 @@

 #include <sys/utsname.h>

-#if defined(HAVE_SCHED_SETSCHEDULER)
-#  include <sched.h>
+#if defined(HAVE_PTHREAD_SETSCHEDPARAM)
+#  include <pthread.h>
 #endif

 #if defined(HAVE_MLOCKALL)
@@ -632,7 +632,7 @@ add_failed:

 /* ================================================== */

-#if defined(HAVE_SCHED_SETSCHEDULER)
+#if defined(HAVE_PTHREAD_SETSCHEDPARAM)
   /* Install SCHED_FIFO real-time scheduler with specified priority */
 void SYS_Linux_SetScheduler(int SchedPriority)
 {
@@ -651,8 +651,8 @@ void SYS_Linux_SetScheduler(int SchedPriority)
     else if ( SchedPriority < pmin ) {
       sched.sched_priority = pmin;
     }
-    if ( sched_setscheduler(0, SCHED_FIFO, &sched) == -1 ) {
-      LOG(LOGS_ERR, "sched_setscheduler() failed");
+    if ( pthread_setschedparam(pthread_self(), SCHED_FIFO, &sched) == -1 ) {
+      LOG(LOGS_ERR, "pthread_setschedparam() failed");
     }
     else {
       DEBUG_LOG("Enabled SCHED_FIFO with priority %d",
@@ -660,7 +660,7 @@ void SYS_Linux_SetScheduler(int SchedPriority)
     }
   }
 }
-#endif /* HAVE_SCHED_SETSCHEDULER */
+#endif /* HAVE_PTHREAD_SETSCHEDPARAM  */

 #if defined(HAVE_MLOCKALL)
 /* Lock the process into RAM so that it will never be swapped out */

-- 
To unsubscribe email chrony-dev-request@xxxxxxxxxxxxxxxxxxxx with "unsubscribe" in the subject.
For help email chrony-dev-request@xxxxxxxxxxxxxxxxxxxx with "help" in the subject.
Trouble?  Email listmaster@xxxxxxxxxxxxxxxxxxxx.


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/