Skip to contentSkip to conversation

perf config: Introduce perf_config_set class

PATCH Applied Taeung Song 3 participants 9 messages Mar 29, 2016 – Apr 2, 2016 perf events

v4 of 11 Applied Latest is v11 →

lore
Share & tools
lore
#0 lore raw

PATCH v4 1/4 perf config: Introduce perf_config_set class

This infrastructure code was designed for
upcoming features of perf-config.

That collect config key-value pairs from user and
system config files (i.e. user wide ~/.perfconfig
and system wide $(sysconfdir)/perfconfig)
to manage perf's configs.

Cc Namhyung Kim <[email protected]>
Cc Jiri Olsa <[email protected]>
---
 tools/perf/util/config.c | 171 +++++++++++++++++++++++++++++++++++++++++++++++
 tools/perf/util/config.h |  26 +++++++
 2 files changed, 197 insertions(+)
 create mode 100644 tools/perf/util/config.h
tools/perf/util/config.c +171 -0
index 4e72763..2dbf47c 100644
@@ -13,6 +13,7 @@
 #include <subcmd/exec-cmd.h>
 #include "util/hist.h"  /* perf_hist_config */
 #include "util/llvm-utils.h"   /* perf_llvm_config */
+#include "config.h"
 
 #define MAXNAME (256)
 
@@ -506,6 +507,176 @@ out:
 	return ret;
 }
 
+static struct perf_config_section *find_section(struct list_head *sections,
+						const char *section_name)
+{
+	struct perf_config_section *section;
+
+	list_for_each_entry(section, sections, list)
+		if (!strcmp(section->name, section_name))
+			return section;
+
+	return NULL;
+}
+
+static struct perf_config_item *find_config_item(const char *name,
+						 struct perf_config_section *section)
+{
+	struct perf_config_item *config_item;
+
+	list_for_each_entry(config_item, &section->config_items, list)
+		if (!strcmp(config_item->name, name))
+			return config_item;
+
+	return NULL;
+}
+
+static void find_config(struct list_head *sections,
+			struct perf_config_section **section,
+			struct perf_config_item **config_item,
+			const char *section_name, const char *name)
+{
+	*section = find_section(sections, section_name);
+
+	if (*section != NULL)
+		*config_item = find_config_item(name, *section);
+	else
+		*config_item = NULL;
+}
+
+static struct perf_config_section *add_section(struct list_head *sections,
+					       const char *section_name)
+{
+	struct perf_config_section *section = zalloc(sizeof(*section));
+
+	if (!section)
+		return NULL;
+
+	INIT_LIST_HEAD(&section->config_items);
+	section->name = strdup(section_name);
+	if (!section->name) {
+		pr_err("%s: strdup failed\n", __func__);
+		free(section);
+		return NULL;
+	}
+
+	list_add_tail(&section->list, sections);
+	return section;
+}
+
+static struct perf_config_item *add_config_item(struct perf_config_section *section,
+						const char *name)
+{
+	struct perf_config_item *config_item = zalloc(sizeof(*config_item));
+
+	if (!config_item)
+		return NULL;
+
+	config_item->name = strdup(name);
+	if (!name) {
+		pr_err("%s: strdup failed\n", __func__);
+		goto out_err;
+	}
+
+	list_add_tail(&config_item->list, &section->config_items);
+	return config_item;
+
+out_err:
+	free(config_item);
+	return NULL;
+}
+
+static int set_value(struct perf_config_item *config_item, const char *value)
+{
+	char *val = strdup(value);
+
+	if (!val)
+		return -1;
+
+	free(config_item->value);
+	config_item->value = val;
+	return 0;
+}
+
+static int collect_config(const char *var, const char *value,
+			  void *perf_config_set)
+{
+	int ret = -1;
+	char *ptr, *key;
+	char *section_name, *name;
+	struct perf_config_section *section = NULL;
+	struct perf_config_item *config_item = NULL;
+	struct perf_config_set *perf_configs = perf_config_set;
+	struct list_head *sections = &perf_configs->sections;
+
+	key = ptr = strdup(var);
+	if (!key) {
+		pr_err("%s: strdup failed\n", __func__);
+		return -1;
+	}
+
+	section_name = strsep(&ptr, ".");
+	name = ptr;
+	if (name == NULL || value == NULL)
+		goto out_free;
+
+	find_config(sections, &section, &config_item, section_name, name);
+
+	if (!section) {
+		section = add_section(sections, section_name);
+		if (!section)
+			goto out_free;
+	}
+	if (!config_item) {
+		config_item = add_config_item(section, name);
+		if (!config_item)
+			goto out_free;
+	}
+
+	ret = set_value(config_item, value);
+	return ret;
+
+out_free:
+	free(key);
+	perf_config_set__delete(perf_configs);
+	return -1;
+}
+
+struct perf_config_set *perf_config_set__new(void)
+{
+	struct perf_config_set *perf_configs = zalloc(sizeof(*perf_configs));
+
+	if (!perf_configs)
+		return NULL;
+
+	INIT_LIST_HEAD(&perf_configs->sections);
+	perf_config(collect_config, perf_configs);
+
+	return perf_configs;
+}
+
+void perf_config_set__delete(struct perf_config_set *perf_configs)
+{
+	struct perf_config_section *section, *section_tmp;
+	struct perf_config_item *config_item, *item_tmp;
+
+	list_for_each_entry_safe(section, section_tmp,
+				 &perf_configs->sections, list) {
+		list_for_each_entry_safe(config_item, item_tmp,
+					 &section->config_items, list) {
+			list_del(&config_item->list);
+			free(config_item->name);
+			free(config_item->value);
+			free(config_item);
+		}
+		list_del(&section->list);
+		free(section->name);
+		free(section);
+	}
+
+	free(perf_configs);
+}
+
 /*
  * Call this to report error for your variable that should not
  * get a boolean value (i.e. "[my] var" means "true").
tools/perf/util/config.h new file +26 -0
new file mode 100644
index 0000000..e270e51
@@ -0,0 +1,26 @@
+#ifndef __PERF_CONFIG_H
+#define __PERF_CONFIG_H
+
+#include <stdbool.h>
+#include <linux/list.h>
+
+struct perf_config_item {
+	char *name;
+	char *value;
+	struct list_head list;
+};
+
+struct perf_config_section {
+	char *name;
+	struct list_head config_items;
+	struct list_head list;
+};
+
+struct perf_config_set {
+	struct list_head sections;
+};
+
+struct perf_config_set *perf_config_set__new(void);
+void perf_config_set__delete(struct perf_config_set *perf_configs);
+
+#endif /* __PERF_CONFIG_H */
Signature · 1 line
2.5.0
#1 lore raw

Em Tue, Mar 29, 2016 at 09:43:13AM +0900, Taeung Song escreveu:

Quoted 11 lines

This infrastructure code was designed for
upcoming features of perf-config.

That collect config key-value pairs from user and
system config files (i.e. user wide ~/.perfconfig
and system wide $(sysconfdir)/perfconfig)
to manage perf's configs.

Cc Namhyung Kim <[email protected]>
Cc Jiri Olsa <[email protected]>

Waiting for ack.

Quoted 229 lines
---
 tools/perf/util/config.c | 171 +++++++++++++++++++++++++++++++++++++++++++++++
 tools/perf/util/config.h |  26 +++++++
 2 files changed, 197 insertions(+)
 create mode 100644 tools/perf/util/config.h
tools/perf/util/config.c +171 -0
index 4e72763..2dbf47c 100644
@@ -13,6 +13,7 @@
 #include <subcmd/exec-cmd.h>
 #include "util/hist.h"  /* perf_hist_config */
 #include "util/llvm-utils.h"   /* perf_llvm_config */
+#include "config.h"
 
 #define MAXNAME (256)
 
@@ -506,6 +507,176 @@ out:
 	return ret;
 }
 
+static struct perf_config_section *find_section(struct list_head *sections,
+						const char *section_name)
+{
+	struct perf_config_section *section;
+
+	list_for_each_entry(section, sections, list)
+		if (!strcmp(section->name, section_name))
+			return section;
+
+	return NULL;
+}
+
+static struct perf_config_item *find_config_item(const char *name,
+						 struct perf_config_section *section)
+{
+	struct perf_config_item *config_item;
+
+	list_for_each_entry(config_item, &section->config_items, list)
+		if (!strcmp(config_item->name, name))
+			return config_item;
+
+	return NULL;
+}
+
+static void find_config(struct list_head *sections,
+			struct perf_config_section **section,
+			struct perf_config_item **config_item,
+			const char *section_name, const char *name)
+{
+	*section = find_section(sections, section_name);
+
+	if (*section != NULL)
+		*config_item = find_config_item(name, *section);
+	else
+		*config_item = NULL;
+}
+
+static struct perf_config_section *add_section(struct list_head *sections,
+					       const char *section_name)
+{
+	struct perf_config_section *section = zalloc(sizeof(*section));
+
+	if (!section)
+		return NULL;
+
+	INIT_LIST_HEAD(&section->config_items);
+	section->name = strdup(section_name);
+	if (!section->name) {
+		pr_err("%s: strdup failed\n", __func__);
+		free(section);
+		return NULL;
+	}
+
+	list_add_tail(&section->list, sections);
+	return section;
+}
+
+static struct perf_config_item *add_config_item(struct perf_config_section *section,
+						const char *name)
+{
+	struct perf_config_item *config_item = zalloc(sizeof(*config_item));
+
+	if (!config_item)
+		return NULL;
+
+	config_item->name = strdup(name);
+	if (!name) {
+		pr_err("%s: strdup failed\n", __func__);
+		goto out_err;
+	}
+
+	list_add_tail(&config_item->list, &section->config_items);
+	return config_item;
+
+out_err:
+	free(config_item);
+	return NULL;
+}
+
+static int set_value(struct perf_config_item *config_item, const char *value)
+{
+	char *val = strdup(value);
+
+	if (!val)
+		return -1;
+
+	free(config_item->value);
+	config_item->value = val;
+	return 0;
+}
+
+static int collect_config(const char *var, const char *value,
+			  void *perf_config_set)
+{
+	int ret = -1;
+	char *ptr, *key;
+	char *section_name, *name;
+	struct perf_config_section *section = NULL;
+	struct perf_config_item *config_item = NULL;
+	struct perf_config_set *perf_configs = perf_config_set;
+	struct list_head *sections = &perf_configs->sections;
+
+	key = ptr = strdup(var);
+	if (!key) {
+		pr_err("%s: strdup failed\n", __func__);
+		return -1;
+	}
+
+	section_name = strsep(&ptr, ".");
+	name = ptr;
+	if (name == NULL || value == NULL)
+		goto out_free;
+
+	find_config(sections, &section, &config_item, section_name, name);
+
+	if (!section) {
+		section = add_section(sections, section_name);
+		if (!section)
+			goto out_free;
+	}
+	if (!config_item) {
+		config_item = add_config_item(section, name);
+		if (!config_item)
+			goto out_free;
+	}
+
+	ret = set_value(config_item, value);
+	return ret;
+
+out_free:
+	free(key);
+	perf_config_set__delete(perf_configs);
+	return -1;
+}
+
+struct perf_config_set *perf_config_set__new(void)
+{
+	struct perf_config_set *perf_configs = zalloc(sizeof(*perf_configs));
+
+	if (!perf_configs)
+		return NULL;
+
+	INIT_LIST_HEAD(&perf_configs->sections);
+	perf_config(collect_config, perf_configs);
+
+	return perf_configs;
+}
+
+void perf_config_set__delete(struct perf_config_set *perf_configs)
+{
+	struct perf_config_section *section, *section_tmp;
+	struct perf_config_item *config_item, *item_tmp;
+
+	list_for_each_entry_safe(section, section_tmp,
+				 &perf_configs->sections, list) {
+		list_for_each_entry_safe(config_item, item_tmp,
+					 &section->config_items, list) {
+			list_del(&config_item->list);
+			free(config_item->name);
+			free(config_item->value);
+			free(config_item);
+		}
+		list_del(&section->list);
+		free(section->name);
+		free(section);
+	}
+
+	free(perf_configs);
+}
+
 /*
  * Call this to report error for your variable that should not
  * get a boolean value (i.e. "[my] var" means "true").
tools/perf/util/config.h new file +26 -0
new file mode 100644
index 0000000..e270e51
@@ -0,0 +1,26 @@
+#ifndef __PERF_CONFIG_H
+#define __PERF_CONFIG_H
+
+#include <stdbool.h>
+#include <linux/list.h>
+
+struct perf_config_item {
+	char *name;
+	char *value;
+	struct list_head list;
+};
+
+struct perf_config_section {
+	char *name;
+	struct list_head config_items;
+	struct list_head list;
+};
+
+struct perf_config_set {
+	struct list_head sections;
+};
+
+struct perf_config_set *perf_config_set__new(void);
+void perf_config_set__delete(struct perf_config_set *perf_configs);
+
+#endif /* __PERF_CONFIG_H */
Signature · 1 line
2.5.0
#2 lore raw

Hi, Arnaldo and Namhyung

Quoted 9 lines · Arnaldo Carvalho de Melo

Em Tue, Mar 29, 2016 at 09:43:13AM +0900, Taeung Song escreveu:

Quoted 8 lines

This infrastructure code was designed for
upcoming features of perf-config.

That collect config key-value pairs from user and
system config files (i.e. user wide ~/.perfconfig
and system wide $(sysconfdir)/perfconfig)
to manage perf's configs.

Quoted 5 lines
Quoted 3 lines
Cc Namhyung Kim <[email protected]>
Cc Jiri Olsa <[email protected]>

Waiting for ack.

Namhyung,
The difference between v3 and v4 for this patch as below. (fill perf_config_set__delete() in collect_config() for state of error)

Can you review this patch, again ?

tools/perf/util/config.c +5 -3
index 725015f..2cfafff 100644
@@ -705,14 +706,15 @@ static int set_value(struct perf_config_item *config_item, const char *value)
 }
 
 static int collect_config(const char *var, const char *value,
-                          void *section_list)
+                          void *perf_config_set)
 {
         int ret = -1;
         char *ptr, *key;
         char *section_name, *name;
         struct perf_config_section *section = NULL;
         struct perf_config_item *config_item = NULL;
-        struct list_head *sections = section_list;
+        struct perf_config_set *perf_configs = perf_config_set;
+        struct list_head *sections = &perf_configs->sections;
 
         key = ptr = strdup(var);
         if (!key) {
@@ -743,7 +745,8 @@ static int collect_config(const char *var, const char *value,
 
 out_free:
         free(key);
-        return ret;
+        perf_config_set__delete(perf_configs);
+        return -1;
 }
 
 static struct perf_config_set *perf_config_set__init(struct 

perf_config_set *perf_configs)
@@ -777,7 +780,7 @@ struct perf_config_set *perf_config_set__new(void)

                 return NULL;

         perf_config_set__init(perf_configs);

- perf_config(collect_config, &perf_configs->sections);
+ perf_config(collect_config, perf_configs);

         return perf_configs;

}

Thanks,
Taeung

Quoted 229 lines
Quoted 229 lines
---
  tools/perf/util/config.c | 171 +++++++++++++++++++++++++++++++++++++++++++++++
  tools/perf/util/config.h |  26 +++++++
  2 files changed, 197 insertions(+)

create mode 100644 tools/perf/util/config.h

tools/perf/util/config.c +171 -0
index 4e72763..2dbf47c 100644
@@ -13,6 +13,7 @@
  #include <subcmd/exec-cmd.h>
  #include "util/hist.h"  /* perf_hist_config */
  #include "util/llvm-utils.h"   /* perf_llvm_config */
+#include "config.h"
 
  #define MAXNAME (256)
 
@@ -506,6 +507,176 @@ out:
  	return ret;
  }
 
+static struct perf_config_section *find_section(struct list_head *sections,
+						const char *section_name)
+{
+	struct perf_config_section *section;
+
+	list_for_each_entry(section, sections, list)
+		if (!strcmp(section->name, section_name))
+			return section;
+
+	return NULL;
+}
+
+static struct perf_config_item *find_config_item(const char *name,
+						 struct perf_config_section *section)
+{
+	struct perf_config_item *config_item;
+
+	list_for_each_entry(config_item, &section->config_items, list)
+		if (!strcmp(config_item->name, name))
+			return config_item;
+
+	return NULL;
+}
+
+static void find_config(struct list_head *sections,
+			struct perf_config_section **section,
+			struct perf_config_item **config_item,
+			const char *section_name, const char *name)
+{
+	*section = find_section(sections, section_name);
+
+	if (*section != NULL)
+		*config_item = find_config_item(name, *section);
+	else
+		*config_item = NULL;
+}
+
+static struct perf_config_section *add_section(struct list_head *sections,
+					       const char *section_name)
+{
+	struct perf_config_section *section = zalloc(sizeof(*section));
+
+	if (!section)
+		return NULL;
+
+	INIT_LIST_HEAD(&section->config_items);
+	section->name = strdup(section_name);
+	if (!section->name) {
+		pr_err("%s: strdup failed\n", __func__);
+		free(section);
+		return NULL;
+	}
+
+	list_add_tail(&section->list, sections);
+	return section;
+}
+
+static struct perf_config_item *add_config_item(struct perf_config_section *section,
+						const char *name)
+{
+	struct perf_config_item *config_item = zalloc(sizeof(*config_item));
+
+	if (!config_item)
+		return NULL;
+
+	config_item->name = strdup(name);
+	if (!name) {
+		pr_err("%s: strdup failed\n", __func__);
+		goto out_err;
+	}
+
+	list_add_tail(&config_item->list, &section->config_items);
+	return config_item;
+
+out_err:
+	free(config_item);
+	return NULL;
+}
+
+static int set_value(struct perf_config_item *config_item, const char *value)
+{
+	char *val = strdup(value);
+
+	if (!val)
+		return -1;
+
+	free(config_item->value);
+	config_item->value = val;
+	return 0;
+}
+
+static int collect_config(const char *var, const char *value,
+			  void *perf_config_set)
+{
+	int ret = -1;
+	char *ptr, *key;
+	char *section_name, *name;
+	struct perf_config_section *section = NULL;
+	struct perf_config_item *config_item = NULL;
+	struct perf_config_set *perf_configs = perf_config_set;
+	struct list_head *sections = &perf_configs->sections;
+
+	key = ptr = strdup(var);
+	if (!key) {
+		pr_err("%s: strdup failed\n", __func__);
+		return -1;
+	}
+
+	section_name = strsep(&ptr, ".");
+	name = ptr;
+	if (name == NULL || value == NULL)
+		goto out_free;
+
+	find_config(sections, &section, &config_item, section_name, name);
+
+	if (!section) {
+		section = add_section(sections, section_name);
+		if (!section)
+			goto out_free;
+	}
+	if (!config_item) {
+		config_item = add_config_item(section, name);
+		if (!config_item)
+			goto out_free;
+	}
+
+	ret = set_value(config_item, value);
+	return ret;
+
+out_free:
+	free(key);
+	perf_config_set__delete(perf_configs);
+	return -1;
+}
+
+struct perf_config_set *perf_config_set__new(void)
+{
+	struct perf_config_set *perf_configs = zalloc(sizeof(*perf_configs));
+
+	if (!perf_configs)
+		return NULL;
+
+	INIT_LIST_HEAD(&perf_configs->sections);
+	perf_config(collect_config, perf_configs);
+
+	return perf_configs;
+}
+
+void perf_config_set__delete(struct perf_config_set *perf_configs)
+{
+	struct perf_config_section *section, *section_tmp;
+	struct perf_config_item *config_item, *item_tmp;
+
+	list_for_each_entry_safe(section, section_tmp,
+				 &perf_configs->sections, list) {
+		list_for_each_entry_safe(config_item, item_tmp,
+					 &section->config_items, list) {
+			list_del(&config_item->list);
+			free(config_item->name);
+			free(config_item->value);
+			free(config_item);
+		}
+		list_del(&section->list);
+		free(section->name);
+		free(section);
+	}
+
+	free(perf_configs);
+}
+
  /*
   * Call this to report error for your variable that should not
   * get a boolean value (i.e. "[my] var" means "true").
tools/perf/util/config.h new file +26 -1
new file mode 100644
index 0000000..e270e51
@@ -0,0 +1,26 @@
+#ifndef __PERF_CONFIG_H
+#define __PERF_CONFIG_H
+
+#include <stdbool.h>
+#include <linux/list.h>
+
+struct perf_config_item {
+	char *name;
+	char *value;
+	struct list_head list;
+};
+
+struct perf_config_section {
+	char *name;
+	struct list_head config_items;
+	struct list_head list;
+};
+
+struct perf_config_set {
+	struct list_head sections;
+};
+
+struct perf_config_set *perf_config_set__new(void);
+void perf_config_set__delete(struct perf_config_set *perf_configs);
+
+#endif /* __PERF_CONFIG_H */
--

2.5.0

Acked-by
#3 lore raw
Quoted 16 lines · Taeung Song

Hi, Arnaldo and Namhyung

Quoted 13 lines · Arnaldo Carvalho de Melo

Em Tue, Mar 29, 2016 at 09:43:13AM +0900, Taeung Song escreveu:

Quoted 11 lines

This infrastructure code was designed for
upcoming features of perf-config.

That collect config key-value pairs from user and
system config files (i.e. user wide ~/.perfconfig
and system wide $(sysconfdir)/perfconfig)
to manage perf's configs.

Cc Namhyung Kim <[email protected]>
Cc Jiri Olsa <[email protected]>
Quoted 7 lines
Quoted 1 line

Waiting for ack.

Namhyung,
The difference between v3 and v4 for this patch as below. (fill perf_config_set__delete() in collect_config() for state of error)

Can you review this patch, again ?

Thanks
Namhyung

Quoted 286 lines
tools/perf/util/config.c +3 -2
index 725015f..2cfafff 100644
@@ -705,14 +706,15 @@ static int set_value(struct perf_config_item 
 *config_item, const char *value)
 }
 
 static int collect_config(const char *var, const char *value,
-                          void *section_list)
+                          void *perf_config_set)
 {
         int ret = -1;
         char *ptr, *key;
         char *section_name, *name;
         struct perf_config_section *section = NULL;
         struct perf_config_item *config_item = NULL;
-        struct list_head *sections = section_list;
+        struct perf_config_set *perf_configs = perf_config_set;
+        struct list_head *sections = &perf_configs->sections;
 
         key = ptr = strdup(var);
         if (!key) {
tools/perf/util/config.c +2 -1
@@ -743,7 +745,8 @@ static int collect_config(const char *var, const 
 char *value,
 
 out_free:
         free(key);
-        return ret;
+        perf_config_set__delete(perf_configs);
+        return -1;
 }
 

static struct perf_config_set *perf_config_set__init(struct
perf_config_set *perf_configs)

tools/perf/util/config.c +1 -1
@@ -777,7 +780,7 @@ struct perf_config_set *perf_config_set__new(void)
                 return NULL;
 
         perf_config_set__init(perf_configs);
-        perf_config(collect_config, &perf_configs->sections);
+        perf_config(collect_config, perf_configs);
 
         return perf_configs;
 }

Thanks,
Taeung

Quoted 2 lines
Quoted 2 lines
---
  tools/perf/util/config.c | 171

+++++++++++++++++++++++++++++++++++++++++++++++

Quoted 14 lines
Quoted 14 lines
  tools/perf/util/config.h |  26 +++++++
  2 files changed, 197 insertions(+)

create mode 100644 tools/perf/util/config.h

tools/perf/util/config.c +1 -0
index 4e72763..2dbf47c 100644
@@ -13,6 +13,7 @@
  #include <subcmd/exec-cmd.h>
  #include "util/hist.h"  /* perf_hist_config */
  #include "util/llvm-utils.h"   /* perf_llvm_config */
+#include "config.h"
 
Quoted 7 lines

tools/perf/util/config.c out: · line 510

Quoted 7 lines
tools/perf/util/config.c +1 -0
  #define MAXNAME (256)
 
@@ -506,6 +507,176 @@ out:
  	return ret;
  }
 
+static struct perf_config_section *find_section(struct list_head

*sections,

Quoted 29 lines
Quoted 29 lines
tools/perf/util/config.c +29 -0
+						const char *section_name)
+{
+	struct perf_config_section *section;
+
+	list_for_each_entry(section, sections, list)
+		if (!strcmp(section->name, section_name))
+			return section;
+
+	return NULL;
+}
+
+static struct perf_config_item *find_config_item(const char *name,
+						 struct perf_config_section *section)
+{
+	struct perf_config_item *config_item;
+
+	list_for_each_entry(config_item, &section->config_items, list)
+		if (!strcmp(config_item->name, name))
+			return config_item;
+
+	return NULL;
+}
+
+static void find_config(struct list_head *sections,
+			struct perf_config_section **section,
+			struct perf_config_item **config_item,
+			const char *section_name, const char *name)
+{
+	*section = find_section(sections, section_name);
Quoted 8 lines

tools/perf/util/config.c out: · line 547

Quoted 8 lines
tools/perf/util/config.c +8 -0
+
+	if (*section != NULL)
+		*config_item = find_config_item(name, *section);
+	else
+		*config_item = NULL;
+}
+
+static struct perf_config_section *add_section(struct list_head

*sections,

Quoted 12 lines
Quoted 12 lines
tools/perf/util/config.c +12 -0
+					       const char *section_name)
+{
+	struct perf_config_section *section = zalloc(sizeof(*section));
+
+	if (!section)
+		return NULL;
+
+	INIT_LIST_HEAD(&section->config_items);
+	section->name = strdup(section_name);
+	if (!section->name) {
+		pr_err("%s: strdup failed\n", __func__);
+		free(section);
Quoted 8 lines

tools/perf/util/config.c out: · line 567

Quoted 8 lines
tools/perf/util/config.c +8 -0
+		return NULL;
+	}
+
+	list_add_tail(&section->list, sections);
+	return section;
+}
+
+static struct perf_config_item *add_config_item(struct

perf_config_section *section,

Quoted 3 lines

tools/perf/util/config.c out: · line 570

Quoted 3 lines
tools/perf/util/config.c +3 -0
+						const char *name)
+{
+	struct perf_config_item *config_item =

zalloc(sizeof(*config_item));

Quoted 11 lines
Quoted 11 lines
tools/perf/util/config.c +11 -0
+
+	if (!config_item)
+		return NULL;
+
+	config_item->name = strdup(name);
+	if (!name) {
+		pr_err("%s: strdup failed\n", __func__);
+		goto out_err;
+	}
+
+	list_add_tail(&config_item->list, &section->config_items);
Quoted 8 lines

tools/perf/util/config.c out: · line 589

Quoted 8 lines
tools/perf/util/config.c +8 -0
+	return config_item;
+
+out_err:
+	free(config_item);
+	return NULL;
+}
+
+static int set_value(struct perf_config_item *config_item, const

char *value)

Quoted 50 lines
Quoted 50 lines
tools/perf/util/config.c +50 -0
+{
+	char *val = strdup(value);
+
+	if (!val)
+		return -1;
+
+	free(config_item->value);
+	config_item->value = val;
+	return 0;
+}
+
+static int collect_config(const char *var, const char *value,
+			  void *perf_config_set)
+{
+	int ret = -1;
+	char *ptr, *key;
+	char *section_name, *name;
+	struct perf_config_section *section = NULL;
+	struct perf_config_item *config_item = NULL;
+	struct perf_config_set *perf_configs = perf_config_set;
+	struct list_head *sections = &perf_configs->sections;
+
+	key = ptr = strdup(var);
+	if (!key) {
+		pr_err("%s: strdup failed\n", __func__);
+		return -1;
+	}
+
+	section_name = strsep(&ptr, ".");
+	name = ptr;
+	if (name == NULL || value == NULL)
+		goto out_free;
+
+	find_config(sections, &section, &config_item, section_name, name);
+
+	if (!section) {
+		section = add_section(sections, section_name);
+		if (!section)
+			goto out_free;
+	}
+	if (!config_item) {
+		config_item = add_config_item(section, name);
+		if (!config_item)
+			goto out_free;
+	}
+
+	ret = set_value(config_item, value);
+	return ret;
+
+out_free:
Quoted 8 lines

tools/perf/util/config.c out: · line 647

Quoted 8 lines
tools/perf/util/config.c +8 -0
+	free(key);
+	perf_config_set__delete(perf_configs);
+	return -1;
+}
+
+struct perf_config_set *perf_config_set__new(void)
+{
+	struct perf_config_set *perf_configs =

zalloc(sizeof(*perf_configs));

Quoted 69 lines
Quoted 69 lines
tools/perf/util/config.c +32 -0
+
+	if (!perf_configs)
+		return NULL;
+
+	INIT_LIST_HEAD(&perf_configs->sections);
+	perf_config(collect_config, perf_configs);
+
+	return perf_configs;
+}
+
+void perf_config_set__delete(struct perf_config_set *perf_configs)
+{
+	struct perf_config_section *section, *section_tmp;
+	struct perf_config_item *config_item, *item_tmp;
+
+	list_for_each_entry_safe(section, section_tmp,
+				 &perf_configs->sections, list) {
+		list_for_each_entry_safe(config_item, item_tmp,
+					 &section->config_items, list) {
+			list_del(&config_item->list);
+			free(config_item->name);
+			free(config_item->value);
+			free(config_item);
+		}
+		list_del(&section->list);
+		free(section->name);
+		free(section);
+	}
+
+	free(perf_configs);
+}
+
  /*
   * Call this to report error for your variable that should not
   * get a boolean value (i.e. "[my] var" means "true").
tools/perf/util/config.h new file +26 -1
new file mode 100644
index 0000000..e270e51
@@ -0,0 +1,26 @@
+#ifndef __PERF_CONFIG_H
+#define __PERF_CONFIG_H
+
+#include <stdbool.h>
+#include <linux/list.h>
+
+struct perf_config_item {
+	char *name;
+	char *value;
+	struct list_head list;
+};
+
+struct perf_config_section {
+	char *name;
+	struct list_head config_items;
+	struct list_head list;
+};
+
+struct perf_config_set {
+	struct list_head sections;
+};
+
+struct perf_config_set *perf_config_set__new(void);
+void perf_config_set__delete(struct perf_config_set *perf_configs);
+
+#endif /* __PERF_CONFIG_H */
--

2.5.0

Signature · 1 line
Sent from my Android device with K-9 Mail. Please excuse my brevity.
#4 lore raw

Hi, Namhyung

Quoted 19 lines · Namhyung Kim
Quoted 18 lines · Taeung Song

Hi, Arnaldo and Namhyung

Quoted 14 lines · Arnaldo Carvalho de Melo

Em Tue, Mar 29, 2016 at 09:43:13AM +0900, Taeung Song escreveu:

Quoted 11 lines

This infrastructure code was designed for
upcoming features of perf-config.

That collect config key-value pairs from user and
system config files (i.e. user wide ~/.perfconfig
and system wide $(sysconfdir)/perfconfig)
to manage perf's configs.

Cc Namhyung Kim <[email protected]>
Cc Jiri Olsa <[email protected]>

Waiting for ack.

Quoted 8 lines
Quoted 5 lines

Namhyung,
The difference between v3 and v4 for this patch as below. (fill perf_config_set__delete() in collect_config() for state of error)

Can you review this patch, again ?

Thank you !! :-)

Taeung

Quoted 290 lines
Quoted 288 lines
tools/perf/util/config.c +3 -2
index 725015f..2cfafff 100644
@@ -705,14 +706,15 @@ static int set_value(struct perf_config_item
 *config_item, const char *value)
  }
 
  static int collect_config(const char *var, const char *value,
-                          void *section_list)
+                          void *perf_config_set)
  {
          int ret = -1;
          char *ptr, *key;
          char *section_name, *name;
          struct perf_config_section *section = NULL;
          struct perf_config_item *config_item = NULL;
-        struct list_head *sections = section_list;
+        struct perf_config_set *perf_configs = perf_config_set;
+        struct list_head *sections = &perf_configs->sections;
 
          key = ptr = strdup(var);
          if (!key) {
tools/perf/util/config.c +2 -1
@@ -743,7 +745,8 @@ static int collect_config(const char *var, const
 char *value,
 
  out_free:
          free(key);
-        return ret;
+        perf_config_set__delete(perf_configs);
+        return -1;
  }
 

static struct perf_config_set *perf_config_set__init(struct
perf_config_set *perf_configs)

tools/perf/util/config.c +1 -1
@@ -777,7 +780,7 @@ struct perf_config_set *perf_config_set__new(void)
                  return NULL;
 
          perf_config_set__init(perf_configs);
-        perf_config(collect_config, &perf_configs->sections);
+        perf_config(collect_config, perf_configs);
 
          return perf_configs;
  }

Thanks,
Taeung

Quoted 2 lines
Quoted 2 lines
---
   tools/perf/util/config.c | 171

+++++++++++++++++++++++++++++++++++++++++++++++

Quoted 14 lines
Quoted 14 lines
   tools/perf/util/config.h |  26 +++++++
   2 files changed, 197 insertions(+)

create mode 100644 tools/perf/util/config.h

tools/perf/util/config.c +1 -0
index 4e72763..2dbf47c 100644
@@ -13,6 +13,7 @@
   #include <subcmd/exec-cmd.h>
   #include "util/hist.h"  /* perf_hist_config */
   #include "util/llvm-utils.h"   /* perf_llvm_config */
+#include "config.h"
 
Quoted 7 lines

tools/perf/util/config.c out: · line 510

Quoted 7 lines
tools/perf/util/config.c +1 -0
   #define MAXNAME (256)
 
@@ -506,6 +507,176 @@ out:
   	return ret;
   }
 
+static struct perf_config_section *find_section(struct list_head

*sections,

Quoted 29 lines
Quoted 29 lines
tools/perf/util/config.c +29 -0
+						const char *section_name)
+{
+	struct perf_config_section *section;
+
+	list_for_each_entry(section, sections, list)
+		if (!strcmp(section->name, section_name))
+			return section;
+
+	return NULL;
+}
+
+static struct perf_config_item *find_config_item(const char *name,
+						 struct perf_config_section *section)
+{
+	struct perf_config_item *config_item;
+
+	list_for_each_entry(config_item, &section->config_items, list)
+		if (!strcmp(config_item->name, name))
+			return config_item;
+
+	return NULL;
+}
+
+static void find_config(struct list_head *sections,
+			struct perf_config_section **section,
+			struct perf_config_item **config_item,
+			const char *section_name, const char *name)
+{
+	*section = find_section(sections, section_name);
Quoted 8 lines

tools/perf/util/config.c out: · line 547

Quoted 8 lines
tools/perf/util/config.c +8 -0
+
+	if (*section != NULL)
+		*config_item = find_config_item(name, *section);
+	else
+		*config_item = NULL;
+}
+
+static struct perf_config_section *add_section(struct list_head

*sections,

Quoted 12 lines
Quoted 12 lines
tools/perf/util/config.c +12 -0
+					       const char *section_name)
+{
+	struct perf_config_section *section = zalloc(sizeof(*section));
+
+	if (!section)
+		return NULL;
+
+	INIT_LIST_HEAD(&section->config_items);
+	section->name = strdup(section_name);
+	if (!section->name) {
+		pr_err("%s: strdup failed\n", __func__);
+		free(section);
Quoted 8 lines

tools/perf/util/config.c out: · line 567

Quoted 8 lines
tools/perf/util/config.c +8 -0
+		return NULL;
+	}
+
+	list_add_tail(&section->list, sections);
+	return section;
+}
+
+static struct perf_config_item *add_config_item(struct

perf_config_section *section,

Quoted 3 lines

tools/perf/util/config.c out: · line 570

Quoted 3 lines
tools/perf/util/config.c +3 -0
+						const char *name)
+{
+	struct perf_config_item *config_item =

zalloc(sizeof(*config_item));

Quoted 11 lines
Quoted 11 lines
tools/perf/util/config.c +11 -0
+
+	if (!config_item)
+		return NULL;
+
+	config_item->name = strdup(name);
+	if (!name) {
+		pr_err("%s: strdup failed\n", __func__);
+		goto out_err;
+	}
+
+	list_add_tail(&config_item->list, &section->config_items);
Quoted 8 lines

tools/perf/util/config.c out: · line 589

Quoted 8 lines
tools/perf/util/config.c +8 -0
+	return config_item;
+
+out_err:
+	free(config_item);
+	return NULL;
+}
+
+static int set_value(struct perf_config_item *config_item, const

char *value)

Quoted 50 lines
Quoted 50 lines
tools/perf/util/config.c +50 -0
+{
+	char *val = strdup(value);
+
+	if (!val)
+		return -1;
+
+	free(config_item->value);
+	config_item->value = val;
+	return 0;
+}
+
+static int collect_config(const char *var, const char *value,
+			  void *perf_config_set)
+{
+	int ret = -1;
+	char *ptr, *key;
+	char *section_name, *name;
+	struct perf_config_section *section = NULL;
+	struct perf_config_item *config_item = NULL;
+	struct perf_config_set *perf_configs = perf_config_set;
+	struct list_head *sections = &perf_configs->sections;
+
+	key = ptr = strdup(var);
+	if (!key) {
+		pr_err("%s: strdup failed\n", __func__);
+		return -1;
+	}
+
+	section_name = strsep(&ptr, ".");
+	name = ptr;
+	if (name == NULL || value == NULL)
+		goto out_free;
+
+	find_config(sections, &section, &config_item, section_name, name);
+
+	if (!section) {
+		section = add_section(sections, section_name);
+		if (!section)
+			goto out_free;
+	}
+	if (!config_item) {
+		config_item = add_config_item(section, name);
+		if (!config_item)
+			goto out_free;
+	}
+
+	ret = set_value(config_item, value);
+	return ret;
+
+out_free:
Quoted 8 lines

tools/perf/util/config.c out: · line 647

Quoted 8 lines
tools/perf/util/config.c +8 -0
+	free(key);
+	perf_config_set__delete(perf_configs);
+	return -1;
+}
+
+struct perf_config_set *perf_config_set__new(void)
+{
+	struct perf_config_set *perf_configs =

zalloc(sizeof(*perf_configs));

Quoted 69 lines
Quoted 69 lines
tools/perf/util/config.c +32 -0
+
+	if (!perf_configs)
+		return NULL;
+
+	INIT_LIST_HEAD(&perf_configs->sections);
+	perf_config(collect_config, perf_configs);
+
+	return perf_configs;
+}
+
+void perf_config_set__delete(struct perf_config_set *perf_configs)
+{
+	struct perf_config_section *section, *section_tmp;
+	struct perf_config_item *config_item, *item_tmp;
+
+	list_for_each_entry_safe(section, section_tmp,
+				 &perf_configs->sections, list) {
+		list_for_each_entry_safe(config_item, item_tmp,
+					 &section->config_items, list) {
+			list_del(&config_item->list);
+			free(config_item->name);
+			free(config_item->value);
+			free(config_item);
+		}
+		list_del(&section->list);
+		free(section->name);
+		free(section);
+	}
+
+	free(perf_configs);
+}
+
   /*
    * Call this to report error for your variable that should not
    * get a boolean value (i.e. "[my] var" means "true").
tools/perf/util/config.h new file +26 -1
new file mode 100644
index 0000000..e270e51
@@ -0,0 +1,26 @@
+#ifndef __PERF_CONFIG_H
+#define __PERF_CONFIG_H
+
+#include <stdbool.h>
+#include <linux/list.h>
+
+struct perf_config_item {
+	char *name;
+	char *value;
+	struct list_head list;
+};
+
+struct perf_config_section {
+	char *name;
+	struct list_head config_items;
+	struct list_head list;
+};
+
+struct perf_config_set {
+	struct list_head sections;
+};
+
+struct perf_config_set *perf_config_set__new(void);
+void perf_config_set__delete(struct perf_config_set *perf_configs);
+
+#endif /* __PERF_CONFIG_H */
--

2.5.0

#5 lore raw
In reply to Taeung Song

Em Tue, Mar 29, 2016 at 09:43:13AM +0900, Taeung Song escreveu:

Quoted 91 lines

This infrastructure code was designed for
upcoming features of perf-config.

That collect config key-value pairs from user and
system config files (i.e. user wide ~/.perfconfig
and system wide $(sysconfdir)/perfconfig)
to manage perf's configs.

Cc Namhyung Kim <[email protected]>
Cc Jiri Olsa <[email protected]>
---
 tools/perf/util/config.c | 171 +++++++++++++++++++++++++++++++++++++++++++++++
 tools/perf/util/config.h |  26 +++++++
 2 files changed, 197 insertions(+)
 create mode 100644 tools/perf/util/config.h
tools/perf/util/config.c +58 -0
index 4e72763..2dbf47c 100644
@@ -13,6 +13,7 @@
 #include <subcmd/exec-cmd.h>
 #include "util/hist.h"  /* perf_hist_config */
 #include "util/llvm-utils.h"   /* perf_llvm_config */
+#include "config.h"
 
 #define MAXNAME (256)
 
@@ -506,6 +507,176 @@ out:
 	return ret;
 }
 
+static struct perf_config_section *find_section(struct list_head *sections,
+						const char *section_name)
+{
+	struct perf_config_section *section;
+
+	list_for_each_entry(section, sections, list)
+		if (!strcmp(section->name, section_name))
+			return section;
+
+	return NULL;
+}
+
+static struct perf_config_item *find_config_item(const char *name,
+						 struct perf_config_section *section)
+{
+	struct perf_config_item *config_item;
+
+	list_for_each_entry(config_item, &section->config_items, list)
+		if (!strcmp(config_item->name, name))
+			return config_item;
+
+	return NULL;
+}
+
+static void find_config(struct list_head *sections,
+			struct perf_config_section **section,
+			struct perf_config_item **config_item,
+			const char *section_name, const char *name)
+{
+	*section = find_section(sections, section_name);
+
+	if (*section != NULL)
+		*config_item = find_config_item(name, *section);
+	else
+		*config_item = NULL;
 
+}
+
+static struct perf_config_section *add_section(struct list_head *sections,
+					       const char *section_name)
+{
+	struct perf_config_section *section = zalloc(sizeof(*section));
+
+	if (!section)
+		return NULL;
+
+	INIT_LIST_HEAD(&section->config_items);
+	section->name = strdup(section_name);
+	if (!section->name) {
+		pr_err("%s: strdup failed\n", __func__);
+		free(section);
+		return NULL;
+	}
+
+	list_add_tail(&section->list, sections);
+	return section;
+}
+

tools/perf/util/config.c out: · line 577

Quoted 10 lines
tools/perf/util/config.c +10 -0
+static struct perf_config_item *add_config_item(struct perf_config_section *section,
+						const char *name)
+{
+	struct perf_config_item *config_item = zalloc(sizeof(*config_item));
+
+	if (!config_item)
+		return NULL;
+
+	config_item->name = strdup(name);
+	if (!name) {

Huh? You're testing the wrong variable.

Quoted 28 lines
tools/perf/util/config.c +28 -0
+		pr_err("%s: strdup failed\n", __func__);
+		goto out_err;
+	}
+
+	list_add_tail(&config_item->list, &section->config_items);
+	return config_item;
+
+out_err:
+	free(config_item);
+	return NULL;
+}
+
+static int set_value(struct perf_config_item *config_item, const char *value)
+{
+	char *val = strdup(value);
+
+	if (!val)
+		return -1;
+
+	free(config_item->value);
+	config_item->value = val;
+	return 0;
+}
+
+static int collect_config(const char *var, const char *value,
+			  void *perf_config_set)
+{
+	int ret = -1;

tools/perf/util/config.c out: · line 614

Quoted 10 lines
tools/perf/util/config.c +10 -0
+	char *ptr, *key;
+	char *section_name, *name;
+	struct perf_config_section *section = NULL;
+	struct perf_config_item *config_item = NULL;
+	struct perf_config_set *perf_configs = perf_config_set;
+	struct list_head *sections = &perf_configs->sections;
+
+	key = ptr = strdup(var);
+	if (!key) {
+		pr_err("%s: strdup failed\n", __func__);

pr_debug()

tools/perf/util/config.c out: · line 623

Quoted 9 lines
tools/perf/util/config.c +9 -0
+		return -1;
+	}
+
+	section_name = strsep(&ptr, ".");
+	name = ptr;
+	if (name == NULL || value == NULL)
+		goto out_free;
+
+	find_config(sections, &section, &config_item, section_name, name);

This idiom is confusing, why not ditch this 'find_config()' function and do the searches here? I.e.:

	section = find_section(sections, section_name);

tools/perf/util/config.c out: · line 629

Quoted 5 lines
tools/perf/util/config.c +5 -0
+	if (!section) {
+		section = add_section(sections, section_name);
+		if (!section)
+			goto out_free;
+	}

config_item = find_config_item(name, section);

Quoted 17 lines
tools/perf/util/config.c +17 -0
+	if (!config_item) {
+		config_item = add_config_item(section, name);
+		if (!config_item)
+			goto out_free;
+	}
+
+	ret = set_value(config_item, value);
+	return ret;
+
+out_free:
+	free(key);
+	perf_config_set__delete(perf_configs);
+	return -1;
+}
+
+struct perf_config_set *perf_config_set__new(void)
+{

tools/perf/util/config.c out: · line 656

Quoted 10 lines
tools/perf/util/config.c +10 -0
+	struct perf_config_set *perf_configs = zalloc(sizeof(*perf_configs));
+
+	if (!perf_configs)
+		return NULL;
+
+	INIT_LIST_HEAD(&perf_configs->sections);
+	perf_config(collect_config, perf_configs);
+
+	return perf_configs;
+}

Usually for these short functions we could do it more compactly as:

struct perf_config_set *perf_config_set__new(void)

{
	struct perf_config_set *perf_configs = zalloc(sizeof(*perf_configs));

	if (perf_configs) {
		INIT_LIST_HEAD(&perf_configs->sections);
		perf_config(collect_config, perf_configs);
	}

	return perf_configs;
}

But I'm not dwelling on this...

Quoted 11 lines
tools/perf/util/config.c +11 -0
+void perf_config_set__delete(struct perf_config_set *perf_configs)
+{
+	struct perf_config_section *section, *section_tmp;
+	struct perf_config_item *config_item, *item_tmp;
+
+	list_for_each_entry_safe(section, section_tmp,
+				 &perf_configs->sections, list) {
+		list_for_each_entry_safe(config_item, item_tmp,
+					 &section->config_items, list) {
+			list_del(&config_item->list);
+			free(config_item->name);

tools/perf/util/config.c out: · line 678

Quoted 10 lines
tools/perf/util/config.c +10 -0
+			free(config_item->value);
+			free(config_item);
+		}
+		list_del(&section->list);
+		free(section->name);
+		free(section);
+	}
+
+	free(perf_configs);
+}

What is the problem with having perf_config_item__delete() and perf_config_section__delete() and then have it like below, also please rename those foo->list to foo->node.

void perf_config_item__delete(struct perf_config_item *item)

{
	zfree(&item->name);
	zfree(&item->value);
	free(item);
}

void perf_config_section__purge(struct perf_config_section *section)

{
	struct perf_config_item *item, *tmp;

	list_for_each_entry_safe(item, tmp, &section->items, node) {
		list_del_init(&item->node);
		perf_config_item__delete(item);
	}
}

void perf_config_section__delete(struct perf_config_section *section)

{
	perf_config_section__purge(section);
	zfree(&section->name);
	free(section);
}

void perf_config_set__purge(struct perf_config_set *set)

{
	struct perf_config_section *section, *tmp;

	list_for_each_entry_safe(section, tmp, &set->sections, node) {
		list_del_init(&section->node);
		perf_config_section__delete(section);
	}
}

void perf_config_set__delete(struct perf_config_set *set)

{
	perf_config_set__purge(set);
	free(set);
}

Using zfree() and list_del_init to NULL or poison the freed pointers helps with debugging, please use them.

Quoted 10 lines
tools/perf/util/config.c +1 -0
+
 /*
  * Call this to report error for your variable that should not
  * get a boolean value (i.e. "[my] var" means "true").
tools/perf/util/config.h new file +0 -0
new file mode 100644
index 0000000..e270e51

tools/perf/util/config.h line 10

Quoted 10 lines
tools/perf/util/config.h new file +10 -0
@@ -0,0 +1,26 @@
+#ifndef __PERF_CONFIG_H
+#define __PERF_CONFIG_H
+
+#include <stdbool.h>
+#include <linux/list.h>
+
+struct perf_config_item {
+	char *name;
+	char *value;
+	struct list_head list;

s/list/node/g

tools/perf/util/config.h line 15

Quoted 5 lines
tools/perf/util/config.h new file +5 -0
+};
+
+struct perf_config_section {
+	char *name;
+	struct list_head config_items;

s/config_items/items/g

tools/perf/util/config.h line 16

Quoted 1 line
tools/perf/util/config.h new file +1 -0
+	struct list_head list;

s/list/node/g

tools/perf/util/config.h line 20

Quoted 4 lines
tools/perf/util/config.h new file +4 -0
+};
+
+struct perf_config_set {
+	struct list_head sections;

See? Here you did it right, no point in having it as "config_sections"

tools/perf/util/config.h line 24

Quoted 4 lines
tools/perf/util/config.h new file +4 -0
+};
+
+struct perf_config_set *perf_config_set__new(void);
+void perf_config_set__delete(struct perf_config_set *perf_configs);
   void perf_config_set__delete(struct perf_config_set *set);
Quoted 4 lines
tools/perf/util/config.h new file +2 -0
+
+#endif /* __PERF_CONFIG_H */
Signature · 1 line
2.5.0
#6 lore raw

Hi, Arnaldo

Thank you for your review.

Quoted 96 lines · Arnaldo Carvalho de Melo

Em Tue, Mar 29, 2016 at 09:43:13AM +0900, Taeung Song escreveu:

Quoted 95 lines

This infrastructure code was designed for
upcoming features of perf-config.

That collect config key-value pairs from user and
system config files (i.e. user wide ~/.perfconfig
and system wide $(sysconfdir)/perfconfig)
to manage perf's configs.

Cc Namhyung Kim <[email protected]>
Cc Jiri Olsa <[email protected]>
---
  tools/perf/util/config.c | 171 +++++++++++++++++++++++++++++++++++++++++++++++
  tools/perf/util/config.h |  26 +++++++
  2 files changed, 197 insertions(+)

create mode 100644 tools/perf/util/config.h

tools/perf/util/config.c +62 -0
index 4e72763..2dbf47c 100644
@@ -13,6 +13,7 @@
  #include <subcmd/exec-cmd.h>
  #include "util/hist.h"  /* perf_hist_config */
  #include "util/llvm-utils.h"   /* perf_llvm_config */
+#include "config.h"
 
  #define MAXNAME (256)
 
@@ -506,6 +507,176 @@ out:
  	return ret;
  }
 
+static struct perf_config_section *find_section(struct list_head *sections,
+						const char *section_name)
+{
+	struct perf_config_section *section;
+
+	list_for_each_entry(section, sections, list)
+		if (!strcmp(section->name, section_name))
+			return section;
+
+	return NULL;
+}
+
+static struct perf_config_item *find_config_item(const char *name,
+						 struct perf_config_section *section)
+{
+	struct perf_config_item *config_item;
+
+	list_for_each_entry(config_item, &section->config_items, list)
+		if (!strcmp(config_item->name, name))
+			return config_item;
+
+	return NULL;
+}
+
+static void find_config(struct list_head *sections,
+			struct perf_config_section **section,
+			struct perf_config_item **config_item,
+			const char *section_name, const char *name)
+{
+	*section = find_section(sections, section_name);
+
+	if (*section != NULL)
+		*config_item = find_config_item(name, *section);
+	else
+		*config_item = NULL;
 
+}
+
+static struct perf_config_section *add_section(struct list_head *sections,
+					       const char *section_name)
+{
+	struct perf_config_section *section = zalloc(sizeof(*section));
+
+	if (!section)
+		return NULL;
+
+	INIT_LIST_HEAD(&section->config_items);
+	section->name = strdup(section_name);
+	if (!section->name) {
+		pr_err("%s: strdup failed\n", __func__);
+		free(section);
+		return NULL;
+	}
+
+	list_add_tail(&section->list, sections);
+	return section;
+}
+
+static struct perf_config_item *add_config_item(struct perf_config_section *section,
+						const char *name)
+{
+	struct perf_config_item *config_item = zalloc(sizeof(*config_item));
Quoted 9 lines

tools/perf/util/config.c out: · line 576

Quoted 6 lines
tools/perf/util/config.c +6 -0
+
+	if (!config_item)
+		return NULL;
+
+	config_item->name = strdup(name);
+	if (!name) {

Huh? You're testing the wrong variable.

Sorry, my stupid mistake..

Quoted 32 lines
Quoted 32 lines
tools/perf/util/config.c +32 -0
+		pr_err("%s: strdup failed\n", __func__);
+		goto out_err;
+	}
+
+	list_add_tail(&config_item->list, &section->config_items);
+	return config_item;
+
+out_err:
+	free(config_item);
+	return NULL;
+}
+
+static int set_value(struct perf_config_item *config_item, const char *value)
+{
+	char *val = strdup(value);
+
+	if (!val)
+		return -1;
+
+	free(config_item->value);
+	config_item->value = val;
+	return 0;
+}
+
+static int collect_config(const char *var, const char *value,
+			  void *perf_config_set)
+{
+	int ret = -1;
+	char *ptr, *key;
+	char *section_name, *name;
+	struct perf_config_section *section = NULL;
+	struct perf_config_item *config_item = NULL;
Quoted 9 lines

tools/perf/util/config.c out: · line 614

Quoted 6 lines
tools/perf/util/config.c +6 -0
+	struct perf_config_set *perf_configs = perf_config_set;
+	struct list_head *sections = &perf_configs->sections;
+
+	key = ptr = strdup(var);
+	if (!key) {
+		pr_err("%s: strdup failed\n", __func__);

pr_debug()

I'll change pr_err to pr_debug.
But why do use pr_debug at only this part ?

Quoted 13 lines

tools/perf/util/config.c out: · line 623

Quoted 9 lines
tools/perf/util/config.c +9 -0
+		return -1;
+	}
+
+	section_name = strsep(&ptr, ".");
+	name = ptr;
+	if (name == NULL || value == NULL)
+		goto out_free;
+
+	find_config(sections, &section, &config_item, section_name, name);

This idiom is confusing, why not ditch this 'find_config()' function and do the searches here? I.e.:

Quoted 1 line
	section = find_section(sections, section_name);

I got it.
I think it is needed to remove needless find_config() function as you said.

Quoted 7 lines

tools/perf/util/config.c out: · line 629

Quoted 5 lines
tools/perf/util/config.c +5 -0
+	if (!section) {
+		section = add_section(sections, section_name);
+		if (!section)
+			goto out_free;
+	}
  	config_item = find_config_item(name, section);

ok.

Quoted 42 lines
Quoted 17 lines
tools/perf/util/config.c +17 -0
+	if (!config_item) {
+		config_item = add_config_item(section, name);
+		if (!config_item)
+			goto out_free;
+	}
+
+	ret = set_value(config_item, value);
+	return ret;
+
+out_free:
+	free(key);
+	perf_config_set__delete(perf_configs);
+	return -1;
+}
+
+struct perf_config_set *perf_config_set__new(void)
+{

tools/perf/util/config.c out: · line 656

Quoted 10 lines
tools/perf/util/config.c +10 -0
+	struct perf_config_set *perf_configs = zalloc(sizeof(*perf_configs));
+
+	if (!perf_configs)
+		return NULL;
+
+	INIT_LIST_HEAD(&perf_configs->sections);
+	perf_config(collect_config, perf_configs);
+
+	return perf_configs;
+}

Usually for these short functions we could do it more compactly as:

struct perf_config_set *perf_config_set__new(void)

{
	struct perf_config_set *perf_configs = zalloc(sizeof(*perf_configs));

	if (perf_configs) {
		INIT_LIST_HEAD(&perf_configs->sections);
		perf_config(collect_config, perf_configs);
	}

	return perf_configs;
}
Quoted 2 lines

But I'm not dwelling on this...

I got it, too!

Quoted 17 lines
Quoted 17 lines
tools/perf/util/config.c +17 -0
+void perf_config_set__delete(struct perf_config_set *perf_configs)
+{
+	struct perf_config_section *section, *section_tmp;
+	struct perf_config_item *config_item, *item_tmp;
+
+	list_for_each_entry_safe(section, section_tmp,
+				 &perf_configs->sections, list) {
+		list_for_each_entry_safe(config_item, item_tmp,
+					 &section->config_items, list) {
+			list_del(&config_item->list);
+			free(config_item->name);
+			free(config_item->value);
+			free(config_item);
+		}
+		list_del(&section->list);
+		free(section->name);
+		free(section);
Quoted 9 lines

tools/perf/util/config.c out: · line 678

Quoted 4 lines
tools/perf/util/config.c +4 -0
+	}
+
+	free(perf_configs);
+}

What is the problem with having perf_config_item__delete() and perf_config_section__delete() and then have it like below, also please rename those foo->list to foo->node.

No problem!
OK, I'll rename it.

Quoted 34 lines

void perf_config_item__delete(struct perf_config_item *item)

{
	zfree(&item->name);
	zfree(&item->value);
	free(item);
}

void perf_config_section__purge(struct perf_config_section *section)

{
	struct perf_config_item *item, *tmp;

	list_for_each_entry_safe(item, tmp, &section->items, node) {
		list_del_init(&item->node);
		perf_config_item__delete(item);
	}
}

void perf_config_section__delete(struct perf_config_section *section)

{
	perf_config_section__purge(section);
	zfree(&section->name);
	free(section);
}

void perf_config_set__purge(struct perf_config_set *set)

{
	struct perf_config_section *section, *tmp;

	list_for_each_entry_safe(section, tmp, &set->sections, node) {
		list_del_init(&section->node);
		perf_config_section__delete(section);
	}
}
Quoted 8 lines

void perf_config_set__delete(struct perf_config_set *set)

{
	perf_config_set__purge(set);
	free(set);
}

Using zfree() and list_del_init to NULL or poison the freed pointers helps with debugging, please use them.

ok.

Quoted 35 lines
Quoted 11 lines
tools/perf/util/config.c +1 -0
+
  /*
   * Call this to report error for your variable that should not
   * get a boolean value (i.e. "[my] var" means "true").
tools/perf/util/config.h new file +0 -0
new file mode 100644
index 0000000..e270e51

tools/perf/util/config.h line 10

Quoted 10 lines
tools/perf/util/config.h new file +10 -0
@@ -0,0 +1,26 @@
+#ifndef __PERF_CONFIG_H
+#define __PERF_CONFIG_H
+
+#include <stdbool.h>
+#include <linux/list.h>
+
+struct perf_config_item {
+	char *name;
+	char *value;
+	struct list_head list;

s/list/node/g

tools/perf/util/config.h line 15

Quoted 5 lines
tools/perf/util/config.h new file +5 -0
+};
+
+struct perf_config_section {
+	char *name;
+	struct list_head config_items;

s/config_items/items/g

tools/perf/util/config.h line 16

Quoted 1 line
tools/perf/util/config.h new file +1 -0
+	struct list_head list;

s/list/node/g

Quoted 7 lines

tools/perf/util/config.h line 20

Quoted 4 lines
tools/perf/util/config.h new file +4 -0
+};
+
+struct perf_config_set {
+	struct list_head sections;

See? Here you did it right, no point in having it as "config_sections"

I'll rename it to 'config_sections'.

Quoted 7 lines

tools/perf/util/config.h line 24

Quoted 4 lines
tools/perf/util/config.h new file +4 -0
+};
+
+struct perf_config_set *perf_config_set__new(void);
+void perf_config_set__delete(struct perf_config_set *perf_configs);
    void perf_config_set__delete(struct perf_config_set *set);

OK, I'll use 'set' variable name instead of perf_configs on this source file.

I'll resend this patch after modifying what you said. :-)

Thanks,
Taeung

Quoted 4 lines
Quoted 4 lines
tools/perf/util/config.h new file +2 -1
+
+#endif /* __PERF_CONFIG_H */
--

2.5.0

#7 lore raw

Em Fri, Apr 01, 2016 at 07:27:22PM +0900, Taeung Song escreveu:

Quoted 13 lines · Arnaldo Carvalho de Melo
Quoted 11 lines

Em Tue, Mar 29, 2016 at 09:43:13AM +0900, Taeung Song escreveu:

Quoted 10 lines
tools/perf/util/config.c +10 -0
+static int collect_config(const char *var, const char *value,
+			  void *perf_config_set)
+{
+	int ret = -1;
+	char *ptr, *key;
+	char *section_name, *name;
+	struct perf_config_section *section = NULL;
+	struct perf_config_item *config_item = NULL;
+	struct perf_config_set *perf_configs = perf_config_set;
+	struct list_head *sections = &perf_configs->sections;
Quoted 8 lines
Quoted 5 lines

tools/perf/util/config.c out: · line 614

Quoted 3 lines
tools/perf/util/config.c +3 -0
+	key = ptr = strdup(var);
+	if (!key) {
+		pr_err("%s: strdup failed\n", __func__);

pr_debug()

I'll change pr_err to pr_debug.
But why do use pr_debug at only this part ?

Well, ideally one would propagate the errors from library level code to the code in the tool, i.e. closer to builtin-foo.c, where it would decide how to present it to the user.

For extra messages, that may help a more advanced user or to be sent to the tool developers, use pr_debug().

<SNIP>

Quoted 9 lines
Quoted 9 lines

tools/perf/util/config.h line 15

Quoted 3 lines
tools/perf/util/config.h new file +3 -0
+struct perf_config_section {
+	char *name;
+	struct list_head config_items;

s/config_items/items/g

Review

Quoted 1 line
(quoted diff) +1 -0
+	struct list_head list;

s/list/node/g

Quoted 8 lines
Quoted 6 lines

tools/perf/util/config.h line 20

Quoted 4 lines
tools/perf/util/config.h new file +4 -0
+};
+
+struct perf_config_set {
+	struct list_head sections;

See? Here you did it right, no point in having it as "config_sections"

I'll rename it to 'config_sections'.

I meant to keep this one like it is, i.e. perf_config_set->sections, and use the same principle: shorter names, removing useless stuff like "config_", that in this case can be implied by the name of the class, and apply it to the perf_config_section case, making it perf_config_section->items, ok?

- Arnaldo

#8 lore raw
Quoted 23 lines · Arnaldo Carvalho de Melo

Em Fri, Apr 01, 2016 at 07:27:22PM +0900, Taeung Song escreveu:

Quoted 13 lines · Arnaldo Carvalho de Melo
Quoted 11 lines

Em Tue, Mar 29, 2016 at 09:43:13AM +0900, Taeung Song escreveu:

Quoted 10 lines
tools/perf/util/config.c +10 -0
+static int collect_config(const char *var, const char *value,
+			  void *perf_config_set)
+{
+	int ret = -1;
+	char *ptr, *key;
+	char *section_name, *name;
+	struct perf_config_section *section = NULL;
+	struct perf_config_item *config_item = NULL;
+	struct perf_config_set *perf_configs = perf_config_set;
+	struct list_head *sections = &perf_configs->sections;
Quoted 8 lines
Quoted 5 lines

tools/perf/util/config.c out: · line 614

Quoted 3 lines
tools/perf/util/config.c +3 -0
+	key = ptr = strdup(var);
+	if (!key) {
+		pr_err("%s: strdup failed\n", __func__);

pr_debug()

I'll change pr_err to pr_debug.
But why do use pr_debug at only this part ?

Quoted 6 lines

Well, ideally one would propagate the errors from library level code to the code in the tool, i.e. closer to builtin-foo.c, where it would decide how to present it to the user.

For extra messages, that may help a more advanced user or to be sent to the tool developers, use pr_debug().

I understood it!

Quoted 17 lines
Quoted 16 lines
Quoted 16 lines

tools/perf/util/config.h line 15

Quoted 4 lines
tools/perf/util/config.h new file +3 -0
+struct perf_config_section {
+	char *name;
+	struct list_head config_items;

s/config_items/items/g

Review

Quoted 1 line
(quoted diff) +1 -0
+	struct list_head list;

s/list/node/g

tools/perf/util/config.h line 20

Quoted 4 lines
tools/perf/util/config.h new file +4 -0
+};
+
+struct perf_config_set {
+	struct list_head sections;

See? Here you did it right, no point in having it as "config_sections"

Quoted 7 lines
Quoted 1 line

I'll rename it to 'config_sections'.

I meant to keep this one like it is, i.e. perf_config_set->sections, and use the same principle: shorter names, removing useless stuff like "config_", that in this case can be implied by the name of the class, and apply it to the perf_config_section case, making it perf_config_section->items, ok?

I got it. :-)

I'll resend v5

Thanks,
Taeung