reduce redundant code for file processing

There are some redundant codes about file processing which with the same
logic. so abstract the same logic to the one function, and invoke it
where it need.

A different is that in add_one_node(), not return if fopen() fail, and
remove the ferror() judgement, I think it has no effect.

Signed-off-by: Yunfeng Ye <yeyunfeng@huawei.com>
This commit is contained in:
Yunfeng Ye 2019-10-12 12:00:59 +08:00
parent 503a3e6e9f
commit c7122666e0
5 changed files with 72 additions and 124 deletions

View file

@ -36,22 +36,10 @@ static int check_affinity(struct irq_info *info, cpumask_t applied_mask)
{ {
cpumask_t current_mask; cpumask_t current_mask;
char buf[PATH_MAX]; char buf[PATH_MAX];
char *line = NULL;
size_t size = 0;
FILE *file;
sprintf(buf, "/proc/irq/%i/smp_affinity", info->irq); sprintf(buf, "/proc/irq/%i/smp_affinity", info->irq);
file = fopen(buf, "r"); if (process_one_line(buf, get_mask_from_bitmap, &current_mask) < 0)
if (!file)
return 1; return 1;
if (getline(&line, &size, file)<=0) {
free(line);
fclose(file);
return 1;
}
cpumask_parse_user(line, strlen(line), current_mask);
fclose(file);
free(line);
return cpus_equal(applied_mask, current_mask); return cpus_equal(applied_mask, current_mask);
} }

View file

@ -350,10 +350,7 @@ static struct irq_info *add_one_irq_to_db(const char *devpath, struct irq_info *
int numa_node; int numa_node;
char path[PATH_MAX]; char path[PATH_MAX];
FILE *fd; FILE *fd;
char *lcpu_mask;
GList *entry; GList *entry;
ssize_t ret;
size_t blen;
/* /*
* First check to make sure this isn't a duplicate entry * First check to make sure this isn't a duplicate entry
@ -409,23 +406,11 @@ get_numa_node:
else else
new->numa_node = get_numa_node(numa_node); new->numa_node = get_numa_node(numa_node);
sprintf(path, "%s/local_cpus", devpath);
fd = fopen(path, "r");
if (!fd) {
cpus_setall(new->cpumask); cpus_setall(new->cpumask);
goto out;
}
lcpu_mask = NULL;
ret = getline(&lcpu_mask, &blen, fd);
fclose(fd);
if (ret <= 0) {
cpus_setall(new->cpumask);
} else {
cpumask_parse_user(lcpu_mask, ret, new->cpumask);
}
free(lcpu_mask);
out: sprintf(path, "%s/local_cpus", devpath);
process_one_line(path, get_mask_from_bitmap, &new->cpumask);
log(TO_CONSOLE, LOG_INFO, "Adding IRQ %d to database\n", irq); log(TO_CONSOLE, LOG_INFO, "Adding IRQ %d to database\n", irq);
return new; return new;
} }

130
cputree.c
View file

@ -59,6 +59,37 @@ cpumask_t cpu_possible_map;
*/ */
cpumask_t unbanned_cpus; cpumask_t unbanned_cpus;
int process_one_line(char *path, void (*cb)(char *line, void *data), void *data)
{
FILE *file;
char *line = NULL;
size_t size = 0;
int ret = -1;
file = fopen(path, "r");
if (!file)
return ret;
if (getline(&line, &size, file) > 0) {
cb(line, data);
ret = 0;
}
free(line);
fclose(file);
return ret;
}
void get_mask_from_bitmap(char *line, void *mask)
{
cpumask_parse_user(line, strlen(line), *(cpumask_t *)mask);
}
static void get_mask_from_cpulist(char *line, void *mask)
{
if (strlen(line) && line[0] != '\n')
cpulist_parse(line, strlen(line), *(cpumask_t *)mask);
}
/* /*
* By default do not place IRQs on CPUs the kernel keeps isolated or * By default do not place IRQs on CPUs the kernel keeps isolated or
* nohz_full, as specified through the boot commandline. Users can * nohz_full, as specified through the boot commandline. Users can
@ -66,9 +97,7 @@ cpumask_t unbanned_cpus;
*/ */
static void setup_banned_cpus(void) static void setup_banned_cpus(void)
{ {
FILE *file; char *path = NULL;
char *line = NULL;
size_t size = 0;
char buffer[4096]; char buffer[4096];
cpumask_t nohz_full; cpumask_t nohz_full;
cpumask_t isolated_cpus; cpumask_t isolated_cpus;
@ -86,29 +115,12 @@ static void setup_banned_cpus(void)
cpumask_parse_user(getenv("IRQBALANCE_BANNED_CPUS"), strlen(getenv("IRQBALANCE_BANNED_CPUS")), banned_cpus); cpumask_parse_user(getenv("IRQBALANCE_BANNED_CPUS"), strlen(getenv("IRQBALANCE_BANNED_CPUS")), banned_cpus);
goto out; goto out;
} }
file = fopen("/sys/devices/system/cpu/isolated", "r");
if (file) {
if (getline(&line, &size, file) > 0) {
if (strlen(line) && line[0] != '\n')
cpulist_parse(line, strlen(line), isolated_cpus);
}
free(line);
line = NULL;
size = 0;
fclose(file);
}
file = fopen("/sys/devices/system/cpu/nohz_full", "r"); path = "/sys/devices/system/cpu/isolated";
if (file) { process_one_line(path, get_mask_from_cpulist, &isolated_cpus);
if (getline(&line, &size, file) > 0) {
if (strlen(line) && line[0] != '\n') path = "/sys/devices/system/cpu/nohz_full";
cpulist_parse(line, strlen(line), nohz_full); process_one_line(path, get_mask_from_cpulist, &nohz_full);
}
free(line);
line = NULL;
size = 0;
fclose(file);
}
cpus_or(banned_cpus, nohz_full, isolated_cpus); cpus_or(banned_cpus, nohz_full, isolated_cpus);
@ -258,11 +270,24 @@ static struct topo_obj* add_cpu_to_cache_domain(struct topo_obj *cpu,
return cache; return cache;
} }
static void get_offline_status(char *line, void *data)
{
int *status = (int *)data;
*status = (line && line[0] == '0') ? 1 : 0;
}
static void get_packageid(char *line, void *data)
{
int *packageid = (int *)data;
*packageid = strtoul(line, NULL, 10);
}
#define ADJ_SIZE(r,s) PATH_MAX-strlen(r)-strlen(#s) #define ADJ_SIZE(r,s) PATH_MAX-strlen(r)-strlen(#s)
static void do_one_cpu(char *path) static void do_one_cpu(char *path)
{ {
struct topo_obj *cpu; struct topo_obj *cpu;
FILE *file;
char new_path[PATH_MAX]; char new_path[PATH_MAX];
cpumask_t cache_mask, package_mask; cpumask_t cache_mask, package_mask;
struct topo_obj *cache; struct topo_obj *cache;
@ -271,21 +296,13 @@ static void do_one_cpu(char *path)
int nodeid; int nodeid;
int packageid = 0; int packageid = 0;
unsigned int max_cache_index, cache_index, cache_stat; unsigned int max_cache_index, cache_index, cache_stat;
int ret = 1; int offline_status = 0;
/* skip offline cpus */ /* skip offline cpus */
snprintf(new_path, ADJ_SIZE(path,"/online"), "%s/online", path); snprintf(new_path, ADJ_SIZE(path,"/online"), "%s/online", path);
file = fopen(new_path, "r"); process_one_line(new_path, get_offline_status, &offline_status);
if (file) { if (offline_status)
char *line = NULL;
size_t size = 0;
if (getline(&line, &size, file)>0)
ret = (line && line[0]=='0') ? 1 : 0;
fclose(file);
free(line);
if (ret)
return; return;
}
cpu = calloc(sizeof(struct topo_obj), 1); cpu = calloc(sizeof(struct topo_obj), 1);
if (!cpu) if (!cpu)
@ -313,36 +330,17 @@ static void do_one_cpu(char *path)
return; return;
} }
cpu_set(cpu->number, package_mask);
/* try to read the package mask; if it doesn't exist assume solitary */ /* try to read the package mask; if it doesn't exist assume solitary */
snprintf(new_path, ADJ_SIZE(path, "/topology/core_siblings"), snprintf(new_path, ADJ_SIZE(path, "/topology/core_siblings"),
"%s/topology/core_siblings", path); "%s/topology/core_siblings", path);
file = fopen(new_path, "r"); process_one_line(new_path, get_mask_from_bitmap, &package_mask);
cpu_set(cpu->number, package_mask);
if (file) {
char *line = NULL;
size_t size = 0;
if (getline(&line, &size, file) > 0)
cpumask_parse_user(line, strlen(line), package_mask);
fclose(file);
free(line);
line = NULL;
size = 0;
}
/* try to read the package id */ /* try to read the package id */
snprintf(new_path, ADJ_SIZE(path, "/topology/physical_package_id"), snprintf(new_path, ADJ_SIZE(path, "/topology/physical_package_id"),
"%s/topology/physical_package_id", path); "%s/topology/physical_package_id", path);
file = fopen(new_path, "r"); process_one_line(new_path, get_packageid, &packageid);
if (file) {
char *line = NULL;
size_t size = 0;
if (getline(&line, &size, file) > 0)
packageid = strtoul(line, NULL, 10);
fclose(file);
free(line);
line = NULL;
size = 0;
}
/* try to read the cache mask; if it doesn't exist assume solitary */ /* try to read the cache mask; if it doesn't exist assume solitary */
/* We want the deepest cache level available */ /* We want the deepest cache level available */
@ -367,17 +365,7 @@ static void do_one_cpu(char *path)
/* Extra 10 subtraction is for the max character length of %d */ /* Extra 10 subtraction is for the max character length of %d */
snprintf(new_path, ADJ_SIZE(path, "/cache/index%d/shared_cpu_map") - 10, snprintf(new_path, ADJ_SIZE(path, "/cache/index%d/shared_cpu_map") - 10,
"%s/cache/index%d/shared_cpu_map", path, max_cache_index); "%s/cache/index%d/shared_cpu_map", path, max_cache_index);
file = fopen(new_path, "r"); process_one_line(new_path, get_mask_from_bitmap, &cache_mask);
if (file) {
char *line = NULL;
size_t size = 0;
if (getline(&line, &size, file) > 0)
cpumask_parse_user(line, strlen(line), cache_mask);
fclose(file);
free(line);
line = NULL;
size = 0;
}
} }
nodeid=-1; nodeid=-1;

View file

@ -160,5 +160,8 @@ extern unsigned int log_mask;
#define SOCKET_PATH "irqbalance" #define SOCKET_PATH "irqbalance"
#define SOCKET_TMPFS "/run/irqbalance/" #define SOCKET_TMPFS "/run/irqbalance/"
extern int process_one_line(char *path, void (*cb)(char *line, void *data), void *data);
extern void get_mask_from_bitmap(char *line, void *mask);
#endif /* __INCLUDE_GUARD_IRQBALANCE_H_ */ #endif /* __INCLUDE_GUARD_IRQBALANCE_H_ */

24
numa.c
View file

@ -56,31 +56,15 @@ static void add_one_node(const char *nodename)
{ {
char path[PATH_MAX]; char path[PATH_MAX];
struct topo_obj *new; struct topo_obj *new;
char *cpustr = NULL;
FILE *f;
ssize_t ret;
size_t blen;
new = calloc(1, sizeof(struct topo_obj)); new = calloc(1, sizeof(struct topo_obj));
if (!new) if (!new)
return; return;
cpus_clear(new->mask);
sprintf(path, "%s/%s/cpumap", SYSFS_NODE_PATH, nodename); sprintf(path, "%s/%s/cpumap", SYSFS_NODE_PATH, nodename);
f = fopen(path, "r"); process_one_line(path, get_mask_from_bitmap, &new->mask);
if (!f) {
free(new);
return;
}
if (ferror(f)) {
cpus_clear(new->mask);
} else {
ret = getline(&cpustr, &blen, f);
if (ret <= 0)
cpus_clear(new->mask);
else
cpumask_parse_user(cpustr, ret, new->mask);
free(cpustr);
}
fclose(f);
new->obj_type = OBJ_TYPE_NODE; new->obj_type = OBJ_TYPE_NODE;
new->number = strtoul(&nodename[4], NULL, 10); new->number = strtoul(&nodename[4], NULL, 10);
new->obj_type_list = &numa_nodes; new->obj_type_list = &numa_nodes;