Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
realtime: request SCHED_RR using CAP_SYS_NICE
Try to gain SCHED_RR (round-robin) realtime scheduling privileges before starting the server. This requires CAP_SYS_NICE on Linux systems. We additionally register a pthread_atfork callback which resets the scheduling class back to SCHED_OTHER (the Linux system default). Due to CAP_SYS_NICE, setting RLIMIT_RTPRIO has no effect on the process as documented within man 7 sched (from Linux): Privileged (CAP_SYS_NICE) threads ignore the RLIMIT_RTPRIO limit; as with older kernels, they can make arbitrary changes to scheduling policy and priority. See getrlimit(2) for further information on RLIMIT_RTPRIO Note that this requires the sway distribution packagers to set the CAP_SYS_NICE capability on the sway binary. Supersedes #6992
- Loading branch information
1 parent
3f60056
commit a3a82ef
Showing
4 changed files
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#include <sys/resource.h> | ||
#include <sched.h> | ||
#include <unistd.h> | ||
#include <pthread.h> | ||
#include "sway/server.h" | ||
#include "log.h" | ||
|
||
static void child_fork_callback(void) { | ||
struct sched_param param; | ||
|
||
param.sched_priority = 0; | ||
|
||
int ret = pthread_setschedparam(pthread_self(), SCHED_OTHER, ¶m); | ||
if (ret != 0) { | ||
sway_log(SWAY_ERROR, "Failed to reset scheduler policy on fork"); | ||
} | ||
} | ||
|
||
void set_rr_scheduling(void) { | ||
int prio = sched_get_priority_min(SCHED_RR); | ||
int old_policy; | ||
int ret; | ||
struct sched_param param; | ||
|
||
ret = pthread_getschedparam(pthread_self(), &old_policy, ¶m); | ||
if (ret != 0) { | ||
sway_log(SWAY_DEBUG, "Failed to get old scheduling priority"); | ||
return; | ||
} | ||
|
||
param.sched_priority = prio; | ||
|
||
ret = pthread_setschedparam(pthread_self(), SCHED_RR, ¶m); | ||
if (ret != 0) { | ||
sway_log(SWAY_INFO, "Failed to set scheduling priority to %d", prio); | ||
return; | ||
} | ||
|
||
pthread_atfork(NULL, NULL, child_fork_callback); | ||
} |