struct metric_expr *expr, *tmp;
list_for_each_entry_safe(expr, tmp, &me->head, nd) {
+ free((char *)expr->metric_name);
free(expr->metric_refs);
free(expr->metric_events);
free(expr);
struct expr_parse_ctx *pctx;
/** The name of the metric such as "IPC". */
const char *metric_name;
+ /** Modifier on the metric such as "u" or NULL for none. */
+ const char *modifier;
/** The expression to parse, for example, "instructions/cycles". */
const char *metric_expr;
/**
}
static struct metric *metric__new(const struct pmu_event *pe,
+ const char *modifier,
bool metric_no_group,
int runtime)
{
}
m->metric_name = pe->metric_name;
+ m->modifier = modifier ? strdup(modifier) : NULL;
+ if (modifier && !m->modifier) {
+ free(m);
+ expr__ctx_free(m->pctx);
+ return NULL;
+ }
m->metric_expr = pe->metric_expr;
m->metric_unit = pe->unit;
m->pctx->runtime = runtime;
{
free(m->metric_refs);
expr__ctx_free(m->pctx);
+ free((char *)m->modifier);
free(m);
}
return 0;
}
-static int decode_all_metric_ids(struct evlist *perf_evlist)
+static int decode_all_metric_ids(struct evlist *perf_evlist, const char *modifier)
{
struct evsel *ev;
struct strbuf sb = STRBUF_INIT;
* give a more friendly display version.
*/
if (strstr(ev->name, "metric-id=")) {
+ bool has_slash = false;
+
free(ev->name);
- for (cur = strchr(sb.buf, '@') ; cur; cur = strchr(++cur, '@'))
+ for (cur = strchr(sb.buf, '@') ; cur; cur = strchr(++cur, '@')) {
*cur = '/';
+ has_slash = true;
+ }
+ if (modifier) {
+ if (!has_slash && !strchr(sb.buf, ':')) {
+ ret = strbuf_addch(&sb, ':');
+ if (ret)
+ break;
+ }
+ ret = strbuf_addstr(&sb, modifier);
+ if (ret)
+ break;
+ }
ev->name = strdup(sb.buf);
if (!ev->name) {
ret = -ENOMEM;
static int metricgroup__build_event_string(struct strbuf *events,
const struct expr_parse_ctx *ctx,
+ const char *modifier,
bool has_constraint)
{
struct hashmap_entry *cur;
ret = strbuf_addstr(events, sep + 1);
RETURN_IF_NON_ZERO(ret);
}
+ if (modifier) {
+ ret = strbuf_addstr(events, modifier);
+ RETURN_IF_NON_ZERO(ret);
+ }
}
if (has_duration) {
if (no_group) {
struct metricgroup_add_iter_data {
struct list_head *metric_list;
const char *metric_name;
+ const char *modifier;
int *ret;
bool *has_match;
bool metric_no_group;
static int add_metric(struct list_head *metric_list,
const struct pmu_event *pe,
+ const char *modifier,
bool metric_no_group,
struct metric *root_metric,
const struct visited_metric *visited,
* resolve_metric - Locate metrics within the root metric and recursively add
* references to them.
* @metric_list: The list the metric is added to.
+ * @modifier: if non-null event modifiers like "u".
* @metric_no_group: Should events written to events be grouped "{}" or
* global. Grouping is the default but due to multiplexing the
* user may override.
* architecture perf is running upon.
*/
static int resolve_metric(struct list_head *metric_list,
+ const char *modifier,
bool metric_no_group,
struct metric *root_metric,
const struct visited_metric *visited,
* context.
*/
for (i = 0; i < pending_cnt; i++) {
- ret = add_metric(metric_list, pending[i].pe, metric_no_group,
+ ret = add_metric(metric_list, pending[i].pe, modifier, metric_no_group,
root_metric, visited, map);
if (ret)
break;
* __add_metric - Add a metric to metric_list.
* @metric_list: The list the metric is added to.
* @pe: The pmu_event containing the metric to be added.
+ * @modifier: if non-null event modifiers like "u".
* @metric_no_group: Should events written to events be grouped "{}" or
* global. Grouping is the default but due to multiplexing the
* user may override.
*/
static int __add_metric(struct list_head *metric_list,
const struct pmu_event *pe,
+ const char *modifier,
bool metric_no_group,
int runtime,
struct metric *root_metric,
* This metric is the root of a tree and may reference other
* metrics that are added recursively.
*/
- root_metric = metric__new(pe, metric_no_group, runtime);
+ root_metric = metric__new(pe, modifier, metric_no_group, runtime);
if (!root_metric)
return -ENOMEM;
ret = -EINVAL;
} else {
/* Resolve referenced metrics. */
- ret = resolve_metric(metric_list, metric_no_group, root_metric,
+ ret = resolve_metric(metric_list, modifier, metric_no_group, root_metric,
&visited_node, map);
}
static int add_metric(struct list_head *metric_list,
const struct pmu_event *pe,
+ const char *modifier,
bool metric_no_group,
struct metric *root_metric,
const struct visited_metric *visited,
pr_debug("metric expr %s for %s\n", pe->metric_expr, pe->metric_name);
if (!strstr(pe->metric_expr, "?")) {
- ret = __add_metric(metric_list, pe, metric_no_group, 0,
+ ret = __add_metric(metric_list, pe, modifier, metric_no_group, 0,
root_metric, visited, map);
} else {
int j, count;
*/
for (j = 0; j < count && !ret; j++)
- ret = __add_metric(metric_list, pe, metric_no_group, j,
+ ret = __add_metric(metric_list, pe, modifier, metric_no_group, j,
root_metric, visited, map);
}
if (!match_pe_metric(pe, d->metric_name))
return 0;
- ret = add_metric(d->metric_list, pe, d->metric_no_group,
+ ret = add_metric(d->metric_list, pe, d->modifier, d->metric_no_group,
d->root_metric, d->visited, d->map);
if (ret)
goto out;
* @metric_name: The name of the metric or metric group. For example, "IPC"
* could be the name of a metric and "TopDownL1" the name of a
* metric group.
+ * @modifier: if non-null event modifiers like "u".
* @metric_no_group: Should events written to events be grouped "{}" or
* global. Grouping is the default but due to multiplexing the
* user may override.
* @map: The map that is searched for metrics, most commonly the table for the
* architecture perf is running upon.
*/
-static int metricgroup__add_metric(const char *metric_name, bool metric_no_group,
+static int metricgroup__add_metric(const char *metric_name, const char *modifier,
+ bool metric_no_group,
struct list_head *metric_list,
const struct pmu_events_map *map)
{
*/
map_for_each_metric(pe, i, map, metric_name) {
has_match = true;
- ret = add_metric(&list, pe, metric_no_group,
+ ret = add_metric(&list, pe, modifier, metric_no_group,
/*root_metric=*/NULL,
/*visited_metrics=*/NULL, map);
if (ret)
.data = (void *) &(struct metricgroup_add_iter_data) {
.metric_list = &list,
.metric_name = metric_name,
+ .modifier = modifier,
.metric_no_group = metric_no_group,
.has_match = &has_match,
.ret = &ret,
struct list_head *metric_list,
const struct pmu_events_map *map)
{
- char *llist, *nlist, *p;
+ char *list_itr, *list_copy, *metric_name, *modifier;
int ret, count = 0;
- nlist = strdup(list);
- if (!nlist)
+ list_copy = strdup(list);
+ if (!list_copy)
return -ENOMEM;
- llist = nlist;
+ list_itr = list_copy;
- while ((p = strsep(&llist, ",")) != NULL) {
- ret = metricgroup__add_metric(p, metric_no_group, metric_list,
+ while ((metric_name = strsep(&list_itr, ",")) != NULL) {
+ modifier = strchr(metric_name, ':');
+ if (modifier)
+ *modifier++ = '\0';
+
+ ret = metricgroup__add_metric(metric_name, modifier,
+ metric_no_group, metric_list,
map);
if (ret == -EINVAL)
- pr_err("Cannot find metric or group `%s'\n", p);
+ pr_err("Cannot find metric or group `%s'\n", metric_name);
if (ret)
break;
count++;
}
- free(nlist);
+ free(list_copy);
if (!ret) {
/*
return -ENOMEM;
list_for_each_entry(m, metric_list, nd) {
- if (m->has_constraint) {
+ if (m->has_constraint && !m->modifier) {
hashmap__for_each_entry(m->pctx->ids, cur, bkt) {
dup = strdup(cur->key);
if (!dup) {
* evlist. The encoded metric_ids are decoded.
* @fake_pmu: used when testing metrics not supported by the current CPU.
* @ids: the event identifiers parsed from a metric.
+ * @modifier: any modifiers added to the events.
* @has_constraint: false if events should be placed in a weak group.
* @out_evlist: the created list of events.
*/
static int parse_ids(struct perf_pmu *fake_pmu, struct expr_parse_ctx *ids,
- bool has_constraint, struct evlist **out_evlist)
+ const char *modifier, bool has_constraint, struct evlist **out_evlist)
{
struct parse_events_error parse_error;
struct evlist *parsed_evlist;
ids__insert(ids->ids, tmp);
}
- ret = metricgroup__build_event_string(&events, ids, has_constraint);
+ ret = metricgroup__build_event_string(&events, ids, modifier,
+ has_constraint);
if (ret)
return ret;
parse_events_print_error(&parse_error, events.buf);
goto err_out;
}
- ret = decode_all_metric_ids(parsed_evlist);
+ ret = decode_all_metric_ids(parsed_evlist, modifier);
if (ret)
goto err_out;
ret = build_combined_expr_ctx(&metric_list, &combined);
if (!ret && combined && hashmap__size(combined->ids)) {
- ret = parse_ids(fake_pmu, combined, /*has_constraint=*/true,
+ ret = parse_ids(fake_pmu, combined, /*modifier=*/NULL,
+ /*has_constraint=*/true,
&combined_evlist);
}
if (combined)
if (n->evlist == NULL)
continue;
+ if ((!m->modifier && n->modifier) ||
+ (m->modifier && !n->modifier) ||
+ (m->modifier && n->modifier &&
+ strcmp(m->modifier, n->modifier)))
+ continue;
+
if (expr__subset_of_ids(n->pctx, m->pctx)) {
pr_debug("Events in '%s' fully contained within '%s'\n",
m->metric_name, n->metric_name);
}
}
if (!metric_evlist) {
- ret = parse_ids(fake_pmu, m->pctx, m->has_constraint,
- &m->evlist);
+ ret = parse_ids(fake_pmu, m->pctx, m->modifier,
+ m->has_constraint, &m->evlist);
if (ret)
goto out;
expr->metric_refs = m->metric_refs;
m->metric_refs = NULL;
expr->metric_expr = m->metric_expr;
- expr->metric_name = m->metric_name;
+ if (m->modifier) {
+ char *tmp;
+
+ if (asprintf(&tmp, "%s:%s", m->metric_name, m->modifier) < 0)
+ expr->metric_name = NULL;
+ else
+ expr->metric_name = tmp;
+ } else
+ expr->metric_name = strdup(m->metric_name);
+
+ if (!expr->metric_name) {
+ ret = -ENOMEM;
+ free(metric_events);
+ goto out;
+ }
expr->metric_unit = m->metric_unit;
expr->metric_events = metric_events;
expr->runtime = m->pctx->runtime;
return -ENOMEM;
new_expr->metric_expr = old_expr->metric_expr;
- new_expr->metric_name = old_expr->metric_name;
+ new_expr->metric_name = strdup(old_expr->metric_name);
+ if (!new_expr->metric_name)
+ return -ENOMEM;
+
new_expr->metric_unit = old_expr->metric_unit;
new_expr->runtime = old_expr->runtime;