Merge pull request #102 from mdiluz/config-refactor

Small config refactor
This commit is contained in:
Alex Smith 2019-02-20 09:00:32 +00:00 committed by GitHub
commit 7550a1937d
6 changed files with 118 additions and 139 deletions

View File

@ -49,6 +49,15 @@ POSSIBILITY OF SUCH DAMAGE.
/* Default value for the reaper frequency */ /* Default value for the reaper frequency */
#define DEFAULT_REAPER_FREQ 5 #define DEFAULT_REAPER_FREQ 5
/* Helper macro for defining the config variable getter */
#define DEFINE_CONFIG_GET(name) \
long config_get_##name(GameModeConfig *self) \
{ \
long value = 0; \
memcpy_locked_config(self, &value, &self->values.name, sizeof(long)); \
return value; \
}
/** /**
* The config holds various details as needed * The config holds various details as needed
* and a rwlock to allow config_reload to be called * and a rwlock to allow config_reload to be called
@ -58,6 +67,7 @@ struct GameModeConfig {
int inotfd; int inotfd;
int inotwd; int inotwd;
struct {
char whitelist[CONFIG_LIST_MAX][CONFIG_VALUE_MAX]; char whitelist[CONFIG_LIST_MAX][CONFIG_VALUE_MAX];
char blacklist[CONFIG_LIST_MAX][CONFIG_VALUE_MAX]; char blacklist[CONFIG_LIST_MAX][CONFIG_VALUE_MAX];
@ -84,6 +94,7 @@ struct GameModeConfig {
long nv_perf_level; long nv_perf_level;
long amd_core_clock_percentage; long amd_core_clock_percentage;
long amd_mem_clock_percentage; long amd_mem_clock_percentage;
} values;
}; };
/* /*
@ -181,52 +192,52 @@ static int inih_handler(void *user, const char *section, const char *name, const
if (strcmp(section, "filter") == 0) { if (strcmp(section, "filter") == 0) {
/* Filter subsection */ /* Filter subsection */
if (strcmp(name, "whitelist") == 0) { if (strcmp(name, "whitelist") == 0) {
valid = append_value_to_list(name, value, self->whitelist); valid = append_value_to_list(name, value, self->values.whitelist);
} else if (strcmp(name, "blacklist") == 0) { } else if (strcmp(name, "blacklist") == 0) {
valid = append_value_to_list(name, value, self->blacklist); valid = append_value_to_list(name, value, self->values.blacklist);
} }
} else if (strcmp(section, "general") == 0) { } else if (strcmp(section, "general") == 0) {
/* General subsection */ /* General subsection */
if (strcmp(name, "reaper_freq") == 0) { if (strcmp(name, "reaper_freq") == 0) {
valid = get_long_value(name, value, &self->reaper_frequency); valid = get_long_value(name, value, &self->values.reaper_frequency);
} else if (strcmp(name, "defaultgov") == 0) { } else if (strcmp(name, "defaultgov") == 0) {
valid = get_string_value(value, self->defaultgov); valid = get_string_value(value, self->values.defaultgov);
} else if (strcmp(name, "desiredgov") == 0) { } else if (strcmp(name, "desiredgov") == 0) {
valid = get_string_value(value, self->desiredgov); valid = get_string_value(value, self->values.desiredgov);
} else if (strcmp(name, "softrealtime") == 0) { } else if (strcmp(name, "softrealtime") == 0) {
valid = get_string_value(value, self->softrealtime); valid = get_string_value(value, self->values.softrealtime);
} else if (strcmp(name, "renice") == 0) { } else if (strcmp(name, "renice") == 0) {
valid = get_long_value(name, value, &self->renice); valid = get_long_value(name, value, &self->values.renice);
} else if (strcmp(name, "ioprio") == 0) { } else if (strcmp(name, "ioprio") == 0) {
valid = get_string_value(value, self->ioprio); valid = get_string_value(value, self->values.ioprio);
} else if (strcmp(name, "inhibit_screensaver") == 0) { } else if (strcmp(name, "inhibit_screensaver") == 0) {
valid = get_long_value(name, value, &self->inhibit_screensaver); valid = get_long_value(name, value, &self->values.inhibit_screensaver);
} }
} else if (strcmp(section, "gpu") == 0) { } else if (strcmp(section, "gpu") == 0) {
/* GPU subsection */ /* GPU subsection */
if (strcmp(name, "apply_gpu_optimisations") == 0) { if (strcmp(name, "apply_gpu_optimisations") == 0) {
valid = get_string_value(value, self->apply_gpu_optimisations); valid = get_string_value(value, self->values.apply_gpu_optimisations);
} else if (strcmp(name, "gpu_vendor") == 0) { } else if (strcmp(name, "gpu_vendor") == 0) {
valid = get_long_value_hex(name, value, &self->gpu_vendor); valid = get_long_value_hex(name, value, &self->values.gpu_vendor);
} else if (strcmp(name, "gpu_device") == 0) { } else if (strcmp(name, "gpu_device") == 0) {
valid = get_long_value(name, value, &self->gpu_device); valid = get_long_value(name, value, &self->values.gpu_device);
} else if (strcmp(name, "nv_core_clock_mhz_offset") == 0) { } else if (strcmp(name, "nv_core_clock_mhz_offset") == 0) {
valid = get_long_value(name, value, &self->nv_core_clock_mhz_offset); valid = get_long_value(name, value, &self->values.nv_core_clock_mhz_offset);
} else if (strcmp(name, "nv_mem_clock_mhz_offset") == 0) { } else if (strcmp(name, "nv_mem_clock_mhz_offset") == 0) {
valid = get_long_value(name, value, &self->nv_mem_clock_mhz_offset); valid = get_long_value(name, value, &self->values.nv_mem_clock_mhz_offset);
} else if (strcmp(name, "nv_perf_level") == 0) { } else if (strcmp(name, "nv_perf_level") == 0) {
valid = get_long_value(name, value, &self->nv_perf_level); valid = get_long_value(name, value, &self->values.nv_perf_level);
} else if (strcmp(name, "amd_core_clock_percentage") == 0) { } else if (strcmp(name, "amd_core_clock_percentage") == 0) {
valid = get_long_value(name, value, &self->amd_core_clock_percentage); valid = get_long_value(name, value, &self->values.amd_core_clock_percentage);
} else if (strcmp(name, "amd_mem_clock_percentage") == 0) { } else if (strcmp(name, "amd_mem_clock_percentage") == 0) {
valid = get_long_value(name, value, &self->amd_mem_clock_percentage); valid = get_long_value(name, value, &self->values.amd_mem_clock_percentage);
} }
} else if (strcmp(section, "custom") == 0) { } else if (strcmp(section, "custom") == 0) {
/* Custom subsection */ /* Custom subsection */
if (strcmp(name, "start") == 0) { if (strcmp(name, "start") == 0) {
valid = append_value_to_list(name, value, self->startscripts); valid = append_value_to_list(name, value, self->values.startscripts);
} else if (strcmp(name, "end") == 0) { } else if (strcmp(name, "end") == 0) {
valid = append_value_to_list(name, value, self->endscripts); valid = append_value_to_list(name, value, self->values.endscripts);
} }
} }
@ -271,25 +282,14 @@ static void load_config_files(GameModeConfig *self)
pthread_rwlock_wrlock(&self->rwlock); pthread_rwlock_wrlock(&self->rwlock);
/* Clear our config values */ /* Clear our config values */
memset(self->ioprio, 0, sizeof(self->ioprio)); memset(&self->values, 0, sizeof(self->values));
memset(self->whitelist, 0, sizeof(self->whitelist));
memset(self->blacklist, 0, sizeof(self->blacklist)); /* Set some non-zero defaults */
memset(self->startscripts, 0, sizeof(self->startscripts)); self->values.inhibit_screensaver = 1; /* Defaults to on */
memset(self->endscripts, 0, sizeof(self->endscripts)); self->values.renice = 4; /* default value of 4 */
memset(self->defaultgov, 0, sizeof(self->defaultgov)); self->values.reaper_frequency = DEFAULT_REAPER_FREQ;
memset(self->desiredgov, 0, sizeof(self->desiredgov)); self->values.gpu_device = -1; /* 0 is a valid device ID so use -1 to indicate no value */
memset(self->softrealtime, 0, sizeof(self->softrealtime)); self->values.nv_perf_level = -1;
memset(self->apply_gpu_optimisations, 0, sizeof(self->apply_gpu_optimisations));
self->inhibit_screensaver = 1; /* Defaults to on */
self->renice = 4; /* default value of 4 */
self->reaper_frequency = DEFAULT_REAPER_FREQ;
self->gpu_vendor = 0;
self->gpu_device = -1; /* 0 is a valid device ID so use -1 to indicate no value */
self->nv_core_clock_mhz_offset = 0;
self->nv_mem_clock_mhz_offset = 0;
self->nv_perf_level = -1;
self->amd_core_clock_percentage = 0;
self->amd_mem_clock_percentage = 0;
/* /*
* Locations to load, in order * Locations to load, in order
@ -393,14 +393,14 @@ bool config_get_client_whitelisted(GameModeConfig *self, const char *client)
/* If the whitelist is empty then everything passes */ /* If the whitelist is empty then everything passes */
bool found = true; bool found = true;
if (self->whitelist[0][0]) { if (self->values.whitelist[0][0]) {
/* /*
* Check if the value is found in our whitelist * Check if the value is found in our whitelist
* Currently is a simple strstr check, but could be modified for wildcards etc. * Currently is a simple strstr check, but could be modified for wildcards etc.
*/ */
found = false; found = false;
for (unsigned int i = 0; i < CONFIG_LIST_MAX && self->whitelist[i][0]; i++) { for (unsigned int i = 0; i < CONFIG_LIST_MAX && self->values.whitelist[i][0]; i++) {
if (strstr(client, self->whitelist[i])) { if (strstr(client, self->values.whitelist[i])) {
found = true; found = true;
} }
} }
@ -424,8 +424,8 @@ bool config_get_client_blacklisted(GameModeConfig *self, const char *client)
* Currently is a simple strstr check, but could be modified for wildcards etc. * Currently is a simple strstr check, but could be modified for wildcards etc.
*/ */
bool found = false; bool found = false;
for (unsigned int i = 0; i < CONFIG_LIST_MAX && self->blacklist[i][0]; i++) { for (unsigned int i = 0; i < CONFIG_LIST_MAX && self->values.blacklist[i][0]; i++) {
if (strstr(client, self->blacklist[i])) { if (strstr(client, self->values.blacklist[i])) {
found = true; found = true;
} }
} }
@ -438,10 +438,7 @@ bool config_get_client_blacklisted(GameModeConfig *self, const char *client)
/* /*
* Gets the reaper frequency * Gets the reaper frequency
*/ */
void config_get_reaper_thread_frequency(GameModeConfig *self, long *value) DEFINE_CONFIG_GET(reaper_frequency)
{
memcpy_locked_config(self, value, &self->reaper_frequency, sizeof(long));
}
/* /*
* Gets the screensaver inhibit setting * Gets the screensaver inhibit setting
@ -449,7 +446,7 @@ void config_get_reaper_thread_frequency(GameModeConfig *self, long *value)
bool config_get_inhibit_screensaver(GameModeConfig *self) bool config_get_inhibit_screensaver(GameModeConfig *self)
{ {
long val; long val;
memcpy_locked_config(self, &val, &self->inhibit_screensaver, sizeof(long)); memcpy_locked_config(self, &val, &self->values.inhibit_screensaver, sizeof(long));
return val == 1; return val == 1;
} }
@ -459,7 +456,10 @@ bool config_get_inhibit_screensaver(GameModeConfig *self)
void config_get_gamemode_start_scripts(GameModeConfig *self, void config_get_gamemode_start_scripts(GameModeConfig *self,
char scripts[CONFIG_LIST_MAX][CONFIG_VALUE_MAX]) char scripts[CONFIG_LIST_MAX][CONFIG_VALUE_MAX])
{ {
memcpy_locked_config(self, scripts, self->startscripts, sizeof(self->startscripts)); memcpy_locked_config(self,
scripts,
self->values.startscripts,
sizeof(self->values.startscripts));
} }
/* /*
@ -468,7 +468,7 @@ void config_get_gamemode_start_scripts(GameModeConfig *self,
void config_get_gamemode_end_scripts(GameModeConfig *self, void config_get_gamemode_end_scripts(GameModeConfig *self,
char scripts[CONFIG_LIST_MAX][CONFIG_VALUE_MAX]) char scripts[CONFIG_LIST_MAX][CONFIG_VALUE_MAX])
{ {
memcpy_locked_config(self, scripts, self->endscripts, sizeof(self->startscripts)); memcpy_locked_config(self, scripts, self->values.endscripts, sizeof(self->values.startscripts));
} }
/* /*
@ -476,7 +476,7 @@ void config_get_gamemode_end_scripts(GameModeConfig *self,
*/ */
void config_get_default_governor(GameModeConfig *self, char governor[CONFIG_VALUE_MAX]) void config_get_default_governor(GameModeConfig *self, char governor[CONFIG_VALUE_MAX])
{ {
memcpy_locked_config(self, governor, self->defaultgov, sizeof(self->defaultgov)); memcpy_locked_config(self, governor, self->values.defaultgov, sizeof(self->values.defaultgov));
} }
/* /*
@ -484,7 +484,7 @@ void config_get_default_governor(GameModeConfig *self, char governor[CONFIG_VALU
*/ */
void config_get_desired_governor(GameModeConfig *self, char governor[CONFIG_VALUE_MAX]) void config_get_desired_governor(GameModeConfig *self, char governor[CONFIG_VALUE_MAX])
{ {
memcpy_locked_config(self, governor, self->desiredgov, sizeof(self->desiredgov)); memcpy_locked_config(self, governor, self->values.desiredgov, sizeof(self->values.desiredgov));
} }
/* /*
@ -492,30 +492,37 @@ void config_get_desired_governor(GameModeConfig *self, char governor[CONFIG_VALU
*/ */
void config_get_soft_realtime(GameModeConfig *self, char softrealtime[CONFIG_VALUE_MAX]) void config_get_soft_realtime(GameModeConfig *self, char softrealtime[CONFIG_VALUE_MAX])
{ {
memcpy_locked_config(self, softrealtime, self->softrealtime, sizeof(self->softrealtime)); memcpy_locked_config(self,
softrealtime,
self->values.softrealtime,
sizeof(self->values.softrealtime));
} }
/* /*
* Get the renice value * Get the renice value
*/ */
void config_get_renice_value(GameModeConfig *self, long *value) long config_get_renice_value(GameModeConfig *self)
{ {
memcpy_locked_config(self, value, &self->renice, sizeof(long)); long value = 0;
memcpy_locked_config(self, &value, &self->values.renice, sizeof(long));
return value;
} }
/* /*
* Get the ioprio value * Get the ioprio value
*/ */
void config_get_ioprio_value(GameModeConfig *self, int *value) long config_get_ioprio_value(GameModeConfig *self)
{ {
long value = 0;
char ioprio_value[CONFIG_VALUE_MAX] = { 0 }; char ioprio_value[CONFIG_VALUE_MAX] = { 0 };
memcpy_locked_config(self, ioprio_value, &self->ioprio, sizeof(self->ioprio)); memcpy_locked_config(self, ioprio_value, &self->values.ioprio, sizeof(self->values.ioprio));
if (0 == strncmp(ioprio_value, "off", sizeof(self->ioprio))) if (0 == strncmp(ioprio_value, "off", sizeof(self->values.ioprio)))
*value = IOPRIO_DONT_SET; value = IOPRIO_DONT_SET;
else if (0 == strncmp(ioprio_value, "default", sizeof(self->ioprio))) else if (0 == strncmp(ioprio_value, "default", sizeof(self->values.ioprio)))
*value = IOPRIO_RESET_DEFAULT; value = IOPRIO_RESET_DEFAULT;
else else
*value = atoi(ioprio_value); value = atoi(ioprio_value);
return value;
} }
/* /*
@ -525,40 +532,15 @@ void config_get_apply_gpu_optimisations(GameModeConfig *self, char value[CONFIG_
{ {
memcpy_locked_config(self, memcpy_locked_config(self,
value, value,
&self->apply_gpu_optimisations, &self->values.apply_gpu_optimisations,
sizeof(self->apply_gpu_optimisations)); sizeof(self->values.apply_gpu_optimisations));
} }
void config_get_gpu_vendor(GameModeConfig *self, long *value) /* Define the getters for GPU values */
{ DEFINE_CONFIG_GET(gpu_vendor)
memcpy_locked_config(self, value, &self->gpu_vendor, sizeof(long)); DEFINE_CONFIG_GET(gpu_device)
} DEFINE_CONFIG_GET(nv_core_clock_mhz_offset)
DEFINE_CONFIG_GET(nv_mem_clock_mhz_offset)
void config_get_gpu_device(GameModeConfig *self, long *value) DEFINE_CONFIG_GET(nv_perf_level)
{ DEFINE_CONFIG_GET(amd_core_clock_percentage)
memcpy_locked_config(self, value, &self->gpu_device, sizeof(long)); DEFINE_CONFIG_GET(amd_mem_clock_percentage)
}
void config_get_nv_core_clock_mhz_offset(GameModeConfig *self, long *value)
{
memcpy_locked_config(self, value, &self->nv_core_clock_mhz_offset, sizeof(long));
}
void config_get_nv_mem_clock_mhz_offset(GameModeConfig *self, long *value)
{
memcpy_locked_config(self, value, &self->nv_mem_clock_mhz_offset, sizeof(long));
}
void config_get_nv_perf_level(GameModeConfig *self, long *value)
{
memcpy_locked_config(self, value, &self->nv_perf_level, sizeof(long));
}
void config_get_amd_core_clock_percentage(GameModeConfig *self, long *value)
{
memcpy_locked_config(self, value, &self->amd_core_clock_percentage, sizeof(long));
}
void config_get_amd_mem_clock_percentage(GameModeConfig *self, long *value)
{
memcpy_locked_config(self, value, &self->amd_mem_clock_percentage, sizeof(long));
}

View File

@ -87,7 +87,7 @@ bool config_get_client_blacklisted(GameModeConfig *self, const char *client);
/* /*
* Get the frequency (in seconds) for the reaper thread * Get the frequency (in seconds) for the reaper thread
*/ */
void config_get_reaper_thread_frequency(GameModeConfig *self, long *value); long config_get_reaper_frequency(GameModeConfig *self);
/* /*
* Get whether we want to inhibit the screensaver (defaults to true) * Get whether we want to inhibit the screensaver (defaults to true)
@ -123,21 +123,21 @@ void config_get_soft_realtime(GameModeConfig *self, char softrealtime[CONFIG_VAL
/* /*
* Get the renice value * Get the renice value
*/ */
void config_get_renice_value(GameModeConfig *self, long *value); long config_get_renice_value(GameModeConfig *self);
/* /*
* Get the ioprio value * Get the ioprio value
*/ */
void config_get_ioprio_value(GameModeConfig *self, int *value); long config_get_ioprio_value(GameModeConfig *self);
/* /*
* Get various config info for gpu optimisations * Get various config info for gpu optimisations
*/ */
void config_get_apply_gpu_optimisations(GameModeConfig *self, char value[CONFIG_VALUE_MAX]); void config_get_apply_gpu_optimisations(GameModeConfig *self, char value[CONFIG_VALUE_MAX]);
void config_get_gpu_vendor(GameModeConfig *self, long *value); long config_get_gpu_vendor(GameModeConfig *self);
void config_get_gpu_device(GameModeConfig *self, long *value); long config_get_gpu_device(GameModeConfig *self);
void config_get_nv_core_clock_mhz_offset(GameModeConfig *self, long *value); long config_get_nv_core_clock_mhz_offset(GameModeConfig *self);
void config_get_nv_mem_clock_mhz_offset(GameModeConfig *self, long *value); long config_get_nv_mem_clock_mhz_offset(GameModeConfig *self);
void config_get_nv_perf_level(GameModeConfig *self, long *value); long config_get_nv_perf_level(GameModeConfig *self);
void config_get_amd_core_clock_percentage(GameModeConfig *self, long *value); long config_get_amd_core_clock_percentage(GameModeConfig *self);
void config_get_amd_mem_clock_percentage(GameModeConfig *self, long *value); long config_get_amd_mem_clock_percentage(GameModeConfig *self);

View File

@ -71,8 +71,8 @@ int game_mode_initialise_gpu(GameModeConfig *config, GameModeGPUInfo **info)
memset(new_info, 0, sizeof(GameModeGPUInfo)); memset(new_info, 0, sizeof(GameModeGPUInfo));
/* Get the config parameters */ /* Get the config parameters */
config_get_gpu_vendor(config, &new_info->vendor); new_info->vendor = config_get_gpu_vendor(config);
config_get_gpu_device(config, &new_info->device); new_info->device = config_get_gpu_device(config);
/* verify device ID */ /* verify device ID */
if (new_info->device == -1) { if (new_info->device == -1) {
@ -100,8 +100,8 @@ int game_mode_initialise_gpu(GameModeConfig *config, GameModeGPUInfo **info)
/* Load the config based on GPU and also verify the values are sane */ /* Load the config based on GPU and also verify the values are sane */
switch (new_info->vendor) { switch (new_info->vendor) {
case Vendor_NVIDIA: case Vendor_NVIDIA:
config_get_nv_core_clock_mhz_offset(config, &new_info->core); new_info->core = config_get_nv_core_clock_mhz_offset(config);
config_get_nv_mem_clock_mhz_offset(config, &new_info->mem); new_info->mem = config_get_nv_mem_clock_mhz_offset(config);
/* Reject values over some guessed values /* Reject values over some guessed values
* If a user wants to go into very unsafe levels they can recompile * If a user wants to go into very unsafe levels they can recompile
@ -122,7 +122,7 @@ int game_mode_initialise_gpu(GameModeConfig *config, GameModeGPUInfo **info)
} }
/* Sanity check the performance level value as well */ /* Sanity check the performance level value as well */
config_get_nv_perf_level(config, &new_info->nv_perf_level); new_info->nv_perf_level = config_get_nv_perf_level(config);
if (new_info->nv_perf_level < 0 || new_info->nv_perf_level > 16) { if (new_info->nv_perf_level < 0 || new_info->nv_perf_level > 16) {
LOG_ERROR( LOG_ERROR(
"NVIDIA Performance level value likely invalid (%ld), will not apply " "NVIDIA Performance level value likely invalid (%ld), will not apply "
@ -134,8 +134,8 @@ int game_mode_initialise_gpu(GameModeConfig *config, GameModeGPUInfo **info)
break; break;
case Vendor_AMD: case Vendor_AMD:
config_get_amd_core_clock_percentage(config, &new_info->core); new_info->core = config_get_amd_core_clock_percentage(config);
config_get_amd_mem_clock_percentage(config, &new_info->mem); new_info->mem = config_get_amd_mem_clock_percentage(config);
/* Reject values over 20% /* Reject values over 20%
* If a user wants to go into very unsafe levels they can recompile * If a user wants to go into very unsafe levels they can recompile

View File

@ -102,8 +102,7 @@ void game_mode_apply_ioprio(const GameModeContext *self, const pid_t client)
/* /*
* read configuration "ioprio" (0..7) * read configuration "ioprio" (0..7)
*/ */
int ioprio = 0; int ioprio = (int)config_get_ioprio_value(config);
config_get_ioprio_value(config, &ioprio);
if (IOPRIO_RESET_DEFAULT == ioprio) { if (IOPRIO_RESET_DEFAULT == ioprio) {
LOG_MSG("IO priority will be reset to default behavior (based on CPU priority).\n"); LOG_MSG("IO priority will be reset to default behavior (based on CPU priority).\n");
ioprio = 0; ioprio = 0;

View File

@ -66,8 +66,7 @@ void game_mode_apply_renice(const GameModeContext *self, const pid_t client)
/* /*
* read configuration "renice" (1..20) * read configuration "renice" (1..20)
*/ */
long int renice = 0; long int renice = config_get_renice_value(config);
config_get_renice_value(config, &renice);
if ((renice < 1) || (renice > 20)) { if ((renice < 1) || (renice > 20)) {
LOG_ONCE(ERROR, "Configured renice value '%ld' is invalid, will not renice.\n", renice); LOG_ONCE(ERROR, "Configured renice value '%ld' is invalid, will not renice.\n", renice);
return; return;

View File

@ -549,8 +549,7 @@ static void *game_mode_context_reaper(void *userdata)
/* Stack, not allocated, won't disappear. */ /* Stack, not allocated, won't disappear. */
GameModeContext *self = userdata; GameModeContext *self = userdata;
long reaper_interval = 0.0f; long reaper_interval = config_get_reaper_frequency(self->config);
config_get_reaper_thread_frequency(self->config, &reaper_interval);
struct timespec ts = { 0, 0 }; struct timespec ts = { 0, 0 };
ts.tv_sec = time(NULL) + reaper_interval; ts.tv_sec = time(NULL) + reaper_interval;