Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1370485 > unrolled thread

[PATCH v6 1/4] perf config: Introduce perf_config_set class

Started byTaeung Song <treeze.taeung@gmail.com>
First post2016-04-04 11:20 +0200
Last post2016-04-04 14:50 +0200
Articles 3 — 2 participants

Back to article view | Back to linux.kernel

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  [PATCH v6 1/4] perf config: Introduce perf_config_set class Taeung Song <treeze.taeung@gmail.com> - 2016-04-04 11:20 +0200
    Re: [PATCH v6 1/4] perf config: Introduce perf_config_set class Taeung Song <treeze.taeung@gmail.com> - 2016-04-04 14:50 +0200
    Re: [PATCH v6 1/4] perf config: Introduce perf_config_set class Masami Hiramatsu <mhiramat@kernel.org> - 2016-04-04 14:50 +0200

#1370485 — [PATCH v6 1/4] perf config: Introduce perf_config_set class

FromTaeung Song <treeze.taeung@gmail.com>
Date2016-04-04 11:20 +0200
Subject[PATCH v6 1/4] perf config: Introduce perf_config_set class
Message-ID<rk8PU-S8-17@gated-at.bofh.it>
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 <namhyung@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Signed-off-by: Taeung Song <treeze.taeung@gmail.com>
---
 tools/perf/util/config.c | 173 +++++++++++++++++++++++++++++++++++++++++++++++
 tools/perf/util/config.h |  26 +++++++
 2 files changed, 199 insertions(+)
 create mode 100644 tools/perf/util/config.h

diff --git a/tools/perf/util/config.c b/tools/perf/util/config.c
index 5c20d78..53e756e 100644
--- a/tools/perf/util/config.c
+++ b/tools/perf/util/config.c
@@ -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)
 
@@ -524,6 +525,178 @@ 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, node)
+		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 *item;
+
+	list_for_each_entry(item, &section->items, node)
+		if (!strcmp(item->name, name))
+			return item;
+
+	return 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->items);
+	section->name = strdup(section_name);
+	if (!section->name) {
+		pr_debug("%s: strdup failed\n", __func__);
+		free(section);
+		return NULL;
+	}
+
+	list_add_tail(&section->node, sections);
+	return section;
+}
+
+static struct perf_config_item *add_config_item(struct perf_config_section *section,
+						const char *name)
+{
+	struct perf_config_item *item = zalloc(sizeof(*item));
+
+	if (!item)
+		return NULL;
+
+	item->name = strdup(name);
+	if (!item->name) {
+		pr_debug("%s: strdup failed\n", __func__);
+		free(item);
+		return NULL;
+	}
+
+	list_add_tail(&item->node, &section->items);
+	return item;
+}
+
+static int set_value(struct perf_config_item *item, const char *value)
+{
+	char *val = strdup(value);
+
+	if (!val)
+		return -1;
+
+	free(item->value);
+	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 *item = NULL;
+	struct perf_config_set *set = perf_config_set;
+	struct list_head *sections = &set->sections;
+
+	key = ptr = strdup(var);
+	if (!key) {
+		pr_debug("%s: strdup failed\n", __func__);
+		return -1;
+	}
+
+	section_name = strsep(&ptr, ".");
+	name = ptr;
+	if (name == NULL || value == NULL)
+		goto out_free;
+
+	section = find_section(sections, section_name);
+	if (!section) {
+		section = add_section(sections, section_name);
+		if (!section)
+			goto out_free;
+	}
+
+	item = find_config_item(name, section);
+	if (!item) {
+		item = add_config_item(section, name);
+		if (!item)
+			goto out_free;
+	}
+
+	ret = set_value(item, value);
+	return ret;
+
+out_free:
+	free(key);
+	perf_config_set__delete(set);
+	return -1;
+}
+
+struct perf_config_set *perf_config_set__new(void)
+{
+	struct perf_config_set *set = zalloc(sizeof(*set));
+
+	if (set) {
+		INIT_LIST_HEAD(&set->sections);
+		perf_config(collect_config, set);
+	}
+
+	return set;
+}
+
+static void perf_config_item__delete(struct perf_config_item *item)
+{
+	zfree(&item->name);
+	zfree(&item->value);
+	free(item);
+}
+
+static 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);
+	}
+}
+
+static void perf_config_section__delete(struct perf_config_section *section)
+{
+	perf_config_section__purge(section);
+	zfree(&section->name);
+	free(section);
+}
+
+static 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);
+}
+
 /*
  * Call this to report error for your variable that should not
  * get a boolean value (i.e. "[my] var" means "true").
diff --git a/tools/perf/util/config.h b/tools/perf/util/config.h
new file mode 100644
index 0000000..22ec626
--- /dev/null
+++ b/tools/perf/util/config.h
@@ -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 node;
+};
+
+struct perf_config_section {
+	char *name;
+	struct list_head items;
+	struct list_head node;
+};
+
+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 *set);
+
+#endif /* __PERF_CONFIG_H */
-- 
2.5.0

[toc] | [next] | [standalone]


#1370598

FromTaeung Song <treeze.taeung@gmail.com>
Date2016-04-04 14:50 +0200
Message-ID<rkc78-34X-9@gated-at.bofh.it>
In reply to#1370485

On 04/04/2016 09:41 PM, Masami Hiramatsu wrote:
> On Mon,  4 Apr 2016 18:17:04 +0900
> Taeung Song <treeze.taeung@gmail.com> wrote:
>
>> 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.
>>
> Looks good to me:)
>
> Reviewed-by: Masami Hiramatsu <mhiramat@kernel.org>
>
> Thanks!

Thank you !! :-)

Taeung

>
>> Cc: Namhyung Kim <namhyung@kernel.org>
>> Cc: Jiri Olsa <jolsa@kernel.org>
>> Signed-off-by: Taeung Song <treeze.taeung@gmail.com>
>> ---
>>   tools/perf/util/config.c | 173 +++++++++++++++++++++++++++++++++++++++++++++++
>>   tools/perf/util/config.h |  26 +++++++
>>   2 files changed, 199 insertions(+)
>>   create mode 100644 tools/perf/util/config.h
>>
>> diff --git a/tools/perf/util/config.c b/tools/perf/util/config.c
>> index 5c20d78..53e756e 100644
>> --- a/tools/perf/util/config.c
>> +++ b/tools/perf/util/config.c
>> @@ -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)
>>
>> @@ -524,6 +525,178 @@ 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, node)
>> +		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 *item;
>> +
>> +	list_for_each_entry(item, &section->items, node)
>> +		if (!strcmp(item->name, name))
>> +			return item;
>> +
>> +	return 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->items);
>> +	section->name = strdup(section_name);
>> +	if (!section->name) {
>> +		pr_debug("%s: strdup failed\n", __func__);
>> +		free(section);
>> +		return NULL;
>> +	}
>> +
>> +	list_add_tail(&section->node, sections);
>> +	return section;
>> +}
>> +
>> +static struct perf_config_item *add_config_item(struct perf_config_section *section,
>> +						const char *name)
>> +{
>> +	struct perf_config_item *item = zalloc(sizeof(*item));
>> +
>> +	if (!item)
>> +		return NULL;
>> +
>> +	item->name = strdup(name);
>> +	if (!item->name) {
>> +		pr_debug("%s: strdup failed\n", __func__);
>> +		free(item);
>> +		return NULL;
>> +	}
>> +
>> +	list_add_tail(&item->node, &section->items);
>> +	return item;
>> +}
>> +
>> +static int set_value(struct perf_config_item *item, const char *value)
>> +{
>> +	char *val = strdup(value);
>> +
>> +	if (!val)
>> +		return -1;
>> +
>> +	free(item->value);
>> +	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 *item = NULL;
>> +	struct perf_config_set *set = perf_config_set;
>> +	struct list_head *sections = &set->sections;
>> +
>> +	key = ptr = strdup(var);
>> +	if (!key) {
>> +		pr_debug("%s: strdup failed\n", __func__);
>> +		return -1;
>> +	}
>> +
>> +	section_name = strsep(&ptr, ".");
>> +	name = ptr;
>> +	if (name == NULL || value == NULL)
>> +		goto out_free;
>> +
>> +	section = find_section(sections, section_name);
>> +	if (!section) {
>> +		section = add_section(sections, section_name);
>> +		if (!section)
>> +			goto out_free;
>> +	}
>> +
>> +	item = find_config_item(name, section);
>> +	if (!item) {
>> +		item = add_config_item(section, name);
>> +		if (!item)
>> +			goto out_free;
>> +	}
>> +
>> +	ret = set_value(item, value);
>> +	return ret;
>> +
>> +out_free:
>> +	free(key);
>> +	perf_config_set__delete(set);
>> +	return -1;
>> +}
>> +
>> +struct perf_config_set *perf_config_set__new(void)
>> +{
>> +	struct perf_config_set *set = zalloc(sizeof(*set));
>> +
>> +	if (set) {
>> +		INIT_LIST_HEAD(&set->sections);
>> +		perf_config(collect_config, set);
>> +	}
>> +
>> +	return set;
>> +}
>> +
>> +static void perf_config_item__delete(struct perf_config_item *item)
>> +{
>> +	zfree(&item->name);
>> +	zfree(&item->value);
>> +	free(item);
>> +}
>> +
>> +static 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);
>> +	}
>> +}
>> +
>> +static void perf_config_section__delete(struct perf_config_section *section)
>> +{
>> +	perf_config_section__purge(section);
>> +	zfree(&section->name);
>> +	free(section);
>> +}
>> +
>> +static 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);
>> +}
>> +
>>   /*
>>    * Call this to report error for your variable that should not
>>    * get a boolean value (i.e. "[my] var" means "true").
>> diff --git a/tools/perf/util/config.h b/tools/perf/util/config.h
>> new file mode 100644
>> index 0000000..22ec626
>> --- /dev/null
>> +++ b/tools/perf/util/config.h
>> @@ -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 node;
>> +};
>> +
>> +struct perf_config_section {
>> +	char *name;
>> +	struct list_head items;
>> +	struct list_head node;
>> +};
>> +
>> +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 *set);
>> +
>> +#endif /* __PERF_CONFIG_H */
>> --
>> 2.5.0
>>
>
>

[toc] | [prev] | [next] | [standalone]


#1370601

FromMasami Hiramatsu <mhiramat@kernel.org>
Date2016-04-04 14:50 +0200
Message-ID<rkc78-34X-3@gated-at.bofh.it>
In reply to#1370485
On Mon,  4 Apr 2016 18:17:04 +0900
Taeung Song <treeze.taeung@gmail.com> wrote:

> 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.
> 
Looks good to me:)

Reviewed-by: Masami Hiramatsu <mhiramat@kernel.org>

Thanks!

> Cc: Namhyung Kim <namhyung@kernel.org>
> Cc: Jiri Olsa <jolsa@kernel.org>
> Signed-off-by: Taeung Song <treeze.taeung@gmail.com>
> ---
>  tools/perf/util/config.c | 173 +++++++++++++++++++++++++++++++++++++++++++++++
>  tools/perf/util/config.h |  26 +++++++
>  2 files changed, 199 insertions(+)
>  create mode 100644 tools/perf/util/config.h
> 
> diff --git a/tools/perf/util/config.c b/tools/perf/util/config.c
> index 5c20d78..53e756e 100644
> --- a/tools/perf/util/config.c
> +++ b/tools/perf/util/config.c
> @@ -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)
>  
> @@ -524,6 +525,178 @@ 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, node)
> +		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 *item;
> +
> +	list_for_each_entry(item, &section->items, node)
> +		if (!strcmp(item->name, name))
> +			return item;
> +
> +	return 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->items);
> +	section->name = strdup(section_name);
> +	if (!section->name) {
> +		pr_debug("%s: strdup failed\n", __func__);
> +		free(section);
> +		return NULL;
> +	}
> +
> +	list_add_tail(&section->node, sections);
> +	return section;
> +}
> +
> +static struct perf_config_item *add_config_item(struct perf_config_section *section,
> +						const char *name)
> +{
> +	struct perf_config_item *item = zalloc(sizeof(*item));
> +
> +	if (!item)
> +		return NULL;
> +
> +	item->name = strdup(name);
> +	if (!item->name) {
> +		pr_debug("%s: strdup failed\n", __func__);
> +		free(item);
> +		return NULL;
> +	}
> +
> +	list_add_tail(&item->node, &section->items);
> +	return item;
> +}
> +
> +static int set_value(struct perf_config_item *item, const char *value)
> +{
> +	char *val = strdup(value);
> +
> +	if (!val)
> +		return -1;
> +
> +	free(item->value);
> +	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 *item = NULL;
> +	struct perf_config_set *set = perf_config_set;
> +	struct list_head *sections = &set->sections;
> +
> +	key = ptr = strdup(var);
> +	if (!key) {
> +		pr_debug("%s: strdup failed\n", __func__);
> +		return -1;
> +	}
> +
> +	section_name = strsep(&ptr, ".");
> +	name = ptr;
> +	if (name == NULL || value == NULL)
> +		goto out_free;
> +
> +	section = find_section(sections, section_name);
> +	if (!section) {
> +		section = add_section(sections, section_name);
> +		if (!section)
> +			goto out_free;
> +	}
> +
> +	item = find_config_item(name, section);
> +	if (!item) {
> +		item = add_config_item(section, name);
> +		if (!item)
> +			goto out_free;
> +	}
> +
> +	ret = set_value(item, value);
> +	return ret;
> +
> +out_free:
> +	free(key);
> +	perf_config_set__delete(set);
> +	return -1;
> +}
> +
> +struct perf_config_set *perf_config_set__new(void)
> +{
> +	struct perf_config_set *set = zalloc(sizeof(*set));
> +
> +	if (set) {
> +		INIT_LIST_HEAD(&set->sections);
> +		perf_config(collect_config, set);
> +	}
> +
> +	return set;
> +}
> +
> +static void perf_config_item__delete(struct perf_config_item *item)
> +{
> +	zfree(&item->name);
> +	zfree(&item->value);
> +	free(item);
> +}
> +
> +static 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);
> +	}
> +}
> +
> +static void perf_config_section__delete(struct perf_config_section *section)
> +{
> +	perf_config_section__purge(section);
> +	zfree(&section->name);
> +	free(section);
> +}
> +
> +static 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);
> +}
> +
>  /*
>   * Call this to report error for your variable that should not
>   * get a boolean value (i.e. "[my] var" means "true").
> diff --git a/tools/perf/util/config.h b/tools/perf/util/config.h
> new file mode 100644
> index 0000000..22ec626
> --- /dev/null
> +++ b/tools/perf/util/config.h
> @@ -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 node;
> +};
> +
> +struct perf_config_section {
> +	char *name;
> +	struct list_head items;
> +	struct list_head node;
> +};
> +
> +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 *set);
> +
> +#endif /* __PERF_CONFIG_H */
> -- 
> 2.5.0
> 


-- 
Masami Hiramatsu <mhiramat@kernel.org>

[toc] | [prev] | [standalone]


Back to top | Article view | linux.kernel


csiph-web