mirror of
https://github.com/FeralInteractive/gamemode.git
synced 2025-06-26 17:31:45 +02:00
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:
191
common/common-external.c
Normal file
191
common/common-external.c
Normal file
@ -0,0 +1,191 @@
|
||||
/*
|
||||
|
||||
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 "common-external.h"
|
||||
#include "common-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;
|
||||
}
|
37
common/common-external.h
Normal file
37
common/common-external.h
Normal file
@ -0,0 +1,37 @@
|
||||
/*
|
||||
|
||||
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);
|
149
common/common-governors.c
Normal file
149
common/common-governors.c
Normal file
@ -0,0 +1,149 @@
|
||||
/*
|
||||
|
||||
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 "common-governors.h"
|
||||
#include "common-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;
|
||||
}
|
47
common/common-governors.h
Normal file
47
common/common-governors.h
Normal file
@ -0,0 +1,47 @@
|
||||
/*
|
||||
|
||||
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);
|
75
common/common-gpu.c
Normal file
75
common/common-gpu.c
Normal file
@ -0,0 +1,75 @@
|
||||
/*
|
||||
|
||||
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 "common-gpu.h"
|
||||
#include "common-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;
|
||||
}
|
59
common/common-gpu.h
Normal file
59
common/common-gpu.h
Normal file
@ -0,0 +1,59 @@
|
||||
/*
|
||||
|
||||
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 CONFIG_VALUE_MAX 256
|
||||
|
||||
/* 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);
|
41
common/common-helpers.c
Normal file
41
common/common-helpers.c
Normal file
@ -0,0 +1,41 @@
|
||||
/*
|
||||
|
||||
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 "common-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);
|
83
common/common-helpers.h
Normal file
83
common/common-helpers.h
Normal file
@ -0,0 +1,83 @@
|
||||
/*
|
||||
|
||||
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)))
|
54
common/common-logging.c
Normal file
54
common/common-logging.c
Normal file
@ -0,0 +1,54 @@
|
||||
/*
|
||||
|
||||
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 "common-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;
|
||||
}
|
108
common/common-logging.h
Normal file
108
common/common-logging.h
Normal file
@ -0,0 +1,108 @@
|
||||
/*
|
||||
|
||||
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);
|
20
common/meson.build
Normal file
20
common/meson.build
Normal file
@ -0,0 +1,20 @@
|
||||
# Convenience library for the duplicated logging functionality
|
||||
common_sources = [
|
||||
'common-logging.c',
|
||||
'common-governors.c',
|
||||
'common-external.c',
|
||||
'common-helpers.c',
|
||||
'common-gpu.c',
|
||||
]
|
||||
|
||||
daemon_common = static_library(
|
||||
'daemon-common',
|
||||
sources: common_sources,
|
||||
install: false,
|
||||
)
|
||||
|
||||
link_daemon_common = declare_dependency(
|
||||
link_with: daemon_common,
|
||||
)
|
||||
|
||||
include_daemon_common = include_directories('.')
|
Reference in New Issue
Block a user