Skip to content
Permalink
Browse files

irqbalance: use process_one_line() instead of fscanf()

The logic using fscanf() to get data from open file can be instead by
process_one_line(), and provide two functions get_hex() and get_int()
to convert string to integer. also use get_int() instead of
get_offline_status() and get_packageid().

Signed-off-by: Yunfeng Ye <yeyunfeng@huawei.com>
  • Loading branch information
yeyunfeng-dev committed Nov 10, 2019
1 parent 3cbccb9 commit 56679afd67ba2706387407ea258eb0d2413c0c1e
Showing with 20 additions and 41 deletions.
  1. +4 −23 classify.c
  2. +14 −18 cputree.c
  3. +2 −0 irqbalance.h
@@ -187,20 +187,11 @@ static int map_pci_irq_class(unsigned int pci_class)
static unsigned int read_pci_data(const char *devpath, const char* file)
{
char path[PATH_MAX];
FILE *fd;
unsigned int data = PCI_INVAL_DATA;

sprintf(path, "%s/%s", devpath, file);

fd = fopen(path, "r");

if (!fd) {
log(TO_CONSOLE, LOG_WARNING, "PCI: can't open file:%s\n", path);
return data;
}

(void) fscanf(fd, "%x", &data);
fclose(fd);
if (process_one_line(path, get_hex, &data) < 0)
log(TO_CONSOLE, LOG_WARNING, "PCI: can't get from file:%s\n", path);

return data;
}
@@ -349,7 +340,6 @@ static struct irq_info *add_one_irq_to_db(const char *devpath, struct irq_info *
struct irq_info *new;
int numa_node;
char path[PATH_MAX];
FILE *fd;
GList *entry;

/*
@@ -394,11 +384,7 @@ static struct irq_info *add_one_irq_to_db(const char *devpath, struct irq_info *
numa_node = NUMA_NO_NODE;
if (devpath != NULL && numa_avail) {
sprintf(path, "%s/numa_node", devpath);
fd = fopen(path, "r");
if (fd) {
fscanf(fd, "%d", &numa_node);
fclose(fd);
}
process_one_line(path, get_int, &numa_node);
}

if (pol->numa_node_set == 1)
@@ -619,7 +605,6 @@ static void build_one_dev_entry(const char *dirname, GList *tmp_irqs)
{
struct dirent *entry;
DIR *msidir;
FILE *fd;
int irqnum;
struct irq_info *new, hint;
char path[PATH_MAX];
@@ -661,10 +646,7 @@ static void build_one_dev_entry(const char *dirname, GList *tmp_irqs)
}

sprintf(path, "%s/%s/irq", SYSPCI_DIR, dirname);
fd = fopen(path, "r");
if (!fd)
return;
if (fscanf(fd, "%d", &irqnum) < 0)
if (process_one_line(path, get_int, &irqnum) < 0)
goto done;

/*
@@ -693,7 +675,6 @@ static void build_one_dev_entry(const char *dirname, GList *tmp_irqs)
}

done:
fclose(fd);
return;
}

@@ -78,6 +78,16 @@ int process_one_line(char *path, void (*cb)(char *line, void *data), void *data)
return ret;
}

void get_hex(char *line, void *data)
{
*(int *)data = strtoul(line, NULL, 16);
}

void get_int(char *line, void *data)
{
*(int *)data = strtoul(line, NULL, 10);
}

void get_mask_from_bitmap(char *line, void *mask)
{
cpumask_parse_user(line, strlen(line), *(cpumask_t *)mask);
@@ -244,20 +254,6 @@ static struct topo_obj* add_cpu_to_cache_domain(struct topo_obj *cpu,
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)
static void do_one_cpu(char *path)
{
@@ -270,12 +266,12 @@ static void do_one_cpu(char *path)
int nodeid;
int packageid = 0;
unsigned int max_cache_index, cache_index, cache_stat;
int offline_status = 0;
int online_status = 1;

/* skip offline cpus */
snprintf(new_path, ADJ_SIZE(path,"/online"), "%s/online", path);
process_one_line(new_path, get_offline_status, &offline_status);
if (offline_status)
process_one_line(new_path, get_int, &online_status);
if (!online_status)
return;

cpu = calloc(1, sizeof(struct topo_obj));
@@ -315,7 +311,7 @@ static void do_one_cpu(char *path)
/* try to read the package id */
snprintf(new_path, ADJ_SIZE(path, "/topology/physical_package_id"),
"%s/topology/physical_package_id", path);
process_one_line(new_path, get_packageid, &packageid);
process_one_line(new_path, get_int, &packageid);

/* try to read the cache mask; if it doesn't exist assume solitary */
/* We want the deepest cache level available */
@@ -163,6 +163,8 @@ extern unsigned int log_mask;

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);
extern void get_int(char *line, void *data);
extern void get_hex(char *line, void *data);

#endif /* __INCLUDE_GUARD_IRQBALANCE_H_ */

0 comments on commit 56679af

Please sign in to comment.
You can’t perform that action at this time.