SDL_threadprio.c 12 KB

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