[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <1428169288-8482-2-git-send-email-treeze.taeung@gmail.com>
Date: Sun, 5 Apr 2015 02:41:28 +0900
From: Taeung Song <treeze.taeung@...il.com>
To: Arnaldo Carvalho de Melo <acme@...nel.org>
Cc: linux-kernel@...r.kernel.org, jolsa@...hat.com,
namhyung@...nel.org, Taeung Song <treeze.taeung@...il.com>
Subject: [PATCH 2/2] perf tools: Add options 'get', 'set' and 'unset' to 'perf-config'.
Add 'get' option that can show value of a specific config variable
and 'set' and 'unset' option that can create or replace a config variable.
Signed-off-by: Taeung Song <treeze.taeung@...il.com>
---
tools/perf/builtin-config.c | 198 ++++++++++++++++++++++++++++++++++++++++++++
tools/perf/util/cache.h | 18 +++-
tools/perf/util/config.c | 35 +++++++-
3 files changed, 247 insertions(+), 4 deletions(-)
diff --git a/tools/perf/builtin-config.c b/tools/perf/builtin-config.c
index 494bd73..b0885ff 100644
--- a/tools/perf/builtin-config.c
+++ b/tools/perf/builtin-config.c
@@ -15,14 +15,25 @@
static struct {
bool list_action;
+ bool get_action;
+ bool set_action;
+ bool unset_action;
} params;
+LIST_HEAD(sections);
+static char *section_name_;
+static char *subkey_;
+static char *value_;
+
static const char * const config_usage[] = {
"perf config [options]",
NULL
};
static const struct option config_options[] = {
OPT_BOOLEAN('l', "list", ¶ms.list_action, "list up current configurations"),
+ OPT_BOOLEAN(0, "get", ¶ms.get_action, "get value: section.subkey"),
+ OPT_BOOLEAN(0, "set", ¶ms.set_action, "create or replace a variable: section.subkey value"),
+ OPT_BOOLEAN(0, "unset", ¶ms.unset_action, "remove a variable: section.subkey"),
OPT_END()
};
@@ -34,6 +45,72 @@ static void check_argc(int argc, int min, int max)
usage_with_options(config_usage, config_options);
}
+static struct config_section *find_config_section(const char *section_name)
+{
+ struct config_section *section_node;
+ list_for_each_entry(section_node, §ions, list)
+ if (!strcmp(section_node->name, section_name))
+ return section_node;
+
+ return NULL;
+}
+
+static struct config_element *find_config_element(const char *subkey
+ , struct config_section *section_node)
+{
+ struct config_element *element_node;
+
+ list_for_each_entry(element_node, §ion_node->element_head, list)
+ if (!strcmp(element_node->subkey, subkey))
+ return element_node;
+
+ return NULL;
+}
+
+static struct config_section *init_config_section(const char *section_name)
+{
+ struct config_section *section_node;
+ LIST_HEAD(element_head);
+
+ section_node = zalloc(sizeof(*section_node));
+ if (!section_node)
+ return NULL;
+
+ INIT_LIST_HEAD(§ion_node->element_head);
+ list_splice(&element_head, §ion_node->element_head);
+ section_node->name = strdup(section_name);
+ if (!section_node->name) {
+ pr_err("%s: strdup failed\n", __func__);
+ return NULL;
+ }
+
+ return section_node;
+}
+
+static int add_config_element(struct list_head *head
+ , const char *subkey, const char *value)
+{
+ struct config_element *element_node;
+ element_node = zalloc(sizeof(*element_node));
+ element_node->subkey = strdup(subkey);
+ if (!element_node->subkey) {
+ pr_err("%s: strdup failed\n", __func__);
+ return -1;
+ }
+ if (value) {
+ element_node->value = strdup(value);
+ if (!element_node->value) {
+ pr_err("%s: strdup failed\n", __func__);
+ return -1;
+ }
+ } else
+ element_node->value = NULL;
+
+ list_add_tail(&element_node->list, head);
+
+ return 0;
+}
+
static int show_config(const char *key, const char *value,
void *cb __maybe_unused)
{
@@ -45,6 +122,112 @@ static int show_config(const char *key, const char *value,
return 0;
}
+static int show_spec_config(struct config_section *section_node
+ , struct config_element *element_node)
+{
+ char key[BUFSIZ];
+
+ if (section_node && element_node) {
+ sprintf(key, "%s.%s", section_node->name, element_node->subkey);
+ show_config(key, element_node->value, NULL);
+ }
+
+ return 0;
+}
+
+static int set_config(struct config_section *section_node, struct config_element *element_node)
+{
+ if (!value_) {
+ /* value == NULL means unset */
+ if (section_node && element_node)
+ element_node->value = NULL;
+ else /* do nothing */
+ return 0;
+ } else {
+ /* if there isn't existent section, add a new section */
+ if (!section_node) {
+ section_node = init_config_section(section_name_);
+ if (!section_node)
+ return -1;
+ list_add_tail(§ion_node->list, §ions);
+ }
+ /* if nothing to replace, add a new element which contains key-value pair. */
+ if (!element_node)
+ add_config_element(§ion_node->element_head, subkey_, value_);
+ else
+ element_node->value = strdup(value_);
+ }
+
+ perf_configset_write_in_full();
+
+ return 0;
+}
+
+static int collect_config(const char *var, const char *value
+ , void *cb __maybe_unused)
+{
+ struct config_section *section_node;
+ char *section_name, *subkey;
+ char *key = strdup(var);
+
+ if (!key) {
+ pr_err("%s: strdup failed\n", __func__);
+ return -1;
+ }
+ section_name = strsep(&key, ".");
+ subkey = strsep(&key, ".");
+ section_node = find_config_section(section_name);
+ if (!section_node) {
+ /* Add a new section */
+ section_node = init_config_section(section_name);
+ if (!section_node)
+ return -1;
+ list_add_tail(§ion_node->list, §ions);
+ }
+
+ add_config_element(§ion_node->element_head, subkey, value);
+
+ return 0;
+}
+
+static int perf_configset_with_option(configset_fn_t fn, const char *var, const char *value)
+{
+ struct config_section *section_node = NULL;
+ struct config_element *element_node = NULL;
+ char *key = strdup(var);
+ const char *last_dot = strrchr(key, '.');
+
+ /*
+ * Since "key" actually contains the section name and the real
+ * key name separated by a dot, we have to know where the dot is.
+ */
+ if (last_dot == NULL || last_dot == key) {
+ pr_err("The config variable does not contain a section: %s", key);
+ return -1;
+ }
+ if (!last_dot[1]) {
+ pr_err("The config varible does not contain variable name: %s", key);
+ return -1;
+ }
+
+ perf_config(collect_config, NULL);
+ section_name_ = strsep(&key, ".");
+ subkey_ = strsep(&key, ".");
+ if (value)
+ value_ = strdup(value);
+ else
+ value_ = NULL;
+
+ section_node = find_config_section(section_name_);
+
+ if (section_node != NULL)
+ element_node = find_config_element(subkey_, section_node);
+
+ fn(section_node, element_node);
+
+ return 0;
+}
+
int cmd_config(int argc, const char **argv, const char *prefix __maybe_unused)
{
int ret = 0;
@@ -63,5 +246,20 @@ int cmd_config(int argc, const char **argv, const char *prefix __maybe_unused)
ret = perf_config(show_config, NULL);
}
+ else if (params.get_action) {
+ check_argc(argc, 1, 1);
+ ret = perf_configset_with_option(show_spec_config, argv[0], argv[1]);
+ }
+
+ else if (params.set_action) {
+ check_argc(argc, 2, 2);
+ ret = perf_configset_with_option(set_config, argv[0], argv[1]);
+ }
+
+ else if (params.unset_action) {
+ check_argc(argc, 1, 1);
+ ret = perf_configset_with_option(set_config, argv[0], argv[1]);
+ }
+
return ret;
}
diff --git a/tools/perf/util/cache.h b/tools/perf/util/cache.h
index fbcca21..c06b08d 100644
--- a/tools/perf/util/cache.h
+++ b/tools/perf/util/cache.h
@@ -1,6 +1,7 @@
#ifndef __PERF_CACHE_H
#define __PERF_CACHE_H
+#include <linux/list.h>
#include <stdbool.h>
#include "util.h"
#include "strbuf.h"
@@ -19,6 +20,22 @@
#define PERF_DEBUGFS_ENVIRONMENT "PERF_DEBUGFS_DIR"
#define PERF_TRACEFS_ENVIRONMENT "PERF_TRACEFS_DIR"
+struct config_element {
+ char *subkey;
+ char *value;
+ struct list_head list;
+};
+
+struct config_section {
+ char *name;
+ struct list_head element_head;
+ struct list_head list;
+};
+
+extern struct list_head sections;
+
+typedef int (*configset_fn_t)(struct config_section *, struct config_element *);
+extern int perf_configset_write_in_full(void);
typedef int (*config_fn_t)(const char *, const char *, void *);
extern int perf_default_config(const char *, const char *, void *);
extern int perf_config(config_fn_t fn, void *);
@@ -27,7 +44,6 @@ extern u64 perf_config_u64(const char *, const char *);
extern int perf_config_bool(const char *, const char *);
extern int config_error_nonbool(const char *);
extern const char *perf_config_dirname(const char *, const char *);
-
/* pager.c */
extern void setup_pager(void);
extern const char *pager_program;
diff --git a/tools/perf/util/config.c b/tools/perf/util/config.c
index e18f653..ffcbe96 100644
--- a/tools/perf/util/config.c
+++ b/tools/perf/util/config.c
@@ -21,7 +21,7 @@
char buildid_dir[MAXPATHLEN]; /* root dir for buildid, binary cache */
static FILE *config_file;
-static const char *config_file_name;
+static char *config_file_name;
static int config_linenr;
static int config_file_eof;
@@ -420,12 +420,11 @@ static int perf_config_from_file(config_fn_t fn, const char *filename, void *dat
ret = -1;
if (f) {
config_file = f;
- config_file_name = filename;
+ config_file_name = strdup(filename);
config_linenr = 1;
config_file_eof = 0;
ret = perf_parse_file(fn, data);
fclose(f);
- config_file_name = NULL;
}
return ret;
}
@@ -502,6 +501,36 @@ out:
return ret;
}
+int perf_configset_write_in_full(void)
+{
+ struct config_section *section_node;
+ struct config_element *element_node;
+ char section[BUFSIZ];
+ char key_value_pair[BUFSIZ];
+ const char *first_line = "# this file is auto-generated.\n";
+ FILE *f = fopen(config_file_name, "w");
+
+ if (!f)
+ return -1;
+
+ fwrite(first_line, strlen(first_line), 1, f);
+ /* overwrite configvariables */
+ list_for_each_entry(section_node, §ions, list) {
+ sprintf(section, "[%s]\n", section_node->name);
+ fwrite(section, strlen(section), 1, f);
+ list_for_each_entry(element_node, §ion_node->element_head, list) {
+ if (element_node->value) {
+ sprintf(key_value_pair, "\t%s = %s\n"
+ , element_node->subkey, element_node->value);
+ fwrite(key_value_pair, strlen(key_value_pair), 1, f);
+ }
+ }
+ }
+ fclose(f);
+
+ return 0;
+}
+
/*
* Call this to report error for your variable that should not
* get a boolean value (i.e. "[my] var" means "true").
--
1.9.1
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@...r.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
Powered by blists - more mailing lists