perf metric: Add utilities to work on ids map.

Add utilities to new/free an ids hashmap, as well as to union. Add
testing of the union. Unioning hashmaps will be used when parsing the
metric, if a value is known then the hashmap is unnecessary, otherwise
we need to union together all the event ids to compute their values for
reporting.

Signed-off-by: Ian Rogers <irogers@google.com>
Tested-by: John Garry <john.garry@huawei.com>
Acked-by: Jiri Olsa <jolsa@redhat.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Jin Yao <yao.jin@linux.intel.com>
Cc: Kajol Jain <kjain@linux.ibm.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Paul Clarke <pc@us.ibm.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Sandeep Dasgupta <sdasgup@google.com>
Cc: Stephane Eranian <eranian@google.com>
Link: https://lore.kernel.org/r/20210923074616.674826-9-irogers@google.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
This commit is contained in:
Ian Rogers
2021-09-23 00:46:11 -07:00
committed by Arnaldo Carvalho de Melo
parent 7e06a5e30a
commit 114a9d6e39
3 changed files with 126 additions and 4 deletions

View File

@@ -59,8 +59,29 @@ static bool key_equal(const void *key1, const void *key2,
return !strcmp((const char *)key1, (const char *)key2);
}
/* Caller must make sure id is allocated */
int expr__add_id(struct expr_parse_ctx *ctx, const char *id)
struct hashmap *ids__new(void)
{
return hashmap__new(key_hash, key_equal, NULL);
}
void ids__free(struct hashmap *ids)
{
struct hashmap_entry *cur;
size_t bkt;
if (ids == NULL)
return;
hashmap__for_each_entry(ids, cur, bkt) {
free((char *)cur->key);
free(cur->value);
}
hashmap__free(ids);
}
int ids__insert(struct hashmap *ids, const char *id,
struct expr_id *parent)
{
struct expr_id_data *data_ptr = NULL, *old_data = NULL;
char *old_key = NULL;
@@ -70,10 +91,10 @@ int expr__add_id(struct expr_parse_ctx *ctx, const char *id)
if (!data_ptr)
return -ENOMEM;
data_ptr->parent = ctx->parent;
data_ptr->parent = parent;
data_ptr->kind = EXPR_ID_DATA__PARENT;
ret = hashmap__set(ctx->ids, id, data_ptr,
ret = hashmap__set(ids, id, data_ptr,
(const void **)&old_key, (void **)&old_data);
if (ret)
free(data_ptr);
@@ -82,6 +103,48 @@ int expr__add_id(struct expr_parse_ctx *ctx, const char *id)
return ret;
}
struct hashmap *ids__union(struct hashmap *ids1, struct hashmap *ids2)
{
size_t bkt;
struct hashmap_entry *cur;
int ret;
struct expr_id_data *old_data = NULL;
char *old_key = NULL;
if (!ids1)
return ids2;
if (!ids2)
return ids1;
if (hashmap__size(ids1) < hashmap__size(ids2)) {
struct hashmap *tmp = ids1;
ids1 = ids2;
ids2 = tmp;
}
hashmap__for_each_entry(ids2, cur, bkt) {
ret = hashmap__set(ids1, cur->key, cur->value,
(const void **)&old_key, (void **)&old_data);
free(old_key);
free(old_data);
if (ret) {
hashmap__free(ids1);
hashmap__free(ids2);
return NULL;
}
}
hashmap__free(ids2);
return ids1;
}
/* Caller must make sure id is allocated */
int expr__add_id(struct expr_parse_ctx *ctx, const char *id)
{
return ids__insert(ctx->ids, id, ctx->parent);
}
/* Caller must make sure id is allocated */
int expr__add_id_val(struct expr_parse_ctx *ctx, const char *id, double val)
{