SDL_threadprio.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2025 Sam Lantinga <[email protected]>
  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 SDL_PLATFORM_LINUX
  20. #ifndef SDL_THREADS_DISABLED
  21. #include <sys/time.h>
  22. #include <sys/resource.h>
  23. #include <pthread.h>
  24. #include <sched.h>
  25. #include <unistd.h>
  26. // RLIMIT_RTTIME requires kernel >= 2.6.25 and is in glibc >= 2.14
  27. #ifndef RLIMIT_RTTIME
  28. #define RLIMIT_RTTIME 15
  29. #endif
  30. // SCHED_RESET_ON_FORK is in kernel >= 2.6.32.
  31. #ifndef SCHED_RESET_ON_FORK
  32. #define SCHED_RESET_ON_FORK 0x40000000
  33. #endif
  34. #include "SDL_dbus.h"
  35. #ifdef SDL_USE_LIBDBUS
  36. // d-bus queries to org.freedesktop.RealtimeKit1.
  37. #define RTKIT_DBUS_NODE "org.freedesktop.RealtimeKit1"
  38. #define RTKIT_DBUS_PATH "/org/freedesktop/RealtimeKit1"
  39. #define RTKIT_DBUS_INTERFACE "org.freedesktop.RealtimeKit1"
  40. // d-bus queries to the XDG portal interface to RealtimeKit1
  41. #define XDG_PORTAL_DBUS_NODE "org.freedesktop.portal.Desktop"
  42. #define XDG_PORTAL_DBUS_PATH "/org/freedesktop/portal/desktop"
  43. #define XDG_PORTAL_DBUS_INTERFACE "org.freedesktop.portal.Realtime"
  44. static bool rtkit_use_session_conn;
  45. static const char *rtkit_dbus_node;
  46. static const char *rtkit_dbus_path;
  47. static const char *rtkit_dbus_interface;
  48. static pthread_once_t rtkit_initialize_once = PTHREAD_ONCE_INIT;
  49. static Sint32 rtkit_min_nice_level = -20;
  50. static Sint32 rtkit_max_realtime_priority = 99;
  51. static Sint64 rtkit_max_rttime_usec = 200000;
  52. /*
  53. * Checking that the RTTimeUSecMax property exists and is an int64 confirms that:
  54. * - The desktop portal exists and supports the realtime interface.
  55. * - The realtime interface is new enough to have the required bug fixes applied.
  56. */
  57. static bool realtime_portal_supported(DBusConnection *conn)
  58. {
  59. Sint64 res;
  60. return SDL_DBus_QueryPropertyOnConnection(conn, XDG_PORTAL_DBUS_NODE, XDG_PORTAL_DBUS_PATH, XDG_PORTAL_DBUS_INTERFACE,
  61. "RTTimeUSecMax", DBUS_TYPE_INT64, &res);
  62. }
  63. static void set_rtkit_interface(void)
  64. {
  65. SDL_DBusContext *dbus = SDL_DBus_GetContext();
  66. // xdg-desktop-portal works in all instances, so check for it first.
  67. if (dbus && realtime_portal_supported(dbus->session_conn)) {
  68. rtkit_use_session_conn = true;
  69. rtkit_dbus_node = XDG_PORTAL_DBUS_NODE;
  70. rtkit_dbus_path = XDG_PORTAL_DBUS_PATH;
  71. rtkit_dbus_interface = XDG_PORTAL_DBUS_INTERFACE;
  72. } else { // Fall back to the standard rtkit interface in all other cases.
  73. rtkit_use_session_conn = false;
  74. rtkit_dbus_node = RTKIT_DBUS_NODE;
  75. rtkit_dbus_path = RTKIT_DBUS_PATH;
  76. rtkit_dbus_interface = RTKIT_DBUS_INTERFACE;
  77. }
  78. }
  79. static DBusConnection *get_rtkit_dbus_connection(void)
  80. {
  81. SDL_DBusContext *dbus = SDL_DBus_GetContext();
  82. if (dbus) {
  83. return rtkit_use_session_conn ? dbus->session_conn : dbus->system_conn;
  84. }
  85. return NULL;
  86. }
  87. static void rtkit_initialize(void)
  88. {
  89. DBusConnection *dbus_conn;
  90. set_rtkit_interface();
  91. dbus_conn = get_rtkit_dbus_connection();
  92. // Try getting minimum nice level: this is often greater than PRIO_MIN (-20).
  93. if (!dbus_conn || !SDL_DBus_QueryPropertyOnConnection(dbus_conn, rtkit_dbus_node, rtkit_dbus_path, rtkit_dbus_interface, "MinNiceLevel",
  94. DBUS_TYPE_INT32, &rtkit_min_nice_level)) {
  95. rtkit_min_nice_level = -20;
  96. }
  97. // Try getting maximum realtime priority: this can be less than the POSIX default (99).
  98. if (!dbus_conn || !SDL_DBus_QueryPropertyOnConnection(dbus_conn, rtkit_dbus_node, rtkit_dbus_path, rtkit_dbus_interface, "MaxRealtimePriority",
  99. DBUS_TYPE_INT32, &rtkit_max_realtime_priority)) {
  100. rtkit_max_realtime_priority = 99;
  101. }
  102. // Try getting maximum rttime allowed by rtkit: exceeding this value will result in SIGKILL
  103. if (!dbus_conn || !SDL_DBus_QueryPropertyOnConnection(dbus_conn, rtkit_dbus_node, rtkit_dbus_path, rtkit_dbus_interface, "RTTimeUSecMax",
  104. DBUS_TYPE_INT64, &rtkit_max_rttime_usec)) {
  105. rtkit_max_rttime_usec = 200000;
  106. }
  107. }
  108. static bool rtkit_initialize_realtime_thread(void)
  109. {
  110. // Following is an excerpt from rtkit README that outlines the requirements
  111. // a thread must meet before making rtkit requests:
  112. //
  113. // * Only clients with RLIMIT_RTTIME set will get RT scheduling
  114. //
  115. // * RT scheduling will only be handed out to processes with
  116. // SCHED_RESET_ON_FORK set to guarantee that the scheduling
  117. // settings cannot 'leak' to child processes, thus making sure
  118. // that 'RT fork bombs' cannot be used to bypass RLIMIT_RTTIME
  119. // and take the system down.
  120. //
  121. // * Limits are enforced on all user controllable resources, only
  122. // a maximum number of users, processes, threads can request RT
  123. // scheduling at the same time.
  124. //
  125. // * Only a limited number of threads may be made RT in a
  126. // specific time frame.
  127. //
  128. // * Client authorization is verified with PolicyKit
  129. int err;
  130. struct rlimit rlimit;
  131. int nLimit = RLIMIT_RTTIME;
  132. pid_t nPid = 0; // self
  133. int nSchedPolicy = sched_getscheduler(nPid) | SCHED_RESET_ON_FORK;
  134. struct sched_param schedParam;
  135. SDL_zero(schedParam);
  136. // Requirement #1: Set RLIMIT_RTTIME
  137. err = getrlimit(nLimit, &rlimit);
  138. if (err) {
  139. return false;
  140. }
  141. // Current rtkit allows a max of 200ms right now
  142. rlimit.rlim_max = rtkit_max_rttime_usec;
  143. rlimit.rlim_cur = rlimit.rlim_max / 2;
  144. err = setrlimit(nLimit, &rlimit);
  145. if (err) {
  146. return false;
  147. }
  148. // Requirement #2: Add SCHED_RESET_ON_FORK to the scheduler policy
  149. err = sched_getparam(nPid, &schedParam);
  150. if (err) {
  151. return false;
  152. }
  153. err = sched_setscheduler(nPid, nSchedPolicy, &schedParam);
  154. if (err) {
  155. return false;
  156. }
  157. return true;
  158. }
  159. static bool rtkit_setpriority_nice(pid_t thread, int nice_level)
  160. {
  161. DBusConnection *dbus_conn;
  162. Uint64 pid = (Uint64)getpid();
  163. Uint64 tid = (Uint64)thread;
  164. Sint32 nice = (Sint32)nice_level;
  165. pthread_once(&rtkit_initialize_once, rtkit_initialize);
  166. dbus_conn = get_rtkit_dbus_connection();
  167. if (nice < rtkit_min_nice_level) {
  168. nice = rtkit_min_nice_level;
  169. }
  170. if (!dbus_conn || !SDL_DBus_CallMethodOnConnection(dbus_conn,
  171. rtkit_dbus_node, rtkit_dbus_path, rtkit_dbus_interface, "MakeThreadHighPriorityWithPID",
  172. DBUS_TYPE_UINT64, &pid, DBUS_TYPE_UINT64, &tid, DBUS_TYPE_INT32, &nice, DBUS_TYPE_INVALID,
  173. DBUS_TYPE_INVALID)) {
  174. return false;
  175. }
  176. return true;
  177. }
  178. static bool rtkit_setpriority_realtime(pid_t thread, int rt_priority)
  179. {
  180. DBusConnection *dbus_conn;
  181. Uint64 pid = (Uint64)getpid();
  182. Uint64 tid = (Uint64)thread;
  183. Uint32 priority = (Uint32)rt_priority;
  184. pthread_once(&rtkit_initialize_once, rtkit_initialize);
  185. dbus_conn = get_rtkit_dbus_connection();
  186. if (priority > rtkit_max_realtime_priority) {
  187. priority = rtkit_max_realtime_priority;
  188. }
  189. // We always perform the thread state changes necessary for rtkit.
  190. // This wastes some system calls if the state is already set but
  191. // typically code sets a thread priority and leaves it so it's
  192. // not expected that this wasted effort will be an issue.
  193. // We also do not quit if this fails, we let the rtkit request
  194. // go through to determine whether it really needs to fail or not.
  195. rtkit_initialize_realtime_thread();
  196. if (!dbus_conn || !SDL_DBus_CallMethodOnConnection(dbus_conn,
  197. rtkit_dbus_node, rtkit_dbus_path, rtkit_dbus_interface, "MakeThreadRealtimeWithPID",
  198. DBUS_TYPE_UINT64, &pid, DBUS_TYPE_UINT64, &tid, DBUS_TYPE_UINT32, &priority, DBUS_TYPE_INVALID,
  199. DBUS_TYPE_INVALID)) {
  200. return false;
  201. }
  202. return true;
  203. }
  204. #else
  205. #define rtkit_max_realtime_priority 99
  206. #endif // dbus
  207. #endif // threads
  208. // this is a public symbol, so it has to exist even if threads are disabled.
  209. bool SDL_SetLinuxThreadPriority(Sint64 threadID, int priority)
  210. {
  211. #ifdef SDL_THREADS_DISABLED
  212. return SDL_Unsupported();
  213. #else
  214. if (setpriority(PRIO_PROCESS, (id_t)threadID, priority) == 0) {
  215. return true;
  216. }
  217. #ifdef SDL_USE_LIBDBUS
  218. /* Note that this fails you most likely:
  219. * Have your process's scheduler incorrectly configured.
  220. See the requirements at:
  221. http://git.0pointer.net/rtkit.git/tree/README#n16
  222. * Encountered dbus/polkit security restrictions. Note
  223. that the RealtimeKit1 dbus endpoint is inaccessible
  224. over ssh connections for most common distro configs.
  225. You might want to check your local config for details:
  226. /usr/share/polkit-1/actions/org.freedesktop.RealtimeKit1.policy
  227. README and sample code at: http://git.0pointer.net/rtkit.git
  228. */
  229. if (rtkit_setpriority_nice((pid_t)threadID, priority)) {
  230. return true;
  231. }
  232. #endif
  233. return SDL_SetError("setpriority() failed");
  234. #endif
  235. }
  236. // this is a public symbol, so it has to exist even if threads are disabled.
  237. bool SDL_SetLinuxThreadPriorityAndPolicy(Sint64 threadID, int sdlPriority, int schedPolicy)
  238. {
  239. #ifdef SDL_THREADS_DISABLED
  240. return SDL_Unsupported();
  241. #else
  242. int osPriority;
  243. if (schedPolicy == SCHED_RR || schedPolicy == SCHED_FIFO) {
  244. if (sdlPriority == SDL_THREAD_PRIORITY_LOW) {
  245. osPriority = 1;
  246. } else if (sdlPriority == SDL_THREAD_PRIORITY_HIGH) {
  247. osPriority = rtkit_max_realtime_priority * 3 / 4;
  248. } else if (sdlPriority == SDL_THREAD_PRIORITY_TIME_CRITICAL) {
  249. osPriority = rtkit_max_realtime_priority;
  250. } else {
  251. osPriority = rtkit_max_realtime_priority / 2;
  252. }
  253. } else {
  254. if (sdlPriority == SDL_THREAD_PRIORITY_LOW) {
  255. osPriority = 19;
  256. } else if (sdlPriority == SDL_THREAD_PRIORITY_HIGH) {
  257. osPriority = -10;
  258. } else if (sdlPriority == SDL_THREAD_PRIORITY_TIME_CRITICAL) {
  259. osPriority = -20;
  260. } else {
  261. osPriority = 0;
  262. }
  263. if (setpriority(PRIO_PROCESS, (id_t)threadID, osPriority) == 0) {
  264. return true;
  265. }
  266. }
  267. #ifdef SDL_USE_LIBDBUS
  268. /* Note that this fails you most likely:
  269. * Have your process's scheduler incorrectly configured.
  270. See the requirements at:
  271. http://git.0pointer.net/rtkit.git/tree/README#n16
  272. * Encountered dbus/polkit security restrictions. Note
  273. that the RealtimeKit1 dbus endpoint is inaccessible
  274. over ssh connections for most common distro configs.
  275. You might want to check your local config for details:
  276. /usr/share/polkit-1/actions/org.freedesktop.RealtimeKit1.policy
  277. README and sample code at: http://git.0pointer.net/rtkit.git
  278. */
  279. if (schedPolicy == SCHED_RR || schedPolicy == SCHED_FIFO) {
  280. if (rtkit_setpriority_realtime((pid_t)threadID, osPriority)) {
  281. return true;
  282. }
  283. } else {
  284. if (rtkit_setpriority_nice((pid_t)threadID, osPriority)) {
  285. return true;
  286. }
  287. }
  288. #endif
  289. return SDL_SetError("setpriority() failed");
  290. #endif
  291. }
  292. #endif // SDL_PLATFORM_LINUX