Skip to contentSkip to conversation

perf config: Introduce default config key-value pairs arrays

PATCH Superseded Taeung Song 1 participant 8 messages Aug 9, 2016 perf events

v7 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
#0 lore raw

v7 cover perf config: Introduce default config key-value pairs arrays

Hello, :)

When initializing default perf config values,
we currently use values of actual type(int, bool, char *, etc.). But I suggest using default config key-value pairs arrays.

For example,
If there isn't a user config value for 'annotate.use_offset' config variable at ~/.perfconfig,
default value for it is 'true' bool type value in perf like below.

At ui/browsers/annoate.c

static struct annotate_browser_opt {
       bool hide_src_code,
            use_offset,
	    jump_arrows,
	    show_linenr,
	    show_nr_jumps,
	    show_total_period;
} annotate_browser__opts = {

.use_offset = true,
.jump_arrows = true,
};

But if we use new config arrays that have all default config key-value pairs, we could initialize default config values with them.

If we do, we can manage default perf config values at one spot (like util/config.c) and It can be easy and simple to modify existing default config values or add default values for new config item.

For example,
If we use new default config arrays and there isn't user config value for 'annoate.use_offset' default value for it will be set as annotate_config_items[CONFIG_ANNOATE_USE_OFFSET].value.b instead of actual boolean type value 'true'.

IMHO, I think it would needed to use new default config arrays to manage default perf config values more effectively.
And this pathset contains patchs for only 'colors' and 'annoate' section because waiting for other opinions.

If you review this patchset, I'd appreciate it :-)

Thanks,
Taeung

v7:
- fix wrong handling a exception from strdup (Arnaldo)
- rebased on current acme/perf/core

v6:
- rename 'fore_back_colors' to simple 'colors' of ui_browser_colorset (Namhyung)
- remove unnecssary whitespace changes of PATCH 4/7, 5/7 (Namhyung)
- make more general macro instead of making accessor macro for each config section (Namhyung)
- rebased on current acme/perf/core

v5:
- rebased on current acme/perf/core

v4:
- rename 'fb_ground' to 'fore_back_colors' (Namhyung)
- add struct default_config_section
- split first patch[PATCH 1/7] as two
- remove perf_default_config_init() at perf.c
- rebased on current acme/perf/core

v3:
- remove default config arrays for the rest sections except 'colors' and 'annotate'
- use combined {fore, back}ground colors instead of each two color
- introduce perf_default_config_init() that call all default_*_config_init() for each config section

v2:
- rename 'ui_browser__config_gcolors' to 'ui_browser__config_colors' (Arnaldo)
- change 'ground colors' to '{back, fore}ground colors' (Arnaldo)
- use strtok + ltrim instead of strchr and while (isspace(*++bg)); (Arnaldo)

Taeung Song (7):
perf config: Introduce default_config_section and default_config_item
for default config key-value pairs
perf config: Add macros assigning key-value pairs to
default_config_item
perf config: Add default section and item arrays for 'colors' config perf config: Use combined {fore,back}ground colors value instead of
each two color
perf config: Initialize ui_browser__colorsets with default config
items
perf config: Add default section and item arrays for 'annotate' config perf config: Initialize annotate_browser__opts with default config
items

 tools/perf/ui/browser.c           | 64 +++++++++++++++++--------------
 tools/perf/ui/browsers/annotate.c | 16 ++++++--
 tools/perf/util/config.c          | 26 +++++++++++++
 tools/perf/util/config.h          | 80 +++++++++++++++++++++++++++++++++++++++
 4 files changed, 154 insertions(+), 32 deletions(-)
Signature · 1 line
2.5.0
#1 lore raw

PATCH v7 1/7 perf config: Introduce default_config_section and default_config_item for default config key-value pairs

When initializing default perf config values,
we currently use values of actual type(int, bool, char *, etc.).

For example,
If there isn't a user config value for 'annotate.use_offset' config variable at ~/.perfconfig,
default value for it is 'true' bool type value in perf like below.

At ui/browsers/annoate.c

static struct annotate_browser_opt {
        bool hide_src_code,
             use_offset,
             jump_arrows,
             show_linenr,
             show_nr_jumps,
             show_total_period;
} annotate_browser__opts = {

.use_offset = true,
.jump_arrows = true,
};

But I suggest using 'struct default_config_section' and
'struct default_config_item' that can contain default config key-value pairs in order to initialize default config values with them, in near future.

If we do, we could manage default perf config values at one spot (i.e. util/config.c) with default config arrays and it could be easy and simple to modify existing default config values or add default values for new config item.

Cc Namhyung Kim <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc Wang Nan <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Masami Hiramatsu <[email protected]>
---
 tools/perf/util/config.h | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)
tools/perf/util/config.h +29 -0
index 6f813d4..1fd8e4c 100644
@@ -63,4 +63,33 @@ void perf_config__refresh(void);
 	perf_config_sections__for_each_entry(&set->sections, section)		\
 	perf_config_items__for_each_entry(&section->items, item)
 
+enum perf_config_type {
+	CONFIG_TYPE_BOOL,
+	CONFIG_TYPE_INT,
+	CONFIG_TYPE_LONG,
+	CONFIG_TYPE_U64,
+	CONFIG_TYPE_FLOAT,
+	CONFIG_TYPE_DOUBLE,
+	CONFIG_TYPE_STRING
+};
+
+struct default_config_item {
+	const char *name;
+	union {
+		bool b;
+		int i;
+		u32 l;
+		u64 ll;
+		float f;
+		double d;
+		const char *s;
+	} value;
+	enum perf_config_type type;
+};
+
+struct default_config_section {
+	const char *name;
+	const struct default_config_item *items;
+};
+
 #endif /* __PERF_CONFIG_H */
Signature · 1 line
2.5.0
#2 lore raw

PATCH v7 2/7 perf config: Add macros assigning key-value pairs to default_config_item

In reply to Taeung Song

In near future, default_config_item arrays will be added (e.g. const struct default_config_item colors_config_items[]) To simply assign config key-value pairs to default_config_item, add macros that are CONF_VAR() and CONF_*_VAR() for each type.

Cc Namhyung Kim <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc Wang Nan <[email protected]>
Cc Masami Hiramatsu <[email protected]>
---
 tools/perf/util/config.h | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
tools/perf/util/config.h +20 -0
index 1fd8e4c..613900f 100644
@@ -92,4 +92,24 @@ struct default_config_section {
 	const struct default_config_item *items;
 };
 
+#define CONF_VAR(_name, _field, _val, _type)			\
+	{ .name = _name, .value._field = _val, .type = _type }
+
+#define CONF_BOOL_VAR(_name, _val)			\
+	CONF_VAR(_name, b, _val, CONFIG_TYPE_BOOL)
+#define CONF_INT_VAR(_name, _val)			\
+	CONF_VAR(_name, i, _val, CONFIG_TYPE_INT)
+#define CONF_LONG_VAR(_name, _val)			\
+	CONF_VAR(_name, l, _val, CONFIG_TYPE_LONG)
+#define CONF_U64_VAR(_name, _val)			\
+	CONF_VAR(_name, ll, _val, CONFIG_TYPE_U64)
+#define CONF_FLOAT_VAR(_name, _val)			\
+	CONF_VAR(_name, f, _val, CONFIG_TYPE_FLOAT)
+#define CONF_DOUBLE_VAR(_name, _val)			\
+	CONF_VAR(_name, d, _val, CONFIG_TYPE_DOUBLE)
+#define CONF_STR_VAR(_name, _val)			\
+	CONF_VAR(_name, s, _val, CONFIG_TYPE_STRING)
+#define CONF_END()					\
+	{ .name = NULL }
+
 #endif /* __PERF_CONFIG_H */
Signature · 1 line
2.5.0
#3 lore raw

PATCH v7 3/7 perf config: Add default section and item arrays for 'colors' config

In reply to Taeung Song

Actual values for default configs of 'colors' section is like below.

(at ui/browser.c)

static struct ui_browser_colorset {
	const char *name, *fg, *bg;
	int colorset;
} ui_browser__colorsets[] = {
	{
		.colorset = HE_COLORSET_TOP,
		.name	  = "top",
		.fg	  = "red",
		.bg	  = "default",
	},

...

But I suggest using default config arrays for 'colors' section that contain all default config key-value pairs for it.

In near future, this array will be used on ui/browser.c because of setting default values of actual variables for 'colors' config.

Cc Namhyung Kim <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc Masami Hiramatsu <[email protected]>
Cc Wang Nan <[email protected]>
---
 tools/perf/util/config.c | 15 +++++++++++++++
 tools/perf/util/config.h | 17 +++++++++++++++++
 2 files changed, 32 insertions(+)
tools/perf/util/config.c +15 -0
index 18dae74..a0c0170 100644
@@ -30,6 +30,21 @@ static struct perf_config_set *config_set;
 
 const char *config_exclusive_filename;
 
+const struct default_config_item colors_config_items[] = {
+	CONF_STR_VAR("top", "red, default"),
+	CONF_STR_VAR("medium", "green, default"),
+	CONF_STR_VAR("normal", "default, default"),
+	CONF_STR_VAR("selected", "black, yellow"),
+	CONF_STR_VAR("jump_arrows", "blue, default"),
+	CONF_STR_VAR("addr", "magenta, default"),
+	CONF_STR_VAR("root", "white, blue"),
+	CONF_END()
+};
+
+const struct default_config_section default_sections[] = {
+	{ .name = "colors", .items = colors_config_items },
+};
+
 static int get_next_char(void)
 {
 	int c;
tools/perf/util/config.h +17 -0
index 613900f..b9190fe 100644
@@ -73,6 +73,20 @@ enum perf_config_type {
 	CONFIG_TYPE_STRING
 };
 
+enum config_section_idx {
+	CONFIG_COLORS,
+};
+
+enum colors_config_items_idx {
+	CONFIG_COLORS_TOP,
+	CONFIG_COLORS_MEDIUM,
+	CONFIG_COLORS_NORMAL,
+	CONFIG_COLORS_SELECTED,
+	CONFIG_COLORS_JUMP_ARROWS,
+	CONFIG_COLORS_ADDR,
+	CONFIG_COLORS_ROOT,
+};
+
 struct default_config_item {
 	const char *name;
 	union {
@@ -112,4 +126,7 @@ struct default_config_section {
 #define CONF_END()					\
 	{ .name = NULL }
 
+extern const struct default_config_section default_sections[];
+extern const struct default_config_item colors_config_items[];
+
 #endif /* __PERF_CONFIG_H */
Signature · 1 line
2.5.0
#4 lore raw

PATCH v7 4/7 perf config: Use combined {fore,back}ground colors value instead of each two color

In reply to Taeung Song

To easily set default config values into actual variables for 'colors' config, it would be better that actual variables for each 'colors' config have only one value like 'default_config_item' type.

If we use combined {fore,back}ground colors values in ui_browser_colorset, it smoothly work to initialize default config values for 'colors' config by 'colors_config_items' array that contains default values for it at util/config.c. because both actual variable and config item of 'colors_config_items' are equal in the number of values (as just one).

Cc Namhyung Kim <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc Masami Hiramatsu <[email protected]>
Cc Wang Nan <[email protected]>
---
 tools/perf/ui/browser.c | 53 +++++++++++++++++++++++--------------------------
 1 file changed, 25 insertions(+), 28 deletions(-)
tools/perf/ui/browser.c +25 -28
index 3eb3edb..1c80f00 100644
@@ -503,61 +503,53 @@ unsigned int ui_browser__list_head_refresh(struct ui_browser *browser)
 }
 
 static struct ui_browser_colorset {
-	const char *name, *fg, *bg;
+	const char *name, *colors;
 	int colorset;
 } ui_browser__colorsets[] = {
 	{
 		.colorset = HE_COLORSET_TOP,
 		.name	  = "top",
-		.fg	  = "red",
-		.bg	  = "default",
+		.colors	  = "red, default",
 	},
 	{
 		.colorset = HE_COLORSET_MEDIUM,
 		.name	  = "medium",
-		.fg	  = "green",
-		.bg	  = "default",
+		.colors	  = "green, default",
 	},
 	{
 		.colorset = HE_COLORSET_NORMAL,
 		.name	  = "normal",
-		.fg	  = "default",
-		.bg	  = "default",
+		.colors	  = "default, default",
 	},
 	{
 		.colorset = HE_COLORSET_SELECTED,
 		.name	  = "selected",
-		.fg	  = "black",
-		.bg	  = "yellow",
+		.colors	  = "black, yellow",
 	},
 	{
 		.colorset = HE_COLORSET_JUMP_ARROWS,
 		.name	  = "jump_arrows",
-		.fg	  = "blue",
-		.bg	  = "default",
+		.colors	  = "blue, default",
 	},
 	{
 		.colorset = HE_COLORSET_ADDR,
 		.name	  = "addr",
-		.fg	  = "magenta",
-		.bg	  = "default",
+		.colors	  = "magenta, default",
 	},
 	{
 		.colorset = HE_COLORSET_ROOT,
 		.name	  = "root",
-		.fg	  = "white",
-		.bg	  = "blue",
+		.colors	  = "white, blue",
 	},
 	{
 		.name = NULL,
 	}
 };
 
-
 static int ui_browser__color_config(const char *var, const char *value,
 				    void *data __maybe_unused)
 {
-	char *fg = NULL, *bg;
+	char *colors;
 	int i;
 
 	/* same dir for all commands */
@@ -570,22 +562,18 @@ static int ui_browser__color_config(const char *var, const char *value,
 		if (strcmp(ui_browser__colorsets[i].name, name) != 0)
 			continue;
 
-		fg = strdup(value);
-		if (fg == NULL)
-			break;
+		if (strstr(value, ",") == NULL)
+			return -1;
 
-		bg = strchr(fg, ',');
-		if (bg == NULL)
+		colors = strdup(value);
+		if (colors == NULL)
 			break;
+		ui_browser__colorsets[i].colors = colors;
 
-		*bg = '\0';
-		while (isspace(*++bg));
-		ui_browser__colorsets[i].bg = bg;
-		ui_browser__colorsets[i].fg = fg;
 		return 0;
 	}
 
-	free(fg);
+	free(colors);
 	return -1;
 }
 
@@ -743,8 +731,17 @@ void ui_browser__init(void)
 	perf_config(ui_browser__color_config, NULL);
 
 	while (ui_browser__colorsets[i].name) {
+		char *colors, *fg, *bg;
 		struct ui_browser_colorset *c = &ui_browser__colorsets[i++];
-		sltt_set_color(c->colorset, c->name, c->fg, c->bg);
+
+		colors = strdup(c->colors);
+		if (colors == NULL)
+			break;
+		fg = strtok(colors, ",");
+		bg = strtok(NULL, ",");
+		bg = ltrim(bg);
+		sltt_set_color(c->colorset, c->name, fg, bg);
+		free(colors);
 	}
 
 	annotate_browser__init();
Signature · 1 line
2.5.0
#5 lore raw

PATCH v7 5/7 perf config: Initialize ui_browser__colorsets with default config items

In reply to Taeung Song

Set default config values for 'colors' section with 'colors_config_items[]' instead of actual const char * type values.
(e.g. using colors_config_item[CONFIG_COLORS_TOP].value.s instead of "red, default" string value for 'colors.top')

Cc Namhyung Kim <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc Masami Hiramatsu <[email protected]>
Cc Wang Nan <[email protected]>
---
 tools/perf/ui/browser.c | 25 ++++++++++++++++++-------
 1 file changed, 18 insertions(+), 7 deletions(-)
tools/perf/ui/browser.c +18 -7
index 1c80f00..5caa7e4 100644
@@ -509,37 +509,30 @@ static struct ui_browser_colorset {
 	{
 		.colorset = HE_COLORSET_TOP,
 		.name	  = "top",
-		.colors	  = "red, default",
 	},
 	{
 		.colorset = HE_COLORSET_MEDIUM,
 		.name	  = "medium",
-		.colors	  = "green, default",
 	},
 	{
 		.colorset = HE_COLORSET_NORMAL,
 		.name	  = "normal",
-		.colors	  = "default, default",
 	},
 	{
 		.colorset = HE_COLORSET_SELECTED,
 		.name	  = "selected",
-		.colors	  = "black, yellow",
 	},
 	{
 		.colorset = HE_COLORSET_JUMP_ARROWS,
 		.name	  = "jump_arrows",
-		.colors	  = "blue, default",
 	},
 	{
 		.colorset = HE_COLORSET_ADDR,
 		.name	  = "addr",
-		.colors	  = "magenta, default",
 	},
 	{
 		.colorset = HE_COLORSET_ROOT,
 		.name	  = "root",
-		.colors	  = "white, blue",
 	},
 	{
 		.name = NULL,
@@ -724,10 +717,28 @@ void __ui_browser__line_arrow(struct ui_browser *browser, unsigned int column,
 		__ui_browser__line_arrow_down(browser, column, start, end);
 }
 
+static void default_colors_config_init(void)
+{
+	int i, j;
+
+	for (i = 0; ui_browser__colorsets[i].name != NULL; ++i) {
+		const char *name = ui_browser__colorsets[i].name;
+
+		for (j = 0; colors_config_items[j].name != NULL; j++) {
+			if (!strcmp(name, colors_config_items[j].name)) {
+				ui_browser__colorsets[i].colors =
+					colors_config_items[j].value.s;
+				break;
+			}
+		}
+	}
+}
+
 void ui_browser__init(void)
 {
 	int i = 0;
 
+	default_colors_config_init();
 	perf_config(ui_browser__color_config, NULL);
 
 	while (ui_browser__colorsets[i].name) {
Signature · 1 line
2.5.0
#6 lore raw

PATCH v7 6/7 perf config: Add default section and item arrays for 'annotate' config

In reply to Taeung Song

Actual values for default configs of 'annotate' section is like below.

(at ui/browsers/annoate.c)

static struct annotate_browser_opt {
        bool hide_src_code,
             use_offset,
             jump_arrows,
             show_linenr,
             show_nr_jumps,
             show_total_period;
} annotate_browser__opts = {

.use_offset = true,
.jump_arrows = true,
};

But I suggest using default config arrays for 'annotate' section that contain all default config key-value pairs for it.

In near future, this arrays will be used on ui/browsers/annoate.c because of setting default values of actual variables for 'annotate' config.

Cc Namhyung Kim <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc Masami Hiramatsu <[email protected]>
Cc Wang Nan <[email protected]>
---
 tools/perf/util/config.c | 11 +++++++++++
 tools/perf/util/config.h | 11 +++++++++++
 2 files changed, 22 insertions(+)
tools/perf/util/config.c +11 -0
index a0c0170..d8d5415 100644
@@ -41,8 +41,19 @@ const struct default_config_item colors_config_items[] = {
 	CONF_END()
 };
 
+const struct default_config_item annotate_config_items[] = {
+	CONF_BOOL_VAR("hide_src_code", false),
+	CONF_BOOL_VAR("use_offset", true),
+	CONF_BOOL_VAR("jump_arrows", true),
+	CONF_BOOL_VAR("show_nr_jumps", false),
+	CONF_BOOL_VAR("show_linenr", false),
+	CONF_BOOL_VAR("show_total_period", false),
+	CONF_END()
+};
+
 const struct default_config_section default_sections[] = {
 	{ .name = "colors", .items = colors_config_items },
+	{ .name = "annotate", .items = annotate_config_items },
 };
 
 static int get_next_char(void)
tools/perf/util/config.h +11 -0
index b9190fe..2fcfd51 100644
@@ -75,6 +75,7 @@ enum perf_config_type {
 
 enum config_section_idx {
 	CONFIG_COLORS,
+	CONFIG_ANNOTATE,
 };
 
 enum colors_config_items_idx {
@@ -87,6 +88,15 @@ enum colors_config_items_idx {
 	CONFIG_COLORS_ROOT,
 };
 
+enum annotate_config_items_idx {
+	CONFIG_ANNOTATE_HIDE_SRC_CODE,
+	CONFIG_ANNOTATE_USE_OFFSET,
+	CONFIG_ANNOTATE_JUMP_ARROWS,
+	CONFIG_ANNOTATE_SHOW_NR_JUMPS,
+	CONFIG_ANNOTATE_SHOW_LINENR,
+	CONFIG_ANNOTATE_SHOW_TOTAL_PERIOD,
+};
+
 struct default_config_item {
 	const char *name;
 	union {
@@ -128,5 +138,6 @@ struct default_config_section {
 
 extern const struct default_config_section default_sections[];
 extern const struct default_config_item colors_config_items[];
+extern const struct default_config_item annotate_config_items[];
 
 #endif /* __PERF_CONFIG_H */
Signature · 1 line
2.5.0
#7 lore raw

PATCH v7 7/7 perf config: Initialize annotate_browser__opts with default config items

In reply to Taeung Song

Set default config values for 'annotate' section with 'annotate_config_items[]' instead of actual bool type values.
(e.g. using annotate_config_items[CONFIG_ANNOTATE_USE_OFFSET].value.b instead of 'true' bool type value for 'annotate.use_offset'.)

Cc Namhyung Kim <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc Masami Hiramatsu <[email protected]>
Cc Wang Nan <[email protected]>
---
 tools/perf/ui/browsers/annotate.c | 16 ++++++++++++----
 tools/perf/util/config.h          |  3 +++
 2 files changed, 15 insertions(+), 4 deletions(-)
tools/perf/ui/browsers/annotate.c +12 -4
index 2e2d100..d9dad4e 100644
@@ -38,10 +38,7 @@ static struct annotate_browser_opt {
 	     show_linenr,
 	     show_nr_jumps,
 	     show_total_period;
-} annotate_browser__opts = {
-	.use_offset	= true,
-	.jump_arrows	= true,
-};
+} annotate_browser__opts;
 
 struct annotate_browser {
 	struct ui_browser b;
@@ -1160,7 +1157,18 @@ static int annotate__config(const char *var, const char *value,
 	return 0;
 }
 
+static void default_annotate_config_init(void)
+{
+	annotate_browser__opts.hide_src_code = CONF_DEFAULT_BOOL(ANNOTATE, HIDE_SRC_CODE);
+	annotate_browser__opts.use_offset = CONF_DEFAULT_BOOL(ANNOTATE, USE_OFFSET);
+	annotate_browser__opts.jump_arrows = CONF_DEFAULT_BOOL(ANNOTATE, JUMP_ARROWS);
+	annotate_browser__opts.show_linenr = CONF_DEFAULT_BOOL(ANNOTATE, SHOW_LINENR);
+	annotate_browser__opts.show_nr_jumps = CONF_DEFAULT_BOOL(ANNOTATE, SHOW_NR_JUMPS);
+	annotate_browser__opts.show_total_period = CONF_DEFAULT_BOOL(ANNOTATE, SHOW_TOTAL_PERIOD);
+}
+
 void annotate_browser__init(void)
 {
+	default_annotate_config_init();
 	perf_config(annotate__config, NULL);
 }
tools/perf/util/config.h +3 -0
index 2fcfd51..76f5b21 100644
@@ -136,6 +136,9 @@ struct default_config_section {
 #define CONF_END()					\
 	{ .name = NULL }
 
+#define CONF_DEFAULT_BOOL(sec, name)					\
+	default_sections[CONFIG_##sec].items[CONFIG_##sec##_##name].value.b
+
 extern const struct default_config_section default_sections[];
 extern const struct default_config_item colors_config_items[];
 extern const struct default_config_item annotate_config_items[];
Signature · 1 line
2.5.0