SDL_threadprio.c 12 KB

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