Skip to content
Permalink
Browse files

Merge pull request #142 from yeyunfeng-dev/master

use process_one_line() instead of fscanf() and remove redundancy check in add_one_irq_to_db()
  • Loading branch information
nhorman committed Nov 12, 2019
2 parents 3cbccb9 + 4ed5d04 commit f1fd0b5c43ad81189750fd3c035c425975aa2d76
Showing with 20 additions and 67 deletions.
  1. +4 −49 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;
}
@@ -294,17 +285,6 @@ void add_cl_banned_irq(int irq)
add_banned_irq(irq, &cl_banned_irqs);
}

static int is_banned_irq(int irq)
{
GList *entry;
struct irq_info find;

find.irq = irq;

entry = g_list_find_custom(banned_irqs, &find, compare_ints);
return entry ? 1:0;
}

gint substr_find(gconstpointer a, gconstpointer b)
{
if (strstr(b, a))
@@ -349,22 +329,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;

/*
* First check to make sure this isn't a duplicate entry
*/
entry = g_list_find_custom(interrupts_db, hint, compare_ints);
if (entry) {
log(TO_CONSOLE, LOG_INFO, "DROPPING DUPLICATE ENTRY FOR IRQ %d on path %s\n", irq, devpath);
return NULL;
}

if (is_banned_irq(irq)) {
log(TO_ALL, LOG_INFO, "SKIPPING BANNED IRQ %d\n", irq);
return NULL;
}

new = calloc(1, sizeof(struct irq_info));
if (!new)
@@ -394,11 +358,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 +579,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 +620,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 +649,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 f1fd0b5

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