Skip to content

Commit

Permalink
Apply clang-format to CPU pinning and parking code
Browse files Browse the repository at this point in the history
The original PR #416 failed the format check, but this wasn't apparent
until after merging.
  • Loading branch information
afayaz-feral committed Dec 4, 2023
1 parent 775c930 commit fad889d
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 37 deletions.
4 changes: 2 additions & 2 deletions common/common-cpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ POSSIBILITY OF SUCH DAMAGE.
*/

#include <stdlib.h>
#include <sched.h>
#include "common-cpu.h"
#include "common-logging.h"
#include <sched.h>
#include <stdlib.h>

char *parse_cpulist(char *cpulist, long *from, long *to)
{
Expand Down
3 changes: 1 addition & 2 deletions common/common-cpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ POSSIBILITY OF SUCH DAMAGE.
#pragma once

#define IS_CPU_PARK 0
#define IS_CPU_PIN 1
#define IS_CPU_PIN 1

/* Storage for CPU info*/
struct GameModeCPUInfo {
Expand All @@ -44,4 +44,3 @@ struct GameModeCPUInfo {

/* parses a list of cpu cores in the format "a,b-c,d-e,f" */
char *parse_cpulist(char *cpulist, long *from, long *to);

1 change: 0 additions & 1 deletion daemon/gamemode-config.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ void config_get_amd_performance_level(GameModeConfig *self, char value[CONFIG_VA
void config_get_cpu_park_cores(GameModeConfig *self, char value[CONFIG_VALUE_MAX]);
void config_get_cpu_pin_cores(GameModeConfig *self, char value[CONFIG_VALUE_MAX]);


/**
* Functions to get supervisor config permissions
*/
Expand Down
52 changes: 24 additions & 28 deletions daemon/gamemode-cpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ POSSIBILITY OF SUCH DAMAGE.

#define _GNU_SOURCE

#include <sched.h>
#include <linux/limits.h>
#include <dirent.h>
#include <linux/limits.h>
#include <sched.h>

#include "common-cpu.h"
#include "common-external.h"
Expand Down Expand Up @@ -87,7 +87,7 @@ static int walk_sysfs(char *cpulist, char **buf, size_t *buflen, GameModeCPUInfo
CPU_SET_S((size_t)cpu, CPU_ALLOC_SIZE(info->num_cpu), info->online);

/* check for L3 cache non-uniformity among the cores */
int ret =
int ret =
snprintf(path, PATH_MAX, "/sys/devices/system/cpu/cpu%ld/cache/index3/size", cpu);

if (ret > 0 && ret < PATH_MAX) {
Expand Down Expand Up @@ -118,9 +118,9 @@ static int walk_sysfs(char *cpulist, char **buf, size_t *buflen, GameModeCPUInfo

/* check for frequency non-uniformity among the cores */
ret = snprintf(path,
PATH_MAX,
"/sys/devices/system/cpu/cpu%ld/cpufreq/cpuinfo_max_freq",
cpu);
PATH_MAX,
"/sys/devices/system/cpu/cpu%ld/cpufreq/cpuinfo_max_freq",
cpu);

if (ret > 0 && ret < PATH_MAX) {
if (read_small_file(path, buf, buflen)) {
Expand All @@ -141,14 +141,14 @@ static int walk_sysfs(char *cpulist, char **buf, size_t *buflen, GameModeCPUInfo
}
}

if (CPU_EQUAL_S(CPU_ALLOC_SIZE(info->num_cpu), info->online, info->to_keep) ||
if (CPU_EQUAL_S(CPU_ALLOC_SIZE(info->num_cpu), info->online, info->to_keep) ||
CPU_COUNT_S(CPU_ALLOC_SIZE(info->num_cpu), info->to_keep) == 0) {
LOG_MSG("cpu L3 cache was uniform, this is not a x3D with multiple chiplets\n");

CPU_FREE(info->to_keep);
info->to_keep = freq_cores;

if (CPU_EQUAL_S(CPU_ALLOC_SIZE(info->num_cpu), info->online, info->to_keep) ||
if (CPU_EQUAL_S(CPU_ALLOC_SIZE(info->num_cpu), info->online, info->to_keep) ||
CPU_COUNT_S(CPU_ALLOC_SIZE(info->num_cpu), info->to_keep) == 0)
LOG_MSG("cpu frequency was uniform, this is not a big.LITTLE type of system\n");
} else {
Expand Down Expand Up @@ -209,11 +209,11 @@ int game_mode_initialise_cpu(GameModeConfig *config, GameModeCPUInfo **info)
int park_or_pin = -1;

if (pin_cores[0] != '\0') {
if (strcasecmp(pin_cores, "no") == 0 || strcasecmp(pin_cores, "false") == 0 ||
if (strcasecmp(pin_cores, "no") == 0 || strcasecmp(pin_cores, "false") == 0 ||
strcmp(pin_cores, "0") == 0) {
park_or_pin = -2;
} else if (strcasecmp(pin_cores, "yes") == 0 || strcasecmp(pin_cores, "true") == 0 ||
strcmp(pin_cores, "1") == 0) {
} else if (strcasecmp(pin_cores, "yes") == 0 || strcasecmp(pin_cores, "true") == 0 ||
strcmp(pin_cores, "1") == 0) {
pin_cores[0] = '\0';
park_or_pin = IS_CPU_PIN;
} else {
Expand All @@ -222,14 +222,14 @@ int game_mode_initialise_cpu(GameModeConfig *config, GameModeCPUInfo **info)
}

if (park_or_pin != IS_CPU_PIN && park_cores[0] != '\0') {
if (strcasecmp(park_cores, "no") == 0 || strcasecmp(park_cores, "false") == 0 ||
if (strcasecmp(park_cores, "no") == 0 || strcasecmp(park_cores, "false") == 0 ||
strcmp(park_cores, "0") == 0) {
if (park_or_pin == -2)
return 0;

park_or_pin = -1;
} else if (strcasecmp(park_cores, "yes") == 0 || strcasecmp(park_cores, "true") == 0 ||
strcmp(park_cores, "1") == 0) {
} else if (strcasecmp(park_cores, "yes") == 0 || strcasecmp(park_cores, "true") == 0 ||
strcmp(park_cores, "1") == 0) {
park_cores[0] = '\0';
park_or_pin = IS_CPU_PARK;
} else {
Expand Down Expand Up @@ -282,7 +282,7 @@ int game_mode_initialise_cpu(GameModeConfig *config, GameModeCPUInfo **info)
goto error_exit;
}

if (park_or_pin == IS_CPU_PARK &&
if (park_or_pin == IS_CPU_PARK &&
CPU_EQUAL_S(CPU_ALLOC_SIZE(new_info->num_cpu), new_info->online, new_info->to_keep)) {
game_mode_free_cpu(&new_info);
LOG_MSG("I can find no reason to perform core parking on this system!\n");
Expand Down Expand Up @@ -355,7 +355,7 @@ int game_mode_park_cpu(const GameModeCPUInfo *info)
int pos = 0;

for (long cpu = 0; cpu < (long)(info->num_cpu); cpu++) {
if (CPU_ISSET_S((size_t)cpu, CPU_ALLOC_SIZE(info->num_cpu), info->online) &&
if (CPU_ISSET_S((size_t)cpu, CPU_ALLOC_SIZE(info->num_cpu), info->online) &&
!CPU_ISSET_S((size_t)cpu, CPU_ALLOC_SIZE(info->num_cpu), info->to_keep)) {
if (first == -1) {
first = cpu;
Expand Down Expand Up @@ -400,7 +400,7 @@ int game_mode_unpark_cpu(const GameModeCPUInfo *info)
int pos = 0;

for (long cpu = 0; cpu < (long)(info->num_cpu); cpu++) {
if (CPU_ISSET_S((size_t)cpu, CPU_ALLOC_SIZE(info->num_cpu), info->online) &&
if (CPU_ISSET_S((size_t)cpu, CPU_ALLOC_SIZE(info->num_cpu), info->online) &&
!CPU_ISSET_S((size_t)cpu, CPU_ALLOC_SIZE(info->num_cpu), info->to_keep)) {
if (first == -1) {
first = cpu;
Expand Down Expand Up @@ -434,34 +434,29 @@ int game_mode_unpark_cpu(const GameModeCPUInfo *info)
return 0;
}

static void apply_affinity_mask (pid_t pid, size_t cpusetsize, const cpu_set_t *mask, const bool be_silent)
static void apply_affinity_mask(pid_t pid, size_t cpusetsize, const cpu_set_t *mask,
const bool be_silent)
{
char buffer[PATH_MAX];
char *proc_path = NULL;
DIR *proc_dir = NULL;

if (!(proc_path = buffered_snprintf(buffer, "/proc/%d/task", pid))) {
if (!be_silent) {
LOG_ERROR(
"Unable to find executable for PID %d: %s\n",
pid,
strerror(errno));
LOG_ERROR("Unable to find executable for PID %d: %s\n", pid, strerror(errno));
}
return;
}

if (!(proc_dir = opendir(proc_path))) {
if (!be_silent) {
LOG_ERROR(
"Unable to find executable for PID %d: %s\n",
pid,
strerror(errno));
LOG_ERROR("Unable to find executable for PID %d: %s\n", pid, strerror(errno));
}
return;
}

struct dirent *entry;
while((entry = readdir(proc_dir))) {
while ((entry = readdir(proc_dir))) {
if (entry->d_name[0] == '.')
continue;

Expand All @@ -474,7 +469,8 @@ static void apply_affinity_mask (pid_t pid, size_t cpusetsize, const cpu_set_t *
closedir(proc_dir);
}

void game_mode_apply_core_pinning(const GameModeCPUInfo *info, const pid_t client, const bool be_silent)
void game_mode_apply_core_pinning(const GameModeCPUInfo *info, const pid_t client,
const bool be_silent)
{
if (!info || info->park_or_pin == IS_CPU_PARK)
return;
Expand Down
3 changes: 2 additions & 1 deletion daemon/gamemode.h
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,8 @@ void game_mode_free_cpu(GameModeCPUInfo **info);
void game_mode_reconfig_cpu(GameModeConfig *config, GameModeCPUInfo **info);
int game_mode_park_cpu(const GameModeCPUInfo *info);
int game_mode_unpark_cpu(const GameModeCPUInfo *info);
void game_mode_apply_core_pinning(const GameModeCPUInfo *info, const pid_t client, const bool be_silent);
void game_mode_apply_core_pinning(const GameModeCPUInfo *info, const pid_t client,
const bool be_silent);
void game_mode_undo_core_pinning(const GameModeCPUInfo *info, const pid_t client);

/** gamemode-dbus.c
Expand Down
6 changes: 3 additions & 3 deletions util/cpucorectl.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ POSSIBILITY OF SUCH DAMAGE.

#define _GNU_SOURCE

#include <linux/limits.h>
#include <sched.h>
#include <unistd.h>
#include <linux/limits.h>

#include "common-cpu.h"
#include "common-logging.h"
Expand Down Expand Up @@ -91,8 +91,8 @@ static int set_state(char *cpulist, int state)
/* on some systems one cannot park core #0 */
if (cpu != 0) {
if (state == '0') {
LOG_ERROR("unable to park core #%ld, will not apply cpu core parking!\n",
cpu);
LOG_ERROR("unable to park core #%ld, will not apply cpu core parking!\n",
cpu);
return -1;
}

Expand Down

0 comments on commit fad889d

Please sign in to comment.