49 Commits

Author SHA1 Message Date
Marc Di Luzio
42dd7e6ea8 Add initial screensaver inhibiting code 2019-01-28 16:36:56 +00:00
Kai Krakow
5396370e5d refactor: Simplify the log hinter
Signed-off-by: Kai Krakow <kai@kaishome.de>
2018-10-09 00:33:46 +02:00
Kai Krakow
f4cd01f989 refactor: Break wine API functions out of the main daemon source
Signed-off-by: Kai Krakow <kai@kaishome.de>
2018-10-09 00:33:46 +02:00
Kai Krakow
403ce122f6 refactor: Break ioprio API functions out of the main daemon source
Signed-off-by: Kai Krakow <kai@kaishome.de>
2018-10-09 00:33:46 +02:00
Kai Krakow
edf9257de4 refactor: Break sched API functions out of the main daemon source
Signed-off-by: Kai Krakow <kai@kaishome.de>
2018-10-09 00:33:46 +02:00
Kai Krakow
b9c9a5f120 refactor: Avoid exposing various internal types
Signed-off-by: Kai Krakow <kai@kaishome.de>
2018-10-09 00:33:46 +02:00
Kai Krakow
92967b135b refactor: Break env API functions out of the main daemon source
Signed-off-by: Kai Krakow <kai@kaishome.de>
2018-10-09 00:33:46 +02:00
Kai Krakow
748808be7e refactor: Break proc API functions out of the main daemon source
Signed-off-by: Kai Krakow <kai@kaishome.de>
2018-10-09 00:33:46 +02:00
Kai Krakow
c4ff3d7250 refactor: Break helpers out of the main daemon source
Signed-off-by: Kai Krakow <kai@kaishome.de>
2018-10-08 20:53:04 +02:00
Alex Smith
9bbcc03c50 Merge pull request #84 from kakra/improve-scheduler-logging
gamemode-scheduler: Improve logging
2018-10-08 17:30:06 +01:00
Kai Krakow
29f8f0883c gamemode: Explain the concerning logs a little better
Testing showed that wine processes may provoke some concerning logs
multiple times. Let's explain this a little more so no questions from
worried users show up.

Signed-off-by: Kai Krakow <kai@kaishome.de>
2018-10-05 20:03:17 +02:00
Kai Krakow
fd4166279b gamemode: Convert game_mode_context_has_client() to return the client
Returning a bool is not useful if we want to create some log information
from this. It now returns NULL or a client pointer which is compatible
with all of the current users.

When dereferencing the returned pointer, a read lock must be acquired
around using the returned result as the client may be deallocated from
heap otherwise.

Signed-off-by: Kai Krakow <kai@kaishome.de>
2018-10-05 20:03:17 +02:00
Kai Krakow
d023495a5d gamemode: Add a tiny buffered snprintf helper
Be careful using this as it may introduce some non-obvious pitfalls.
This saves us from a heap allocation in some code locations.

Signed-off-by: Kai Krakow <kai@kaishome.de>
2018-10-05 20:03:17 +02:00
Kai Krakow
4fd93ee30a gamemode: Map wine preloaders to original exe
This commit hooks game_mode_resolve_wine_preloader() into the function
to lookup the exe from a PID.

Signed-off-by: Kai Krakow <kai@kaishome.de>
2018-10-05 19:54:40 +02:00
Kai Krakow
ba80a6cc3d gamemode: Add function to resolve the wine preloader executable
This commit adds a function to resolve a wine preloader binary into its
original windows binary.

Signed-off-by: Kai Krakow <kai@kaishome.de>
2018-10-05 19:54:40 +02:00
Kai Krakow
373fe5a8af gamemode: Add a helper to compare string tails
Signed-off-by: Kai Krakow <kai@kaishome.de>
2018-10-05 19:54:40 +02:00
Kai Krakow
c2344f4387 gamemode: Add function to search environment of a PID
This function can look up arbitrary environment variables from a running
PID (given we have access permissions which should be usually true).

Signed-off-by: Kai Krakow <kai@kaishome.de>
2018-10-05 19:54:40 +02:00
Kai Krakow
47ea4514cd gamemode: Add function to lookup user home directory
We first try to use `$HOME`, and only then fall back to passwd lookup.

Signed-off-by: Kai Krakow <kai@kaishome.de>
2018-10-05 19:54:40 +02:00
Kai Krakow
3a6d258eae gamemode: Make game_mode_context_find_exe() thread-safe
Let's use our new safe_snprintf() helper. It's designed to make
thread-safe usage easy and will also be used by the next commits.

Reported-by: Alex Smith <alex@alex-smith.me.uk>
Signed-off-by: Kai Krakow <kai@kaishome.de>
2018-10-05 19:54:40 +02:00
Kai Krakow
81a52ddc4d gamemode: Add a tiny safe snprintf helper
Give it a buffer, it returns an allocated string respecting the buffer
size.

Signed-off-by: Kai Krakow <kai@kaishome.de>
2018-10-05 19:54:23 +02:00
Kai Krakow
bd811c6646 gamemode: Add executable to initializer
This allows to initialize the GameModeClient later and cleans up the
error path.

Signed-off-by: Kai Krakow <kai@kaishome.de>
2018-10-05 19:45:31 +02:00
Kai Krakow
8214f93630 gamemode: Optimize detection of dupe registers
GameMode can do a pretty expensive lookup function now for the exe.
Let's spare some CPU cycles by detecting a duplicate PID early. Nothing
makes use of the exe path at this stage.

Signed-off-by: Kai Krakow <kai@kaishome.de>
2018-10-05 19:45:31 +02:00
Kai Krakow
2a0c6e7098 gamemode: Never add NULL to the client list
When running wine games, there's good chance the executable is already
gone when GameMode decided to add it to the list. Let's silently bail
out here. It probably grabs a non-matching PID anyway in this moment.

Signed-off-by: Kai Krakow <kai@kaishome.de>
2018-10-05 19:45:31 +02:00
Kai Krakow
8a618a29fa gamemode-scheduler: Improve logging
During finding the cause of an issue, we discovered bad interactions
with other resource management daemon that may be running at the same
time and may interfere with GameMode's ability to properly set
scheduling parameters of the process. Usually, this results in EPERM
(instead of EINVAL for lacking kernel support). It also means that the
other daemon is leaking wrong scheduling settings into the games with
negative performance impacts (quite the opposite of what the user wanted
to achieve). So we should give some valuable hints.

This commit improves logging by detecting the error situation and giving
context-based hints on what to do next. To keep logging verbosity low,
the hint is only logged once per error code.

Closes: https://github.com/FeralInteractive/gamemode/issues/68
Signed-off-by: Kai Krakow <kai@kaishome.de>
2018-10-05 19:44:58 +02:00
Kai Krakow
62865b1e32 gamemode: Apply io priority to clients
This commit allows changing the io priority of the client to a value
specified in the configuration file. That can possibly reduce lags or
latency when a game has to load assets on demand and you have background
IO activity running (or other concurrent IO).

Signed-off-by: Kai Krakow <kai@kaishome.de>
2018-10-03 04:24:56 +02:00
Kai Krakow
a91a2e7739 gamemode: Add a value clamping helper
Signed-off-by: Kai Krakow <kai@kaishome.de>
2018-09-30 17:36:34 +02:00
Kai Krakow
5e4d5fb2e7 gamemode: Don't leak SCHED_ISO into children
We should not leak `SCHED_ISO` into children processes. This is obviously
a no-op if launchers use the `LD_PRELOAD` method because every child would
also preload the gamemode library (except they patch the environment
before forking).

But for games supporting gamemode natively, this prevents leaking the
scheduler settings into child processes which is important because it
children may create high CPU usage which counterfeits the original idea
of this.

Apparently, this won't work for the nice value except we would intercept
the libc forking functions (which would be possible but not very
transparent and prone to compatibility problems). However, if we
implemented it, it shouldn't be part of this commit anyway.

Signed-off-by: Kai Krakow <kai@kaishome.de>
2018-09-24 20:31:17 +02:00
Alex Smith
22a90a73ef Fix formatting 2018-08-04 11:05:33 +01:00
Alex Smith
442475a78b Make error messages for failure to change scheduling policies more descriptive
Out of the box on most distros, both of these steps will fail (renicing
requires permission which may need adjustment to limits.conf, and the
upstream kernel does not support SCHED_ISO).

Explicitly state this in the error messages to hopefully reduce user
confusion as to why these might be failing.
2018-08-04 10:54:59 +01:00
Matthias Gerstner
39d7b95a26 game_mode_context_register: fix memory leak on error conditions
Clients can reach this memory leak by trying to unregister non-existing
clients via D-Bus.
2018-07-30 17:29:55 +02:00
Kai Krakow
6f93020d0b scheduler: Apply renice configuration to clients
This commit applies the configured nice value to the client. It accepts
values from 1 to 20, the negated value is applied as a nice value.
Negation was chosen due to limits of the configuration parser. Since low
priority values (0 to 19) make no sense in the scope of GameMode, this
is a safe approach.

Signed-off-by: Kai Krakow <kai@kaishome.de>
2018-06-10 09:36:21 +02:00
Kai Krakow
addfe1fbc0 scheduler: Add heuristics and option to force on/off
This commit adds a simple heuristic to auto detect whether to use
SCHED_ISO or not. It does this by looking at the number of cores:

According to some reports by users of SCHED_ISO and an explanation by
Con Kolivas, games running busy loops and running on too few cores might
start fighting with scheduling of the graphic driver, leading to
priority inversion. This results in actually lower performance.

So let's only enable SCHED_ISO on at least four cores.

The user can still force SCHED_ISO on or off by setting the
configuration value. All other values (or none) will apply heuristics.

Signed-off-by: Kai Krakow <kai@kaishome.de>
2018-05-24 22:41:04 +02:00
Kai Krakow
d8231a2972 scheduler: Don't renice if already reniced
This is a heuristic to not apply a nice level if the user already did
that (or the process itself).

Signed-off-by: Kai Krakow <kai@kaishome.de>
2018-05-24 22:41:04 +02:00
Kai Krakow
76ece2fc26 Leverage soft real time capabilities
Kernels that support SCHED_ISO scheduling policy can give processes soft
real time support. This improves latency without compromising system
stability. See https://lwn.net/Articles/720227/.

This commit adds support for setting this policy with a safe fall back if
kernel support is lacking by just ignoring the error condition.

Additionally, it also tries to raise the nice priority of the game to -4
to give it a slight IO and CPU priority over other background processes.
This needs PAM adjustments to allow users raising priority to certain
levels. If it doesn't work, the fall back strategy is also ignoring the
error condition. See /etc/security/limits.conf.

Kernels that currently support SCHED_ISO include kernels with Con
Kolivas MuQSS patchset (likely the CK patchset). This patchset is
generally recommended for desktop machines but usually not found in
standard distribution kernels due to lack of widespread stability tests.

Signed-off-by: Kai Krakow <kai@kaishome.de>
2018-05-23 22:23:42 +02:00
Marc Di Luzio
5ebc77a0f1 Add defaultgov and desiredgov config settings
Allows users to choose which governor settings they want.

	Should provide some future compatibility for other governor settings.
2018-05-15 17:04:31 +01:00
Marc Di Luzio
20828da140 Refactor getting config values to stop duplicating the rwlock 2018-05-15 17:04:20 +01:00
Marc Di Luzio
1c38a6047e Refactor to store the initial governor state when gamemode is entered
Rather than when gamemoded is started. As see in Issue #52.

	A small refactor here to ensure things stay consistent, especially
	to stop storing a pointer from get_gov_state that might have had
	it's memory changed at some point, confusingly.
2018-05-15 16:37:09 +01:00
Marc Di Luzio
4f3bc2c9a2 Add gamemode_query_status and teach gamemoded '-s'
This allows the client to query the daemon about the status of gamemode.

	Returns the following:
		 0 if gamemode is inactive
		 1 if gamemode is active
		 2 if gamemode is active and this client is registered
		-1 if the query failed

	Passing -s to gamemoded will simply query and print the current status.

	Allows for more comprehensive testing when using 'gamemoded -r' as well as more reactionary program behaviour
2018-04-25 14:51:38 +01:00
Marc Di Luzio
db8a70b7ba Add support for user defined local script plugins
A much requested feature, this allows for providing custom scripts in the config file. An example in the man page is below and would trigger both a system notification, and allow control over a background crypto mining script automatically in gamemode.

	[custom]
	; Custom scripts (executed using the shell) when gamemode starts and ends
	start=notify-send "GameMode started"
	    /home/me/bin/stop_ethmining.sh

	end=notify-send "GameMode ended"
	    /home/me/bin/start_ethmining.sh

	Scripts are run with system() and do not have any special privilages, as with the rest of the daemon, custom scripts that require root will need their own permissions set up externally.

	This commit also renames two defines as they needed to be moved to the public interface.
2018-04-24 16:59:17 +01:00
Marc Di Luzio
51e1e7203f Add "reaper_freq" config setting in the "general" section
This simply allows control over the reaper thread frequency.

	Defaults to 5 as before.
2018-04-24 14:37:39 +01:00
Marc Di Luzio
52a99a2102 Apply clang-format to commit d26ac60 2018-04-16 17:46:32 +01:00
OlliC
6b71edf740 Integrated sd_notify to get status messages in systemd (#28)
* Integrated sd_notify to get status messages in systemd
* reset color attributes after use in string
2018-04-16 17:04:41 +01:00
Ikey Doherty
69db9c2a61 Conformance: Update copyright range to include 2018
Signed-off-by: Ikey Doherty <ikey@solus-project.com>
2018-04-10 13:23:38 +01:00
Marc Di Luzio
759cbc3c40 Add config file parsing
Checks for a gamemode.ini in /usr/share/gamemode/ (or in the cwd for debugging)

	Currently allows for blacklisting and whitelisting clients based on rudimentary needle-haystack executable name checks

	See the example/gamemode.ini file for expected syntax

	Using the BSD licensed inih library (with additional meson.build file)
2018-03-23 16:59:15 +00:00
Marc Di Luzio
7a3ae91781 Call pthread_rwlock_init on the rwlock
The spec says the results are undefined if an uninitialised
	read-write lock is used without being initialised, though
	evidently things were working out alright
2018-03-23 16:59:15 +00:00
Ikey Doherty
68e326de60 Transform into a full D-BUS service with Polkit support
Primarily we convert the service into a thread safe one that isn't reliant
on signaling for control flow, eliminating data race conditions. We also
enable interleaving by separating game mode pivoting from explicit client
registration.

The static pid list is now converted into a dynamic list that is OOM safe
to store all registered clients (with a reasonable upper limit of 256 clients)
to better handle cases where LD_PRELOAD is used for a large process group.
Additionally we begin storing some metadata on the connected clients such
as their executable path, which will enable us to perform some basic
whitelisting in future.

The cpugovctl binary is now moved into the libexecdir as an explicit helper
of the D-BUS service, using the shared library to merge some code back into
the daemon. This saves having to execute a process to query the state of the
governors, as we don't need a privileged client to do this.

In order to sanely set the governors, we require that the binary is running
as euid 0, and execute this using `pkexec`. A PolKit policy definition is
provided which allows active/logged in users to execute this helper through
a path whitelist. As such we can convert the daemon into user-mode only, with
the privileged helper being dispatched exclusively via polkit. This removes
the need for a setuid helper or having a system mode daemon.

Lastly we clean up the codebase a bit to be consistent with modern C code
conventions, using pragmas where available. The library component still uses
the older ifdef approach to support older compilers, but the daemon portion
uses the directive to simplify intent and speed up compilation. Additionally
we move all comments to C style comments for consistency, instead of mixing
in C++ style single line comments, in order to establish a formal coding
style.

The net result is a more robust service which can be D-BUS activated when
clients need it, that can perform scaling automatically without harassing
the user with authentication popups.

Signed-off-by: Ikey Doherty <ikey@solus-project.com>
2018-03-05 17:32:25 +00:00
Ikey Doherty
400dcb9c53 Fix compilation on newer Linux systems
Defining _GNU_SOURCE will set the appropriate POSIX levels required to
use the `_MAX`, `kill`, `realpath`, etc, set of POSIX family functions
and defines.

Signed-off-by: Ikey Doherty <ikey@solus-project.com>
2018-03-05 17:32:02 +00:00
Marc Di Luzio
2bbaab129b Apply clang format to files
Also add brackets for all scopes at the same time
2018-03-05 17:32:01 +00:00
Marc Di Luzio
c459c05076 Initial 0.1 commit
This is a small daemon/libary to enable games to request a
performance "game mode" from the system.

Currently only implemented to upgrade the CPU governor and
automatically downgrade it once done.

A game only needs to load libgamemodeauto to activate the request.
Which means an LD_PRELOAD can be applied to any game to activate
the mode as needed.

However gamemode_client.h can be used to manually activate/deactivate
the mode as needed, and also perform error checking.

The libs are miminal loaders to call into an installed libgamemode
which, if the daemon is installed and running, will send through
the equivelant game mode request, and magic will happen.

See the README.md for more details.

Currently licensed under the BSD 3 clause license.
2018-03-05 17:32:01 +00:00