Restructure files and libraries

Rename a bunch of files to make the consistent
	Create two new subdirectories for common code, and utilities
This commit is contained in:
Marc Di Luzio
2019-05-22 18:55:52 +01:00
parent 41988b7f1c
commit 1b78d0dcf7
28 changed files with 151 additions and 174 deletions

View File

@ -1,93 +0,0 @@
/*
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 "governors-query.h"
#include "logging.h"
#include <ctype.h>
#include <errno.h>
#include <sys/types.h>
/**
* Sets all governors to a value
*/
static int set_gov_state(const char *value)
{
char governors[MAX_GOVERNORS][MAX_GOVERNOR_LENGTH] = { { 0 } };
int num = fetch_governors(governors);
int retval = EXIT_SUCCESS;
int res = 0;
for (int i = 0; i < num; i++) {
const char *gov = governors[i];
FILE *f = fopen(gov, "w");
if (!f) {
LOG_ERROR("Failed to open file for write %s\n", gov);
continue;
}
res = fprintf(f, "%s\n", value);
if (res < 0) {
LOG_ERROR("Failed to set governor %s to %s: %s", gov, value, strerror(errno));
retval = EXIT_FAILURE;
}
fclose(f);
}
return retval;
}
/**
* Main entry point, dispatch to the appropriate helper
*/
int main(int argc, char *argv[])
{
if (argc == 2 && strncmp(argv[1], "get", 3) == 0) {
printf("%s", get_gov_state());
} else if (argc == 3 && strncmp(argv[1], "set", 3) == 0) {
const char *value = argv[2];
/* Must be root to set the state */
if (geteuid() != 0) {
LOG_ERROR("This program must be run as root\n");
return EXIT_FAILURE;
}
return set_gov_state(value);
} else {
fprintf(stderr, "usage: cpugovctl [get] [set VALUE]\n");
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}

View File

@ -1,51 +0,0 @@
/*
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.
*/
#pragma once
#include <stdbool.h>
#include "gamemode.h"
/**
* Run the main D-BUS loop "forever"
*/
void game_mode_context_loop(GameModeContext *context) __attribute__((noreturn));
/**
* Inhibit the screensaver
*/
int game_mode_inhibit_screensaver(bool inhibit);
/**
* Signal the ClientCount property has changed
*/
void game_mode_client_count_changed(void);

View File

@ -1,191 +0,0 @@
/*
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 "external-helper.h"
#include "logging.h"
#include <linux/limits.h>
#include <stdio.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
static const int DEFAULT_TIMEOUT = 5;
static int read_child_stdout(int pipe_fd, char buffer[EXTERNAL_BUFFER_MAX], int tsec)
{
fd_set fds;
struct timeval timeout;
int num_readable = 0;
ssize_t buffer_bytes_read = 0;
ssize_t just_read = 0;
bool buffer_full = false;
char discard_buffer[EXTERNAL_BUFFER_MAX];
/* Set up the timout */
timeout.tv_sec = tsec;
timeout.tv_usec = 0;
FD_ZERO(&fds);
/* Wait for the child to finish up with a timout */
while (true) {
FD_SET(pipe_fd, &fds);
num_readable = select(pipe_fd + 1, &fds, NULL, NULL, &timeout);
if (num_readable < 0) {
if (errno == EINTR) {
continue;
} else {
LOG_ERROR("sigtimedwait failed: %s\n", strerror(errno));
return -1;
}
} else if (num_readable == 0) {
return -2;
}
if (!buffer_full) {
just_read = read(pipe_fd,
buffer + buffer_bytes_read,
EXTERNAL_BUFFER_MAX - (size_t)buffer_bytes_read - 1);
} else {
just_read = read(pipe_fd, discard_buffer, EXTERNAL_BUFFER_MAX - 1);
}
if (just_read < 0) {
return -1;
} else if (just_read == 0) {
// EOF encountered
break;
}
if (!buffer_full) {
buffer_bytes_read += just_read;
if (buffer_bytes_read == EXTERNAL_BUFFER_MAX - 1) {
// our buffer is exhausted, discard the rest
// of the output
buffer_full = true;
}
}
}
buffer[buffer_bytes_read] = 0;
return 0;
}
/**
* Call an external process
*/
int run_external_process(const char *const *exec_args, char buffer[EXTERNAL_BUFFER_MAX], int tsec)
{
pid_t p;
int status = 0;
int pipes[2];
int ret = 0;
char internal[EXTERNAL_BUFFER_MAX] = { 0 };
if (pipe(pipes) == -1) {
LOG_ERROR("Could not create pipe: %s!\n", strerror(errno));
return -1;
}
/* Set the default timeout */
if (tsec == -1) {
tsec = DEFAULT_TIMEOUT;
}
if ((p = fork()) < 0) {
close(pipes[0]);
close(pipes[1]);
LOG_ERROR("Failed to fork(): %s\n", strerror(errno));
return false;
} else if (p == 0) {
/* Send STDOUT to the pipe */
dup2(pipes[1], STDOUT_FILENO);
close(pipes[0]);
close(pipes[1]);
/* Execute the command */
/* Note about cast:
* The statement about argv[] and envp[] being constants is
* included to make explicit to future writers of language
* bindings that these objects are completely constant.
* http://pubs.opengroup.org/onlinepubs/9699919799/functions/exec.html
*/
if (execv(exec_args[0], (char *const *)exec_args) != 0) {
LOG_ERROR("Failed to execute external process: %s %s\n", exec_args[0], strerror(errno));
exit(EXIT_FAILURE);
}
// should never be reached
abort();
}
// close the write end of the pipe so we get signaled EOF once the
// child exits
close(pipes[1]);
ret = read_child_stdout(pipes[0], internal, tsec);
close(pipes[0]);
if (ret != 0) {
if (ret == -2) {
LOG_ERROR("Child process timed out for %s, killing and returning\n", exec_args[0]);
kill(p, SIGKILL);
} else {
LOG_ERROR("Failed to read from process %s: %s\n", exec_args[0], strerror(errno));
}
if (buffer) {
// make sure the buffer is a terminated empty string on error
buffer[0] = 0;
}
} else if (buffer) {
memcpy(buffer, internal, EXTERNAL_BUFFER_MAX);
}
if (waitpid(p, &status, 0) < 0) {
LOG_ERROR("Failed to waitpid(%d): %s\n", (int)p, strerror(errno));
return -1;
}
/* i.e. sigsev */
if (!WIFEXITED(status)) {
LOG_ERROR("Child process '%s' exited abnormally\n", exec_args[0]);
} else if (WEXITSTATUS(status) != 0) {
LOG_ERROR("External process failed with exit code %d\n", WEXITSTATUS(status));
LOG_ERROR("Output was: %s\n", internal);
return -1;
}
return 0;
}

View File

@ -1,37 +0,0 @@
/*
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.
*/
#pragma once
#define EXTERNAL_BUFFER_MAX 1024
/* Run an external process and capture the return value */
int run_external_process(const char *const *exec_args, char buffer[EXTERNAL_BUFFER_MAX], int tsec);

View File

@ -30,9 +30,10 @@ POSSIBILITY OF SUCH DAMAGE.
*/
#define _GNU_SOURCE
#include "daemon_config.h"
#include "helpers.h"
#include "logging.h"
#include "gamemode-config.h"
#include "common-helpers.h"
#include "common-logging.h"
/* Ben Hoyt's inih library */
#include "ini.h"

View File

@ -31,14 +31,14 @@ POSSIBILITY OF SUCH DAMAGE.
#define _GNU_SOURCE
#include "common-external.h"
#include "common-governors.h"
#include "common-helpers.h"
#include "common-logging.h"
#include "gamemode-config.h"
#include "gamemode.h"
#include "config.h"
#include "daemon_config.h"
#include "dbus_messaging.h"
#include "external-helper.h"
#include "governors-query.h"
#include "helpers.h"
#include "logging.h"
#include "build-config.h"
#include <fcntl.h>
#include <pthread.h>

View File

@ -31,9 +31,8 @@ POSSIBILITY OF SUCH DAMAGE.
#define _GNU_SOURCE
#include "dbus_messaging.h"
#include "common-logging.h"
#include "gamemode.h"
#include "logging.h"
#include <stdlib.h>
@ -219,21 +218,22 @@ static int method_refresh_config(sd_bus_message *m, void *userdata,
/**
* D-BUS vtable to dispatch virtual methods
*/
static const sd_bus_vtable gamemode_vtable[] =
{ SD_BUS_VTABLE_START(0),
SD_BUS_PROPERTY("ClientCount", "i", property_get_client_count, 0,
SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
SD_BUS_METHOD("RegisterGame", "i", "i", method_register_game, SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD("UnregisterGame", "i", "i", method_unregister_game, SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD("QueryStatus", "i", "i", method_query_status, SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD("RegisterGameByPID", "ii", "i", method_register_game_by_pid,
SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD("UnregisterGameByPID", "ii", "i", method_unregister_game_by_pid,
SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD("QueryStatusByPID", "ii", "i", method_query_status_by_pid,
SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD("RefreshConfig", "", "i", method_refresh_config, SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_VTABLE_END };
static const sd_bus_vtable gamemode_vtable[] = {
SD_BUS_VTABLE_START(0),
SD_BUS_PROPERTY("ClientCount", "i", property_get_client_count, 0,
SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
SD_BUS_METHOD("RegisterGame", "i", "i", method_register_game, SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD("UnregisterGame", "i", "i", method_unregister_game, SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD("QueryStatus", "i", "i", method_query_status, SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD("RegisterGameByPID", "ii", "i", method_register_game_by_pid,
SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD("UnregisterGameByPID", "ii", "i", method_unregister_game_by_pid,
SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD("QueryStatusByPID", "ii", "i", method_query_status_by_pid,
SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD("RefreshConfig", "", "i", method_refresh_config, SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_VTABLE_END
};
/**
* Main process loop for the daemon. Run until quitting has been requested.

View File

@ -32,15 +32,14 @@ POSSIBILITY OF SUCH DAMAGE.
#define _GNU_SOURCE
#include "config.h"
#include "external-helper.h"
#include "helpers.h"
#include "logging.h"
#include "common-external.h"
#include "common-gpu.h"
#include "common-helpers.h"
#include "common-logging.h"
#include "gamemode-config.h"
#include "gamemode.h"
#include "daemon_config.h"
#include "gpu-control.h"
#include "build-config.h"
/**
* Attempts to identify the current in use GPU information

View File

@ -31,10 +31,10 @@ POSSIBILITY OF SUCH DAMAGE.
#define _GNU_SOURCE
#include "daemon_config.h"
#include "common-helpers.h"
#include "common-logging.h"
#include "gamemode-config.h"
#include "gamemode.h"
#include "helpers.h"
#include "logging.h"
#include <dirent.h>
#include <errno.h>

View File

@ -31,9 +31,9 @@ POSSIBILITY OF SUCH DAMAGE.
#define _GNU_SOURCE
#include "daemon_config.h"
#include "common-logging.h"
#include "gamemode-config.h"
#include "gamemode.h"
#include "logging.h"
#include <dirent.h>
#include <errno.h>

View File

@ -31,9 +31,14 @@ POSSIBILITY OF SUCH DAMAGE.
#define _GNU_SOURCE
#include "common-external.h"
#include "common-governors.h"
#include "common-gpu.h"
#include "common-helpers.h"
#include "common-logging.h"
#include "gamemode-config.h"
#include "gamemode.h"
#include "helpers.h"
#include "logging.h"
#include "gamemode_client.h"
#include <libgen.h>
#include <pthread.h>
@ -42,11 +47,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include <sys/wait.h>
#include <unistd.h>
#include "daemon_config.h"
#include "external-helper.h"
#include "gamemode_client.h"
#include "governors-query.h"
#include "gpu-control.h"
struct GameModeConfig;
/* Initial verify step to ensure gamemode isn't already active */
static int verify_gamemode_initial(struct GameModeConfig *config)

View File

@ -31,9 +31,9 @@ POSSIBILITY OF SUCH DAMAGE.
#define _GNU_SOURCE
#include "common-helpers.h"
#include "common-logging.h"
#include "gamemode.h"
#include "helpers.h"
#include "logging.h"
#include <ctype.h>
#include <fcntl.h>

View File

@ -150,3 +150,18 @@ int game_mode_initialise_gpu(GameModeConfig *config, GameModeGPUInfo **info);
void game_mode_free_gpu(GameModeGPUInfo **info);
int game_mode_apply_gpu(const GameModeGPUInfo *info);
int game_mode_get_gpu(GameModeGPUInfo *info);
/**
* Run the main D-BUS loop "forever"
*/
void game_mode_context_loop(GameModeContext *context) __attribute__((noreturn));
/**
* Inhibit the screensaver
*/
int game_mode_inhibit_screensaver(bool inhibit);
/**
* Signal the ClientCount property has changed
*/
void game_mode_client_count_changed(void);

View File

@ -49,11 +49,13 @@ POSSIBILITY OF SUCH DAMAGE.
#define _GNU_SOURCE
#include "config.h"
#include "dbus_messaging.h"
#include "common-logging.h"
#include "gamemode-config.h"
#include "gamemode.h"
#include "gamemode_client.h"
#include "logging.h"
#include "build-config.h"
#include <fcntl.h>
#include <getopt.h>

View File

@ -1,149 +0,0 @@
/*
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 "governors-query.h"
#include "logging.h"
#include <assert.h>
#include <glob.h>
#include <stdio.h>
#include <string.h>
/**
* Discover all governers on the system.
*
* Located at /sys/devices/system/cpu/cpu(*)/cpufreq/scaling_governor
*/
int fetch_governors(char governors[MAX_GOVERNORS][MAX_GOVERNOR_LENGTH])
{
glob_t glo = { 0 };
static const char *path = "/sys/devices/system/cpu/cpu*/cpufreq/scaling_governor";
/* Assert some sanity on this glob */
if (glob(path, GLOB_NOSORT, NULL, &glo) != 0) {
LOG_ERROR("glob failed for cpu governors: (%s)\n", strerror(errno));
return 0;
}
if (glo.gl_pathc < 1) {
globfree(&glo);
LOG_ERROR("no cpu governors found\n");
return 0;
}
int num_governors = 0;
/* Walk the glob set */
for (size_t i = 0; i < glo.gl_pathc; i++) {
if (i >= MAX_GOVERNORS) {
break;
}
/* Get the real path to the file.
* Traditionally cpufreq symlinks to a policy directory that can
* be shared, so let's prevent duplicates.
*/
char fullpath[PATH_MAX] = { 0 };
const char *ptr = realpath(glo.gl_pathv[i], fullpath);
if (fullpath != ptr) {
continue;
}
/* Only add this governor if it is unique */
for (int j = 0; j < num_governors; j++) {
if (strncmp(fullpath, governors[i], PATH_MAX) == 0) {
continue;
}
}
/* Copy this governor into the output set */
static_assert(MAX_GOVERNOR_LENGTH > PATH_MAX, "possible string truncation");
strncpy(governors[num_governors], fullpath, MAX_GOVERNOR_LENGTH);
num_governors++;
}
globfree(&glo);
return num_governors;
}
/**
* Return the current governor state
*/
const char *get_gov_state(void)
{
/* Persistent governor state */
static char governor[64] = { 0 };
memset(governor, 0, sizeof(governor));
/* State for all governors */
char governors[MAX_GOVERNORS][MAX_GOVERNOR_LENGTH] = { { 0 } };
int num = fetch_governors(governors);
/* Check the list */
for (int i = 0; i < num; i++) {
const char *gov = governors[i];
FILE *f = fopen(gov, "r");
if (!f) {
LOG_ERROR("Failed to open file for read %s\n", gov);
continue;
}
/* Grab the file length */
fseek(f, 0, SEEK_END);
long length = ftell(f);
fseek(f, 0, SEEK_SET);
char contents[length];
if (fread(contents, 1, (size_t)length, f) > 0) {
/* Files have a newline */
strtok(contents, "\n");
if (strlen(governor) > 0 && strncmp(governor, contents, 64) != 0) {
/* Don't handle the mixed case, this shouldn't ever happen
* But it is a clear sign we shouldn't carry on */
LOG_ERROR("Governors malformed: got \"%s\", expected \"%s\"", contents, governor);
fclose(f);
return "malformed";
}
strncpy(governor, contents, sizeof(governor) - 1);
} else {
LOG_ERROR("Failed to read contents of %s\n", gov);
}
fclose(f);
}
return governor;
}

View File

@ -1,47 +0,0 @@
/*
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.
*/
#pragma once
#include <linux/limits.h>
#define MAX_GOVERNORS 128
#define MAX_GOVERNOR_LENGTH PATH_MAX + 1
/**
* Grab all of the governors
*/
int fetch_governors(char governors[MAX_GOVERNORS][MAX_GOVERNOR_LENGTH]);
/**
* Get the current governor state
*/
const char *get_gov_state(void);

View File

@ -1,75 +0,0 @@
/*
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.
*/
#include "gpu-control.h"
#include "logging.h"
#include <stdio.h>
/* Get the vendor for a device */
enum GPUVendor gamemode_get_gpu_vendor(long device)
{
enum GPUVendor vendor = Vendor_Invalid;
/* Fill in GPU vendor */
char path[64] = { 0 };
if (snprintf(path, 64, "/sys/class/drm/card%ld/device/vendor", device) < 0) {
LOG_ERROR("snprintf failed, will not apply gpu optimisations!\n");
return Vendor_Invalid;
}
FILE *file = fopen(path, "r");
if (!file) {
LOG_ERROR("Couldn't open vendor file at %s, will not apply gpu optimisations!\n", path);
return Vendor_Invalid;
}
char buff[64];
bool got_line = fgets(buff, 64, file) != NULL;
fclose(file);
if (got_line) {
vendor = strtol(buff, NULL, 0);
} else {
LOG_ERROR("Coudn't read contents of file %s, will not apply optimisations!\n", path);
return Vendor_Invalid;
}
/* verify GPU vendor */
if (!GPUVendorValid(vendor)) {
LOG_ERROR("Unknown vendor value (0x%04x) found, cannot apply optimisations!\n",
(unsigned int)vendor);
LOG_ERROR("Known values are: 0x%04x (NVIDIA) 0x%04x (AMD) 0x%04x (Intel)\n",
(unsigned int)Vendor_NVIDIA,
(unsigned int)Vendor_AMD,
(unsigned int)Vendor_Intel);
return Vendor_Invalid;
}
return vendor;
}

View File

@ -1,59 +0,0 @@
/*
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.
*/
#pragma once
#include "daemon_config.h"
/* Enums for GPU vendors */
enum GPUVendor {
Vendor_Invalid = 0,
Vendor_NVIDIA = 0x10de,
Vendor_AMD = 0x1002,
Vendor_Intel = 0x8086
};
#define GPUVendorValid(vendor) \
(vendor == Vendor_NVIDIA || vendor == Vendor_AMD || vendor == Vendor_Intel)
/* Storage for GPU info*/
struct GameModeGPUInfo {
long vendor;
long device; /* path to device, ie. /sys/class/drm/card#/ */
long nv_core; /* Nvidia core clock */
long nv_mem; /* Nvidia mem clock */
long nv_powermizer_mode; /* NV Powermizer Mode */
char amd_performance_level[CONFIG_VALUE_MAX]; /* The AMD performance level set to */
};
/* Get the vendor for a device */
enum GPUVendor gamemode_get_gpu_vendor(long device);

View File

@ -1,486 +0,0 @@
/*
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 "logging.h"
#include "external-helper.h"
#include "gpu-control.h"
#include <limits.h>
/* NV constants */
#define NV_CORE_OFFSET_ATTRIBUTE "GPUGraphicsClockOffset"
#define NV_MEM_OFFSET_ATTRIBUTE "GPUMemoryTransferRateOffset"
#define NV_POWERMIZER_MODE_ATTRIBUTE "GPUPowerMizerMode"
#define NV_PERFMODES_ATTRIBUTE "GPUPerfModes"
#define NV_PCIDEVICE_ATTRIBUTE "PCIDevice"
#define NV_ATTRIBUTE_FORMAT "[gpu:%ld]/%s"
#define NV_PERF_LEVEL_FORMAT "[%ld]"
#define NV_ARG_MAX 128
/* AMD constants */
#define AMD_DRM_PATH "/sys/class/drm/card%ld/device/%s"
/* Plausible extras to add:
* Intel support - https://blog.ffwll.ch/2013/03/overclocking-your-intel-gpu-on-linux.html
* AMD - Allow setting fan speed as well
* Store baseline values with get_gpu_state to apply when leaving gamemode
*/
/* Helper to quit with usage */
static const char *usage_text =
"usage: gpuclockctl DEVICE {arg}\n\t\tget - return current values\n\t\tset [NV_CORE NV_MEM "
"NV_POWERMIZER_MODE | AMD_PERFORMANCE_LEVEL] - set current values";
static void print_usage_and_exit(void)
{
fprintf(stderr, "%s\n", usage_text);
exit(EXIT_FAILURE);
}
static const char *get_nv_attr(const char *attr)
{
static char out[EXTERNAL_BUFFER_MAX];
const char *exec_args[] = { "/usr/bin/nvidia-settings", "-q", attr, "-t", NULL };
if (run_external_process(exec_args, out, -1) != 0) {
LOG_ERROR("Failed to get %s!\n", attr);
out[0] = 0;
return NULL;
}
return &out[0];
}
static int set_nv_attr(const char *attr)
{
const char *exec_args_core[] = { "/usr/bin/nvidia-settings", "-a", attr, NULL };
if (run_external_process(exec_args_core, NULL, -1) != 0) {
LOG_ERROR("Failed to set %s!\n", attr);
return -1;
}
return 0;
}
/* Get the nvidia driver index for the current GPU */
static long get_gpu_index_id_nv(struct GameModeGPUInfo *info)
{
if (info->vendor != Vendor_NVIDIA)
return -1;
/* NOTE: This is currently based off of a best guess of how the NVidia gpu index works
* ie. that the index is simply the index into available NV gpus in the same order as drm
* If that is not the case then this may fail to discern the correct GPU
*/
int device = 0;
int nv_device = -1;
while (device <= info->device) {
/* Get the vendor for each gpu sequentially */
enum GPUVendor vendor = gamemode_get_gpu_vendor(device++);
switch (vendor) {
case Vendor_NVIDIA:
/* If we've found an nvidia device, increment our counter */
nv_device++;
break;
case Vendor_Invalid:
/* Bail out, we've gone too far */
LOG_ERROR("Failed to find Nvidia GPU with expected index!\n");
break;
default:
/* Non-NV gpu, continue */
break;
}
};
return nv_device;
}
/* Get the max nvidia perf level */
static long get_max_perf_level_nv(struct GameModeGPUInfo *info)
{
if (info->vendor != Vendor_NVIDIA)
return -1;
if (!getenv("DISPLAY"))
LOG_ERROR("Getting Nvidia parameters requires DISPLAY to be set - will likely fail!\n");
char arg[NV_ARG_MAX] = { 0 };
const char *attr;
snprintf(arg, NV_ARG_MAX, NV_ATTRIBUTE_FORMAT, info->device, NV_PERFMODES_ATTRIBUTE);
if ((attr = get_nv_attr(arg)) == NULL) {
return -1;
}
char *ptr = strrchr(attr, ';');
long level = -1;
if (!ptr || sscanf(ptr, "; perf=%ld", &level) != 1) {
LOG_ERROR(
"Output didn't match expected format, couldn't discern highest perf level from "
"nvidia-settings!\n");
LOG_ERROR("Output:%s\n", attr);
return -1;
}
return level;
}
/* Get the nvidia gpu state */
static int get_gpu_state_nv(struct GameModeGPUInfo *info)
{
if (info->vendor != Vendor_NVIDIA)
return -1;
if (!getenv("DISPLAY"))
LOG_ERROR("Getting Nvidia parameters requires DISPLAY to be set - will likely fail!\n");
long perf_level = get_max_perf_level_nv(info);
char arg[NV_ARG_MAX] = { 0 };
const char *attr;
char *end;
/* Get the GPUGraphicsClockOffset parameter */
snprintf(arg,
NV_ARG_MAX,
NV_ATTRIBUTE_FORMAT NV_PERF_LEVEL_FORMAT,
info->device,
NV_CORE_OFFSET_ATTRIBUTE,
perf_level);
if ((attr = get_nv_attr(arg)) == NULL) {
return -1;
}
info->nv_core = strtol(attr, &end, 10);
if (end == attr) {
LOG_ERROR("Failed to parse output for \"%s\" output was \"%s\"!\n", arg, attr);
return -1;
}
/* Get the GPUMemoryTransferRateOffset parameter */
snprintf(arg,
NV_ARG_MAX,
NV_ATTRIBUTE_FORMAT NV_PERF_LEVEL_FORMAT,
info->device,
NV_MEM_OFFSET_ATTRIBUTE,
perf_level);
if ((attr = get_nv_attr(arg)) == NULL) {
return -1;
}
info->nv_mem = strtol(attr, &end, 10);
if (end == attr) {
LOG_ERROR("Failed to parse output for \"%s\" output was \"%s\"!\n", arg, attr);
return -1;
}
/* Get the GPUPowerMizerMode parameter */
snprintf(arg, NV_ARG_MAX, NV_ATTRIBUTE_FORMAT, info->device, NV_POWERMIZER_MODE_ATTRIBUTE);
if ((attr = get_nv_attr(arg)) == NULL) {
return -1;
}
info->nv_powermizer_mode = strtol(attr, &end, 10);
if (end == attr) {
LOG_ERROR("Failed to parse output for \"%s\" output was \"%s\"!\n", arg, attr);
return -1;
}
return 0;
}
/**
* Set the gpu state based on input parameters on Nvidia
*/
static int set_gpu_state_nv(struct GameModeGPUInfo *info)
{
int status = 0;
if (info->vendor != Vendor_NVIDIA)
return -1;
if (!getenv("DISPLAY") || !getenv("XAUTHORITY"))
LOG_ERROR(
"Setting Nvidia parameters requires DISPLAY and XAUTHORITY to be set - will likely "
"fail!\n");
long perf_level = get_max_perf_level_nv(info);
char arg[NV_ARG_MAX] = { 0 };
/* Set the GPUGraphicsClockOffset parameter */
if (info->nv_core != -1) {
snprintf(arg,
NV_ARG_MAX,
NV_ATTRIBUTE_FORMAT NV_PERF_LEVEL_FORMAT "=%ld",
info->device,
NV_CORE_OFFSET_ATTRIBUTE,
perf_level,
info->nv_core);
if (set_nv_attr(arg) != 0) {
status = -1;
}
}
/* Set the GPUMemoryTransferRateOffset parameter */
if (info->nv_mem != -1) {
snprintf(arg,
NV_ARG_MAX,
NV_ATTRIBUTE_FORMAT NV_PERF_LEVEL_FORMAT "=%ld",
info->device,
NV_MEM_OFFSET_ATTRIBUTE,
perf_level,
info->nv_mem);
if (set_nv_attr(arg) != 0) {
status = -1;
}
}
/* Set the GPUPowerMizerMode parameter if requested */
if (info->nv_powermizer_mode != -1) {
snprintf(arg,
NV_ARG_MAX,
NV_ATTRIBUTE_FORMAT "=%ld",
info->device,
NV_POWERMIZER_MODE_ATTRIBUTE,
info->nv_powermizer_mode);
if (set_nv_attr(arg) != 0) {
status = -1;
}
}
return status;
}
/**
* Get the gpu state
* Populates the struct with the GPU info on the system
*/
static int get_gpu_state_amd(struct GameModeGPUInfo *info)
{
if (info->vendor != Vendor_AMD)
return -1;
/* Get the contents of power_dpm_force_performance_level */
char path[PATH_MAX];
snprintf(path, PATH_MAX, AMD_DRM_PATH, info->device, "power_dpm_force_performance_level");
FILE *file = fopen(path, "r");
if (!file) {
LOG_ERROR("Could not open %s for read (%s)!\n", path, strerror(errno));
return -1;
}
int ret = 0;
char buff[CONFIG_VALUE_MAX] = { 0 };
if (!fgets(buff, CONFIG_VALUE_MAX, file)) {
LOG_ERROR("Could not read file %s (%s)!\n", path, strerror(errno));
ret = -1;
}
if (fclose(file) != 0) {
LOG_ERROR("Could not close %s after reading (%s)!\n", path, strerror(errno));
ret = -1;
}
if (ret == 0) {
/* Copy in the value from the file */
strncpy(info->amd_performance_level, buff, CONFIG_VALUE_MAX - 1);
info->amd_performance_level[CONFIG_VALUE_MAX - 1] = '\0';
}
return ret;
}
/*
* Simply set an amd drm file to a value
*/
static int set_gpu_state_amd_file(const char *filename, long device, const char *value)
{
char path[PATH_MAX];
snprintf(path, PATH_MAX, AMD_DRM_PATH, device, filename);
FILE *file = fopen(path, "w");
if (!file) {
LOG_ERROR("Could not open %s for write (%s)!\n", path, strerror(errno));
return -1;
}
int ret = 0;
if (fprintf(file, "%s", value) < 0) {
LOG_ERROR("Could not write to %s (%s)!\n", path, strerror(errno));
ret = -1;
}
if (fclose(file) != 0) {
LOG_ERROR("Could not close %s after writing (%s)!\n", path, strerror(errno));
ret = -1;
}
return ret;
}
/**
* Set the gpu state based on input parameters on amd
*/
static int set_gpu_state_amd(struct GameModeGPUInfo *info)
{
if (info->vendor != Vendor_AMD)
return -1;
/* Must be root to set the state */
if (geteuid() != 0) {
fprintf(stderr, "gpuclockctl must be run as root to set AMD values\n");
print_usage_and_exit();
}
/* First set the amd_performance_level to the chosen setting */
if (set_gpu_state_amd_file("power_dpm_force_performance_level",
info->device,
info->amd_performance_level) != 0)
return -1;
/* TODO: If amd_performance_level is set to "manual" we need to adjust pp_table and/or
pp_od_clk_voltage see
https://dri.freedesktop.org/docs/drm/gpu/amdgpu.html#gpu-power-thermal-controls-and-monitoring
*/
return 0;
}
/* Helper to get and verify device value */
static long get_device(const char *val)
{
char *end;
long ret = strtol(val, &end, 10);
if (ret < 0 || end == val) {
LOG_ERROR("Invalid GPU device passed (%ld)!\n", ret);
print_usage_and_exit();
}
return ret;
}
/* Helper to get and verify nv_core and nv_mem value */
static long get_generic_value(const char *val)
{
char *end;
long ret = strtol(val, &end, 10);
if (end == val) {
LOG_ERROR("Invalid value passed (%ld)!\n", ret);
print_usage_and_exit();
}
return ret;
}
/**
* Main entry point, dispatch to the appropriate helper
*/
int main(int argc, char *argv[])
{
struct GameModeGPUInfo info;
memset(&info, 0, sizeof(info));
if (argc == 3 && strncmp(argv[2], "get", 3) == 0) {
/* Get and verify the vendor and device */
info.device = get_device(argv[1]);
info.vendor = gamemode_get_gpu_vendor(info.device);
/* Fetch the state and print it out */
switch (info.vendor) {
case Vendor_NVIDIA:
/* Adjust the device number to the gpu index for Nvidia */
info.device = get_gpu_index_id_nv(&info);
if (get_gpu_state_nv(&info) != 0)
exit(EXIT_FAILURE);
printf("%ld %ld %ld\n", info.nv_core, info.nv_mem, info.nv_powermizer_mode);
break;
case Vendor_AMD:
if (get_gpu_state_amd(&info) != 0)
exit(EXIT_FAILURE);
printf("%s\n", info.amd_performance_level);
break;
default:
LOG_ERROR("Currently unsupported GPU vendor 0x%04x, doing nothing!\n",
(unsigned short)info.vendor);
break;
}
} else if (argc >= 4 && argc <= 7 && strncmp(argv[2], "set", 3) == 0) {
/* Get and verify the vendor and device */
info.device = get_device(argv[1]);
info.vendor = gamemode_get_gpu_vendor(info.device);
switch (info.vendor) {
case Vendor_NVIDIA:
if (argc < 4) {
LOG_ERROR("Must pass at least 4 arguments for nvidia gpu!\n");
print_usage_and_exit();
}
info.nv_core = get_generic_value(argv[3]);
info.nv_mem = get_generic_value(argv[4]);
/* Adjust the device number to the gpu index for Nvidia */
info.device = get_gpu_index_id_nv(&info);
/* Optional */
info.nv_powermizer_mode = -1;
if (argc >= 6)
info.nv_powermizer_mode = get_generic_value(argv[5]);
return set_gpu_state_nv(&info);
break;
case Vendor_AMD:
if (argc < 3) {
LOG_ERROR("Must pass performance level for AMD gpu!\n");
print_usage_and_exit();
}
strncpy(info.amd_performance_level, argv[3], CONFIG_VALUE_MAX - 1);
return set_gpu_state_amd(&info);
break;
default:
LOG_ERROR("Currently unsupported GPU vendor 0x%04x, doing nothing!\n",
(unsigned short)info.vendor);
print_usage_and_exit();
break;
}
} else {
print_usage_and_exit();
}
return EXIT_SUCCESS;
}

View File

@ -1,41 +0,0 @@
/*
Copyright (c) 2017-2019, Feral Interactive
Copyright (c) 2019, Red Hat
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 "helpers.h"
/* Starting with C99 we can use "inline" without "static" and thus avoid
* having multiple (local) definitions of the same inline function. One
* consequence of that is that if the compiler decides to *not* inline
* a specific call to the function the linker will expect an definition.
*/
extern inline void cleanup_close(int *fd);

View File

@ -1,83 +0,0 @@
/*
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.
*/
#pragma once
#include <stdio.h>
#include <string.h>
#include <sys/param.h>
#include <unistd.h>
/**
* Value clamping helper, works like MIN/MAX but constraints a value within the range.
*/
#define CLAMP(l, u, value) MAX(MIN(l, u), MIN(MAX(l, u), value))
/**
* Little helper to safely print into a buffer, returns a pointer into the buffer
*/
#define buffered_snprintf(b, s, ...) \
(snprintf(b, sizeof(b), s, __VA_ARGS__) < (ssize_t)sizeof(b) ? b : NULL)
/**
* Little helper to safely print into a buffer, returns a newly allocated string
*/
#define safe_snprintf(b, s, ...) \
(snprintf(b, sizeof(b), s, __VA_ARGS__) < (ssize_t)sizeof(b) ? strndup(b, sizeof(b)) : NULL)
/**
* Helper function: Test, if haystack ends with needle.
*/
static inline const char *strtail(const char *haystack, const char *needle)
{
char *pos = strstr(haystack, needle);
if (pos && (strlen(pos) == strlen(needle)))
return pos;
return NULL;
}
/**
* Helper function for autoclosing file-descriptors. Does nothing if the argument
* is NULL or the referenced integer < 0.
*/
inline void cleanup_close(int *fd_ptr)
{
if (fd_ptr == NULL || *fd_ptr < 0)
return;
(void)close(*fd_ptr);
}
/**
* Helper macro for autoclosing file-descriptors: use by prefixing the variable,
* like "autoclose_fd int fd = -1;".
*/
#define autoclose_fd __attribute__((cleanup(cleanup_close)))

View File

@ -1,53 +0,0 @@
/*
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.
*/
#include "logging.h"
#include "syslog.h"
static bool use_syslog = false;
/**
* Control if we want to use the system logger
*/
void set_use_syslog(const char *name)
{
/* Open the syslog */
openlog(name, LOG_PID, LOG_DAEMON);
use_syslog = true;
}
/**
* Simple getter for the syslog var
*/
bool get_use_syslog(void)
{
return use_syslog;
}

View File

@ -1,108 +0,0 @@
/*
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.
*/
#pragma once
#include <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <unistd.h>
/* Macros to help with basic logging */
#define PLOG_MSG(...) printf(__VA_ARGS__)
#define SYSLOG_MSG(...) syslog(LOG_INFO, __VA_ARGS__)
#define LOG_MSG(...) \
do { \
if (get_use_syslog()) { \
SYSLOG_MSG(__VA_ARGS__); \
} else { \
PLOG_MSG(__VA_ARGS__); \
} \
} while (0)
#define PLOG_ERROR(...) fprintf(stderr, "ERROR: " __VA_ARGS__)
#define SYSLOG_ERROR(...) syslog(LOG_ERR, __VA_ARGS__)
#define LOG_ERROR(...) \
do { \
if (get_use_syslog()) { \
SYSLOG_ERROR(__VA_ARGS__); \
} else { \
PLOG_ERROR(__VA_ARGS__); \
} \
} while (0)
#define LOG_ONCE(type, ...) \
do { \
static int __once = 0; \
if (!__once++) \
LOG_##type(__VA_ARGS__); \
} while (0)
/* Fatal warnings trigger an exit */
#define FATAL_ERRORNO(msg) \
do { \
LOG_ERROR(msg " (%s)\n", strerror(errno)); \
exit(EXIT_FAILURE); \
} while (0)
#define FATAL_ERROR(...) \
do { \
LOG_ERROR(__VA_ARGS__); \
exit(EXIT_FAILURE); \
} while (0)
/* Hinting helpers */
#define HINT_ONCE(name, hint) \
do { \
static int __once = 0; \
name = (!__once++ ? hint : ""); \
} while (0)
#define HINT_ONCE_ON(cond, ...) \
do { \
if (cond) \
HINT_ONCE(__VA_ARGS__); \
} while (0);
#define LOG_HINTED(type, msg, hint, ...) \
do { \
const char *__arg; \
HINT_ONCE(__arg, hint); \
LOG_##type(msg "%s", __VA_ARGS__, __arg); \
} while (0)
/**
* Control if and how how we use syslog
*/
void set_use_syslog(const char *name);
bool get_use_syslog(void);

View File

@ -1,35 +1,14 @@
# Convenience library for the duplicated logging functionality
common_sources = [
'logging.c',
'governors-query.c',
'external-helper.c',
'gpu-control.c',
]
daemon_common = static_library(
'daemon-common',
sources: common_sources,
install: false,
)
link_daemon_common = declare_dependency(
link_with: daemon_common,
)
# Main daemon
daemon_sources = [
'main.c',
'gamemode.c',
'gamemode-env.c',
'gamemoded.c',
'gamemode-context.c',
'gamemode-ioprio.c',
'gamemode-proc.c',
'gamemode-sched.c',
'gamemode-wine.c',
'gamemode-tests.c',
'gamemode-gpu.c',
'dbus_messaging.c',
'daemon_config.c',
'helpers.c',
'gamemode-dbus.c',
'gamemode-config.c',
]
gamemoded_includes = libgamemode_includes
@ -45,36 +24,9 @@ executable(
inih_dependency,
libdl,
],
include_directories: gamemoded_includes,
install: true,
)
# Small target util to get and set cpu governors
cpugovctl_sources = [
'cpugovctl.c',
]
cpugovctl = executable(
'cpugovctl',
sources: cpugovctl_sources,
dependencies: [
link_daemon_common,
include_directories: [
gamemoded_includes,
include_daemon_common,
],
install: true,
install_dir: path_libexecdir,
)
# Small target util to get and set gpu clocks values
gpuclockctl_sources = [
'gpuclockctl.c',
]
gpuclockctl = executable(
'gpuclockctl',
sources: gpuclockctl_sources,
dependencies: [
link_daemon_common,
],
install: true,
install_dir: path_libexecdir,
)
)