Added support for adjusting thread priorities using Linux RealtimeKit
authorSam Lantinga <slouken@libsdl.org>
Mon, 23 Apr 2018 17:10:36 -0700
changeset 11946eae649a13dad
parent 11945 ffd52bb02bcc
child 11947 89b721a030c0
Added support for adjusting thread priorities using Linux RealtimeKit

Michael Sartain

This is a quick pass at adding Linux RealtimeKit thread priority support to SDL.

It allows me to bump the thread priority to high without root privileges or setting any caps, etc.

rtkit readme here:
http://git.0pointer.net/rtkit.git/tree/README
src/thread/pthread/SDL_systhread.c
test/testthread.c
     1.1 --- a/src/thread/pthread/SDL_systhread.c	Mon Apr 16 02:11:09 2018 -0400
     1.2 +++ b/src/thread/pthread/SDL_systhread.c	Mon Apr 23 17:10:36 2018 -0700
     1.3 @@ -34,6 +34,9 @@
     1.4  #include <sys/resource.h>
     1.5  #include <sys/syscall.h>
     1.6  #include <unistd.h>
     1.7 +#include <errno.h>
     1.8 +
     1.9 +#include "../../core/linux/SDL_dbus.h"
    1.10  #endif /* __LINUX__ */
    1.11  
    1.12  #if defined(__LINUX__) || defined(__MACOSX__) || defined(__IPHONEOS__)
    1.13 @@ -43,6 +46,7 @@
    1.14  #endif
    1.15  #endif
    1.16  
    1.17 +#include "SDL_log.h"
    1.18  #include "SDL_platform.h"
    1.19  #include "SDL_thread.h"
    1.20  #include "../SDL_thread_c.h"
    1.21 @@ -180,6 +184,60 @@
    1.22      return ((SDL_threadID) pthread_self());
    1.23  }
    1.24  
    1.25 +/* d-bus queries to org.freedesktop.RealtimeKit1. */
    1.26 +#if SDL_USE_LIBDBUS
    1.27 +
    1.28 +#define RTKIT_DBUS_NODE "org.freedesktop.RealtimeKit1"
    1.29 +#define RTKIT_DBUS_PATH "/org/freedesktop/RealtimeKit1"
    1.30 +#define RTKIT_DBUS_INTERFACE "org.freedesktop.RealtimeKit1"
    1.31 +
    1.32 +static pthread_once_t rtkit_initialize_once = PTHREAD_ONCE_INIT;
    1.33 +static Sint32 rtkit_min_nice_level = -20;
    1.34 +
    1.35 +static void
    1.36 +rtkit_initialize()
    1.37 +{
    1.38 +    SDL_DBusContext *dbus = SDL_DBus_GetContext();
    1.39 +
    1.40 +    /* Try getting minimum nice level: this is often greater than PRIO_MIN (-20). */
    1.41 +    if (!SDL_DBus_QueryPropertyOnConnection(dbus->system_conn, RTKIT_DBUS_NODE, RTKIT_DBUS_PATH, RTKIT_DBUS_INTERFACE, "MinNiceLevel",
    1.42 +                                            DBUS_TYPE_INT32, &rtkit_min_nice_level)) {
    1.43 +        rtkit_min_nice_level = -20;
    1.44 +    }
    1.45 +}
    1.46 +
    1.47 +static SDL_bool
    1.48 +rtkit_setpriority(pid_t thread, int nice_level)
    1.49 +{
    1.50 +    Uint64 ui64 = (Uint64)thread;
    1.51 +    Sint32 si32 = (Sint32)nice_level;
    1.52 +    SDL_DBusContext *dbus = SDL_DBus_GetContext();
    1.53 +
    1.54 +    pthread_once(&rtkit_initialize_once, rtkit_initialize);
    1.55 +
    1.56 +    if (si32 < rtkit_min_nice_level)
    1.57 +        si32 = rtkit_min_nice_level;
    1.58 +
    1.59 +    if (!SDL_DBus_CallMethodOnConnection(dbus->system_conn,
    1.60 +            RTKIT_DBUS_NODE, RTKIT_DBUS_PATH, RTKIT_DBUS_INTERFACE, "MakeThreadHighPriority",
    1.61 +            DBUS_TYPE_UINT64, &ui64, DBUS_TYPE_INT32, &si32, DBUS_TYPE_INVALID,
    1.62 +            DBUS_TYPE_INVALID)) {
    1.63 +        return SDL_FALSE;
    1.64 +    }
    1.65 +
    1.66 +    return SDL_TRUE;
    1.67 +}
    1.68 +
    1.69 +#else
    1.70 +
    1.71 +static SDL_bool
    1.72 +rtkit_setpriority(pid_t thread, int nice_level)
    1.73 +{
    1.74 +    return SDL_FALSE;
    1.75 +}
    1.76 +
    1.77 +#endif /* !SDL_USE_LIBDBUS */
    1.78 +
    1.79  int
    1.80  SDL_SYS_SetThreadPriority(SDL_ThreadPriority priority)
    1.81  {
    1.82 @@ -188,6 +246,7 @@
    1.83      return 0;
    1.84  #elif __LINUX__
    1.85      int value;
    1.86 +    pid_t thread = syscall(SYS_gettid);
    1.87  
    1.88      if (priority == SDL_THREAD_PRIORITY_LOW) {
    1.89          value = 19;
    1.90 @@ -196,15 +255,22 @@
    1.91      } else {
    1.92          value = 0;
    1.93      }
    1.94 -    if (setpriority(PRIO_PROCESS, syscall(SYS_gettid), value) < 0) {
    1.95 +    if (setpriority(PRIO_PROCESS, thread, value) < 0) {
    1.96          /* Note that this fails if you're trying to set high priority
    1.97             and you don't have root permission. BUT DON'T RUN AS ROOT!
    1.98  
    1.99             You can grant the ability to increase thread priority by
   1.100             running the following command on your application binary:
   1.101                 sudo setcap 'cap_sys_nice=eip' <application>
   1.102 +
   1.103 +           Let's try setting priority with RealtimeKit...
   1.104 +
   1.105 +           README and sample code at:
   1.106 +             http://git.0pointer.net/rtkit.git
   1.107           */
   1.108 -        return SDL_SetError("setpriority() failed");
   1.109 +        if (rtkit_setpriority(thread, value) == SDL_FALSE) {
   1.110 +            return SDL_SetError("setpriority() failed");
   1.111 +        }
   1.112      }
   1.113      return 0;
   1.114  #else
     2.1 --- a/test/testthread.c	Mon Apr 16 02:11:09 2018 -0400
     2.2 +++ b/test/testthread.c	Mon Apr 23 17:10:36 2018 -0700
     2.3 @@ -20,6 +20,7 @@
     2.4  
     2.5  static SDL_TLSID tls;
     2.6  static int alive = 0;
     2.7 +static int testprio = 0;
     2.8  
     2.9  /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
    2.10  static void
    2.11 @@ -29,14 +30,36 @@
    2.12      exit(rc);
    2.13  }
    2.14  
    2.15 +static const char *
    2.16 +getprioritystr(SDL_ThreadPriority priority)
    2.17 +{
    2.18 +    switch(priority)
    2.19 +    {
    2.20 +    case SDL_THREAD_PRIORITY_LOW: return "SDL_THREAD_PRIORITY_LOW";
    2.21 +    case SDL_THREAD_PRIORITY_NORMAL: return "SDL_THREAD_PRIORITY_NORMAL";
    2.22 +    case SDL_THREAD_PRIORITY_HIGH: return "SDL_THREAD_PRIORITY_HIGH";
    2.23 +    }
    2.24 +
    2.25 +    return "???";
    2.26 +}
    2.27 +
    2.28  int SDLCALL
    2.29  ThreadFunc(void *data)
    2.30  {
    2.31 +    SDL_ThreadPriority prio = SDL_THREAD_PRIORITY_NORMAL;
    2.32 +
    2.33      SDL_TLSSet(tls, "baby thread", NULL);
    2.34      SDL_Log("Started thread %s: My thread id is %lu, thread data = %s\n",
    2.35             (char *) data, SDL_ThreadID(), (const char *)SDL_TLSGet(tls));
    2.36      while (alive) {
    2.37          SDL_Log("Thread '%s' is alive!\n", (char *) data);
    2.38 +
    2.39 +        if (testprio) {
    2.40 +            SDL_Log("SDL_SetThreadPriority(%s):%d\n", getprioritystr(prio), SDL_SetThreadPriority(prio));
    2.41 +            if (++prio > SDL_THREAD_PRIORITY_HIGH)
    2.42 +                prio = SDL_THREAD_PRIORITY_LOW;
    2.43 +        }
    2.44 +
    2.45          SDL_Delay(1 * 1000);
    2.46      }
    2.47      SDL_Log("Thread '%s' exiting!\n", (char *) data);
    2.48 @@ -55,6 +78,7 @@
    2.49  int
    2.50  main(int argc, char *argv[])
    2.51  {
    2.52 +    int arg = 1;
    2.53      SDL_Thread *thread;
    2.54  
    2.55      /* Enable standard application logging */
    2.56 @@ -66,6 +90,13 @@
    2.57          return (1);
    2.58      }
    2.59  
    2.60 +    while (argv[arg] && *argv[arg] == '-') {
    2.61 +        if (SDL_strcmp(argv[arg], "--prio") == 0) {
    2.62 +            testprio = 1;
    2.63 +        }
    2.64 +        ++arg;
    2.65 +    }
    2.66 +
    2.67      tls = SDL_TLSCreate();
    2.68      SDL_assert(tls);
    2.69      SDL_TLSSet(tls, "main thread", NULL);