SDL_threadprio.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "../../SDL_internal.h"
  19. #ifdef __LINUX__
  20. #include "SDL_error.h"
  21. #include "SDL_stdinc.h"
  22. #if !SDL_THREADS_DISABLED
  23. #include <sys/time.h>
  24. #include <sys/resource.h>
  25. #include <pthread.h>
  26. #include "SDL_system.h"
  27. #include "SDL_dbus.h"
  28. #if SDL_USE_LIBDBUS
  29. #include <sched.h>
  30. /* d-bus queries to org.freedesktop.RealtimeKit1. */
  31. #define RTKIT_DBUS_NODE "org.freedesktop.RealtimeKit1"
  32. #define RTKIT_DBUS_PATH "/org/freedesktop/RealtimeKit1"
  33. #define RTKIT_DBUS_INTERFACE "org.freedesktop.RealtimeKit1"
  34. static pthread_once_t rtkit_initialize_once = PTHREAD_ONCE_INIT;
  35. static Sint32 rtkit_min_nice_level = -20;
  36. static Sint32 rtkit_max_realtime_priority = 99;
  37. static void
  38. rtkit_initialize()
  39. {
  40. SDL_DBusContext *dbus = SDL_DBus_GetContext();
  41. /* Try getting minimum nice level: this is often greater than PRIO_MIN (-20). */
  42. if (!dbus || !SDL_DBus_QueryPropertyOnConnection(dbus->system_conn, RTKIT_DBUS_NODE, RTKIT_DBUS_PATH, RTKIT_DBUS_INTERFACE, "MinNiceLevel",
  43. DBUS_TYPE_INT32, &rtkit_min_nice_level)) {
  44. rtkit_min_nice_level = -20;
  45. }
  46. /* Try getting maximum realtime priority: this can be less than the POSIX default (99). */
  47. if (!dbus || !SDL_DBus_QueryPropertyOnConnection(dbus->system_conn, RTKIT_DBUS_NODE, RTKIT_DBUS_PATH, RTKIT_DBUS_INTERFACE, "MaxRealtimePriority",
  48. DBUS_TYPE_INT32, &rtkit_max_realtime_priority)) {
  49. rtkit_max_realtime_priority = 99;
  50. }
  51. }
  52. static SDL_bool
  53. rtkit_initialize_thread()
  54. {
  55. // Following is an excerpt from rtkit README that outlines the requirements
  56. // a thread must meet before making rtkit requests:
  57. //
  58. // * Only clients with RLIMIT_RTTIME set will get RT scheduling
  59. //
  60. // * RT scheduling will only be handed out to processes with
  61. // SCHED_RESET_ON_FORK set to guarantee that the scheduling
  62. // settings cannot 'leak' to child processes, thus making sure
  63. // that 'RT fork bombs' cannot be used to bypass RLIMIT_RTTIME
  64. // and take the system down.
  65. //
  66. // * Limits are enforced on all user controllable resources, only
  67. // a maximum number of users, processes, threads can request RT
  68. // scheduling at the same time.
  69. //
  70. // * Only a limited number of threads may be made RT in a
  71. // specific time frame.
  72. //
  73. // * Client authorization is verified with PolicyKit
  74. int err;
  75. struct rlimit rlimit;
  76. int nLimit = RLIMIT_RTTIME;
  77. pid_t nPid = 0; //self
  78. int nSchedPolicy = sched_getscheduler(nPid) | SCHED_RESET_ON_FORK;
  79. struct sched_param schedParam = {};
  80. // Requirement #1: Set RLIMIT_RTTIME
  81. err = getrlimit(nLimit, &rlimit);
  82. if (err)
  83. {
  84. return SDL_FALSE;
  85. }
  86. rlimit.rlim_cur = rlimit.rlim_max;
  87. err = setrlimit(nLimit, &rlimit);
  88. if (err)
  89. {
  90. return SDL_FALSE;
  91. }
  92. // Requirement #2: Add SCHED_RESET_ON_FORK to the scheduler policy
  93. err = sched_getparam(nPid, &schedParam);
  94. if (err)
  95. {
  96. return SDL_FALSE;
  97. }
  98. err = sched_setscheduler(nPid, nSchedPolicy, &schedParam);
  99. if (err)
  100. {
  101. return SDL_FALSE;
  102. }
  103. return SDL_TRUE;
  104. }
  105. static SDL_bool
  106. rtkit_setpriority_nice(pid_t thread, int nice_level)
  107. {
  108. Uint64 ui64 = (Uint64)thread;
  109. Sint32 si32 = (Sint32)nice_level;
  110. SDL_DBusContext *dbus = SDL_DBus_GetContext();
  111. pthread_once(&rtkit_initialize_once, rtkit_initialize);
  112. if (si32 < rtkit_min_nice_level)
  113. si32 = rtkit_min_nice_level;
  114. // We always perform the thread state changes necessary for rtkit.
  115. // This wastes some system calls if the state is already set but
  116. // typically code sets a thread priority and leaves it so it's
  117. // not expected that this wasted effort will be an issue.
  118. // We also do not quit if this fails, we let the rtkit request
  119. // go through to determine whether it really needs to fail or not.
  120. rtkit_initialize_thread();
  121. if (!dbus || !SDL_DBus_CallMethodOnConnection(dbus->system_conn,
  122. RTKIT_DBUS_NODE, RTKIT_DBUS_PATH, RTKIT_DBUS_INTERFACE, "MakeThreadHighPriority",
  123. DBUS_TYPE_UINT64, &ui64, DBUS_TYPE_INT32, &si32, DBUS_TYPE_INVALID,
  124. DBUS_TYPE_INVALID)) {
  125. return SDL_FALSE;
  126. }
  127. return SDL_TRUE;
  128. }
  129. static SDL_bool
  130. rtkit_setpriority_realtime(pid_t thread, int rt_priority)
  131. {
  132. Uint64 ui64 = (Uint64)thread;
  133. Sint32 si32 = (Sint32)rt_priority;
  134. SDL_DBusContext *dbus = SDL_DBus_GetContext();
  135. pthread_once(&rtkit_initialize_once, rtkit_initialize);
  136. if (si32 > rtkit_max_realtime_priority)
  137. si32 = rtkit_max_realtime_priority;
  138. // We always perform the thread state changes necessary for rtkit.
  139. // This wastes some system calls if the state is already set but
  140. // typically code sets a thread priority and leaves it so it's
  141. // not expected that this wasted effort will be an issue.
  142. // We also do not quit if this fails, we let the rtkit request
  143. // go through to determine whether it really needs to fail or not.
  144. rtkit_initialize_thread();
  145. if (!dbus || !SDL_DBus_CallMethodOnConnection(dbus->system_conn,
  146. RTKIT_DBUS_NODE, RTKIT_DBUS_PATH, RTKIT_DBUS_INTERFACE, "MakeThreadRealtime",
  147. DBUS_TYPE_UINT64, &ui64, DBUS_TYPE_INT32, &si32, DBUS_TYPE_INVALID,
  148. DBUS_TYPE_INVALID)) {
  149. return SDL_FALSE;
  150. }
  151. return SDL_TRUE;
  152. }
  153. #endif /* dbus */
  154. #endif /* threads */
  155. /* this is a public symbol, so it has to exist even if threads are disabled. */
  156. int
  157. SDL_LinuxSetThreadPriority(Sint64 threadID, int priority)
  158. {
  159. #if SDL_THREADS_DISABLED
  160. return SDL_Unsupported();
  161. #else
  162. if (setpriority(PRIO_PROCESS, (id_t)threadID, priority) == 0) {
  163. return 0;
  164. }
  165. #if SDL_USE_LIBDBUS
  166. /* Note that this fails you most likely:
  167. * Have your process's scheduler incorrectly configured.
  168. See the requirements at:
  169. http://git.0pointer.net/rtkit.git/tree/README#n16
  170. * Encountered dbus/polkit security restrictions. Note
  171. that the RealtimeKit1 dbus endpoint is inaccessible
  172. over ssh connections for most common distro configs.
  173. You might want to check your local config for details:
  174. /usr/share/polkit-1/actions/org.freedesktop.RealtimeKit1.policy
  175. README and sample code at: http://git.0pointer.net/rtkit.git
  176. */
  177. if (rtkit_setpriority_nice((pid_t)threadID, priority)) {
  178. return 0;
  179. }
  180. #endif
  181. return SDL_SetError("setpriority() failed");
  182. #endif
  183. }
  184. /* this is a public symbol, so it has to exist even if threads are disabled. */
  185. int
  186. SDL_LinuxSetThreadPriorityAndPolicy(Sint64 threadID, int sdlPriority, int schedPolicy)
  187. {
  188. #if SDL_THREADS_DISABLED
  189. return SDL_Unsupported();
  190. #else
  191. int osPriority;
  192. if (schedPolicy == SCHED_RR || schedPolicy == SCHED_FIFO) {
  193. if (sdlPriority == SDL_THREAD_PRIORITY_LOW) {
  194. osPriority = 1;
  195. } else if (sdlPriority == SDL_THREAD_PRIORITY_HIGH) {
  196. osPriority = rtkit_max_realtime_priority * 3 / 4;
  197. } else if (sdlPriority == SDL_THREAD_PRIORITY_TIME_CRITICAL) {
  198. osPriority = rtkit_max_realtime_priority;
  199. } else {
  200. osPriority = rtkit_max_realtime_priority / 2;
  201. }
  202. } else {
  203. if (sdlPriority == SDL_THREAD_PRIORITY_LOW) {
  204. osPriority = 19;
  205. } else if (sdlPriority == SDL_THREAD_PRIORITY_HIGH) {
  206. osPriority = -10;
  207. } else if (sdlPriority == SDL_THREAD_PRIORITY_TIME_CRITICAL) {
  208. osPriority = -20;
  209. } else {
  210. osPriority = 0;
  211. }
  212. if (setpriority(PRIO_PROCESS, (id_t)threadID, osPriority) == 0) {
  213. return 0;
  214. }
  215. }
  216. #if SDL_USE_LIBDBUS
  217. /* Note that this fails you most likely:
  218. * Have your process's scheduler incorrectly configured.
  219. See the requirements at:
  220. http://git.0pointer.net/rtkit.git/tree/README#n16
  221. * Encountered dbus/polkit security restrictions. Note
  222. that the RealtimeKit1 dbus endpoint is inaccessible
  223. over ssh connections for most common distro configs.
  224. You might want to check your local config for details:
  225. /usr/share/polkit-1/actions/org.freedesktop.RealtimeKit1.policy
  226. README and sample code at: http://git.0pointer.net/rtkit.git
  227. */
  228. if (schedPolicy == SCHED_RR || schedPolicy == SCHED_FIFO) {
  229. if (rtkit_setpriority_realtime((pid_t)threadID, osPriority)) {
  230. return 0;
  231. }
  232. } else {
  233. if (rtkit_setpriority_nice((pid_t)threadID, osPriority)) {
  234. return 0;
  235. }
  236. }
  237. #endif
  238. return SDL_SetError("setpriority() failed");
  239. #endif
  240. }
  241. #endif /* __LINUX__ */
  242. /* vi: set ts=4 sw=4 expandtab: */