File: gamemode-sched.c

package info (click to toggle)
gamemode 1.8.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 640 kB
  • sloc: ansic: 5,314; sh: 101; xml: 34; makefile: 7
file content (204 lines) | stat: -rw-r--r-- 7,100 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*

Copyright (c) 2017-2019, Feral Interactive
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

 * Redistributions of source code must retain the above copyright notice,
   this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.
 * Neither the name of Feral Interactive nor the names of its contributors
   may be used to endorse or promote products derived from this software
   without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.

 */

#define _GNU_SOURCE

#include "gamemode.h"
#include "common-logging.h"
#include "gamemode-config.h"

#include <dirent.h>
#include <sched.h>
#include <sys/resource.h>
#include <sys/sysinfo.h>

/* SCHED_ISO may not be defined as it is a reserved value not yet
 * implemented in official kernel sources, see linux/sched.h.
 */
#ifndef SCHED_ISO
#define SCHED_ISO 4
#endif

/**
 * Apply scheduling policies
 *
 * This tries to change the scheduler of the client to soft realtime mode
 * available in some kernels as SCHED_ISO. It also tries to adjust the nice
 * level. If some of each fail, ignore this and log a warning.
 */

#define RENICE_INVALID -128 /* Special value to store invalid value */
int game_mode_get_renice(const pid_t client)
{
	/* Clear errno as -1 is a regitimate return */
	errno = 0;
	int priority = getpriority(PRIO_PROCESS, (id_t)client);
	if (priority == -1 && errno) {
		LOG_ERROR("getprority(PRIO_PROCESS, %d) failed : %s\n", client, strerror(errno));
		return RENICE_INVALID;
	}
	return -priority;
}

/* If expected is 0 then we try to apply our renice, otherwise, we try to remove it */
void game_mode_apply_renice(const GameModeContext *self, const pid_t client, int expected)
{
	if (expected == RENICE_INVALID)
		/* Silently bail if fed an invalid value */
		return;

	GameModeConfig *config = game_mode_config_from_context(self);

	/*
	 * read configuration "renice" (1..20)
	 */
	long int renice = config_get_renice_value(config);
	if (renice == 0) {
		return;
	}

	/* Invert the renice value */
	renice = -renice;

	/* When expected is non-zero, we should try and remove the renice only if it doesn't match the
	 * expected value */
	if (expected != 0) {
		expected = (int)renice;
		renice = 0;
	}

	/* Open the tasks dir for the client */
	char tasks[128];
	snprintf(tasks, sizeof(tasks), "/proc/%d/task", client);
	DIR *client_task_dir = opendir(tasks);
	if (client_task_dir == NULL) {
		LOG_ERROR("Could not inspect tasks for client [%d]! Skipping ioprio optimisation.\n",
		          client);
		return;
	}

	/* Iterate for all tasks of client process */
	struct dirent *tid_entry;
	while ((tid_entry = readdir(client_task_dir)) != NULL) {
		/* Skip . and .. */
		if (tid_entry->d_name[0] == '.')
			continue;

		/* task name is the name of the file */
		int tid = atoi(tid_entry->d_name);

		/* Clear errno as -1 is a regitimate return */
		errno = 0;
		int prio = getpriority(PRIO_PROCESS, (id_t)tid);

		if (prio == -1 && errno) {
			/* Process may well have ended */
			LOG_ERROR("getpriority failed for client [%d,%d] with error: %s\n",
			          client,
			          tid,
			          strerror(errno));
		} else if (prio != expected) {
			/*
			 * Don't adjust priority if it does not match the expected value
			 * ie. Another process has changed it, or it began non-standard
			 */
			LOG_ERROR("Refused to renice client [%d,%d]: prio was (%d) but we expected (%d)\n",
			          client,
			          tid,
			          prio,
			          expected);
		} else if (setpriority(PRIO_PROCESS, (id_t)tid, (int)renice)) {
			LOG_HINTED(ERROR,
			           "Failed to renice client [%d,%d], ignoring error condition: %s\n",
			           "    -- Your user may not have permission to do this. Please read the docs\n"
			           "    -- to learn how to adjust the pam limits.\n",
			           client,
			           tid,
			           strerror(errno));
		}
	}

	closedir(client_task_dir);
}

void game_mode_apply_scheduling(const GameModeContext *self, const pid_t client)
{
	GameModeConfig *config = game_mode_config_from_context(self);

	/*
	 * read configuration "softrealtime" (on, off, auto)
	 */
	char softrealtime[CONFIG_VALUE_MAX] = { 0 };
	config_get_soft_realtime(config, softrealtime);

	/*
	 * Enable unconditionally or auto-detect soft realtime usage,
	 * auto detection is based on observations where dual-core CPU suffered
	 * priority inversion problems with the graphics driver thus running
	 * slower as a result, so enable only with more than 3 cores.
	 */
	bool enable_softrealtime = (strcmp(softrealtime, "on") == 0) ||
	                           ((strcmp(softrealtime, "auto") == 0) && (get_nprocs() > 3));

	/*
	 * Actually apply the scheduler policy if not explicitly turned off
	 */
	if (enable_softrealtime) {
		const struct sched_param p = { .sched_priority = 0 };
		if (sched_setscheduler(client, SCHED_ISO | SCHED_RESET_ON_FORK, &p)) {
			const char *hint = "";
			HINT_ONCE_ON(
			    errno == EPERM,
			    hint,
			    "    -- The error indicates that you may be running a resource management\n"
			    "    -- daemon managing your game launcher and it leaks lower scheduling\n"
			    "    -- classes into the games. This is likely a bug in the management daemon\n"
			    "    -- and not a bug in GameMode, it should be reported upstream.\n"
			    "    -- If unsure, please also look here:\n"
			    "    -- https://github.com/FeralInteractive/gamemode/issues/68\n");
			HINT_ONCE_ON(
			    errno == EINVAL,
			    hint,
			    "    -- The error indicates that your kernel may not support this. If you\n"
			    "    -- don't know what SCHED_ISO means, you can safely ignore this. If you\n"
			    "    -- expected it to work, ensure you're running a kernel with MuQSS or\n"
			    "    -- PDS scheduler.\n"
			    "    -- For further technical reading on the topic start here:\n"
			    "    -- https://lwn.net/Articles/720227/\n");
			LOG_ERROR(
			    "Failed setting client [%d] into SCHED_ISO mode, ignoring error condition: %s\n"
			    "%s",
			    client,
			    strerror(errno),
			    hint);
		}
	}
}